Update revision to use TI's mqtt and Freertos.

Dependencies:   mbed client server

Fork of cc3100_Test_mqtt_CM3 by David Fletcher

Committer:
dflet
Date:
Thu Sep 03 14:02:37 2015 +0000
Revision:
3:a8c249046181
SPI Mode change 1 to 0

Who changed what in which revision?

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