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:
Tue Oct 18 11:45:43 2016 +0200
Revision:
5:c9c5bc673c64
Parent:
4:07537ca85c66
Child:
6:f5d01793bf86
First version of send()

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 uint8_t spirit_rx_buf[MAX_PACKET_LEN];
Wolfgang Betz 4:07537ca85c66 59 uint16_t spirit_tx_len;
Wolfgang Betz 4:07537ca85c66 60 uint16_t spirit_rx_len;
Wolfgang Betz 0:4fb29d9ee571 61 volatile unsigned int spirit_on;
Wolfgang Betz 3:0df38cfb1e53 62 volatile uint8_t receiving_packet;
Wolfgang Betz 3:0df38cfb1e53 63 int packet_is_prepared;
Wolfgang Betz 3:0df38cfb1e53 64 int just_got_an_ack;
Wolfgang Betz 4:07537ca85c66 65 uint16_t last_rssi; //MGR
Wolfgang Betz 4:07537ca85c66 66 uint16_t last_lqi; //MGR
Wolfgang Betz 0:4fb29d9ee571 67
Wolfgang Betz 0:4fb29d9ee571 68 /** Low Level Instance Variables **/
Wolfgang Betz 0:4fb29d9ee571 69 unsigned int _nr_of_irq_disables;
Wolfgang Betz 0:4fb29d9ee571 70
Wolfgang Betz 3:0df38cfb1e53 71 /** Low Level Instance Methods **/
Wolfgang Betz 4:07537ca85c66 72 void disable_spirit_irq(void) {
Wolfgang Betz 0:4fb29d9ee571 73 _irq.disable_irq();
Wolfgang Betz 0:4fb29d9ee571 74 _nr_of_irq_disables++;
Wolfgang Betz 0:4fb29d9ee571 75 MBED_ASSERT(_nr_of_irq_disables != 0);
Wolfgang Betz 0:4fb29d9ee571 76 }
Wolfgang Betz 0:4fb29d9ee571 77
Wolfgang Betz 4:07537ca85c66 78 void enable_spirit_irq(void) {
Wolfgang Betz 0:4fb29d9ee571 79 MBED_ASSERT(_nr_of_irq_disables > 0);
Wolfgang Betz 0:4fb29d9ee571 80 if(--_nr_of_irq_disables == 0)
Wolfgang Betz 0:4fb29d9ee571 81 _irq.enable_irq();
Wolfgang Betz 0:4fb29d9ee571 82 }
Wolfgang Betz 0:4fb29d9ee571 83
Wolfgang Betz 0:4fb29d9ee571 84 void chip_select() { _chip_select = 0; }
Wolfgang Betz 0:4fb29d9ee571 85 void chip_unselect() { _chip_select = 1; }
Wolfgang Betz 0:4fb29d9ee571 86
Wolfgang Betz 0:4fb29d9ee571 87 void enter_shutdown() { _shut_down = 1; }
Wolfgang Betz 0:4fb29d9ee571 88 void exit_shutdown() {
Wolfgang Betz 0:4fb29d9ee571 89 _shut_down = 0;
Wolfgang Betz 0:4fb29d9ee571 90 wait_ms(2); // wait two milliseconds (to allow Spirit1 a proper boot-up sequence)
Wolfgang Betz 0:4fb29d9ee571 91 }
Wolfgang Betz 0:4fb29d9ee571 92
Wolfgang Betz 0:4fb29d9ee571 93 void cs_to_sclk_delay(void) {
Wolfgang Betz 0:4fb29d9ee571 94 wait_us(1); // heuristic value
Wolfgang Betz 0:4fb29d9ee571 95 }
Wolfgang Betz 0:4fb29d9ee571 96
Wolfgang Betz 3:0df38cfb1e53 97 /**
Wolfgang Betz 3:0df38cfb1e53 98 * @brief Write and read a buffer to/from the SPI peripheral device at the same time
Wolfgang Betz 3:0df38cfb1e53 99 * in 8-bit data mode using synchronous SPI communication.
Wolfgang Betz 3:0df38cfb1e53 100 * @param[in] pBufferToWrite pointer to the buffer of data to send.
Wolfgang Betz 3:0df38cfb1e53 101 * @param[out] pBufferToRead pointer to the buffer to read data into.
Wolfgang Betz 3:0df38cfb1e53 102 * @param[in] NumBytes number of bytes to read and write.
Wolfgang Betz 3:0df38cfb1e53 103 * @retval 0 if ok.
Wolfgang Betz 3:0df38cfb1e53 104 * @retval -1 if data format error.
Wolfgang Betz 3:0df38cfb1e53 105 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
Wolfgang Betz 3:0df38cfb1e53 106 * before calling this function and to enable them again after.
Wolfgang Betz 3:0df38cfb1e53 107 */
Wolfgang Betz 3:0df38cfb1e53 108 void spi_write_read(uint8_t* pBufferToWrite, uint8_t* pBufferToRead, uint16_t NumBytes)
Wolfgang Betz 3:0df38cfb1e53 109 {
Wolfgang Betz 3:0df38cfb1e53 110 /* Read and write data at the same time. */
Wolfgang Betz 3:0df38cfb1e53 111 for (int i = 0; i < NumBytes; i++) {
Wolfgang Betz 3:0df38cfb1e53 112 pBufferToRead[i] = _spi.write(pBufferToWrite[i]);
Wolfgang Betz 3:0df38cfb1e53 113 }
Wolfgang Betz 3:0df38cfb1e53 114 }
Wolfgang Betz 3:0df38cfb1e53 115
Wolfgang Betz 0:4fb29d9ee571 116 /** Radio Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 117 void radio_set_xtal_freq(uint32_t freq) {
Wolfgang Betz 0:4fb29d9ee571 118 SpiritRadioSetXtalFrequency(freq);
Wolfgang Betz 0:4fb29d9ee571 119 }
Wolfgang Betz 0:4fb29d9ee571 120
Wolfgang Betz 0:4fb29d9ee571 121 void radio_set_pa_level_dbm(uint8_t cIndex, float fPowerdBm) {
Wolfgang Betz 0:4fb29d9ee571 122 SpiritRadioSetPALeveldBm(cIndex, fPowerdBm);
Wolfgang Betz 0:4fb29d9ee571 123 }
Wolfgang Betz 0:4fb29d9ee571 124
Wolfgang Betz 0:4fb29d9ee571 125 void radio_set_pa_level_max_index(uint8_t cIndex) {
Wolfgang Betz 0:4fb29d9ee571 126 SpiritRadioSetPALevelMaxIndex(cIndex);
Wolfgang Betz 0:4fb29d9ee571 127 }
Wolfgang Betz 0:4fb29d9ee571 128
Wolfgang Betz 0:4fb29d9ee571 129 uint8_t radio_init(SRadioInit *init_struct) {
Wolfgang Betz 0:4fb29d9ee571 130 return SpiritRadioInit(init_struct);
Wolfgang Betz 0:4fb29d9ee571 131 }
Wolfgang Betz 3:0df38cfb1e53 132
Wolfgang Betz 0:4fb29d9ee571 133 void radio_persisten_rx(SpiritFunctionalState xNewState) {
Wolfgang Betz 0:4fb29d9ee571 134 SpiritRadioPersistenRx(xNewState);
Wolfgang Betz 0:4fb29d9ee571 135 }
Wolfgang Betz 0:4fb29d9ee571 136
Wolfgang Betz 0:4fb29d9ee571 137 void radio_afc_freeze_on_sync(SpiritFunctionalState xNewState) {
Wolfgang Betz 0:4fb29d9ee571 138 SpiritRadioAFCFreezeOnSync(xNewState);
Wolfgang Betz 0:4fb29d9ee571 139 }
Wolfgang Betz 0:4fb29d9ee571 140
Wolfgang Betz 0:4fb29d9ee571 141 /** Packet System Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 142 void pkt_basic_init(PktBasicInit* pxPktBasicInit) {
Wolfgang Betz 0:4fb29d9ee571 143 SpiritPktBasicInit(pxPktBasicInit);
Wolfgang Betz 0:4fb29d9ee571 144 }
Wolfgang Betz 0:4fb29d9ee571 145
Wolfgang Betz 3:0df38cfb1e53 146 void pkt_basic_set_payload_length(uint16_t nPayloadLength) {
Wolfgang Betz 3:0df38cfb1e53 147 SpiritPktBasicSetPayloadLength(nPayloadLength);
Wolfgang Betz 3:0df38cfb1e53 148 }
Wolfgang Betz 3:0df38cfb1e53 149
Wolfgang Betz 4:07537ca85c66 150 uint16_t pkt_basic_get_received_pkt_length(void) {
Wolfgang Betz 4:07537ca85c66 151 return SpiritPktBasicGetReceivedPktLength();
Wolfgang Betz 4:07537ca85c66 152 }
Wolfgang Betz 4:07537ca85c66 153
Wolfgang Betz 3:0df38cfb1e53 154 /** IRQ Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 155 void irq_de_init(SpiritIrqs* pxIrqInit) {
Wolfgang Betz 0:4fb29d9ee571 156 SpiritIrqDeInit(pxIrqInit);
Wolfgang Betz 0:4fb29d9ee571 157 }
Wolfgang Betz 0:4fb29d9ee571 158
Wolfgang Betz 0:4fb29d9ee571 159 void irq_clear_status(void) {
Wolfgang Betz 0:4fb29d9ee571 160 SpiritIrqClearStatus();
Wolfgang Betz 0:4fb29d9ee571 161 }
Wolfgang Betz 0:4fb29d9ee571 162
Wolfgang Betz 0:4fb29d9ee571 163 void irq_set_status(IrqList xIrq, SpiritFunctionalState xNewState) {
Wolfgang Betz 0:4fb29d9ee571 164 SpiritIrq(xIrq, xNewState);
Wolfgang Betz 0:4fb29d9ee571 165 }
Wolfgang Betz 0:4fb29d9ee571 166
Wolfgang Betz 4:07537ca85c66 167 void irq_get_status(SpiritIrqs* pxIrqStatus) {
Wolfgang Betz 4:07537ca85c66 168 SpiritIrqGetStatus(pxIrqStatus);
Wolfgang Betz 4:07537ca85c66 169 }
Wolfgang Betz 4:07537ca85c66 170
Wolfgang Betz 0:4fb29d9ee571 171 /** Management Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 172 void mgmt_set_freq_base(uint32_t freq) {
Wolfgang Betz 0:4fb29d9ee571 173 SpiritManagementSetFrequencyBase(freq);
Wolfgang Betz 0:4fb29d9ee571 174 }
Wolfgang Betz 0:4fb29d9ee571 175
Wolfgang Betz 4:07537ca85c66 176 void mgmt_refresh_status(void) {
Wolfgang Betz 4:07537ca85c66 177 SpiritRefreshStatus();
Wolfgang Betz 4:07537ca85c66 178 }
Wolfgang Betz 4:07537ca85c66 179
Wolfgang Betz 0:4fb29d9ee571 180 /** Spirit GPIO Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 181 void spirit_gpio_init(SGpioInit* pxGpioInitStruct) {
Wolfgang Betz 0:4fb29d9ee571 182 SpiritGpioInit(pxGpioInitStruct);
Wolfgang Betz 0:4fb29d9ee571 183 }
Wolfgang Betz 0:4fb29d9ee571 184
Wolfgang Betz 0:4fb29d9ee571 185 /** Qi Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 186 void qi_set_sqi_threshold(SqiThreshold xSqiThr) {
Wolfgang Betz 0:4fb29d9ee571 187 SpiritQiSetSqiThreshold(xSqiThr);
Wolfgang Betz 0:4fb29d9ee571 188 }
Wolfgang Betz 0:4fb29d9ee571 189
Wolfgang Betz 0:4fb29d9ee571 190 void qi_sqi_check(SpiritFunctionalState xNewState) {
Wolfgang Betz 0:4fb29d9ee571 191 SpiritQiSqiCheck(xNewState);
Wolfgang Betz 0:4fb29d9ee571 192 }
Wolfgang Betz 0:4fb29d9ee571 193
Wolfgang Betz 0:4fb29d9ee571 194 void qi_set_rssi_threshold_dbm(int nDbmValue) {
Wolfgang Betz 0:4fb29d9ee571 195 SpiritQiSetRssiThresholddBm(nDbmValue);
Wolfgang Betz 0:4fb29d9ee571 196 }
Wolfgang Betz 0:4fb29d9ee571 197
Wolfgang Betz 4:07537ca85c66 198 float qi_get_rssi_dbm() {
Wolfgang Betz 4:07537ca85c66 199 return (-120.0+((float)(SpiritQiGetRssi()-20))/2);
Wolfgang Betz 4:07537ca85c66 200 }
Wolfgang Betz 4:07537ca85c66 201
Wolfgang Betz 4:07537ca85c66 202 uint8_t qi_get_rssi() {
Wolfgang Betz 4:07537ca85c66 203 return SpiritQiGetRssi();
Wolfgang Betz 4:07537ca85c66 204 }
Wolfgang Betz 4:07537ca85c66 205
Wolfgang Betz 4:07537ca85c66 206 uint8_t qi_get_lqi() {
Wolfgang Betz 4:07537ca85c66 207 return SpiritQiGetLqi();
Wolfgang Betz 4:07537ca85c66 208 }
Wolfgang Betz 4:07537ca85c66 209
Wolfgang Betz 0:4fb29d9ee571 210 /** Timer Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 211 void timer_set_rx_timeout_stop_condition(RxTimeoutStopCondition xStopCondition) {
Wolfgang Betz 0:4fb29d9ee571 212 SpiritTimerSetRxTimeoutStopCondition(xStopCondition);
Wolfgang Betz 0:4fb29d9ee571 213 }
Wolfgang Betz 0:4fb29d9ee571 214
Wolfgang Betz 0:4fb29d9ee571 215 void timer_set_rx_timeout_counter(uint8_t cCounter) {
Wolfgang Betz 0:4fb29d9ee571 216 SpiritTimerSetRxTimeoutCounter(cCounter);
Wolfgang Betz 0:4fb29d9ee571 217 }
Wolfgang Betz 0:4fb29d9ee571 218
Wolfgang Betz 0:4fb29d9ee571 219 void timer_set_infinite_rx_timeout(void) {
Wolfgang Betz 0:4fb29d9ee571 220 timer_set_rx_timeout_counter(0);
Wolfgang Betz 0:4fb29d9ee571 221 }
Wolfgang Betz 0:4fb29d9ee571 222
Wolfgang Betz 0:4fb29d9ee571 223 /** Command Instance Methods**/
Wolfgang Betz 4:07537ca85c66 224 void cmd_strobe(uint8_t cmd) {
Wolfgang Betz 0:4fb29d9ee571 225 SpiritCmdStrobeCommand((SpiritCmd)cmd);
Wolfgang Betz 0:4fb29d9ee571 226 }
Wolfgang Betz 0:4fb29d9ee571 227
Wolfgang Betz 4:07537ca85c66 228 void cmd_strobe_flush_rx_fifo() {
Wolfgang Betz 4:07537ca85c66 229 SpiritCmdStrobeCommand(CMD_FLUSHRXFIFO);
Wolfgang Betz 4:07537ca85c66 230 }
Wolfgang Betz 4:07537ca85c66 231
Wolfgang Betz 3:0df38cfb1e53 232 /** SPI Instance Methods **/
Wolfgang Betz 3:0df38cfb1e53 233 StatusBytes spi_write_linear_fifo(uint8_t cNbBytes, uint8_t* pcBuffer) {
Wolfgang Betz 3:0df38cfb1e53 234 return SdkEvalSpiWriteFifo(cNbBytes, pcBuffer);
Wolfgang Betz 3:0df38cfb1e53 235 }
Wolfgang Betz 3:0df38cfb1e53 236
Wolfgang Betz 4:07537ca85c66 237 StatusBytes spi_read_linear_fifo(uint8_t cNbBytes, uint8_t* pcBuffer) {
Wolfgang Betz 4:07537ca85c66 238 return SdkEvalSpiReadFifo(cNbBytes, pcBuffer);
Wolfgang Betz 4:07537ca85c66 239 }
Wolfgang Betz 4:07537ca85c66 240
Wolfgang Betz 4:07537ca85c66 241 /** Linear FIFO Instance Methods **/
Wolfgang Betz 4:07537ca85c66 242 uint8_t linear_fifo_read_num_elements_rx_fifo(void) {
Wolfgang Betz 4:07537ca85c66 243 return SpiritLinearFifoReadNumElementsRxFifo();
Wolfgang Betz 4:07537ca85c66 244 }
Wolfgang Betz 4:07537ca85c66 245
Wolfgang Betz 5:c9c5bc673c64 246 uint8_t linear_fifo_read_num_elements_tx_fifo(void) {
Wolfgang Betz 5:c9c5bc673c64 247 return SpiritLinearFifoReadNumElementsTxFifo();
Wolfgang Betz 5:c9c5bc673c64 248 }
Wolfgang Betz 5:c9c5bc673c64 249
Wolfgang Betz 3:0df38cfb1e53 250 /** Internal Spirit Methods */
Wolfgang Betz 3:0df38cfb1e53 251 void set_ready_state(void);
Wolfgang Betz 3:0df38cfb1e53 252 uint16_t arch_refresh_status(void);
Wolfgang Betz 3:0df38cfb1e53 253
Wolfgang Betz 0:4fb29d9ee571 254 /** Friend Functions **/
Wolfgang Betz 0:4fb29d9ee571 255 friend StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 256 friend StatusBytes SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 257 friend StatusBytes SdkEvalSpiCommandStrobes(uint8_t cCommandCode);
Wolfgang Betz 0:4fb29d9ee571 258 friend StatusBytes SdkEvalSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 259 friend StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 260
Wolfgang Betz 0:4fb29d9ee571 261 /** Sdk Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 262 StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 263 StatusBytes SdkEvalSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 264 StatusBytes SdkEvalSpiCommandStrobes(uint8_t cCommandCode);
Wolfgang Betz 0:4fb29d9ee571 265 StatusBytes SdkEvalSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 266 StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
Wolfgang Betz 0:4fb29d9ee571 267
Wolfgang Betz 0:4fb29d9ee571 268 /** Helper Instance Methods **/
Wolfgang Betz 0:4fb29d9ee571 269 void chip_sync_select() {
Wolfgang Betz 4:07537ca85c66 270 disable_spirit_irq();
Wolfgang Betz 0:4fb29d9ee571 271 chip_select();
Wolfgang Betz 0:4fb29d9ee571 272 cs_to_sclk_delay();
Wolfgang Betz 0:4fb29d9ee571 273 }
Wolfgang Betz 0:4fb29d9ee571 274
Wolfgang Betz 0:4fb29d9ee571 275 void chip_sync_unselect() {
Wolfgang Betz 0:4fb29d9ee571 276 chip_unselect();
Wolfgang Betz 4:07537ca85c66 277 enable_spirit_irq();
Wolfgang Betz 0:4fb29d9ee571 278 }
Wolfgang Betz 0:4fb29d9ee571 279
Wolfgang Betz 2:45642c5198a2 280 /** Init Instance Method **/
Wolfgang Betz 2:45642c5198a2 281 void init(void);
Wolfgang Betz 2:45642c5198a2 282
Wolfgang Betz 5:c9c5bc673c64 283 /** Spirit Irq Callback */
Wolfgang Betz 5:c9c5bc673c64 284 void IrqHandler();
Wolfgang Betz 5:c9c5bc673c64 285
Wolfgang Betz 0:4fb29d9ee571 286 /** Constructor **/
Wolfgang Betz 0:4fb29d9ee571 287 SimpleSpirit1(PinName mosi, PinName miso, PinName sclk,
Wolfgang Betz 0:4fb29d9ee571 288 PinName irq, PinName cs, PinName sdn,
Wolfgang Betz 0:4fb29d9ee571 289 PinName led);
Wolfgang Betz 0:4fb29d9ee571 290
Wolfgang Betz 0:4fb29d9ee571 291 /** Destructor **/
Wolfgang Betz 0:4fb29d9ee571 292 ~SimpleSpirit1(void); // should never be called!
Wolfgang Betz 0:4fb29d9ee571 293
Wolfgang Betz 0:4fb29d9ee571 294 public:
Wolfgang Betz 0:4fb29d9ee571 295 static SimpleSpirit1& CreateInstance(PinName mosi, PinName miso, PinName sclk,
Wolfgang Betz 0:4fb29d9ee571 296 PinName irq, PinName cs, PinName sdn,
Wolfgang Betz 0:4fb29d9ee571 297 PinName led = NC) {
Wolfgang Betz 0:4fb29d9ee571 298
Wolfgang Betz 0:4fb29d9ee571 299 if(_singleton == NULL) {
Wolfgang Betz 0:4fb29d9ee571 300 _singleton = new SimpleSpirit1(mosi, miso, sclk,
Wolfgang Betz 0:4fb29d9ee571 301 irq, cs, sdn, led);
Wolfgang Betz 2:45642c5198a2 302 _singleton->init();
Wolfgang Betz 0:4fb29d9ee571 303 } else {
Wolfgang Betz 2:45642c5198a2 304 error("SimpleSpirit1 singleton already created!\n");
Wolfgang Betz 0:4fb29d9ee571 305 }
Wolfgang Betz 0:4fb29d9ee571 306
Wolfgang Betz 0:4fb29d9ee571 307 return *_singleton;
Wolfgang Betz 0:4fb29d9ee571 308 }
Wolfgang Betz 0:4fb29d9ee571 309
Wolfgang Betz 0:4fb29d9ee571 310 static SimpleSpirit1& Instance() {
Wolfgang Betz 0:4fb29d9ee571 311 if(_singleton == NULL) {
Wolfgang Betz 2:45642c5198a2 312 error("SimpleSpirit1 must be created before used!\n");
Wolfgang Betz 0:4fb29d9ee571 313 }
Wolfgang Betz 0:4fb29d9ee571 314
Wolfgang Betz 0:4fb29d9ee571 315 return *_singleton;
Wolfgang Betz 0:4fb29d9ee571 316 }
Wolfgang Betz 0:4fb29d9ee571 317
Wolfgang Betz 5:c9c5bc673c64 318 /** Attach a function to be called when by the Spirit Irq handler when packet has arrived
Wolfgang Betz 0:4fb29d9ee571 319 *
Wolfgang Betz 3:0df38cfb1e53 320 * @param func A void() callback, or 0 to set as none
Wolfgang Betz 0:4fb29d9ee571 321 *
Wolfgang Betz 0:4fb29d9ee571 322 * @note Function 'func' will be executed in interrupt context!
Wolfgang Betz 0:4fb29d9ee571 323 */
Wolfgang Betz 5:c9c5bc673c64 324 void attach_irq_callback(Callback<void()> func) {
Wolfgang Betz 4:07537ca85c66 325 _current_irq_callback = func;
Wolfgang Betz 0:4fb29d9ee571 326 }
Wolfgang Betz 3:0df38cfb1e53 327
Wolfgang Betz 4:07537ca85c66 328 /** Switch Radio On/Off **/
Wolfgang Betz 4:07537ca85c66 329 int on(void);
Wolfgang Betz 4:07537ca85c66 330 int off(void);
Wolfgang Betz 4:07537ca85c66 331
Wolfgang Betz 4:07537ca85c66 332 /** Prepare the radio with a packet to be sent. **/
Wolfgang Betz 5:c9c5bc673c64 333 int prepare_contiki(const void *payload, unsigned short payload_len);
Wolfgang Betz 3:0df38cfb1e53 334
Wolfgang Betz 4:07537ca85c66 335 /** Send the packet that has previously been prepared. **/
Wolfgang Betz 5:c9c5bc673c64 336 int transmit_contiki(unsigned short payload_len);
Wolfgang Betz 3:0df38cfb1e53 337
Wolfgang Betz 4:07537ca85c66 338 /** Prepare & Transmit */
Wolfgang Betz 5:c9c5bc673c64 339 int send_contiki(const void *payload, unsigned short payload_len) {
Wolfgang Betz 5:c9c5bc673c64 340 if(prepare_contiki(payload, payload_len) == RADIO_TX_ERR) {
Wolfgang Betz 4:07537ca85c66 341 return RADIO_TX_ERR;
Wolfgang Betz 4:07537ca85c66 342 }
Wolfgang Betz 5:c9c5bc673c64 343 return transmit_contiki(payload_len);
Wolfgang Betz 4:07537ca85c66 344 }
Wolfgang Betz 4:07537ca85c66 345
Wolfgang Betz 5:c9c5bc673c64 346 int send(const void *payload, unsigned short payload_len);
Wolfgang Betz 5:c9c5bc673c64 347
Wolfgang Betz 4:07537ca85c66 348 /** Read into Buffer **/
Wolfgang Betz 4:07537ca85c66 349 int read(void *buf, unsigned short bufsize);
Wolfgang Betz 4:07537ca85c66 350
Wolfgang Betz 4:07537ca85c66 351 /** Perform a Clear-Channel Assessment (CCA) to find out if there is
Wolfgang Betz 4:07537ca85c66 352 a packet in the air or not.
Wolfgang Betz 4:07537ca85c66 353 Returns 0 if packet has been seen.
Wolfgang Betz 4:07537ca85c66 354 */
Wolfgang Betz 4:07537ca85c66 355 int channel_clear(void);
Wolfgang Betz 4:07537ca85c66 356
Wolfgang Betz 4:07537ca85c66 357 /** Check if the radio driver is currently receiving a packet */
Wolfgang Betz 4:07537ca85c66 358 int incoming_packet(void);
Wolfgang Betz 4:07537ca85c66 359
Wolfgang Betz 4:07537ca85c66 360 /** Check if the radio driver has just received a packet **/
Wolfgang Betz 4:07537ca85c66 361 int pending_packet(void);
Wolfgang Betz 0:4fb29d9ee571 362 };