TI's MQTT Demo with freertos CM4F

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cc3100_spi.cpp Source File

cc3100_spi.cpp

00001 /*
00002  * spi.cpp mbed
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 "cli_uart.h"
00040 
00041 #include "osi.h"
00042 #include "portmacro.h"
00043 #include "projdefs.h"
00044 
00045 extern OsiMsgQ_t g_PBQueue; /*Message Queue*/
00046 
00047 namespace mbed_cc3100 {
00048     
00049 P_EVENT_HANDLER   pIraEventHandler = 0;
00050 uint8_t           IntIsMasked;
00051 
00052 cc3100_spi::cc3100_spi(PinName button1_irq, PinName button2_irq, PinName cc3100_irq, PinName cc3100_nHIB, PinName cc3100_cs, SPI cc3100_spi, cc3100_driver &driver)
00053     : _sw1_irq(button1_irq), _sw2_irq(button2_irq), _wlan_irq(cc3100_irq), _wlan_nHIB(cc3100_nHIB), _wlan_cs(cc3100_cs), _wlan_spi(cc3100_spi), _driver(driver)
00054 {
00055 
00056     _wlan_spi.format(8,0);
00057     _wlan_spi.frequency(16000000);
00058     _wlan_irq.rise(this, &cc3100_spi::IntSpiGPIOHandler);      //_SlDrvRxIrqHandler is called from IntSpiGPIOHandler
00059     _sw1_irq.rise(this, &cc3100_spi::buttonHandler_1);
00060     _sw2_irq.rise(this, &cc3100_spi::buttonHandler_2);
00061     _wlan_nHIB = 0;
00062     _wlan_cs = 1;
00063     wait_ms(200);
00064     
00065     
00066 }
00067 
00068 cc3100_spi::~cc3100_spi()
00069 {
00070 
00071 }
00072 
00073 int cc3100_spi::spi_Close(Fd_t fd)
00074 {
00075     
00076     // Disable WLAN Interrupt ...
00077     cc3100_InterruptDisable();
00078     
00079     return NONOS_RET_OK;
00080 }
00081 
00082 void cc3100_spi::button1_InterruptDisable()
00083 {
00084     _sw1_irq.rise(NULL);
00085     wait_ms(1);
00086 }  
00087 
00088 void cc3100_spi::button2_InterruptDisable()
00089 {
00090     _sw2_irq.rise(NULL);
00091     wait_ms(1);
00092 }   
00093 
00094 void cc3100_spi::button1_InterruptEnable()
00095 {    
00096     _sw1_irq.rise(this, &cc3100_spi::buttonHandler_1);
00097 }
00098 
00099 void cc3100_spi::button2_InterruptEnable()
00100 {    
00101     _sw2_irq.rise(this, &cc3100_spi::buttonHandler_2);
00102 }    
00103 
00104 void cc3100_spi::cc3100_InterruptEnable()
00105 {
00106     _wlan_irq.rise(this, &cc3100_spi::IntSpiGPIOHandler);
00107 }
00108 
00109 void cc3100_spi::cc3100_InterruptDisable()
00110 {
00111     _wlan_irq.rise(NULL);
00112 }
00113 
00114 void cc3100_spi::CC3100_disable()
00115 {
00116     _wlan_nHIB = 0;
00117 }
00118 
00119 void cc3100_spi::CC3100_enable()
00120 {
00121     
00122     _wlan_nHIB = 1;
00123 }
00124 
00125 Fd_t cc3100_spi::spi_Open(int8_t *ifName, uint32_t flags)
00126 {
00127 
00128     // 50 ms delay
00129     wait_ms(50);
00130 
00131     // Enable WLAN interrupt
00132     cc3100_InterruptEnable();
00133 
00134     return NONOS_RET_OK;
00135 }
00136 
00137 int cc3100_spi::spi_Write(Fd_t fd, uint8_t *pBuff, int len)
00138 {
00139     int len_to_return = len;
00140 
00141     _wlan_cs = 0;
00142 
00143     while(len) {
00144         _wlan_spi.write(*pBuff++);
00145         len--;
00146     }
00147 
00148     _wlan_cs = 1;
00149     
00150     return len_to_return;
00151 }
00152 
00153 int cc3100_spi::spi_Read(Fd_t fd, uint8_t *pBuff, int len)
00154 {
00155     int i = 0;
00156     _wlan_cs = 0;
00157 
00158     for (i = 0; i < len; i++) {
00159         pBuff[i] = _wlan_spi.write(0xFF);
00160     }
00161 
00162     _wlan_cs = 1;
00163     return len;
00164 }
00165 
00166 void cc3100_spi::IntSpiGPIOHandler(void)
00167 {
00168     
00169     if(_wlan_irq){
00170         _driver._SlDrvRxIrqHandler(0);
00171     }        
00172 }
00173 
00174 /*!
00175     \brief register an interrupt handler for the host IRQ
00176 
00177     \param[in]      InterruptHdl    -    pointer to interrupt handler function
00178 
00179     \param[in]      pValue          -    pointer to a memory strcuture that is
00180                     passed to the interrupt handler.
00181 
00182     \return         upon successful registration, the function shall return 0.
00183                     Otherwise, -1 shall be returned
00184 
00185     \sa
00186     \note           If there is already registered interrupt handler, the
00187                     function should overwrite the old handler with the new one
00188     \warning
00189 */
00190 int cc3100_spi::registerInterruptHandler(P_EVENT_HANDLER InterruptHdl , void* pValue)
00191 {
00192 
00193     pIraEventHandler = InterruptHdl;
00194     return 0;
00195 }
00196 
00197 /*!
00198     \brief     Unmasks the Host IRQ
00199 
00200     \param[in]      none
00201 
00202     \return         none
00203 
00204     \warning
00205 */
00206 void cc3100_spi::UnMaskIntHdlr()
00207 {
00208     IntIsMasked = FALSE;
00209 }
00210 
00211 /*!
00212     \brief      Masks the Host IRQ
00213 
00214     \param[in]      none
00215 
00216     \return         none
00217 
00218     \warning
00219 */
00220 void cc3100_spi::MaskIntHdlr()
00221 {
00222     IntIsMasked = TRUE;
00223 }
00224 
00225 /*!
00226     \brief Handles the button press 1 on the MCU 
00227            and updates the queue with relevant action.
00228     
00229     \param none
00230     
00231     \return none
00232  */
00233 void cc3100_spi::buttonHandler_1(void)
00234 {
00235     int32_t rv = 0;
00236     button1_InterruptDisable();
00237     osi_messages var;
00238 
00239     g_publishCount++;
00240 
00241     var = PUSH_BUTTON_1_PRESSED;
00242     
00243     rv = osi_MsgQWrite(&g_PBQueue, &var, OSI_NO_WAIT);
00244     if(rv < 0){
00245         Uart_Write((uint8_t*)"Messsage queue failed\r\n");
00246     }
00247     
00248 }
00249 
00250 /*!
00251     \brief Handles the button press 2 on the MCU 
00252            and updates the queue with relevant action.
00253     
00254     \param none
00255     
00256     \return none
00257  */
00258 void cc3100_spi::buttonHandler_2(void)
00259 {
00260     int32_t rv = 0;
00261     button2_InterruptDisable();
00262     osi_messages var;
00263 
00264     g_publishCount++;
00265 
00266     var = PUSH_BUTTON_2_PRESSED;
00267     
00268     rv = osi_MsgQWrite(&g_PBQueue, &var, OSI_NO_WAIT);
00269     if(rv < 0){
00270         Uart_Write((uint8_t*)"Messsage queue failed\r\n");
00271     }
00272     
00273 }
00274 
00275 }//namespace mbed_cc3100
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283