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:
Mon Nov 14 15:01:31 2016 +0100
Revision:
26:45dae8d48029
Parent:
25:2ec45788f28c
Child:
27:e68ffb6ac223
Set SPI clock freq to 1MHz

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