cc3000 hostdriver with the mbed socket interface
Fork of cc3000_hostdriver_mbedsocket by
cc3000_spi.cpp@34:1ad18123bf11, 2013-10-08 (annotated)
- Committer:
- Kojto
- Date:
- Tue Oct 08 13:13:05 2013 +0200
- Revision:
- 34:1ad18123bf11
- Parent:
- 33:9e23b24fb4f3
- Child:
- 42:bd2c631a031a
doxygen comments added by Frank V., htonl in Endpoint
Who changed what in which revision?
User | Revision | Line number | New 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 | 20:30b6ed7bf8fd | 46 | cc3000_spi::cc3000_spi(PinName cc3000_irq, PinName cc3000_en, PinName cc3000_cs, SPI cc3000_spi, IRQn_Type irq_port, cc3000_event &event, cc3000_simple_link &simple_link) |
Kojto | 20:30b6ed7bf8fd | 47 | : _wlan_irq(cc3000_irq), _wlan_en(cc3000_en), _wlan_cs(cc3000_cs), _wlan_spi(cc3000_spi), _irq_port(irq_port), |
Kojto | 20:30b6ed7bf8fd | 48 | _event(event), _simple_link(simple_link) { |
Kojto | 20:30b6ed7bf8fd | 49 | /* TODO = clear pending interrupts for PORTS. This is dependent on the used chip */ |
Kojto | 20:30b6ed7bf8fd | 50 | |
Kojto | 20:30b6ed7bf8fd | 51 | _wlan_spi.format(8,1); |
Kojto | 20:30b6ed7bf8fd | 52 | _wlan_spi.frequency(12000000); |
Kojto | 20:30b6ed7bf8fd | 53 | _function_pointer = _wlan_irq.fall(this, &cc3000_spi::WLAN_IRQHandler); |
Kojto | 20:30b6ed7bf8fd | 54 | |
Kojto | 20:30b6ed7bf8fd | 55 | _wlan_en = 0; |
Kojto | 20:30b6ed7bf8fd | 56 | _wlan_cs = 1; |
Kojto | 20:30b6ed7bf8fd | 57 | } |
Kojto | 20:30b6ed7bf8fd | 58 | |
Kojto | 20:30b6ed7bf8fd | 59 | cc3000_spi::~cc3000_spi() { |
Kojto | 20:30b6ed7bf8fd | 60 | |
Kojto | 20:30b6ed7bf8fd | 61 | } |
Kojto | 20:30b6ed7bf8fd | 62 | |
Kojto | 20:30b6ed7bf8fd | 63 | void cc3000_spi::wlan_irq_enable() |
Kojto | 20:30b6ed7bf8fd | 64 | { |
Kojto | 20:30b6ed7bf8fd | 65 | NVIC_EnableIRQ(_irq_port); |
Kojto | 20:30b6ed7bf8fd | 66 | } |
Kojto | 20:30b6ed7bf8fd | 67 | |
Kojto | 20:30b6ed7bf8fd | 68 | void cc3000_spi::wlan_irq_disable() { |
Kojto | 20:30b6ed7bf8fd | 69 | NVIC_DisableIRQ(_irq_port); |
Kojto | 20:30b6ed7bf8fd | 70 | } |
Kojto | 20:30b6ed7bf8fd | 71 | |
Kojto | 20:30b6ed7bf8fd | 72 | uint32_t cc3000_spi::wlan_irq_read() { |
Kojto | 20:30b6ed7bf8fd | 73 | return _wlan_irq.read(); |
Kojto | 20:30b6ed7bf8fd | 74 | } |
Kojto | 20:30b6ed7bf8fd | 75 | |
Kojto | 20:30b6ed7bf8fd | 76 | void cc3000_spi::close() { |
Kojto | 20:30b6ed7bf8fd | 77 | wlan_irq_disable(); |
Kojto | 20:30b6ed7bf8fd | 78 | } |
Kojto | 20:30b6ed7bf8fd | 79 | |
Kojto | 20:30b6ed7bf8fd | 80 | // void cc3000_spi::SpiReceiveHandler() { |
Kojto | 20:30b6ed7bf8fd | 81 | // _simple_link.usEventOrDataReceived = 1; |
Kojto | 20:30b6ed7bf8fd | 82 | // //_simple_link.pucReceivedData = (unsigned char *)pvBuffer; |
Kojto | 20:30b6ed7bf8fd | 83 | |
Kojto | 20:30b6ed7bf8fd | 84 | // hci_unsolicited_event_handler(); |
Kojto | 20:30b6ed7bf8fd | 85 | // } |
Kojto | 20:30b6ed7bf8fd | 86 | |
Kojto | 20:30b6ed7bf8fd | 87 | |
Kojto | 20:30b6ed7bf8fd | 88 | /* TODO |
Kojto | 20:30b6ed7bf8fd | 89 | pRxPacket, pTxPacket do we need to hold this pointer ? |
Kojto | 20:30b6ed7bf8fd | 90 | SPIRxHandler - remove? |
Kojto | 20:30b6ed7bf8fd | 91 | */ |
Kojto | 20:30b6ed7bf8fd | 92 | void cc3000_spi::open() { |
Kojto | 20:30b6ed7bf8fd | 93 | _spi_info.spi_state = eSPI_STATE_POWERUP; |
Kojto | 20:30b6ed7bf8fd | 94 | //_spi_info.SPIRxHandler = pfRxHandler; |
Kojto | 20:30b6ed7bf8fd | 95 | _spi_info.tx_packet_length = 0; |
Kojto | 20:30b6ed7bf8fd | 96 | _spi_info.rx_packet_length = 0; |
Kojto | 20:30b6ed7bf8fd | 97 | //_rx_buffer[CC3000_RX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER; |
Kojto | 20:30b6ed7bf8fd | 98 | //_tx_buffer[CC3000_TX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER; |
Kojto | 20:30b6ed7bf8fd | 99 | wlan_irq_enable(); |
Kojto | 20:30b6ed7bf8fd | 100 | } |
Kojto | 20:30b6ed7bf8fd | 101 | |
Kojto | 20:30b6ed7bf8fd | 102 | uint32_t cc3000_spi::first_write(uint8_t *buffer, uint16_t length) { |
Kojto | 20:30b6ed7bf8fd | 103 | _wlan_cs = 0; |
Kojto | 20:30b6ed7bf8fd | 104 | wait_us(50); |
Kojto | 20:30b6ed7bf8fd | 105 | |
Kojto | 20:30b6ed7bf8fd | 106 | /* first 4 bytes of the data */ |
Kojto | 20:30b6ed7bf8fd | 107 | write_synchronous(buffer, 4); |
Kojto | 20:30b6ed7bf8fd | 108 | wait_us(50); |
Kojto | 20:30b6ed7bf8fd | 109 | write_synchronous(buffer + 4, length - 4); |
Kojto | 20:30b6ed7bf8fd | 110 | _spi_info.spi_state = eSPI_STATE_IDLE; |
Kojto | 20:30b6ed7bf8fd | 111 | _wlan_cs = 1; |
Kojto | 20:30b6ed7bf8fd | 112 | |
Kojto | 20:30b6ed7bf8fd | 113 | return 0; |
Kojto | 20:30b6ed7bf8fd | 114 | } |
Kojto | 20:30b6ed7bf8fd | 115 | |
Kojto | 20:30b6ed7bf8fd | 116 | |
Kojto | 20:30b6ed7bf8fd | 117 | uint32_t cc3000_spi::write(uint8_t *buffer, uint16_t length) { |
Kojto | 20:30b6ed7bf8fd | 118 | uint8_t pad = 0; |
Kojto | 20:30b6ed7bf8fd | 119 | // check the total length of the packet in order to figure out if padding is necessary |
Kojto | 20:30b6ed7bf8fd | 120 | if(!(length & 0x0001)) |
Kojto | 20:30b6ed7bf8fd | 121 | { |
Kojto | 20:30b6ed7bf8fd | 122 | pad++; |
Kojto | 20:30b6ed7bf8fd | 123 | } |
Kojto | 20:30b6ed7bf8fd | 124 | buffer[0] = WRITE; |
Kojto | 20:30b6ed7bf8fd | 125 | buffer[1] = HI(length + pad); |
Kojto | 20:30b6ed7bf8fd | 126 | buffer[2] = LO(length + pad); |
Kojto | 20:30b6ed7bf8fd | 127 | buffer[3] = 0; |
Kojto | 20:30b6ed7bf8fd | 128 | buffer[4] = 0; |
Kojto | 20:30b6ed7bf8fd | 129 | |
Kojto | 20:30b6ed7bf8fd | 130 | length += (SPI_HEADER_SIZE + pad); |
Kojto | 20:30b6ed7bf8fd | 131 | |
Kojto | 20:30b6ed7bf8fd | 132 | // The magic number resides at the end of the TX/RX buffer (1 byte after the allocated size) |
Kojto | 20:30b6ed7bf8fd | 133 | // If the magic number is overwitten - buffer overrun occurred - we will be stuck here forever! |
Kojto | 20:30b6ed7bf8fd | 134 | uint8_t * transmit_buffer = _simple_link.get_transmit_buffer(); |
Kojto | 20:30b6ed7bf8fd | 135 | if (transmit_buffer[CC3000_TX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER) |
Kojto | 20:30b6ed7bf8fd | 136 | { |
Kojto | 20:30b6ed7bf8fd | 137 | while (1); |
Kojto | 20:30b6ed7bf8fd | 138 | } |
Kojto | 20:30b6ed7bf8fd | 139 | |
Kojto | 20:30b6ed7bf8fd | 140 | if (_spi_info.spi_state == eSPI_STATE_POWERUP) |
Kojto | 20:30b6ed7bf8fd | 141 | { |
Kojto | 20:30b6ed7bf8fd | 142 | while (_spi_info.spi_state != eSPI_STATE_INITIALIZED); |
Kojto | 20:30b6ed7bf8fd | 143 | } |
Kojto | 20:30b6ed7bf8fd | 144 | |
Kojto | 20:30b6ed7bf8fd | 145 | if (_spi_info.spi_state == eSPI_STATE_INITIALIZED) |
Kojto | 20:30b6ed7bf8fd | 146 | { |
Kojto | 20:30b6ed7bf8fd | 147 | // TX/RX transaction over SPI after powerup: IRQ is low - send read buffer size command |
Kojto | 20:30b6ed7bf8fd | 148 | first_write(buffer, length); |
Kojto | 20:30b6ed7bf8fd | 149 | } |
Kojto | 20:30b6ed7bf8fd | 150 | else |
Kojto | 20:30b6ed7bf8fd | 151 | { |
Kojto | 20:30b6ed7bf8fd | 152 | // Prevent occurence of a race condition when 2 back to back packets are sent to the |
Kojto | 20:30b6ed7bf8fd | 153 | // device, so the state will move to IDLE and once again to not IDLE due to IRQ |
Kojto | 20:30b6ed7bf8fd | 154 | wlan_irq_disable(); |
Kojto | 20:30b6ed7bf8fd | 155 | |
Kojto | 20:30b6ed7bf8fd | 156 | while (_spi_info.spi_state != eSPI_STATE_IDLE); |
Kojto | 20:30b6ed7bf8fd | 157 | |
Kojto | 20:30b6ed7bf8fd | 158 | _spi_info.spi_state = eSPI_STATE_WRITE_IRQ; |
Kojto | 20:30b6ed7bf8fd | 159 | //_spi_info.pTxPacket = buffer; |
Kojto | 20:30b6ed7bf8fd | 160 | _spi_info.tx_packet_length = length; |
Kojto | 20:30b6ed7bf8fd | 161 | |
Kojto | 20:30b6ed7bf8fd | 162 | // Assert the CS line and wait until the IRQ line is active, then initialize the write operation |
Kojto | 20:30b6ed7bf8fd | 163 | _wlan_cs = 0; |
Kojto | 20:30b6ed7bf8fd | 164 | |
Kojto | 20:30b6ed7bf8fd | 165 | wlan_irq_enable(); |
Kojto | 20:30b6ed7bf8fd | 166 | } |
Kojto | 20:30b6ed7bf8fd | 167 | |
Kojto | 20:30b6ed7bf8fd | 168 | // Wait until the transaction ends |
Kojto | 20:30b6ed7bf8fd | 169 | while (_spi_info.spi_state != eSPI_STATE_IDLE); |
Kojto | 20:30b6ed7bf8fd | 170 | |
Kojto | 20:30b6ed7bf8fd | 171 | return 0; |
Kojto | 20:30b6ed7bf8fd | 172 | } |
Kojto | 20:30b6ed7bf8fd | 173 | |
Kojto | 20:30b6ed7bf8fd | 174 | void cc3000_spi::write_synchronous(uint8_t *data, uint16_t size) { |
Kojto | 20:30b6ed7bf8fd | 175 | while(size) |
Kojto | 20:30b6ed7bf8fd | 176 | { |
Kojto | 20:30b6ed7bf8fd | 177 | _wlan_spi.write(*data++); |
Kojto | 20:30b6ed7bf8fd | 178 | size--; |
Kojto | 20:30b6ed7bf8fd | 179 | } |
Kojto | 20:30b6ed7bf8fd | 180 | } |
Kojto | 20:30b6ed7bf8fd | 181 | |
Kojto | 20:30b6ed7bf8fd | 182 | void cc3000_spi::read_synchronous(uint8_t *data, uint16_t size) { |
Kojto | 20:30b6ed7bf8fd | 183 | for (uint32_t i = 0; i < size; i++) |
Kojto | 20:30b6ed7bf8fd | 184 | { |
Kojto | 20:30b6ed7bf8fd | 185 | data[i] = _wlan_spi.write(0x03);; |
Kojto | 20:30b6ed7bf8fd | 186 | } |
Kojto | 20:30b6ed7bf8fd | 187 | } |
Kojto | 20:30b6ed7bf8fd | 188 | |
Kojto | 20:30b6ed7bf8fd | 189 | uint32_t cc3000_spi::read_data_cont() { |
Kojto | 20:30b6ed7bf8fd | 190 | long data_to_recv; |
Kojto | 20:30b6ed7bf8fd | 191 | unsigned char *evnt_buff, type; |
Kojto | 20:30b6ed7bf8fd | 192 | |
Kojto | 20:30b6ed7bf8fd | 193 | //determine the packet type |
Kojto | 20:30b6ed7bf8fd | 194 | evnt_buff = _simple_link.get_received_buffer(); |
Kojto | 20:30b6ed7bf8fd | 195 | data_to_recv = 0; |
Kojto | 20:30b6ed7bf8fd | 196 | STREAM_TO_UINT8((uint8_t *)(evnt_buff + SPI_HEADER_SIZE), HCI_PACKET_TYPE_OFFSET, type); |
Kojto | 20:30b6ed7bf8fd | 197 | |
Kojto | 20:30b6ed7bf8fd | 198 | switch(type) |
Kojto | 20:30b6ed7bf8fd | 199 | { |
Kojto | 20:30b6ed7bf8fd | 200 | case HCI_TYPE_DATA: |
Kojto | 20:30b6ed7bf8fd | 201 | { |
Kojto | 20:30b6ed7bf8fd | 202 | // Read the remaining data.. |
Kojto | 20:30b6ed7bf8fd | 203 | STREAM_TO_UINT16((uint8_t *)(evnt_buff + SPI_HEADER_SIZE), HCI_DATA_LENGTH_OFFSET, data_to_recv); |
Kojto | 20:30b6ed7bf8fd | 204 | if (!((HEADERS_SIZE_EVNT + data_to_recv) & 1)) |
Kojto | 20:30b6ed7bf8fd | 205 | { |
Kojto | 20:30b6ed7bf8fd | 206 | data_to_recv++; |
Kojto | 20:30b6ed7bf8fd | 207 | } |
Kojto | 20:30b6ed7bf8fd | 208 | |
Kojto | 20:30b6ed7bf8fd | 209 | if (data_to_recv) |
Kojto | 20:30b6ed7bf8fd | 210 | { |
Kojto | 20:30b6ed7bf8fd | 211 | read_synchronous(evnt_buff + 10, data_to_recv); |
Kojto | 20:30b6ed7bf8fd | 212 | } |
Kojto | 20:30b6ed7bf8fd | 213 | break; |
Kojto | 20:30b6ed7bf8fd | 214 | } |
Kojto | 20:30b6ed7bf8fd | 215 | case HCI_TYPE_EVNT: |
Kojto | 20:30b6ed7bf8fd | 216 | { |
Kojto | 20:30b6ed7bf8fd | 217 | // Calculate the rest length of the data |
Kojto | 20:30b6ed7bf8fd | 218 | STREAM_TO_UINT8((char *)(evnt_buff + SPI_HEADER_SIZE), HCI_EVENT_LENGTH_OFFSET, data_to_recv); |
Kojto | 20:30b6ed7bf8fd | 219 | data_to_recv -= 1; |
Kojto | 20:30b6ed7bf8fd | 220 | // Add padding byte if needed |
Kojto | 20:30b6ed7bf8fd | 221 | if ((HEADERS_SIZE_EVNT + data_to_recv) & 1) |
Kojto | 20:30b6ed7bf8fd | 222 | { |
Kojto | 20:30b6ed7bf8fd | 223 | data_to_recv++; |
Kojto | 20:30b6ed7bf8fd | 224 | } |
Kojto | 20:30b6ed7bf8fd | 225 | |
Kojto | 20:30b6ed7bf8fd | 226 | if (data_to_recv) |
Kojto | 20:30b6ed7bf8fd | 227 | { |
Kojto | 20:30b6ed7bf8fd | 228 | read_synchronous(evnt_buff + 10, data_to_recv); |
Kojto | 20:30b6ed7bf8fd | 229 | } |
Kojto | 20:30b6ed7bf8fd | 230 | |
Kojto | 20:30b6ed7bf8fd | 231 | _spi_info.spi_state = eSPI_STATE_READ_EOT; |
Kojto | 20:30b6ed7bf8fd | 232 | break; |
Kojto | 20:30b6ed7bf8fd | 233 | } |
Kojto | 20:30b6ed7bf8fd | 234 | } |
Kojto | 20:30b6ed7bf8fd | 235 | return (0); |
Kojto | 20:30b6ed7bf8fd | 236 | } |
Kojto | 20:30b6ed7bf8fd | 237 | |
Kojto | 34:1ad18123bf11 | 238 | void cc3000_spi::set_wlan_en(uint8_t value) { |
Kojto | 20:30b6ed7bf8fd | 239 | if (value) { |
Kojto | 20:30b6ed7bf8fd | 240 | _wlan_en = 1; |
Kojto | 20:30b6ed7bf8fd | 241 | } else { |
Kojto | 20:30b6ed7bf8fd | 242 | _wlan_en = 0; |
Kojto | 20:30b6ed7bf8fd | 243 | } |
Kojto | 20:30b6ed7bf8fd | 244 | } |
Kojto | 20:30b6ed7bf8fd | 245 | |
Kojto | 20:30b6ed7bf8fd | 246 | void cc3000_spi::WLAN_IRQHandler() { |
Kojto | 20:30b6ed7bf8fd | 247 | if (_spi_info.spi_state == eSPI_STATE_POWERUP) |
Kojto | 20:30b6ed7bf8fd | 248 | { |
Kojto | 20:30b6ed7bf8fd | 249 | // Inform HCI Layer that IRQ occured after powerup |
Kojto | 20:30b6ed7bf8fd | 250 | _spi_info.spi_state = eSPI_STATE_INITIALIZED; |
Kojto | 20:30b6ed7bf8fd | 251 | } |
Kojto | 20:30b6ed7bf8fd | 252 | else if (_spi_info.spi_state == eSPI_STATE_IDLE) |
Kojto | 20:30b6ed7bf8fd | 253 | { |
Kojto | 20:30b6ed7bf8fd | 254 | _spi_info.spi_state = eSPI_STATE_READ_IRQ; |
Kojto | 20:30b6ed7bf8fd | 255 | /* IRQ line goes low - acknowledge it */ |
Kojto | 20:30b6ed7bf8fd | 256 | _wlan_cs = 0; |
Kojto | 20:30b6ed7bf8fd | 257 | read_synchronous(_simple_link.get_received_buffer(), 10); |
Kojto | 20:30b6ed7bf8fd | 258 | _spi_info.spi_state = eSPI_STATE_READ_EOT; |
Kojto | 20:30b6ed7bf8fd | 259 | |
Kojto | 20:30b6ed7bf8fd | 260 | |
Kojto | 20:30b6ed7bf8fd | 261 | // The header was read - continue with the payload read |
Kojto | 20:30b6ed7bf8fd | 262 | if (!read_data_cont()) |
Kojto | 20:30b6ed7bf8fd | 263 | { |
Kojto | 20:30b6ed7bf8fd | 264 | // All the data was read - finalize handling by switching to the task |
Kojto | 20:30b6ed7bf8fd | 265 | // Trigger Rx processing |
Kojto | 20:30b6ed7bf8fd | 266 | wlan_irq_disable(); |
Kojto | 20:30b6ed7bf8fd | 267 | _wlan_cs = 1; |
Kojto | 20:30b6ed7bf8fd | 268 | // The magic number resides at the end of the TX/RX buffer (1 byte after the allocated size) |
Kojto | 20:30b6ed7bf8fd | 269 | // If the magic number is overwitten - buffer overrun occurred - we will be stuck here forever! |
Kojto | 20:30b6ed7bf8fd | 270 | uint8_t *received_buffer = _simple_link.get_received_buffer(); |
Kojto | 20:30b6ed7bf8fd | 271 | if (received_buffer[CC3000_RX_BUFFER_SIZE - 1] != CC3000_BUFFER_MAGIC_NUMBER) |
Kojto | 20:30b6ed7bf8fd | 272 | { |
Kojto | 20:30b6ed7bf8fd | 273 | while (1); |
Kojto | 20:30b6ed7bf8fd | 274 | } |
Kojto | 20:30b6ed7bf8fd | 275 | _spi_info.spi_state = eSPI_STATE_IDLE; |
Kojto | 20:30b6ed7bf8fd | 276 | _event.received_handler(received_buffer + SPI_HEADER_SIZE); |
Kojto | 20:30b6ed7bf8fd | 277 | } |
Kojto | 20:30b6ed7bf8fd | 278 | } |
Kojto | 20:30b6ed7bf8fd | 279 | else if (_spi_info.spi_state == eSPI_STATE_WRITE_IRQ) |
Kojto | 20:30b6ed7bf8fd | 280 | { |
Kojto | 20:30b6ed7bf8fd | 281 | write_synchronous(_simple_link.get_transmit_buffer(), _spi_info.tx_packet_length); |
Kojto | 20:30b6ed7bf8fd | 282 | _spi_info.spi_state = eSPI_STATE_IDLE; |
Kojto | 20:30b6ed7bf8fd | 283 | _wlan_cs = 1; |
Kojto | 20:30b6ed7bf8fd | 284 | } |
Kojto | 20:30b6ed7bf8fd | 285 | } |
Kojto | 20:30b6ed7bf8fd | 286 | |
Kojto | 20:30b6ed7bf8fd | 287 | } |