Added support for WNC M14A2A Cellular LTE Data Module.
Dependencies: WNC14A2AInterface
Dependents: http-example-wnc http-example-wnc-modified
stm-spirit1-rf-driver/stm-spirit1-rf-driver/SimpleSpirit1.h@5:391eac6a0a94, 2017-04-23 (annotated)
- Committer:
- root@developer-sjc-cyan-compiler.local.mbed.org
- Date:
- Sun Apr 23 18:40:51 2017 +0000
- Revision:
- 5:391eac6a0a94
- Parent:
- 0:2563b0415d1f
Added tag att_cellular_K64_wnc_14A2A_20170423 for changeset daf182af022b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:2563b0415d1f | 1 | /*** Mbed Includes ***/ |
JMF | 0:2563b0415d1f | 2 | #include "mbed.h" |
JMF | 0:2563b0415d1f | 3 | #include "mbed_debug.h" |
JMF | 0:2563b0415d1f | 4 | |
JMF | 0:2563b0415d1f | 5 | |
JMF | 0:2563b0415d1f | 6 | /*** Cube Includes ***/ |
JMF | 0:2563b0415d1f | 7 | #include "SPIRIT_Radio.h" |
JMF | 0:2563b0415d1f | 8 | #include "SPIRIT_Management.h" |
JMF | 0:2563b0415d1f | 9 | #include "SPIRIT_Commands.h" |
JMF | 0:2563b0415d1f | 10 | #include "MCU_Interface.h" |
JMF | 0:2563b0415d1f | 11 | |
JMF | 0:2563b0415d1f | 12 | |
JMF | 0:2563b0415d1f | 13 | /*** Contiki Lib Includes ***/ |
JMF | 0:2563b0415d1f | 14 | #include "spirit1.h" |
JMF | 0:2563b0415d1f | 15 | #include "spirit1-config.h" |
JMF | 0:2563b0415d1f | 16 | #include "spirit1-const.h" |
JMF | 0:2563b0415d1f | 17 | |
JMF | 0:2563b0415d1f | 18 | |
JMF | 0:2563b0415d1f | 19 | // betzw: enable beyond macro if you want debug messages also from IRQ handler |
JMF | 0:2563b0415d1f | 20 | // #define DEBUG_IRQ |
JMF | 0:2563b0415d1f | 21 | |
JMF | 0:2563b0415d1f | 22 | |
JMF | 0:2563b0415d1f | 23 | /*** Macros from Cube Implementation ***/ |
JMF | 0:2563b0415d1f | 24 | #define CLEAR_TXBUF() (spirit_tx_len = 0) |
JMF | 0:2563b0415d1f | 25 | #define IS_RXBUF_EMPTY() (spirit_rx_len == 0) |
JMF | 0:2563b0415d1f | 26 | #define CLEAR_RXBUF() do { \ |
JMF | 0:2563b0415d1f | 27 | spirit_rx_len = 0; \ |
JMF | 0:2563b0415d1f | 28 | _spirit_rx_pos = 0; \ |
JMF | 0:2563b0415d1f | 29 | } while(0) |
JMF | 0:2563b0415d1f | 30 | |
JMF | 0:2563b0415d1f | 31 | |
JMF | 0:2563b0415d1f | 32 | /*** Macros from Cube Implementation ***/ |
JMF | 0:2563b0415d1f | 33 | /* transceiver state. */ |
JMF | 0:2563b0415d1f | 34 | #define ON 0 |
JMF | 0:2563b0415d1f | 35 | #define OFF 1 |
JMF | 0:2563b0415d1f | 36 | |
JMF | 0:2563b0415d1f | 37 | |
JMF | 0:2563b0415d1f | 38 | /*** Missing Cube External Declarations ***/ |
JMF | 0:2563b0415d1f | 39 | extern "C" void SpiritManagementSetFrequencyBase(uint32_t); |
JMF | 0:2563b0415d1f | 40 | |
JMF | 0:2563b0415d1f | 41 | |
JMF | 0:2563b0415d1f | 42 | /*** UnlockedSPI for Performance (due to singleton) ***/ |
JMF | 0:2563b0415d1f | 43 | class UnlockedSPI : public SPI { |
JMF | 0:2563b0415d1f | 44 | public: |
JMF | 0:2563b0415d1f | 45 | UnlockedSPI(PinName mosi, PinName miso, PinName sclk) : |
JMF | 0:2563b0415d1f | 46 | SPI(mosi, miso, sclk) { } |
JMF | 0:2563b0415d1f | 47 | virtual ~UnlockedSPI() {} |
JMF | 0:2563b0415d1f | 48 | virtual void lock() { } |
JMF | 0:2563b0415d1f | 49 | virtual void unlock() { } |
JMF | 0:2563b0415d1f | 50 | }; |
JMF | 0:2563b0415d1f | 51 | |
JMF | 0:2563b0415d1f | 52 | |
JMF | 0:2563b0415d1f | 53 | /*** A Simple Spirit1 Class ***/ |
JMF | 0:2563b0415d1f | 54 | // NOTE: must be a singleton (due to mix of MBED/CUBE code)!!! |
JMF | 0:2563b0415d1f | 55 | // NOTE: implementation is IRQ-save but (intentionally) NOT thread-safe!!! |
JMF | 0:2563b0415d1f | 56 | class SimpleSpirit1 { |
JMF | 0:2563b0415d1f | 57 | protected: |
JMF | 0:2563b0415d1f | 58 | static SimpleSpirit1 *_singleton; |
JMF | 0:2563b0415d1f | 59 | |
JMF | 0:2563b0415d1f | 60 | /** Communication Interface Instance Variables **/ |
JMF | 0:2563b0415d1f | 61 | UnlockedSPI _spi; // betzw - NOTE: Morpho/Zio pins are valid only for NUCLEO-F401RE |
JMF | 0:2563b0415d1f | 62 | // mosi: PA_7 (D11) |
JMF | 0:2563b0415d1f | 63 | // miso: PA_6 (D12) |
JMF | 0:2563b0415d1f | 64 | // sclk: PB_3 (D3) or |
JMF | 0:2563b0415d1f | 65 | // PA_5 (D13) (only in case you unmount R4 & mount R7, |
JMF | 0:2563b0415d1f | 66 | // (note: in this case you may not use LED1 on some platforms) |
JMF | 0:2563b0415d1f | 67 | // bits: 8-bit |
JMF | 0:2563b0415d1f | 68 | // mode: 0 |
JMF | 0:2563b0415d1f | 69 | // ordr: MSB |
JMF | 0:2563b0415d1f | 70 | // freq: max 10MHz |
JMF | 0:2563b0415d1f | 71 | InterruptIn _irq; // PC_7 (D9) (falling) |
JMF | 0:2563b0415d1f | 72 | DigitalOut _chip_select; // PB_6 (D10) ('1' == chip unselected) |
JMF | 0:2563b0415d1f | 73 | DigitalOut _shut_down; // PA_10 (D2) ('1' == shut_down) |
JMF | 0:2563b0415d1f | 74 | DigitalOut _led; // PB_4 (D5) (optional) |
JMF | 0:2563b0415d1f | 75 | |
JMF | 0:2563b0415d1f | 76 | Callback<void(int)> _current_irq_callback; |
JMF | 0:2563b0415d1f | 77 | Timeout _rx_receiving_timeout; |
JMF | 0:2563b0415d1f | 78 | |
JMF | 0:2563b0415d1f | 79 | void rx_timeout_handler(void) { |
JMF | 0:2563b0415d1f | 80 | set_ready_state(); |
JMF | 0:2563b0415d1f | 81 | cmd_strobe(SPIRIT1_STROBE_RX); |
JMF | 0:2563b0415d1f | 82 | #ifdef DEBUG_IRQ |
JMF | 0:2563b0415d1f | 83 | debug("\n\r%s (%d)\n\r", __func__, __LINE__); |
JMF | 0:2563b0415d1f | 84 | #endif |
JMF | 0:2563b0415d1f | 85 | } |
JMF | 0:2563b0415d1f | 86 | |
JMF | 0:2563b0415d1f | 87 | void start_rx_timeout(void) { |
JMF | 0:2563b0415d1f | 88 | _rx_receiving_timeout.attach_us(Callback<void()>(this, &SimpleSpirit1::rx_timeout_handler), 100 * 1000); // 100ms |
JMF | 0:2563b0415d1f | 89 | } |
JMF | 0:2563b0415d1f | 90 | |
JMF | 0:2563b0415d1f | 91 | void stop_rx_timeout(void) { |
JMF | 0:2563b0415d1f | 92 | _rx_receiving_timeout.detach(); |
JMF | 0:2563b0415d1f | 93 | } |
JMF | 0:2563b0415d1f | 94 | |
JMF | 0:2563b0415d1f | 95 | /** Static Variables from Cube Implementation **/ |
JMF | 0:2563b0415d1f | 96 | /* |
JMF | 0:2563b0415d1f | 97 | * The buffers which hold incoming data. |
JMF | 0:2563b0415d1f | 98 | * The +1 because of the first byte, |
JMF | 0:2563b0415d1f | 99 | * which will contain the length of the packet. |
JMF | 0:2563b0415d1f | 100 | */ |
JMF | 0:2563b0415d1f | 101 | volatile uint16_t spirit_tx_len; |
JMF | 0:2563b0415d1f | 102 | volatile bool _spirit_tx_started; |
JMF | 0:2563b0415d1f | 103 | volatile uint16_t spirit_rx_len; |
JMF | 0:2563b0415d1f | 104 | volatile uint16_t _spirit_rx_pos; |
JMF | 0:2563b0415d1f | 105 | volatile bool _spirit_rx_err; |
JMF | 0:2563b0415d1f | 106 | uint8_t spirit_rx_buf[MAX_PACKET_LEN]; |
JMF | 0:2563b0415d1f | 107 | volatile bool _is_receiving; |
JMF | 0:2563b0415d1f | 108 | |
JMF | 0:2563b0415d1f | 109 | /** Status Variables from Cube Implementation **/ |
JMF | 0:2563b0415d1f | 110 | unsigned int spirit_on; |
JMF | 0:2563b0415d1f | 111 | uint8_t last_rssi; //MGR |
JMF | 0:2563b0415d1f | 112 | uint8_t last_sqi; //MGR |
JMF | 0:2563b0415d1f | 113 | |
JMF | 0:2563b0415d1f | 114 | /** Low Level Instance Variables **/ |
JMF | 0:2563b0415d1f | 115 | unsigned int _nr_of_irq_disables; |
JMF | 0:2563b0415d1f | 116 | |
JMF | 0:2563b0415d1f | 117 | /** Low Level Instance Methods **/ |
JMF | 0:2563b0415d1f | 118 | void disable_spirit_irq(void) { |
JMF | 0:2563b0415d1f | 119 | _irq.disable_irq(); |
JMF | 0:2563b0415d1f | 120 | _nr_of_irq_disables++; |
JMF | 0:2563b0415d1f | 121 | #ifndef NDEBUG |
JMF | 0:2563b0415d1f | 122 | debug_if(_nr_of_irq_disables == 0, "\n\rassert failed in: %s (%d)\n\r", __func__, __LINE__); |
JMF | 0:2563b0415d1f | 123 | #endif |
JMF | 0:2563b0415d1f | 124 | } |
JMF | 0:2563b0415d1f | 125 | |
JMF | 0:2563b0415d1f | 126 | void enable_spirit_irq(void) { |
JMF | 0:2563b0415d1f | 127 | #ifndef NDEBUG |
JMF | 0:2563b0415d1f | 128 | debug_if(_nr_of_irq_disables == 0, "\n\rassert failed in: %s (%d)\n\r", __func__, __LINE__); |
JMF | 0:2563b0415d1f | 129 | #endif |
JMF | 0:2563b0415d1f | 130 | if(--_nr_of_irq_disables == 0) |
JMF | 0:2563b0415d1f | 131 | _irq.enable_irq(); |
JMF | 0:2563b0415d1f | 132 | } |
JMF | 0:2563b0415d1f | 133 | |
JMF | 0:2563b0415d1f | 134 | void chip_select() { _chip_select = 0; } |
JMF | 0:2563b0415d1f | 135 | void chip_unselect() { _chip_select = 1; } |
JMF | 0:2563b0415d1f | 136 | |
JMF | 0:2563b0415d1f | 137 | void enter_shutdown() { |
JMF | 0:2563b0415d1f | 138 | _shut_down = 1; |
JMF | 0:2563b0415d1f | 139 | wait_ms(5); // wait 5 milliseconds (to allow Spirit1 to shut down) |
JMF | 0:2563b0415d1f | 140 | } |
JMF | 0:2563b0415d1f | 141 | |
JMF | 0:2563b0415d1f | 142 | void exit_shutdown() { |
JMF | 0:2563b0415d1f | 143 | _shut_down = 0; |
JMF | 0:2563b0415d1f | 144 | wait_ms(10); // wait 10 milliseconds (to allow Spirit1 a proper boot-up sequence) |
JMF | 0:2563b0415d1f | 145 | } |
JMF | 0:2563b0415d1f | 146 | |
JMF | 0:2563b0415d1f | 147 | void cs_to_sclk_delay(void) { |
JMF | 0:2563b0415d1f | 148 | wait_us(1); // heuristic value |
JMF | 0:2563b0415d1f | 149 | } |
JMF | 0:2563b0415d1f | 150 | |
JMF | 0:2563b0415d1f | 151 | /** |
JMF | 0:2563b0415d1f | 152 | * @brief Write and read a buffer to/from the SPI peripheral device at the same time |
JMF | 0:2563b0415d1f | 153 | * in 8-bit data mode using synchronous SPI communication. |
JMF | 0:2563b0415d1f | 154 | * @param[in] pBufferToWrite pointer to the buffer of data to send. |
JMF | 0:2563b0415d1f | 155 | * @param[out] pBufferToRead pointer to the buffer to read data into. |
JMF | 0:2563b0415d1f | 156 | * @param[in] NumBytes number of bytes to read and write. |
JMF | 0:2563b0415d1f | 157 | * @retval 0 if ok. |
JMF | 0:2563b0415d1f | 158 | * @retval -1 if data format error. |
JMF | 0:2563b0415d1f | 159 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
JMF | 0:2563b0415d1f | 160 | * before calling this function and to enable them again after. |
JMF | 0:2563b0415d1f | 161 | */ |
JMF | 0:2563b0415d1f | 162 | void spi_write_read(uint8_t* pBufferToWrite, uint8_t* pBufferToRead, uint16_t NumBytes) |
JMF | 0:2563b0415d1f | 163 | { |
JMF | 0:2563b0415d1f | 164 | /* Read and write data at the same time. */ |
JMF | 0:2563b0415d1f | 165 | for (int i = 0; i < NumBytes; i++) { |
JMF | 0:2563b0415d1f | 166 | pBufferToRead[i] = _spi.write(pBufferToWrite[i]); |
JMF | 0:2563b0415d1f | 167 | } |
JMF | 0:2563b0415d1f | 168 | } |
JMF | 0:2563b0415d1f | 169 | |
JMF | 0:2563b0415d1f | 170 | /** Radio Instance Methods **/ |
JMF | 0:2563b0415d1f | 171 | void radio_set_xtal_freq(uint32_t freq) { |
JMF | 0:2563b0415d1f | 172 | SpiritRadioSetXtalFrequency(freq); |
JMF | 0:2563b0415d1f | 173 | } |
JMF | 0:2563b0415d1f | 174 | |
JMF | 0:2563b0415d1f | 175 | void radio_set_pa_level_dbm(uint8_t cIndex, float fPowerdBm) { |
JMF | 0:2563b0415d1f | 176 | SpiritRadioSetPALeveldBm(cIndex, fPowerdBm); |
JMF | 0:2563b0415d1f | 177 | } |
JMF | 0:2563b0415d1f | 178 | |
JMF | 0:2563b0415d1f | 179 | void radio_set_pa_level_max_index(uint8_t cIndex) { |
JMF | 0:2563b0415d1f | 180 | SpiritRadioSetPALevelMaxIndex(cIndex); |
JMF | 0:2563b0415d1f | 181 | } |
JMF | 0:2563b0415d1f | 182 | |
JMF | 0:2563b0415d1f | 183 | uint8_t radio_init(SRadioInit *init_struct) { |
JMF | 0:2563b0415d1f | 184 | return SpiritRadioInit(init_struct); |
JMF | 0:2563b0415d1f | 185 | } |
JMF | 0:2563b0415d1f | 186 | |
JMF | 0:2563b0415d1f | 187 | void radio_persistent_rx(SpiritFunctionalState xNewState) { |
JMF | 0:2563b0415d1f | 188 | SpiritRadioPersistenRx(xNewState); |
JMF | 0:2563b0415d1f | 189 | } |
JMF | 0:2563b0415d1f | 190 | |
JMF | 0:2563b0415d1f | 191 | void radio_afc_freeze_on_sync(SpiritFunctionalState xNewState) { |
JMF | 0:2563b0415d1f | 192 | SpiritRadioAFCFreezeOnSync(xNewState); |
JMF | 0:2563b0415d1f | 193 | } |
JMF | 0:2563b0415d1f | 194 | |
JMF | 0:2563b0415d1f | 195 | /** Packet System Instance Methods **/ |
JMF | 0:2563b0415d1f | 196 | void pkt_basic_init(PktBasicInit* pxPktBasicInit) { |
JMF | 0:2563b0415d1f | 197 | SpiritPktBasicInit(pxPktBasicInit); |
JMF | 0:2563b0415d1f | 198 | } |
JMF | 0:2563b0415d1f | 199 | |
JMF | 0:2563b0415d1f | 200 | void pkt_basic_set_payload_length(uint16_t nPayloadLength) { |
JMF | 0:2563b0415d1f | 201 | SpiritPktBasicSetPayloadLength(nPayloadLength); |
JMF | 0:2563b0415d1f | 202 | } |
JMF | 0:2563b0415d1f | 203 | |
JMF | 0:2563b0415d1f | 204 | uint16_t pkt_basic_get_received_pkt_length(void) { |
JMF | 0:2563b0415d1f | 205 | return SpiritPktBasicGetReceivedPktLength(); |
JMF | 0:2563b0415d1f | 206 | } |
JMF | 0:2563b0415d1f | 207 | |
JMF | 0:2563b0415d1f | 208 | /** IRQ Instance Methods **/ |
JMF | 0:2563b0415d1f | 209 | void irq_de_init(SpiritIrqs* pxIrqInit) { |
JMF | 0:2563b0415d1f | 210 | SpiritIrqDeInit(pxIrqInit); |
JMF | 0:2563b0415d1f | 211 | } |
JMF | 0:2563b0415d1f | 212 | |
JMF | 0:2563b0415d1f | 213 | void irq_clear_status(void) { |
JMF | 0:2563b0415d1f | 214 | SpiritIrqClearStatus(); |
JMF | 0:2563b0415d1f | 215 | } |
JMF | 0:2563b0415d1f | 216 | |
JMF | 0:2563b0415d1f | 217 | void irq_set_status(IrqList xIrq, SpiritFunctionalState xNewState) { |
JMF | 0:2563b0415d1f | 218 | SpiritIrq(xIrq, xNewState); |
JMF | 0:2563b0415d1f | 219 | } |
JMF | 0:2563b0415d1f | 220 | |
JMF | 0:2563b0415d1f | 221 | void irq_get_status(SpiritIrqs* pxIrqStatus) { |
JMF | 0:2563b0415d1f | 222 | SpiritIrqGetStatus(pxIrqStatus); |
JMF | 0:2563b0415d1f | 223 | } |
JMF | 0:2563b0415d1f | 224 | |
JMF | 0:2563b0415d1f | 225 | /** Management Instance Methods **/ |
JMF | 0:2563b0415d1f | 226 | void mgmt_set_freq_base(uint32_t freq) { |
JMF | 0:2563b0415d1f | 227 | SpiritManagementSetFrequencyBase(freq); |
JMF | 0:2563b0415d1f | 228 | } |
JMF | 0:2563b0415d1f | 229 | |
JMF | 0:2563b0415d1f | 230 | void mgmt_refresh_status(void) { |
JMF | 0:2563b0415d1f | 231 | SpiritRefreshStatus(); |
JMF | 0:2563b0415d1f | 232 | } |
JMF | 0:2563b0415d1f | 233 | |
JMF | 0:2563b0415d1f | 234 | /** Spirit GPIO Instance Methods **/ |
JMF | 0:2563b0415d1f | 235 | void spirit_gpio_init(SGpioInit* pxGpioInitStruct) { |
JMF | 0:2563b0415d1f | 236 | SpiritGpioInit(pxGpioInitStruct); |
JMF | 0:2563b0415d1f | 237 | } |
JMF | 0:2563b0415d1f | 238 | |
JMF | 0:2563b0415d1f | 239 | /** Qi Instance Methods **/ |
JMF | 0:2563b0415d1f | 240 | void qi_set_sqi_threshold(SqiThreshold xSqiThr) { |
JMF | 0:2563b0415d1f | 241 | SpiritQiSetSqiThreshold(xSqiThr); |
JMF | 0:2563b0415d1f | 242 | } |
JMF | 0:2563b0415d1f | 243 | |
JMF | 0:2563b0415d1f | 244 | void qi_sqi_check(SpiritFunctionalState xNewState) { |
JMF | 0:2563b0415d1f | 245 | SpiritQiSqiCheck(xNewState); |
JMF | 0:2563b0415d1f | 246 | } |
JMF | 0:2563b0415d1f | 247 | |
JMF | 0:2563b0415d1f | 248 | void qi_set_rssi_threshold_dbm(int nDbmValue) { |
JMF | 0:2563b0415d1f | 249 | SpiritQiSetRssiThresholddBm(nDbmValue); |
JMF | 0:2563b0415d1f | 250 | } |
JMF | 0:2563b0415d1f | 251 | |
JMF | 0:2563b0415d1f | 252 | float qi_get_rssi_dbm() { |
JMF | 0:2563b0415d1f | 253 | last_rssi = qi_get_rssi(); |
JMF | 0:2563b0415d1f | 254 | return get_last_rssi_dbm(); |
JMF | 0:2563b0415d1f | 255 | } |
JMF | 0:2563b0415d1f | 256 | |
JMF | 0:2563b0415d1f | 257 | uint8_t qi_get_rssi() { |
JMF | 0:2563b0415d1f | 258 | return SpiritQiGetRssi(); |
JMF | 0:2563b0415d1f | 259 | } |
JMF | 0:2563b0415d1f | 260 | |
JMF | 0:2563b0415d1f | 261 | uint8_t qi_get_sqi() { |
JMF | 0:2563b0415d1f | 262 | return SpiritQiGetSqi(); |
JMF | 0:2563b0415d1f | 263 | } |
JMF | 0:2563b0415d1f | 264 | |
JMF | 0:2563b0415d1f | 265 | /** Timer Instance Methods **/ |
JMF | 0:2563b0415d1f | 266 | void timer_set_rx_timeout_stop_condition(RxTimeoutStopCondition xStopCondition) { |
JMF | 0:2563b0415d1f | 267 | SpiritTimerSetRxTimeoutStopCondition(xStopCondition); |
JMF | 0:2563b0415d1f | 268 | } |
JMF | 0:2563b0415d1f | 269 | |
JMF | 0:2563b0415d1f | 270 | void timer_set_rx_timeout_counter(uint8_t cCounter) { |
JMF | 0:2563b0415d1f | 271 | SpiritTimerSetRxTimeoutCounter(cCounter); |
JMF | 0:2563b0415d1f | 272 | } |
JMF | 0:2563b0415d1f | 273 | |
JMF | 0:2563b0415d1f | 274 | void timer_set_infinite_rx_timeout(void) { |
JMF | 0:2563b0415d1f | 275 | timer_set_rx_timeout_counter(0); |
JMF | 0:2563b0415d1f | 276 | } |
JMF | 0:2563b0415d1f | 277 | |
JMF | 0:2563b0415d1f | 278 | /** CSMA/CA Instance Methods **/ |
JMF | 0:2563b0415d1f | 279 | void csma_ca_state(SpiritFunctionalState xNewState) { |
JMF | 0:2563b0415d1f | 280 | SpiritCsma(xNewState); |
JMF | 0:2563b0415d1f | 281 | } |
JMF | 0:2563b0415d1f | 282 | |
JMF | 0:2563b0415d1f | 283 | void csma_ca_init(CsmaInit* pxCsmaInit) { |
JMF | 0:2563b0415d1f | 284 | csma_ca_state(S_DISABLE); // Disabled at init |
JMF | 0:2563b0415d1f | 285 | SpiritCsmaInit(pxCsmaInit); |
JMF | 0:2563b0415d1f | 286 | SpiritCsmaSeedReloadMode(S_DISABLE); // always disable seed reload |
JMF | 0:2563b0415d1f | 287 | } |
JMF | 0:2563b0415d1f | 288 | |
JMF | 0:2563b0415d1f | 289 | /** Command Instance Methods**/ |
JMF | 0:2563b0415d1f | 290 | void cmd_strobe(uint8_t cmd) { |
JMF | 0:2563b0415d1f | 291 | SpiritCmdStrobeCommand((SpiritCmd)cmd); |
JMF | 0:2563b0415d1f | 292 | } |
JMF | 0:2563b0415d1f | 293 | |
JMF | 0:2563b0415d1f | 294 | void cmd_strobe_flush_rx_fifo() { |
JMF | 0:2563b0415d1f | 295 | SpiritCmdStrobeCommand(CMD_FLUSHRXFIFO); |
JMF | 0:2563b0415d1f | 296 | } |
JMF | 0:2563b0415d1f | 297 | |
JMF | 0:2563b0415d1f | 298 | /** SPI Instance Methods **/ |
JMF | 0:2563b0415d1f | 299 | StatusBytes spi_write_linear_fifo(uint8_t cNbBytes, uint8_t* pcBuffer) { |
JMF | 0:2563b0415d1f | 300 | return SdkEvalSpiWriteFifo(cNbBytes, pcBuffer); |
JMF | 0:2563b0415d1f | 301 | } |
JMF | 0:2563b0415d1f | 302 | |
JMF | 0:2563b0415d1f | 303 | StatusBytes spi_read_linear_fifo(uint8_t cNbBytes, uint8_t* pcBuffer) { |
JMF | 0:2563b0415d1f | 304 | return SdkEvalSpiReadFifo(cNbBytes, pcBuffer); |
JMF | 0:2563b0415d1f | 305 | } |
JMF | 0:2563b0415d1f | 306 | |
JMF | 0:2563b0415d1f | 307 | /** Linear FIFO Instance Methods **/ |
JMF | 0:2563b0415d1f | 308 | uint8_t linear_fifo_read_num_elements_rx_fifo(void) { |
JMF | 0:2563b0415d1f | 309 | return SpiritLinearFifoReadNumElementsRxFifo(); |
JMF | 0:2563b0415d1f | 310 | } |
JMF | 0:2563b0415d1f | 311 | |
JMF | 0:2563b0415d1f | 312 | uint8_t linear_fifo_read_num_elements_tx_fifo(void) { |
JMF | 0:2563b0415d1f | 313 | return SpiritLinearFifoReadNumElementsTxFifo(); |
JMF | 0:2563b0415d1f | 314 | } |
JMF | 0:2563b0415d1f | 315 | |
JMF | 0:2563b0415d1f | 316 | void linear_fifo_set_almost_full_thr_rx(uint8_t cThrRxFifo) { |
JMF | 0:2563b0415d1f | 317 | SpiritLinearFifoSetAlmostFullThresholdRx(cThrRxFifo); |
JMF | 0:2563b0415d1f | 318 | } |
JMF | 0:2563b0415d1f | 319 | |
JMF | 0:2563b0415d1f | 320 | /** Calibration Instance Methods **/ |
JMF | 0:2563b0415d1f | 321 | void calibration_rco(SpiritFunctionalState xNewState) { |
JMF | 0:2563b0415d1f | 322 | SpiritCalibrationRco(xNewState); |
JMF | 0:2563b0415d1f | 323 | } |
JMF | 0:2563b0415d1f | 324 | |
JMF | 0:2563b0415d1f | 325 | /** Internal Spirit Methods */ |
JMF | 0:2563b0415d1f | 326 | void set_ready_state(void); |
JMF | 0:2563b0415d1f | 327 | uint8_t refresh_state(void); |
JMF | 0:2563b0415d1f | 328 | |
JMF | 0:2563b0415d1f | 329 | /** Friend Functions **/ |
JMF | 0:2563b0415d1f | 330 | friend StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer); |
JMF | 0:2563b0415d1f | 331 | friend StatusBytes SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer); |
JMF | 0:2563b0415d1f | 332 | friend StatusBytes SdkEvalSpiCommandStrobes(uint8_t cCommandCode); |
JMF | 0:2563b0415d1f | 333 | friend StatusBytes SdkEvalSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer); |
JMF | 0:2563b0415d1f | 334 | friend StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer); |
JMF | 0:2563b0415d1f | 335 | |
JMF | 0:2563b0415d1f | 336 | /** Sdk Instance Methods **/ |
JMF | 0:2563b0415d1f | 337 | StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer); |
JMF | 0:2563b0415d1f | 338 | StatusBytes SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer); |
JMF | 0:2563b0415d1f | 339 | StatusBytes SdkEvalSpiCommandStrobes(uint8_t cCommandCode); |
JMF | 0:2563b0415d1f | 340 | StatusBytes SdkEvalSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer); |
JMF | 0:2563b0415d1f | 341 | StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer); |
JMF | 0:2563b0415d1f | 342 | |
JMF | 0:2563b0415d1f | 343 | /** Helper Instance Methods **/ |
JMF | 0:2563b0415d1f | 344 | void chip_sync_select() { |
JMF | 0:2563b0415d1f | 345 | disable_spirit_irq(); |
JMF | 0:2563b0415d1f | 346 | chip_select(); |
JMF | 0:2563b0415d1f | 347 | cs_to_sclk_delay(); |
JMF | 0:2563b0415d1f | 348 | } |
JMF | 0:2563b0415d1f | 349 | |
JMF | 0:2563b0415d1f | 350 | void chip_sync_unselect() { |
JMF | 0:2563b0415d1f | 351 | chip_unselect(); |
JMF | 0:2563b0415d1f | 352 | enable_spirit_irq(); |
JMF | 0:2563b0415d1f | 353 | } |
JMF | 0:2563b0415d1f | 354 | |
JMF | 0:2563b0415d1f | 355 | /** Init Instance Method **/ |
JMF | 0:2563b0415d1f | 356 | void init(); |
JMF | 0:2563b0415d1f | 357 | |
JMF | 0:2563b0415d1f | 358 | /** Spirit Irq Callback */ |
JMF | 0:2563b0415d1f | 359 | void IrqHandler(); |
JMF | 0:2563b0415d1f | 360 | |
JMF | 0:2563b0415d1f | 361 | /** Constructor **/ |
JMF | 0:2563b0415d1f | 362 | SimpleSpirit1(PinName mosi, PinName miso, PinName sclk, |
JMF | 0:2563b0415d1f | 363 | PinName irq, PinName cs, PinName sdn, |
JMF | 0:2563b0415d1f | 364 | PinName led); |
JMF | 0:2563b0415d1f | 365 | |
JMF | 0:2563b0415d1f | 366 | /** Destructor **/ |
JMF | 0:2563b0415d1f | 367 | ~SimpleSpirit1(void); // should never be called! |
JMF | 0:2563b0415d1f | 368 | |
JMF | 0:2563b0415d1f | 369 | public: |
JMF | 0:2563b0415d1f | 370 | enum { |
JMF | 0:2563b0415d1f | 371 | RX_DONE, |
JMF | 0:2563b0415d1f | 372 | TX_DONE, |
JMF | 0:2563b0415d1f | 373 | TX_ERR |
JMF | 0:2563b0415d1f | 374 | }; |
JMF | 0:2563b0415d1f | 375 | |
JMF | 0:2563b0415d1f | 376 | static SimpleSpirit1& CreateInstance(PinName mosi, PinName miso, PinName sclk, |
JMF | 0:2563b0415d1f | 377 | PinName irq, PinName cs, PinName sdn, |
JMF | 0:2563b0415d1f | 378 | PinName led = NC) { |
JMF | 0:2563b0415d1f | 379 | |
JMF | 0:2563b0415d1f | 380 | if(_singleton == NULL) { |
JMF | 0:2563b0415d1f | 381 | _singleton = new SimpleSpirit1(mosi, miso, sclk, |
JMF | 0:2563b0415d1f | 382 | irq, cs, sdn, led); |
JMF | 0:2563b0415d1f | 383 | _singleton->init(); |
JMF | 0:2563b0415d1f | 384 | } else { |
JMF | 0:2563b0415d1f | 385 | error("SimpleSpirit1 singleton already created!\n"); |
JMF | 0:2563b0415d1f | 386 | } |
JMF | 0:2563b0415d1f | 387 | |
JMF | 0:2563b0415d1f | 388 | return *_singleton; |
JMF | 0:2563b0415d1f | 389 | } |
JMF | 0:2563b0415d1f | 390 | |
JMF | 0:2563b0415d1f | 391 | static SimpleSpirit1& Instance() { |
JMF | 0:2563b0415d1f | 392 | if(_singleton == NULL) { |
JMF | 0:2563b0415d1f | 393 | error("SimpleSpirit1 must be created before used!\n"); |
JMF | 0:2563b0415d1f | 394 | } |
JMF | 0:2563b0415d1f | 395 | |
JMF | 0:2563b0415d1f | 396 | return *_singleton; |
JMF | 0:2563b0415d1f | 397 | } |
JMF | 0:2563b0415d1f | 398 | |
JMF | 0:2563b0415d1f | 399 | /** Attach a function to be called by the Spirit Irq handler when packet has arrived |
JMF | 0:2563b0415d1f | 400 | * |
JMF | 0:2563b0415d1f | 401 | * @param func A void() callback, or 0 to set as none |
JMF | 0:2563b0415d1f | 402 | * |
JMF | 0:2563b0415d1f | 403 | * @note Function 'func' will be executed in interrupt context! |
JMF | 0:2563b0415d1f | 404 | */ |
JMF | 0:2563b0415d1f | 405 | void attach_irq_callback(Callback<void(int)> func) { |
JMF | 0:2563b0415d1f | 406 | _current_irq_callback = func; |
JMF | 0:2563b0415d1f | 407 | } |
JMF | 0:2563b0415d1f | 408 | |
JMF | 0:2563b0415d1f | 409 | /** Switch Radio On/Off **/ |
JMF | 0:2563b0415d1f | 410 | int on(void); |
JMF | 0:2563b0415d1f | 411 | int off(void); |
JMF | 0:2563b0415d1f | 412 | |
JMF | 0:2563b0415d1f | 413 | /** Set Channel **/ |
JMF | 0:2563b0415d1f | 414 | void set_channel(uint8_t channel) { |
JMF | 0:2563b0415d1f | 415 | SpiritRadioSetChannel(channel); |
JMF | 0:2563b0415d1f | 416 | } |
JMF | 0:2563b0415d1f | 417 | |
JMF | 0:2563b0415d1f | 418 | /** Send a Buffer **/ |
JMF | 0:2563b0415d1f | 419 | int send(const void *payload, unsigned int payload_len); |
JMF | 0:2563b0415d1f | 420 | |
JMF | 0:2563b0415d1f | 421 | /** Read into Buffer **/ |
JMF | 0:2563b0415d1f | 422 | int read(void *buf, unsigned int bufsize); |
JMF | 0:2563b0415d1f | 423 | |
JMF | 0:2563b0415d1f | 424 | /** Perform a Clear-Channel Assessment (CCA) to find out if there is |
JMF | 0:2563b0415d1f | 425 | a packet in the air or not. |
JMF | 0:2563b0415d1f | 426 | Returns 1 if packet has been seen. |
JMF | 0:2563b0415d1f | 427 | */ |
JMF | 0:2563b0415d1f | 428 | int channel_clear(void); |
JMF | 0:2563b0415d1f | 429 | |
JMF | 0:2563b0415d1f | 430 | /** Check if the radio driver has just received a packet **/ |
JMF | 0:2563b0415d1f | 431 | int get_pending_packet(void); |
JMF | 0:2563b0415d1f | 432 | |
JMF | 0:2563b0415d1f | 433 | /** Is radio currently receiving **/ |
JMF | 0:2563b0415d1f | 434 | bool is_receiving(void) { |
JMF | 0:2563b0415d1f | 435 | return _is_receiving; |
JMF | 0:2563b0415d1f | 436 | } |
JMF | 0:2563b0415d1f | 437 | |
JMF | 0:2563b0415d1f | 438 | /** Get latest value of RSSI (in dBm) **/ |
JMF | 0:2563b0415d1f | 439 | float get_last_rssi_dbm(void) { |
JMF | 0:2563b0415d1f | 440 | get_last_rssi_raw(); |
JMF | 0:2563b0415d1f | 441 | return (-120.0+((float)(last_rssi-20))/2); |
JMF | 0:2563b0415d1f | 442 | } |
JMF | 0:2563b0415d1f | 443 | |
JMF | 0:2563b0415d1f | 444 | /** Get latest value of RSSI (as Spirit1 raw value) **/ |
JMF | 0:2563b0415d1f | 445 | uint8_t get_last_rssi_raw(void) { |
JMF | 0:2563b0415d1f | 446 | if(last_rssi == 0) { |
JMF | 0:2563b0415d1f | 447 | last_rssi = qi_get_rssi(); |
JMF | 0:2563b0415d1f | 448 | } |
JMF | 0:2563b0415d1f | 449 | return last_rssi; |
JMF | 0:2563b0415d1f | 450 | } |
JMF | 0:2563b0415d1f | 451 | |
JMF | 0:2563b0415d1f | 452 | /** Get latest value of LQI (scaled to 8-bit) **/ |
JMF | 0:2563b0415d1f | 453 | uint8_t get_last_sqi(void) { |
JMF | 0:2563b0415d1f | 454 | const uint8_t max_sqi = 8 * ((SYNC_LENGTH>>1)+1); |
JMF | 0:2563b0415d1f | 455 | if(last_sqi == 0) { |
JMF | 0:2563b0415d1f | 456 | last_sqi = qi_get_sqi(); |
JMF | 0:2563b0415d1f | 457 | } |
JMF | 0:2563b0415d1f | 458 | if(last_sqi > max_sqi) last_sqi = max_sqi; |
JMF | 0:2563b0415d1f | 459 | |
JMF | 0:2563b0415d1f | 460 | return (last_sqi * 255 / max_sqi); |
JMF | 0:2563b0415d1f | 461 | } |
JMF | 0:2563b0415d1f | 462 | |
JMF | 0:2563b0415d1f | 463 | /** Reset Board **/ |
JMF | 0:2563b0415d1f | 464 | void reset_board() { |
JMF | 0:2563b0415d1f | 465 | init(); |
JMF | 0:2563b0415d1f | 466 | } |
JMF | 0:2563b0415d1f | 467 | }; |