ST / stm-spirit1-rf-driver

Prototype RF Driver for STM Sub-1 GHz RF Expansion Boards based on the SPSGRF-868 and SPSGRF-915 Modules for STM32 Nucleo

Currently supported boards:

Note, in order to use expansion board X-NUCLEO-IDS01A4 in mbed you need to perform the following HW modifications on the board:

  • Unmount resistor R4
  • Mount resistor R7

Furthermore, on some Nucleo development boards (e.g. the NUCLEO_F429ZI), in order to be able to use Ethernet together with these Sub-1 GHz RF expansion boards, you need to compile this driver with macro SPIRIT1_SPI_MOSI=PB_5 defined, while the development board typically requires some HW modification as e.g. described here!

This driver can be used together with the 6LoWPAN stack (a.k.a. Nanostack).

Committer:
Wolfgang Betz
Date:
Wed Oct 19 10:04:00 2016 +0200
Revision:
6:f5d01793bf86
Parent:
5:c9c5bc673c64
Child:
7:e90fa8f6bc6c
First simple Sender->Receiver example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 0:4fb29d9ee571 1 /*** Mbed Includes ***/
Wolfgang Betz 0:4fb29d9ee571 2 #include "mbed.h"
Wolfgang Betz 0:4fb29d9ee571 3
Wolfgang Betz 0:4fb29d9ee571 4
Wolfgang Betz 0:4fb29d9ee571 5 /*** Cube Includes ***/
Wolfgang Betz 0:4fb29d9ee571 6 #include "SPIRIT_Radio.h"
Wolfgang Betz 0:4fb29d9ee571 7 #include "SPIRIT_Management.h"
Wolfgang Betz 0:4fb29d9ee571 8 #include "SPIRIT_Commands.h"
Wolfgang Betz 0:4fb29d9ee571 9 #include "MCU_Interface.h"
Wolfgang Betz 0:4fb29d9ee571 10
Wolfgang Betz 0:4fb29d9ee571 11
Wolfgang Betz 0:4fb29d9ee571 12 /*** Contiki Lib Includes ***/
Wolfgang Betz 0:4fb29d9ee571 13 #include "spirit1.h"
Wolfgang Betz 0:4fb29d9ee571 14 #include "spirit1-config.h"
Wolfgang Betz 0:4fb29d9ee571 15 #include "spirit1-const.h"
Wolfgang Betz 0:4fb29d9ee571 16
Wolfgang Betz 0:4fb29d9ee571 17
Wolfgang Betz 4:07537ca85c66 18 /*** Macros from Cube Implementation ***/
Wolfgang Betz 4:07537ca85c66 19 /* transceiver state. */
Wolfgang Betz 4:07537ca85c66 20 #define ON 0
Wolfgang Betz 4:07537ca85c66 21 #define OFF 1
Wolfgang Betz 4:07537ca85c66 22
Wolfgang Betz 4:07537ca85c66 23
Wolfgang Betz 0:4fb29d9ee571 24 /*** Missing Cube External Declarations ***/
Wolfgang Betz 0:4fb29d9ee571 25 extern "C" void SpiritManagementSetFrequencyBase(uint32_t);
Wolfgang Betz 0:4fb29d9ee571 26
Wolfgang Betz 0:4fb29d9ee571 27
Wolfgang Betz 0:4fb29d9ee571 28 /*** A Simple Spirit1 Class ***/
Wolfgang Betz 0:4fb29d9ee571 29 class SimpleSpirit1 { // NOTE: must be a singleton (due to mix of MBED/CUBE code)!!!
Wolfgang Betz 0:4fb29d9ee571 30 protected:
Wolfgang Betz 0:4fb29d9ee571 31 static SimpleSpirit1 *_singleton;
Wolfgang Betz 0:4fb29d9ee571 32
Wolfgang Betz 0:4fb29d9ee571 33 /** Communication Interface Instance Variables **/
Wolfgang Betz 2:45642c5198a2 34 SPI _spi; // betzw - NOTE: Arduino pins are valid only for NUCLEO-F401RE
Wolfgang Betz 0:4fb29d9ee571 35 // mosi: PA_7 (D11)
Wolfgang Betz 0:4fb29d9ee571 36 // miso: PA_6 (D12)
Wolfgang Betz 0:4fb29d9ee571 37 // sclk: PB_3 (D3) or
Wolfgang Betz 0:4fb29d9ee571 38 // PA_5 (D13) (only in case you unmount R4 & mount R7,
Wolfgang Betz 0:4fb29d9ee571 39 // (note: in this case you may not use LED1 on some platforms)
Wolfgang Betz 0:4fb29d9ee571 40 // bits: 8-bit
Wolfgang Betz 0:4fb29d9ee571 41 // mode: 0
Wolfgang Betz 0:4fb29d9ee571 42 // ordr: MSB
Wolfgang Betz 0:4fb29d9ee571 43 // freq: max 10MHz
Wolfgang Betz 2:45642c5198a2 44 InterruptIn _irq; // PC_7 (D9) (falling)
Wolfgang Betz 2:45642c5198a2 45 DigitalOut _chip_select; // PB_6 (D10) ('1' == chip unselected)
Wolfgang Betz 2:45642c5198a2 46 DigitalOut _shut_down; // PA_10 (D2) ('1' == shut_down)
Wolfgang Betz 2:45642c5198a2 47 DigitalOut _led; // PB_4 (D5) (optional)
Wolfgang Betz 0:4fb29d9ee571 48
Wolfgang Betz 3:0df38cfb1e53 49 static Timer _busywait_timer;
Wolfgang Betz 4:07537ca85c66 50 Callback<void()> _current_irq_callback;
Wolfgang Betz 3:0df38cfb1e53 51
Wolfgang Betz 0:4fb29d9ee571 52 /** Static Variables from Cube Implementation **/
Wolfgang Betz 0:4fb29d9ee571 53 /*
Wolfgang Betz 0:4fb29d9ee571 54 * The buffers which hold incoming data.
Wolfgang Betz 0:4fb29d9ee571 55 * The +1 because of the first byte,
Wolfgang Betz 0:4fb29d9ee571 56 * which will contain the length of the packet.
Wolfgang Betz 0:4fb29d9ee571 57 */
Wolfgang Betz 4:07537ca85c66 58 uint16_t spirit_tx_len;
Wolfgang Betz 4:07537ca85c66 59 uint16_t spirit_rx_len;
Wolfgang Betz 6:f5d01793bf86 60 uint16_t _spirit_rx_pos;
Wolfgang Betz 6:f5d01793bf86 61 bool _spirit_rx_err;
Wolfgang Betz 6:f5d01793bf86 62 uint8_t spirit_rx_buf[MAX_PACKET_LEN];
Wolfgang Betz 6:f5d01793bf86 63
Wolfgang Betz 6:f5d01793bf86 64 /** Status Variables from Cube Implementation **/
Wolfgang Betz 3:0df38cfb1e53 65 volatile uint8_t receiving_packet;
Wolfgang Betz 6:f5d01793bf86 66 volatile unsigned int spirit_on;
Wolfgang Betz 3:0df38cfb1e53 67 int just_got_an_ack;
Wolfgang Betz 4:07537ca85c66 68 uint16_t last_rssi; //MGR
Wolfgang Betz 4:07537ca85c66 69 uint16_t last_lqi; //MGR
Wolfgang Betz 0:4fb29d9ee571 70
Wolfgang Betz 0:4fb29d9ee571 71 /** Low Level Instance Variables **/
Wolfgang Betz 0:4fb29d9ee571 72 unsigned int _nr_of_irq_disables;
Wolfgang Betz 0:4fb29d9ee571 73
Wolfgang Betz 3:0df38cfb1e53 74 /** Low Level Instance Methods **/
Wolfgang Betz 4:07537ca85c66 75 void disable_spirit_irq(void) {
Wolfgang Betz 0:4fb29d9ee571 76 _irq.disable_irq();
Wolfgang Betz 0:4fb29d9ee571 77 _nr_of_irq_disables++;
Wolfgang Betz 0:4fb29d9ee571 78 MBED_ASSERT(_nr_of_irq_disables != 0);
Wolfgang Betz 0:4fb29d9ee571 79 }
Wolfgang Betz 0:4fb29d9ee571 80
Wolfgang Betz 4:07537ca85c66 81 void enable_spirit_irq(void) {
Wolfgang Betz 0:4fb29d9ee571 82 MBED_ASSERT(_nr_of_irq_disables > 0);
Wolfgang Betz 0:4fb29d9ee571 83 if(--_nr_of_irq_disables == 0)
Wolfgang Betz 0:4fb29d9ee571 84 _irq.enable_irq();
Wolfgang Betz 0:4fb29d9ee571 85 }
Wolfgang Betz 0:4fb29d9ee571 86
Wolfgang Betz 0:4fb29d9ee571 87 void chip_select() { _chip_select = 0; }
Wolfgang Betz 0:4fb29d9ee571 88 void chip_unselect() { _chip_select = 1; }
Wolfgang Betz 0:4fb29d9ee571 89
Wolfgang Betz 0:4fb29d9ee571 90 void enter_shutdown() { _shut_down = 1; }
Wolfgang Betz 0:4fb29d9ee571 91 void exit_shutdown() {
Wolfgang Betz 0:4fb29d9ee571 92 _shut_down = 0;
Wolfgang Betz 0:4fb29d9ee571 93 wait_ms(2); // wait two milliseconds (to allow Spirit1 a proper boot-up sequence)
Wolfgang Betz 0:4fb29d9ee571 94 }
Wolfgang Betz 0:4fb29d9ee571 95
Wolfgang Betz 0:4fb29d9ee571 96 void cs_to_sclk_delay(void) {
Wolfgang Betz 0:4fb29d9ee571 97 wait_us(1); // heuristic value
Wolfgang Betz 0:4fb29d9ee571 98 }
Wolfgang Betz 0:4fb29d9ee571 99
Wolfgang Betz 3:0df38cfb1e53 100 /**
Wolfgang Betz 3:0df38cfb1e53 101 * @brief Write and read a buffer to/from the SPI peripheral device at the same time
Wolfgang Betz 3:0df38cfb1e53 102 * in 8-bit data mode using synchronous SPI communication.
Wolfgang Betz 3:0df38cfb1e53 103 * @param[in] pBufferToWrite pointer to the buffer of data to send.
Wolfgang Betz 3:0df38cfb1e53 104 * @param[out] pBufferToRead pointer to the buffer to read data into.
Wolfgang Betz 3:0df38cfb1e53 105 * @param[in] NumBytes number of bytes to read and write.
Wolfgang Betz 3:0df38cfb1e53 106 * @retval 0 if ok.
Wolfgang Betz 3:0df38cfb1e53 107 * @retval -1 if data format error.
Wolfgang Betz 3:0df38cfb1e53 108 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
Wolfgang Betz 3:0df38cfb1e53 109 * before calling this function and to enable them again after.
Wolfgang Betz 3:0df38cfb1e53 110 */
Wolfgang Betz 3:0df38cfb1e53 111 void spi_write_read(uint8_t* pBufferToWrite, uint8_t* pBufferToRead, uint16_t NumBytes)
Wolfgang Betz 3:0df38cfb1e53 112 {
Wolfgang Betz 3:0df38cfb1e53 113 /* Read and write data at the same time. */
Wolfgang Betz 3:0df38cfb1e53 114 for (int i = 0; i < NumBytes; i++) {
Wolfgang Betz 3:0df38cfb1e53 115 pBufferToRead[i] = _spi.write(pBufferToWrite[i]);
Wolfgang Betz 3:0df38cfb1e53 116 }
Wolfgang Betz 3:0df38cfb1e53 117 }
Wolfgang Betz 3:0df38cfb1e53 118
Wolfgang Betz 0:4fb29d9ee571 119 /** Radio Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 120 void radio_set_xtal_freq(uint32_t freq) {
Wolfgang Betz 0:4fb29d9ee571 121 SpiritRadioSetXtalFrequency(freq);
Wolfgang Betz 0:4fb29d9ee571 122 }
Wolfgang Betz 0:4fb29d9ee571 123
Wolfgang Betz 0:4fb29d9ee571 124 void radio_set_pa_level_dbm(uint8_t cIndex, float fPowerdBm) {
Wolfgang Betz 0:4fb29d9ee571 125 SpiritRadioSetPALeveldBm(cIndex, fPowerdBm);
Wolfgang Betz 0:4fb29d9ee571 126 }
Wolfgang Betz 0:4fb29d9ee571 127
Wolfgang Betz 0:4fb29d9ee571 128 void radio_set_pa_level_max_index(uint8_t cIndex) {
Wolfgang Betz 0:4fb29d9ee571 129 SpiritRadioSetPALevelMaxIndex(cIndex);
Wolfgang Betz 0:4fb29d9ee571 130 }
Wolfgang Betz 0:4fb29d9ee571 131
Wolfgang Betz 0:4fb29d9ee571 132 uint8_t radio_init(SRadioInit *init_struct) {
Wolfgang Betz 0:4fb29d9ee571 133 return SpiritRadioInit(init_struct);
Wolfgang Betz 0:4fb29d9ee571 134 }
Wolfgang Betz 3:0df38cfb1e53 135
Wolfgang Betz 0:4fb29d9ee571 136 void radio_persisten_rx(SpiritFunctionalState xNewState) {
Wolfgang Betz 0:4fb29d9ee571 137 SpiritRadioPersistenRx(xNewState);
Wolfgang Betz 0:4fb29d9ee571 138 }
Wolfgang Betz 0:4fb29d9ee571 139
Wolfgang Betz 0:4fb29d9ee571 140 void radio_afc_freeze_on_sync(SpiritFunctionalState xNewState) {
Wolfgang Betz 0:4fb29d9ee571 141 SpiritRadioAFCFreezeOnSync(xNewState);
Wolfgang Betz 0:4fb29d9ee571 142 }
Wolfgang Betz 0:4fb29d9ee571 143
Wolfgang Betz 0:4fb29d9ee571 144 /** Packet System Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 145 void pkt_basic_init(PktBasicInit* pxPktBasicInit) {
Wolfgang Betz 0:4fb29d9ee571 146 SpiritPktBasicInit(pxPktBasicInit);
Wolfgang Betz 0:4fb29d9ee571 147 }
Wolfgang Betz 0:4fb29d9ee571 148
Wolfgang Betz 3:0df38cfb1e53 149 void pkt_basic_set_payload_length(uint16_t nPayloadLength) {
Wolfgang Betz 3:0df38cfb1e53 150 SpiritPktBasicSetPayloadLength(nPayloadLength);
Wolfgang Betz 3:0df38cfb1e53 151 }
Wolfgang Betz 3:0df38cfb1e53 152
Wolfgang Betz 4:07537ca85c66 153 uint16_t pkt_basic_get_received_pkt_length(void) {
Wolfgang Betz 4:07537ca85c66 154 return SpiritPktBasicGetReceivedPktLength();
Wolfgang Betz 4:07537ca85c66 155 }
Wolfgang Betz 4:07537ca85c66 156
Wolfgang Betz 3:0df38cfb1e53 157 /** IRQ Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 158 void irq_de_init(SpiritIrqs* pxIrqInit) {
Wolfgang Betz 0:4fb29d9ee571 159 SpiritIrqDeInit(pxIrqInit);
Wolfgang Betz 0:4fb29d9ee571 160 }
Wolfgang Betz 0:4fb29d9ee571 161
Wolfgang Betz 0:4fb29d9ee571 162 void irq_clear_status(void) {
Wolfgang Betz 0:4fb29d9ee571 163 SpiritIrqClearStatus();
Wolfgang Betz 0:4fb29d9ee571 164 }
Wolfgang Betz 0:4fb29d9ee571 165
Wolfgang Betz 0:4fb29d9ee571 166 void irq_set_status(IrqList xIrq, SpiritFunctionalState xNewState) {
Wolfgang Betz 0:4fb29d9ee571 167 SpiritIrq(xIrq, xNewState);
Wolfgang Betz 0:4fb29d9ee571 168 }
Wolfgang Betz 0:4fb29d9ee571 169
Wolfgang Betz 4:07537ca85c66 170 void irq_get_status(SpiritIrqs* pxIrqStatus) {
Wolfgang Betz 4:07537ca85c66 171 SpiritIrqGetStatus(pxIrqStatus);
Wolfgang Betz 4:07537ca85c66 172 }
Wolfgang Betz 4:07537ca85c66 173
Wolfgang Betz 0:4fb29d9ee571 174 /** Management Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 175 void mgmt_set_freq_base(uint32_t freq) {
Wolfgang Betz 0:4fb29d9ee571 176 SpiritManagementSetFrequencyBase(freq);
Wolfgang Betz 0:4fb29d9ee571 177 }
Wolfgang Betz 0:4fb29d9ee571 178
Wolfgang Betz 4:07537ca85c66 179 void mgmt_refresh_status(void) {
Wolfgang Betz 4:07537ca85c66 180 SpiritRefreshStatus();
Wolfgang Betz 4:07537ca85c66 181 }
Wolfgang Betz 4:07537ca85c66 182
Wolfgang Betz 0:4fb29d9ee571 183 /** Spirit GPIO Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 184 void spirit_gpio_init(SGpioInit* pxGpioInitStruct) {
Wolfgang Betz 0:4fb29d9ee571 185 SpiritGpioInit(pxGpioInitStruct);
Wolfgang Betz 0:4fb29d9ee571 186 }
Wolfgang Betz 0:4fb29d9ee571 187
Wolfgang Betz 0:4fb29d9ee571 188 /** Qi Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 189 void qi_set_sqi_threshold(SqiThreshold xSqiThr) {
Wolfgang Betz 0:4fb29d9ee571 190 SpiritQiSetSqiThreshold(xSqiThr);
Wolfgang Betz 0:4fb29d9ee571 191 }
Wolfgang Betz 0:4fb29d9ee571 192
Wolfgang Betz 0:4fb29d9ee571 193 void qi_sqi_check(SpiritFunctionalState xNewState) {
Wolfgang Betz 0:4fb29d9ee571 194 SpiritQiSqiCheck(xNewState);
Wolfgang Betz 0:4fb29d9ee571 195 }
Wolfgang Betz 0:4fb29d9ee571 196
Wolfgang Betz 0:4fb29d9ee571 197 void qi_set_rssi_threshold_dbm(int nDbmValue) {
Wolfgang Betz 0:4fb29d9ee571 198 SpiritQiSetRssiThresholddBm(nDbmValue);
Wolfgang Betz 0:4fb29d9ee571 199 }
Wolfgang Betz 0:4fb29d9ee571 200
Wolfgang Betz 4:07537ca85c66 201 float qi_get_rssi_dbm() {
Wolfgang Betz 4:07537ca85c66 202 return (-120.0+((float)(SpiritQiGetRssi()-20))/2);
Wolfgang Betz 4:07537ca85c66 203 }
Wolfgang Betz 4:07537ca85c66 204
Wolfgang Betz 4:07537ca85c66 205 uint8_t qi_get_rssi() {
Wolfgang Betz 4:07537ca85c66 206 return SpiritQiGetRssi();
Wolfgang Betz 4:07537ca85c66 207 }
Wolfgang Betz 4:07537ca85c66 208
Wolfgang Betz 4:07537ca85c66 209 uint8_t qi_get_lqi() {
Wolfgang Betz 4:07537ca85c66 210 return SpiritQiGetLqi();
Wolfgang Betz 4:07537ca85c66 211 }
Wolfgang Betz 4:07537ca85c66 212
Wolfgang Betz 0:4fb29d9ee571 213 /** Timer Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 214 void timer_set_rx_timeout_stop_condition(RxTimeoutStopCondition xStopCondition) {
Wolfgang Betz 0:4fb29d9ee571 215 SpiritTimerSetRxTimeoutStopCondition(xStopCondition);
Wolfgang Betz 0:4fb29d9ee571 216 }
Wolfgang Betz 0:4fb29d9ee571 217
Wolfgang Betz 0:4fb29d9ee571 218 void timer_set_rx_timeout_counter(uint8_t cCounter) {
Wolfgang Betz 0:4fb29d9ee571 219 SpiritTimerSetRxTimeoutCounter(cCounter);
Wolfgang Betz 0:4fb29d9ee571 220 }
Wolfgang Betz 0:4fb29d9ee571 221
Wolfgang Betz 0:4fb29d9ee571 222 void timer_set_infinite_rx_timeout(void) {
Wolfgang Betz 0:4fb29d9ee571 223 timer_set_rx_timeout_counter(0);
Wolfgang Betz 0:4fb29d9ee571 224 }
Wolfgang Betz 0:4fb29d9ee571 225
Wolfgang Betz 0:4fb29d9ee571 226 /** Command Instance Methods**/
Wolfgang Betz 4:07537ca85c66 227 void cmd_strobe(uint8_t cmd) {
Wolfgang Betz 0:4fb29d9ee571 228 SpiritCmdStrobeCommand((SpiritCmd)cmd);
Wolfgang Betz 0:4fb29d9ee571 229 }
Wolfgang Betz 0:4fb29d9ee571 230
Wolfgang Betz 4:07537ca85c66 231 void cmd_strobe_flush_rx_fifo() {
Wolfgang Betz 4:07537ca85c66 232 SpiritCmdStrobeCommand(CMD_FLUSHRXFIFO);
Wolfgang Betz 4:07537ca85c66 233 }
Wolfgang Betz 4:07537ca85c66 234
Wolfgang Betz 3:0df38cfb1e53 235 /** SPI Instance Methods **/
Wolfgang Betz 3:0df38cfb1e53 236 StatusBytes spi_write_linear_fifo(uint8_t cNbBytes, uint8_t* pcBuffer) {
Wolfgang Betz 3:0df38cfb1e53 237 return SdkEvalSpiWriteFifo(cNbBytes, pcBuffer);
Wolfgang Betz 3:0df38cfb1e53 238 }
Wolfgang Betz 3:0df38cfb1e53 239
Wolfgang Betz 4:07537ca85c66 240 StatusBytes spi_read_linear_fifo(uint8_t cNbBytes, uint8_t* pcBuffer) {
Wolfgang Betz 4:07537ca85c66 241 return SdkEvalSpiReadFifo(cNbBytes, pcBuffer);
Wolfgang Betz 4:07537ca85c66 242 }
Wolfgang Betz 4:07537ca85c66 243
Wolfgang Betz 4:07537ca85c66 244 /** Linear FIFO Instance Methods **/
Wolfgang Betz 4:07537ca85c66 245 uint8_t linear_fifo_read_num_elements_rx_fifo(void) {
Wolfgang Betz 4:07537ca85c66 246 return SpiritLinearFifoReadNumElementsRxFifo();
Wolfgang Betz 4:07537ca85c66 247 }
Wolfgang Betz 4:07537ca85c66 248
Wolfgang Betz 5:c9c5bc673c64 249 uint8_t linear_fifo_read_num_elements_tx_fifo(void) {
Wolfgang Betz 5:c9c5bc673c64 250 return SpiritLinearFifoReadNumElementsTxFifo();
Wolfgang Betz 5:c9c5bc673c64 251 }
Wolfgang Betz 5:c9c5bc673c64 252
Wolfgang Betz 3:0df38cfb1e53 253 /** Internal Spirit Methods */
Wolfgang Betz 3:0df38cfb1e53 254 void set_ready_state(void);
Wolfgang Betz 3:0df38cfb1e53 255 uint16_t arch_refresh_status(void);
Wolfgang Betz 3:0df38cfb1e53 256
Wolfgang Betz 0:4fb29d9ee571 257 /** Friend Functions **/
Wolfgang Betz 0:4fb29d9ee571 258 friend StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 259 friend StatusBytes SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 260 friend StatusBytes SdkEvalSpiCommandStrobes(uint8_t cCommandCode);
Wolfgang Betz 0:4fb29d9ee571 261 friend StatusBytes SdkEvalSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 262 friend StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 263
Wolfgang Betz 0:4fb29d9ee571 264 /** Sdk Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 265 StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 266 StatusBytes SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 267 StatusBytes SdkEvalSpiCommandStrobes(uint8_t cCommandCode);
Wolfgang Betz 0:4fb29d9ee571 268 StatusBytes SdkEvalSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 269 StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 270
Wolfgang Betz 0:4fb29d9ee571 271 /** Helper Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 272 void chip_sync_select() {
Wolfgang Betz 4:07537ca85c66 273 disable_spirit_irq();
Wolfgang Betz 0:4fb29d9ee571 274 chip_select();
Wolfgang Betz 0:4fb29d9ee571 275 cs_to_sclk_delay();
Wolfgang Betz 0:4fb29d9ee571 276 }
Wolfgang Betz 0:4fb29d9ee571 277
Wolfgang Betz 0:4fb29d9ee571 278 void chip_sync_unselect() {
Wolfgang Betz 0:4fb29d9ee571 279 chip_unselect();
Wolfgang Betz 4:07537ca85c66 280 enable_spirit_irq();
Wolfgang Betz 0:4fb29d9ee571 281 }
Wolfgang Betz 0:4fb29d9ee571 282
Wolfgang Betz 2:45642c5198a2 283 /** Init Instance Method **/
Wolfgang Betz 2:45642c5198a2 284 void init(void);
Wolfgang Betz 2:45642c5198a2 285
Wolfgang Betz 5:c9c5bc673c64 286 /** Spirit Irq Callback */
Wolfgang Betz 5:c9c5bc673c64 287 void IrqHandler();
Wolfgang Betz 5:c9c5bc673c64 288
Wolfgang Betz 0:4fb29d9ee571 289 /** Constructor **/
Wolfgang Betz 0:4fb29d9ee571 290 SimpleSpirit1(PinName mosi, PinName miso, PinName sclk,
Wolfgang Betz 0:4fb29d9ee571 291 PinName irq, PinName cs, PinName sdn,
Wolfgang Betz 0:4fb29d9ee571 292 PinName led);
Wolfgang Betz 0:4fb29d9ee571 293
Wolfgang Betz 0:4fb29d9ee571 294 /** Destructor **/
Wolfgang Betz 0:4fb29d9ee571 295 ~SimpleSpirit1(void); // should never be called!
Wolfgang Betz 0:4fb29d9ee571 296
Wolfgang Betz 6:f5d01793bf86 297 private:
Wolfgang Betz 6:f5d01793bf86 298 /*** Original Contiki APIs & Variables ***/
Wolfgang Betz 6:f5d01793bf86 299 /** Variable(s) for Original API(s) **/
Wolfgang Betz 6:f5d01793bf86 300 int packet_is_prepared;
Wolfgang Betz 6:f5d01793bf86 301
Wolfgang Betz 6:f5d01793bf86 302 /** Prepare the radio with a packet to be sent. **/
Wolfgang Betz 6:f5d01793bf86 303 int prepare_contiki(const void *payload, unsigned short payload_len);
Wolfgang Betz 6:f5d01793bf86 304
Wolfgang Betz 6:f5d01793bf86 305 /** Send the packet that has previously been prepared. **/
Wolfgang Betz 6:f5d01793bf86 306 int transmit_contiki(unsigned short payload_len);
Wolfgang Betz 6:f5d01793bf86 307
Wolfgang Betz 6:f5d01793bf86 308 /** Prepare & Transmit */
Wolfgang Betz 6:f5d01793bf86 309 int send_contiki(const void *payload, unsigned short payload_len) {
Wolfgang Betz 6:f5d01793bf86 310 if(prepare_contiki(payload, payload_len) == RADIO_TX_ERR) {
Wolfgang Betz 6:f5d01793bf86 311 return RADIO_TX_ERR;
Wolfgang Betz 6:f5d01793bf86 312 }
Wolfgang Betz 6:f5d01793bf86 313 return transmit_contiki(payload_len);
Wolfgang Betz 6:f5d01793bf86 314 }
Wolfgang Betz 6:f5d01793bf86 315
Wolfgang Betz 0:4fb29d9ee571 316 public:
Wolfgang Betz 0:4fb29d9ee571 317 static SimpleSpirit1& CreateInstance(PinName mosi, PinName miso, PinName sclk,
Wolfgang Betz 0:4fb29d9ee571 318 PinName irq, PinName cs, PinName sdn,
Wolfgang Betz 0:4fb29d9ee571 319 PinName led = NC) {
Wolfgang Betz 0:4fb29d9ee571 320
Wolfgang Betz 0:4fb29d9ee571 321 if(_singleton == NULL) {
Wolfgang Betz 0:4fb29d9ee571 322 _singleton = new SimpleSpirit1(mosi, miso, sclk,
Wolfgang Betz 0:4fb29d9ee571 323 irq, cs, sdn, led);
Wolfgang Betz 2:45642c5198a2 324 _singleton->init();
Wolfgang Betz 0:4fb29d9ee571 325 } else {
Wolfgang Betz 2:45642c5198a2 326 error("SimpleSpirit1 singleton already created!\n");
Wolfgang Betz 0:4fb29d9ee571 327 }
Wolfgang Betz 0:4fb29d9ee571 328
Wolfgang Betz 0:4fb29d9ee571 329 return *_singleton;
Wolfgang Betz 0:4fb29d9ee571 330 }
Wolfgang Betz 0:4fb29d9ee571 331
Wolfgang Betz 0:4fb29d9ee571 332 static SimpleSpirit1& Instance() {
Wolfgang Betz 0:4fb29d9ee571 333 if(_singleton == NULL) {
Wolfgang Betz 2:45642c5198a2 334 error("SimpleSpirit1 must be created before used!\n");
Wolfgang Betz 0:4fb29d9ee571 335 }
Wolfgang Betz 0:4fb29d9ee571 336
Wolfgang Betz 0:4fb29d9ee571 337 return *_singleton;
Wolfgang Betz 0:4fb29d9ee571 338 }
Wolfgang Betz 0:4fb29d9ee571 339
Wolfgang Betz 6:f5d01793bf86 340 /** Attach a function to be called by the Spirit Irq handler when packet has arrived
Wolfgang Betz 0:4fb29d9ee571 341 *
Wolfgang Betz 3:0df38cfb1e53 342 * @param func A void() callback, or 0 to set as none
Wolfgang Betz 0:4fb29d9ee571 343 *
Wolfgang Betz 0:4fb29d9ee571 344 * @note Function 'func' will be executed in interrupt context!
Wolfgang Betz 0:4fb29d9ee571 345 */
Wolfgang Betz 5:c9c5bc673c64 346 void attach_irq_callback(Callback<void()> func) {
Wolfgang Betz 4:07537ca85c66 347 _current_irq_callback = func;
Wolfgang Betz 0:4fb29d9ee571 348 }
Wolfgang Betz 3:0df38cfb1e53 349
Wolfgang Betz 4:07537ca85c66 350 /** Switch Radio On/Off **/
Wolfgang Betz 4:07537ca85c66 351 int on(void);
Wolfgang Betz 4:07537ca85c66 352 int off(void);
Wolfgang Betz 4:07537ca85c66 353
Wolfgang Betz 6:f5d01793bf86 354 int send(const void *payload, unsigned int payload_len);
Wolfgang Betz 5:c9c5bc673c64 355
Wolfgang Betz 4:07537ca85c66 356 /** Read into Buffer **/
Wolfgang Betz 6:f5d01793bf86 357 int read(void *buf, unsigned int bufsize);
Wolfgang Betz 4:07537ca85c66 358
Wolfgang Betz 4:07537ca85c66 359 /** Perform a Clear-Channel Assessment (CCA) to find out if there is
Wolfgang Betz 4:07537ca85c66 360 a packet in the air or not.
Wolfgang Betz 4:07537ca85c66 361 Returns 0 if packet has been seen.
Wolfgang Betz 4:07537ca85c66 362 */
Wolfgang Betz 4:07537ca85c66 363 int channel_clear(void);
Wolfgang Betz 4:07537ca85c66 364
Wolfgang Betz 4:07537ca85c66 365 /** Check if the radio driver is currently receiving a packet */
Wolfgang Betz 4:07537ca85c66 366 int incoming_packet(void);
Wolfgang Betz 4:07537ca85c66 367
Wolfgang Betz 4:07537ca85c66 368 /** Check if the radio driver has just received a packet **/
Wolfgang Betz 4:07537ca85c66 369 int pending_packet(void);
Wolfgang Betz 0:4fb29d9ee571 370 };