TI's CC3100 websocket camera demo with Arducam mini ov5642 and freertos. Should work with other M3's. Work in progress test demo.

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