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

cc3000_spi_hci.h

Committer:
frankvnk
Date:
2013-11-29
Revision:
13:e1ab6b5ab826
Parent:
9:8db50def96e5

File content as of revision 13:e1ab6b5ab826:

/*****************************************************************************
*
*  cc3000_spi_hci.h  - CC3000 Host Driver Implementation.
*  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*    Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
*    Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the   
*    distribution.
*
*    Neither the name of Texas Instruments Incorporated nor the names of
*    its contributors may be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/


#ifndef __SPI_H__
#define __SPI_H__

#include "cc3000_common.h"

//*****************************************************************************
//
//! \addtogroup cc3000_spi_hci
//! @{
//
//*****************************************************************************

/** CC3000 SPI & HCI library
*
*/

#ifdef  __cplusplus
extern "C" {
#endif

/* ===========================================================================================
                                              SPI 
   =========================================================================================== */
#define READ                    3
#define WRITE                   1

#define HI(value)               (((value) & 0xFF00) >> 8)
#define LO(value)               ((value) & 0x00FF)

#define SPI_HEADER_SIZE      (5)

#define HEADERS_SIZE_EVNT    (SPI_HEADER_SIZE + 5)

#define  eSPI_STATE_POWERUP             (0)
#define  eSPI_STATE_INITIALIZED         (1)
#define  eSPI_STATE_IDLE                (2)
#define  eSPI_STATE_WRITE_IRQ           (3)
#define  eSPI_STATE_WRITE_FIRST_PORTION (4)
#define  eSPI_STATE_WRITE_EOT           (5)
#define  eSPI_STATE_READ_IRQ            (6)
#define  eSPI_STATE_READ_FIRST_PORTION  (7)
#define  eSPI_STATE_READ_EOT            (8)

// The magic number that resides at the end of the TX/RX buffer (1 byte after the allocated size)
// for the purpose of detection of the overrun. The location of the memory where the magic number
// resides shall never be written. In case it is written - overrun occured and either recevie function
// or send function will be stuck forever.
#define CC3000_BUFFER_MAGIC_NUMBER (0xDE)


typedef void (*gcSpiHandleRx)(void *p);
typedef void (*gcSpiHandleTx)(void);

typedef struct
{
   gcSpiHandleRx  SPIRxHandler;
   unsigned short usTxPacketLength;
   unsigned short usRxPacketLength;
   unsigned long  ulSpiState;
   unsigned char *pTxPacket;
   unsigned char *pRxPacket;

}tSpiInformation;

extern unsigned char wlan_tx_buffer[];

/**
* First SPI write after powerup (delay needed between SPI header and body)
* @param  pointer to write buffer
* @param  buffer length
* @return none
*/
long SpiFirstWrite(unsigned char *ucBuf, unsigned short usLength);

/**
* Low level SPI write
* @param  pointer to data buffer
* @param  number of bytes
* @return none
*/
void SpiWriteDataSynchronous(unsigned char *data, unsigned short size);

/**
* Low level SPI read
* @param  pointer to data buffer
* @param  number of bytes
* @return none
*/
void SpiReadDataSynchronous(unsigned char *data, unsigned short size);

/**
* Process the received SPI Header and in accordance with it - continue reading the packet
* @param  None
* @return 0
*/
long SpiReadDataCont(void);

// Prototypes for the SPI APIs.

/**
* Open the SPI interface
* @param  pointer to RX handle
* @return none
*/
extern void SpiOpen(gcSpiHandleRx pfRxHandler);

/**
* Close the SPI interface
* @param  none
* @return none
*/
extern void SpiClose(void);

/**
* SPI Write function
* @param  pointer to write buffer
* @param  buffer length
* @return 0
*/
extern long SpiWrite(unsigned char *pUserBuffer, unsigned short usLength);

/**
* SPI interrupt Handler.
* The external WLAN device asserts the IRQ line when data is ready.\n
* The host CPU needs to acknowledges the IRQ by asserting CS.\n
* @param  none
* @return none
*/
extern void WLAN_IRQHandler(void);


/* ===========================================================================================
                                              HCI 
   =========================================================================================== */

#define SL_PATCH_PORTION_SIZE                        (1000)

#define SIMPLE_LINK_HCI_CMND_HEADER_SIZE             (4)
#define HEADERS_SIZE_CMD                             (SPI_HEADER_SIZE + SIMPLE_LINK_HCI_CMND_HEADER_SIZE)
#define SIMPLE_LINK_HCI_DATA_CMND_HEADER_SIZE        (5)
#define SIMPLE_LINK_HCI_DATA_HEADER_SIZE             (5)
#define SIMPLE_LINK_HCI_PATCH_HEADER_SIZE            (2)

// Values that can be used as HCI Commands and HCI Packet header defines
#define  HCI_TYPE_CMND          0x1
#define  HCI_TYPE_DATA          0x2
#define  HCI_TYPE_PATCH         0x3
#define  HCI_TYPE_EVNT          0x4


#define HCI_EVENT_PATCHES_DRV_REQ             (1)
#define HCI_EVENT_PATCHES_FW_REQ              (2)
#define HCI_EVENT_PATCHES_BOOTLOAD_REQ        (3)


#define  HCI_CMND_WLAN_BASE                              (0x0000)
#define  HCI_CMND_WLAN_CONNECT                            0x0001
#define  HCI_CMND_WLAN_DISCONNECT                         0x0002
#define  HCI_CMND_WLAN_IOCTL_SET_SCANPARAM                0x0003
#define  HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY        0x0004
#define  HCI_CMND_WLAN_IOCTL_ADD_PROFILE                  0x0005
#define  HCI_CMND_WLAN_IOCTL_DEL_PROFILE                  0x0006
#define  HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS             0x0007
#define  HCI_CMND_EVENT_MASK                              0x0008
#define  HCI_CMND_WLAN_IOCTL_STATUSGET                    0x0009
#define  HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START          0x000A
#define  HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP           0x000B
#define  HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX     0x000C
#define  HCI_CMND_WLAN_CONFIGURE_PATCH                    0x000D


#define  HCI_CMND_SOCKET_BASE          0x1000
#define  HCI_CMND_SOCKET               0x1001
#define  HCI_CMND_BIND                 0x1002
#define  HCI_CMND_RECV                 0x1004
#define  HCI_CMND_ACCEPT               0x1005
#define  HCI_CMND_LISTEN               0x1006
#define  HCI_CMND_CONNECT              0x1007
#define  HCI_CMND_BSD_SELECT           0x1008
#define  HCI_CMND_SETSOCKOPT           0x1009
#define  HCI_CMND_GETSOCKOPT           0x100A
#define  HCI_CMND_CLOSE_SOCKET         0x100B
#define  HCI_CMND_RECVFROM             0x100D
#define  HCI_CMND_GETHOSTNAME          0x1010
#define  HCI_CMND_MDNS_ADVERTISE       0x1011


#define HCI_DATA_BASE                                0x80

#define HCI_CMND_SEND                               (0x01 + HCI_DATA_BASE)
#define HCI_CMND_SENDTO                             (0x03 + HCI_DATA_BASE)
#define HCI_DATA_BSD_RECVFROM                       (0x04 + HCI_DATA_BASE)
#define HCI_DATA_BSD_RECV                           (0x05 + HCI_DATA_BASE)


#define HCI_CMND_NVMEM_CBASE        (0x0200)


#define HCI_CMND_NVMEM_CREATE_ENTRY (0x0203)
#define HCI_CMND_NVMEM_SWAP_ENTRY   (0x0205)
#define HCI_CMND_NVMEM_READ         (0x0201)
#define HCI_CMND_NVMEM_WRITE        (0x0090)
#define HCI_CMND_NVMEM_WRITE_PATCH  (0x0204)
#define HCI_CMND_READ_SP_VERSION    (0x0207)

#define  HCI_CMND_READ_BUFFER_SIZE  0x400B
#define  HCI_CMND_SIMPLE_LINK_START 0x4000

#define HCI_CMND_NETAPP_BASE        0x2000

#define HCI_NETAPP_DHCP                    (0x0001 + HCI_CMND_NETAPP_BASE)
#define HCI_NETAPP_PING_SEND               (0x0002 + HCI_CMND_NETAPP_BASE)
#define HCI_NETAPP_PING_REPORT             (0x0003 + HCI_CMND_NETAPP_BASE)
#define HCI_NETAPP_PING_STOP               (0x0004 + HCI_CMND_NETAPP_BASE)
#define HCI_NETAPP_IPCONFIG                (0x0005 + HCI_CMND_NETAPP_BASE)
#define HCI_NETAPP_ARP_FLUSH               (0x0006 + HCI_CMND_NETAPP_BASE)
#define HCI_NETAPP_SET_DEBUG_LEVEL         (0x0008 + HCI_CMND_NETAPP_BASE)
#define HCI_NETAPP_SET_TIMERS              (0x0009 + HCI_CMND_NETAPP_BASE)

// Values that can be used as HCI Events defines
#define  HCI_EVNT_WLAN_BASE     0x0000
#define  HCI_EVNT_WLAN_CONNECT  0x0001
#define  HCI_EVNT_WLAN_DISCONNECT \
                                0x0002
#define  HCI_EVNT_WLAN_IOCTL_ADD_PROFILE  \
                                0x0005


#define  HCI_EVNT_SOCKET              HCI_CMND_SOCKET
#define  HCI_EVNT_BIND                HCI_CMND_BIND
#define  HCI_EVNT_RECV                HCI_CMND_RECV
#define  HCI_EVNT_ACCEPT              HCI_CMND_ACCEPT
#define  HCI_EVNT_LISTEN              HCI_CMND_LISTEN
#define  HCI_EVNT_CONNECT             HCI_CMND_CONNECT
#define  HCI_EVNT_SELECT              HCI_CMND_BSD_SELECT
#define  HCI_EVNT_CLOSE_SOCKET        HCI_CMND_CLOSE_SOCKET
#define  HCI_EVNT_RECVFROM            HCI_CMND_RECVFROM
#define  HCI_EVNT_SETSOCKOPT          HCI_CMND_SETSOCKOPT
#define  HCI_EVNT_GETSOCKOPT          HCI_CMND_GETSOCKOPT
#define  HCI_EVNT_BSD_GETHOSTBYNAME   HCI_CMND_GETHOSTNAME
#define  HCI_EVNT_MDNS_ADVERTISE      HCI_CMND_MDNS_ADVERTISE
 
#define  HCI_EVNT_SEND                0x1003
#define  HCI_EVNT_WRITE               0x100E
#define  HCI_EVNT_SENDTO              0x100F

#define HCI_EVNT_PATCHES_REQ          0x1000

#define HCI_EVNT_UNSOL_BASE           0x4000

#define HCI_EVNT_WLAN_UNSOL_BASE     (0x8000)

#define HCI_EVNT_WLAN_UNSOL_CONNECT             (0x0001 + HCI_EVNT_WLAN_UNSOL_BASE)
#define HCI_EVNT_WLAN_UNSOL_DISCONNECT          (0x0002 + HCI_EVNT_WLAN_UNSOL_BASE)
#define HCI_EVNT_WLAN_UNSOL_INIT                (0x0004 + HCI_EVNT_WLAN_UNSOL_BASE)
#define HCI_EVNT_WLAN_TX_COMPLETE               (0x0008 + HCI_EVNT_WLAN_UNSOL_BASE)
#define HCI_EVNT_WLAN_UNSOL_DHCP                (0x0010 + HCI_EVNT_WLAN_UNSOL_BASE)
#define HCI_EVNT_WLAN_ASYNC_PING_REPORT         (0x0040 + HCI_EVNT_WLAN_UNSOL_BASE)
#define HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE  (0x0080 + HCI_EVNT_WLAN_UNSOL_BASE)
#define HCI_EVNT_WLAN_KEEPALIVE                 (0x0200  + HCI_EVNT_WLAN_UNSOL_BASE)
#define    HCI_EVNT_BSD_TCP_CLOSE_WAIT          (0x0800 + HCI_EVNT_WLAN_UNSOL_BASE)

#define HCI_EVNT_DATA_UNSOL_FREE_BUFF \
                                0x4100

#define HCI_EVNT_NVMEM_CREATE_ENTRY \
                                HCI_CMND_NVMEM_CREATE_ENTRY
#define HCI_EVNT_NVMEM_SWAP_ENTRY HCI_CMND_NVMEM_SWAP_ENTRY

#define HCI_EVNT_NVMEM_READ     HCI_CMND_NVMEM_READ
#define HCI_EVNT_NVMEM_WRITE    (0x0202)

#define HCI_EVNT_READ_SP_VERSION      \
                HCI_CMND_READ_SP_VERSION

#define  HCI_EVNT_INPROGRESS    0xFFFF


#define HCI_DATA_RECVFROM       0x84
#define HCI_DATA_RECV           0x85
#define HCI_DATA_NVMEM          0x91

#define HCI_EVENT_CC3000_CAN_SHUT_DOWN 0x99

// Prototypes for the structures for HCI APIs.
#define HCI_DATA_HEADER_SIZE        (5)
#define HCI_EVENT_HEADER_SIZE       (5)
#define HCI_DATA_CMD_HEADER_SIZE    (5)
#define HCI_PATCH_HEADER_SIZE       (6)

#define HCI_PACKET_TYPE_OFFSET      (0)
#define HCI_PACKET_ARGSIZE_OFFSET   (2)
#define HCI_PACKET_LENGTH_OFFSET    (3)


#define HCI_EVENT_OPCODE_OFFSET     (1)
#define HCI_EVENT_LENGTH_OFFSET     (3)
#define HCI_EVENT_STATUS_OFFSET     (4)
#define HCI_DATA_LENGTH_OFFSET      (3)
  
  


// Prototypes for the HCI APIs.

/**
* Send a HCI command.
* @param  usOpcode     command operation code
* @param  pucBuff      pointer to the command's arguments buffer
* @param  ucArgsLength length of the arguments
* @return              none
*/
extern unsigned short hci_command_send(unsigned short usOpcode, 
                                   unsigned char *ucArgs,
                                   unsigned char ucArgsLength);
 

/**
* Send HCI data
* @param  usOpcode        command operation code
* @param  ucArgs          pointer to the command's arguments buffer
* @param  usArgsLength    length of the arguments
* @param  ucTail          pointer to the data buffer
* @param  usTailLength    buffer length
* @return none
*/
extern long hci_data_send(unsigned char ucOpcode,
                                      unsigned char *ucArgs,
                                      unsigned short usArgsLength,
                                      unsigned short usDataLength,
                                      const unsigned char *ucTail,
                                      unsigned short usTailLength);


/**
* Prepare HCI header and send HCI data
* @param  usOpcode      command operation code
* @param  pucBuff       pointer to the data buffer
* @param  ucArgsLength  arguments length
* @param  ucDataLength  data length
* @return none
*/
extern void hci_data_command_send(unsigned short usOpcode,
                                  unsigned char *pucBuff,
                                  unsigned char ucArgsLength,
                                  unsigned short ucDataLength);


/**
* Prepare HCI header and send HCI patch
* @param  usOpcode      command operation code
* @param  pucBuff       pointer to the command's arguments buffer
* @param  patch         pointer to patch content buffer 
* @param  usDataLength  data length
* @return none
*/
extern void hci_patch_send(unsigned char ucOpcode, unsigned char *pucBuff, char *patch, unsigned short usDataLength);

#ifdef  __cplusplus
}
#endif // __cplusplus

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

#endif