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 Jul 06 14:15:57 2017 +0200
Revision:
68:247fd6e0ab66
Parent:
67:93bec0baf1de
Child:
69:22e9b407effa
Better handling of persisten CCA timeouts

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 60:956afcef7ef7 54 _spi.frequency(10000000); // 10MHz (i.e. max speed allowed for 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 47:3a30b960a8c2 112 irq_set_status(VALID_SYNC, S_ENABLE);
Wolfgang Betz 34:edda6a7238ec 113 irq_set_status(TX_FIFO_ERROR, S_ENABLE);
Wolfgang Betz 34:edda6a7238ec 114 irq_set_status(RX_FIFO_ERROR, S_ENABLE);
Wolfgang Betz 47:3a30b960a8c2 115 #ifndef RX_FIFO_THR_WA
Wolfgang Betz 47:3a30b960a8c2 116 irq_set_status(TX_FIFO_ALMOST_EMPTY, S_ENABLE);
Wolfgang Betz 34:edda6a7238ec 117 irq_set_status(RX_FIFO_ALMOST_FULL, S_ENABLE);
Wolfgang Betz 47:3a30b960a8c2 118 #endif // !RX_FIFO_THR_WA
Wolfgang Betz 34:edda6a7238ec 119
Wolfgang Betz 34:edda6a7238ec 120 /* Configure Spirit1 */
Wolfgang Betz 34:edda6a7238ec 121 radio_persistent_rx(S_ENABLE);
Wolfgang Betz 34:edda6a7238ec 122 qi_set_sqi_threshold(SQI_TH_0);
Wolfgang Betz 34:edda6a7238ec 123 qi_sqi_check(S_ENABLE);
Wolfgang Betz 34:edda6a7238ec 124 qi_set_rssi_threshold_dbm(CCA_THRESHOLD);
Wolfgang Betz 34:edda6a7238ec 125 timer_set_rx_timeout_stop_condition(SQI_ABOVE_THRESHOLD);
Wolfgang Betz 34:edda6a7238ec 126 timer_set_infinite_rx_timeout();
Wolfgang Betz 34:edda6a7238ec 127 radio_afc_freeze_on_sync(S_ENABLE);
Wolfgang Betz 34:edda6a7238ec 128 calibration_rco(S_ENABLE);
Wolfgang Betz 34:edda6a7238ec 129
Wolfgang Betz 34:edda6a7238ec 130 spirit_on = OFF;
Wolfgang Betz 34:edda6a7238ec 131 CLEAR_TXBUF();
Wolfgang Betz 34:edda6a7238ec 132 CLEAR_RXBUF();
Wolfgang Betz 34:edda6a7238ec 133 _spirit_tx_started = false;
Wolfgang Betz 34:edda6a7238ec 134 _is_receiving = false;
Wolfgang Betz 34:edda6a7238ec 135
Wolfgang Betz 34:edda6a7238ec 136 /* Configure the radio to route the IRQ signal to its GPIO 3 */
Wolfgang Betz 34:edda6a7238ec 137 SGpioInit x_gpio_init = {
Wolfgang Betz 34:edda6a7238ec 138 SPIRIT_GPIO_IRQ,
Wolfgang Betz 34:edda6a7238ec 139 SPIRIT_GPIO_MODE_DIGITAL_OUTPUT_LP,
Wolfgang Betz 34:edda6a7238ec 140 SPIRIT_GPIO_DIG_OUT_IRQ
Wolfgang Betz 34:edda6a7238ec 141 };
Wolfgang Betz 34:edda6a7238ec 142 spirit_gpio_init(&x_gpio_init);
Wolfgang Betz 34:edda6a7238ec 143
Wolfgang Betz 34:edda6a7238ec 144 /* Setup CSMA/CA */
Wolfgang Betz 34:edda6a7238ec 145 CsmaInit x_csma_init = {
Wolfgang Betz 34:edda6a7238ec 146 S_ENABLE, // enable persistent mode
Wolfgang Betz 34:edda6a7238ec 147 TBIT_TIME_64, // Tcca time
Wolfgang Betz 34:edda6a7238ec 148 TCCA_TIME_3, // Lcca length
Wolfgang Betz 66:1e09d233280b 149 5, // max nr of backoffs (<8)
Wolfgang Betz 34:edda6a7238ec 150 1, // BU counter seed
Wolfgang Betz 34:edda6a7238ec 151 8 // BU prescaler
Wolfgang Betz 34:edda6a7238ec 152 };
Wolfgang Betz 34:edda6a7238ec 153 csma_ca_init(&x_csma_init);
Wolfgang Betz 34:edda6a7238ec 154
Wolfgang Betz 34:edda6a7238ec 155 #ifdef USE_STANDBY_STATE
Wolfgang Betz 34:edda6a7238ec 156 /* Puts the SPIRIT1 in STANDBY mode (125us -> rx/tx) */
Wolfgang Betz 34:edda6a7238ec 157 cmd_strobe(SPIRIT1_STROBE_STANDBY);
Wolfgang Betz 34:edda6a7238ec 158 #endif // USE_STANDBY_STATE
Wolfgang Betz 34:edda6a7238ec 159 }
Wolfgang Betz 34:edda6a7238ec 160
Wolfgang Betz 45:2d01cc9bc761 161 static volatile int tx_fifo_remaining = 0; // to be used in irq handler
Wolfgang Betz 45:2d01cc9bc761 162 static volatile int tx_buffer_pos = 0; // to be used in irq handler
Wolfgang Betz 45:2d01cc9bc761 163 const volatile uint8_t *tx_fifo_buffer = NULL; // to be used in irq handler
Wolfgang Betz 64:28ef790e4ef7 164 int SimpleSpirit1::send(const void *payload, unsigned int payload_len, bool use_csma_ca) {
Wolfgang Betz 34:edda6a7238ec 165 /* Checks if the payload length is supported */
Wolfgang Betz 34:edda6a7238ec 166 if(payload_len > MAX_PACKET_LEN) {
Wolfgang Betz 34:edda6a7238ec 167 return RADIO_TX_ERR;
Wolfgang Betz 34:edda6a7238ec 168 }
Wolfgang Betz 34:edda6a7238ec 169
Wolfgang Betz 34:edda6a7238ec 170 disable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 171
Wolfgang Betz 34:edda6a7238ec 172 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 34:edda6a7238ec 173 #ifndef NDEBUG
Wolfgang Betz 34:edda6a7238ec 174 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) {
Wolfgang Betz 68:247fd6e0ab66 175 debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1);
Wolfgang Betz 34:edda6a7238ec 176 }
Wolfgang Betz 34:edda6a7238ec 177 #endif
Wolfgang Betz 34:edda6a7238ec 178
Wolfgang Betz 34:edda6a7238ec 179 /* Reset State to Ready */
Wolfgang Betz 34:edda6a7238ec 180 set_ready_state();
Wolfgang Betz 34:edda6a7238ec 181
Wolfgang Betz 34:edda6a7238ec 182 cmd_strobe(SPIRIT1_STROBE_FTX); // flush TX FIFO buffer
Wolfgang Betz 34:edda6a7238ec 183
Wolfgang Betz 34:edda6a7238ec 184 #ifndef NDEBUG
Wolfgang Betz 68:247fd6e0ab66 185 debug_if(!(linear_fifo_read_num_elements_tx_fifo() == 0), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 34:edda6a7238ec 186 #endif
Wolfgang Betz 34:edda6a7238ec 187
Wolfgang Betz 34:edda6a7238ec 188 pkt_basic_set_payload_length(payload_len); // set desired payload len
Wolfgang Betz 34:edda6a7238ec 189
Wolfgang Betz 64:28ef790e4ef7 190 if(use_csma_ca) {
Wolfgang Betz 64:28ef790e4ef7 191 csma_ca_state(S_ENABLE); // enable CSMA/CA
Wolfgang Betz 64:28ef790e4ef7 192 }
Wolfgang Betz 34:edda6a7238ec 193
Wolfgang Betz 45:2d01cc9bc761 194 /* Init buffer & number of bytes to be send */
Wolfgang Betz 45:2d01cc9bc761 195 tx_fifo_remaining = payload_len;
Wolfgang Betz 45:2d01cc9bc761 196 tx_fifo_buffer = (const uint8_t*)payload;
Wolfgang Betz 45:2d01cc9bc761 197
Wolfgang Betz 45:2d01cc9bc761 198 int8_t fifo_available = SPIRIT_MAX_FIFO_LEN; // fill-up whole fifo
Wolfgang Betz 45:2d01cc9bc761 199 int8_t to_send = (tx_fifo_remaining > fifo_available) ? fifo_available : tx_fifo_remaining;
Wolfgang Betz 34:edda6a7238ec 200
Wolfgang Betz 45:2d01cc9bc761 201 tx_fifo_remaining -= to_send;
Wolfgang Betz 34:edda6a7238ec 202
Wolfgang Betz 45:2d01cc9bc761 203 /* Fill FIFO Buffer */
Wolfgang Betz 45:2d01cc9bc761 204 if(to_send > 0) {
Wolfgang Betz 45:2d01cc9bc761 205 spi_write_linear_fifo(to_send, (uint8_t*)&tx_fifo_buffer[0]);
Wolfgang Betz 45:2d01cc9bc761 206 }
Wolfgang Betz 34:edda6a7238ec 207
Wolfgang Betz 45:2d01cc9bc761 208 tx_buffer_pos = to_send;
Wolfgang Betz 34:edda6a7238ec 209 _spirit_tx_started = true;
Wolfgang Betz 34:edda6a7238ec 210
Wolfgang Betz 34:edda6a7238ec 211 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 212
Wolfgang Betz 45:2d01cc9bc761 213 /* Start transmitting */
Wolfgang Betz 45:2d01cc9bc761 214 cmd_strobe(SPIRIT1_STROBE_TX);
Wolfgang Betz 45:2d01cc9bc761 215
Wolfgang Betz 45:2d01cc9bc761 216 while(tx_fifo_remaining != 0); // wait until not everything is yet send (evtl. by irq handler)
Wolfgang Betz 45:2d01cc9bc761 217
Wolfgang Betz 34:edda6a7238ec 218 BUSYWAIT_UNTIL(!_spirit_tx_started, STATE_TIMEOUT);
Wolfgang Betz 34:edda6a7238ec 219 #ifdef HEAVY_DEBUG
Wolfgang Betz 68:247fd6e0ab66 220 debug("\r\n%s (%d): state=%x, _spirit_tx_started=%d\r\n", __func__, __LINE__, SPIRIT1_STATUS()>>1, _spirit_tx_started);
Wolfgang Betz 34:edda6a7238ec 221 #endif
Wolfgang Betz 34:edda6a7238ec 222
Wolfgang Betz 64:28ef790e4ef7 223 if(use_csma_ca) {
Wolfgang Betz 64:28ef790e4ef7 224 csma_ca_state(S_DISABLE); // disable CSMA/CA
Wolfgang Betz 64:28ef790e4ef7 225 }
Wolfgang Betz 43:a512f909514a 226
Wolfgang Betz 34:edda6a7238ec 227 cmd_strobe(SPIRIT1_STROBE_RX); // Return to RX state
Wolfgang Betz 34:edda6a7238ec 228
Wolfgang Betz 68:247fd6e0ab66 229 if(_spirit_tx_started) { // in case of state timeout
Wolfgang Betz 68:247fd6e0ab66 230 _spirit_tx_started = false;
Wolfgang Betz 68:247fd6e0ab66 231 return RADIO_TX_ERR;
Wolfgang Betz 68:247fd6e0ab66 232 } else {
Wolfgang Betz 68:247fd6e0ab66 233 return RADIO_TX_OK;
Wolfgang Betz 68:247fd6e0ab66 234 }
Wolfgang Betz 34:edda6a7238ec 235 }
Wolfgang Betz 34:edda6a7238ec 236
Wolfgang Betz 34:edda6a7238ec 237 /** Set Ready State **/
Wolfgang Betz 34:edda6a7238ec 238 void SimpleSpirit1::set_ready_state(void) {
Wolfgang Betz 34:edda6a7238ec 239 uint16_t state;
Wolfgang Betz 34:edda6a7238ec 240
Wolfgang Betz 34:edda6a7238ec 241 disable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 242
Wolfgang Betz 34:edda6a7238ec 243 _spirit_tx_started = false;
Wolfgang Betz 34:edda6a7238ec 244 _is_receiving = false;
Wolfgang Betz 34:edda6a7238ec 245 stop_rx_timeout();
Wolfgang Betz 34:edda6a7238ec 246
Wolfgang Betz 34:edda6a7238ec 247 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 34:edda6a7238ec 248 CLEAR_RXBUF();
Wolfgang Betz 34:edda6a7238ec 249 CLEAR_TXBUF();
Wolfgang Betz 34:edda6a7238ec 250
Wolfgang Betz 34:edda6a7238ec 251 state = SPIRIT1_STATUS();
Wolfgang Betz 34:edda6a7238ec 252 if(state == SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 34:edda6a7238ec 253 cmd_strobe(SPIRIT1_STROBE_READY);
Wolfgang Betz 34:edda6a7238ec 254 } else if(state == SPIRIT1_STATE_RX) {
Wolfgang Betz 34:edda6a7238ec 255 cmd_strobe(SPIRIT1_STROBE_SABORT);
Wolfgang Betz 34:edda6a7238ec 256 } else if(state != SPIRIT1_STATE_READY) {
Wolfgang Betz 34:edda6a7238ec 257 #ifndef NDEBUG
Wolfgang Betz 68:247fd6e0ab66 258 debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, state>>1);
Wolfgang Betz 34:edda6a7238ec 259 #endif
Wolfgang Betz 34:edda6a7238ec 260 }
Wolfgang Betz 34:edda6a7238ec 261
Wolfgang Betz 34:edda6a7238ec 262 BUSYWAIT_UNTIL((SPIRIT1_STATUS() == SPIRIT1_STATE_READY) && ((last_state & XO_ON) == XO_ON), STATE_TIMEOUT);
Wolfgang Betz 34:edda6a7238ec 263 if(last_state != (SPIRIT1_STATE_READY | XO_ON)) {
Wolfgang Betz 68:247fd6e0ab66 264 error("\r\nSpirit1: failed to become ready (%x) => pls. reset!\r\n", last_state);
Wolfgang Betz 34:edda6a7238ec 265 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 266 return;
Wolfgang Betz 34:edda6a7238ec 267 }
Wolfgang Betz 34:edda6a7238ec 268
Wolfgang Betz 34:edda6a7238ec 269 irq_clear_status();
Wolfgang Betz 34:edda6a7238ec 270
Wolfgang Betz 34:edda6a7238ec 271 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 272 }
Wolfgang Betz 34:edda6a7238ec 273
Wolfgang Betz 34:edda6a7238ec 274 int SimpleSpirit1::off(void) {
Wolfgang Betz 34:edda6a7238ec 275 if(spirit_on == ON) {
Wolfgang Betz 34:edda6a7238ec 276 /* Disables the mcu to get IRQ from the SPIRIT1 */
Wolfgang Betz 34:edda6a7238ec 277 disable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 278
Wolfgang Betz 34:edda6a7238ec 279 /* first stop rx/tx */
Wolfgang Betz 34:edda6a7238ec 280 set_ready_state();
Wolfgang Betz 34:edda6a7238ec 281
Wolfgang Betz 34:edda6a7238ec 282 #ifdef USE_STANDBY_STATE
Wolfgang Betz 34:edda6a7238ec 283 /* Puts the SPIRIT1 in STANDBY */
Wolfgang Betz 34:edda6a7238ec 284 cmd_strobe(SPIRIT1_STROBE_STANDBY);
Wolfgang Betz 34:edda6a7238ec 285 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_STANDBY, STATE_TIMEOUT);
Wolfgang Betz 34:edda6a7238ec 286 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 68:247fd6e0ab66 287 error("\r\nSpirit1: failed to enter standby (%x)\r\n", last_state>>1);
Wolfgang Betz 34:edda6a7238ec 288 return 1;
Wolfgang Betz 34:edda6a7238ec 289 }
Wolfgang Betz 34:edda6a7238ec 290 #endif // USE_STANDBY_STATE
Wolfgang Betz 34:edda6a7238ec 291
Wolfgang Betz 34:edda6a7238ec 292 spirit_on = OFF;
Wolfgang Betz 34:edda6a7238ec 293 _nr_of_irq_disables = 1;
Wolfgang Betz 34:edda6a7238ec 294 }
Wolfgang Betz 34:edda6a7238ec 295 return 0;
Wolfgang Betz 34:edda6a7238ec 296 }
Wolfgang Betz 34:edda6a7238ec 297
Wolfgang Betz 34:edda6a7238ec 298 int SimpleSpirit1::on(void) {
Wolfgang Betz 34:edda6a7238ec 299 if(spirit_on == OFF) {
Wolfgang Betz 34:edda6a7238ec 300 set_ready_state();
Wolfgang Betz 34:edda6a7238ec 301
Wolfgang Betz 34:edda6a7238ec 302 /* now we go to Rx */
Wolfgang Betz 34:edda6a7238ec 303 cmd_strobe(SPIRIT1_STROBE_RX);
Wolfgang Betz 34:edda6a7238ec 304
Wolfgang Betz 34:edda6a7238ec 305 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 34:edda6a7238ec 306 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) {
Wolfgang Betz 68:247fd6e0ab66 307 error("\r\nSpirit1: failed to enter rx (%x) => retry\r\n", last_state>>1);
Wolfgang Betz 34:edda6a7238ec 308 }
Wolfgang Betz 34:edda6a7238ec 309
Wolfgang Betz 34:edda6a7238ec 310 /* Enables the mcu to get IRQ from the SPIRIT1 */
Wolfgang Betz 34:edda6a7238ec 311 spirit_on = ON;
Wolfgang Betz 34:edda6a7238ec 312 #ifndef NDEBUG
Wolfgang Betz 68:247fd6e0ab66 313 debug_if(!(_nr_of_irq_disables == 1), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 34:edda6a7238ec 314 #endif
Wolfgang Betz 34:edda6a7238ec 315 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 316 }
Wolfgang Betz 34:edda6a7238ec 317
Wolfgang Betz 34:edda6a7238ec 318 #ifndef NDEBUG
Wolfgang Betz 34:edda6a7238ec 319 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 68:247fd6e0ab66 320 debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1);
Wolfgang Betz 34:edda6a7238ec 321 }
Wolfgang Betz 34:edda6a7238ec 322 #endif
Wolfgang Betz 34:edda6a7238ec 323
Wolfgang Betz 34:edda6a7238ec 324 return 0;
Wolfgang Betz 34:edda6a7238ec 325 }
Wolfgang Betz 34:edda6a7238ec 326
Wolfgang Betz 34:edda6a7238ec 327 uint8_t SimpleSpirit1::refresh_state(void) {
Wolfgang Betz 34:edda6a7238ec 328 uint8_t mcstate;
Wolfgang Betz 34:edda6a7238ec 329
Wolfgang Betz 34:edda6a7238ec 330 SpiritSpiReadRegisters(MC_STATE0_BASE, 1, &mcstate);
Wolfgang Betz 34:edda6a7238ec 331
Wolfgang Betz 34:edda6a7238ec 332 return mcstate;
Wolfgang Betz 34:edda6a7238ec 333 }
Wolfgang Betz 34:edda6a7238ec 334
Wolfgang Betz 34:edda6a7238ec 335 int SimpleSpirit1::read(void *buf, unsigned int bufsize)
Wolfgang Betz 34:edda6a7238ec 336 {
Wolfgang Betz 34:edda6a7238ec 337 disable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 338
Wolfgang Betz 34:edda6a7238ec 339 /* Checks if the RX buffer is empty */
Wolfgang Betz 34:edda6a7238ec 340 if(IS_RXBUF_EMPTY()) {
Wolfgang Betz 43:a512f909514a 341 #ifndef NDEBUG
Wolfgang Betz 68:247fd6e0ab66 342 debug("\r\nBuffer is empty\r\n");
Wolfgang Betz 43:a512f909514a 343 #endif
Wolfgang Betz 34:edda6a7238ec 344 set_ready_state();
Wolfgang Betz 34:edda6a7238ec 345
Wolfgang Betz 34:edda6a7238ec 346 cmd_strobe(SPIRIT1_STROBE_RX);
Wolfgang Betz 34:edda6a7238ec 347 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 34:edda6a7238ec 348 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 349 return 0;
Wolfgang Betz 34:edda6a7238ec 350 }
Wolfgang Betz 34:edda6a7238ec 351
Wolfgang Betz 34:edda6a7238ec 352 if(bufsize < spirit_rx_len) {
Wolfgang Betz 34:edda6a7238ec 353 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 354
Wolfgang Betz 34:edda6a7238ec 355 /* If buf has the correct size */
Wolfgang Betz 34:edda6a7238ec 356 #ifndef NDEBUG
Wolfgang Betz 68:247fd6e0ab66 357 debug("\r\nTOO SMALL BUF\r\n");
Wolfgang Betz 34:edda6a7238ec 358 #endif
Wolfgang Betz 34:edda6a7238ec 359 return 0;
Wolfgang Betz 34:edda6a7238ec 360 } else {
Wolfgang Betz 34:edda6a7238ec 361 /* Copies the packet received */
Wolfgang Betz 34:edda6a7238ec 362 memcpy(buf, spirit_rx_buf, spirit_rx_len);
Wolfgang Betz 34:edda6a7238ec 363
Wolfgang Betz 34:edda6a7238ec 364 bufsize = spirit_rx_len;
Wolfgang Betz 34:edda6a7238ec 365 CLEAR_RXBUF();
Wolfgang Betz 34:edda6a7238ec 366
Wolfgang Betz 34:edda6a7238ec 367 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 368
Wolfgang Betz 34:edda6a7238ec 369 return bufsize;
Wolfgang Betz 34:edda6a7238ec 370 }
Wolfgang Betz 34:edda6a7238ec 371
Wolfgang Betz 34:edda6a7238ec 372 }
Wolfgang Betz 34:edda6a7238ec 373
Wolfgang Betz 34:edda6a7238ec 374 int SimpleSpirit1::channel_clear(void)
Wolfgang Betz 34:edda6a7238ec 375 {
Wolfgang Betz 34:edda6a7238ec 376 float rssi_value;
Wolfgang Betz 34:edda6a7238ec 377 /* Local variable used to memorize the SPIRIT1 state */
Wolfgang Betz 34:edda6a7238ec 378 uint8_t spirit_state = ON;
Wolfgang Betz 34:edda6a7238ec 379
Wolfgang Betz 34:edda6a7238ec 380 if(spirit_on == OFF) {
Wolfgang Betz 34:edda6a7238ec 381 /* Wakes up the SPIRIT1 */
Wolfgang Betz 34:edda6a7238ec 382 on();
Wolfgang Betz 34:edda6a7238ec 383 spirit_state = OFF;
Wolfgang Betz 34:edda6a7238ec 384 }
Wolfgang Betz 34:edda6a7238ec 385
Wolfgang Betz 34:edda6a7238ec 386 #ifndef NDEBUG
Wolfgang Betz 34:edda6a7238ec 387 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 68:247fd6e0ab66 388 debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1);
Wolfgang Betz 34:edda6a7238ec 389 }
Wolfgang Betz 34:edda6a7238ec 390 #endif
Wolfgang Betz 34:edda6a7238ec 391
Wolfgang Betz 34:edda6a7238ec 392 disable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 393
Wolfgang Betz 34:edda6a7238ec 394 /* Reset State to Ready */
Wolfgang Betz 34:edda6a7238ec 395 set_ready_state();
Wolfgang Betz 34:edda6a7238ec 396
Wolfgang Betz 34:edda6a7238ec 397 /* Stores the RSSI value */
Wolfgang Betz 34:edda6a7238ec 398 rssi_value = qi_get_rssi_dbm();
Wolfgang Betz 34:edda6a7238ec 399
Wolfgang Betz 34:edda6a7238ec 400 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 401
Wolfgang Betz 34:edda6a7238ec 402 /* Puts the SPIRIT1 in its previous state */
Wolfgang Betz 34:edda6a7238ec 403 if(spirit_state==OFF) {
Wolfgang Betz 34:edda6a7238ec 404 off();
Wolfgang Betz 34:edda6a7238ec 405 #ifndef NDEBUG
Wolfgang Betz 34:edda6a7238ec 406 #ifdef USE_STANDBY_STATE
Wolfgang Betz 34:edda6a7238ec 407 if(SPIRIT1_STATUS() != SPIRIT1_STATE_STANDBY) {
Wolfgang Betz 34:edda6a7238ec 408 #else
Wolfgang Betz 34:edda6a7238ec 409 if(SPIRIT1_STATUS() != SPIRIT1_STATE_READY) {
Wolfgang Betz 34:edda6a7238ec 410 #endif
Wolfgang Betz 68:247fd6e0ab66 411 debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1);
Wolfgang Betz 34:edda6a7238ec 412 }
Wolfgang Betz 34:edda6a7238ec 413 #endif
Wolfgang Betz 34:edda6a7238ec 414 } else {
Wolfgang Betz 34:edda6a7238ec 415 disable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 416
Wolfgang Betz 34:edda6a7238ec 417 set_ready_state();
Wolfgang Betz 34:edda6a7238ec 418
Wolfgang Betz 34:edda6a7238ec 419 cmd_strobe(SPIRIT1_STROBE_RX);
Wolfgang Betz 34:edda6a7238ec 420 BUSYWAIT_UNTIL(SPIRIT1_STATUS() == SPIRIT1_STATE_RX, STATE_TIMEOUT);
Wolfgang Betz 34:edda6a7238ec 421 if((last_state & SPIRIT1_STATE_STATEBITS) != SPIRIT1_STATE_RX) {
Wolfgang Betz 68:247fd6e0ab66 422 error("\r\nSpirit1: (#2) failed to enter rx (%x) => retry\r\n", last_state>>1);
Wolfgang Betz 34:edda6a7238ec 423 }
Wolfgang Betz 34:edda6a7238ec 424
Wolfgang Betz 34:edda6a7238ec 425 enable_spirit_irq();
Wolfgang Betz 34:edda6a7238ec 426
Wolfgang Betz 34:edda6a7238ec 427 #ifndef NDEBUG
Wolfgang Betz 34:edda6a7238ec 428 if(SPIRIT1_STATUS() != SPIRIT1_STATE_RX) {
Wolfgang Betz 68:247fd6e0ab66 429 debug("\r\nAssert failed in: %s (%d): state=%x\r\n", __func__, __LINE__, last_state>>1);
Wolfgang Betz 34:edda6a7238ec 430 }
Wolfgang Betz 34:edda6a7238ec 431 #endif
Wolfgang Betz 34:edda6a7238ec 432 }
Wolfgang Betz 34:edda6a7238ec 433
Wolfgang Betz 34:edda6a7238ec 434 /* Checks the RSSI value with the threshold */
Wolfgang Betz 34:edda6a7238ec 435 if(rssi_value<CCA_THRESHOLD) {
Wolfgang Betz 34:edda6a7238ec 436 return 0;
Wolfgang Betz 34:edda6a7238ec 437 } else {
Wolfgang Betz 34:edda6a7238ec 438 return 1;
Wolfgang Betz 34:edda6a7238ec 439 }
Wolfgang Betz 34:edda6a7238ec 440 }
Wolfgang Betz 34:edda6a7238ec 441
Wolfgang Betz 34:edda6a7238ec 442 int SimpleSpirit1::get_pending_packet(void)
Wolfgang Betz 34:edda6a7238ec 443 {
Wolfgang Betz 34:edda6a7238ec 444 return !IS_RXBUF_EMPTY();
Wolfgang Betz 34:edda6a7238ec 445 }
Wolfgang Betz 34:edda6a7238ec 446
Wolfgang Betz 34:edda6a7238ec 447 /** Spirit Irq Callback **/
Wolfgang Betz 57:8cc871dc6cac 448 /* betzw - TODO: use threaded interrupt handling when `MBED_CONF_RTOS_PRESENT` is defined (see `atmel-rf-driver`) */
Wolfgang Betz 34:edda6a7238ec 449 void SimpleSpirit1::IrqHandler() {
Wolfgang Betz 34:edda6a7238ec 450 st_lib_spirit_irqs x_irq_status;
Wolfgang Betz 34:edda6a7238ec 451
Wolfgang Betz 34:edda6a7238ec 452 /* get interrupt source from radio */
Wolfgang Betz 34:edda6a7238ec 453 irq_get_status(&x_irq_status);
Wolfgang Betz 34:edda6a7238ec 454
Wolfgang Betz 67:93bec0baf1de 455 /* The IRQ_TX_DATA_SENT notifies the packet has been sent. Puts the SPIRIT1 in RX */
Wolfgang Betz 65:a16f0064110a 456 if(x_irq_status.IRQ_TX_DATA_SENT) { /* betzw - NOTE: MUST be handled before `IRQ_RX_DATA_READY` for Nanostack integration!
Wolfgang Betz 65:a16f0064110a 457 Logically, Nanostack only expects the "DONE" after "SUCCESS" (if it gets
Wolfgang Betz 65:a16f0064110a 458 DONE before SUCCESS, it assumes you're not going to bother to send SUCCESS).
Wolfgang Betz 65:a16f0064110a 459 */
Wolfgang Betz 34:edda6a7238ec 460 #ifdef DEBUG_IRQ
Wolfgang Betz 34:edda6a7238ec 461 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 68:247fd6e0ab66 462 debug_if(!_spirit_tx_started, "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 68:247fd6e0ab66 463 debug_if(!((*tmp) & IRQ_TX_DATA_SENT_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 68:247fd6e0ab66 464 debug_if(tx_fifo_remaining != 0, "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 34:edda6a7238ec 465 #endif
Wolfgang Betz 45:2d01cc9bc761 466
Wolfgang Betz 45:2d01cc9bc761 467 tx_fifo_buffer = NULL;
Wolfgang Betz 45:2d01cc9bc761 468 _spirit_tx_started = false;
Wolfgang Betz 45:2d01cc9bc761 469
Wolfgang Betz 45:2d01cc9bc761 470 /* call user callback */
Wolfgang Betz 45:2d01cc9bc761 471 if(_current_irq_callback) {
Wolfgang Betz 45:2d01cc9bc761 472 _current_irq_callback(TX_DONE);
Wolfgang Betz 34:edda6a7238ec 473 }
Wolfgang Betz 40:343254875d89 474
Wolfgang Betz 45:2d01cc9bc761 475 /* Disable handling of other TX flags */
Wolfgang Betz 46:d104e58c5caf 476 x_irq_status.IRQ_TX_FIFO_ALMOST_EMPTY = S_RESET;
Wolfgang Betz 45:2d01cc9bc761 477 }
Wolfgang Betz 45:2d01cc9bc761 478
Wolfgang Betz 47:3a30b960a8c2 479 #ifndef RX_FIFO_THR_WA
Wolfgang Betz 45:2d01cc9bc761 480 /* The IRQ_TX_FIFO_ALMOST_EMPTY notifies an nearly empty TX fifo */
Wolfgang Betz 45:2d01cc9bc761 481 if(x_irq_status.IRQ_TX_FIFO_ALMOST_EMPTY) {
Wolfgang Betz 45:2d01cc9bc761 482 #ifdef DEBUG_IRQ
Wolfgang Betz 45:2d01cc9bc761 483 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 68:247fd6e0ab66 484 debug_if(!((*tmp) & IRQ_TX_FIFO_ALMOST_EMPTY_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 68:247fd6e0ab66 485 debug_if(!_spirit_tx_started, "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 68:247fd6e0ab66 486 debug_if(tx_fifo_buffer == NULL, "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 45:2d01cc9bc761 487 #endif
Wolfgang Betz 45:2d01cc9bc761 488
Wolfgang Betz 45:2d01cc9bc761 489 int8_t fifo_available = SPIRIT_MAX_FIFO_LEN/2; // fill-up half fifo
Wolfgang Betz 45:2d01cc9bc761 490 int8_t to_send = (tx_fifo_remaining > fifo_available) ? fifo_available : tx_fifo_remaining;
Wolfgang Betz 45:2d01cc9bc761 491
Wolfgang Betz 45:2d01cc9bc761 492 tx_fifo_remaining -= to_send;
Wolfgang Betz 45:2d01cc9bc761 493
Wolfgang Betz 45:2d01cc9bc761 494 /* Fill FIFO Buffer */
Wolfgang Betz 45:2d01cc9bc761 495 if(to_send > 0) {
Wolfgang Betz 45:2d01cc9bc761 496 spi_write_linear_fifo(to_send, (uint8_t*)&tx_fifo_buffer[tx_buffer_pos]);
Wolfgang Betz 45:2d01cc9bc761 497 }
Wolfgang Betz 45:2d01cc9bc761 498 tx_buffer_pos += to_send;
Wolfgang Betz 34:edda6a7238ec 499 }
Wolfgang Betz 47:3a30b960a8c2 500 #endif // !RX_FIFO_THR_WA
Wolfgang Betz 34:edda6a7238ec 501
Wolfgang Betz 67:93bec0baf1de 502 /* TX FIFO underflow/overflow error */
Wolfgang Betz 34:edda6a7238ec 503 if(x_irq_status.IRQ_TX_FIFO_ERROR) {
Wolfgang Betz 34:edda6a7238ec 504 #ifdef DEBUG_IRQ
Wolfgang Betz 34:edda6a7238ec 505 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 68:247fd6e0ab66 506 debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp);
Wolfgang Betz 68:247fd6e0ab66 507 debug_if(!((*tmp) & IRQ_TX_FIFO_ERROR_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 34:edda6a7238ec 508 #endif
Wolfgang Betz 34:edda6a7238ec 509 if(_spirit_tx_started) {
Wolfgang Betz 34:edda6a7238ec 510 _spirit_tx_started = false;
Wolfgang Betz 34:edda6a7238ec 511 /* call user callback */
Wolfgang Betz 34:edda6a7238ec 512 if(_current_irq_callback) {
Wolfgang Betz 34:edda6a7238ec 513 _current_irq_callback(TX_ERR);
Wolfgang Betz 34:edda6a7238ec 514 }
Wolfgang Betz 34:edda6a7238ec 515 }
Wolfgang Betz 40:343254875d89 516
Wolfgang Betz 45:2d01cc9bc761 517 /* reset data still to be sent */
Wolfgang Betz 45:2d01cc9bc761 518 tx_fifo_remaining = 0;
Wolfgang Betz 34:edda6a7238ec 519 }
Wolfgang Betz 34:edda6a7238ec 520
Wolfgang Betz 34:edda6a7238ec 521 /* The IRQ_RX_DATA_READY notifies a new packet arrived */
Wolfgang Betz 34:edda6a7238ec 522 if(x_irq_status.IRQ_RX_DATA_READY) {
Wolfgang Betz 42:92a60a905ee7 523 #ifdef DEBUG_IRQ
Wolfgang Betz 42:92a60a905ee7 524 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 68:247fd6e0ab66 525 debug_if(!((*tmp) & IRQ_RX_DATA_READY_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 42:92a60a905ee7 526 #endif
Wolfgang Betz 34:edda6a7238ec 527
Wolfgang Betz 44:aedd63cb0ce3 528 if(!_is_receiving) { // spurious irq?!? (betzw: see comments on macro 'RX_FIFO_THR_WA'!)
Wolfgang Betz 44:aedd63cb0ce3 529 #ifdef HEAVY_DEBUG
Wolfgang Betz 68:247fd6e0ab66 530 debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp);
Wolfgang Betz 43:a512f909514a 531 #endif
Wolfgang Betz 43:a512f909514a 532 } else {
Wolfgang Betz 43:a512f909514a 533 _is_receiving = false; // Finished receiving
Wolfgang Betz 43:a512f909514a 534 stop_rx_timeout();
Wolfgang Betz 43:a512f909514a 535
Wolfgang Betz 43:a512f909514a 536 spirit_rx_len = pkt_basic_get_received_pkt_length();
Wolfgang Betz 34:edda6a7238ec 537
Wolfgang Betz 34:edda6a7238ec 538 #ifdef DEBUG_IRQ
Wolfgang Betz 68:247fd6e0ab66 539 debug_if(!(spirit_rx_len <= MAX_PACKET_LEN), "\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp);
Wolfgang Betz 34:edda6a7238ec 540 #endif
Wolfgang Betz 34:edda6a7238ec 541
Wolfgang Betz 48:373e1b7e424f 542 if(spirit_rx_len <= MAX_PACKET_LEN) {
Wolfgang Betz 43:a512f909514a 543 uint8_t to_receive = spirit_rx_len - _spirit_rx_pos;
Wolfgang Betz 43:a512f909514a 544 if(to_receive > 0) {
Wolfgang Betz 43:a512f909514a 545 spi_read_linear_fifo(to_receive, &spirit_rx_buf[_spirit_rx_pos]);
Wolfgang Betz 43:a512f909514a 546 _spirit_rx_pos += to_receive;
Wolfgang Betz 43:a512f909514a 547 }
Wolfgang Betz 34:edda6a7238ec 548 }
Wolfgang Betz 43:a512f909514a 549
Wolfgang Betz 43:a512f909514a 550 cmd_strobe(SPIRIT1_STROBE_FRX);
Wolfgang Betz 43:a512f909514a 551
Wolfgang Betz 43:a512f909514a 552 last_rssi = qi_get_rssi(); //MGR
Wolfgang Betz 43:a512f909514a 553 last_sqi = qi_get_sqi(); //MGR
Wolfgang Betz 43:a512f909514a 554
Wolfgang Betz 43:a512f909514a 555 /* call user callback */
Wolfgang Betz 48:373e1b7e424f 556 if((_spirit_rx_pos == spirit_rx_len) && _current_irq_callback) {
Wolfgang Betz 43:a512f909514a 557 _current_irq_callback(RX_DONE);
Wolfgang Betz 43:a512f909514a 558 }
Wolfgang Betz 43:a512f909514a 559
Wolfgang Betz 43:a512f909514a 560 /* Disable handling of other RX flags */
Wolfgang Betz 46:d104e58c5caf 561 x_irq_status.IRQ_RX_FIFO_ALMOST_FULL = S_RESET;
Wolfgang Betz 40:343254875d89 562 }
Wolfgang Betz 43:a512f909514a 563 }
Wolfgang Betz 34:edda6a7238ec 564
Wolfgang Betz 47:3a30b960a8c2 565 #ifndef RX_FIFO_THR_WA
Wolfgang Betz 43:a512f909514a 566 /* RX FIFO almost full */
Wolfgang Betz 43:a512f909514a 567 if(x_irq_status.IRQ_RX_FIFO_ALMOST_FULL) {
Wolfgang Betz 43:a512f909514a 568 #ifdef DEBUG_IRQ
Wolfgang Betz 43:a512f909514a 569 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 68:247fd6e0ab66 570 debug_if(!((*tmp) & IRQ_RX_FIFO_ALMOST_FULL_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 43:a512f909514a 571 #endif
Wolfgang Betz 43:a512f909514a 572 if(!_is_receiving) { // spurious irq?!?
Wolfgang Betz 43:a512f909514a 573 #ifdef DEBUG_IRQ
Wolfgang Betz 68:247fd6e0ab66 574 debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp);
Wolfgang Betz 43:a512f909514a 575 #endif
Wolfgang Betz 43:a512f909514a 576 } else {
Wolfgang Betz 43:a512f909514a 577 uint8_t fifo_available = linear_fifo_read_num_elements_rx_fifo();
Wolfgang Betz 48:373e1b7e424f 578 if((fifo_available + _spirit_rx_pos) <= MAX_PACKET_LEN) {
Wolfgang Betz 48:373e1b7e424f 579 spi_read_linear_fifo(fifo_available, &spirit_rx_buf[_spirit_rx_pos]);
Wolfgang Betz 48:373e1b7e424f 580 _spirit_rx_pos += fifo_available;
Wolfgang Betz 48:373e1b7e424f 581 } else {
Wolfgang Betz 48:373e1b7e424f 582 #ifdef DEBUG_IRQ
Wolfgang Betz 68:247fd6e0ab66 583 debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp);
Wolfgang Betz 48:373e1b7e424f 584 #endif
Wolfgang Betz 48:373e1b7e424f 585 }
Wolfgang Betz 45:2d01cc9bc761 586 }
Wolfgang Betz 45:2d01cc9bc761 587 }
Wolfgang Betz 47:3a30b960a8c2 588 #endif // !RX_FIFO_THR_WA
Wolfgang Betz 43:a512f909514a 589
Wolfgang Betz 45:2d01cc9bc761 590 /* Reception errors */
Wolfgang Betz 45:2d01cc9bc761 591 if((x_irq_status.IRQ_RX_FIFO_ERROR) || (x_irq_status.IRQ_RX_DATA_DISC)) {
Wolfgang Betz 45:2d01cc9bc761 592 #ifdef DEBUG_IRQ
Wolfgang Betz 45:2d01cc9bc761 593 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 68:247fd6e0ab66 594 debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp);
Wolfgang Betz 68:247fd6e0ab66 595 debug_if(!((*tmp) & (IRQ_RX_FIFO_ERROR_MASK | IRQ_RX_DATA_DISC_MASK)), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 45:2d01cc9bc761 596 #endif
Wolfgang Betz 45:2d01cc9bc761 597 rx_timeout_handler();
Wolfgang Betz 45:2d01cc9bc761 598 if(_spirit_tx_started) {
Wolfgang Betz 45:2d01cc9bc761 599 _spirit_tx_started = false;
Wolfgang Betz 45:2d01cc9bc761 600 /* call user callback */
Wolfgang Betz 45:2d01cc9bc761 601 if(_current_irq_callback) {
Wolfgang Betz 45:2d01cc9bc761 602 _current_irq_callback(TX_ERR);
Wolfgang Betz 45:2d01cc9bc761 603 }
Wolfgang Betz 34:edda6a7238ec 604 }
Wolfgang Betz 34:edda6a7238ec 605 }
Wolfgang Betz 34:edda6a7238ec 606
Wolfgang Betz 34:edda6a7238ec 607 /* The IRQ_VALID_SYNC is used to notify a new packet is coming */
Wolfgang Betz 34:edda6a7238ec 608 if(x_irq_status.IRQ_VALID_SYNC) {
Wolfgang Betz 42:92a60a905ee7 609 #ifdef DEBUG_IRQ
Wolfgang Betz 42:92a60a905ee7 610 uint32_t *tmp = (uint32_t*)&x_irq_status;
Wolfgang Betz 68:247fd6e0ab66 611 debug_if(!((*tmp) & IRQ_VALID_SYNC_MASK), "\r\nAssert failed in: %s (%d)\r\n", __func__, __LINE__);
Wolfgang Betz 42:92a60a905ee7 612 #endif
Wolfgang Betz 39:75481c4c6655 613 /* betzw - NOTE: there is a race condition between Spirit1 receiving packets and
Wolfgang Betz 39:75481c4c6655 614 * the MCU trying to send a packet, which gets resolved in favor of
Wolfgang Betz 39:75481c4c6655 615 * sending.
Wolfgang Betz 39:75481c4c6655 616 */
Wolfgang Betz 39:75481c4c6655 617 if(_spirit_tx_started) {
Wolfgang Betz 34:edda6a7238ec 618 #ifdef DEBUG_IRQ
Wolfgang Betz 68:247fd6e0ab66 619 debug("\r\n%s (%d): irq=%x\r\n", __func__, __LINE__, *tmp);
Wolfgang Betz 34:edda6a7238ec 620 #endif
Wolfgang Betz 39:75481c4c6655 621 } else {
Wolfgang Betz 39:75481c4c6655 622 _is_receiving = true;
Wolfgang Betz 39:75481c4c6655 623 start_rx_timeout();
Wolfgang Betz 39:75481c4c6655 624 }
Wolfgang Betz 34:edda6a7238ec 625 }
Wolfgang Betz 34:edda6a7238ec 626 }