SX1272Lib updated in order to be RTOS aware

Fork of SX1272Lib by Semtech

Since Semtech original SX1272 library used InterruptIn and Timout mbed-os classes, whose ISRs are not allowed to lock RTOS mutexes, any SPI-related operation was doomed to fail. Indeed, SPI transactions functions are always nested inside a spi-level mutex lock/unlock pair in order to provide for thread access safety. A typical case occurs for example when radio is set to sleep state after a RX timeout.

This fork solves such problems by mean of a EventQueue/Thread pair, where any InterruptIn and Timeout ISRs actually enqueue callback calls.

Take a look at usage example at https://github.com/maiorfi/mbedos_lablet_lora_1

Committer:
Lorenzo Maiorfi
Date:
Sat Feb 24 08:47:30 2018 +0100
Revision:
10:bd29cdff8f3e
Parent:
8:1002d3025eaa
Child:
11:866b939cf709
Radio Events event queue and related thread should now be passed externally (e.g. by main()) as pointers. This frees user code from locking/unlocking through mutex in order to implement an async state machine interlaced with radio events

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:45c4f0364ca4 1 /*
mluis 0:45c4f0364ca4 2 / _____) _ | |
mluis 0:45c4f0364ca4 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:45c4f0364ca4 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:45c4f0364ca4 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:45c4f0364ca4 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:45c4f0364ca4 7 (C) 2015 Semtech
mluis 0:45c4f0364ca4 8
mluis 0:45c4f0364ca4 9 Description: Actual implementation of a SX1272 radio, inherits Radio
mluis 0:45c4f0364ca4 10
mluis 0:45c4f0364ca4 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:45c4f0364ca4 12
mluis 0:45c4f0364ca4 13 Maintainers: Miguel Luis, Gregory Cristian and Nicolas Huguenin
mluis 0:45c4f0364ca4 14 */
mluis 0:45c4f0364ca4 15 #ifndef __SX1272_H__
mluis 0:45c4f0364ca4 16 #define __SX1272_H__
mluis 0:45c4f0364ca4 17
mluis 0:45c4f0364ca4 18 #include "radio.h"
mluis 0:45c4f0364ca4 19 #include "./registers/sx1272Regs-Fsk.h"
mluis 0:45c4f0364ca4 20 #include "./registers/sx1272Regs-LoRa.h"
mluis 0:45c4f0364ca4 21 #include "./typedefs/typedefs.h"
mluis 0:45c4f0364ca4 22
mluis 0:45c4f0364ca4 23 /*!
mluis 7:b988b60083a1 24 * Radio wake-up time from sleep
mluis 7:b988b60083a1 25 */
mluis 7:b988b60083a1 26 #define RADIO_WAKEUP_TIME 1 // [ms]
mluis 7:b988b60083a1 27
mluis 7:b988b60083a1 28 /*!
mluis 7:b988b60083a1 29 * Sync word for Private LoRa networks
mluis 0:45c4f0364ca4 30 */
mluis 7:b988b60083a1 31 #define LORA_MAC_PRIVATE_SYNCWORD 0x12
mluis 7:b988b60083a1 32
mluis 7:b988b60083a1 33 /*!
mluis 7:b988b60083a1 34 * Sync word for Public LoRa networks
mluis 7:b988b60083a1 35 */
mluis 7:b988b60083a1 36 #define LORA_MAC_PUBLIC_SYNCWORD 0x34
mluis 7:b988b60083a1 37
mluis 0:45c4f0364ca4 38
mluis 0:45c4f0364ca4 39 /*!
mluis 0:45c4f0364ca4 40 * SX1272 definitions
mluis 0:45c4f0364ca4 41 */
mluis 0:45c4f0364ca4 42 #define XTAL_FREQ 32000000
mluis 0:45c4f0364ca4 43 #define FREQ_STEP 61.03515625
mluis 0:45c4f0364ca4 44
mluis 0:45c4f0364ca4 45 #define RX_BUFFER_SIZE 256
mluis 0:45c4f0364ca4 46
mluis 0:45c4f0364ca4 47 /*!
mluis 0:45c4f0364ca4 48 * Constant values need to compute the RSSI value
mluis 0:45c4f0364ca4 49 */
mluis 0:45c4f0364ca4 50 #define RSSI_OFFSET -139
mluis 0:45c4f0364ca4 51
mluis 0:45c4f0364ca4 52 /*!
mluis 0:45c4f0364ca4 53 * Actual implementation of a SX1272 radio, inherits Radio
mluis 0:45c4f0364ca4 54 */
mluis 0:45c4f0364ca4 55 class SX1272 : public Radio
mluis 0:45c4f0364ca4 56 {
mluis 0:45c4f0364ca4 57 protected:
mluis 0:45c4f0364ca4 58 /*!
mluis 0:45c4f0364ca4 59 * SPI Interface
mluis 0:45c4f0364ca4 60 */
mluis 0:45c4f0364ca4 61 SPI spi; // mosi, miso, sclk
mluis 0:45c4f0364ca4 62 DigitalOut nss;
mluis 0:45c4f0364ca4 63
mluis 0:45c4f0364ca4 64 /*!
mluis 0:45c4f0364ca4 65 * SX1272 Reset pin
mluis 0:45c4f0364ca4 66 */
mluis 0:45c4f0364ca4 67 DigitalInOut reset;
mluis 0:45c4f0364ca4 68
mluis 0:45c4f0364ca4 69 /*!
mluis 0:45c4f0364ca4 70 * SX1272 DIO pins
mluis 0:45c4f0364ca4 71 */
mluis 0:45c4f0364ca4 72 InterruptIn dio0;
mluis 0:45c4f0364ca4 73 InterruptIn dio1;
mluis 0:45c4f0364ca4 74 InterruptIn dio2;
mluis 0:45c4f0364ca4 75 InterruptIn dio3;
mluis 0:45c4f0364ca4 76 InterruptIn dio4;
mluis 0:45c4f0364ca4 77 DigitalIn dio5;
mluis 7:b988b60083a1 78
mluis 0:45c4f0364ca4 79 bool isRadioActive;
mluis 7:b988b60083a1 80
mluis 0:45c4f0364ca4 81 uint8_t boardConnected; //1 = SX1272MB1DCS; 0 = SX1272MB1DAS
mluis 7:b988b60083a1 82
mluis 7:b988b60083a1 83 uint8_t *rxtxBuffer;
mluis 7:b988b60083a1 84
mluis 0:45c4f0364ca4 85 /*!
mluis 0:45c4f0364ca4 86 * Hardware DIO IRQ functions
mluis 0:45c4f0364ca4 87 */
mluis 0:45c4f0364ca4 88 DioIrqHandler *dioIrq;
mluis 7:b988b60083a1 89
mluis 0:45c4f0364ca4 90 /*!
mluis 0:45c4f0364ca4 91 * Tx and Rx timers
mluis 0:45c4f0364ca4 92 */
mluis 0:45c4f0364ca4 93 Timeout txTimeoutTimer;
mluis 0:45c4f0364ca4 94 Timeout rxTimeoutTimer;
mluis 0:45c4f0364ca4 95 Timeout rxTimeoutSyncWord;
mluis 7:b988b60083a1 96
mluis 0:45c4f0364ca4 97 RadioSettings_t settings;
mluis 7:b988b60083a1 98
mluis 7:b988b60083a1 99 static const FskBandwidth_t FskBandwidths[];
Lorenzo Maiorfi 8:1002d3025eaa 100
Lorenzo Maiorfi 8:1002d3025eaa 101 // <RTOS>
Lorenzo Maiorfi 10:bd29cdff8f3e 102 Thread* _thread_events_queue;
Lorenzo Maiorfi 10:bd29cdff8f3e 103 EventQueue* _eq_events;
Lorenzo Maiorfi 8:1002d3025eaa 104 // </RTOS>
Lorenzo Maiorfi 8:1002d3025eaa 105
mluis 0:45c4f0364ca4 106 protected:
mluis 0:45c4f0364ca4 107
mluis 0:45c4f0364ca4 108 /*!
mluis 0:45c4f0364ca4 109 * Performs the Rx chain calibration for LF and HF bands
mluis 0:45c4f0364ca4 110 * \remark Must be called just after the reset so all registers are at their
mluis 0:45c4f0364ca4 111 * default values
mluis 0:45c4f0364ca4 112 */
mluis 0:45c4f0364ca4 113 void RxChainCalibration( void );
mluis 0:45c4f0364ca4 114
mluis 0:45c4f0364ca4 115 public:
mluis 0:45c4f0364ca4 116 SX1272( RadioEvents_t *events,
mluis 0:45c4f0364ca4 117 PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
mluis 7:b988b60083a1 118 PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 );
mluis 0:45c4f0364ca4 119 SX1272( RadioEvents_t *events );
mluis 0:45c4f0364ca4 120 virtual ~SX1272( );
Lorenzo Maiorfi 10:bd29cdff8f3e 121
Lorenzo Maiorfi 10:bd29cdff8f3e 122 // <RTOS>
Lorenzo Maiorfi 10:bd29cdff8f3e 123 void assign_events_queue_thread(Thread* thread) {_thread_events_queue=thread;}
Lorenzo Maiorfi 10:bd29cdff8f3e 124 void assign_events_queue(EventQueue* event_queue) {_eq_events=event_queue;}
Lorenzo Maiorfi 10:bd29cdff8f3e 125 // </RTOS>
mluis 0:45c4f0364ca4 126
mluis 0:45c4f0364ca4 127 //-------------------------------------------------------------------------
mluis 0:45c4f0364ca4 128 // Redefined Radio functions
mluis 0:45c4f0364ca4 129 //-------------------------------------------------------------------------
mluis 0:45c4f0364ca4 130 /*!
mluis 0:45c4f0364ca4 131 * @brief Initializes the radio
mluis 0:45c4f0364ca4 132 *
mluis 0:45c4f0364ca4 133 * @param [IN] events Structure containing the driver callback functions
mluis 0:45c4f0364ca4 134 */
mluis 0:45c4f0364ca4 135 virtual void Init( RadioEvents_t *events );
mluis 0:45c4f0364ca4 136 /*!
mluis 0:45c4f0364ca4 137 * Return current radio status
mluis 0:45c4f0364ca4 138 *
mluis 0:45c4f0364ca4 139 * @param status Radio status. [RF_IDLE, RX_RUNNING, TX_RUNNING]
mluis 0:45c4f0364ca4 140 */
mluis 0:45c4f0364ca4 141 virtual RadioState GetStatus( void );
mluis 0:45c4f0364ca4 142 /*!
mluis 0:45c4f0364ca4 143 * @brief Configures the SX1272 with the given modem
mluis 0:45c4f0364ca4 144 *
mluis 0:45c4f0364ca4 145 * @param [IN] modem Modem to be used [0: FSK, 1: LoRa]
mluis 0:45c4f0364ca4 146 */
mluis 0:45c4f0364ca4 147 virtual void SetModem( RadioModems_t modem );
mluis 0:45c4f0364ca4 148 /*!
mluis 0:45c4f0364ca4 149 * @brief Sets the channel frequency
mluis 0:45c4f0364ca4 150 *
mluis 0:45c4f0364ca4 151 * @param [IN] freq Channel RF frequency
mluis 0:45c4f0364ca4 152 */
mluis 0:45c4f0364ca4 153 virtual void SetChannel( uint32_t freq );
mluis 0:45c4f0364ca4 154 /*!
mluis 0:45c4f0364ca4 155 * @brief Sets the channels configuration
mluis 0:45c4f0364ca4 156 *
mluis 0:45c4f0364ca4 157 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
mluis 0:45c4f0364ca4 158 * @param [IN] freq Channel RF frequency
mluis 0:45c4f0364ca4 159 * @param [IN] rssiThresh RSSI threshold
mluis 0:45c4f0364ca4 160 *
mluis 0:45c4f0364ca4 161 * @retval isFree [true: Channel is free, false: Channel is not free]
mluis 0:45c4f0364ca4 162 */
mluis 0:45c4f0364ca4 163 virtual bool IsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh );
mluis 0:45c4f0364ca4 164 /*!
mluis 0:45c4f0364ca4 165 * @brief Generates a 32 bits random value based on the RSSI readings
mluis 0:45c4f0364ca4 166 *
mluis 0:45c4f0364ca4 167 * \remark This function sets the radio in LoRa modem mode and disables
mluis 0:45c4f0364ca4 168 * all interrupts.
mluis 0:45c4f0364ca4 169 * After calling this function either Radio.SetRxConfig or
mluis 0:45c4f0364ca4 170 * Radio.SetTxConfig functions must be called.
mluis 0:45c4f0364ca4 171 *
mluis 0:45c4f0364ca4 172 * @retval randomValue 32 bits random value
mluis 0:45c4f0364ca4 173 */
mluis 0:45c4f0364ca4 174 virtual uint32_t Random( void );
mluis 0:45c4f0364ca4 175 /*!
mluis 0:45c4f0364ca4 176 * @brief Sets the reception parameters
mluis 0:45c4f0364ca4 177 *
mluis 0:45c4f0364ca4 178 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
mluis 0:45c4f0364ca4 179 * @param [IN] bandwidth Sets the bandwidth
mluis 0:45c4f0364ca4 180 * FSK : >= 2600 and <= 250000 Hz
mluis 0:45c4f0364ca4 181 * LoRa: [0: 125 kHz, 1: 250 kHz,
mluis 0:45c4f0364ca4 182 * 2: 500 kHz, 3: Reserved]
mluis 0:45c4f0364ca4 183 * @param [IN] datarate Sets the Datarate
mluis 0:45c4f0364ca4 184 * FSK : 600..300000 bits/s
mluis 0:45c4f0364ca4 185 * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
mluis 0:45c4f0364ca4 186 * 10: 1024, 11: 2048, 12: 4096 chips]
mluis 0:45c4f0364ca4 187 * @param [IN] coderate Sets the coding rate ( LoRa only )
mluis 0:45c4f0364ca4 188 * FSK : N/A ( set to 0 )
mluis 0:45c4f0364ca4 189 * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
mluis 0:45c4f0364ca4 190 * @param [IN] bandwidthAfc Sets the AFC Bandwidth ( FSK only )
mluis 0:45c4f0364ca4 191 * FSK : >= 2600 and <= 250000 Hz
mluis 0:45c4f0364ca4 192 * LoRa: N/A ( set to 0 )
mluis 0:45c4f0364ca4 193 * @param [IN] preambleLen Sets the Preamble length ( LoRa only )
mluis 0:45c4f0364ca4 194 * FSK : N/A ( set to 0 )
mluis 0:45c4f0364ca4 195 * LoRa: Length in symbols ( the hardware adds 4 more symbols )
mluis 7:b988b60083a1 196 * @param [IN] symbTimeout Sets the RxSingle timeout value
mluis 7:b988b60083a1 197 * FSK : timeout number of bytes
mluis 0:45c4f0364ca4 198 * LoRa: timeout in symbols
mluis 0:45c4f0364ca4 199 * @param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
mluis 0:45c4f0364ca4 200 * @param [IN] payloadLen Sets payload length when fixed lenght is used
mluis 0:45c4f0364ca4 201 * @param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
mluis 0:45c4f0364ca4 202 * @param [IN] freqHopOn Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
mluis 0:45c4f0364ca4 203 * @param [IN] hopPeriod Number of symbols bewteen each hop (LoRa only)
mluis 0:45c4f0364ca4 204 * @param [IN] iqInverted Inverts IQ signals ( LoRa only )
mluis 0:45c4f0364ca4 205 * FSK : N/A ( set to 0 )
mluis 0:45c4f0364ca4 206 * LoRa: [0: not inverted, 1: inverted]
mluis 0:45c4f0364ca4 207 * @param [IN] rxContinuous Sets the reception in continuous mode
mluis 0:45c4f0364ca4 208 * [false: single mode, true: continuous mode]
mluis 0:45c4f0364ca4 209 */
mluis 0:45c4f0364ca4 210 virtual void SetRxConfig ( RadioModems_t modem, uint32_t bandwidth,
mluis 0:45c4f0364ca4 211 uint32_t datarate, uint8_t coderate,
mluis 0:45c4f0364ca4 212 uint32_t bandwidthAfc, uint16_t preambleLen,
mluis 0:45c4f0364ca4 213 uint16_t symbTimeout, bool fixLen,
mluis 0:45c4f0364ca4 214 uint8_t payloadLen,
mluis 0:45c4f0364ca4 215 bool crcOn, bool freqHopOn, uint8_t hopPeriod,
mluis 0:45c4f0364ca4 216 bool iqInverted, bool rxContinuous );
mluis 0:45c4f0364ca4 217 /*!
mluis 0:45c4f0364ca4 218 * @brief Sets the transmission parameters
mluis 0:45c4f0364ca4 219 *
mluis 0:45c4f0364ca4 220 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
mluis 0:45c4f0364ca4 221 * @param [IN] power Sets the output power [dBm]
mluis 0:45c4f0364ca4 222 * @param [IN] fdev Sets the frequency deviation ( FSK only )
mluis 0:45c4f0364ca4 223 * FSK : [Hz]
mluis 0:45c4f0364ca4 224 * LoRa: 0
mluis 0:45c4f0364ca4 225 * @param [IN] bandwidth Sets the bandwidth ( LoRa only )
mluis 0:45c4f0364ca4 226 * FSK : 0
mluis 0:45c4f0364ca4 227 * LoRa: [0: 125 kHz, 1: 250 kHz,
mluis 0:45c4f0364ca4 228 * 2: 500 kHz, 3: Reserved]
mluis 0:45c4f0364ca4 229 * @param [IN] datarate Sets the Datarate
mluis 0:45c4f0364ca4 230 * FSK : 600..300000 bits/s
mluis 0:45c4f0364ca4 231 * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
mluis 0:45c4f0364ca4 232 * 10: 1024, 11: 2048, 12: 4096 chips]
mluis 0:45c4f0364ca4 233 * @param [IN] coderate Sets the coding rate ( LoRa only )
mluis 0:45c4f0364ca4 234 * FSK : N/A ( set to 0 )
mluis 0:45c4f0364ca4 235 * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
mluis 0:45c4f0364ca4 236 * @param [IN] preambleLen Sets the preamble length
mluis 0:45c4f0364ca4 237 * @param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
mluis 0:45c4f0364ca4 238 * @param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
mluis 0:45c4f0364ca4 239 * @param [IN] freqHopOn Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
mluis 0:45c4f0364ca4 240 * @param [IN] hopPeriod Number of symbols bewteen each hop (LoRa only)
mluis 0:45c4f0364ca4 241 * @param [IN] iqInverted Inverts IQ signals ( LoRa only )
mluis 0:45c4f0364ca4 242 * FSK : N/A ( set to 0 )
mluis 0:45c4f0364ca4 243 * LoRa: [0: not inverted, 1: inverted]
mluis 7:b988b60083a1 244 * @param [IN] timeout Transmission timeout [ms]
mluis 0:45c4f0364ca4 245 */
mluis 0:45c4f0364ca4 246 virtual void SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
mluis 0:45c4f0364ca4 247 uint32_t bandwidth, uint32_t datarate,
mluis 0:45c4f0364ca4 248 uint8_t coderate, uint16_t preambleLen,
mluis 0:45c4f0364ca4 249 bool fixLen, bool crcOn, bool freqHopOn,
mluis 0:45c4f0364ca4 250 uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
mluis 7:b988b60083a1 251 /*!
mluis 7:b988b60083a1 252 * @brief Checks if the given RF frequency is supported by the hardware
mluis 7:b988b60083a1 253 *
mluis 7:b988b60083a1 254 * @param [IN] frequency RF frequency to be checked
mluis 7:b988b60083a1 255 * @retval isSupported [true: supported, false: unsupported]
mluis 7:b988b60083a1 256 */
mluis 7:b988b60083a1 257 virtual bool CheckRfFrequency( uint32_t frequency ) = 0;
mluis 0:45c4f0364ca4 258 /*!
mluis 0:45c4f0364ca4 259 * @brief Computes the packet time on air for the given payload
mluis 0:45c4f0364ca4 260 *
mluis 0:45c4f0364ca4 261 * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
mluis 0:45c4f0364ca4 262 *
mluis 0:45c4f0364ca4 263 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
mluis 0:45c4f0364ca4 264 * @param [IN] pktLen Packet payload length
mluis 0:45c4f0364ca4 265 *
mluis 0:45c4f0364ca4 266 * @retval airTime Computed airTime for the given packet payload length
mluis 0:45c4f0364ca4 267 */
mluis 7:b988b60083a1 268 virtual uint32_t TimeOnAir ( RadioModems_t modem, uint8_t pktLen );
mluis 0:45c4f0364ca4 269 /*!
mluis 0:45c4f0364ca4 270 * @brief Sends the buffer of size. Prepares the packet to be sent and sets
mluis 0:45c4f0364ca4 271 * the radio in transmission
mluis 0:45c4f0364ca4 272 *
mluis 0:45c4f0364ca4 273 * @param [IN]: buffer Buffer pointer
mluis 0:45c4f0364ca4 274 * @param [IN]: size Buffer size
mluis 0:45c4f0364ca4 275 */
mluis 0:45c4f0364ca4 276 virtual void Send( uint8_t *buffer, uint8_t size );
mluis 0:45c4f0364ca4 277 /*!
mluis 0:45c4f0364ca4 278 * @brief Sets the radio in sleep mode
mluis 0:45c4f0364ca4 279 */
mluis 0:45c4f0364ca4 280 virtual void Sleep( void );
mluis 0:45c4f0364ca4 281 /*!
mluis 0:45c4f0364ca4 282 * @brief Sets the radio in standby mode
mluis 0:45c4f0364ca4 283 */
mluis 0:45c4f0364ca4 284 virtual void Standby( void );
mluis 7:b988b60083a1 285 /*!
mluis 7:b988b60083a1 286 * @brief Sets the radio in CAD mode
mluis 7:b988b60083a1 287 */
mluis 7:b988b60083a1 288 virtual void StartCad( void );
mluis 0:45c4f0364ca4 289 /*!
mluis 0:45c4f0364ca4 290 * @brief Sets the radio in reception mode for the given time
mluis 7:b988b60083a1 291 * @param [IN] timeout Reception timeout [ms]
mluis 0:45c4f0364ca4 292 * [0: continuous, others timeout]
mluis 0:45c4f0364ca4 293 */
mluis 0:45c4f0364ca4 294 virtual void Rx( uint32_t timeout );
mluis 0:45c4f0364ca4 295 /*!
mluis 0:45c4f0364ca4 296 * @brief Sets the radio in transmission mode for the given time
mluis 7:b988b60083a1 297 * @param [IN] timeout Transmission timeout [ms]
mluis 0:45c4f0364ca4 298 * [0: continuous, others timeout]
mluis 0:45c4f0364ca4 299 */
mluis 0:45c4f0364ca4 300 virtual void Tx( uint32_t timeout );
mluis 0:45c4f0364ca4 301 /*!
mluis 7:b988b60083a1 302 * @brief Sets the radio in continuous wave transmission mode
mluis 7:b988b60083a1 303 *
mluis 7:b988b60083a1 304 * @param [IN]: freq Channel RF frequency
mluis 7:b988b60083a1 305 * @param [IN]: power Sets the output power [dBm]
mluis 7:b988b60083a1 306 * @param [IN]: time Transmission mode timeout [s]
mluis 0:45c4f0364ca4 307 */
mluis 7:b988b60083a1 308 virtual void SetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time );
mluis 0:45c4f0364ca4 309 /*!
mluis 0:45c4f0364ca4 310 * @brief Reads the current RSSI value
mluis 0:45c4f0364ca4 311 *
mluis 0:45c4f0364ca4 312 * @retval rssiValue Current RSSI value in [dBm]
mluis 0:45c4f0364ca4 313 */
mluis 0:45c4f0364ca4 314 virtual int16_t GetRssi ( RadioModems_t modem );
mluis 0:45c4f0364ca4 315 /*!
mluis 0:45c4f0364ca4 316 * @brief Writes the radio register at the specified address
mluis 0:45c4f0364ca4 317 *
mluis 0:45c4f0364ca4 318 * @param [IN]: addr Register address
mluis 0:45c4f0364ca4 319 * @param [IN]: data New register value
mluis 0:45c4f0364ca4 320 */
mluis 0:45c4f0364ca4 321 virtual void Write ( uint8_t addr, uint8_t data ) = 0;
mluis 0:45c4f0364ca4 322 /*!
mluis 0:45c4f0364ca4 323 * @brief Reads the radio register at the specified address
mluis 0:45c4f0364ca4 324 *
mluis 0:45c4f0364ca4 325 * @param [IN]: addr Register address
mluis 0:45c4f0364ca4 326 * @retval data Register value
mluis 0:45c4f0364ca4 327 */
mluis 0:45c4f0364ca4 328 virtual uint8_t Read ( uint8_t addr ) = 0;
mluis 0:45c4f0364ca4 329 /*!
mluis 0:45c4f0364ca4 330 * @brief Writes multiple radio registers starting at address
mluis 0:45c4f0364ca4 331 *
mluis 0:45c4f0364ca4 332 * @param [IN] addr First Radio register address
mluis 0:45c4f0364ca4 333 * @param [IN] buffer Buffer containing the new register's values
mluis 0:45c4f0364ca4 334 * @param [IN] size Number of registers to be written
mluis 0:45c4f0364ca4 335 */
mluis 0:45c4f0364ca4 336 virtual void Write( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
mluis 0:45c4f0364ca4 337 /*!
mluis 0:45c4f0364ca4 338 * @brief Reads multiple radio registers starting at address
mluis 0:45c4f0364ca4 339 *
mluis 0:45c4f0364ca4 340 * @param [IN] addr First Radio register address
mluis 0:45c4f0364ca4 341 * @param [OUT] buffer Buffer where to copy the registers data
mluis 0:45c4f0364ca4 342 * @param [IN] size Number of registers to be read
mluis 0:45c4f0364ca4 343 */
mluis 0:45c4f0364ca4 344 virtual void Read ( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
mluis 0:45c4f0364ca4 345 /*!
mluis 0:45c4f0364ca4 346 * @brief Writes the buffer contents to the SX1272 FIFO
mluis 0:45c4f0364ca4 347 *
mluis 0:45c4f0364ca4 348 * @param [IN] buffer Buffer containing data to be put on the FIFO.
mluis 0:45c4f0364ca4 349 * @param [IN] size Number of bytes to be written to the FIFO
mluis 0:45c4f0364ca4 350 */
mluis 0:45c4f0364ca4 351 virtual void WriteFifo( uint8_t *buffer, uint8_t size ) = 0;
mluis 0:45c4f0364ca4 352 /*!
mluis 0:45c4f0364ca4 353 * @brief Reads the contents of the SX1272 FIFO
mluis 0:45c4f0364ca4 354 *
mluis 0:45c4f0364ca4 355 * @param [OUT] buffer Buffer where to copy the FIFO read data.
mluis 0:45c4f0364ca4 356 * @param [IN] size Number of bytes to be read from the FIFO
mluis 0:45c4f0364ca4 357 */
mluis 0:45c4f0364ca4 358 virtual void ReadFifo( uint8_t *buffer, uint8_t size ) = 0;
mluis 0:45c4f0364ca4 359 /*!
mluis 0:45c4f0364ca4 360 * @brief Resets the SX1272
mluis 0:45c4f0364ca4 361 */
mluis 0:45c4f0364ca4 362 virtual void Reset( void ) = 0;
mluis 7:b988b60083a1 363
mluis 0:45c4f0364ca4 364 /*!
mluis 0:45c4f0364ca4 365 * @brief Sets the maximum payload length.
mluis 0:45c4f0364ca4 366 *
mluis 0:45c4f0364ca4 367 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
mluis 0:45c4f0364ca4 368 * @param [IN] max Maximum payload length in bytes
mluis 0:45c4f0364ca4 369 */
mluis 0:45c4f0364ca4 370 virtual void SetMaxPayloadLength( RadioModems_t modem, uint8_t max );
mluis 7:b988b60083a1 371
mluis 7:b988b60083a1 372 /*!
mluis 7:b988b60083a1 373 * \brief Sets the network to public or private. Updates the sync byte.
mluis 7:b988b60083a1 374 *
mluis 7:b988b60083a1 375 * \remark Applies to LoRa modem only
mluis 7:b988b60083a1 376 *
mluis 7:b988b60083a1 377 * \param [IN] enable if true, it enables a public network
mluis 7:b988b60083a1 378 */
mluis 7:b988b60083a1 379 virtual void SetPublicNetwork( bool enable );
mluis 7:b988b60083a1 380
mluis 0:45c4f0364ca4 381 //-------------------------------------------------------------------------
mluis 0:45c4f0364ca4 382 // Board relative functions
mluis 0:45c4f0364ca4 383 //-------------------------------------------------------------------------
mluis 7:b988b60083a1 384
mluis 0:45c4f0364ca4 385 protected:
mluis 0:45c4f0364ca4 386 /*!
mluis 0:45c4f0364ca4 387 * @brief Initializes the radio I/Os pins interface
mluis 0:45c4f0364ca4 388 */
mluis 0:45c4f0364ca4 389 virtual void IoInit( void ) = 0;
mluis 0:45c4f0364ca4 390
mluis 0:45c4f0364ca4 391 /*!
mluis 0:45c4f0364ca4 392 * @brief Initializes the radio registers
mluis 0:45c4f0364ca4 393 */
mluis 0:45c4f0364ca4 394 virtual void RadioRegistersInit( ) = 0;
mluis 0:45c4f0364ca4 395
mluis 0:45c4f0364ca4 396 /*!
mluis 0:45c4f0364ca4 397 * @brief Initializes the radio SPI
mluis 0:45c4f0364ca4 398 */
mluis 0:45c4f0364ca4 399 virtual void SpiInit( void ) = 0;
mluis 7:b988b60083a1 400
mluis 0:45c4f0364ca4 401 /*!
mluis 0:45c4f0364ca4 402 * @brief Initializes DIO IRQ handlers
mluis 0:45c4f0364ca4 403 *
mluis 0:45c4f0364ca4 404 * @param [IN] irqHandlers Array containing the IRQ callback functions
mluis 0:45c4f0364ca4 405 */
mluis 0:45c4f0364ca4 406 virtual void IoIrqInit( DioIrqHandler *irqHandlers ) = 0;
mluis 0:45c4f0364ca4 407
mluis 0:45c4f0364ca4 408 /*!
mluis 0:45c4f0364ca4 409 * @brief De-initializes the radio I/Os pins interface.
mluis 0:45c4f0364ca4 410 *
mluis 0:45c4f0364ca4 411 * \remark Useful when going in MCU lowpower modes
mluis 0:45c4f0364ca4 412 */
mluis 0:45c4f0364ca4 413 virtual void IoDeInit( void ) = 0;
mluis 0:45c4f0364ca4 414
mluis 0:45c4f0364ca4 415 /*!
mluis 7:b988b60083a1 416 * @brief Sets the radio output power.
mluis 7:b988b60083a1 417 *
mluis 7:b988b60083a1 418 * @param [IN] power Sets the RF output power
mluis 7:b988b60083a1 419 */
mluis 7:b988b60083a1 420 virtual void SetRfTxPower( int8_t power ) = 0;
mluis 7:b988b60083a1 421
mluis 7:b988b60083a1 422 /*!
mluis 0:45c4f0364ca4 423 * @brief Gets the board PA selection configuration
mluis 0:45c4f0364ca4 424 *
mluis 0:45c4f0364ca4 425 * @param [IN] channel Channel frequency in Hz
mluis 0:45c4f0364ca4 426 * @retval PaSelect RegPaConfig PaSelect value
mluis 0:45c4f0364ca4 427 */
mluis 0:45c4f0364ca4 428 virtual uint8_t GetPaSelect( uint32_t channel ) = 0;
mluis 0:45c4f0364ca4 429
mluis 0:45c4f0364ca4 430 /*!
mluis 0:45c4f0364ca4 431 * @brief Set the RF Switch I/Os pins in Low Power mode
mluis 0:45c4f0364ca4 432 *
mluis 0:45c4f0364ca4 433 * @param [IN] status enable or disable
mluis 0:45c4f0364ca4 434 */
mluis 0:45c4f0364ca4 435 virtual void SetAntSwLowPower( bool status ) = 0;
mluis 0:45c4f0364ca4 436
mluis 0:45c4f0364ca4 437 /*!
mluis 0:45c4f0364ca4 438 * @brief Initializes the RF Switch I/Os pins interface
mluis 0:45c4f0364ca4 439 */
mluis 0:45c4f0364ca4 440 virtual void AntSwInit( void ) = 0;
mluis 0:45c4f0364ca4 441
mluis 0:45c4f0364ca4 442 /*!
mluis 0:45c4f0364ca4 443 * @brief De-initializes the RF Switch I/Os pins interface
mluis 0:45c4f0364ca4 444 *
mluis 0:45c4f0364ca4 445 * \remark Needed to decrease the power consumption in MCU lowpower modes
mluis 0:45c4f0364ca4 446 */
mluis 0:45c4f0364ca4 447 virtual void AntSwDeInit( void ) = 0;
mluis 0:45c4f0364ca4 448
mluis 0:45c4f0364ca4 449 /*!
mluis 7:b988b60083a1 450 * @brief Controls the antenna switch if necessary.
mluis 0:45c4f0364ca4 451 *
mluis 0:45c4f0364ca4 452 * \remark see errata note
mluis 0:45c4f0364ca4 453 *
mluis 7:b988b60083a1 454 * @param [IN] opMode Current radio operating mode
mluis 0:45c4f0364ca4 455 */
mluis 7:b988b60083a1 456 virtual void SetAntSw( uint8_t opMode ) = 0;
mluis 0:45c4f0364ca4 457 protected:
mluis 0:45c4f0364ca4 458
mluis 0:45c4f0364ca4 459 /*!
mluis 0:45c4f0364ca4 460 * @brief Sets the SX1272 operating mode
mluis 0:45c4f0364ca4 461 *
mluis 0:45c4f0364ca4 462 * @param [IN] opMode New operating mode
mluis 0:45c4f0364ca4 463 */
mluis 0:45c4f0364ca4 464 virtual void SetOpMode( uint8_t opMode );
mluis 0:45c4f0364ca4 465
mluis 0:45c4f0364ca4 466 /*
mluis 0:45c4f0364ca4 467 * SX1272 DIO IRQ callback functions prototype
mluis 0:45c4f0364ca4 468 */
mluis 0:45c4f0364ca4 469
mluis 0:45c4f0364ca4 470 /*!
mluis 0:45c4f0364ca4 471 * @brief DIO 0 IRQ callback
mluis 0:45c4f0364ca4 472 */
mluis 0:45c4f0364ca4 473 virtual void OnDio0Irq( void );
mluis 0:45c4f0364ca4 474
mluis 0:45c4f0364ca4 475 /*!
mluis 0:45c4f0364ca4 476 * @brief DIO 1 IRQ callback
mluis 0:45c4f0364ca4 477 */
mluis 0:45c4f0364ca4 478 virtual void OnDio1Irq( void );
mluis 0:45c4f0364ca4 479
mluis 0:45c4f0364ca4 480 /*!
mluis 0:45c4f0364ca4 481 * @brief DIO 2 IRQ callback
mluis 0:45c4f0364ca4 482 */
mluis 0:45c4f0364ca4 483 virtual void OnDio2Irq( void );
mluis 0:45c4f0364ca4 484
mluis 0:45c4f0364ca4 485 /*!
mluis 0:45c4f0364ca4 486 * @brief DIO 3 IRQ callback
mluis 0:45c4f0364ca4 487 */
mluis 0:45c4f0364ca4 488 virtual void OnDio3Irq( void );
mluis 0:45c4f0364ca4 489
mluis 0:45c4f0364ca4 490 /*!
mluis 0:45c4f0364ca4 491 * @brief DIO 4 IRQ callback
mluis 0:45c4f0364ca4 492 */
mluis 0:45c4f0364ca4 493 virtual void OnDio4Irq( void );
mluis 0:45c4f0364ca4 494
mluis 0:45c4f0364ca4 495 /*!
mluis 0:45c4f0364ca4 496 * @brief DIO 5 IRQ callback
mluis 0:45c4f0364ca4 497 */
mluis 0:45c4f0364ca4 498 virtual void OnDio5Irq( void );
mluis 0:45c4f0364ca4 499
mluis 0:45c4f0364ca4 500 /*!
mluis 0:45c4f0364ca4 501 * @brief Tx & Rx timeout timer callback
mluis 0:45c4f0364ca4 502 */
mluis 0:45c4f0364ca4 503 virtual void OnTimeoutIrq( void );
mluis 7:b988b60083a1 504
mluis 0:45c4f0364ca4 505 /*!
mluis 0:45c4f0364ca4 506 * Returns the known FSK bandwidth registers value
mluis 0:45c4f0364ca4 507 *
mluis 0:45c4f0364ca4 508 * \param [IN] bandwidth Bandwidth value in Hz
mluis 0:45c4f0364ca4 509 * \retval regValue Bandwidth register value.
mluis 0:45c4f0364ca4 510 */
mluis 0:45c4f0364ca4 511 static uint8_t GetFskBandwidthRegValue( uint32_t bandwidth );
Lorenzo Maiorfi 8:1002d3025eaa 512
Lorenzo Maiorfi 8:1002d3025eaa 513 // Inizio nuova parte per RTOS
Lorenzo Maiorfi 8:1002d3025eaa 514
Lorenzo Maiorfi 8:1002d3025eaa 515 virtual void enqueueOnTimeoutIrq();
Lorenzo Maiorfi 8:1002d3025eaa 516
Lorenzo Maiorfi 8:1002d3025eaa 517 virtual void enqueueRadioEvent_RxTimeout();
Lorenzo Maiorfi 8:1002d3025eaa 518 virtual void enqueueRadioEvent_TxTimeout();
Lorenzo Maiorfi 8:1002d3025eaa 519 virtual void enqueueRadioEvent_RxError();
Lorenzo Maiorfi 8:1002d3025eaa 520 virtual void enqueueRadioEvent_RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
Lorenzo Maiorfi 8:1002d3025eaa 521 virtual void enqueueRadioEvent_TxDone();
Lorenzo Maiorfi 8:1002d3025eaa 522 virtual void enqueueRadioEvent_FhssChangeChannel(uint8_t currentChannel);
Lorenzo Maiorfi 8:1002d3025eaa 523 virtual void enqueueRadioEvent_CadDone(bool channelActivityDetected);
Lorenzo Maiorfi 8:1002d3025eaa 524 virtual void enqueueOnDio0Irq();
Lorenzo Maiorfi 8:1002d3025eaa 525 virtual void enqueueOnDio1Irq();
Lorenzo Maiorfi 8:1002d3025eaa 526 virtual void enqueueOnDio2Irq();
Lorenzo Maiorfi 8:1002d3025eaa 527 virtual void enqueueOnDio3Irq();
Lorenzo Maiorfi 8:1002d3025eaa 528 virtual void enqueueOnDio4Irq();
Lorenzo Maiorfi 8:1002d3025eaa 529 virtual void enqueueOnDio5Irq();
Lorenzo Maiorfi 8:1002d3025eaa 530
Lorenzo Maiorfi 8:1002d3025eaa 531 private:
Lorenzo Maiorfi 8:1002d3025eaa 532
Lorenzo Maiorfi 8:1002d3025eaa 533 void radioEvent_RxTimeout();
Lorenzo Maiorfi 8:1002d3025eaa 534 void radioEvent_TxTimeout();
Lorenzo Maiorfi 8:1002d3025eaa 535 void radioEvent_RxError();
Lorenzo Maiorfi 8:1002d3025eaa 536 void radioEvent_RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
Lorenzo Maiorfi 8:1002d3025eaa 537 void radioEvent_TxDone();
Lorenzo Maiorfi 8:1002d3025eaa 538 void radioEvent_FhssChangeChannel(uint8_t currentChannel);
Lorenzo Maiorfi 8:1002d3025eaa 539 void radioEvent_CadDone(bool channelActivityDetected);
Lorenzo Maiorfi 8:1002d3025eaa 540
Lorenzo Maiorfi 8:1002d3025eaa 541 // Fine nuova parte per RTOS
mluis 0:45c4f0364ca4 542 };
mluis 0:45c4f0364ca4 543
mluis 0:45c4f0364ca4 544 #endif // __SX1272_H__