![](/media/cache/profiles/5f55d0baa59f4bc1dc393149183f1492.jpg.50x50_q85.jpg)
wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/stm-spirit1-rf-driver/source/SimpleSpirit1.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | /*** Mbed Includes ***/ |
JMF | 0:24d3eb812fd4 | 2 | #include "SimpleSpirit1.h" |
JMF | 0:24d3eb812fd4 | 3 | #include "radio_spi.h" |
JMF | 0:24d3eb812fd4 | 4 | |
JMF | 0:24d3eb812fd4 | 5 | #define SPIRIT_GPIO_IRQ (SPIRIT_GPIO_3) |
JMF | 0:24d3eb812fd4 | 6 | |
JMF | 0:24d3eb812fd4 | 7 | static uint16_t last_state; |
JMF | 0:24d3eb812fd4 | 8 | #define SPIRIT1_STATUS() ((last_state = (uint16_t)refresh_state()) & SPIRIT1_STATE_STATEBITS) |
JMF | 0:24d3eb812fd4 | 9 | |
JMF | 0:24d3eb812fd4 | 10 | #define XO_ON (0x1) |
JMF | 0:24d3eb812fd4 | 11 | |
JMF | 0:24d3eb812fd4 | 12 | #define BUSYWAIT_UNTIL(cond, millisecs) \ |
JMF | 0:24d3eb812fd4 | 13 | do { \ |
JMF | 0:24d3eb812fd4 | 14 | uint32_t start = us_ticker_read(); \ |
JMF | 0:24d3eb812fd4 | 15 | uint32_t limit = (uint32_t)millisecs*1000U; \ |
JMF | 0:24d3eb812fd4 | 16 | while (!(cond)) { \ |
JMF | 0:24d3eb812fd4 | 17 | uint32_t now = us_ticker_read(); \ |
JMF | 0:24d3eb812fd4 | 18 | if((now - start) > limit) break; \ |
JMF | 0:24d3eb812fd4 | 19 | } \ |
JMF | 0:24d3eb812fd4 | 20 | } while(0) |
JMF | 0:24d3eb812fd4 | 21 | |
JMF | 0:24d3eb812fd4 | 22 | #define st_lib_spirit_irqs SpiritIrqs |
JMF | 0:24d3eb812fd4 | 23 | |
JMF | 0:24d3eb812fd4 | 24 | #define STATE_TIMEOUT (100) |
JMF | 0:24d3eb812fd4 | 25 | |
JMF | 0:24d3eb812fd4 | 26 | // betzw: switching force & back from standby is on some devices quite unstable |
JMF | 0:24d3eb812fd4 | 27 | #define USE_STANDBY_STATE |
JMF | 0:24d3eb812fd4 | 28 | |
JMF | 0:24d3eb812fd4 | 29 | /*** Class Implementation ***/ |
JMF | 0:24d3eb812fd4 | 30 | /** Static Class Variables **/ |
JMF | 0:24d3eb812fd4 | 31 | SimpleSpirit1 *SimpleSpirit1::_singleton = NULL; |
JMF | 0:24d3eb812fd4 | 32 | |
JMF | 0:24d3eb812fd4 | 33 | /** Constructor **/ |
JMF | 0:24d3eb812fd4 | 34 | SimpleSpirit1::SimpleSpirit1(PinName mosi, PinName miso, PinName sclk, |
JMF | 0:24d3eb812fd4 | 35 | PinName irq, PinName cs, PinName sdn, |
JMF | 0:24d3eb812fd4 | 36 | PinName led) : |
JMF | 0:24d3eb812fd4 | 37 | _spi(mosi, miso, sclk), |
JMF | 0:24d3eb812fd4 | 38 | _irq(irq), |
JMF | 0:24d3eb812fd4 | 39 | _chip_select(cs), |
JMF | 0:24d3eb812fd4 | 40 | _shut_down(sdn), |
JMF | 0:24d3eb812fd4 | 41 | _led(led), |
JMF | 0:24d3eb812fd4 | 42 | _current_irq_callback(), |
JMF | 0:24d3eb812fd4 | 43 | _rx_receiving_timeout() |
JMF | 0:24d3eb812fd4 | 44 | { |
JMF | 0:24d3eb812fd4 | 45 | } |
JMF | 0:24d3eb812fd4 | 46 | |
JMF | 0:24d3eb812fd4 | 47 | /** Init Function **/ |
JMF | 0:24d3eb812fd4 | 48 | void SimpleSpirit1::init() { |
JMF | 0:24d3eb812fd4 | 49 | /* reset irq disable counter and irq callback & disable irq */ |
JMF | 0:24d3eb812fd4 | 50 | _nr_of_irq_disables = 0; |
JMF | 0:24d3eb812fd4 | 51 | disable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 52 | |
JMF | 0:24d3eb812fd4 | 53 | /* unselect chip */ |
JMF | 0:24d3eb812fd4 | 54 | chip_unselect(); |
JMF | 0:24d3eb812fd4 | 55 | |
JMF | 0:24d3eb812fd4 | 56 | /* configure spi */ |
JMF | 0:24d3eb812fd4 | 57 | _spi.format(8, 0); /* 8-bit, mode = 0, [order = SPI_MSB] only available in mbed3 */ |
JMF | 0:24d3eb812fd4 | 58 | _spi.frequency(10000000); // 10MHz (i.e. max speed allowed for Spirit1) |
JMF | 0:24d3eb812fd4 | 59 | |
JMF | 0:24d3eb812fd4 | 60 | /* install irq handler */ |
JMF | 0:24d3eb812fd4 | 61 | _irq.mode(PullUp); |
JMF | 0:24d3eb812fd4 | 62 | _irq.fall(Callback<void()>(this, &SimpleSpirit1::IrqHandler)); |
JMF | 0:24d3eb812fd4 | 63 | |
JMF | 0:24d3eb812fd4 | 64 | /* init cube vars */ |
JMF | 0:24d3eb812fd4 | 65 | spirit_on = OFF; |
JMF | 0:24d3eb812fd4 | 66 | last_rssi = 0 ; //MGR |
JMF | 0:24d3eb812fd4 | 67 | last_sqi = 0 ; //MGR |
JMF | 0:24d3eb812fd4 | 68 | |
JMF | 0:24d3eb812fd4 | 69 | /* set frequencies */ |
JMF | 0:24d3eb812fd4 | 70 | radio_set_xtal_freq(XTAL_FREQUENCY); |
JMF | 0:24d3eb812fd4 | 71 | mgmt_set_freq_base((uint32_t)BASE_FREQUENCY); |
JMF | 0:24d3eb812fd4 | 72 | |
JMF | 0:24d3eb812fd4 | 73 | /* restart board */ |
JMF | 0:24d3eb812fd4 | 74 | enter_shutdown(); |
JMF | 0:24d3eb812fd4 | 75 | exit_shutdown(); |
JMF | 0:24d3eb812fd4 | 76 | |
JMF | 0:24d3eb812fd4 | 77 | /* soft core reset */ |
JMF | 0:24d3eb812fd4 | 78 | cmd_strobe(SPIRIT1_STROBE_SRES); |
JMF | 0:24d3eb812fd4 | 79 | |
JMF | 0:24d3eb812fd4 | 80 | /* Configures the SPIRIT1 radio part */ |
JMF | 0:24d3eb812fd4 | 81 | SRadioInit x_radio_init = { |
JMF | 0:24d3eb812fd4 | 82 | XTAL_OFFSET_PPM, |
JMF | 0:24d3eb812fd4 | 83 | (uint32_t)BASE_FREQUENCY, |
JMF | 0:24d3eb812fd4 | 84 | (uint32_t)CHANNEL_SPACE, |
JMF | 0:24d3eb812fd4 | 85 | CHANNEL_NUMBER, |
JMF | 0:24d3eb812fd4 | 86 | MODULATION_SELECT, |
JMF | 0:24d3eb812fd4 | 87 | DATARATE, |
JMF | 0:24d3eb812fd4 | 88 | (uint32_t)FREQ_DEVIATION, |
JMF | 0:24d3eb812fd4 | 89 | (uint32_t)BANDWIDTH |
JMF | 0:24d3eb812fd4 | 90 | }; |
JMF | 0:24d3eb812fd4 | 91 | radio_init(&x_radio_init); |
JMF | 0:24d3eb812fd4 | 92 | radio_set_pa_level_dbm(0,POWER_DBM); |
JMF | 0:24d3eb812fd4 | 93 | radio_set_pa_level_max_index(0); |
JMF | 0:24d3eb812fd4 | 94 | |
JMF | 0:24d3eb812fd4 | 95 | /* Configures the SPIRIT1 packet handler part*/ |
JMF | 0:24d3eb812fd4 | 96 | PktBasicInit x_basic_init = { |
JMF | 0:24d3eb812fd4 | 97 | PREAMBLE_LENGTH, |
JMF | 0:24d3eb812fd4 | 98 | SYNC_LENGTH, |
JMF | 0:24d3eb812fd4 | 99 | SYNC_WORD, |
JMF | 0:24d3eb812fd4 | 100 | LENGTH_TYPE, |
JMF | 0:24d3eb812fd4 | 101 | LENGTH_WIDTH, |
JMF | 0:24d3eb812fd4 | 102 | CRC_MODE, |
JMF | 0:24d3eb812fd4 | 103 | CONTROL_LENGTH, |
JMF | 0:24d3eb812fd4 | 104 | EN_ADDRESS, |
JMF | 0:24d3eb812fd4 | 105 | EN_FEC, |
JMF | 0:24d3eb812fd4 | 106 | EN_WHITENING |
JMF | 0:24d3eb812fd4 | 107 | }; |
JMF | 0:24d3eb812fd4 | 108 | pkt_basic_init(&x_basic_init); |
JMF | 0:24d3eb812fd4 | 109 | |
JMF | 0:24d3eb812fd4 | 110 | /* Enable the following interrupt sources, routed to GPIO */ |
JMF | 0:24d3eb812fd4 | 111 | irq_de_init(NULL); |
JMF | 0:24d3eb812fd4 | 112 | irq_clear_status(); |
JMF | 0:24d3eb812fd4 | 113 | irq_set_status(TX_DATA_SENT, S_ENABLE); |
JMF | 0:24d3eb812fd4 | 114 | irq_set_status(RX_DATA_READY,S_ENABLE); |
JMF | 0:24d3eb812fd4 | 115 | irq_set_status(RX_DATA_DISC, S_ENABLE); |
JMF | 0:24d3eb812fd4 | 116 | irq_set_status(VALID_SYNC, S_ENABLE); |
JMF | 0:24d3eb812fd4 | 117 | irq_set_status(TX_FIFO_ERROR, S_ENABLE); |
JMF | 0:24d3eb812fd4 | 118 | irq_set_status(RX_FIFO_ERROR, S_ENABLE); |
JMF | 0:24d3eb812fd4 | 119 | #ifndef RX_FIFO_THR_WA |
JMF | 0:24d3eb812fd4 | 120 | irq_set_status(TX_FIFO_ALMOST_EMPTY, S_ENABLE); |
JMF | 0:24d3eb812fd4 | 121 | irq_set_status(RX_FIFO_ALMOST_FULL, S_ENABLE); |
JMF | 0:24d3eb812fd4 | 122 | #endif // !RX_FIFO_THR_WA |
JMF | 0:24d3eb812fd4 | 123 | |
JMF | 0:24d3eb812fd4 | 124 | /* Configure Spirit1 */ |
JMF | 0:24d3eb812fd4 | 125 | radio_persistent_rx(S_ENABLE); |
JMF | 0:24d3eb812fd4 | 126 | qi_set_sqi_threshold(SQI_TH_0); |
JMF | 0:24d3eb812fd4 | 127 | qi_sqi_check(S_ENABLE); |
JMF | 0:24d3eb812fd4 | 128 | qi_set_rssi_threshold_dbm(CCA_THRESHOLD); |
JMF | 0:24d3eb812fd4 | 129 | timer_set_rx_timeout_stop_condition(SQI_ABOVE_THRESHOLD); |
JMF | 0:24d3eb812fd4 | 130 | timer_set_infinite_rx_timeout(); |
JMF | 0:24d3eb812fd4 | 131 | radio_afc_freeze_on_sync(S_ENABLE); |
JMF | 0:24d3eb812fd4 | 132 | calibration_rco(S_ENABLE); |
JMF | 0:24d3eb812fd4 | 133 | |
JMF | 0:24d3eb812fd4 | 134 | CLEAR_TXBUF(); |
JMF | 0:24d3eb812fd4 | 135 | CLEAR_RXBUF(); |
JMF | 0:24d3eb812fd4 | 136 | _spirit_tx_started = false; |
JMF | 0:24d3eb812fd4 | 137 | _is_receiving = false; |
JMF | 0:24d3eb812fd4 | 138 | |
JMF | 0:24d3eb812fd4 | 139 | /* Configure the radio to route the IRQ signal to its GPIO 3 */ |
JMF | 0:24d3eb812fd4 | 140 | SGpioInit x_gpio_init = { |
JMF | 0:24d3eb812fd4 | 141 | SPIRIT_GPIO_IRQ, |
JMF | 0:24d3eb812fd4 | 142 | SPIRIT_GPIO_MODE_DIGITAL_OUTPUT_LP, |
JMF | 0:24d3eb812fd4 | 143 | SPIRIT_GPIO_DIG_OUT_IRQ |
JMF | 0:24d3eb812fd4 | 144 | }; |
JMF | 0:24d3eb812fd4 | 145 | spirit_gpio_init(&x_gpio_init); |
JMF | 0:24d3eb812fd4 | 146 | |
JMF | 0:24d3eb812fd4 | 147 | /* Setup CSMA/CA */ |
JMF | 0:24d3eb812fd4 | 148 | CsmaInit x_csma_init = { |
JMF | 0:24d3eb812fd4 | 149 | S_ENABLE, // enable persistent mode |
JMF | 0:24d3eb812fd4 | 150 | TBIT_TIME_64, // Tcca time |
JMF | 0:24d3eb812fd4 | 151 | TCCA_TIME_3, // Lcca length |
JMF | 0:24d3eb812fd4 | 152 | 5, // max nr of backoffs (<8) |
JMF | 0:24d3eb812fd4 | 153 | 1, // BU counter seed |
JMF | 0:24d3eb812fd4 | 154 | 8 // BU prescaler |
JMF | 0:24d3eb812fd4 | 155 | }; |
JMF | 0:24d3eb812fd4 | 156 | csma_ca_init(&x_csma_init); |
JMF | 0:24d3eb812fd4 | 157 | |
JMF | 0:24d3eb812fd4 | 158 | #ifdef USE_STANDBY_STATE |
JMF | 0:24d3eb812fd4 | 159 | /* Puts the SPIRIT1 in STANDBY mode (125us -> rx/tx) */ |
JMF | 0:24d3eb812fd4 | 160 | cmd_strobe(SPIRIT1_STROBE_STANDBY); |
JMF | 0:24d3eb812fd4 | 161 | #endif // USE_STANDBY_STATE |
JMF | 0:24d3eb812fd4 | 162 | } |
JMF | 0:24d3eb812fd4 | 163 | |
JMF | 0:24d3eb812fd4 | 164 | static volatile int tx_fifo_remaining = 0; // to be used in irq handler |
JMF | 0:24d3eb812fd4 | 165 | static volatile int tx_buffer_pos = 0; // to be used in irq handler |
JMF | 0:24d3eb812fd4 | 166 | static const volatile uint8_t *tx_fifo_buffer = NULL; // to be used in irq handler |
JMF | 0:24d3eb812fd4 | 167 | int SimpleSpirit1::send(const void *payload, unsigned int payload_len, bool use_csma_ca) { |
JMF | 0:24d3eb812fd4 | 168 | /* Checks if the payload length is supported */ |
JMF | 0:24d3eb812fd4 | 169 | if(payload_len > MAX_PACKET_LEN) { |
JMF | 0:24d3eb812fd4 | 170 | return RADIO_TX_ERR; |
JMF | 0:24d3eb812fd4 | 171 | } |
JMF | 0:24d3eb812fd4 | 172 | |
JMF | 0:24d3eb812fd4 | 173 | disable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 174 | |
JMF | 0:24d3eb812fd4 | 175 | BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 176 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 177 | if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) { |
JMF | 0:24d3eb812fd4 | 178 | debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1); |
JMF | 0:24d3eb812fd4 | 179 | } |
JMF | 0:24d3eb812fd4 | 180 | #endif |
JMF | 0:24d3eb812fd4 | 181 | |
JMF | 0:24d3eb812fd4 | 182 | /* Reset State to Ready */ |
JMF | 0:24d3eb812fd4 | 183 | set_ready_state(); |
JMF | 0:24d3eb812fd4 | 184 | |
JMF | 0:24d3eb812fd4 | 185 | cmd_strobe(SPIRIT1_STROBE_FTX); // flush TX FIFO buffer |
JMF | 0:24d3eb812fd4 | 186 | |
JMF | 0:24d3eb812fd4 | 187 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 188 | debug_if(!(linear_fifo_read_num_elements_tx_fifo() == 0), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 189 | #endif |
JMF | 0:24d3eb812fd4 | 190 | |
JMF | 0:24d3eb812fd4 | 191 | pkt_basic_set_payload_length(payload_len); // set desired payload len |
JMF | 0:24d3eb812fd4 | 192 | |
JMF | 0:24d3eb812fd4 | 193 | if(use_csma_ca) { |
JMF | 0:24d3eb812fd4 | 194 | csma_ca_state(S_ENABLE); // enable CSMA/CA |
JMF | 0:24d3eb812fd4 | 195 | } |
JMF | 0:24d3eb812fd4 | 196 | |
JMF | 0:24d3eb812fd4 | 197 | /* Init buffer & number of bytes to be send */ |
JMF | 0:24d3eb812fd4 | 198 | tx_fifo_remaining = payload_len; |
JMF | 0:24d3eb812fd4 | 199 | tx_fifo_buffer = (const uint8_t*)payload; |
JMF | 0:24d3eb812fd4 | 200 | |
JMF | 0:24d3eb812fd4 | 201 | int8_t fifo_available = SPIRIT_MAX_FIFO_LEN; // fill-up whole fifo |
JMF | 0:24d3eb812fd4 | 202 | int8_t to_send = (tx_fifo_remaining > fifo_available) ? fifo_available : tx_fifo_remaining; |
JMF | 0:24d3eb812fd4 | 203 | |
JMF | 0:24d3eb812fd4 | 204 | tx_fifo_remaining -= to_send; |
JMF | 0:24d3eb812fd4 | 205 | |
JMF | 0:24d3eb812fd4 | 206 | /* Fill FIFO Buffer */ |
JMF | 0:24d3eb812fd4 | 207 | if(to_send > 0) { |
JMF | 0:24d3eb812fd4 | 208 | spi_write_linear_fifo(to_send, (uint8_t*)&tx_fifo_buffer[0]); |
JMF | 0:24d3eb812fd4 | 209 | } |
JMF | 0:24d3eb812fd4 | 210 | |
JMF | 0:24d3eb812fd4 | 211 | tx_buffer_pos = to_send; |
JMF | 0:24d3eb812fd4 | 212 | _spirit_tx_started = true; |
JMF | 0:24d3eb812fd4 | 213 | |
JMF | 0:24d3eb812fd4 | 214 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 215 | |
JMF | 0:24d3eb812fd4 | 216 | /* Start transmitting */ |
JMF | 0:24d3eb812fd4 | 217 | cmd_strobe(SPIRIT1_STROBE_TX); |
JMF | 0:24d3eb812fd4 | 218 | |
JMF | 0:24d3eb812fd4 | 219 | while(tx_fifo_remaining != 0); // wait until not everything is yet send (evtl. by irq handler) |
JMF | 0:24d3eb812fd4 | 220 | |
JMF | 0:24d3eb812fd4 | 221 | BUSYWAIT_UNTIL(!_spirit_tx_started, STATE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 222 | #ifdef HEAVY_DEBUG |
JMF | 0:24d3eb812fd4 | 223 | debug("\r\n%s (%d): state=%x, _spirit_tx_started=%d\r\n", __func__, __LINE__, SPIRIT1_STATUS()>>1, _spirit_tx_started); |
JMF | 0:24d3eb812fd4 | 224 | #endif |
JMF | 0:24d3eb812fd4 | 225 | |
JMF | 0:24d3eb812fd4 | 226 | if(use_csma_ca) { |
JMF | 0:24d3eb812fd4 | 227 | csma_ca_state(S_DISABLE); // disable CSMA/CA |
JMF | 0:24d3eb812fd4 | 228 | } |
JMF | 0:24d3eb812fd4 | 229 | |
JMF | 0:24d3eb812fd4 | 230 | cmd_strobe(SPIRIT1_STROBE_RX); // Return to RX state |
JMF | 0:24d3eb812fd4 | 231 | |
JMF | 0:24d3eb812fd4 | 232 | disable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 233 | if(_spirit_tx_started) { // in case of state timeout |
JMF | 0:24d3eb812fd4 | 234 | _spirit_tx_started = false; |
JMF | 0:24d3eb812fd4 | 235 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 236 | return RADIO_TX_ERR; |
JMF | 0:24d3eb812fd4 | 237 | } else { |
JMF | 0:24d3eb812fd4 | 238 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 239 | return RADIO_TX_OK; |
JMF | 0:24d3eb812fd4 | 240 | } |
JMF | 0:24d3eb812fd4 | 241 | } |
JMF | 0:24d3eb812fd4 | 242 | |
JMF | 0:24d3eb812fd4 | 243 | /** Set Ready State **/ |
JMF | 0:24d3eb812fd4 | 244 | void SimpleSpirit1::set_ready_state(void) { |
JMF | 0:24d3eb812fd4 | 245 | uint16_t state; |
JMF | 0:24d3eb812fd4 | 246 | |
JMF | 0:24d3eb812fd4 | 247 | disable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 248 | |
JMF | 0:24d3eb812fd4 | 249 | _spirit_tx_started = false; |
JMF | 0:24d3eb812fd4 | 250 | _is_receiving = false; |
JMF | 0:24d3eb812fd4 | 251 | stop_rx_timeout(); |
JMF | 0:24d3eb812fd4 | 252 | |
JMF | 0:24d3eb812fd4 | 253 | cmd_strobe(SPIRIT1_STROBE_FRX); |
JMF | 0:24d3eb812fd4 | 254 | CLEAR_RXBUF(); |
JMF | 0:24d3eb812fd4 | 255 | CLEAR_TXBUF(); |
JMF | 0:24d3eb812fd4 | 256 | |
JMF | 0:24d3eb812fd4 | 257 | state = SPIRIT1_STATUS(); |
JMF | 0:24d3eb812fd4 | 258 | if(state == SPIRIT1_STATE_STANDBY) { |
JMF | 0:24d3eb812fd4 | 259 | cmd_strobe(SPIRIT1_STROBE_READY); |
JMF | 0:24d3eb812fd4 | 260 | } else if(state == SPIRIT1_STATE_RX) { |
JMF | 0:24d3eb812fd4 | 261 | cmd_strobe(SPIRIT1_STROBE_SABORT); |
JMF | 0:24d3eb812fd4 | 262 | } else if(state != SPIRIT1_STATE_READY) { |
JMF | 0:24d3eb812fd4 | 263 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 264 | debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, state>>1); |
JMF | 0:24d3eb812fd4 | 265 | #endif |
JMF | 0:24d3eb812fd4 | 266 | } |
JMF | 0:24d3eb812fd4 | 267 | |
JMF | 0:24d3eb812fd4 | 268 | BUSYWAIT_UNTIL((SPIRIT1_STATUS() == SPIRIT1_STATE_READY) && ((last_state & XO_ON) == XO_ON), STATE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 269 | if(last_state != (SPIRIT1_STATE_READY | XO_ON)) { |
JMF | 0:24d3eb812fd4 | 270 | error("\r\nSpirit1: failed to become ready (%x) => pls. reset!\r\n", last_state); |
JMF | 0:24d3eb812fd4 | 271 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 272 | return; |
JMF | 0:24d3eb812fd4 | 273 | } |
JMF | 0:24d3eb812fd4 | 274 | |
JMF | 0:24d3eb812fd4 | 275 | irq_clear_status(); |
JMF | 0:24d3eb812fd4 | 276 | |
JMF | 0:24d3eb812fd4 | 277 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 278 | } |
JMF | 0:24d3eb812fd4 | 279 | |
JMF | 0:24d3eb812fd4 | 280 | int SimpleSpirit1::off(void) { |
JMF | 0:24d3eb812fd4 | 281 | if(spirit_on == ON) { |
JMF | 0:24d3eb812fd4 | 282 | /* Disables the mcu to get IRQ from the SPIRIT1 */ |
JMF | 0:24d3eb812fd4 | 283 | disable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 284 | |
JMF | 0:24d3eb812fd4 | 285 | /* first stop rx/tx */ |
JMF | 0:24d3eb812fd4 | 286 | set_ready_state(); |
JMF | 0:24d3eb812fd4 | 287 | |
JMF | 0:24d3eb812fd4 | 288 | #ifdef USE_STANDBY_STATE |
JMF | 0:24d3eb812fd4 | 289 | /* Puts the SPIRIT1 in STANDBY */ |
JMF | 0:24d3eb812fd4 | 290 | cmd_strobe(SPIRIT1_STROBE_STANDBY); |
JMF | 0:24d3eb812fd4 | 291 | BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_STANDBY, STATE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 292 | if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_STANDBY) { |
JMF | 0:24d3eb812fd4 | 293 | error("\r\nSpirit1: failed to enter standby (%x)\r\n", last_state>>1); |
JMF | 0:24d3eb812fd4 | 294 | return 1; |
JMF | 0:24d3eb812fd4 | 295 | } |
JMF | 0:24d3eb812fd4 | 296 | #endif // USE_STANDBY_STATE |
JMF | 0:24d3eb812fd4 | 297 | |
JMF | 0:24d3eb812fd4 | 298 | spirit_on = OFF; |
JMF | 0:24d3eb812fd4 | 299 | _nr_of_irq_disables = 1; |
JMF | 0:24d3eb812fd4 | 300 | } |
JMF | 0:24d3eb812fd4 | 301 | return 0; |
JMF | 0:24d3eb812fd4 | 302 | } |
JMF | 0:24d3eb812fd4 | 303 | |
JMF | 0:24d3eb812fd4 | 304 | int SimpleSpirit1::on(void) { |
JMF | 0:24d3eb812fd4 | 305 | if(spirit_on == OFF) { |
JMF | 0:24d3eb812fd4 | 306 | set_ready_state(); |
JMF | 0:24d3eb812fd4 | 307 | |
JMF | 0:24d3eb812fd4 | 308 | /* now we go to Rx */ |
JMF | 0:24d3eb812fd4 | 309 | cmd_strobe(SPIRIT1_STROBE_RX); |
JMF | 0:24d3eb812fd4 | 310 | |
JMF | 0:24d3eb812fd4 | 311 | BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 312 | if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) { |
JMF | 0:24d3eb812fd4 | 313 | error("\r\nSpirit1: failed to enter rx (%x) => retry\r\n", last_state>>1); |
JMF | 0:24d3eb812fd4 | 314 | } |
JMF | 0:24d3eb812fd4 | 315 | |
JMF | 0:24d3eb812fd4 | 316 | /* Enables the mcu to get IRQ from the SPIRIT1 */ |
JMF | 0:24d3eb812fd4 | 317 | spirit_on = ON; |
JMF | 0:24d3eb812fd4 | 318 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 319 | debug_if(!(_nr_of_irq_disables == 1), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 320 | #endif |
JMF | 0:24d3eb812fd4 | 321 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 322 | } |
JMF | 0:24d3eb812fd4 | 323 | |
JMF | 0:24d3eb812fd4 | 324 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 325 | if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) { |
JMF | 0:24d3eb812fd4 | 326 | debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1); |
JMF | 0:24d3eb812fd4 | 327 | } |
JMF | 0:24d3eb812fd4 | 328 | #endif |
JMF | 0:24d3eb812fd4 | 329 | |
JMF | 0:24d3eb812fd4 | 330 | return 0; |
JMF | 0:24d3eb812fd4 | 331 | } |
JMF | 0:24d3eb812fd4 | 332 | |
JMF | 0:24d3eb812fd4 | 333 | uint8_t SimpleSpirit1::refresh_state(void) { |
JMF | 0:24d3eb812fd4 | 334 | uint8_t mcstate; |
JMF | 0:24d3eb812fd4 | 335 | |
JMF | 0:24d3eb812fd4 | 336 | SpiritSpiReadRegisters(MC_STATE0_BASE, 1, &mcstate); |
JMF | 0:24d3eb812fd4 | 337 | |
JMF | 0:24d3eb812fd4 | 338 | return mcstate; |
JMF | 0:24d3eb812fd4 | 339 | } |
JMF | 0:24d3eb812fd4 | 340 | |
JMF | 0:24d3eb812fd4 | 341 | int SimpleSpirit1::read(void *buf, unsigned int bufsize) |
JMF | 0:24d3eb812fd4 | 342 | { |
JMF | 0:24d3eb812fd4 | 343 | disable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 344 | |
JMF | 0:24d3eb812fd4 | 345 | /* Checks if the RX buffer is empty */ |
JMF | 0:24d3eb812fd4 | 346 | if(IS_RXBUF_EMPTY()) { |
JMF | 0:24d3eb812fd4 | 347 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 348 | debug("\r\nBuffer is empty\r\n"); |
JMF | 0:24d3eb812fd4 | 349 | #endif |
JMF | 0:24d3eb812fd4 | 350 | set_ready_state(); |
JMF | 0:24d3eb812fd4 | 351 | |
JMF | 0:24d3eb812fd4 | 352 | cmd_strobe(SPIRIT1_STROBE_RX); |
JMF | 0:24d3eb812fd4 | 353 | BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 354 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 355 | return 0; |
JMF | 0:24d3eb812fd4 | 356 | } |
JMF | 0:24d3eb812fd4 | 357 | |
JMF | 0:24d3eb812fd4 | 358 | if(bufsize < spirit_rx_len) { |
JMF | 0:24d3eb812fd4 | 359 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 360 | |
JMF | 0:24d3eb812fd4 | 361 | /* If buf has the correct size */ |
JMF | 0:24d3eb812fd4 | 362 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 363 | debug("\r\nTOO SMALL BUF\r\n"); |
JMF | 0:24d3eb812fd4 | 364 | #endif |
JMF | 0:24d3eb812fd4 | 365 | return 0; |
JMF | 0:24d3eb812fd4 | 366 | } else { |
JMF | 0:24d3eb812fd4 | 367 | /* Copies the packet received */ |
JMF | 0:24d3eb812fd4 | 368 | memcpy(buf, spirit_rx_buf, spirit_rx_len); |
JMF | 0:24d3eb812fd4 | 369 | |
JMF | 0:24d3eb812fd4 | 370 | bufsize = spirit_rx_len; |
JMF | 0:24d3eb812fd4 | 371 | CLEAR_RXBUF(); |
JMF | 0:24d3eb812fd4 | 372 | |
JMF | 0:24d3eb812fd4 | 373 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 374 | |
JMF | 0:24d3eb812fd4 | 375 | return bufsize; |
JMF | 0:24d3eb812fd4 | 376 | } |
JMF | 0:24d3eb812fd4 | 377 | |
JMF | 0:24d3eb812fd4 | 378 | } |
JMF | 0:24d3eb812fd4 | 379 | |
JMF | 0:24d3eb812fd4 | 380 | /* betzw - TODO: CCA should be reviewed (completely)! */ |
JMF | 0:24d3eb812fd4 | 381 | int SimpleSpirit1::channel_clear(void) |
JMF | 0:24d3eb812fd4 | 382 | { |
JMF | 0:24d3eb812fd4 | 383 | float rssi_value; |
JMF | 0:24d3eb812fd4 | 384 | /* Local variable used to memorize the SPIRIT1 state */ |
JMF | 0:24d3eb812fd4 | 385 | uint8_t spirit_state = ON; |
JMF | 0:24d3eb812fd4 | 386 | |
JMF | 0:24d3eb812fd4 | 387 | if(spirit_on == OFF) { |
JMF | 0:24d3eb812fd4 | 388 | /* Wakes up the SPIRIT1 */ |
JMF | 0:24d3eb812fd4 | 389 | on(); |
JMF | 0:24d3eb812fd4 | 390 | spirit_state = OFF; |
JMF | 0:24d3eb812fd4 | 391 | } |
JMF | 0:24d3eb812fd4 | 392 | |
JMF | 0:24d3eb812fd4 | 393 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 394 | if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) { |
JMF | 0:24d3eb812fd4 | 395 | debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1); |
JMF | 0:24d3eb812fd4 | 396 | } |
JMF | 0:24d3eb812fd4 | 397 | #endif |
JMF | 0:24d3eb812fd4 | 398 | |
JMF | 0:24d3eb812fd4 | 399 | disable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 400 | |
JMF | 0:24d3eb812fd4 | 401 | /* Reset State to Ready */ |
JMF | 0:24d3eb812fd4 | 402 | set_ready_state(); |
JMF | 0:24d3eb812fd4 | 403 | |
JMF | 0:24d3eb812fd4 | 404 | /* Stores the RSSI value */ |
JMF | 0:24d3eb812fd4 | 405 | rssi_value = qi_get_rssi_dbm(); |
JMF | 0:24d3eb812fd4 | 406 | |
JMF | 0:24d3eb812fd4 | 407 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 408 | |
JMF | 0:24d3eb812fd4 | 409 | /* Puts the SPIRIT1 in its previous state */ |
JMF | 0:24d3eb812fd4 | 410 | if(spirit_state == OFF) { |
JMF | 0:24d3eb812fd4 | 411 | off(); |
JMF | 0:24d3eb812fd4 | 412 | |
JMF | 0:24d3eb812fd4 | 413 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 414 | #ifdef USE_STANDBY_STATE |
JMF | 0:24d3eb812fd4 | 415 | if(SPIRIT1_STATUS() != SPIRIT1_STATE_STANDBY) { |
JMF | 0:24d3eb812fd4 | 416 | debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1); |
JMF | 0:24d3eb812fd4 | 417 | } |
JMF | 0:24d3eb812fd4 | 418 | #else // !USE_STANDBY_STATE |
JMF | 0:24d3eb812fd4 | 419 | if(SPIRIT1_STATUS() != SPIRIT1_STATE_READY) { |
JMF | 0:24d3eb812fd4 | 420 | debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1); |
JMF | 0:24d3eb812fd4 | 421 | } |
JMF | 0:24d3eb812fd4 | 422 | #endif // !USE_STANDBY_STATE |
JMF | 0:24d3eb812fd4 | 423 | #endif // NDEBUG |
JMF | 0:24d3eb812fd4 | 424 | } else { // spirit_state != OFF |
JMF | 0:24d3eb812fd4 | 425 | disable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 426 | |
JMF | 0:24d3eb812fd4 | 427 | set_ready_state(); |
JMF | 0:24d3eb812fd4 | 428 | |
JMF | 0:24d3eb812fd4 | 429 | cmd_strobe(SPIRIT1_STROBE_RX); |
JMF | 0:24d3eb812fd4 | 430 | BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT); |
JMF | 0:24d3eb812fd4 | 431 | if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) { |
JMF | 0:24d3eb812fd4 | 432 | error("\r\nSpirit1: (#2) failed to enter rx (%x) => retry\r\n", last_state>>1); |
JMF | 0:24d3eb812fd4 | 433 | } |
JMF | 0:24d3eb812fd4 | 434 | |
JMF | 0:24d3eb812fd4 | 435 | enable_spirit_irq(); |
JMF | 0:24d3eb812fd4 | 436 | |
JMF | 0:24d3eb812fd4 | 437 | #ifndef NDEBUG |
JMF | 0:24d3eb812fd4 | 438 | if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) { |
JMF | 0:24d3eb812fd4 | 439 | debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1); |
JMF | 0:24d3eb812fd4 | 440 | } |
JMF | 0:24d3eb812fd4 | 441 | #endif |
JMF | 0:24d3eb812fd4 | 442 | } |
JMF | 0:24d3eb812fd4 | 443 | |
JMF | 0:24d3eb812fd4 | 444 | /* If the received signal strength is above a certain level the medium is considered busy! */ |
JMF | 0:24d3eb812fd4 | 445 | /* Compare RSSI value with threshold */ |
JMF | 0:24d3eb812fd4 | 446 | if(rssi_value < CCA_THRESHOLD) { |
JMF | 0:24d3eb812fd4 | 447 | return 0; // idle |
JMF | 0:24d3eb812fd4 | 448 | } else { |
JMF | 0:24d3eb812fd4 | 449 | return 1; // busy |
JMF | 0:24d3eb812fd4 | 450 | } |
JMF | 0:24d3eb812fd4 | 451 | } |
JMF | 0:24d3eb812fd4 | 452 | |
JMF | 0:24d3eb812fd4 | 453 | int SimpleSpirit1::get_pending_packet(void) |
JMF | 0:24d3eb812fd4 | 454 | { |
JMF | 0:24d3eb812fd4 | 455 | return !IS_RXBUF_EMPTY(); |
JMF | 0:24d3eb812fd4 | 456 | } |
JMF | 0:24d3eb812fd4 | 457 | |
JMF | 0:24d3eb812fd4 | 458 | /** Spirit Irq Callback **/ |
JMF | 0:24d3eb812fd4 | 459 | /* betzw - TODO: use threaded interrupt handling when `MBED_CONF_RTOS_PRESENT` is defined (see `atmel-rf-driver`) */ |
JMF | 0:24d3eb812fd4 | 460 | void SimpleSpirit1::IrqHandler() { |
JMF | 0:24d3eb812fd4 | 461 | st_lib_spirit_irqs x_irq_status; |
JMF | 0:24d3eb812fd4 | 462 | |
JMF | 0:24d3eb812fd4 | 463 | /* get interrupt source from radio */ |
JMF | 0:24d3eb812fd4 | 464 | irq_get_status(&x_irq_status); |
JMF | 0:24d3eb812fd4 | 465 | |
JMF | 0:24d3eb812fd4 | 466 | /* The IRQ_TX_DATA_SENT notifies the packet has been sent. Puts the SPIRIT1 in RX */ |
JMF | 0:24d3eb812fd4 | 467 | if(x_irq_status.IRQ_TX_DATA_SENT) { /* betzw - NOTE: MUST be handled before `IRQ_RX_DATA_READY` for Nanostack integration! |
JMF | 0:24d3eb812fd4 | 468 | Logically, Nanostack only expects the "DONE" after "SUCCESS" (if it gets |
JMF | 0:24d3eb812fd4 | 469 | DONE before SUCCESS, it assumes you're not going to bother to send SUCCESS). |
JMF | 0:24d3eb812fd4 | 470 | */ |
JMF | 0:24d3eb812fd4 | 471 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 472 | uint32_t *tmp = (uint32_t*)&x_irq_status; |
JMF | 0:24d3eb812fd4 | 473 | debug_if(!((*tmp) & IRQ_TX_DATA_SENT_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 474 | debug_if(tx_fifo_remaining != 0, "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 475 | #endif |
JMF | 0:24d3eb812fd4 | 476 | |
JMF | 0:24d3eb812fd4 | 477 | if(_spirit_tx_started) { |
JMF | 0:24d3eb812fd4 | 478 | _spirit_tx_started = false; |
JMF | 0:24d3eb812fd4 | 479 | |
JMF | 0:24d3eb812fd4 | 480 | /* call user callback */ |
JMF | 0:24d3eb812fd4 | 481 | if(_current_irq_callback) { |
JMF | 0:24d3eb812fd4 | 482 | _current_irq_callback(TX_DONE); |
JMF | 0:24d3eb812fd4 | 483 | } |
JMF | 0:24d3eb812fd4 | 484 | } |
JMF | 0:24d3eb812fd4 | 485 | |
JMF | 0:24d3eb812fd4 | 486 | /* Disable handling of other TX flags */ |
JMF | 0:24d3eb812fd4 | 487 | x_irq_status.IRQ_TX_FIFO_ALMOST_EMPTY = S_RESET; |
JMF | 0:24d3eb812fd4 | 488 | tx_fifo_buffer = NULL; |
JMF | 0:24d3eb812fd4 | 489 | } |
JMF | 0:24d3eb812fd4 | 490 | |
JMF | 0:24d3eb812fd4 | 491 | #ifndef RX_FIFO_THR_WA |
JMF | 0:24d3eb812fd4 | 492 | /* The IRQ_TX_FIFO_ALMOST_EMPTY notifies an nearly empty TX fifo */ |
JMF | 0:24d3eb812fd4 | 493 | if(x_irq_status.IRQ_TX_FIFO_ALMOST_EMPTY) { |
JMF | 0:24d3eb812fd4 | 494 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 495 | uint32_t *tmp = (uint32_t*)&x_irq_status; |
JMF | 0:24d3eb812fd4 | 496 | debug_if(!((*tmp) & IRQ_TX_FIFO_ALMOST_EMPTY_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 497 | debug_if(!_spirit_tx_started, "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 498 | debug_if(tx_fifo_buffer == NULL, "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 499 | #endif |
JMF | 0:24d3eb812fd4 | 500 | |
JMF | 0:24d3eb812fd4 | 501 | int8_t fifo_available = SPIRIT_MAX_FIFO_LEN/2; // fill-up half fifo |
JMF | 0:24d3eb812fd4 | 502 | int8_t to_send = (tx_fifo_remaining > fifo_available) ? fifo_available : tx_fifo_remaining; |
JMF | 0:24d3eb812fd4 | 503 | |
JMF | 0:24d3eb812fd4 | 504 | tx_fifo_remaining -= to_send; |
JMF | 0:24d3eb812fd4 | 505 | |
JMF | 0:24d3eb812fd4 | 506 | /* Fill FIFO Buffer */ |
JMF | 0:24d3eb812fd4 | 507 | if(to_send > 0) { |
JMF | 0:24d3eb812fd4 | 508 | spi_write_linear_fifo(to_send, (uint8_t*)&tx_fifo_buffer[tx_buffer_pos]); |
JMF | 0:24d3eb812fd4 | 509 | } |
JMF | 0:24d3eb812fd4 | 510 | tx_buffer_pos += to_send; |
JMF | 0:24d3eb812fd4 | 511 | } |
JMF | 0:24d3eb812fd4 | 512 | #endif // !RX_FIFO_THR_WA |
JMF | 0:24d3eb812fd4 | 513 | |
JMF | 0:24d3eb812fd4 | 514 | /* TX FIFO underflow/overflow error */ |
JMF | 0:24d3eb812fd4 | 515 | if(x_irq_status.IRQ_TX_FIFO_ERROR) { |
JMF | 0:24d3eb812fd4 | 516 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 517 | uint32_t *tmp = (uint32_t*)&x_irq_status; |
JMF | 0:24d3eb812fd4 | 518 | debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp); |
JMF | 0:24d3eb812fd4 | 519 | debug_if(!((*tmp) & IRQ_TX_FIFO_ERROR_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 520 | #endif |
JMF | 0:24d3eb812fd4 | 521 | if(_spirit_tx_started) { |
JMF | 0:24d3eb812fd4 | 522 | _spirit_tx_started = false; |
JMF | 0:24d3eb812fd4 | 523 | /* call user callback */ |
JMF | 0:24d3eb812fd4 | 524 | if(_current_irq_callback) { |
JMF | 0:24d3eb812fd4 | 525 | _current_irq_callback(TX_ERR); |
JMF | 0:24d3eb812fd4 | 526 | } |
JMF | 0:24d3eb812fd4 | 527 | } |
JMF | 0:24d3eb812fd4 | 528 | |
JMF | 0:24d3eb812fd4 | 529 | /* reset data still to be sent */ |
JMF | 0:24d3eb812fd4 | 530 | tx_fifo_remaining = 0; |
JMF | 0:24d3eb812fd4 | 531 | } |
JMF | 0:24d3eb812fd4 | 532 | |
JMF | 0:24d3eb812fd4 | 533 | /* The IRQ_RX_DATA_READY notifies a new packet arrived */ |
JMF | 0:24d3eb812fd4 | 534 | if(x_irq_status.IRQ_RX_DATA_READY) { |
JMF | 0:24d3eb812fd4 | 535 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 536 | uint32_t *tmp = (uint32_t*)&x_irq_status; |
JMF | 0:24d3eb812fd4 | 537 | debug_if(!((*tmp) & IRQ_RX_DATA_READY_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 538 | #endif |
JMF | 0:24d3eb812fd4 | 539 | |
JMF | 0:24d3eb812fd4 | 540 | if(!_is_receiving) { // spurious irq?!? (betzw: see comments on macro 'RX_FIFO_THR_WA'!) |
JMF | 0:24d3eb812fd4 | 541 | #ifdef HEAVY_DEBUG |
JMF | 0:24d3eb812fd4 | 542 | debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp); |
JMF | 0:24d3eb812fd4 | 543 | #endif |
JMF | 0:24d3eb812fd4 | 544 | } else { |
JMF | 0:24d3eb812fd4 | 545 | _is_receiving = false; // Finished receiving |
JMF | 0:24d3eb812fd4 | 546 | stop_rx_timeout(); |
JMF | 0:24d3eb812fd4 | 547 | |
JMF | 0:24d3eb812fd4 | 548 | spirit_rx_len = pkt_basic_get_received_pkt_length(); |
JMF | 0:24d3eb812fd4 | 549 | |
JMF | 0:24d3eb812fd4 | 550 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 551 | debug_if(!(spirit_rx_len <= MAX_PACKET_LEN), "\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp); |
JMF | 0:24d3eb812fd4 | 552 | #endif |
JMF | 0:24d3eb812fd4 | 553 | |
JMF | 0:24d3eb812fd4 | 554 | if(spirit_rx_len <= MAX_PACKET_LEN) { |
JMF | 0:24d3eb812fd4 | 555 | uint8_t to_receive = spirit_rx_len - _spirit_rx_pos; |
JMF | 0:24d3eb812fd4 | 556 | if(to_receive > 0) { |
JMF | 0:24d3eb812fd4 | 557 | spi_read_linear_fifo(to_receive, &spirit_rx_buf[_spirit_rx_pos]); |
JMF | 0:24d3eb812fd4 | 558 | _spirit_rx_pos += to_receive; |
JMF | 0:24d3eb812fd4 | 559 | } |
JMF | 0:24d3eb812fd4 | 560 | } |
JMF | 0:24d3eb812fd4 | 561 | |
JMF | 0:24d3eb812fd4 | 562 | cmd_strobe(SPIRIT1_STROBE_FRX); |
JMF | 0:24d3eb812fd4 | 563 | |
JMF | 0:24d3eb812fd4 | 564 | last_rssi = qi_get_rssi(); //MGR |
JMF | 0:24d3eb812fd4 | 565 | last_sqi = qi_get_sqi(); //MGR |
JMF | 0:24d3eb812fd4 | 566 | |
JMF | 0:24d3eb812fd4 | 567 | /* call user callback */ |
JMF | 0:24d3eb812fd4 | 568 | if((_spirit_rx_pos == spirit_rx_len) && _current_irq_callback) { |
JMF | 0:24d3eb812fd4 | 569 | _current_irq_callback(RX_DONE); |
JMF | 0:24d3eb812fd4 | 570 | } |
JMF | 0:24d3eb812fd4 | 571 | |
JMF | 0:24d3eb812fd4 | 572 | /* Disable handling of other RX flags */ |
JMF | 0:24d3eb812fd4 | 573 | x_irq_status.IRQ_RX_FIFO_ALMOST_FULL = S_RESET; |
JMF | 0:24d3eb812fd4 | 574 | } |
JMF | 0:24d3eb812fd4 | 575 | } |
JMF | 0:24d3eb812fd4 | 576 | |
JMF | 0:24d3eb812fd4 | 577 | #ifndef RX_FIFO_THR_WA |
JMF | 0:24d3eb812fd4 | 578 | /* RX FIFO almost full */ |
JMF | 0:24d3eb812fd4 | 579 | if(x_irq_status.IRQ_RX_FIFO_ALMOST_FULL) { |
JMF | 0:24d3eb812fd4 | 580 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 581 | uint32_t *tmp = (uint32_t*)&x_irq_status; |
JMF | 0:24d3eb812fd4 | 582 | debug_if(!((*tmp) & IRQ_RX_FIFO_ALMOST_FULL_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 583 | #endif |
JMF | 0:24d3eb812fd4 | 584 | if(!_is_receiving) { // spurious irq?!? |
JMF | 0:24d3eb812fd4 | 585 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 586 | debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp); |
JMF | 0:24d3eb812fd4 | 587 | #endif |
JMF | 0:24d3eb812fd4 | 588 | } else { |
JMF | 0:24d3eb812fd4 | 589 | uint8_t fifo_available = linear_fifo_read_num_elements_rx_fifo(); |
JMF | 0:24d3eb812fd4 | 590 | if((fifo_available + _spirit_rx_pos) <= MAX_PACKET_LEN) { |
JMF | 0:24d3eb812fd4 | 591 | spi_read_linear_fifo(fifo_available, &spirit_rx_buf[_spirit_rx_pos]); |
JMF | 0:24d3eb812fd4 | 592 | _spirit_rx_pos += fifo_available; |
JMF | 0:24d3eb812fd4 | 593 | } else { |
JMF | 0:24d3eb812fd4 | 594 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 595 | debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp); |
JMF | 0:24d3eb812fd4 | 596 | #endif |
JMF | 0:24d3eb812fd4 | 597 | } |
JMF | 0:24d3eb812fd4 | 598 | } |
JMF | 0:24d3eb812fd4 | 599 | } |
JMF | 0:24d3eb812fd4 | 600 | #endif // !RX_FIFO_THR_WA |
JMF | 0:24d3eb812fd4 | 601 | |
JMF | 0:24d3eb812fd4 | 602 | /* Reception errors */ |
JMF | 0:24d3eb812fd4 | 603 | if((x_irq_status.IRQ_RX_FIFO_ERROR) || (x_irq_status.IRQ_RX_DATA_DISC)) { |
JMF | 0:24d3eb812fd4 | 604 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 605 | uint32_t *tmp = (uint32_t*)&x_irq_status; |
JMF | 0:24d3eb812fd4 | 606 | debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp); |
JMF | 0:24d3eb812fd4 | 607 | debug_if(!((*tmp) & (IRQ_RX_FIFO_ERROR_MASK | IRQ_RX_DATA_DISC_MASK)), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 608 | #endif |
JMF | 0:24d3eb812fd4 | 609 | rx_timeout_handler(); |
JMF | 0:24d3eb812fd4 | 610 | if(_spirit_tx_started) { |
JMF | 0:24d3eb812fd4 | 611 | _spirit_tx_started = false; |
JMF | 0:24d3eb812fd4 | 612 | /* call user callback */ |
JMF | 0:24d3eb812fd4 | 613 | if(_current_irq_callback) { |
JMF | 0:24d3eb812fd4 | 614 | _current_irq_callback(TX_ERR); |
JMF | 0:24d3eb812fd4 | 615 | } |
JMF | 0:24d3eb812fd4 | 616 | } |
JMF | 0:24d3eb812fd4 | 617 | } |
JMF | 0:24d3eb812fd4 | 618 | |
JMF | 0:24d3eb812fd4 | 619 | /* The IRQ_VALID_SYNC is used to notify a new packet is coming */ |
JMF | 0:24d3eb812fd4 | 620 | if(x_irq_status.IRQ_VALID_SYNC) { |
JMF | 0:24d3eb812fd4 | 621 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 622 | uint32_t *tmp = (uint32_t*)&x_irq_status; |
JMF | 0:24d3eb812fd4 | 623 | debug_if(!((*tmp) & IRQ_VALID_SYNC_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__); |
JMF | 0:24d3eb812fd4 | 624 | #endif |
JMF | 0:24d3eb812fd4 | 625 | /* betzw - NOTE: there is a race condition between Spirit1 receiving packets and |
JMF | 0:24d3eb812fd4 | 626 | * the MCU trying to send a packet, which gets resolved in favor of |
JMF | 0:24d3eb812fd4 | 627 | * sending. |
JMF | 0:24d3eb812fd4 | 628 | */ |
JMF | 0:24d3eb812fd4 | 629 | if(_spirit_tx_started) { |
JMF | 0:24d3eb812fd4 | 630 | #ifdef DEBUG_IRQ |
JMF | 0:24d3eb812fd4 | 631 | debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp); |
JMF | 0:24d3eb812fd4 | 632 | #endif |
JMF | 0:24d3eb812fd4 | 633 | } else { |
JMF | 0:24d3eb812fd4 | 634 | _is_receiving = true; |
JMF | 0:24d3eb812fd4 | 635 | start_rx_timeout(); |
JMF | 0:24d3eb812fd4 | 636 | } |
JMF | 0:24d3eb812fd4 | 637 | } |
JMF | 0:24d3eb812fd4 | 638 | } |