Updated to use external spawn.

Fork of simplelink_V2 by David Fletcher

Committer:
dflet
Date:
Sat Jun 06 13:48:29 2015 +0000
Revision:
1:9b68e650b3f6
Parent:
0:1a07906111ec
Oppps

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:1a07906111ec 1 /*
dflet 0:1a07906111ec 2 * spi.cpp mbed
dflet 0:1a07906111ec 3 *
dflet 0:1a07906111ec 4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
dflet 0:1a07906111ec 5 *
dflet 0:1a07906111ec 6 *
dflet 0:1a07906111ec 7 * Redistribution and use in source and binary forms, with or without
dflet 0:1a07906111ec 8 * modification, are permitted provided that the following conditions
dflet 0:1a07906111ec 9 * are met:
dflet 0:1a07906111ec 10 *
dflet 0:1a07906111ec 11 * Redistributions of source code must retain the above copyright
dflet 0:1a07906111ec 12 * notice, this list of conditions and the following disclaimer.
dflet 0:1a07906111ec 13 *
dflet 0:1a07906111ec 14 * Redistributions in binary form must reproduce the above copyright
dflet 0:1a07906111ec 15 * notice, this list of conditions and the following disclaimer in the
dflet 0:1a07906111ec 16 * documentation and/or other materials provided with the
dflet 0:1a07906111ec 17 * distribution.
dflet 0:1a07906111ec 18 *
dflet 0:1a07906111ec 19 * Neither the name of Texas Instruments Incorporated nor the names of
dflet 0:1a07906111ec 20 * its contributors may be used to endorse or promote products derived
dflet 0:1a07906111ec 21 * from this software without specific prior written permission.
dflet 0:1a07906111ec 22 *
dflet 0:1a07906111ec 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
dflet 0:1a07906111ec 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
dflet 0:1a07906111ec 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
dflet 0:1a07906111ec 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
dflet 0:1a07906111ec 27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dflet 0:1a07906111ec 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
dflet 0:1a07906111ec 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
dflet 0:1a07906111ec 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
dflet 0:1a07906111ec 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
dflet 0:1a07906111ec 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dflet 0:1a07906111ec 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dflet 0:1a07906111ec 34 *
dflet 0:1a07906111ec 35 */
dflet 0:1a07906111ec 36
dflet 0:1a07906111ec 37 #include "cc3100_simplelink.h"
dflet 0:1a07906111ec 38 #include "cc3100_spi.h"
dflet 0:1a07906111ec 39 #include "cli_uart.h"
dflet 0:1a07906111ec 40
dflet 0:1a07906111ec 41 #include "osi.h"
dflet 0:1a07906111ec 42 #include "portmacro.h"
dflet 0:1a07906111ec 43 #include "projdefs.h"
dflet 0:1a07906111ec 44
dflet 0:1a07906111ec 45 extern OsiMsgQ_t g_PBQueue; /*Message Queue*/
dflet 0:1a07906111ec 46
dflet 0:1a07906111ec 47 namespace mbed_cc3100 {
dflet 0:1a07906111ec 48
dflet 0:1a07906111ec 49 P_EVENT_HANDLER pIraEventHandler = 0;
dflet 0:1a07906111ec 50 uint8_t IntIsMasked;
dflet 0:1a07906111ec 51
dflet 0:1a07906111ec 52 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)
dflet 0:1a07906111ec 53 : _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)
dflet 0:1a07906111ec 54 {
dflet 0:1a07906111ec 55
dflet 0:1a07906111ec 56 _wlan_spi.format(8,1);
dflet 0:1a07906111ec 57 _wlan_spi.frequency(16000000);
dflet 0:1a07906111ec 58 _wlan_irq.rise(this, &cc3100_spi::IntSpiGPIOHandler); //_SlDrvRxIrqHandler is called from IntSpiGPIOHandler
dflet 0:1a07906111ec 59 _sw1_irq.rise(this, &cc3100_spi::buttonHandler_1);
dflet 0:1a07906111ec 60 _sw2_irq.rise(this, &cc3100_spi::buttonHandler_2);
dflet 0:1a07906111ec 61 _wlan_nHIB = 0;
dflet 0:1a07906111ec 62 _wlan_cs = 1;
dflet 0:1a07906111ec 63 wait_ms(200);
dflet 0:1a07906111ec 64
dflet 0:1a07906111ec 65
dflet 0:1a07906111ec 66 }
dflet 0:1a07906111ec 67
dflet 0:1a07906111ec 68 cc3100_spi::~cc3100_spi()
dflet 0:1a07906111ec 69 {
dflet 0:1a07906111ec 70
dflet 0:1a07906111ec 71 }
dflet 0:1a07906111ec 72
dflet 0:1a07906111ec 73 int cc3100_spi::spi_Close(Fd_t fd)
dflet 0:1a07906111ec 74 {
dflet 0:1a07906111ec 75
dflet 0:1a07906111ec 76 // Disable WLAN Interrupt ...
dflet 0:1a07906111ec 77 cc3100_InterruptDisable();
dflet 0:1a07906111ec 78
dflet 0:1a07906111ec 79 return NONOS_RET_OK;
dflet 0:1a07906111ec 80 }
dflet 0:1a07906111ec 81
dflet 0:1a07906111ec 82 void cc3100_spi::button1_InterruptDisable()
dflet 0:1a07906111ec 83 {
dflet 0:1a07906111ec 84 _sw1_irq.rise(NULL);
dflet 0:1a07906111ec 85 wait_ms(1);
dflet 0:1a07906111ec 86 }
dflet 0:1a07906111ec 87
dflet 0:1a07906111ec 88 void cc3100_spi::button2_InterruptDisable()
dflet 0:1a07906111ec 89 {
dflet 0:1a07906111ec 90 _sw2_irq.rise(NULL);
dflet 0:1a07906111ec 91 wait_ms(1);
dflet 0:1a07906111ec 92 }
dflet 0:1a07906111ec 93
dflet 0:1a07906111ec 94 void cc3100_spi::button1_InterruptEnable()
dflet 0:1a07906111ec 95 {
dflet 0:1a07906111ec 96 _sw1_irq.rise(this, &cc3100_spi::buttonHandler_1);
dflet 0:1a07906111ec 97 }
dflet 0:1a07906111ec 98
dflet 0:1a07906111ec 99 void cc3100_spi::button2_InterruptEnable()
dflet 0:1a07906111ec 100 {
dflet 0:1a07906111ec 101 _sw2_irq.rise(this, &cc3100_spi::buttonHandler_2);
dflet 0:1a07906111ec 102 }
dflet 0:1a07906111ec 103
dflet 0:1a07906111ec 104 void cc3100_spi::cc3100_InterruptEnable()
dflet 0:1a07906111ec 105 {
dflet 0:1a07906111ec 106 _wlan_irq.rise(this, &cc3100_spi::IntSpiGPIOHandler);
dflet 0:1a07906111ec 107 }
dflet 0:1a07906111ec 108
dflet 0:1a07906111ec 109 void cc3100_spi::cc3100_InterruptDisable()
dflet 0:1a07906111ec 110 {
dflet 0:1a07906111ec 111 _wlan_irq.rise(NULL);
dflet 0:1a07906111ec 112 }
dflet 0:1a07906111ec 113
dflet 0:1a07906111ec 114 void cc3100_spi::CC3100_disable()
dflet 0:1a07906111ec 115 {
dflet 0:1a07906111ec 116 _wlan_nHIB = 0;
dflet 0:1a07906111ec 117 }
dflet 0:1a07906111ec 118
dflet 0:1a07906111ec 119 void cc3100_spi::CC3100_enable()
dflet 0:1a07906111ec 120 {
dflet 0:1a07906111ec 121
dflet 0:1a07906111ec 122 _wlan_nHIB = 1;
dflet 0:1a07906111ec 123 }
dflet 0:1a07906111ec 124
dflet 0:1a07906111ec 125 Fd_t cc3100_spi::spi_Open(int8_t *ifName, uint32_t flags)
dflet 0:1a07906111ec 126 {
dflet 0:1a07906111ec 127
dflet 0:1a07906111ec 128 // 50 ms delay
dflet 0:1a07906111ec 129 wait_ms(50);
dflet 0:1a07906111ec 130
dflet 0:1a07906111ec 131 // Enable WLAN interrupt
dflet 0:1a07906111ec 132 cc3100_InterruptEnable();
dflet 0:1a07906111ec 133
dflet 0:1a07906111ec 134 return NONOS_RET_OK;
dflet 0:1a07906111ec 135 }
dflet 0:1a07906111ec 136
dflet 0:1a07906111ec 137 int cc3100_spi::spi_Write(Fd_t fd, uint8_t *pBuff, int len)
dflet 0:1a07906111ec 138 {
dflet 0:1a07906111ec 139 int len_to_return = len;
dflet 0:1a07906111ec 140
dflet 0:1a07906111ec 141 _wlan_cs = 0;
dflet 0:1a07906111ec 142
dflet 0:1a07906111ec 143 while(len) {
dflet 0:1a07906111ec 144 _wlan_spi.write(*pBuff++);
dflet 0:1a07906111ec 145 len--;
dflet 0:1a07906111ec 146 }
dflet 0:1a07906111ec 147
dflet 0:1a07906111ec 148 _wlan_cs = 1;
dflet 0:1a07906111ec 149
dflet 0:1a07906111ec 150 return len_to_return;
dflet 0:1a07906111ec 151 }
dflet 0:1a07906111ec 152
dflet 0:1a07906111ec 153 int cc3100_spi::spi_Read(Fd_t fd, uint8_t *pBuff, int len)
dflet 0:1a07906111ec 154 {
dflet 0:1a07906111ec 155 int i = 0;
dflet 0:1a07906111ec 156 _wlan_cs = 0;
dflet 0:1a07906111ec 157
dflet 0:1a07906111ec 158 for (i = 0; i < len; i++) {
dflet 0:1a07906111ec 159 pBuff[i] = _wlan_spi.write(0xFF);
dflet 0:1a07906111ec 160 }
dflet 0:1a07906111ec 161
dflet 0:1a07906111ec 162 _wlan_cs = 1;
dflet 0:1a07906111ec 163 return len;
dflet 0:1a07906111ec 164 }
dflet 0:1a07906111ec 165
dflet 0:1a07906111ec 166 void cc3100_spi::IntSpiGPIOHandler(void)
dflet 0:1a07906111ec 167 {
dflet 0:1a07906111ec 168
dflet 0:1a07906111ec 169 if(_wlan_irq){
dflet 0:1a07906111ec 170 _driver._SlDrvRxIrqHandler(0);
dflet 0:1a07906111ec 171 }
dflet 0:1a07906111ec 172 }
dflet 0:1a07906111ec 173
dflet 0:1a07906111ec 174 /*!
dflet 0:1a07906111ec 175 \brief register an interrupt handler for the host IRQ
dflet 0:1a07906111ec 176
dflet 0:1a07906111ec 177 \param[in] InterruptHdl - pointer to interrupt handler function
dflet 0:1a07906111ec 178
dflet 0:1a07906111ec 179 \param[in] pValue - pointer to a memory strcuture that is
dflet 0:1a07906111ec 180 passed to the interrupt handler.
dflet 0:1a07906111ec 181
dflet 0:1a07906111ec 182 \return upon successful registration, the function shall return 0.
dflet 0:1a07906111ec 183 Otherwise, -1 shall be returned
dflet 0:1a07906111ec 184
dflet 0:1a07906111ec 185 \sa
dflet 0:1a07906111ec 186 \note If there is already registered interrupt handler, the
dflet 0:1a07906111ec 187 function should overwrite the old handler with the new one
dflet 0:1a07906111ec 188 \warning
dflet 0:1a07906111ec 189 */
dflet 0:1a07906111ec 190 int cc3100_spi::registerInterruptHandler(P_EVENT_HANDLER InterruptHdl , void* pValue)
dflet 0:1a07906111ec 191 {
dflet 0:1a07906111ec 192
dflet 0:1a07906111ec 193 pIraEventHandler = InterruptHdl;
dflet 0:1a07906111ec 194 return 0;
dflet 0:1a07906111ec 195 }
dflet 0:1a07906111ec 196
dflet 0:1a07906111ec 197 /*!
dflet 0:1a07906111ec 198 \brief Unmasks the Host IRQ
dflet 0:1a07906111ec 199
dflet 0:1a07906111ec 200 \param[in] none
dflet 0:1a07906111ec 201
dflet 0:1a07906111ec 202 \return none
dflet 0:1a07906111ec 203
dflet 0:1a07906111ec 204 \warning
dflet 0:1a07906111ec 205 */
dflet 0:1a07906111ec 206 void cc3100_spi::UnMaskIntHdlr()
dflet 0:1a07906111ec 207 {
dflet 0:1a07906111ec 208 IntIsMasked = FALSE;
dflet 0:1a07906111ec 209 }
dflet 0:1a07906111ec 210
dflet 0:1a07906111ec 211 /*!
dflet 0:1a07906111ec 212 \brief Masks the Host IRQ
dflet 0:1a07906111ec 213
dflet 0:1a07906111ec 214 \param[in] none
dflet 0:1a07906111ec 215
dflet 0:1a07906111ec 216 \return none
dflet 0:1a07906111ec 217
dflet 0:1a07906111ec 218 \warning
dflet 0:1a07906111ec 219 */
dflet 0:1a07906111ec 220 void cc3100_spi::MaskIntHdlr()
dflet 0:1a07906111ec 221 {
dflet 0:1a07906111ec 222 IntIsMasked = TRUE;
dflet 0:1a07906111ec 223 }
dflet 0:1a07906111ec 224
dflet 0:1a07906111ec 225 /*!
dflet 0:1a07906111ec 226 \brief Handles the button press 1 on the MCU
dflet 0:1a07906111ec 227 and updates the queue with relevant action.
dflet 0:1a07906111ec 228
dflet 0:1a07906111ec 229 \param none
dflet 0:1a07906111ec 230
dflet 0:1a07906111ec 231 \return none
dflet 0:1a07906111ec 232 */
dflet 0:1a07906111ec 233 void cc3100_spi::buttonHandler_1(void)
dflet 0:1a07906111ec 234 {
dflet 0:1a07906111ec 235 int32_t rv = 0;
dflet 0:1a07906111ec 236 button1_InterruptDisable();
dflet 0:1a07906111ec 237 osi_messages var;
dflet 0:1a07906111ec 238
dflet 0:1a07906111ec 239 g_publishCount++;
dflet 0:1a07906111ec 240
dflet 0:1a07906111ec 241 var = PUSH_BUTTON_1_PRESSED;
dflet 0:1a07906111ec 242
dflet 0:1a07906111ec 243 rv = osi_MsgQWrite(&g_PBQueue, &var, OSI_NO_WAIT);
dflet 0:1a07906111ec 244 if(rv < 0){
dflet 0:1a07906111ec 245 Uart_Write((uint8_t*)"Messsage queue failed\r\n");
dflet 0:1a07906111ec 246 }
dflet 0:1a07906111ec 247
dflet 0:1a07906111ec 248 }
dflet 0:1a07906111ec 249
dflet 0:1a07906111ec 250 /*!
dflet 0:1a07906111ec 251 \brief Handles the button press 2 on the MCU
dflet 0:1a07906111ec 252 and updates the queue with relevant action.
dflet 0:1a07906111ec 253
dflet 0:1a07906111ec 254 \param none
dflet 0:1a07906111ec 255
dflet 0:1a07906111ec 256 \return none
dflet 0:1a07906111ec 257 */
dflet 0:1a07906111ec 258 void cc3100_spi::buttonHandler_2(void)
dflet 0:1a07906111ec 259 {
dflet 0:1a07906111ec 260 int32_t rv = 0;
dflet 0:1a07906111ec 261 button2_InterruptDisable();
dflet 0:1a07906111ec 262 osi_messages var;
dflet 0:1a07906111ec 263
dflet 0:1a07906111ec 264 g_publishCount++;
dflet 0:1a07906111ec 265
dflet 0:1a07906111ec 266 var = PUSH_BUTTON_2_PRESSED;
dflet 0:1a07906111ec 267
dflet 0:1a07906111ec 268 rv = osi_MsgQWrite(&g_PBQueue, &var, OSI_NO_WAIT);
dflet 0:1a07906111ec 269 if(rv < 0){
dflet 0:1a07906111ec 270 Uart_Write((uint8_t*)"Messsage queue failed\r\n");
dflet 0:1a07906111ec 271 }
dflet 0:1a07906111ec 272
dflet 0:1a07906111ec 273 }
dflet 0:1a07906111ec 274
dflet 0:1a07906111ec 275 }//namespace mbed_cc3100
dflet 0:1a07906111ec 276
dflet 0:1a07906111ec 277
dflet 0:1a07906111ec 278
dflet 0:1a07906111ec 279
dflet 0:1a07906111ec 280