Easily add all supported connectivity methods to your mbed OS project

Dependencies:   type-yd-driver

Committer:
MACRUM
Date:
Wed Jul 12 10:52:58 2017 +0000
Revision:
0:615f90842ce8
Initial commit

Who changed what in which revision?

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