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.
Fork of SX1276GenericLib by
sx1276.h
00001 /* 00002 / _____) _ | | 00003 ( (____ _____ ____ _| |_ _____ ____| |__ 00004 \____ \| ___ | (_ _) ___ |/ ___) _ \ 00005 _____) ) ____| | | || |_| ____( (___| | | | 00006 (______/|_____)_|_|_| \__)_____)\____)_| |_| 00007 (C) 2014 Semtech 00008 00009 Description: Actual implementation of a SX1276 radio, inherits Radio 00010 00011 License: Revised BSD License, see LICENSE.TXT file include in the project 00012 00013 Maintainers: Miguel Luis, Gregory Cristian and Nicolas Huguenin 00014 */ 00015 00016 /* 00017 * additional development to make it more generic across multiple OS versions 00018 * (c) 2017 Helmut Tschemernjak 00019 * 30826 Garbsen (Hannover) Germany 00020 */ 00021 00022 #ifndef __SX1276_H__ 00023 #define __SX1276_H__ 00024 00025 #include "radio.h" 00026 #include "sx1276Regs-Fsk.h" 00027 #include "sx1276Regs-LoRa.h" 00028 00029 00030 00031 /*! 00032 * Radio wake-up time from sleep 00033 */ 00034 #define RADIO_WAKEUP_TIME 1 // [ms] 00035 00036 /*! 00037 * Sync word for Private LoRa networks 00038 */ 00039 #define LORA_MAC_PRIVATE_SYNCWORD 0x12 00040 00041 /*! 00042 * Sync word for Public LoRa networks 00043 */ 00044 #define LORA_MAC_PUBLIC_SYNCWORD 0x34 00045 00046 00047 /*! 00048 * SX1276 definitions 00049 */ 00050 #define XTAL_FREQ 32000000 00051 #define FREQ_STEP 61.03515625 00052 00053 #define RX_BUFFER_SIZE 256 00054 00055 /*! 00056 * Constant values need to compute the RSSI value 00057 */ 00058 #define RSSI_OFFSET_LF -164.0 00059 #define RSSI_OFFSET_HF -157.0 00060 00061 #define RF_MID_BAND_THRESH 525000000 00062 00063 00064 00065 00066 /*! 00067 * Type of the supported board. [SX1276MB1MAS / SX1276MB1LAS] 00068 */ 00069 typedef enum BoardType 00070 { 00071 SX1276MB1MAS = 0, 00072 SX1276MB1LAS, 00073 RFM95_SX1276, 00074 MURATA_SX1276, 00075 UNKNOWN 00076 }BoardType_t; 00077 00078 00079 typedef enum { 00080 LORA_SF6 = 6, // 64 chips/symbol, SF6 requires an TCXO! 00081 LORA_SF7 = 7, // 128 chips/symbol 00082 LORA_SF8 = 8, // 256 chips/symbol 00083 LORA_SF9 = 9, // 512 chips/symbol 00084 LORA_SF10 = 10, // 1024 chips/symbol 00085 LORA_SF11 = 11, // 2048 chips/symbol 00086 LORA_SF12 = 12, // 4096 chips/symbol 00087 } lora_spreading_factor_t; 00088 00089 00090 typedef enum { // cyclic error coding to perform forward error detection and correction 00091 LORA_ERROR_CODING_RATE_4_5 = 1, // 1.25x overhead 00092 LORA_ERROR_CODING_RATE_4_6 = 2, // 1.50x overhead 00093 LORA_ERROR_CODING_RATE_4_7 = 3, // 1.75x overhead 00094 LORA_ERROR_CODING_RATE_4_8 = 4, // 2.00x overhead 00095 } lora_coding_rate_t; 00096 00097 00098 typedef enum { 00099 RF_FREQUENCY_868_0 = 868000000, // Hz 00100 RF_FREQUENCY_868_1 = 868100000, // Hz 00101 RF_FREQUENCY_868_3 = 868300000, // Hz 00102 RF_FREQUENCY_868_5 = 868500000, // Hz 00103 RF_FREQUENCY_915_0 = 915000000, // Hz 00104 RF_FREQUENCY_913_8 = 913880000, // Hz 00105 } rf_frequency_t; 00106 00107 00108 00109 /*! 00110 * Actual implementation of a SX1276 radio, inherits Radio 00111 */ 00112 class SX1276 : public Radio 00113 { 00114 protected: 00115 00116 bool isRadioActive; 00117 00118 BoardType_t boardConnected; //1 = SX1276MB1LAS; 0 = SX1276MB1MAS 00119 00120 uint8_t *rxtxBuffer; 00121 00122 /*! 00123 * Hardware IO IRQ callback function definition 00124 */ 00125 typedef void ( SX1276 ::*DioIrqHandler )( void ); 00126 00127 /*! 00128 * Hardware DIO IRQ functions 00129 */ 00130 DioIrqHandler *dioIrq ; 00131 00132 00133 00134 RadioSettings_t settings; 00135 00136 /*! 00137 * FSK bandwidth definition 00138 */ 00139 struct BandwidthMap { 00140 uint32_t bandwidth; 00141 uint8_t RegValue; 00142 }; 00143 static const struct BandwidthMap FskBandwidths[]; 00144 static const struct BandwidthMap LoRaBandwidths[]; 00145 00146 protected: 00147 00148 /*! 00149 * Performs the Rx chain calibration for LF and HF bands 00150 * \remark Must be called just after the reset so all registers are at their 00151 * default values 00152 */ 00153 void RxChainCalibration ( void ); 00154 00155 public: 00156 SX1276 ( RadioEvents_t *events); 00157 virtual ~SX1276 ( ); 00158 00159 00160 00161 00162 //------------------------------------------------------------------------- 00163 // Redefined Radio functions 00164 //------------------------------------------------------------------------- 00165 /*! 00166 * @brief Return current radio status, returns true if a radios has been found. 00167 * 00168 * @param [IN] events Structure containing the driver callback functions 00169 */ 00170 virtual bool Init( RadioEvents_t *events ); 00171 00172 /*! 00173 * @brief Initializes the radio registers 00174 */ 00175 virtual void RadioRegistersInit(void); 00176 00177 /*! 00178 * Return current radio status 00179 * 00180 * @param status Radio status. [RF_IDLE, RX_RUNNING, TX_RUNNING, CAD_RUNNING] 00181 */ 00182 virtual RadioState GetStatus ( void ); 00183 00184 /*! 00185 * @brief Configures the SX1276 with the given modem 00186 * 00187 * @param [IN] modem Modem to be used [0: FSK, 1: LoRa] 00188 */ 00189 virtual void SetModem( RadioModems_t modem ); 00190 00191 /*! 00192 * @brief Sets the channel frequency 00193 * 00194 * @param [IN] freq Channel RF frequency 00195 */ 00196 virtual void SetChannel( uint32_t freq ); 00197 00198 /*! 00199 * @brief Sets the channels configuration 00200 * 00201 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00202 * @param [IN] freq Channel RF frequency 00203 * @param [IN] rssiThresh RSSI threshold 00204 * 00205 * @retval isFree [true: Channel is free, false: Channel is not free] 00206 */ 00207 virtual bool IsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh ); 00208 00209 /*! 00210 * @brief Generates a 32 bits random value based on the RSSI readings 00211 * 00212 * \remark This function sets the radio in LoRa modem mode and disables 00213 * all interrupts. 00214 * After calling this function either Radio.SetRxConfig or 00215 * Radio.SetTxConfig functions must be called. 00216 * 00217 * @retval randomValue 32 bits random value 00218 */ 00219 virtual uint32_t Random( void ); 00220 00221 /*! 00222 * @brief Sets the reception parameters 00223 * 00224 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00225 * @param [IN] bandwidth Sets the bandwidth 00226 * FSK : >= 2600 and <= 250000 Hz 00227 * LoRa: [0: 125 kHz, 1: 250 kHz, 00228 * 2: 500 kHz, 3: Reserved] 00229 * @param [IN] datarate Sets the Datarate 00230 * FSK : 600..300000 bits/s 00231 * LoRa: [6: 64, 7: 128, 8: 256, 9: 512, 00232 * 10: 1024, 11: 2048, 12: 4096 chips] 00233 * @param [IN] coderate Sets the coding rate ( LoRa only ) 00234 * FSK : N/A ( set to 0 ) 00235 * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] 00236 * @param [IN] bandwidthAfc Sets the AFC Bandwidth ( FSK only ) 00237 * FSK : >= 2600 and <= 250000 Hz 00238 * LoRa: N/A ( set to 0 ) 00239 * @param [IN] preambleLen Sets the Preamble length ( LoRa only ) 00240 * FSK : N/A ( set to 0 ) 00241 * LoRa: Length in symbols ( the hardware adds 4 more symbols ) 00242 * @param [IN] symbTimeout Sets the RxSingle timeout value 00243 * FSK : timeout number of bytes 00244 * LoRa: timeout in symbols 00245 * @param [IN] fixLen Fixed length packets [0: variable, 1: fixed] 00246 * @param [IN] payloadLen Sets payload length when fixed lenght is used 00247 * @param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON] 00248 * @param [IN] freqHopOn Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only) 00249 * @param [IN] hopPeriod Number of symbols bewteen each hop (LoRa only) 00250 * @param [IN] iqInverted Inverts IQ signals ( LoRa only ) 00251 * FSK : N/A ( set to 0 ) 00252 * LoRa: [0: not inverted, 1: inverted] 00253 * @param [IN] rxContinuous Sets the reception in continuous mode 00254 * [false: single mode, true: continuous mode] 00255 */ 00256 virtual void SetRxConfig ( RadioModems_t modem, uint32_t bandwidth, 00257 uint32_t datarate, uint8_t coderate, 00258 uint32_t bandwidthAfc, uint16_t preambleLen, 00259 uint16_t symbTimeout, bool fixLen, 00260 uint8_t payloadLen, 00261 bool crcOn, bool freqHopOn, uint8_t hopPeriod, 00262 bool iqInverted, bool rxContinuous ); 00263 00264 /*! 00265 * @brief Sets the transmission parameters 00266 * 00267 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00268 * @param [IN] power Sets the output power [dBm] 00269 * @param [IN] fdev Sets the frequency deviation ( FSK only ) 00270 * FSK : [Hz] 00271 * LoRa: 0 00272 * @param [IN] bandwidth Sets the bandwidth ( LoRa only ) 00273 * FSK : 0 00274 * LoRa: [0: 125 kHz, 1: 250 kHz, 00275 * 2: 500 kHz, 3: Reserved] 00276 * @param [IN] datarate Sets the Datarate 00277 * FSK : 600..300000 bits/s 00278 * LoRa: [6: 64, 7: 128, 8: 256, 9: 512, 00279 * 10: 1024, 11: 2048, 12: 4096 chips] 00280 * @param [IN] coderate Sets the coding rate ( LoRa only ) 00281 * FSK : N/A ( set to 0 ) 00282 * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] 00283 * @param [IN] preambleLen Sets the preamble length 00284 * @param [IN] fixLen Fixed length packets [0: variable, 1: fixed] 00285 * @param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON] 00286 * @param [IN] freqHopOn Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only) 00287 * @param [IN] hopPeriod Number of symbols bewteen each hop (LoRa only) 00288 * @param [IN] iqInverted Inverts IQ signals ( LoRa only ) 00289 * FSK : N/A ( set to 0 ) 00290 * LoRa: [0: not inverted, 1: inverted] 00291 * @param [IN] timeout Transmission timeout [ms] 00292 */ 00293 virtual void SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev, 00294 uint32_t bandwidth, uint32_t datarate, 00295 uint8_t coderate, uint16_t preambleLen, 00296 bool fixLen, bool crcOn, bool freqHopOn, 00297 uint8_t hopPeriod, bool iqInverted, uint32_t timeout ); 00298 00299 00300 /*! 00301 * @brief Checks if the given RF frequency is supported by the hardware 00302 * 00303 * @param [IN] frequency RF frequency to be checked 00304 * @retval isSupported [true: supported, false: unsupported] 00305 */ 00306 virtual bool CheckRfFrequency( uint32_t frequency ) = 0; 00307 00308 /*! 00309 * @brief Computes the packet time on air for the given payload 00310 * 00311 * \Remark Can only be called once SetRxConfig or SetTxConfig have been called 00312 * 00313 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00314 * @param [IN] pktLen Packet payload length 00315 * 00316 * @retval airTime Computed airTime for the given packet payload length 00317 */ 00318 virtual uint32_t TimeOnAir ( RadioModems_t modem, int16_t pktLen ); 00319 00320 /*! 00321 * @brief Sends the buffer of size. Prepares the packet to be sent and sets 00322 * the radio in transmission 00323 * 00324 * @param [IN]: buffer Buffer pointer 00325 * @param [IN]: size Buffer size 00326 * @param [IN]: buffer Header pointer 00327 * @param [IN]: size Header size 00328 */ 00329 virtual void Send(void *buffer, int16_t size, void *header = NULL, int16_t hsize = 0); 00330 00331 /*! 00332 * @brief Sets the radio in sleep mode 00333 */ 00334 virtual void Sleep( void ); 00335 00336 /*! 00337 * @brief Sets the radio in standby mode 00338 */ 00339 virtual void Standby( void ); 00340 00341 /*! 00342 * @brief Sets the radio in CAD mode 00343 */ 00344 virtual void StartCad( void ); 00345 00346 /*! 00347 * @brief Sets the radio in reception mode for the given time 00348 * @param [IN] timeout Reception timeout [ms] 00349 * [0: continuous, others timeout] 00350 */ 00351 virtual void Rx( uint32_t timeout ); 00352 00353 /*! 00354 * @brief Check is radio receives a signal 00355 */ 00356 virtual bool RxSignalPending(); 00357 00358 00359 /*! 00360 * @brief Sets the radio in transmission mode for the given time 00361 * @param [IN] timeout Transmission timeout [ms] 00362 * [0: continuous, others timeout] 00363 */ 00364 virtual void Tx( uint32_t timeout ); 00365 00366 /*! 00367 * @brief Sets the radio in continuous wave transmission mode 00368 * 00369 * @param [IN]: freq Channel RF frequency 00370 * @param [IN]: power Sets the output power [dBm] 00371 * @param [IN]: time Transmission mode timeout [s] 00372 */ 00373 00374 virtual void SetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time ); 00375 00376 /*! 00377 * @brief Returns the maximal transfer unit for a given modem 00378 * 00379 * @retval MTU size in bytes 00380 */ 00381 virtual int16_t MaxMTUSize( RadioModems_t modem ); 00382 00383 /*! 00384 * @brief Reads the current RSSI value 00385 * 00386 * @retval rssiValue Current RSSI value in [dBm] 00387 */ 00388 virtual int16_t GetRssi ( RadioModems_t modem ); 00389 00390 /*! 00391 * @brief Reads the current frequency error 00392 * 00393 * @retval frequency error value in [Hz] 00394 */ 00395 virtual int32_t GetFrequencyError( RadioModems_t modem ); 00396 00397 /*! 00398 * @brief Writes the radio register at the specified address 00399 * 00400 * @param [IN]: addr Register address 00401 * @param [IN]: data New register value 00402 */ 00403 virtual void Write ( uint8_t addr, uint8_t data ) = 0; 00404 00405 /*! 00406 * @brief Reads the radio register at the specified address 00407 * 00408 * @param [IN]: addr Register address 00409 * @retval data Register value 00410 */ 00411 virtual uint8_t Read ( uint8_t addr ) = 0; 00412 00413 /*! 00414 * @brief Writes multiple radio registers starting at address 00415 * 00416 * @param [IN] addr First Radio register address 00417 * @param [IN] buffer Buffer containing the new register's values 00418 * @param [IN] size Number of registers to be written 00419 */ 00420 virtual void Write( uint8_t addr, void *buffer, uint8_t size ) = 0; 00421 00422 /*! 00423 * @brief Reads multiple radio registers starting at address 00424 * 00425 * @param [IN] addr First Radio register address 00426 * @param [OUT] buffer Buffer where to copy the registers data 00427 * @param [IN] size Number of registers to be read 00428 */ 00429 virtual void Read ( uint8_t addr, void *buffer, uint8_t size ) = 0; 00430 00431 /*! 00432 * @brief Writes the buffer contents to the SX1276 FIFO 00433 * 00434 * @param [IN] buffer Buffer containing data to be put on the FIFO. 00435 * @param [IN] size Number of bytes to be written to the FIFO 00436 */ 00437 virtual void WriteFifo( void *buffer, uint8_t size ) = 0; 00438 00439 /*! 00440 * @brief Reads the contents of the SX1276 FIFO 00441 * 00442 * @param [OUT] buffer Buffer where to copy the FIFO read data. 00443 * @param [IN] size Number of bytes to be read from the FIFO 00444 */ 00445 virtual void ReadFifo( void *buffer, uint8_t size ) = 0; 00446 /*! 00447 * @brief Resets the SX1276 00448 */ 00449 virtual void Reset( void ) = 0; 00450 00451 /*! 00452 * @brief Sets the maximum payload length. 00453 * 00454 * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa] 00455 * @param [IN] max Maximum payload length in bytes 00456 */ 00457 virtual void SetMaxPayloadLength( RadioModems_t modem, uint8_t max ); 00458 00459 /*! 00460 * \brief Sets the network to public or private. Updates the sync byte. 00461 * 00462 * \remark Applies to LoRa modem only 00463 * 00464 * \param [IN] enable if true, it enables a public network 00465 */ 00466 virtual void SetPublicNetwork( bool enable ); 00467 00468 /*! 00469 * @brief Sets the radio output power. 00470 * 00471 * @param [IN] power Sets the RF output power 00472 */ 00473 virtual void SetRfTxPower( int8_t power ) = 0; 00474 00475 //------------------------------------------------------------------------- 00476 // Board relative functions 00477 //------------------------------------------------------------------------- 00478 /*! 00479 * Radio registers definition 00480 */ 00481 struct RadioRegisters { 00482 ModemType Modem; 00483 uint8_t Addr; 00484 uint8_t Value; 00485 }; 00486 00487 00488 static const struct RadioRegisters RadioRegsInit[]; 00489 00490 typedef enum { 00491 RXTimeoutTimer, 00492 TXTimeoutTimer, 00493 RXTimeoutSyncWordTimer 00494 } TimeoutTimer_t; 00495 00496 00497 protected: 00498 /*! 00499 * @brief Initializes the radio I/Os pins interface 00500 */ 00501 virtual void IoInit( void ) = 0; 00502 00503 /*! 00504 * @brief Initializes the radio SPI 00505 */ 00506 virtual void SpiInit( void ) = 0; 00507 00508 /*! 00509 * @brief Initializes DIO IRQ handlers 00510 * 00511 * @param [IN] irqHandlers Array containing the IRQ callback functions 00512 */ 00513 virtual void IoIrqInit( DioIrqHandler *irqHandlers ) = 0; 00514 00515 /*! 00516 * @brief De-initializes the radio I/Os pins interface. 00517 * 00518 * \remark Useful when going in MCU lowpower modes 00519 */ 00520 virtual void IoDeInit( void ) = 0; 00521 00522 /*! 00523 * @brief Gets the board PA selection configuration 00524 * 00525 * @param [IN] channel Channel frequency in Hz 00526 * @retval PaSelect RegPaConfig PaSelect value 00527 */ 00528 virtual uint8_t GetPaSelect( uint32_t channel ) = 0; 00529 00530 /*! 00531 * @brief Set the RF Switch I/Os pins in Low Power mode 00532 * 00533 * @param [IN] status enable or disable 00534 */ 00535 virtual void SetAntSwLowPower( bool status ) = 0; 00536 00537 /*! 00538 * @brief Initializes the RF Switch I/Os pins interface 00539 */ 00540 virtual void AntSwInit( void ) = 0; 00541 00542 /*! 00543 * @brief De-initializes the RF Switch I/Os pins interface 00544 * 00545 * \remark Needed to decrease the power consumption in MCU lowpower modes 00546 */ 00547 virtual void AntSwDeInit( void ) = 0; 00548 00549 /*! 00550 * @brief Controls the antenna switch if necessary. 00551 * 00552 * \remark see errata note 00553 * 00554 * @param [IN] opMode Current radio operating mode 00555 */ 00556 virtual void SetAntSw( uint8_t opMode ) = 0; 00557 00558 typedef void ( SX1276 ::*timeoutFuncPtr)( void ); 00559 00560 00561 /* 00562 * The the Timeout for a given Timer. 00563 */ 00564 virtual void SetTimeout(TimeoutTimer_t timer, timeoutFuncPtr, int timeout_ms = 0) = 0; 00565 00566 /* 00567 * A simple ms sleep 00568 */ 00569 virtual void Sleep_ms(int ms) = 0; 00570 00571 protected: 00572 00573 /*! 00574 * @brief Sets the SX1276 operating mode 00575 * 00576 * @param [IN] opMode New operating mode 00577 */ 00578 virtual void SetOpMode( uint8_t opMode ); 00579 00580 /* 00581 * SX1276 DIO IRQ callback functions prototype 00582 */ 00583 00584 /*! 00585 * @brief DIO 0 IRQ callback 00586 */ 00587 virtual void OnDio0Irq( void ); 00588 00589 /*! 00590 * @brief DIO 1 IRQ callback 00591 */ 00592 virtual void OnDio1Irq( void ); 00593 00594 /*! 00595 * @brief DIO 2 IRQ callback 00596 */ 00597 virtual void OnDio2Irq( void ); 00598 00599 /*! 00600 * @brief DIO 3 IRQ callback 00601 */ 00602 virtual void OnDio3Irq( void ); 00603 00604 /*! 00605 * @brief DIO 4 IRQ callback 00606 */ 00607 virtual void OnDio4Irq( void ); 00608 00609 /*! 00610 * @brief DIO 5 IRQ callback 00611 */ 00612 virtual void OnDio5Irq( void ); 00613 00614 /*! 00615 * @brief Tx & Rx timeout timer callback 00616 */ 00617 virtual void OnTimeoutIrq( void ); 00618 00619 /*! 00620 * Returns the known FSK bandwidth registers value 00621 * 00622 * \param [IN] bandwidth Bandwidth value in Hz 00623 * \retval regValue Bandwidth register value. 00624 */ 00625 static uint8_t GetFskBandwidthRegValue ( uint32_t bandwidth ); 00626 00627 static uint8_t GetLoRaBandwidthRegValue ( uint32_t bandwidth ); 00628 00629 enum { 00630 LORA_BANKWIDTH_7kHz = 0, // 7.8 kHz requires TCXO 00631 LORA_BANKWIDTH_10kHz = 1, // 10.4 kHz requires TCXO 00632 LORA_BANKWIDTH_15kHz = 2, // 15.6 kHz requires TCXO 00633 LORA_BANKWIDTH_20kHz = 3, // 20.8 kHz requires TCXO 00634 LORA_BANKWIDTH_31kHz = 4, // 31.2 kHz requires TCXO 00635 LORA_BANKWIDTH_41kHz = 5, // 41.4 kHz requires TCXO 00636 LORA_BANKWIDTH_62kHz = 6, // 62.5 kHz requires TCXO 00637 LORA_BANKWIDTH_125kHz = 7, 00638 LORA_BANKWIDTH_250kHz = 8, 00639 LORA_BANKWIDTH_500kHz = 9, 00640 LORA_BANKWIDTH_RESERVED = 10, 00641 }; 00642 }; 00643 00644 #endif // __SX1276_H__
Generated on Tue Jul 12 2022 18:32:30 by
1.7.2
