Driver for CC3000 Wi-Fi module

Dependencies:   NVIC_set_all_priorities

Dependents:   CC3000_Simple_Socket Wi-Go_IOT_Demo

Information

The current code has been reworked to a full object oriented application and contains an mbed socket compatible API.

CC3000 Wi-Fi module library

Info

This is the low level driver for TI's SimpleLink CC3000 device.
Port from Avnet's Wi-Go KEIL code (based on TI's CC3000 code).
Special thanks to Jim Carver from Avnet for providing the Wi-Go board and for his assistance.

Differences with TI's original code

The code functionality stays exactly the same.
In order to make it easier to use the code, following changes were made :

  • Addition of a tool to shift all IRQ priorities to a lower level since it is very important to keep the SPI handler at the highest system priority, the WLAN interrupt the second highest and all other system interrupts at a lower priority, so their handlers can be preempted by the CC3000 interrupts.
  • Addition of low level I/O controls and conditional compiler controls in cc3000_common.h.
  • CC3000 initialisation, pin declarations, SPI and WLAN irq priorities are set in Init_HostDriver , we need to call this function at the start of the main function.
  • The SPI and HCI code are joined into one file.
  • The include list has been rearranged - Only #include "wlan.h" is needed in the user API.
  • Part of the CC3000's user eeprom memory is used to store additional info (52 bytes in NVMEM_USER_FILE_1):
# bytesDescriptionInfo
1First time config parameterUseful when connecting
2Firmware updater versionused with the Firmware update tool
2Service Pack versionused with the Firmware update tool
3Driver Versionused with the Firmware update tool
3Firmware Versionused with the Firmware update tool
1CIK validation (Client Interface Key)
40CIK data (Client Interface Key)used with the exosite

Using the Library

A user API is needed to access the CC3000 functions.
Examples:

Using the library with other processors

cc3000_common.cpp loads the irq tool for all targets:
All current mbed targets are supported by this library.

#include "NVIC_set_all_priorities.h"


All low level settings that need to change are available in cc3000_common.h

//*****************************************************************************
//              PIN CONTROLS & COMPILE CONTROLS
//*****************************************************************************
// Compiler control
#define CC3000_UNENCRYPTED_SMART_CONFIG   // No encryption
//#define CC3000_TINY_DRIVER                // Driver for small memory model CPUs

//Interrupt controls
#define NVIC_ALL_IRQ        NVIC_set_all_irq_priorities(3);         // Set ALL interrupt priorities to level 3
#define NVIC_SPI_IRQ        NVIC_SetPriority(SPI0_IRQn, 0x0);       // Wi-Fi SPI interrupt must be higher priority than SysTick
#define NVIC_PORT_IRQ       NVIC_SetPriority(PORTA_IRQn, 0x1);
#define NVIC_SYSTICK_IRQ    NVIC_SetPriority(SysTick_IRQn, 0x2);    // SysTick set to lower priority than Wi-Fi SPI bus interrupt
//#define NVIC_ADC_IRQ        NVIC_SetPriority(ADC0_IRQn, 0x3);       // ADC is the lowest of all

// Wlan controls
#define WLAN_ISF_PCR        PORTA->PCR[16]
#define WLAN_ISF_ISFR       PORTA->ISFR
#define WLAN_ISF_MASK       (1<<16)

#define WLAN_ASSERT_CS      wlan_cs = 0;   //CS : active low
#define WLAN_DEASSERT_CS    wlan_cs = 1;

#define WLAN_ASSERT_EN      wlan_en = 1;   //EN : active high
#define WLAN_DEASSERT_EN    wlan_en = 0;

#define WLAN_READ_IRQ       wlan_int

#define WLAN_ENABLE_IRQ     wlan_int.fall(&WLAN_IRQHandler);
#define WLAN_DISABLE_IRQ    wlan_int.fall(NULL);

#define WLAN_IRQ_PIN_CREATE         InterruptIn wlan_int (PTA16);
#define WLAN_EN_PIN_CREATE          DigitalOut  wlan_en  (PTA13);
#define WLAN_CS_PIN_CREATE          DigitalOut  wlan_cs  (PTD0);
#define WLAN_SPI_PORT_CREATE        SPI wlan(PTD2, PTD3, PTC5); // mosi, miso, sclk

#define WLAN_SPI_PORT_INIT          wlan.format(8,1);
#define WLAN_SPI_SET_FREQ           wlan.frequency(12000000);
#define WLAN_SPI_SET_IRQ_HANDLER    wlan_int.fall(&WLAN_IRQHandler);

#define WLAN_SPI_WRITE              wlan.write(*data++);
#define WLAN_SPI_READ               wlan.write(0x03);          // !! DO NOT MODIFY the 0x03 parameter (CC3000 will not respond).

API documentation

Due to a little problem with the links on the mbed site, the API documentation is not directly accessible (will be solved in a next release).
Currently, it is only accessible by adding modules.html to the API doc link: http://mbed.org/users/frankvnk/code/CC3000_Hostdriver/docs/tip/modules.html

Files at this revision

API Documentation at this revision

Comitter:
frankvnk
Date:
Mon Jul 08 13:18:12 2013 +0000
Parent:
1:bbcaf0b2f367
Child:
3:3818c9c7b14e
Commit message:
See history.h

Changed in this revision

CC3000_spi.cpp Show annotated file Show diff for this revision Revisions of this file
cc3000.cpp Show annotated file Show diff for this revision Revisions of this file
cc3000.h Show annotated file Show diff for this revision Revisions of this file
cc3000_common.cpp Show annotated file Show diff for this revision Revisions of this file
evnt_handler.cpp Show annotated file Show diff for this revision Revisions of this file
hci.cpp Show annotated file Show diff for this revision Revisions of this file
wlan.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CC3000_spi.cpp	Thu Jul 04 08:10:21 2013 +0000
+++ b/CC3000_spi.cpp	Mon Jul 08 13:18:12 2013 +0000
@@ -186,7 +186,7 @@
    else
    {
 
-printf("Write\n");
+//printf("Write\n");
       // We need to prevent here race that can occur in case 2 back to back packets are sent to the
       // device, so the state will move to IDLE and once again to not IDLE due to IRQ
       tSLInformation.WlanInterruptDisable();
@@ -202,14 +202,14 @@
 
       tSLInformation.WlanInterruptEnable();
 
-      // check for a missing interrupt between the CS assertion and iterrupt enable
-/*      if (tSLInformation.ReadWlanInterruptPin() == 0)
+      // check for a missing interrupt between the CS assertion and interrupt enable
+      if (tSLInformation.ReadWlanInterruptPin() == 0)
       {
-printf("INT missed\n");
+//printf("INT missed\n");
           SpiWriteDataSynchronous(sSpiInformation.pTxPacket, sSpiInformation.usTxPacketLength);
           sSpiInformation.ulSpiState = eSPI_STATE_IDLE;
           wlan_cs = 1;
-      }*/
+      }
    }
 
    // Due to the fact that we are currently implementing a blocking situation, wait until the transaction ends
@@ -236,15 +236,13 @@
 //*****************************************************************************
 void SpiWriteDataSynchronous(unsigned char *data, unsigned short size)
 {
-printf("W %02i : ",size);
-   while (size)
+//printf("W %02i : ",size);
+   for (; size > 0 ; size--)
    {
-printf("%02X ",*data);
-      wlan.write(*data);
-      size--;
-      data++;
+//printf("%02X ",*data);
+       wlan.write(*data++);
    }
-printf("\n");
+//printf("\n");
 }
 
 //*****************************************************************************
@@ -262,14 +260,13 @@
 void SpiReadDataSynchronous(unsigned char *data, unsigned short size)
 {
    long i = 0;
-//   unsigned char *data_to_send = tSpiReadHeader;
-printf("R %02i : ",size);
-   for (i = 0; i < size; i ++)
+//printf("R %02i : ",size);
+   for (i = 0; i < size; i++)
    {
       data[i] = wlan.write(READ);
-printf("%02X ",data[i]);
+//printf("%02X ",data[i]);
    }
-printf("\n");
+//printf("\n");
 }
 
 //*****************************************************************************
@@ -440,9 +437,10 @@
 void WLAN_IRQHandler(void)
 {
    // Clear pending interrupt
-   WLAN_ISF_PCR  |= PORT_PCR_ISF_MASK;
-   WLAN_ISF_ISFR |= WLAN_ISF_MASK;
-
+//    NVIC_ClearPendingIRQ(PORTA_IRQn);
+//    WLAN_ISF_PCR  |= PORT_PCR_ISF_MASK;
+//    WLAN_ISF_ISFR |= WLAN_ISF_MASK;
+//printf("IRQ\n");
    if (sSpiInformation.ulSpiState == eSPI_STATE_POWERUP)
    {
       // Inform HCI Layer that IRQ occured after powerup
--- a/cc3000.cpp	Thu Jul 04 08:10:21 2013 +0000
+++ b/cc3000.cpp	Mon Jul 08 13:18:12 2013 +0000
@@ -39,10 +39,9 @@
 DigitalOut  wlan_en  (PIN_WLAN_EN);
 DigitalOut  wlan_cs  (PIN_WLAN_CS);
 SPI wlan(PIN_MOSI, PIN_MISO, PIN_SCLK); // mosi, miso, sclk
-//Ticker ueh_check;
-//int ueh_enable;
 
-//extern int ueh_enable;
+extern int ueh_enable;
+
 long ulSocket;
 
 /** \brief Indicates whether the Smart Config Process has finished */
@@ -171,9 +170,8 @@
 //*****************************************************************************
 int initDriver(unsigned short cRequestPatch)
 {
-//    ueh_enable = 0;
-    
     // WLAN On API Implementation
+//ueh_enable = 0;
     wlan_init( CC3000_UsynchCallback,
                sendWLFWPatch,
                sendDriverPatch,
@@ -183,10 +181,10 @@
                WlanInterruptDisable,
                WriteWlanPin);
     wait_us(450);
-printf("\nWlan init done\n");    
+//printf("\nWlan init done\n");    
     // Trigger a WLAN device
     wlan_start(cRequestPatch);
-printf("\nWlan start done\n");    
+//printf("\nWlan start done\n");    
     wlan_smart_config_set_prefix((char *)aucCC3000_prefix);
     wlan_ioctl_set_connection_policy(0, 0, 0);  
     wlan_ioctl_del_profile(255);
@@ -198,7 +196,7 @@
     
     // Check unsolicited events
     hci_unsolicited_event_handler();
-//    ueh_enable = 1;
+//ueh_enable = 1;
     return(0);
 }
 
@@ -320,18 +318,26 @@
 //*****************************************************************************
 void Init_HostDriver(void)
 {
-//    ueh_enable = 0;
-//    ueh_check.attach(&SysTick_Handler, 0.5);
-    //Initial state for wlan module : EN = 0 (disabled) and CS = 1 (not selected).
+    //Set Port A IRQ priority to a higher state
+//    NVIC_DisableIRQ(PORTA_IRQn); //__disable_irq();
+//    NVIC_SetPriority(PORTA_IRQn, 0);
+    //Ensure PTA4 pin (NMI input) is configured for GPIO output function and not NMI
+//    PORTA->PCR[4]  = PORT_PCR_MUX(1);    // GPIO is alt1 function for PTA4 pin
+//    PORTA->PCR[4] |= PORT_PCR_IRQC(0);   // Disable interrupts on PTA4 pin
+//    NVIC_EnableIRQ(PORTA_IRQn); //__enable_irq();
 
     // Clear pending interrupt
     WLAN_ISF_PCR  |= PORT_PCR_ISF_MASK;
     WLAN_ISF_ISFR |= WLAN_ISF_MASK;
+    //Initial state for wlan module : EN = 0 (disabled) and CS = 1 (not selected).
     wlan_en = 0;
     wlan_cs = 1;
 
     wlan.format(8,1);
     wlan.frequency(1000000);
+    wlan_int.fall(&WLAN_IRQHandler);
+//    NVIC_DisableIRQ(PORTA_IRQn);
+
 }
 
 //*****************************************************************************
@@ -347,7 +353,6 @@
 //*****************************************************************************
 long ReadWlanInterruptPin(void)
 {
-  //printf("GPIOA= %x\n", GPIOA_PDIR); 
     return (wlan_int);
 }
 
@@ -365,8 +370,9 @@
 void WlanInterruptEnable()
 {
 //printf("IE\n");
-   wlan_int.fall(&WLAN_IRQHandler);
-   wlan_int.mode(PullUp);
+//   wlan_int.fall(&WLAN_IRQHandler);
+//   wlan_int.mode(PullUp);
+    NVIC_EnableIRQ(PORTA_IRQn);
 }
 
 //*****************************************************************************
@@ -382,11 +388,12 @@
 //*****************************************************************************
 void WlanInterruptDisable()
 {
-   wlan_int.fall(NULL);
+NVIC_DisableIRQ(PORTA_IRQn);
+//    NVIC_ClearPendingIRQ(PORTA_IRQn);
+//    wlan_int.fall(NULL);
    // Clear pending interrupt
 //   WLAN_ISF_PCR  |= PORT_PCR_ISF_MASK;
 //   WLAN_ISF_ISFR |= WLAN_ISF_MASK;
-//printf("ID\n");
 }
 
 
@@ -405,23 +412,10 @@
 {
     if (val)
     {
-      //printf("LAN Enable\n");  
             wlan_en = 1;
     }
     else
     {
-      //printf("LAN Disable\n");  
             wlan_en = 0;
     }
 }
-
-/*void SysTick_Handler(void)
-{
-    if(ueh_enable) hci_unsolicited_event_handler();
-}*/
-
-
-
-
-
-
--- a/cc3000.h	Thu Jul 04 08:10:21 2013 +0000
+++ b/cc3000.h	Mon Jul 08 13:18:12 2013 +0000
@@ -49,7 +49,6 @@
 #include "hci.h"
 #include "cc3000.h"
 
-
 /** CC3000 Functions
 *
 */
--- a/cc3000_common.cpp	Thu Jul 04 08:10:21 2013 +0000
+++ b/cc3000_common.cpp	Mon Jul 08 13:18:12 2013 +0000
@@ -60,8 +60,7 @@
 //!  @brief stub function for ASSERT macro
 //
 //*****************************************************************************
-void
-__error__(char *pcFilename, unsigned long ulLine)
+void __error__(char *pcFilename, unsigned long ulLine)
 {
     //TODO: create function
 }
--- a/evnt_handler.cpp	Thu Jul 04 08:10:21 2013 +0000
+++ b/evnt_handler.cpp	Mon Jul 08 13:18:12 2013 +0000
@@ -181,7 +181,6 @@
         {
 printf("HCI_EVENT_PATCHES_BOOTLOAD_REQ\n");
             patch = tSLInformation.sBootLoaderPatches(&ucLength);
-            
             if (patch)
             {
                 hci_patch_send(HCI_EVENT_PATCHES_BOOTLOAD_REQ, tSLInformation.pucTxCommandBuffer, patch, ucLength);
@@ -228,12 +227,15 @@
         if (tSLInformation.usEventOrDataReceived != 0)
         {                
             pucReceivedData = (tSLInformation.pucReceivedData);
+//printf("pucReceivedData : %04X\n",*pucReceivedData);
 
             if (*pucReceivedData == HCI_TYPE_EVNT)
             {
                 // Event Received
+//printf("STREAM - usReceivedEventOpcode : %04X\n",usReceivedEventOpcode);
                 STREAM_TO_UINT16((char *)pucReceivedData, HCI_EVENT_OPCODE_OFFSET,usReceivedEventOpcode);
                 pucReceivedParams = pucReceivedData + HCI_EVENT_HEADER_SIZE;        
+//printf("STREAM - usReceivedEventOpcode : %04X\n",usReceivedEventOpcode);
                 RecvParams = pucReceivedParams;
                 RetParams = (unsigned char *)pRetParams;
                 
@@ -409,6 +411,8 @@
     
                     }
                 }
+//printf("  REO : %04X\n",usReceivedEventOpcode);
+//printf("RxREO : %04X\n",tSLInformation.usRxEventOpcode);
                 
                 if (usReceivedEventOpcode == tSLInformation.usRxEventOpcode)
                 {
@@ -439,8 +443,7 @@
             
             SpiResumeSpi();
             
-            // Since we are going to TX - we need to handle this event after the 
-            // ResumeSPi since we need interrupts
+            // Since we are going to TX - we need to handle this event after the ResumeSPi since we need interrupts
             if ((*pucReceivedData == HCI_TYPE_EVNT) && (usReceivedEventOpcode == HCI_EVNT_PATCHES_REQ))
             {
                 hci_unsol_handle_patch_request((char *)pucReceivedData);
@@ -450,6 +453,24 @@
                 return NULL;
             }    
         }
+
+/*if(*((char *)(pucReceivedData) + HCI_EVENT_HEADER_SIZE) == HCI_EVENT_PATCHES_FW_REQ)
+{
+    printf("ERROR\n");
+    int wli = wlan_int;
+    printf("tSLInformation.usRxEventOpcode : %04X\n",tSLInformation.usRxEventOpcode);
+    printf("tSLInformation.usRxDataPending : %u\n",tSLInformation.usRxDataPending);
+    printf("WLAN int pin : %u\n",wli);
+    printf("HCI_EVENT_HEADER_SIZE    : %02X\n",HCI_EVENT_HEADER_SIZE);
+    printf("HCI_EVENT_PATCHES_FW_REQ : %02X\n",HCI_EVENT_PATCHES_FW_REQ);
+    printf("HCI_TYPE_EVNT            : %02X\n",HCI_TYPE_EVNT);
+    printf("HCI_EVNT_PATCHES_REQ     : %02X\n\n",HCI_EVNT_PATCHES_REQ);
+    
+    printf("*(pucReceivedData + HCI_EVENT_HEADER_SIZE) : %04X\n",*(pucReceivedData + HCI_EVENT_HEADER_SIZE));
+    printf("usReceivedEventOpcode : %04X\n",usReceivedEventOpcode);
+    printf("pucReceivedData : %04X\n",*pucReceivedData);
+    while(1){}
+}*/
 //printf(".");
     }
 }
@@ -754,7 +775,7 @@
     // after the end of current transaction
     tSLInformation.usRxEventOpcode = usOpcode;
     hci_event_handler(pRetParams, 0, 0);
-printf("SLWE EXIT\n");
+//printf("SLWE EXIT\n");
 }
 
 //*****************************************************************************
--- a/hci.cpp	Thu Jul 04 08:10:21 2013 +0000
+++ b/hci.cpp	Mon Jul 08 13:18:12 2013 +0000
@@ -68,7 +68,7 @@
     UINT8_TO_STREAM(stream, ucArgsLength);
     //Update the opcode of the event we will be waiting for
     SpiWrite(pucBuff, ucArgsLength + SIMPLE_LINK_HCI_CMND_HEADER_SIZE);
-    
+
     return(0);
 }
 
@@ -164,17 +164,15 @@
     unsigned char *data_ptr = (pucBuff + SPI_HEADER_SIZE);
     unsigned short usTransLength;
     unsigned char *stream = (pucBuff + SPI_HEADER_SIZE);
-    
+
     UINT8_TO_STREAM(stream, HCI_TYPE_PATCH);
     UINT8_TO_STREAM(stream, ucOpcode);
     stream = UINT16_TO_STREAM(stream, usDataLength + SIMPLE_LINK_HCI_PATCH_HEADER_SIZE);
-    
     if (usDataLength <= SL_PATCH_PORTION_SIZE)
     {
         UINT16_TO_STREAM(stream, usDataLength);
         stream = UINT16_TO_STREAM(stream, usDataLength);
         memcpy((pucBuff + SPI_HEADER_SIZE) + HCI_PATCH_HEADER_SIZE, patch, usDataLength);
-        
         // Update the opcode of the event we will be waiting for
         SpiWrite(pucBuff, usDataLength + HCI_PATCH_HEADER_SIZE);
     }
--- a/wlan.cpp	Thu Jul 04 08:10:21 2013 +0000
+++ b/wlan.cpp	Mon Jul 08 13:18:12 2013 +0000
@@ -258,7 +258,7 @@
     tSLInformation.slTransmitDataError = 0;
     tSLInformation.usEventOrDataReceived = 0;
     tSLInformation.pucReceivedData = 0;
-    
+
     // Allocate the memory for the RX/TX data transactions
     tSLInformation.pucTxCommandBuffer = (unsigned char *)wlan_tx_buffer;