Prototype RF driver for STM Sub-1 GHz RF expansion board based on the SPSGRF-868 module for STM32 Nucleo.

Prototype RF Driver for STM Sub-1 GHz RF Expansion Boards based on the SPSGRF-868 and SPSGRF-915 Modules for STM32 Nucleo

Currently supported boards:

Note, in order to use expansion board X-NUCLEO-IDS01A4 in mbed you need to perform the following HW modifications on the board:

  • Unmount resistor R4
  • Mount resistor R7

Furthermore, on some Nucleo development boards (e.g. the NUCLEO_F429ZI), in order to be able to use Ethernet together with these Sub-1 GHz RF expansion boards, you need to compile this driver with macro SPIRIT1_SPI_MOSI=PB_5 defined, while the development board typically requires some HW modification as e.g. described here!

This driver can be used together with the 6LoWPAN stack (a.k.a. Nanostack).

Committer:
Wolfgang Betz
Date:
Fri Nov 18 13:35:27 2016 +0100
Revision:
30:9c6dcfc47619
Parent:
29:fe1b113f71d0
Child:
31:a4d8072139f2
Improve RX error handling

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 0:4fb29d9ee571 1 /*** Mbed Includes ***/
Wolfgang Betz 0:4fb29d9ee571 2 #include "SimpleSpirit1.h"
Wolfgang Betz 3:0df38cfb1e53 3 #include "radio_spi.h"
Wolfgang Betz 0:4fb29d9ee571 4
Wolfgang Betz 4:07537ca85c66 5 #define SPIRIT_GPIO_IRQ (SPIRIT_GPIO_3)
Wolfgang Betz 3:0df38cfb1e53 6
Wolfgang Betz 26:45dae8d48029 7 static uint16_t last_state;
Wolfgang Betz 28:6a71e15d5272 8 #define SPIRIT1_STATUS() ((last_state = (uint16_t)refresh_state()) & SPIRIT1_STATE_STATEBITS)
Wolfgang Betz 28:6a71e15d5272 9
Wolfgang Betz 28:6a71e15d5272 10 #define XO_ON (0x1)
Wolfgang Betz 3:0df38cfb1e53 11
Wolfgang Betz 3:0df38cfb1e53 12 #define BUSYWAIT_UNTIL(cond, millisecs) \
Wolfgang Betz 16:25dc4b811ad3 13 do { \
Wolfgang Betz 16:25dc4b811ad3 14 uint32_t start = us_ticker_read(); \
Wolfgang Betz 16:25dc4b811ad3 15 while (!(cond) && ((us_ticker_read() - start) < ((uint32_t)millisecs)*1000U)); \
Wolfgang Betz 16:25dc4b811ad3 16 } while(0)
Wolfgang Betz 3:0df38cfb1e53 17
Wolfgang Betz 4:07537ca85c66 18 #define st_lib_spirit_irqs SpiritIrqs
Wolfgang Betz 4:07537ca85c66 19
Wolfgang Betz 25:2ec45788f28c 20 #define STATE_TIMEOUT (1000)
Wolfgang Betz 0:4fb29d9ee571 21
Wolfgang Betz 28:6a71e15d5272 22 // betzw: switching force & back from standby seems to be unstable
Wolfgang Betz 28:6a71e15d5272 23 // #define USE_STANDBY_STATE
Wolfgang Betz 28:6a71e15d5272 24
Wolfgang Betz 28:6a71e15d5272 25 // betzw: enable beyond macro if you want debug messages also from IRQ handler
Wolfgang Betz 28:6a71e15d5272 26 #define DEBUG_IRQ
Wolfgang Betz 28:6a71e15d5272 27
Wolfgang Betz 0:4fb29d9ee571 28 /*** Class Implementation ***/
Wolfgang Betz 3:0df38cfb1e53 29 /** Static Class Variables **/
Wolfgang Betz 3:0df38cfb1e53 30 SimpleSpirit1 *SimpleSpirit1::_singleton = NULL;
Wolfgang Betz 3:0df38cfb1e53 31
Wolfgang Betz 3:0df38cfb1e53 32 /** Constructor **/
Wolfgang Betz 0:4fb29d9ee571 33 SimpleSpirit1::SimpleSpirit1(PinName mosi, PinName miso, PinName sclk,
Wolfgang Betz 16:25dc4b811ad3 34 PinName irq, PinName cs, PinName sdn,
Wolfgang Betz 16:25dc4b811ad3 35 PinName led) :
Wolfgang Betz 16:25dc4b811ad3 36 _spi(mosi, miso, sclk),
Wolfgang Betz 16:25dc4b811ad3 37 _irq(irq),
Wolfgang Betz 16:25dc4b811ad3 38 _chip_select(cs),
Wolfgang Betz 16:25dc4b811ad3 39 _shut_down(sdn),
Wolfgang Betz 16:25dc4b811ad3 40 _led(led),
Wolfgang Betz 16:25dc4b811ad3 41 _current_irq_callback(),
Wolfgang Betz 16:25dc4b811ad3 42 _rx_receiving_timeout()
Wolfgang Betz 0:4fb29d9ee571 43 {
Wolfgang Betz 9:3db68ab23070 44 }
Wolfgang Betz 9:3db68ab23070 45
Wolfgang Betz 9:3db68ab23070 46 /** Init Function **/
Wolfgang Betz 9:3db68ab23070 47 void SimpleSpirit1::init() {
Wolfgang Betz 16:25dc4b811ad3 48 /* reset irq disable counter and irq callback & disable irq */
Wolfgang Betz 3:0df38cfb1e53 49 _nr_of_irq_disables = 0;
Wolfgang Betz 16:25dc4b811ad3 50 disable_spirit_irq();
Wolfgang Betz 0:4fb29d9ee571 51
Wolfgang Betz 16:25dc4b811ad3 52 /* unselect chip */
Wolfgang Betz 16:25dc4b811ad3 53 chip_unselect();
Wolfgang Betz 0:4fb29d9ee571 54
Wolfgang Betz 16:25dc4b811ad3 55 /* configure spi */
Wolfgang Betz 16:25dc4b811ad3 56 _spi.format(8, 0); /* 8-bit, mode = 0, [order = SPI_MSB] only available in mbed3 */
Wolfgang Betz 27:e68ffb6ac223 57 _spi.frequency(1000000); // 1MHz // betzw - NOTE: higher frequencies lead to instability of Spirit1
Wolfgang Betz 3:0df38cfb1e53 58
Wolfgang Betz 16:25dc4b811ad3 59 /* install irq handler */
Wolfgang Betz 16:25dc4b811ad3 60 _irq.mode(PullUp);
Wolfgang Betz 16:25dc4b811ad3 61 _irq.fall(Callback<void()>(this, &SimpleSpirit1::IrqHandler));
Wolfgang Betz 5:c9c5bc673c64 62
Wolfgang Betz 16:25dc4b811ad3 63 /* init cube vars */
Wolfgang Betz 16:25dc4b811ad3 64 spirit_on = OFF;
Wolfgang Betz 16:25dc4b811ad3 65 last_rssi = 0 ; //MGR
Wolfgang Betz 30:9c6dcfc47619 66 last_sqi = 0 ; //MGR
Wolfgang Betz 0:4fb29d9ee571 67
Wolfgang Betz 16:25dc4b811ad3 68 /* set frequencies */
Wolfgang Betz 16:25dc4b811ad3 69 radio_set_xtal_freq(XTAL_FREQUENCY);
Wolfgang Betz 16:25dc4b811ad3 70 mgmt_set_freq_base((uint32_t)BASE_FREQUENCY);
Wolfgang Betz 0:4fb29d9ee571 71
Wolfgang Betz 16:25dc4b811ad3 72 /* restart board */
Wolfgang Betz 16:25dc4b811ad3 73 enter_shutdown();
Wolfgang Betz 16:25dc4b811ad3 74 exit_shutdown();
Wolfgang Betz 0:4fb29d9ee571 75
Wolfgang Betz 16:25dc4b811ad3 76 /* soft core reset */
Wolfgang Betz 16:25dc4b811ad3 77 cmd_strobe(SPIRIT1_STROBE_SRES);
Wolfgang Betz 15:852b92eed64a 78
Wolfgang Betz 16:25dc4b811ad3 79 /* Configures the SPIRIT1 radio part */
Wolfgang Betz 16:25dc4b811ad3 80 SRadioInit x_radio_init = {
Wolfgang Betz 16:25dc4b811ad3 81 XTAL_OFFSET_PPM,
Wolfgang Betz 16:25dc4b811ad3 82 (uint32_t)BASE_FREQUENCY,
Wolfgang Betz 16:25dc4b811ad3 83 (uint32_t)CHANNEL_SPACE,
Wolfgang Betz 16:25dc4b811ad3 84 CHANNEL_NUMBER,
Wolfgang Betz 16:25dc4b811ad3 85 MODULATION_SELECT,
Wolfgang Betz 16:25dc4b811ad3 86 DATARATE,
Wolfgang Betz 16:25dc4b811ad3 87 (uint32_t)FREQ_DEVIATION,
Wolfgang Betz 16:25dc4b811ad3 88 (uint32_t)BANDWIDTH
Wolfgang Betz 16:25dc4b811ad3 89 };
Wolfgang Betz 16:25dc4b811ad3 90 radio_init(&x_radio_init);
Wolfgang Betz 16:25dc4b811ad3 91 radio_set_pa_level_dbm(0,POWER_DBM);
Wolfgang Betz 16:25dc4b811ad3 92 radio_set_pa_level_max_index(0);
Wolfgang Betz 0:4fb29d9ee571 93
Wolfgang Betz 16:25dc4b811ad3 94 /* Configures the SPIRIT1 packet handler part*/
Wolfgang Betz 16:25dc4b811ad3 95 PktBasicInit x_basic_init = {
Wolfgang Betz 16:25dc4b811ad3 96 PREAMBLE_LENGTH,
Wolfgang Betz 16:25dc4b811ad3 97 SYNC_LENGTH,
Wolfgang Betz 16:25dc4b811ad3 98 SYNC_WORD,
Wolfgang Betz 16:25dc4b811ad3 99 LENGTH_TYPE,
Wolfgang Betz 16:25dc4b811ad3 100 LENGTH_WIDTH,
Wolfgang Betz 16:25dc4b811ad3 101 CRC_MODE,
Wolfgang Betz 16:25dc4b811ad3 102 CONTROL_LENGTH,
Wolfgang Betz 16:25dc4b811ad3 103 EN_ADDRESS,
Wolfgang Betz 16:25dc4b811ad3 104 EN_FEC,
Wolfgang Betz 16:25dc4b811ad3 105 EN_WHITENING
Wolfgang Betz 16:25dc4b811ad3 106 };
Wolfgang Betz 16:25dc4b811ad3 107 pkt_basic_init(&x_basic_init);
Wolfgang Betz 0:4fb29d9ee571 108
Wolfgang Betz 16:25dc4b811ad3 109 /* Enable the following interrupt sources, routed to GPIO */
Wolfgang Betz 16:25dc4b811ad3 110 irq_de_init(NULL);
Wolfgang Betz 16:25dc4b811ad3 111 irq_clear_status();
Wolfgang Betz 16:25dc4b811ad3 112 irq_set_status(TX_DATA_SENT, S_ENABLE);
Wolfgang Betz 16:25dc4b811ad3 113 irq_set_status(RX_DATA_READY,S_ENABLE);
Wolfgang Betz 16:25dc4b811ad3 114 irq_set_status(RX_DATA_DISC, S_ENABLE);
Wolfgang Betz 16:25dc4b811ad3 115 irq_set_status(TX_FIFO_ERROR, S_ENABLE);
Wolfgang Betz 16:25dc4b811ad3 116 irq_set_status(RX_FIFO_ERROR, S_ENABLE);
Wolfgang Betz 16:25dc4b811ad3 117 irq_set_status(RX_FIFO_ALMOST_FULL, S_ENABLE);
Wolfgang Betz 16:25dc4b811ad3 118 irq_set_status(VALID_SYNC, S_ENABLE);
Wolfgang Betz 0:4fb29d9ee571 119
Wolfgang Betz 16:25dc4b811ad3 120 /* Configure Spirit1 */
Wolfgang Betz 28:6a71e15d5272 121 radio_persistent_rx(S_ENABLE);
Wolfgang Betz 16:25dc4b811ad3 122 qi_set_sqi_threshold(SQI_TH_0);
Wolfgang Betz 16:25dc4b811ad3 123 qi_sqi_check(S_ENABLE);
Wolfgang Betz 16:25dc4b811ad3 124 qi_set_rssi_threshold_dbm(CCA_THRESHOLD);
Wolfgang Betz 16:25dc4b811ad3 125 timer_set_rx_timeout_stop_condition(SQI_ABOVE_THRESHOLD);
Wolfgang Betz 16:25dc4b811ad3 126 timer_set_infinite_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 127 radio_afc_freeze_on_sync(S_ENABLE);
Wolfgang Betz 29:fe1b113f71d0 128 calibration_rco(S_ENABLE);
Wolfgang Betz 0:4fb29d9ee571 129
Wolfgang Betz 16:25dc4b811ad3 130 spirit_on = OFF;
Wolfgang Betz 16:25dc4b811ad3 131 CLEAR_TXBUF();
Wolfgang Betz 16:25dc4b811ad3 132 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 133 _spirit_tx_started = false;
Wolfgang Betz 16:25dc4b811ad3 134 _spirit_rx_err = false;
Wolfgang Betz 16:25dc4b811ad3 135 _is_receiving = false;
Wolfgang Betz 0:4fb29d9ee571 136
Wolfgang Betz 16:25dc4b811ad3 137 /* Configure the radio to route the IRQ signal to its GPIO 3 */
Wolfgang Betz 16:25dc4b811ad3 138 SGpioInit x_gpio_init = {
Wolfgang Betz 16:25dc4b811ad3 139 SPIRIT_GPIO_IRQ,
Wolfgang Betz 16:25dc4b811ad3 140 SPIRIT_GPIO_MODE_DIGITAL_OUTPUT_LP,
Wolfgang Betz 16:25dc4b811ad3 141 SPIRIT_GPIO_DIG_OUT_IRQ
Wolfgang Betz 16:25dc4b811ad3 142 };
Wolfgang Betz 16:25dc4b811ad3 143 spirit_gpio_init(&x_gpio_init);
Wolfgang Betz 18:d6f789f6f4c9 144
Wolfgang Betz 23:4192649f35da 145 /* Setup CSMA/CA */
Wolfgang Betz 23:4192649f35da 146 CsmaInit x_csma_init = {
Wolfgang Betz 29:fe1b113f71d0 147 S_ENABLE, // enable persistent mode
Wolfgang Betz 23:4192649f35da 148 TBIT_TIME_64, // Tcca time
Wolfgang Betz 23:4192649f35da 149 TCCA_TIME_3, // Lcca length
Wolfgang Betz 23:4192649f35da 150 3, // max nr of backoffs (<8)
Wolfgang Betz 23:4192649f35da 151 1, // BU counter seed
Wolfgang Betz 23:4192649f35da 152 8 // BU prescaler
Wolfgang Betz 23:4192649f35da 153 };
Wolfgang Betz 23:4192649f35da 154 csma_ca_init(&x_csma_init);
Wolfgang Betz 23:4192649f35da 155
Wolfgang Betz 30:9c6dcfc47619 156 #ifdef RX_FIFO_THR_AO_CSMA_WA
Wolfgang Betz 18:d6f789f6f4c9 157 linear_fifo_set_almost_full_thr_rx(SPIRIT_MAX_FIFO_LEN-(MAX_PACKET_LEN+1));
Wolfgang Betz 18:d6f789f6f4c9 158 #endif
Wolfgang Betz 25:2ec45788f28c 159
Wolfgang Betz 28:6a71e15d5272 160 #ifdef USE_STANDBY_STATE
Wolfgang Betz 25:2ec45788f28c 161 /* Puts the SPIRIT1 in STANDBY mode (125us -> rx/tx) */
Wolfgang Betz 25:2ec45788f28c 162 cmd_strobe(SPIRIT1_STROBE_STANDBY);
Wolfgang Betz 28:6a71e15d5272 163 #endif // USE_STANDBY_STATE
Wolfgang Betz 0:4fb29d9ee571 164 }
Wolfgang Betz 3:0df38cfb1e53 165
Wolfgang Betz 6:f5d01793bf86 166 int SimpleSpirit1::send(const void *payload, unsigned int payload_len) {
Wolfgang Betz 5:c9c5bc673c64 167 /* Checks if the payload length is supported */
Wolfgang Betz 5:c9c5bc673c64 168 if(payload_len > MAX_PACKET_LEN) {
Wolfgang Betz 5:c9c5bc673c64 169 return RADIO_TX_ERR;
Wolfgang Betz 5:c9c5bc673c64 170 }
Wolfgang Betz 5:c9c5bc673c64 171
Wolfgang Betz 28:6a71e15d5272 172 disable_spirit_irq();
Wolfgang Betz 28:6a71e15d5272 173
Wolfgang Betz 28:6a71e15d5272 174 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 25:2ec45788f28c 175 #ifndef NDEBUG
Wolfgang Betz 28:6a71e15d5272 176 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) {
Wolfgang Betz 27:e68ffb6ac223 177 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 178 }
Wolfgang Betz 25:2ec45788f28c 179 #endif
Wolfgang Betz 25:2ec45788f28c 180
Wolfgang Betz 5:c9c5bc673c64 181 /* Reset State to Ready */
Wolfgang Betz 5:c9c5bc673c64 182 set_ready_state();
Wolfgang Betz 5:c9c5bc673c64 183
Wolfgang Betz 5:c9c5bc673c64 184 cmd_strobe(SPIRIT1_STROBE_FTX); // flush TX FIFO buffer
Wolfgang Betz 25:2ec45788f28c 185
Wolfgang Betz 22:9165bd73c61e 186 #ifndef NDEBUG
Wolfgang Betz 27:e68ffb6ac223 187 debug_if(!(linear_fifo_read_num_elements_tx_fifo() == 0), "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 22:9165bd73c61e 188 #endif
Wolfgang Betz 5:c9c5bc673c64 189
Wolfgang Betz 5:c9c5bc673c64 190 pkt_basic_set_payload_length(payload_len); // set desired payload len
Wolfgang Betz 26:45dae8d48029 191
Wolfgang Betz 30:9c6dcfc47619 192 #ifdef RX_FIFO_THR_AO_CSMA_WA
Wolfgang Betz 30:9c6dcfc47619 193 // betzw - TODO: enabling CSMA/CA seems to be incompatible with TX FIFO usage (to be investigated)
Wolfgang Betz 23:4192649f35da 194 csma_ca_state(S_ENABLE); // enable CSMA/CA
Wolfgang Betz 26:45dae8d48029 195 #endif
Wolfgang Betz 26:45dae8d48029 196
Wolfgang Betz 5:c9c5bc673c64 197 int i = 0;
Wolfgang Betz 5:c9c5bc673c64 198 int remaining = payload_len;
Wolfgang Betz 26:45dae8d48029 199 const uint8_t *buffer = (const uint8_t*)payload;
Wolfgang Betz 28:6a71e15d5272 200 bool tx_triggered = false;
Wolfgang Betz 5:c9c5bc673c64 201 do {
Wolfgang Betz 5:c9c5bc673c64 202 uint8_t fifo_available = SPIRIT_MAX_FIFO_LEN - linear_fifo_read_num_elements_tx_fifo();
Wolfgang Betz 5:c9c5bc673c64 203 uint8_t to_send = (remaining > fifo_available) ? fifo_available : remaining;
Wolfgang Betz 5:c9c5bc673c64 204
Wolfgang Betz 5:c9c5bc673c64 205 /* Fill FIFO Buffer */
Wolfgang Betz 26:45dae8d48029 206 if(to_send > 0) {
Wolfgang Betz 26:45dae8d48029 207 spi_write_linear_fifo(to_send, (uint8_t*)&buffer[i]);
Wolfgang Betz 5:c9c5bc673c64 208 }
Wolfgang Betz 5:c9c5bc673c64 209
Wolfgang Betz 28:6a71e15d5272 210 if(!tx_triggered) {
Wolfgang Betz 28:6a71e15d5272 211 cmd_strobe(SPIRIT1_STROBE_TX);
Wolfgang Betz 28:6a71e15d5272 212 tx_triggered = true;
Wolfgang Betz 28:6a71e15d5272 213 }
Wolfgang Betz 28:6a71e15d5272 214
Wolfgang Betz 5:c9c5bc673c64 215 i += to_send;
Wolfgang Betz 5:c9c5bc673c64 216 remaining -= to_send;
Wolfgang Betz 5:c9c5bc673c64 217 } while(remaining != 0);
Wolfgang Betz 5:c9c5bc673c64 218
Wolfgang Betz 7:e90fa8f6bc6c 219 _spirit_tx_started = true;
Wolfgang Betz 7:e90fa8f6bc6c 220
Wolfgang Betz 7:e90fa8f6bc6c 221 enable_spirit_irq();
Wolfgang Betz 7:e90fa8f6bc6c 222
Wolfgang Betz 28:6a71e15d5272 223 BUSYWAIT_UNTIL(!_spirit_tx_started, STATE_TIMEOUT);
Wolfgang Betz 30:9c6dcfc47619 224 #ifdef HEAVY_DEBUG
Wolfgang Betz 30:9c6dcfc47619 225 debug("\n\r%s (%d): state=%x, _spirit_tx_started=%d\n\r", __func__, __LINE__, SPIRIT1_STATUS()>>1, _spirit_tx_started);
Wolfgang Betz 23:4192649f35da 226 #endif
Wolfgang Betz 5:c9c5bc673c64 227
Wolfgang Betz 28:6a71e15d5272 228 csma_ca_state(S_DISABLE); // disable CSMA/CA
Wolfgang Betz 28:6a71e15d5272 229 cmd_strobe(SPIRIT1_STROBE_RX); // Return to RX state
Wolfgang Betz 28:6a71e15d5272 230
Wolfgang Betz 5:c9c5bc673c64 231 return RADIO_TX_OK;
Wolfgang Betz 5:c9c5bc673c64 232 }
Wolfgang Betz 5:c9c5bc673c64 233
Wolfgang Betz 3:0df38cfb1e53 234 /** Set Ready State **/
Wolfgang Betz 3:0df38cfb1e53 235 void SimpleSpirit1::set_ready_state(void) {
Wolfgang Betz 25:2ec45788f28c 236 uint16_t state;
Wolfgang Betz 25:2ec45788f28c 237
Wolfgang Betz 16:25dc4b811ad3 238 disable_spirit_irq();
Wolfgang Betz 3:0df38cfb1e53 239
Wolfgang Betz 30:9c6dcfc47619 240 _spirit_tx_started = false;
Wolfgang Betz 16:25dc4b811ad3 241 _is_receiving = false;
Wolfgang Betz 30:9c6dcfc47619 242 _spirit_rx_err = false;
Wolfgang Betz 16:25dc4b811ad3 243 stop_rx_timeout();
Wolfgang Betz 12:b8056eda4028 244
Wolfgang Betz 30:9c6dcfc47619 245 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 30:9c6dcfc47619 246 CLEAR_RXBUF();
Wolfgang Betz 30:9c6dcfc47619 247 CLEAR_TXBUF();
Wolfgang Betz 30:9c6dcfc47619 248
Wolfgang Betz 25:2ec45788f28c 249 state = SPIRIT1_STATUS();
Wolfgang Betz 25:2ec45788f28c 250 if(state == SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 16:25dc4b811ad3 251 cmd_strobe(SPIRIT1_STROBE_READY);
Wolfgang Betz 25:2ec45788f28c 252 } else if(state == SPIRIT1_STATE_RX) {
Wolfgang Betz 16:25dc4b811ad3 253 cmd_strobe(SPIRIT1_STROBE_SABORT);
Wolfgang Betz 25:2ec45788f28c 254 } else if(state != SPIRIT1_STATE_READY) {
Wolfgang Betz 25:2ec45788f28c 255 #ifndef NDEBUG
Wolfgang Betz 27:e68ffb6ac223 256 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, state>>1);
Wolfgang Betz 25:2ec45788f28c 257 #endif
Wolfgang Betz 16:25dc4b811ad3 258 }
Wolfgang Betz 25:2ec45788f28c 259
Wolfgang Betz 28:6a71e15d5272 260 BUSYWAIT_UNTIL((SPIRIT1_STATUS() == SPIRIT1_STATE_READY) && ((last_state & XO_ON) == XO_ON), STATE_TIMEOUT);
Wolfgang Betz 28:6a71e15d5272 261 if(last_state != (SPIRIT1_STATE_READY | XO_ON)) {
Wolfgang Betz 28:6a71e15d5272 262 error("\n\rSpirit1: failed to become ready (%x) => pls. reset!\n\r", last_state);
Wolfgang Betz 25:2ec45788f28c 263 enable_spirit_irq();
Wolfgang Betz 25:2ec45788f28c 264 return;
Wolfgang Betz 25:2ec45788f28c 265 }
Wolfgang Betz 25:2ec45788f28c 266
Wolfgang Betz 16:25dc4b811ad3 267 irq_clear_status();
Wolfgang Betz 3:0df38cfb1e53 268
Wolfgang Betz 16:25dc4b811ad3 269 enable_spirit_irq();
Wolfgang Betz 3:0df38cfb1e53 270 }
Wolfgang Betz 3:0df38cfb1e53 271
Wolfgang Betz 4:07537ca85c66 272 int SimpleSpirit1::off(void) {
Wolfgang Betz 16:25dc4b811ad3 273 if(spirit_on == ON) {
Wolfgang Betz 16:25dc4b811ad3 274 /* Disables the mcu to get IRQ from the SPIRIT1 */
Wolfgang Betz 16:25dc4b811ad3 275 disable_spirit_irq();
Wolfgang Betz 3:0df38cfb1e53 276
Wolfgang Betz 16:25dc4b811ad3 277 /* first stop rx/tx */
Wolfgang Betz 25:2ec45788f28c 278 set_ready_state();
Wolfgang Betz 3:0df38cfb1e53 279
Wolfgang Betz 28:6a71e15d5272 280 #ifdef USE_STANDBY_STATE
Wolfgang Betz 16:25dc4b811ad3 281 /* Puts the SPIRIT1 in STANDBY */
Wolfgang Betz 16:25dc4b811ad3 282 cmd_strobe(SPIRIT1_STROBE_STANDBY);
Wolfgang Betz 25:2ec45788f28c 283 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_STANDBY, STATE_TIMEOUT);
Wolfgang Betz 28:6a71e15d5272 284 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 26:45dae8d48029 285 error("\n\rSpirit1: failed to enter standby (%x)\n\r", last_state>>1);
Wolfgang Betz 16:25dc4b811ad3 286 return 1;
Wolfgang Betz 16:25dc4b811ad3 287 }
Wolfgang Betz 28:6a71e15d5272 288 #endif // USE_STANDBY_STATE
Wolfgang Betz 3:0df38cfb1e53 289
Wolfgang Betz 16:25dc4b811ad3 290 spirit_on = OFF;
Wolfgang Betz 16:25dc4b811ad3 291 _nr_of_irq_disables = 1;
Wolfgang Betz 16:25dc4b811ad3 292 }
Wolfgang Betz 16:25dc4b811ad3 293 return 0;
Wolfgang Betz 3:0df38cfb1e53 294 }
Wolfgang Betz 3:0df38cfb1e53 295
Wolfgang Betz 4:07537ca85c66 296 int SimpleSpirit1::on(void) {
Wolfgang Betz 16:25dc4b811ad3 297 if(spirit_on == OFF) {
Wolfgang Betz 25:2ec45788f28c 298 set_ready_state();
Wolfgang Betz 25:2ec45788f28c 299
Wolfgang Betz 25:2ec45788f28c 300 /* now we go to Rx */
Wolfgang Betz 25:2ec45788f28c 301 cmd_strobe(SPIRIT1_STROBE_RX);
Wolfgang Betz 25:2ec45788f28c 302
Wolfgang Betz 25:2ec45788f28c 303 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 28:6a71e15d5272 304 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) {
Wolfgang Betz 26:45dae8d48029 305 error("\n\rSpirit1: failed to enter rx (%x) => retry\n\r", last_state>>1);
Wolfgang Betz 16:25dc4b811ad3 306 }
Wolfgang Betz 3:0df38cfb1e53 307
Wolfgang Betz 16:25dc4b811ad3 308 /* Enables the mcu to get IRQ from the SPIRIT1 */
Wolfgang Betz 16:25dc4b811ad3 309 spirit_on = ON;
Wolfgang Betz 22:9165bd73c61e 310 #ifndef NDEBUG
Wolfgang Betz 27:e68ffb6ac223 311 debug_if(!(_nr_of_irq_disables == 1), "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 22:9165bd73c61e 312 #endif
Wolfgang Betz 16:25dc4b811ad3 313 enable_spirit_irq();
Wolfgang Betz 16:25dc4b811ad3 314 }
Wolfgang Betz 3:0df38cfb1e53 315
Wolfgang Betz 25:2ec45788f28c 316 #ifndef NDEBUG
Wolfgang Betz 26:45dae8d48029 317 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 27:e68ffb6ac223 318 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 319 }
Wolfgang Betz 25:2ec45788f28c 320 #endif
Wolfgang Betz 25:2ec45788f28c 321
Wolfgang Betz 16:25dc4b811ad3 322 return 0;
Wolfgang Betz 3:0df38cfb1e53 323 }
Wolfgang Betz 3:0df38cfb1e53 324
Wolfgang Betz 25:2ec45788f28c 325 uint8_t SimpleSpirit1::refresh_state(void) {
Wolfgang Betz 25:2ec45788f28c 326 uint8_t mcstate;
Wolfgang Betz 3:0df38cfb1e53 327
Wolfgang Betz 25:2ec45788f28c 328 SpiritSpiReadRegisters(MC_STATE0_BASE, 1, &mcstate);
Wolfgang Betz 3:0df38cfb1e53 329
Wolfgang Betz 16:25dc4b811ad3 330 return mcstate;
Wolfgang Betz 3:0df38cfb1e53 331 }
Wolfgang Betz 3:0df38cfb1e53 332
Wolfgang Betz 6:f5d01793bf86 333 int SimpleSpirit1::read(void *buf, unsigned int bufsize)
Wolfgang Betz 4:07537ca85c66 334 {
Wolfgang Betz 16:25dc4b811ad3 335 disable_spirit_irq();
Wolfgang Betz 6:f5d01793bf86 336
Wolfgang Betz 16:25dc4b811ad3 337 /* Checks if the RX buffer is empty */
Wolfgang Betz 16:25dc4b811ad3 338 if(IS_RXBUF_EMPTY()) {
Wolfgang Betz 25:2ec45788f28c 339 set_ready_state();
Wolfgang Betz 25:2ec45788f28c 340
Wolfgang Betz 16:25dc4b811ad3 341 cmd_strobe(SPIRIT1_STROBE_RX);
Wolfgang Betz 25:2ec45788f28c 342 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 16:25dc4b811ad3 343 enable_spirit_irq();
Wolfgang Betz 16:25dc4b811ad3 344 return 0;
Wolfgang Betz 16:25dc4b811ad3 345 }
Wolfgang Betz 4:07537ca85c66 346
Wolfgang Betz 16:25dc4b811ad3 347 if(bufsize < spirit_rx_len) {
Wolfgang Betz 16:25dc4b811ad3 348 enable_spirit_irq();
Wolfgang Betz 6:f5d01793bf86 349
Wolfgang Betz 16:25dc4b811ad3 350 /* If buf has the correct size */
Wolfgang Betz 22:9165bd73c61e 351 #ifndef NDEBUG
Wolfgang Betz 22:9165bd73c61e 352 debug("\n\rTOO SMALL BUF\n\r");
Wolfgang Betz 22:9165bd73c61e 353 #endif
Wolfgang Betz 16:25dc4b811ad3 354 return 0;
Wolfgang Betz 16:25dc4b811ad3 355 } else {
Wolfgang Betz 16:25dc4b811ad3 356 /* Copies the packet received */
Wolfgang Betz 16:25dc4b811ad3 357 memcpy(buf, spirit_rx_buf, spirit_rx_len);
Wolfgang Betz 4:07537ca85c66 358
Wolfgang Betz 16:25dc4b811ad3 359 bufsize = spirit_rx_len;
Wolfgang Betz 16:25dc4b811ad3 360 CLEAR_RXBUF();
Wolfgang Betz 4:07537ca85c66 361
Wolfgang Betz 16:25dc4b811ad3 362 enable_spirit_irq();
Wolfgang Betz 6:f5d01793bf86 363
Wolfgang Betz 16:25dc4b811ad3 364 return bufsize;
Wolfgang Betz 16:25dc4b811ad3 365 }
Wolfgang Betz 4:07537ca85c66 366
Wolfgang Betz 4:07537ca85c66 367 }
Wolfgang Betz 4:07537ca85c66 368
Wolfgang Betz 4:07537ca85c66 369 int SimpleSpirit1::channel_clear(void)
Wolfgang Betz 4:07537ca85c66 370 {
Wolfgang Betz 16:25dc4b811ad3 371 float rssi_value;
Wolfgang Betz 16:25dc4b811ad3 372 /* Local variable used to memorize the SPIRIT1 state */
Wolfgang Betz 16:25dc4b811ad3 373 uint8_t spirit_state = ON;
Wolfgang Betz 4:07537ca85c66 374
Wolfgang Betz 16:25dc4b811ad3 375 if(spirit_on == OFF) {
Wolfgang Betz 16:25dc4b811ad3 376 /* Wakes up the SPIRIT1 */
Wolfgang Betz 16:25dc4b811ad3 377 on();
Wolfgang Betz 16:25dc4b811ad3 378 spirit_state = OFF;
Wolfgang Betz 16:25dc4b811ad3 379 }
Wolfgang Betz 4:07537ca85c66 380
Wolfgang Betz 25:2ec45788f28c 381 #ifndef NDEBUG
Wolfgang Betz 26:45dae8d48029 382 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 27:e68ffb6ac223 383 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 384 }
Wolfgang Betz 25:2ec45788f28c 385 #endif
Wolfgang Betz 25:2ec45788f28c 386
Wolfgang Betz 16:25dc4b811ad3 387 disable_spirit_irq();
Wolfgang Betz 7:e90fa8f6bc6c 388
Wolfgang Betz 16:25dc4b811ad3 389 /* Reset State to Ready */
Wolfgang Betz 16:25dc4b811ad3 390 set_ready_state();
Wolfgang Betz 4:07537ca85c66 391
Wolfgang Betz 16:25dc4b811ad3 392 /* Stores the RSSI value */
Wolfgang Betz 16:25dc4b811ad3 393 rssi_value = qi_get_rssi_dbm();
Wolfgang Betz 4:07537ca85c66 394
Wolfgang Betz 16:25dc4b811ad3 395 enable_spirit_irq();
Wolfgang Betz 7:e90fa8f6bc6c 396
Wolfgang Betz 16:25dc4b811ad3 397 /* Puts the SPIRIT1 in its previous state */
Wolfgang Betz 16:25dc4b811ad3 398 if(spirit_state==OFF) {
Wolfgang Betz 16:25dc4b811ad3 399 off();
Wolfgang Betz 25:2ec45788f28c 400 #ifndef NDEBUG
Wolfgang Betz 28:6a71e15d5272 401 #ifdef USE_STANDBY_STATE
Wolfgang Betz 26:45dae8d48029 402 if(SPIRIT1_STATUS() != SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 28:6a71e15d5272 403 #else
Wolfgang Betz 28:6a71e15d5272 404 if(SPIRIT1_STATUS() != SPIRIT1_STATE_READY) {
Wolfgang Betz 28:6a71e15d5272 405 #endif
Wolfgang Betz 27:e68ffb6ac223 406 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 407 }
Wolfgang Betz 25:2ec45788f28c 408 #endif
Wolfgang Betz 16:25dc4b811ad3 409 } else {
Wolfgang Betz 16:25dc4b811ad3 410 disable_spirit_irq();
Wolfgang Betz 25:2ec45788f28c 411
Wolfgang Betz 25:2ec45788f28c 412 set_ready_state();
Wolfgang Betz 25:2ec45788f28c 413
Wolfgang Betz 16:25dc4b811ad3 414 cmd_strobe(SPIRIT1_STROBE_RX);
Wolfgang Betz 25:2ec45788f28c 415 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 28:6a71e15d5272 416 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) {
Wolfgang Betz 26:45dae8d48029 417 error("\n\rSpirit1: (#2) failed to enter rx (%x) => retry\n\r", last_state>>1);
Wolfgang Betz 25:2ec45788f28c 418 }
Wolfgang Betz 25:2ec45788f28c 419
Wolfgang Betz 16:25dc4b811ad3 420 enable_spirit_irq();
Wolfgang Betz 25:2ec45788f28c 421
Wolfgang Betz 25:2ec45788f28c 422 #ifndef NDEBUG
Wolfgang Betz 26:45dae8d48029 423 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 27:e68ffb6ac223 424 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 425 }
Wolfgang Betz 25:2ec45788f28c 426 #endif
Wolfgang Betz 16:25dc4b811ad3 427 }
Wolfgang Betz 4:07537ca85c66 428
Wolfgang Betz 16:25dc4b811ad3 429 /* Checks the RSSI value with the threshold */
Wolfgang Betz 16:25dc4b811ad3 430 if(rssi_value<CCA_THRESHOLD) {
Wolfgang Betz 16:25dc4b811ad3 431 return 0;
Wolfgang Betz 16:25dc4b811ad3 432 } else {
Wolfgang Betz 16:25dc4b811ad3 433 return 1;
Wolfgang Betz 16:25dc4b811ad3 434 }
Wolfgang Betz 4:07537ca85c66 435 }
Wolfgang Betz 4:07537ca85c66 436
Wolfgang Betz 7:e90fa8f6bc6c 437 int SimpleSpirit1::get_pending_packet(void)
Wolfgang Betz 4:07537ca85c66 438 {
Wolfgang Betz 16:25dc4b811ad3 439 return !IS_RXBUF_EMPTY();
Wolfgang Betz 4:07537ca85c66 440 }
Wolfgang Betz 4:07537ca85c66 441
Wolfgang Betz 5:c9c5bc673c64 442 /** Spirit Irq Callback **/
Wolfgang Betz 5:c9c5bc673c64 443 void SimpleSpirit1::IrqHandler() {
Wolfgang Betz 16:25dc4b811ad3 444 st_lib_spirit_irqs x_irq_status;
Wolfgang Betz 4:07537ca85c66 445
Wolfgang Betz 16:25dc4b811ad3 446 /* get interrupt source from radio */
Wolfgang Betz 16:25dc4b811ad3 447 irq_get_status(&x_irq_status);
Wolfgang Betz 4:07537ca85c66 448
Wolfgang Betz 16:25dc4b811ad3 449 /* Reception errors */
Wolfgang Betz 26:45dae8d48029 450 if((x_irq_status.IRQ_RX_FIFO_ERROR) || (x_irq_status.IRQ_RX_DATA_DISC)) {
Wolfgang Betz 28:6a71e15d5272 451 #ifdef DEBUG_IRQ
Wolfgang Betz 28:6a71e15d5272 452 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 28:6a71e15d5272 453 debug("\n\r%s (%d): irq=%x", __func__, __LINE__, *tmp);
Wolfgang Betz 23:4192649f35da 454 #endif
Wolfgang Betz 16:25dc4b811ad3 455 _spirit_rx_err = true;
Wolfgang Betz 16:25dc4b811ad3 456 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 457 CLEAR_RXBUF();
Wolfgang Betz 6:f5d01793bf86 458 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 459 if(_spirit_tx_started) {
Wolfgang Betz 16:25dc4b811ad3 460 _spirit_tx_started = false;
Wolfgang Betz 16:25dc4b811ad3 461 CLEAR_TXBUF();
Wolfgang Betz 16:25dc4b811ad3 462 /* call user callback */
Wolfgang Betz 16:25dc4b811ad3 463 if(_current_irq_callback) {
Wolfgang Betz 16:25dc4b811ad3 464 _current_irq_callback(TX_ERR);
Wolfgang Betz 6:f5d01793bf86 465 }
Wolfgang Betz 6:f5d01793bf86 466 }
Wolfgang Betz 16:25dc4b811ad3 467 }
Wolfgang Betz 16:25dc4b811ad3 468
Wolfgang Betz 16:25dc4b811ad3 469 /* Transmission error */
Wolfgang Betz 16:25dc4b811ad3 470 if(x_irq_status.IRQ_TX_FIFO_ERROR) {
Wolfgang Betz 28:6a71e15d5272 471 #ifdef DEBUG_IRQ
Wolfgang Betz 28:6a71e15d5272 472 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 28:6a71e15d5272 473 debug("\n\r%s (%d): irq=%x", __func__, __LINE__, *tmp);
Wolfgang Betz 22:9165bd73c61e 474 #endif
Wolfgang Betz 23:4192649f35da 475 csma_ca_state(S_DISABLE); // disable CSMA/CA
Wolfgang Betz 16:25dc4b811ad3 476 cmd_strobe(SPIRIT1_STROBE_FTX);
Wolfgang Betz 28:6a71e15d5272 477 // cmd_strobe(SPIRIT1_STROBE_SABORT); // betzw: we do not know in which state we are (most likely it's a not stable state)!
Wolfgang Betz 16:25dc4b811ad3 478 if(_spirit_tx_started) {
Wolfgang Betz 16:25dc4b811ad3 479 _spirit_tx_started = false;
Wolfgang Betz 16:25dc4b811ad3 480 CLEAR_TXBUF();
Wolfgang Betz 16:25dc4b811ad3 481 /* call user callback */
Wolfgang Betz 16:25dc4b811ad3 482 if(_current_irq_callback) {
Wolfgang Betz 16:25dc4b811ad3 483 _current_irq_callback(TX_ERR);
Wolfgang Betz 16:25dc4b811ad3 484 }
Wolfgang Betz 16:25dc4b811ad3 485 }
Wolfgang Betz 16:25dc4b811ad3 486 }
Wolfgang Betz 16:25dc4b811ad3 487
Wolfgang Betz 16:25dc4b811ad3 488 /* The IRQ_TX_DATA_SENT notifies the packet received. Puts the SPIRIT1 in RX */
Wolfgang Betz 16:25dc4b811ad3 489 if(x_irq_status.IRQ_TX_DATA_SENT) {
Wolfgang Betz 28:6a71e15d5272 490 #ifdef DEBUG_IRQ
Wolfgang Betz 27:e68ffb6ac223 491 debug_if(!_spirit_tx_started, "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 22:9165bd73c61e 492 #endif
Wolfgang Betz 5:c9c5bc673c64 493
Wolfgang Betz 28:6a71e15d5272 494 _spirit_rx_err = false;
Wolfgang Betz 28:6a71e15d5272 495 _spirit_tx_started = false;
Wolfgang Betz 28:6a71e15d5272 496 // cmd_strobe(SPIRIT1_STROBE_RX); // data-sheet says that we will return to READY state automatically (furthermore we are in a not stable state)!
Wolfgang Betz 16:25dc4b811ad3 497 CLEAR_TXBUF();
Wolfgang Betz 16:25dc4b811ad3 498 CLEAR_RXBUF();
Wolfgang Betz 4:07537ca85c66 499
Wolfgang Betz 6:f5d01793bf86 500 /* call user callback */
Wolfgang Betz 6:f5d01793bf86 501 if(_current_irq_callback) {
Wolfgang Betz 23:4192649f35da 502 _current_irq_callback(TX_DONE);
Wolfgang Betz 6:f5d01793bf86 503 }
Wolfgang Betz 16:25dc4b811ad3 504 }
Wolfgang Betz 15:852b92eed64a 505
Wolfgang Betz 16:25dc4b811ad3 506 /* RX FIFO almost full */
Wolfgang Betz 16:25dc4b811ad3 507 if(x_irq_status.IRQ_RX_FIFO_ALMOST_FULL) {
Wolfgang Betz 16:25dc4b811ad3 508 if(_spirit_rx_err) {
Wolfgang Betz 16:25dc4b811ad3 509 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 510 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 511 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 512 } else {
Wolfgang Betz 16:25dc4b811ad3 513 uint8_t fifo_available = linear_fifo_read_num_elements_rx_fifo();
Wolfgang Betz 16:25dc4b811ad3 514 unsigned int remaining = MAX_PACKET_LEN - _spirit_rx_pos;
Wolfgang Betz 16:25dc4b811ad3 515 if(fifo_available > remaining) {
Wolfgang Betz 16:25dc4b811ad3 516 _spirit_rx_err = true;
Wolfgang Betz 16:25dc4b811ad3 517 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 518 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 519 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 520 } else {
Wolfgang Betz 16:25dc4b811ad3 521 spi_read_linear_fifo(fifo_available, &spirit_rx_buf[_spirit_rx_pos]);
Wolfgang Betz 16:25dc4b811ad3 522 _spirit_rx_pos += fifo_available;
Wolfgang Betz 16:25dc4b811ad3 523 if(!_is_receiving) {
Wolfgang Betz 16:25dc4b811ad3 524 _is_receiving = true;
Wolfgang Betz 16:25dc4b811ad3 525 start_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 526 }
Wolfgang Betz 16:25dc4b811ad3 527 }
Wolfgang Betz 16:25dc4b811ad3 528 }
Wolfgang Betz 16:25dc4b811ad3 529 }
Wolfgang Betz 15:852b92eed64a 530
Wolfgang Betz 16:25dc4b811ad3 531 /* The IRQ_RX_DATA_READY notifies a new packet arrived */
Wolfgang Betz 16:25dc4b811ad3 532 if(x_irq_status.IRQ_RX_DATA_READY) {
Wolfgang Betz 16:25dc4b811ad3 533 _is_receiving = false; // Finished receiving
Wolfgang Betz 16:25dc4b811ad3 534 stop_rx_timeout();
Wolfgang Betz 15:852b92eed64a 535
Wolfgang Betz 16:25dc4b811ad3 536 if(_spirit_rx_err) {
Wolfgang Betz 16:25dc4b811ad3 537 _spirit_rx_err = false;
Wolfgang Betz 16:25dc4b811ad3 538 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 539 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 540 } else {
Wolfgang Betz 16:25dc4b811ad3 541 spirit_rx_len = pkt_basic_get_received_pkt_length();
Wolfgang Betz 16:25dc4b811ad3 542
Wolfgang Betz 28:6a71e15d5272 543 #ifdef DEBUG_IRQ
Wolfgang Betz 27:e68ffb6ac223 544 debug_if(!(spirit_rx_len <= MAX_PACKET_LEN), "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 22:9165bd73c61e 545 #endif
Wolfgang Betz 16:25dc4b811ad3 546
Wolfgang Betz 16:25dc4b811ad3 547 for(; _spirit_rx_pos < spirit_rx_len;) {
Wolfgang Betz 25:2ec45788f28c 548 uint8_t to_receive = spirit_rx_len - _spirit_rx_pos;
Wolfgang Betz 16:25dc4b811ad3 549 if(to_receive > 0) {
Wolfgang Betz 16:25dc4b811ad3 550 spi_read_linear_fifo(to_receive, &spirit_rx_buf[_spirit_rx_pos]);
Wolfgang Betz 16:25dc4b811ad3 551 _spirit_rx_pos += to_receive;
Wolfgang Betz 16:25dc4b811ad3 552 }
Wolfgang Betz 16:25dc4b811ad3 553 }
Wolfgang Betz 16:25dc4b811ad3 554
Wolfgang Betz 16:25dc4b811ad3 555 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 556
Wolfgang Betz 16:25dc4b811ad3 557 last_rssi = qi_get_rssi(); //MGR
Wolfgang Betz 30:9c6dcfc47619 558 last_sqi = qi_get_sqi(); //MGR
Wolfgang Betz 16:25dc4b811ad3 559
Wolfgang Betz 16:25dc4b811ad3 560 /* call user callback */
Wolfgang Betz 16:25dc4b811ad3 561 if(_current_irq_callback) {
Wolfgang Betz 23:4192649f35da 562 _current_irq_callback(RX_DONE);
Wolfgang Betz 16:25dc4b811ad3 563 }
Wolfgang Betz 16:25dc4b811ad3 564 }
Wolfgang Betz 16:25dc4b811ad3 565 }
Wolfgang Betz 23:4192649f35da 566
Wolfgang Betz 28:6a71e15d5272 567 /* The IRQ_VALID_SYNC is used to notify a new packet is coming */
Wolfgang Betz 28:6a71e15d5272 568 if(x_irq_status.IRQ_VALID_SYNC) {
Wolfgang Betz 28:6a71e15d5272 569 _is_receiving = true;
Wolfgang Betz 28:6a71e15d5272 570 _spirit_rx_err = false;
Wolfgang Betz 28:6a71e15d5272 571 CLEAR_RXBUF();
Wolfgang Betz 28:6a71e15d5272 572 #ifdef DEBUG_IRQ
Wolfgang Betz 28:6a71e15d5272 573 debug_if(_spirit_tx_started, "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 28:6a71e15d5272 574 #endif
Wolfgang Betz 28:6a71e15d5272 575 start_rx_timeout();
Wolfgang Betz 23:4192649f35da 576 }
Wolfgang Betz 4:07537ca85c66 577 }