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 17 08:26:41 2016 +0100
Revision:
29:fe1b113f71d0
Parent:
28:6a71e15d5272
Child:
30:9c6dcfc47619
Version ping-pong fot two Nucleo boards

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 16:25dc4b811ad3 66 last_lqi = 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 18:d6f789f6f4c9 156 #ifdef RX_FIFO_THR_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 26:45dae8d48029 192 #ifdef RX_FIFO_THR_WA
Wolfgang Betz 26:45dae8d48029 193 // betzw - TODO: 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 23:4192649f35da 224 #ifndef NDEBUG
Wolfgang Betz 28:6a71e15d5272 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 16:25dc4b811ad3 240 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 241 stop_rx_timeout();
Wolfgang Betz 12:b8056eda4028 242
Wolfgang Betz 25:2ec45788f28c 243 state = SPIRIT1_STATUS();
Wolfgang Betz 25:2ec45788f28c 244 if(state == SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 16:25dc4b811ad3 245 cmd_strobe(SPIRIT1_STROBE_READY);
Wolfgang Betz 25:2ec45788f28c 246 } else if(state == SPIRIT1_STATE_RX) {
Wolfgang Betz 16:25dc4b811ad3 247 cmd_strobe(SPIRIT1_STROBE_SABORT);
Wolfgang Betz 25:2ec45788f28c 248 } else if(state != SPIRIT1_STATE_READY) {
Wolfgang Betz 25:2ec45788f28c 249 #ifndef NDEBUG
Wolfgang Betz 27:e68ffb6ac223 250 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, state>>1);
Wolfgang Betz 25:2ec45788f28c 251 #endif
Wolfgang Betz 16:25dc4b811ad3 252 }
Wolfgang Betz 25:2ec45788f28c 253
Wolfgang Betz 28:6a71e15d5272 254 BUSYWAIT_UNTIL((SPIRIT1_STATUS() == SPIRIT1_STATE_READY) && ((last_state & XO_ON) == XO_ON), STATE_TIMEOUT);
Wolfgang Betz 28:6a71e15d5272 255 if(last_state != (SPIRIT1_STATE_READY | XO_ON)) {
Wolfgang Betz 28:6a71e15d5272 256 error("\n\rSpirit1: failed to become ready (%x) => pls. reset!\n\r", last_state);
Wolfgang Betz 25:2ec45788f28c 257 enable_spirit_irq();
Wolfgang Betz 25:2ec45788f28c 258 return;
Wolfgang Betz 25:2ec45788f28c 259 }
Wolfgang Betz 25:2ec45788f28c 260
Wolfgang Betz 16:25dc4b811ad3 261 irq_clear_status();
Wolfgang Betz 3:0df38cfb1e53 262
Wolfgang Betz 16:25dc4b811ad3 263 enable_spirit_irq();
Wolfgang Betz 3:0df38cfb1e53 264 }
Wolfgang Betz 3:0df38cfb1e53 265
Wolfgang Betz 4:07537ca85c66 266 int SimpleSpirit1::off(void) {
Wolfgang Betz 16:25dc4b811ad3 267 if(spirit_on == ON) {
Wolfgang Betz 16:25dc4b811ad3 268 /* Disables the mcu to get IRQ from the SPIRIT1 */
Wolfgang Betz 16:25dc4b811ad3 269 disable_spirit_irq();
Wolfgang Betz 3:0df38cfb1e53 270
Wolfgang Betz 16:25dc4b811ad3 271 /* first stop rx/tx */
Wolfgang Betz 25:2ec45788f28c 272 set_ready_state();
Wolfgang Betz 3:0df38cfb1e53 273
Wolfgang Betz 28:6a71e15d5272 274 #ifdef USE_STANDBY_STATE
Wolfgang Betz 16:25dc4b811ad3 275 /* Puts the SPIRIT1 in STANDBY */
Wolfgang Betz 16:25dc4b811ad3 276 cmd_strobe(SPIRIT1_STROBE_STANDBY);
Wolfgang Betz 25:2ec45788f28c 277 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_STANDBY, STATE_TIMEOUT);
Wolfgang Betz 28:6a71e15d5272 278 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 26:45dae8d48029 279 error("\n\rSpirit1: failed to enter standby (%x)\n\r", last_state>>1);
Wolfgang Betz 16:25dc4b811ad3 280 return 1;
Wolfgang Betz 16:25dc4b811ad3 281 }
Wolfgang Betz 28:6a71e15d5272 282 #endif // USE_STANDBY_STATE
Wolfgang Betz 3:0df38cfb1e53 283
Wolfgang Betz 16:25dc4b811ad3 284 spirit_on = OFF;
Wolfgang Betz 16:25dc4b811ad3 285 _nr_of_irq_disables = 1;
Wolfgang Betz 16:25dc4b811ad3 286 _spirit_tx_started = false;
Wolfgang Betz 16:25dc4b811ad3 287 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 288 stop_rx_timeout();
Wolfgang Betz 12:b8056eda4028 289
Wolfgang Betz 16:25dc4b811ad3 290 CLEAR_TXBUF();
Wolfgang Betz 16:25dc4b811ad3 291 CLEAR_RXBUF();
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 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 309 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 310 _spirit_rx_err = false;
Wolfgang Betz 16:25dc4b811ad3 311 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 312 stop_rx_timeout();
Wolfgang Betz 3:0df38cfb1e53 313
Wolfgang Betz 16:25dc4b811ad3 314 /* Enables the mcu to get IRQ from the SPIRIT1 */
Wolfgang Betz 16:25dc4b811ad3 315 spirit_on = ON;
Wolfgang Betz 22:9165bd73c61e 316 #ifndef NDEBUG
Wolfgang Betz 27:e68ffb6ac223 317 debug_if(!(_nr_of_irq_disables == 1), "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 22:9165bd73c61e 318 #endif
Wolfgang Betz 16:25dc4b811ad3 319 enable_spirit_irq();
Wolfgang Betz 16:25dc4b811ad3 320 }
Wolfgang Betz 3:0df38cfb1e53 321
Wolfgang Betz 25:2ec45788f28c 322 #ifndef NDEBUG
Wolfgang Betz 26:45dae8d48029 323 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 27:e68ffb6ac223 324 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 325 }
Wolfgang Betz 25:2ec45788f28c 326 #endif
Wolfgang Betz 25:2ec45788f28c 327
Wolfgang Betz 16:25dc4b811ad3 328 return 0;
Wolfgang Betz 3:0df38cfb1e53 329 }
Wolfgang Betz 3:0df38cfb1e53 330
Wolfgang Betz 25:2ec45788f28c 331 uint8_t SimpleSpirit1::refresh_state(void) {
Wolfgang Betz 25:2ec45788f28c 332 uint8_t mcstate;
Wolfgang Betz 3:0df38cfb1e53 333
Wolfgang Betz 25:2ec45788f28c 334 SpiritSpiReadRegisters(MC_STATE0_BASE, 1, &mcstate);
Wolfgang Betz 3:0df38cfb1e53 335
Wolfgang Betz 16:25dc4b811ad3 336 return mcstate;
Wolfgang Betz 3:0df38cfb1e53 337 }
Wolfgang Betz 3:0df38cfb1e53 338
Wolfgang Betz 6:f5d01793bf86 339 int SimpleSpirit1::read(void *buf, unsigned int bufsize)
Wolfgang Betz 4:07537ca85c66 340 {
Wolfgang Betz 16:25dc4b811ad3 341 disable_spirit_irq();
Wolfgang Betz 6:f5d01793bf86 342
Wolfgang Betz 16:25dc4b811ad3 343 /* Checks if the RX buffer is empty */
Wolfgang Betz 16:25dc4b811ad3 344 if(IS_RXBUF_EMPTY()) {
Wolfgang Betz 16:25dc4b811ad3 345 CLEAR_RXBUF();
Wolfgang Betz 25:2ec45788f28c 346 set_ready_state();
Wolfgang Betz 25:2ec45788f28c 347
Wolfgang Betz 16:25dc4b811ad3 348 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 349 cmd_strobe(SPIRIT1_STROBE_RX);
Wolfgang Betz 25:2ec45788f28c 350 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 16:25dc4b811ad3 351 _spirit_rx_err = false;
Wolfgang Betz 16:25dc4b811ad3 352 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 353 stop_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 354 enable_spirit_irq();
Wolfgang Betz 16:25dc4b811ad3 355 return 0;
Wolfgang Betz 16:25dc4b811ad3 356 }
Wolfgang Betz 4:07537ca85c66 357
Wolfgang Betz 16:25dc4b811ad3 358 if(bufsize < spirit_rx_len) {
Wolfgang Betz 16:25dc4b811ad3 359 enable_spirit_irq();
Wolfgang Betz 6:f5d01793bf86 360
Wolfgang Betz 16:25dc4b811ad3 361 /* If buf has the correct size */
Wolfgang Betz 22:9165bd73c61e 362 #ifndef NDEBUG
Wolfgang Betz 22:9165bd73c61e 363 debug("\n\rTOO SMALL BUF\n\r");
Wolfgang Betz 22:9165bd73c61e 364 #endif
Wolfgang Betz 16:25dc4b811ad3 365 return 0;
Wolfgang Betz 16:25dc4b811ad3 366 } else {
Wolfgang Betz 16:25dc4b811ad3 367 /* Copies the packet received */
Wolfgang Betz 16:25dc4b811ad3 368 memcpy(buf, spirit_rx_buf, spirit_rx_len);
Wolfgang Betz 4:07537ca85c66 369
Wolfgang Betz 16:25dc4b811ad3 370 bufsize = spirit_rx_len;
Wolfgang Betz 16:25dc4b811ad3 371 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 372 stop_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 373 CLEAR_RXBUF();
Wolfgang Betz 4:07537ca85c66 374
Wolfgang Betz 16:25dc4b811ad3 375 enable_spirit_irq();
Wolfgang Betz 6:f5d01793bf86 376
Wolfgang Betz 16:25dc4b811ad3 377 return bufsize;
Wolfgang Betz 16:25dc4b811ad3 378 }
Wolfgang Betz 4:07537ca85c66 379
Wolfgang Betz 4:07537ca85c66 380 }
Wolfgang Betz 4:07537ca85c66 381
Wolfgang Betz 4:07537ca85c66 382 int SimpleSpirit1::channel_clear(void)
Wolfgang Betz 4:07537ca85c66 383 {
Wolfgang Betz 16:25dc4b811ad3 384 float rssi_value;
Wolfgang Betz 16:25dc4b811ad3 385 /* Local variable used to memorize the SPIRIT1 state */
Wolfgang Betz 16:25dc4b811ad3 386 uint8_t spirit_state = ON;
Wolfgang Betz 4:07537ca85c66 387
Wolfgang Betz 16:25dc4b811ad3 388 if(spirit_on == OFF) {
Wolfgang Betz 16:25dc4b811ad3 389 /* Wakes up the SPIRIT1 */
Wolfgang Betz 16:25dc4b811ad3 390 on();
Wolfgang Betz 16:25dc4b811ad3 391 spirit_state = OFF;
Wolfgang Betz 16:25dc4b811ad3 392 }
Wolfgang Betz 4:07537ca85c66 393
Wolfgang Betz 25:2ec45788f28c 394 #ifndef NDEBUG
Wolfgang Betz 26:45dae8d48029 395 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 27:e68ffb6ac223 396 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 397 }
Wolfgang Betz 25:2ec45788f28c 398 #endif
Wolfgang Betz 25:2ec45788f28c 399
Wolfgang Betz 16:25dc4b811ad3 400 disable_spirit_irq();
Wolfgang Betz 7:e90fa8f6bc6c 401
Wolfgang Betz 16:25dc4b811ad3 402 /* Reset State to Ready */
Wolfgang Betz 16:25dc4b811ad3 403 set_ready_state();
Wolfgang Betz 4:07537ca85c66 404
Wolfgang Betz 16:25dc4b811ad3 405 /* Stores the RSSI value */
Wolfgang Betz 16:25dc4b811ad3 406 rssi_value = qi_get_rssi_dbm();
Wolfgang Betz 4:07537ca85c66 407
Wolfgang Betz 16:25dc4b811ad3 408 enable_spirit_irq();
Wolfgang Betz 7:e90fa8f6bc6c 409
Wolfgang Betz 16:25dc4b811ad3 410 /* Puts the SPIRIT1 in its previous state */
Wolfgang Betz 16:25dc4b811ad3 411 if(spirit_state==OFF) {
Wolfgang Betz 16:25dc4b811ad3 412 off();
Wolfgang Betz 25:2ec45788f28c 413 #ifndef NDEBUG
Wolfgang Betz 28:6a71e15d5272 414 #ifdef USE_STANDBY_STATE
Wolfgang Betz 26:45dae8d48029 415 if(SPIRIT1_STATUS() != SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 28:6a71e15d5272 416 #else
Wolfgang Betz 28:6a71e15d5272 417 if(SPIRIT1_STATUS() != SPIRIT1_STATE_READY) {
Wolfgang Betz 28:6a71e15d5272 418 #endif
Wolfgang Betz 27:e68ffb6ac223 419 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 420 }
Wolfgang Betz 25:2ec45788f28c 421 #endif
Wolfgang Betz 16:25dc4b811ad3 422 } else {
Wolfgang Betz 16:25dc4b811ad3 423 disable_spirit_irq();
Wolfgang Betz 25:2ec45788f28c 424
Wolfgang Betz 25:2ec45788f28c 425 set_ready_state();
Wolfgang Betz 25:2ec45788f28c 426
Wolfgang Betz 16:25dc4b811ad3 427 cmd_strobe(SPIRIT1_STROBE_RX);
Wolfgang Betz 25:2ec45788f28c 428 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 28:6a71e15d5272 429 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) {
Wolfgang Betz 26:45dae8d48029 430 error("\n\rSpirit1: (#2) failed to enter rx (%x) => retry\n\r", last_state>>1);
Wolfgang Betz 25:2ec45788f28c 431 }
Wolfgang Betz 25:2ec45788f28c 432
Wolfgang Betz 16:25dc4b811ad3 433 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 434 _spirit_rx_err = false;
Wolfgang Betz 16:25dc4b811ad3 435 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 436 stop_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 437 enable_spirit_irq();
Wolfgang Betz 25:2ec45788f28c 438
Wolfgang Betz 25:2ec45788f28c 439 #ifndef NDEBUG
Wolfgang Betz 26:45dae8d48029 440 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 27:e68ffb6ac223 441 debug("\n\rAssert failed in: %s (%d): state=%x\n\r", __func__, __LINE__, last_state>>1);
Wolfgang Betz 26:45dae8d48029 442 }
Wolfgang Betz 25:2ec45788f28c 443 #endif
Wolfgang Betz 16:25dc4b811ad3 444 }
Wolfgang Betz 4:07537ca85c66 445
Wolfgang Betz 16:25dc4b811ad3 446 /* Checks the RSSI value with the threshold */
Wolfgang Betz 16:25dc4b811ad3 447 if(rssi_value<CCA_THRESHOLD) {
Wolfgang Betz 16:25dc4b811ad3 448 return 0;
Wolfgang Betz 16:25dc4b811ad3 449 } else {
Wolfgang Betz 16:25dc4b811ad3 450 return 1;
Wolfgang Betz 16:25dc4b811ad3 451 }
Wolfgang Betz 4:07537ca85c66 452 }
Wolfgang Betz 4:07537ca85c66 453
Wolfgang Betz 7:e90fa8f6bc6c 454 int SimpleSpirit1::get_pending_packet(void)
Wolfgang Betz 4:07537ca85c66 455 {
Wolfgang Betz 16:25dc4b811ad3 456 return !IS_RXBUF_EMPTY();
Wolfgang Betz 4:07537ca85c66 457 }
Wolfgang Betz 4:07537ca85c66 458
Wolfgang Betz 5:c9c5bc673c64 459 /** Spirit Irq Callback **/
Wolfgang Betz 5:c9c5bc673c64 460 void SimpleSpirit1::IrqHandler() {
Wolfgang Betz 16:25dc4b811ad3 461 st_lib_spirit_irqs x_irq_status;
Wolfgang Betz 4:07537ca85c66 462
Wolfgang Betz 16:25dc4b811ad3 463 /* get interrupt source from radio */
Wolfgang Betz 16:25dc4b811ad3 464 irq_get_status(&x_irq_status);
Wolfgang Betz 4:07537ca85c66 465
Wolfgang Betz 16:25dc4b811ad3 466 /* Reception errors */
Wolfgang Betz 26:45dae8d48029 467 if((x_irq_status.IRQ_RX_FIFO_ERROR) || (x_irq_status.IRQ_RX_DATA_DISC)) {
Wolfgang Betz 28:6a71e15d5272 468 #ifdef DEBUG_IRQ
Wolfgang Betz 28:6a71e15d5272 469 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 28:6a71e15d5272 470 debug("\n\r%s (%d): irq=%x", __func__, __LINE__, *tmp);
Wolfgang Betz 23:4192649f35da 471 #endif
Wolfgang Betz 16:25dc4b811ad3 472 _spirit_rx_err = true;
Wolfgang Betz 16:25dc4b811ad3 473 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 474 CLEAR_RXBUF();
Wolfgang Betz 6:f5d01793bf86 475 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 476 stop_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 477 if(_spirit_tx_started) {
Wolfgang Betz 16:25dc4b811ad3 478 _spirit_tx_started = false;
Wolfgang Betz 16:25dc4b811ad3 479 CLEAR_TXBUF();
Wolfgang Betz 16:25dc4b811ad3 480 /* call user callback */
Wolfgang Betz 16:25dc4b811ad3 481 if(_current_irq_callback) {
Wolfgang Betz 16:25dc4b811ad3 482 _current_irq_callback(TX_ERR);
Wolfgang Betz 6:f5d01793bf86 483 }
Wolfgang Betz 6:f5d01793bf86 484 }
Wolfgang Betz 16:25dc4b811ad3 485 }
Wolfgang Betz 16:25dc4b811ad3 486
Wolfgang Betz 16:25dc4b811ad3 487 /* Transmission error */
Wolfgang Betz 16:25dc4b811ad3 488 if(x_irq_status.IRQ_TX_FIFO_ERROR) {
Wolfgang Betz 28:6a71e15d5272 489 #ifdef DEBUG_IRQ
Wolfgang Betz 28:6a71e15d5272 490 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 28:6a71e15d5272 491 debug("\n\r%s (%d): irq=%x", __func__, __LINE__, *tmp);
Wolfgang Betz 22:9165bd73c61e 492 #endif
Wolfgang Betz 23:4192649f35da 493 csma_ca_state(S_DISABLE); // disable CSMA/CA
Wolfgang Betz 16:25dc4b811ad3 494 cmd_strobe(SPIRIT1_STROBE_FTX);
Wolfgang Betz 28:6a71e15d5272 495 // 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 496 if(_spirit_tx_started) {
Wolfgang Betz 16:25dc4b811ad3 497 _spirit_tx_started = false;
Wolfgang Betz 16:25dc4b811ad3 498 CLEAR_TXBUF();
Wolfgang Betz 16:25dc4b811ad3 499 /* call user callback */
Wolfgang Betz 16:25dc4b811ad3 500 if(_current_irq_callback) {
Wolfgang Betz 16:25dc4b811ad3 501 _current_irq_callback(TX_ERR);
Wolfgang Betz 16:25dc4b811ad3 502 }
Wolfgang Betz 16:25dc4b811ad3 503 }
Wolfgang Betz 16:25dc4b811ad3 504 }
Wolfgang Betz 16:25dc4b811ad3 505
Wolfgang Betz 16:25dc4b811ad3 506 /* The IRQ_TX_DATA_SENT notifies the packet received. Puts the SPIRIT1 in RX */
Wolfgang Betz 16:25dc4b811ad3 507 if(x_irq_status.IRQ_TX_DATA_SENT) {
Wolfgang Betz 28:6a71e15d5272 508 #ifdef DEBUG_IRQ
Wolfgang Betz 27:e68ffb6ac223 509 debug_if(!_spirit_tx_started, "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 22:9165bd73c61e 510 #endif
Wolfgang Betz 5:c9c5bc673c64 511
Wolfgang Betz 28:6a71e15d5272 512 _spirit_rx_err = false;
Wolfgang Betz 28:6a71e15d5272 513 _spirit_tx_started = false;
Wolfgang Betz 28:6a71e15d5272 514 // 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 515 CLEAR_TXBUF();
Wolfgang Betz 16:25dc4b811ad3 516 CLEAR_RXBUF();
Wolfgang Betz 4:07537ca85c66 517
Wolfgang Betz 6:f5d01793bf86 518 /* call user callback */
Wolfgang Betz 6:f5d01793bf86 519 if(_current_irq_callback) {
Wolfgang Betz 23:4192649f35da 520 _current_irq_callback(TX_DONE);
Wolfgang Betz 6:f5d01793bf86 521 }
Wolfgang Betz 16:25dc4b811ad3 522 }
Wolfgang Betz 15:852b92eed64a 523
Wolfgang Betz 16:25dc4b811ad3 524 /* RX FIFO almost full */
Wolfgang Betz 16:25dc4b811ad3 525 if(x_irq_status.IRQ_RX_FIFO_ALMOST_FULL) {
Wolfgang Betz 16:25dc4b811ad3 526 if(_spirit_rx_err) {
Wolfgang Betz 16:25dc4b811ad3 527 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 528 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 529 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 530 stop_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 531 } else {
Wolfgang Betz 16:25dc4b811ad3 532 uint8_t fifo_available = linear_fifo_read_num_elements_rx_fifo();
Wolfgang Betz 16:25dc4b811ad3 533 unsigned int remaining = MAX_PACKET_LEN - _spirit_rx_pos;
Wolfgang Betz 16:25dc4b811ad3 534 if(fifo_available > remaining) {
Wolfgang Betz 16:25dc4b811ad3 535 _spirit_rx_err = true;
Wolfgang Betz 16:25dc4b811ad3 536 _is_receiving = false;
Wolfgang Betz 16:25dc4b811ad3 537 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 538 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 539 stop_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 540 } else {
Wolfgang Betz 16:25dc4b811ad3 541 spi_read_linear_fifo(fifo_available, &spirit_rx_buf[_spirit_rx_pos]);
Wolfgang Betz 16:25dc4b811ad3 542 _spirit_rx_pos += fifo_available;
Wolfgang Betz 16:25dc4b811ad3 543 if(!_is_receiving) {
Wolfgang Betz 16:25dc4b811ad3 544 _is_receiving = true;
Wolfgang Betz 16:25dc4b811ad3 545 start_rx_timeout();
Wolfgang Betz 16:25dc4b811ad3 546 }
Wolfgang Betz 16:25dc4b811ad3 547 }
Wolfgang Betz 16:25dc4b811ad3 548 }
Wolfgang Betz 16:25dc4b811ad3 549 }
Wolfgang Betz 15:852b92eed64a 550
Wolfgang Betz 16:25dc4b811ad3 551 /* The IRQ_RX_DATA_READY notifies a new packet arrived */
Wolfgang Betz 16:25dc4b811ad3 552 if(x_irq_status.IRQ_RX_DATA_READY) {
Wolfgang Betz 16:25dc4b811ad3 553 _is_receiving = false; // Finished receiving
Wolfgang Betz 16:25dc4b811ad3 554 stop_rx_timeout();
Wolfgang Betz 15:852b92eed64a 555
Wolfgang Betz 16:25dc4b811ad3 556 if(_spirit_rx_err) {
Wolfgang Betz 16:25dc4b811ad3 557 _spirit_rx_err = false;
Wolfgang Betz 16:25dc4b811ad3 558 CLEAR_RXBUF();
Wolfgang Betz 16:25dc4b811ad3 559 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 560 } else {
Wolfgang Betz 16:25dc4b811ad3 561 spirit_rx_len = pkt_basic_get_received_pkt_length();
Wolfgang Betz 16:25dc4b811ad3 562
Wolfgang Betz 28:6a71e15d5272 563 #ifdef DEBUG_IRQ
Wolfgang Betz 27:e68ffb6ac223 564 debug_if(!(spirit_rx_len <= MAX_PACKET_LEN), "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 22:9165bd73c61e 565 #endif
Wolfgang Betz 16:25dc4b811ad3 566
Wolfgang Betz 16:25dc4b811ad3 567 for(; _spirit_rx_pos < spirit_rx_len;) {
Wolfgang Betz 25:2ec45788f28c 568 uint8_t to_receive = spirit_rx_len - _spirit_rx_pos;
Wolfgang Betz 16:25dc4b811ad3 569 if(to_receive > 0) {
Wolfgang Betz 16:25dc4b811ad3 570 spi_read_linear_fifo(to_receive, &spirit_rx_buf[_spirit_rx_pos]);
Wolfgang Betz 16:25dc4b811ad3 571 _spirit_rx_pos += to_receive;
Wolfgang Betz 16:25dc4b811ad3 572 }
Wolfgang Betz 16:25dc4b811ad3 573 }
Wolfgang Betz 16:25dc4b811ad3 574
Wolfgang Betz 16:25dc4b811ad3 575 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 16:25dc4b811ad3 576
Wolfgang Betz 16:25dc4b811ad3 577 last_rssi = qi_get_rssi(); //MGR
Wolfgang Betz 16:25dc4b811ad3 578 last_lqi = qi_get_lqi(); //MGR
Wolfgang Betz 16:25dc4b811ad3 579
Wolfgang Betz 16:25dc4b811ad3 580 /* call user callback */
Wolfgang Betz 16:25dc4b811ad3 581 if(_current_irq_callback) {
Wolfgang Betz 23:4192649f35da 582 _current_irq_callback(RX_DONE);
Wolfgang Betz 16:25dc4b811ad3 583 }
Wolfgang Betz 16:25dc4b811ad3 584 }
Wolfgang Betz 16:25dc4b811ad3 585 }
Wolfgang Betz 23:4192649f35da 586
Wolfgang Betz 28:6a71e15d5272 587 /* The IRQ_VALID_SYNC is used to notify a new packet is coming */
Wolfgang Betz 28:6a71e15d5272 588 if(x_irq_status.IRQ_VALID_SYNC) {
Wolfgang Betz 28:6a71e15d5272 589 _is_receiving = true;
Wolfgang Betz 28:6a71e15d5272 590 _spirit_rx_err = false;
Wolfgang Betz 28:6a71e15d5272 591 CLEAR_RXBUF();
Wolfgang Betz 28:6a71e15d5272 592 #ifdef DEBUG_IRQ
Wolfgang Betz 28:6a71e15d5272 593 debug_if(_spirit_tx_started, "\n\rAssert failed in: %s (%d)\n\r", __func__, __LINE__);
Wolfgang Betz 28:6a71e15d5272 594 #endif
Wolfgang Betz 28:6a71e15d5272 595 start_rx_timeout();
Wolfgang Betz 23:4192649f35da 596 }
Wolfgang Betz 4:07537ca85c66 597 }