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