TI's CC3100 host driver and demo. Experimental and a work in progress.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc3100_spi.cpp Source File

cc3100_spi.cpp

00001 /*
00002  * spi.c - msp430f5529 experiment board spi interface implementation
00003  *
00004  * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
00005  *
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *    Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *
00014  *    Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the
00017  *    distribution.
00018  *
00019  *    Neither the name of Texas Instruments Incorporated nor the names of
00020  *    its contributors may be used to endorse or promote products derived
00021  *    from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00026  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  * 
00035 */
00036 
00037 #include "cc3100_simplelink.h"
00038 #include "cc3100_spi.h"
00039 #include "BoardInit.h"
00040 
00041 #if (THIS_BOARD == MBED_BOARD_LPC1768) 
00042     cc3100_spi spi_(p9, p10, p8, SPI(p5, p6, p7));//LPC1768  nHib, irq, cs, mosi, miso, sck 
00043 /*    cc3100_spi spi_(p9, p10, p8, SPI(p11, p12, p13));//LPC1768  nHib, irq, cs, mosi, miso, sck. Alt spi pins */
00044 #elif (THIS_BOARD == ST_MBED_NUCLEOF411)
00045     cc3100_spi spi_(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF411  nHib, irq, cs, mosi, miso, sck 
00046 #elif (THIS_BOARD == EA_MBED_LPC4088)
00047     cc3100_spi spi_(p9, p10, p8, SPI(p5, p6, p7));
00048 #elif (THIS_BOARD == ST_MBED_NUCLEOF103)     
00049     cc3100_spi spi_(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));   
00050 #else    
00051       
00052 #endif  
00053     
00054 P_EVENT_HANDLER             pIraEventHandler = 0; 
00055 uint8_t                         IntIsMasked;
00056 
00057 cc3100_spi::cc3100_spi(PinName cc3100_irq, PinName cc3100_en, PinName cc3100_cs, SPI cc3100_spi)
00058   : _wlan_irq(cc3100_irq), _wlan_nHIB(cc3100_en), _wlan_cs(cc3100_cs), _wlan_spi(cc3100_spi){
00059 
00060     _wlan_spi.format(8,1);
00061     _wlan_spi.frequency(12000000);
00062     _wlan_irq.rise(&IntSpiGPIOHandler);      //_SlDrvRxIrqHandler is triggered after IntSpiGPIOHandler
00063     _wlan_nHIB = 0;
00064     _wlan_cs = 1;
00065     wait_ms(200);
00066 }
00067 
00068 cc3100_spi::~cc3100_spi() {
00069 
00070 }
00071 /*
00072 int cc3100_spi::spi_Close(Fd_t fd)
00073 {
00074     // Disable WLAN Interrupt ... 
00075     cc3100_InterruptDisable();
00076 
00077     return NONOS_RET_OK;
00078 }
00079 */
00080 void cc3100_spi::cc3100_InterruptEnable()
00081 {
00082       __enable_irq();
00083 }
00084 
00085 void cc3100_spi::cc3100_InterruptDisable()
00086 { 
00087       __disable_irq();
00088 }
00089 /*
00090 void cc3100_spi::CC3100_disable()
00091 {
00092       _wlan_en = 0;
00093 }
00094 */
00095 /*
00096 void CC3100_enable()
00097 {
00098       _wlan_en = 1;
00099 }
00100 */
00101 /*
00102 Fd_t cc3100_spi::spi_Open(char *ifName, unsigned long flags)
00103 {
00104 
00105     // 50 ms delay 
00106     wait_ms(50);
00107 
00108     // Enable WLAN interrupt 
00109     cc3100_InterruptEnable();
00110 
00111     return NONOS_RET_OK;
00112 }
00113 */
00114 /*
00115 int cc3100_spi::spi_Write(Fd_t fd, unsigned char *pBuff, int len)
00116 {
00117     int len_to_return = len;
00118 
00119     _wlan_cs = 0;
00120     
00121     while(len)
00122    {
00123         _wlan_spi.write(*pBuff++);
00124         len--;
00125    }
00126 
00127     _wlan_cs = 1;
00128 
00129     return len_to_return;
00130 }
00131 
00132 int cc3100_spi::spi_Read(Fd_t fd, unsigned char *pBuff, int len)
00133 {
00134     int i = 0;
00135 
00136     _wlan_cs = 0;
00137     
00138     for (i = 0; i < len; i++)
00139    {
00140         pBuff[i] = _wlan_spi.write(0xFF);
00141    }
00142 
00143     _wlan_cs = 1;
00144 
00145     return len;
00146 }
00147 */
00148 
00149 void IntSpiGPIOHandler(void)
00150 {
00151        switch(spi_._wlan_irq){
00152         
00153         case 1:
00154           if(pIraEventHandler)
00155           {
00156             pIraEventHandler(0);
00157           }
00158         break;
00159     default:
00160         break;
00161     }           
00162 }
00163 
00164 /*!
00165     \brief register an interrupt handler for the host IRQ
00166 
00167     \param[in]      InterruptHdl    -    pointer to interrupt handler function
00168 
00169     \param[in]      pValue          -    pointer to a memory strcuture that is
00170                     passed to the interrupt handler.
00171 
00172     \return         upon successful registration, the function shall return 0.
00173                     Otherwise, -1 shall be returned
00174 
00175     \sa
00176     \note           If there is already registered interrupt handler, the
00177                     function should overwrite the old handler with the new one
00178     \warning
00179 */
00180 int registerInterruptHandler(P_EVENT_HANDLER InterruptHdl , void* pValue)
00181 {
00182 //    printf("Register Interrupt Handler Called\r\n");
00183     pIraEventHandler = InterruptHdl;
00184     return 0;
00185 }
00186 
00187 /*!
00188     \brief     Unmasks the Host IRQ
00189 
00190     \param[in]      none
00191 
00192     \return         none
00193 
00194     \warning
00195 */
00196 void UnMaskIntHdlr()
00197 {
00198     IntIsMasked = FALSE;
00199 }
00200 
00201 /*!
00202     \brief      Masks the Host IRQ
00203 
00204     \param[in]      none
00205 
00206     \return         none
00207 
00208     \warning
00209 */
00210 void MaskIntHdlr()
00211 {
00212     IntIsMasked = TRUE;
00213 }
00214 
00215 void CC3100_disable()
00216 {
00217  
00218      spi_._wlan_nHIB = 0;
00219       
00220 }
00221 
00222 void CC3100_enable()
00223 {
00224        
00225       spi_._wlan_nHIB = 1;
00226         
00227 }
00228 
00229 Fd_t spi_Open(char *ifName, unsigned long flags)
00230 {
00231 
00232     /* 50 ms delay */
00233     wait_ms(50);
00234 
00235     /* Enable WLAN interrupt */
00236     spi_.cc3100_InterruptEnable();
00237 
00238     return NONOS_RET_OK;
00239 }
00240 
00241 int spi_Close(Fd_t fd)
00242 {
00243 
00244     wait_ms(2);
00245     /* Disable WLAN Interrupt ... */
00246     spi_.cc3100_InterruptDisable();
00247 
00248     return NONOS_RET_OK;
00249 }
00250 
00251 int spi_Write(Fd_t fd, unsigned char *pBuff, int len)
00252 {
00253 
00254     int len_to_return = len;
00255 
00256     spi_._wlan_cs = 0;
00257     
00258     wait_us(50);
00259     
00260     while(len)
00261    {
00262         spi_._wlan_spi.write(*pBuff++);
00263         len--;
00264 
00265    }
00266    
00267     spi_._wlan_cs = 1;
00268 
00269     return len_to_return;
00270 }
00271 
00272 int spi_Read(Fd_t fd, unsigned char *pBuff, int len)
00273 {
00274     
00275     spi_._wlan_cs = 0;
00276     
00277     wait_us(50);
00278     
00279     for (int i = 0; i < len; i++){
00280         pBuff[i] = spi_._wlan_spi.write(0xFF);
00281 
00282     }
00283 
00284     spi_._wlan_cs = 1;
00285 
00286     return len;
00287 }
00288 
00289 //}//namespace