Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SxRadio1272.h
00001 /* 00002 / _____) _ | | 00003 ( (____ _____ ____ _| |_ _____ ____| |__ 00004 \____ \| ___ | (_ _) ___ |/ ___) _ \ 00005 _____) ) ____| | | || |_| ____( (___| | | | 00006 (______/|_____)_|_|_| \__)_____)\____)_| |_| 00007 (C)2013 Semtech 00008 00009 Description: Generic SX1272 driver implementation 00010 00011 License: Revised BSD License, see LICENSE.TXT file include in the project 00012 00013 Maintainer: Miguel Luis and Gregory Cristian 00014 */ 00015 #ifndef __SXRADIO1272_H__ 00016 #define __SXRADIO1272_H__ 00017 00018 #include "SxRadio.h" 00019 #include "SxRadio1272Regs-Fsk.h" 00020 #include "SxRadio1272Regs-LoRa.h" 00021 00022 /*! 00023 * Radio wakeup time from SLEEP mode 00024 */ 00025 #if defined(TARGET_XDOT_L151CC) 00026 #define RADIO_WAKEUP_TIME 4 // [ms] 00027 #else 00028 #define RADIO_WAKEUP_TIME 2 // [ms] 00029 #endif 00030 00031 /*! 00032 * SX1272 definitions 00033 */ 00034 #define XTAL_FREQ 32000000 00035 #define FREQ_STEP 61.03515625 00036 00037 #define RX_BUFFER_SIZE 256 00038 00039 /*! 00040 * ============================================================================ 00041 * Public functions prototypes 00042 * ============================================================================ 00043 */ 00044 00045 class SxRadio1272 : public SxRadio 00046 { 00047 public: 00048 enum Bandwidth { 00049 BW_125, BW_250, BW_500 00050 }; 00051 00052 SxRadio1272 (PinName reset, PinName DIO0, PinName DIO1, PinName DIO2, 00053 PinName DIO3, PinName DIO4, PinName mosi, PinName miso, 00054 PinName sclk, PinName nss, osPriority priority = osPriorityAboveNormal); 00055 virtual ~SxRadio1272 () {} 00056 00057 /*! 00058 * \brief Initializes the radio 00059 * 00060 * \param [IN] events Structure containing the driver callback functions 00061 */ 00062 virtual void Init( SxRadioEvents *events ); 00063 /*! 00064 * \brief Prepares the radio for destruction 00065 */ 00066 virtual void Terminate( void ); 00067 /*! 00068 * \brief Configures the radio with the given modem 00069 * 00070 * \param [IN] modem Modem to be used [0: FSK, 1: LoRa] 00071 */ 00072 virtual void SetModem( RadioModems_t modem ); 00073 /*! 00074 * \brief Sets the channel frequency 00075 * 00076 * \param [IN] freq Channel RF frequency 00077 */ 00078 virtual void SetChannel( uint32_t freq ); 00079 /*! 00080 * \brief Detect channel free using CAD 00081 * 00082 * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00083 * \param [IN] freq Channel RF frequency 00084 * \param [IN] rssiThresh RSSI threshold 00085 * \param [IN] bandwidth 0:125k, 1:250k, 2:500k 00086 * \param [IN] rssiVal pointer to variable to hold RSSI value if desired - ignored if NULL 00087 * 00088 * \retval isFree [true: Channel is free, false: Channel is not free] 00089 */ 00090 virtual bool IsChannelFree( RadioModems_t modem, uint32_t freq, uint8_t datarate, int16_t rssiThresh, uint8_t bandwidth, uint32_t timeout = 5000, int16_t *rssiVal = NULL ); 00091 /*! 00092 * \brief Generates a 32 bits random value based on the RSSI readings 00093 * 00094 * \remark This function sets the radio in LoRa modem mode and disables 00095 * all interrupts. 00096 * After calling this function either Radio.SetRxConfig or 00097 * Radio.SetTxConfig functions must be called. 00098 * 00099 * \retval randomValue 32 bits random value 00100 */ 00101 virtual uint32_t Random( void ); 00102 /*! 00103 * \brief Sets the reception parameters 00104 * 00105 * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00106 * \param [IN] bandwidth Sets the bandwidth 00107 * FSK : >= 2600 and <= 250000 Hz 00108 * LoRa: [0: 125 kHz, 1: 250 kHz, 00109 * 2: 500 kHz, 3: Reserved] 00110 * \param [IN] datarate Sets the Datarate 00111 * FSK : 600..300000 bits/s 00112 * LoRa: [6: 64, 7: 128, 8: 256, 9: 512, 00113 * 10: 1024, 11: 2048, 12: 4096 chips] 00114 * \param [IN] coderate Sets the coding rate (LoRa only) 00115 * FSK : N/A ( set to 0 ) 00116 * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] 00117 * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only) 00118 * FSK : >= 2600 and <= 250000 Hz 00119 * LoRa: N/A ( set to 0 ) 00120 * \param [IN] preambleLen Sets the Preamble length 00121 * FSK : Number of bytes 00122 * LoRa: Length in symbols (the hardware adds 4 more symbols) 00123 * \param [IN] symbTimeout Sets the RxSingle timeout value (LoRa only) 00124 * FSK : N/A ( set to 0 ) 00125 * LoRa: timeout in symbols 00126 * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed] 00127 * \param [IN] payloadLen Sets payload length when fixed length is used 00128 * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON] 00129 * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping 00130 * FSK : N/A ( set to 0 ) 00131 * LoRa: [0: OFF, 1: ON] 00132 * \param [IN] HopPeriod Number of symbols bewteen each hop 00133 * FSK : N/A ( set to 0 ) 00134 * LoRa: Number of symbols 00135 * \param [IN] iqInverted Inverts IQ signals (LoRa only) 00136 * FSK : N/A ( set to 0 ) 00137 * LoRa: [0: not inverted, 1: inverted] 00138 * \param [IN] rxContinuous Sets the reception in continuous mode 00139 * [false: single mode, true: continuous mode] 00140 */ 00141 virtual void SetRxConfig( RadioModems_t modem, uint32_t bandwidth, 00142 uint32_t datarate, uint8_t coderate, 00143 uint32_t bandwidthAfc, uint16_t preambleLen, 00144 uint16_t symbTimeout, bool fixLen, 00145 uint8_t payloadLen, 00146 bool crcOn, bool FreqHopOn, uint8_t HopPeriod, 00147 bool iqInverted, bool rxContinuous ); 00148 /*! 00149 * \brief Sets the transmission parameters 00150 * 00151 * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00152 * \param [IN] power Sets the output power [dBm] 00153 * \param [IN] fdev Sets the frequency deviation (FSK only) 00154 * FSK : [Hz] 00155 * LoRa: 0 00156 * \param [IN] bandwidth Sets the bandwidth (LoRa only) 00157 * FSK : 0 00158 * LoRa: [0: 125 kHz, 1: 250 kHz, 00159 * 2: 500 kHz, 3: Reserved] 00160 * \param [IN] datarate Sets the Datarate 00161 * FSK : 600..300000 bits/s 00162 * LoRa: [6: 64, 7: 128, 8: 256, 9: 512, 00163 * 10: 1024, 11: 2048, 12: 4096 chips] 00164 * \param [IN] coderate Sets the coding rate (LoRa only) 00165 * FSK : N/A ( set to 0 ) 00166 * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] 00167 * \param [IN] preambleLen Sets the preamble length 00168 * FSK : Number of bytes 00169 * LoRa: Length in symbols (the hardware adds 4 more symbols) 00170 * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed] 00171 * \param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON] 00172 * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping 00173 * FSK : N/A ( set to 0 ) 00174 * LoRa: [0: OFF, 1: ON] 00175 * \param [IN] HopPeriod Number of symbols bewteen each hop 00176 * FSK : N/A ( set to 0 ) 00177 * LoRa: Number of symbols 00178 * \param [IN] iqInverted Inverts IQ signals (LoRa only) 00179 * FSK : N/A ( set to 0 ) 00180 * LoRa: [0: not inverted, 1: inverted] 00181 * \param [IN] timeout Transmission timeout [us] 00182 */ 00183 virtual void SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev, 00184 uint32_t bandwidth, uint32_t datarate, 00185 uint8_t coderate, uint16_t preambleLen, 00186 bool fixLen, bool crcOn, bool FreqHopOn, 00187 uint8_t HopPeriod, bool iqInverted, uint32_t timeout ); 00188 00189 /** 00190 * Generate CW with current TX config 00191 */ 00192 virtual void SetTxContinuous(bool enable); 00193 00194 /*! 00195 * \brief Computes the packet time on air for the given payload 00196 * 00197 * \Remark Can only be called once SetRxConfig or SetTxConfig have been called 00198 * 00199 * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00200 * \param [IN] pktLen Packet payload length 00201 * 00202 * \retval airTime Computed airTime for the given packet payload length 00203 */ 00204 virtual double TimeOnAir( RadioModems_t modem, uint8_t pktLen ); 00205 /*! 00206 * \brief Sends the buffer of size. Prepares the packet to be sent and sets 00207 * the radio in transmission 00208 * 00209 * \param [IN]: buffer Buffer pointer 00210 * \param [IN]: size Buffer size 00211 */ 00212 virtual void Send( const uint8_t *buffer, uint8_t size ); 00213 /*! 00214 * \brief Sets the radio in sleep mode 00215 */ 00216 virtual void Sleep( void ); 00217 /*! 00218 * \brief Sets the radio in standby mode 00219 */ 00220 virtual void Standby( void ); 00221 /*! 00222 * \brief Sets the radio in reception mode for the given time 00223 * \param [IN] timeout Reception timeout [ms] 00224 * [0: continuous, others timeout] 00225 */ 00226 virtual void Rx( uint32_t timeout ); 00227 /*! 00228 * \brief Start a Channel Activity Detection 00229 */ 00230 virtual void StartCad( void ); 00231 /*! 00232 * \brief Reads the current RSSI value 00233 * 00234 * \retval rssiValue Current RSSI value in [dBm] 00235 */ 00236 virtual int16_t Rssi( RadioModems_t modem ); 00237 /*! 00238 * \brief Writes the radio register at the specified address 00239 * 00240 * \param [IN]: addr Register address 00241 * \param [IN]: data New register value 00242 */ 00243 virtual void Write( uint8_t addr, uint8_t data ); 00244 /*! 00245 * \brief Reads the radio register at the specified address 00246 * 00247 * \param [IN]: addr Register address 00248 * \retval data Register value 00249 */ 00250 virtual uint8_t Read ( uint8_t addr ); 00251 /*! 00252 * \brief Writes multiple radio registers starting at address 00253 * 00254 * \param [IN] addr First Radio register address 00255 * \param [IN] buffer Buffer containing the new register's values 00256 * \param [IN] size Number of registers to be written 00257 */ 00258 virtual void WriteBuffer( uint8_t addr, const uint8_t *buffer, uint8_t size ); 00259 /*! 00260 * \brief Reads multiple radio registers starting at address 00261 * 00262 * \param [IN] addr First Radio register address 00263 * \param [OUT] buffer Buffer where to copy the registers data 00264 * \param [IN] size Number of registers to be read 00265 */ 00266 virtual void ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size ); 00267 00268 virtual void SignalMacEvent(void); 00269 00270 virtual void CheckForReset(void); 00271 00272 virtual void ResetRadio(); 00273 00274 virtual uint32_t GetTimeOnAir(); 00275 protected: 00276 /*! 00277 * \brief Set antenna switch to low power 00278 * 00279 * \param [IN] status [true: Antenna in low power, false: Antenna active] 00280 */ 00281 virtual void SetAntSwLowPower( bool status ) { } 00282 00283 /*! 00284 * \brief Set antenna direction 00285 * 00286 * \param [IN] rxTx [1: transmitter, 0: receiver] 00287 */ 00288 virtual void SetAntSw( uint8_t rxTx ) { } 00289 00290 /*! 00291 * \brief Get power amp setting 00292 * 00293 * \param [IN] channel Channel RF frequency 00294 * 00295 * \retval Power amp setting 00296 */ 00297 virtual uint8_t GetPaSelect( uint32_t channel ) 00298 { 00299 return RF_PACONFIG_PASELECT_PABOOST; 00300 } 00301 00302 private: 00303 /*! 00304 * Radio registers definition 00305 */ 00306 typedef struct 00307 { 00308 RadioModems_t Modem; 00309 uint8_t Addr; 00310 uint8_t Value; 00311 }RadioRegisters_t; 00312 00313 /*! 00314 * Radio FSK modem parameters 00315 */ 00316 typedef struct 00317 { 00318 int8_t Power; 00319 uint32_t Fdev; 00320 uint32_t Bandwidth; 00321 uint32_t BandwidthAfc; 00322 uint32_t Datarate; 00323 uint16_t PreambleLen; 00324 bool FixLen; 00325 uint8_t PayloadLen; 00326 bool CrcOn; 00327 bool IqInverted; 00328 bool RxContinuous; 00329 uint32_t TxTimeout; 00330 }RadioFskSettings_t; 00331 00332 /*! 00333 * Radio FSK packet handler state 00334 */ 00335 typedef struct 00336 { 00337 bool SyncWordDetected; 00338 int8_t RssiValue; 00339 int32_t AfcValue; 00340 uint8_t RxGain; 00341 uint16_t Size; 00342 uint16_t NbBytes; 00343 uint8_t FifoThresh; 00344 uint8_t ChunkSize; 00345 }RadioFskPacketHandler_t; 00346 00347 /*! 00348 * Radio LoRa modem parameters 00349 */ 00350 typedef struct 00351 { 00352 int8_t Power; 00353 uint32_t Bandwidth; 00354 uint32_t Datarate; 00355 bool LowDatarateOptimize; 00356 uint8_t Coderate; 00357 uint16_t PreambleLen; 00358 bool FixLen; 00359 uint8_t PayloadLen; 00360 bool CrcOn; 00361 bool FreqHopOn; 00362 uint8_t HopPeriod; 00363 bool IqInverted; 00364 bool RxContinuous; 00365 uint32_t TxTimeout; 00366 }RadioLoRaSettings_t; 00367 00368 /*! 00369 * Radio LoRa packet handler state 00370 */ 00371 typedef struct 00372 { 00373 int8_t SnrValue; 00374 int16_t RssiValue; 00375 uint8_t Size; 00376 }RadioLoRaPacketHandler_t; 00377 00378 /*! 00379 * Radio Settings 00380 */ 00381 typedef struct 00382 { 00383 uint32_t Channel; 00384 RadioFskSettings_t Fsk; 00385 RadioFskPacketHandler_t FskPacketHandler; 00386 RadioLoRaSettings_t LoRa; 00387 RadioLoRaPacketHandler_t LoRaPacketHandler; 00388 }RadioSettings_t; 00389 00390 /*! 00391 * \brief Resets the SX1272 00392 */ 00393 void Reset( void ); 00394 00395 /*! 00396 * \brief Sets the SX1272 operating mode 00397 * 00398 * \param [IN] opMode New operating mode 00399 */ 00400 void SetOpMode( uint8_t opMode ); 00401 00402 /*! 00403 * \brief Sets the SX1272 in transmission mode for the given time 00404 * \param [IN] timeout Transmission timeout [ms] [0: continuous, others timeout] 00405 */ 00406 void SetTx( uint32_t timeout ); 00407 00408 /*! 00409 * \brief Writes the buffer contents to the SX1272 FIFO 00410 * 00411 * \param [IN] buffer Buffer containing data to be put on the FIFO. 00412 * \param [IN] size Number of bytes to be written to the FIFO 00413 */ 00414 void WriteFifo( const uint8_t *buffer, uint8_t size ); 00415 00416 /*! 00417 * \brief Reads the contents of the SX1272 FIFO 00418 * 00419 * \param [OUT] buffer Buffer where to copy the FIFO read data. 00420 * \param [IN] size Number of bytes to be read from the FIFO 00421 */ 00422 void ReadFifo( uint8_t *buffer, uint8_t size ); 00423 00424 static void OnRxTxTimeoutIrq( void const *arg ); 00425 static void OnSyncTimeoutIrq( void const *arg ); 00426 void OnTimeoutIrqBottom( void ); 00427 void SX1272OnDioIrq( void ); 00428 void SX1272OnDio3Irq( void ); 00429 void SX1272OnDio1Irq( void ); 00430 00431 void SX1272IrqTxDone(); 00432 void SX1272IrqFskRxDone(uint8_t irqFlags2); 00433 void SX1272IrqFskFifoLevel(void); 00434 void SX1272IrqFskFifoEmpty(void); 00435 void SX1272IrqFskSyncMatch(void); 00436 void SX1272IrqFsk(void); 00437 void SX1272IrqLoraRxDone(uint8_t irqFlags); 00438 void SX1272IrqLoraRxTimeout(); 00439 void SX1272IrqLoraCadDone(uint8_t irqFlags); 00440 void SX1272IrqLoraFhss(void); 00441 void SX1272IrqLora(void); 00442 void SX1272Irq(void); 00443 00444 static void RadioThread(void const *args); 00445 00446 /*! 00447 * Interrupt thread 00448 */ 00449 Thread thread; 00450 uint64_t Stack[2560/8]; // 2.5kB stack for the radio thread 00451 //align stack fix for ARM mbed 5.5.* custom stack error 00452 00453 /*! 00454 * Reset IO line 00455 */ 00456 DigitalInOut reset; 00457 00458 InterruptIn DIO3; 00459 InterruptIn DIO4; 00460 InterruptIn DIO0; 00461 InterruptIn DIO1; 00462 InterruptIn DIO2; 00463 00464 00465 SPI spi; 00466 /*! 00467 * SPI slave select 00468 */ 00469 DigitalOut nss; 00470 00471 /*! 00472 * Event callback handler 00473 */ 00474 SxRadioEvents *RadioEvents; 00475 00476 /*! 00477 * Reception buffer 00478 */ 00479 uint8_t RxTxBuffer[RX_BUFFER_SIZE]; 00480 00481 /*! 00482 * Radio hardware and global parameters 00483 */ 00484 RadioSettings_t settings; 00485 00486 RtosTimer TxTimeoutTimer; 00487 RtosTimer RxTimeoutTimer; 00488 RtosTimer RxTimeoutSyncWord; 00489 00490 bool RxTxTimeoutPending; 00491 bool SyncTimeoutPending; 00492 00493 uint8_t opMode; 00494 uint32_t timeOnAir; 00495 00496 static const RadioRegisters_t RadioRegsInit[]; 00497 }; 00498 00499 #endif // __SXRADIO1272_H__
Generated on Tue Jul 12 2022 12:07:34 by
