센서보드 RF IRQ 테스트 중

Fork of stm-spirit1-rf-driver by ST

Committer:
Wolfgang Betz
Date:
Fri Jul 21 15:30:53 2017 +0200
Revision:
74:7672516efa2a
Parent:
73:a1342eb7974c
Child:
82:a18c22d2b83a
Minor comment corrections

Who changed what in which revision?

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