cc3000 hostdriver with the mbed socket interface

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
frankvnk
Date:
Sat Mar 28 21:00:14 2015 +0000
Revision:
49:aee80f12ccca
Parent:
45:50ab13d8f2dc
Child:
51:897cfc2c7e8c
Added debug info when TX or RX buffer overrun occurs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 20:30b6ed7bf8fd 1 /*****************************************************************************
Kojto 20:30b6ed7bf8fd 2 *
Kojto 20:30b6ed7bf8fd 3 * C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to
Kojto 20:30b6ed7bf8fd 4 * Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and
Kojto 20:30b6ed7bf8fd 5 * provided help.
Kojto 20:30b6ed7bf8fd 6 *
Kojto 20:30b6ed7bf8fd 7 * This version of "host driver" uses CC3000 Host Driver Implementation. Thus
Kojto 20:30b6ed7bf8fd 8 * read the following copyright:
Kojto 20:30b6ed7bf8fd 9 *
Kojto 20:30b6ed7bf8fd 10 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Kojto 20:30b6ed7bf8fd 11 *
Kojto 20:30b6ed7bf8fd 12 * Redistribution and use in source and binary forms, with or without
Kojto 20:30b6ed7bf8fd 13 * modification, are permitted provided that the following conditions
Kojto 20:30b6ed7bf8fd 14 * are met:
Kojto 20:30b6ed7bf8fd 15 *
Kojto 20:30b6ed7bf8fd 16 * Redistributions of source code must retain the above copyright
Kojto 20:30b6ed7bf8fd 17 * notice, this list of conditions and the following disclaimer.
Kojto 20:30b6ed7bf8fd 18 *
Kojto 20:30b6ed7bf8fd 19 * Redistributions in binary form must reproduce the above copyright
Kojto 20:30b6ed7bf8fd 20 * notice, this list of conditions and the following disclaimer in the
Kojto 20:30b6ed7bf8fd 21 * documentation and/or other materials provided with the
Kojto 20:30b6ed7bf8fd 22 * distribution.
Kojto 20:30b6ed7bf8fd 23 *
Kojto 20:30b6ed7bf8fd 24 * Neither the name of Texas Instruments Incorporated nor the names of
Kojto 20:30b6ed7bf8fd 25 * its contributors may be used to endorse or promote products derived
Kojto 20:30b6ed7bf8fd 26 * from this software without specific prior written permission.
Kojto 20:30b6ed7bf8fd 27 *
Kojto 20:30b6ed7bf8fd 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Kojto 20:30b6ed7bf8fd 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Kojto 20:30b6ed7bf8fd 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Kojto 20:30b6ed7bf8fd 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Kojto 20:30b6ed7bf8fd 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Kojto 20:30b6ed7bf8fd 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Kojto 20:30b6ed7bf8fd 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Kojto 20:30b6ed7bf8fd 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Kojto 20:30b6ed7bf8fd 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Kojto 20:30b6ed7bf8fd 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Kojto 20:30b6ed7bf8fd 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 20:30b6ed7bf8fd 39 *
Kojto 20:30b6ed7bf8fd 40 *****************************************************************************/
Kojto 20:30b6ed7bf8fd 41 #include "cc3000.h"
Kojto 20:30b6ed7bf8fd 42 #include "cc3000_spi.h"
Kojto 20:30b6ed7bf8fd 43
Kojto 20:30b6ed7bf8fd 44 namespace mbed_cc3000 {
Kojto 20:30b6ed7bf8fd 45
Kojto 45:50ab13d8f2dc 46 cc3000_spi::cc3000_spi(PinName cc3000_irq, PinName cc3000_en, PinName cc3000_cs, SPI cc3000_spi, cc3000_event &event, cc3000_simple_link &simple_link)
Kojto 45:50ab13d8f2dc 47 : _wlan_irq(cc3000_irq), _wlan_en(cc3000_en), _wlan_cs(cc3000_cs), _wlan_spi(cc3000_spi), _event(event), _simple_link(simple_link) {
Kojto 20:30b6ed7bf8fd 48
Kojto 20:30b6ed7bf8fd 49 _wlan_spi.format(8,1);
Kojto 20:30b6ed7bf8fd 50 _wlan_spi.frequency(12000000);
Kojto 45:50ab13d8f2dc 51 _wlan_irq.fall(this, &cc3000_spi::WLAN_IRQHandler);
Kojto 20:30b6ed7bf8fd 52
Kojto 20:30b6ed7bf8fd 53 _wlan_en = 0;
Kojto 20:30b6ed7bf8fd 54 _wlan_cs = 1;
Kojto 45:50ab13d8f2dc 55 wait_ms(50); /* mbed board delay */
Kojto 20:30b6ed7bf8fd 56 }
Kojto 20:30b6ed7bf8fd 57
Kojto 20:30b6ed7bf8fd 58 cc3000_spi::~cc3000_spi() {
Kojto 20:30b6ed7bf8fd 59
Kojto 20:30b6ed7bf8fd 60 }
Kojto 20:30b6ed7bf8fd 61
Kojto 20:30b6ed7bf8fd 62 void cc3000_spi::wlan_irq_enable()
Kojto 20:30b6ed7bf8fd 63 {
Kojto 45:50ab13d8f2dc 64 _process_irq = true;
Kojto 45:50ab13d8f2dc 65 //_wlan_irq.enable_irq();
Kojto 44:960b73df5981 66
Kojto 45:50ab13d8f2dc 67 if (wlan_irq_read() == 0) {
Kojto 44:960b73df5981 68 WLAN_IRQHandler();
Kojto 44:960b73df5981 69 }
Kojto 20:30b6ed7bf8fd 70 }
Kojto 20:30b6ed7bf8fd 71
Kojto 20:30b6ed7bf8fd 72 void cc3000_spi::wlan_irq_disable() {
Kojto 45:50ab13d8f2dc 73 _process_irq = false;
Kojto 45:50ab13d8f2dc 74 //_wlan_irq.disable_irq();
Kojto 20:30b6ed7bf8fd 75 }
Kojto 20:30b6ed7bf8fd 76
Kojto 20:30b6ed7bf8fd 77 uint32_t cc3000_spi::wlan_irq_read() {
Kojto 20:30b6ed7bf8fd 78 return _wlan_irq.read();
Kojto 20:30b6ed7bf8fd 79 }
Kojto 20:30b6ed7bf8fd 80
Kojto 20:30b6ed7bf8fd 81 void cc3000_spi::close() {
Kojto 20:30b6ed7bf8fd 82 wlan_irq_disable();
Kojto 20:30b6ed7bf8fd 83 }
Kojto 20:30b6ed7bf8fd 84
Kojto 20:30b6ed7bf8fd 85 void cc3000_spi::open() {
Kojto 20:30b6ed7bf8fd 86 _spi_info.spi_state = eSPI_STATE_POWERUP;
Kojto 20:30b6ed7bf8fd 87 _spi_info.tx_packet_length = 0;
Kojto 20:30b6ed7bf8fd 88 _spi_info.rx_packet_length = 0;
Kojto 20:30b6ed7bf8fd 89 wlan_irq_enable();
Kojto 20:30b6ed7bf8fd 90 }
Kojto 20:30b6ed7bf8fd 91
Kojto 20:30b6ed7bf8fd 92 uint32_t cc3000_spi::first_write(uint8_t *buffer, uint16_t length) {
Kojto 20:30b6ed7bf8fd 93 _wlan_cs = 0;
Kojto 20:30b6ed7bf8fd 94 wait_us(50);
Kojto 20:30b6ed7bf8fd 95
Kojto 20:30b6ed7bf8fd 96 /* first 4 bytes of the data */
Kojto 20:30b6ed7bf8fd 97 write_synchronous(buffer, 4);
Kojto 20:30b6ed7bf8fd 98 wait_us(50);
Kojto 20:30b6ed7bf8fd 99 write_synchronous(buffer + 4, length - 4);
Kojto 20:30b6ed7bf8fd 100 _spi_info.spi_state = eSPI_STATE_IDLE;
Kojto 20:30b6ed7bf8fd 101 _wlan_cs = 1;
Kojto 20:30b6ed7bf8fd 102
Kojto 20:30b6ed7bf8fd 103 return 0;
Kojto 20:30b6ed7bf8fd 104 }
Kojto 20:30b6ed7bf8fd 105
Kojto 20:30b6ed7bf8fd 106
Kojto 20:30b6ed7bf8fd 107 uint32_t cc3000_spi::write(uint8_t *buffer, uint16_t length) {
Kojto 20:30b6ed7bf8fd 108 uint8_t pad = 0;
Kojto 45:50ab13d8f2dc 109 // check the total length of the packet in order to figure out if padding is necessary
Kojto 45:50ab13d8f2dc 110 if(!(length & 0x0001)) {
Kojto 20:30b6ed7bf8fd 111 pad++;
Kojto 45:50ab13d8f2dc 112 }
Kojto 45:50ab13d8f2dc 113 buffer[0] = WRITE;
Kojto 45:50ab13d8f2dc 114 buffer[1] = HI(length + pad);
Kojto 45:50ab13d8f2dc 115 buffer[2] = LO(length + pad);
Kojto 45:50ab13d8f2dc 116 buffer[3] = 0;
Kojto 45:50ab13d8f2dc 117 buffer[4] = 0;
Kojto 20:30b6ed7bf8fd 118
Kojto 45:50ab13d8f2dc 119 length += (SPI_HEADER_SIZE + pad);
Kojto 20:30b6ed7bf8fd 120
Kojto 45:50ab13d8f2dc 121 // The magic number resides at the end of the TX/RX buffer (1 byte after the allocated size)
Kojto 45:50ab13d8f2dc 122 // If the magic number is overwitten - buffer overrun occurred - we will be stuck here forever!
Kojto 45:50ab13d8f2dc 123 uint8_t *transmit_buffer = _simple_link.get_transmit_buffer();
Kojto 45:50ab13d8f2dc 124 if (transmit_buffer[CC3000_TX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER) {
frankvnk 49:aee80f12ccca 125 DBG_CC("System halted - TX buffer overflow detected (buffer size: %d).", CC3000_TX_BUFFER_SIZE);
Kojto 45:50ab13d8f2dc 126 while (1);
Kojto 45:50ab13d8f2dc 127 }
Kojto 45:50ab13d8f2dc 128
Kojto 45:50ab13d8f2dc 129 if (_spi_info.spi_state == eSPI_STATE_POWERUP) {
Kojto 45:50ab13d8f2dc 130 while (_spi_info.spi_state != eSPI_STATE_INITIALIZED);
Kojto 45:50ab13d8f2dc 131 }
Kojto 20:30b6ed7bf8fd 132
Kojto 45:50ab13d8f2dc 133 if (_spi_info.spi_state == eSPI_STATE_INITIALIZED) {
Kojto 45:50ab13d8f2dc 134 // TX/RX transaction over SPI after powerup: IRQ is low - send read buffer size command
Kojto 45:50ab13d8f2dc 135 first_write(buffer, length);
Kojto 45:50ab13d8f2dc 136 } else {
Kojto 45:50ab13d8f2dc 137 // Prevent occurence of a race condition when 2 back to back packets are sent to the
Kojto 45:50ab13d8f2dc 138 // device, so the state will move to IDLE and once again to not IDLE due to IRQ
Kojto 45:50ab13d8f2dc 139 wlan_irq_disable();
Kojto 20:30b6ed7bf8fd 140
Kojto 45:50ab13d8f2dc 141 while (_spi_info.spi_state != eSPI_STATE_IDLE);
Kojto 20:30b6ed7bf8fd 142
Kojto 45:50ab13d8f2dc 143 _spi_info.spi_state = eSPI_STATE_WRITE_IRQ;
Kojto 45:50ab13d8f2dc 144 //_spi_info.pTxPacket = buffer;
Kojto 45:50ab13d8f2dc 145 _spi_info.tx_packet_length = length;
Kojto 20:30b6ed7bf8fd 146
Kojto 45:50ab13d8f2dc 147 // Assert the CS line and wait until the IRQ line is active, then initialize the write operation
Kojto 45:50ab13d8f2dc 148 _wlan_cs = 0;
Kojto 20:30b6ed7bf8fd 149
Kojto 45:50ab13d8f2dc 150 wlan_irq_enable();
Kojto 45:50ab13d8f2dc 151 }
Kojto 20:30b6ed7bf8fd 152
Kojto 45:50ab13d8f2dc 153 // Wait until the transaction ends
Kojto 45:50ab13d8f2dc 154 while (_spi_info.spi_state != eSPI_STATE_IDLE);
Kojto 20:30b6ed7bf8fd 155
Kojto 45:50ab13d8f2dc 156 return 0;
Kojto 20:30b6ed7bf8fd 157 }
Kojto 20:30b6ed7bf8fd 158
Kojto 20:30b6ed7bf8fd 159 void cc3000_spi::write_synchronous(uint8_t *data, uint16_t size) {
Kojto 45:50ab13d8f2dc 160 while(size) {
Kojto 20:30b6ed7bf8fd 161 _wlan_spi.write(*data++);
Kojto 20:30b6ed7bf8fd 162 size--;
Kojto 45:50ab13d8f2dc 163 }
Kojto 20:30b6ed7bf8fd 164 }
Kojto 20:30b6ed7bf8fd 165
Kojto 20:30b6ed7bf8fd 166 void cc3000_spi::read_synchronous(uint8_t *data, uint16_t size) {
Kojto 45:50ab13d8f2dc 167 for (uint32_t i = 0; i < size; i++) {
Kojto 45:50ab13d8f2dc 168 data[i] = _wlan_spi.write(0x03);
Kojto 45:50ab13d8f2dc 169 }
Kojto 20:30b6ed7bf8fd 170 }
Kojto 20:30b6ed7bf8fd 171
Kojto 20:30b6ed7bf8fd 172 uint32_t cc3000_spi::read_data_cont() {
Kojto 45:50ab13d8f2dc 173 int32_t data_to_recv;
Kojto 45:50ab13d8f2dc 174 uint8_t *evnt_buff, type;
Kojto 20:30b6ed7bf8fd 175
Kojto 20:30b6ed7bf8fd 176 //determine the packet type
Kojto 20:30b6ed7bf8fd 177 evnt_buff = _simple_link.get_received_buffer();
Kojto 20:30b6ed7bf8fd 178 data_to_recv = 0;
Kojto 20:30b6ed7bf8fd 179 STREAM_TO_UINT8((uint8_t *)(evnt_buff + SPI_HEADER_SIZE), HCI_PACKET_TYPE_OFFSET, type);
Kojto 20:30b6ed7bf8fd 180
Kojto 45:50ab13d8f2dc 181 switch(type) {
Kojto 20:30b6ed7bf8fd 182 case HCI_TYPE_DATA:
Kojto 45:50ab13d8f2dc 183 // Read the remaining data..
Kojto 45:50ab13d8f2dc 184 STREAM_TO_UINT16((uint8_t *)(evnt_buff + SPI_HEADER_SIZE), HCI_DATA_LENGTH_OFFSET, data_to_recv);
Kojto 45:50ab13d8f2dc 185 if (!((HEADERS_SIZE_EVNT + data_to_recv) & 1)) {
Kojto 45:50ab13d8f2dc 186 data_to_recv++;
Kojto 45:50ab13d8f2dc 187 }
Kojto 20:30b6ed7bf8fd 188
Kojto 45:50ab13d8f2dc 189 if (data_to_recv) {
Kojto 20:30b6ed7bf8fd 190 read_synchronous(evnt_buff + 10, data_to_recv);
Kojto 45:50ab13d8f2dc 191 }
Kojto 20:30b6ed7bf8fd 192 break;
Kojto 20:30b6ed7bf8fd 193 case HCI_TYPE_EVNT:
Kojto 45:50ab13d8f2dc 194 // Calculate the rest length of the data
Kojto 20:30b6ed7bf8fd 195 STREAM_TO_UINT8((char *)(evnt_buff + SPI_HEADER_SIZE), HCI_EVENT_LENGTH_OFFSET, data_to_recv);
Kojto 45:50ab13d8f2dc 196 data_to_recv -= 1;
Kojto 45:50ab13d8f2dc 197 // Add padding byte if needed
Kojto 45:50ab13d8f2dc 198 if ((HEADERS_SIZE_EVNT + data_to_recv) & 1) {
Kojto 45:50ab13d8f2dc 199 data_to_recv++;
Kojto 45:50ab13d8f2dc 200 }
Kojto 20:30b6ed7bf8fd 201
Kojto 45:50ab13d8f2dc 202 if (data_to_recv) {
Kojto 20:30b6ed7bf8fd 203 read_synchronous(evnt_buff + 10, data_to_recv);
Kojto 45:50ab13d8f2dc 204 }
Kojto 20:30b6ed7bf8fd 205
Kojto 45:50ab13d8f2dc 206 _spi_info.spi_state = eSPI_STATE_READ_EOT;
Kojto 20:30b6ed7bf8fd 207 break;
Kojto 20:30b6ed7bf8fd 208 }
Kojto 45:50ab13d8f2dc 209 return 0;
Kojto 20:30b6ed7bf8fd 210 }
Kojto 20:30b6ed7bf8fd 211
Kojto 34:1ad18123bf11 212 void cc3000_spi::set_wlan_en(uint8_t value) {
Kojto 20:30b6ed7bf8fd 213 if (value) {
Kojto 20:30b6ed7bf8fd 214 _wlan_en = 1;
Kojto 20:30b6ed7bf8fd 215 } else {
Kojto 20:30b6ed7bf8fd 216 _wlan_en = 0;
Kojto 20:30b6ed7bf8fd 217 }
Kojto 20:30b6ed7bf8fd 218 }
Kojto 20:30b6ed7bf8fd 219
Kojto 20:30b6ed7bf8fd 220 void cc3000_spi::WLAN_IRQHandler() {
Kojto 45:50ab13d8f2dc 221 if (_process_irq) {
Kojto 45:50ab13d8f2dc 222 if (_spi_info.spi_state == eSPI_STATE_POWERUP) {
Kojto 45:50ab13d8f2dc 223 // Inform HCI Layer that IRQ occured after powerup
Kojto 45:50ab13d8f2dc 224 _spi_info.spi_state = eSPI_STATE_INITIALIZED;
Kojto 45:50ab13d8f2dc 225 } else if (_spi_info.spi_state == eSPI_STATE_IDLE) {
Kojto 45:50ab13d8f2dc 226 _spi_info.spi_state = eSPI_STATE_READ_IRQ;
Kojto 45:50ab13d8f2dc 227 /* IRQ line goes low - acknowledge it */
Kojto 45:50ab13d8f2dc 228 _wlan_cs = 0;
Kojto 45:50ab13d8f2dc 229 read_synchronous(_simple_link.get_received_buffer(), 10);
Kojto 45:50ab13d8f2dc 230 _spi_info.spi_state = eSPI_STATE_READ_EOT;
Kojto 20:30b6ed7bf8fd 231
Kojto 45:50ab13d8f2dc 232 // The header was read - continue with the payload read
Kojto 45:50ab13d8f2dc 233 if (!read_data_cont()) {
Kojto 45:50ab13d8f2dc 234 // All the data was read - finalize handling by switching to the task
Kojto 45:50ab13d8f2dc 235 // Trigger Rx processing
Kojto 45:50ab13d8f2dc 236 wlan_irq_disable();
Kojto 45:50ab13d8f2dc 237 _wlan_cs = 1;
Kojto 45:50ab13d8f2dc 238 // The magic number resides at the end of the TX/RX buffer (1 byte after the allocated size)
Kojto 45:50ab13d8f2dc 239 // If the magic number is overwitten - buffer overrun occurred - we will be stuck here forever!
Kojto 45:50ab13d8f2dc 240 uint8_t *received_buffer = _simple_link.get_received_buffer();
Kojto 45:50ab13d8f2dc 241 if (received_buffer[CC3000_RX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER) {
frankvnk 49:aee80f12ccca 242 DBG_CC("System halted - RX buffer overflow detected (buffer size: %d).", CC3000_RX_BUFFER_SIZE);
Kojto 45:50ab13d8f2dc 243 while (1);
Kojto 45:50ab13d8f2dc 244 }
Kojto 45:50ab13d8f2dc 245
Kojto 45:50ab13d8f2dc 246 _spi_info.spi_state = eSPI_STATE_IDLE;
Kojto 45:50ab13d8f2dc 247 _event.received_handler(received_buffer + SPI_HEADER_SIZE);
Kojto 45:50ab13d8f2dc 248 }
Kojto 45:50ab13d8f2dc 249 } else if (_spi_info.spi_state == eSPI_STATE_WRITE_IRQ) {
Kojto 45:50ab13d8f2dc 250 write_synchronous(_simple_link.get_transmit_buffer(), _spi_info.tx_packet_length);
Kojto 45:50ab13d8f2dc 251 _spi_info.spi_state = eSPI_STATE_IDLE;
Kojto 45:50ab13d8f2dc 252 _wlan_cs = 1;
Kojto 45:50ab13d8f2dc 253 }
Kojto 45:50ab13d8f2dc 254 }
Kojto 20:30b6ed7bf8fd 255 }
Kojto 20:30b6ed7bf8fd 256
Kojto 45:50ab13d8f2dc 257 } // namespace mbed_cc3000