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:
Thu Aug 15 10:09:57 2013 +0000
Parent:
8:b48bb4df9319
Child:
10:5afa438c12fd
Commit message:
moved GlobalAssigns.h content to cc3000_common.h
; further code cleanup

Changed in this revision

GlobalAssigns.h Show diff for this revision Revisions of this file
KL25Z_irq_proirities.lib 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
cc3000_common.h Show annotated file Show diff for this revision Revisions of this file
cc3000_spi_hci.cpp Show annotated file Show diff for this revision Revisions of this file
cc3000_spi_hci.h Show annotated file Show diff for this revision Revisions of this file
wlan.h Show annotated file Show diff for this revision Revisions of this file
--- a/GlobalAssigns.h	Fri Aug 09 12:23:24 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-#ifndef GLOBALPINS_H
-#define GLOBALPINS_H
-
-#include "mbed.h"
-
-// Compiler control
-#define CC3000_UNENCRYPTED_SMART_CONFIG   // No encryption
-//#define CC3000_TINY_DRIVER                // Driver for small memory model CPUs
-
-
-// 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     NVIC_EnableIRQ(PORTA_IRQn);
-#define WLAN_DISABLE_IRQ    NVIC_DisableIRQ(PORTA_IRQn);
-
-#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).
-
-extern DigitalOut  wlan_en;
-extern DigitalOut  wlan_cs;
-extern InterruptIn wlan_int;
-extern SPI wlan;
-
-#endif 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/KL25Z_irq_proirities.lib	Thu Aug 15 10:09:57 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/frankvnk/code/KL25Z_irq_proirities/#d2dd5df65f0e
--- a/cc3000_common.cpp	Fri Aug 09 12:23:24 2013 +0000
+++ b/cc3000_common.cpp	Thu Aug 15 10:09:57 2013 +0000
@@ -34,6 +34,103 @@
 *****************************************************************************/
 
 #include "cc3000_common.h"
+#include "KL25Z_irq_prio.h"
+#include "wlan.h"
+
+//*****************************************************************************
+//                  CC3000 I/O setup and control
+//*****************************************************************************
+
+WLAN_IRQ_PIN_CREATE;
+WLAN_EN_PIN_CREATE;
+WLAN_CS_PIN_CREATE;
+WLAN_SPI_PORT_CREATE;
+
+
+void Init_HostDriver(void)
+{
+    NVIC_SetAllPriority(3);               // Set ALL interrupt priorities to level 3
+    NVIC_SetPriority(SPI0_IRQn, 0x0);     // Wi-Fi SPI interrupt must be higher priority than SysTick
+    NVIC_SetPriority(PORTA_IRQn, 0x1);
+    NVIC_SetPriority(SysTick_IRQn, 0x2);  // SysTick set to lower priority than Wi-Fi SPI bus interrupt
+//    NVIC_SetPriority(ADC0_IRQn, 0x3);     // ADC is the lowest of all
+    // 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_DEASSERT_EN;
+    WLAN_DEASSERT_CS;
+
+    WLAN_SPI_PORT_INIT;
+    WLAN_SPI_SET_FREQ;
+    WLAN_SPI_SET_IRQ_HANDLER;
+    // WLAN On API Implementation
+    wlan_init( CC3000_UsynchCallback,
+               sendWLFWPatch,
+               sendDriverPatch,
+               sendBootLoaderPatch,
+               ReadWlanInterruptPin,
+               WlanInterruptEnable,
+               WlanInterruptDisable,
+               WriteWlanPin);
+}
+
+
+char *sendDriverPatch(unsigned long *Length)
+{
+    *Length = 0;
+    return NULL;
+}
+
+
+char *sendBootLoaderPatch(unsigned long *Length)
+{
+    *Length = 0;
+    return NULL;
+}
+
+
+char *sendWLFWPatch(unsigned long *Length)
+{
+    *Length = 0;
+    return NULL;
+}
+
+
+long ReadWlanInterruptPin(void)
+{
+    return (WLAN_READ_IRQ);
+}
+
+
+void WlanInterruptEnable()
+{
+    WLAN_ENABLE_IRQ;
+}
+
+
+void WlanInterruptDisable()
+{
+    WLAN_DISABLE_IRQ;
+}
+
+
+void WriteWlanPin( unsigned char val )
+{
+    if (val)
+    {
+            WLAN_ASSERT_EN;
+    }
+    else
+    {
+            WLAN_DEASSERT_EN;
+    }
+}
+
+
+//*****************************************************************************
+//                  STREAMS
+//*****************************************************************************
 
 /**
 * stub function for ASSERT macro
--- a/cc3000_common.h	Fri Aug 09 12:23:24 2013 +0000
+++ b/cc3000_common.h	Thu Aug 15 10:09:57 2013 +0000
@@ -35,7 +35,8 @@
 #ifndef __COMMON_H__
 #define __COMMON_H__
 
-#include "GlobalAssigns.h"
+//#include "GlobalAssigns.h"
+#include "mbed.h"
 #include <errno.h>
 
 //*****************************************************************************
@@ -59,6 +60,105 @@
 #define EFAIL          -1
 #define EERROR          EFAIL
 
+
+//*****************************************************************************
+//              PIN CONTROLS & COMPILE CONTROLS
+//*****************************************************************************
+// Compiler control
+#define CC3000_UNENCRYPTED_SMART_CONFIG   // No encryption
+//#define CC3000_TINY_DRIVER                // Driver for small memory model CPUs
+
+
+// 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     NVIC_EnableIRQ(PORTA_IRQn);
+#define WLAN_DISABLE_IRQ    NVIC_DisableIRQ(PORTA_IRQn);
+
+#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).
+
+extern DigitalOut  wlan_en;
+extern DigitalOut  wlan_cs;
+extern InterruptIn wlan_int;
+extern SPI wlan;
+extern void CC3000_UsynchCallback(long lEventType, char * data, unsigned char length);
+
+
+/** Set basic controls and parameters for the HostDriver.
+* @param  none
+* @return none
+*/
+void Init_HostDriver(void);
+
+/** Return a pointer to the driver patch.
+* Since there is no patch, 0 is returned\n
+* (the patches are taken from the EEPROM and not from the host)\n
+* @param  pointer to the length
+* @return NULL
+*/ 
+char *sendDriverPatch(unsigned long *Length);
+
+/** Return a pointer to the bootloader patch.
+* Since there is no patch, 0 is returned \n
+* (the patches are taken from the EEPROM and not from the host)\n
+* @param  pointer to the length
+* @return NULL
+*/
+char *sendBootLoaderPatch(unsigned long *Length);
+
+/** Return a pointer to the firmware patch.
+* Since there is no patch, 0 is returned\n
+* (the patches are taken from the EEPROM and not from the host)\n
+* @param  pointer to the length
+* @return NULL
+*/
+char *sendWLFWPatch(unsigned long *Length);
+
+/** Read Wlan Interrupt pin.
+* @param  none
+* @return wlan interrup pin level
+*/
+long ReadWlanInterruptPin(void);
+
+/** Enable waln IrQ pin.
+* @param  none
+* @return none
+*/
+void WlanInterruptEnable(void);
+
+/** Disable waln IrQ pin.
+* @param  none
+* @return none
+*/
+void WlanInterruptDisable(void);
+
+/** WriteWlanPin.
+* @param  val (1: enable - 0: disable)
+* @return none
+*/
+void WriteWlanPin( unsigned char val );
+
+
 //*****************************************************************************
 //                  COMMON DEFINES
 //*****************************************************************************
--- a/cc3000_spi_hci.cpp	Fri Aug 09 12:23:24 2013 +0000
+++ b/cc3000_spi_hci.cpp	Thu Aug 15 10:09:57 2013 +0000
@@ -147,8 +147,6 @@
       WLAN_ASSERT_CS;
 
       tSLInformation.WlanInterruptEnable();
-
-      // check for a missing interrupt between the CS assertion and interrupt enable
    }
 
    // Wait until the transaction ends
@@ -177,12 +175,6 @@
 }
 
 
-void SpiReadHeader(void)
-{
-   SpiReadDataSynchronous(sSpiInformation.pRxPacket, 10);
-}
-
-
 long SpiReadDataCont(void)
 {
    long data_to_recv;
@@ -234,33 +226,6 @@
 }
 
 
-void SpiTriggerRxProcessing(void)
-{
-   // Trigger Rx processing
-   tSLInformation.WlanInterruptDisable();
-   WLAN_DEASSERT_CS;
-   // The magic number resides at the end of the TX/RX buffer (1 byte after the allocated size)
-   // If the magic number is overwitten - buffer overrun occurred - we will be stuck here forever!
-   if (sSpiInformation.pRxPacket[CC3000_RX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER)
-   {
-      while (1);
-   }
-   sSpiInformation.ulSpiState = eSPI_STATE_IDLE;
-   sSpiInformation.SPIRxHandler(sSpiInformation.pRxPacket + SPI_HEADER_SIZE);
-}
-
-
-
-void SSIContReadOperation(void)
-{
-   // The header was read - continue with the payload read
-   if (!SpiReadDataCont())
-   {
-      // All the data was read - finalize handling by switching to the task
-      SpiTriggerRxProcessing();
-   }
-}
-
 void WLAN_IRQHandler(void)
 {
    if (sSpiInformation.ulSpiState == eSPI_STATE_POWERUP)
@@ -273,9 +238,26 @@
       sSpiInformation.ulSpiState = eSPI_STATE_READ_IRQ;
       /* IRQ line goes low - acknowledge it */
       WLAN_ASSERT_CS;
-      SpiReadHeader();
+      SpiReadDataSynchronous(sSpiInformation.pRxPacket, 10);
       sSpiInformation.ulSpiState = eSPI_STATE_READ_EOT;
-      SSIContReadOperation();
+
+
+      // The header was read - continue with the payload read
+      if (!SpiReadDataCont())
+      {
+          // All the data was read - finalize handling by switching to the task
+          // Trigger Rx processing
+          tSLInformation.WlanInterruptDisable();
+          WLAN_DEASSERT_CS;
+          // The magic number resides at the end of the TX/RX buffer (1 byte after the allocated size)
+          // If the magic number is overwitten - buffer overrun occurred - we will be stuck here forever!
+          if (sSpiInformation.pRxPacket[CC3000_RX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER)
+              {
+                  while (1);
+              }
+              sSpiInformation.ulSpiState = eSPI_STATE_IDLE;
+              sSpiInformation.SPIRxHandler(sSpiInformation.pRxPacket + SPI_HEADER_SIZE);
+      }
    }
    else if (sSpiInformation.ulSpiState == eSPI_STATE_WRITE_IRQ)
    {
@@ -404,3 +386,5 @@
 }
 
 
+
+
--- a/cc3000_spi_hci.h	Fri Aug 09 12:23:24 2013 +0000
+++ b/cc3000_spi_hci.h	Thu Aug 15 10:09:57 2013 +0000
@@ -125,34 +125,12 @@
 void SpiReadDataSynchronous(unsigned char *data, unsigned short size);
 
 /**
-* Read 5 SPI header bytes and 5 Event Data bytes
-* @param  none
-* @return none
-*/
-void SpiReadHeader(void);
-
-/**
 * Process the received SPI Header and in accordance with it - continue reading the packet
 * @param  None
 * @return 0
 */
 long SpiReadDataCont(void);
 
-/**
-* Trigger RX processing
-* @param  SpiTriggerRxProcessing
-* @return none
-*/
-void SpiTriggerRxProcessing(void);
-
-/**
-* SSIContReadOperation
-* @param  none
-* @return none
-*/
-void SSIContReadOperation(void);
-
-
 // Prototypes for the SPI APIs.
 
 /**
--- a/wlan.h	Fri Aug 09 12:23:24 2013 +0000
+++ b/wlan.h	Thu Aug 15 10:09:57 2013 +0000
@@ -133,14 +133,14 @@
 * @sa       wlan_set_event_mask , wlan_start , wlan_stop 
 * @warning  This function must be called before ANY other wlan driver function
 */
-extern void wlan_init(tWlanCB               sWlanCB,
-                      tFWPatches            sFWPatches,
-                      tDriverPatches        sDriverPatches,
-                      tBootLoaderPatches    sBootLoaderPatches,
-                      tWlanReadInteruptPin  sReadWlanInterruptPin,
-                      tWlanInterruptEnable  sWlanInterruptEnable,
-                      tWlanInterruptDisable sWlanInterruptDisable,
-                      tWriteWlanPin         sWriteWlanPin);
+void wlan_init(tWlanCB               sWlanCB,
+               tFWPatches            sFWPatches,
+               tDriverPatches        sDriverPatches,
+               tBootLoaderPatches    sBootLoaderPatches,
+               tWlanReadInteruptPin  sReadWlanInterruptPin,
+               tWlanInterruptEnable  sWlanInterruptEnable,
+               tWlanInterruptDisable sWlanInterruptDisable,
+               tWriteWlanPin         sWriteWlanPin);
 
 /**
 * Trigger Received event/data processing - called from the SPI library to receive the data