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:
Thu Nov 24 08:47:27 2016 +0100
Revision:
39:75481c4c6655
Parent:
37:bc043030b55a
Child:
40:343254875d89
Treat RX/TX race condition in favor of TX

Who changed what in which revision?

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