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.
Dependents: SX126xDevKit SX1262PingPong SX126X_TXonly SX126X_PingPong_Demo ... more
Fork of SX126xLib by
sx126x.h
00001 /* 00002 ______ _ 00003 / _____) _ | | 00004 ( (____ _____ ____ _| |_ _____ ____| |__ 00005 \____ \| ___ | (_ _) ___ |/ ___) _ \ 00006 _____) ) ____| | | || |_| ____( (___| | | | 00007 (______/|_____)_|_|_| \__)_____)\____)_| |_| 00008 (C)2017 Semtech 00009 00010 Description: Generic SX126x driver implementation 00011 00012 License: Revised BSD License, see LICENSE.TXT file include in the project 00013 00014 Authors: Miguel Luis, Gregory Cristian 00015 */ 00016 #ifndef __SX126x_H__ 00017 #define __SX126x_H__ 00018 00019 #include "radio.h" 00020 00021 /*! 00022 * \brief Enables/disables driver debug features 00023 */ 00024 #define SX126x_DEBUG 0 00025 00026 00027 00028 /*! 00029 * \brief Hardware IO IRQ callback function definition 00030 */ 00031 class SX126x; 00032 typedef void ( SX126x::*DioIrqHandler )( void ); 00033 00034 /*! 00035 * \brief IRQ triggers callback function definition 00036 */ 00037 class SX126xHal; 00038 typedef void ( SX126xHal::*Trigger )( void ); 00039 00040 /*! 00041 * \brief Provides the frequency of the chip running on the radio and the frequency step 00042 * 00043 * \remark These defines are used for computing the frequency divider to set the RF frequency 00044 */ 00045 #define XTAL_FREQ 32000000 00046 #define FREQ_DIV 33554432 00047 #define FREQ_STEP 0.95367431640625 // ( ( double )( XTAL_FREQ / ( double )FREQ_DIV ) ) 00048 #define FREQ_ERR 0.47683715820312 00049 00050 00051 /*! 00052 * \brief List of devices supported by this driver 00053 */ 00054 #define SX1261 0 00055 #define SX1262 1 00056 #define SX1268 2 00057 00058 /*! 00059 * \brief List of matching supported by the sx126x 00060 */ 00061 #define MATCHING_FREQ_915 0 00062 #define MATCHING_FREQ_780 1 00063 #define MATCHING_FREQ_490 2 00064 #define MATCHING_FREQ_434 3 00065 #define MATCHING_FREQ_280 4 00066 #define MATCHING_FREQ_169 5 00067 #define MATCHING_FREQ_868 6 00068 00069 /*! 00070 * \brief Compensation delay for SetAutoTx/Rx functions in 15.625 microseconds 00071 */ 00072 #define AUTO_RX_TX_OFFSET 2 00073 00074 /*! 00075 * \brief LFSR initial value to compute IBM type CRC 00076 */ 00077 #define CRC_IBM_SEED 0xFFFF 00078 00079 /*! 00080 * \brief LFSR initial value to compute CCIT type CRC 00081 */ 00082 #define CRC_CCITT_SEED 0x1D0F 00083 00084 /*! 00085 * \brief Polynomial used to compute IBM CRC 00086 */ 00087 #define CRC_POLYNOMIAL_IBM 0x8005 00088 00089 /*! 00090 * \brief Polynomial used to compute CCIT CRC 00091 */ 00092 #define CRC_POLYNOMIAL_CCITT 0x1021 00093 00094 /*! 00095 * \brief The address of the register holding the first byte defining the CRC seed 00096 * 00097 */ 00098 #define REG_LR_CRCSEEDBASEADDR 0x06BC 00099 00100 /*! 00101 * \brief The address of the register holding the first byte defining the CRC polynomial 00102 */ 00103 #define REG_LR_CRCPOLYBASEADDR 0x06BE 00104 00105 /*! 00106 * \brief The address of the register holding the first byte defining the whitening seed 00107 */ 00108 #define REG_LR_WHITSEEDBASEADDR_MSB 0x06B8 00109 #define REG_LR_WHITSEEDBASEADDR_LSB 0x06B9 00110 00111 /*! 00112 * \brief The address of the register holding the packet configuration 00113 */ 00114 #define REG_LR_PACKETPARAMS 0x0704 00115 00116 /*! 00117 * \brief The address of the register holding the payload size 00118 */ 00119 #define REG_LR_PAYLOADLENGTH 0x0702 00120 00121 /*! 00122 * \brief The addresses of the registers holding SyncWords values 00123 */ 00124 #define REG_LR_SYNCWORDBASEADDRESS 0x06C0 00125 00126 /*! 00127 * \brief The addresses of the register holding LoRa Modem SyncWord value 00128 */ 00129 #define REG_LR_SYNCWORD 0x0740 00130 00131 /*! 00132 * Syncword for Private LoRa networks 00133 */ 00134 #define LORA_MAC_PRIVATE_SYNCWORD 0x1424 00135 00136 /*! 00137 * Syncword for Public LoRa networks 00138 */ 00139 #define LORA_MAC_PUBLIC_SYNCWORD 0x3444 00140 00141 /*! 00142 * The address of the register giving a 4 bytes random number 00143 */ 00144 #define RANDOM_NUMBER_GENERATORBASEADDR 0x0819 00145 00146 /*! 00147 * The address of the register holding RX Gain value (0x94: power saving, 0x96: rx boosted) 00148 */ 00149 #define REG_RX_GAIN 0x08AC 00150 00151 /*! 00152 * The address of the register holding frequency error indication 00153 */ 00154 #define REG_FREQUENCY_ERRORBASEADDR 0x076B 00155 00156 /*! 00157 * Change the value on the device internal trimming capacitor 00158 */ 00159 #define REG_XTA_TRIM 0x0911 00160 00161 /*! 00162 * Set the current max value in the over current protection 00163 */ 00164 #define REG_OCP 0x08E7 00165 00166 /*! 00167 * \brief Represents the states of the radio 00168 */ 00169 typedef enum 00170 { 00171 RF_IDLE = 0x00, //!< The radio is idle 00172 RF_RX_RUNNING, //!< The radio is in reception state 00173 RF_TX_RUNNING, //!< The radio is in transmission state 00174 RF_CAD, //!< The radio is doing channel activity detection 00175 }RadioStates_t; 00176 00177 /*! 00178 * \brief Represents the operating mode the radio is actually running 00179 */ 00180 typedef enum 00181 { 00182 MODE_SLEEP = 0x00, //! The radio is in sleep mode 00183 MODE_STDBY_RC, //! The radio is in standby mode with RC oscillator 00184 MODE_STDBY_XOSC, //! The radio is in standby mode with XOSC oscillator 00185 MODE_FS, //! The radio is in frequency synthesis mode 00186 MODE_TX, //! The radio is in transmit mode 00187 MODE_RX, //! The radio is in receive mode 00188 MODE_RX_DC, //! The radio is in receive duty cycle mode 00189 MODE_CAD //! The radio is in channel activity detection mode 00190 }RadioOperatingModes_t; 00191 00192 /*! 00193 * \brief Declares the oscillator in use while in standby mode 00194 * 00195 * Using the STDBY_RC standby mode allow to reduce the energy consumption 00196 * STDBY_XOSC should be used for time critical applications 00197 */ 00198 typedef enum 00199 { 00200 STDBY_RC = 0x00, 00201 STDBY_XOSC = 0x01, 00202 }RadioStandbyModes_t; 00203 00204 /*! 00205 * \brief Declares the power regulation used to power the device 00206 * 00207 * This command allows the user to specify if DC-DC or LDO is used for power regulation. 00208 * Using only LDO implies that the Rx or Tx current is doubled 00209 */ 00210 typedef enum 00211 { 00212 USE_LDO = 0x00, // default 00213 USE_DCDC = 0x01, 00214 }RadioRegulatorMode_t; 00215 00216 /*! 00217 * \brief Represents the possible packet type (i.e. modem) used 00218 */ 00219 typedef enum 00220 { 00221 PACKET_TYPE_GFSK = 0x00, 00222 PACKET_TYPE_LORA = 0x01, 00223 PACKET_TYPE_NONE = 0x0F, 00224 }RadioPacketTypes_t; 00225 00226 /*! 00227 * \brief Represents the ramping time for power amplifier 00228 */ 00229 typedef enum 00230 { 00231 RADIO_RAMP_10_US = 0x00, 00232 RADIO_RAMP_20_US = 0x01, 00233 RADIO_RAMP_40_US = 0x02, 00234 RADIO_RAMP_80_US = 0x03, 00235 RADIO_RAMP_200_US = 0x04, 00236 RADIO_RAMP_800_US = 0x05, 00237 RADIO_RAMP_1700_US = 0x06, 00238 RADIO_RAMP_3400_US = 0x07, 00239 }RadioRampTimes_t; 00240 00241 /*! 00242 * \brief Represents the number of symbols to be used for channel activity detection operation 00243 */ 00244 typedef enum 00245 { 00246 LORA_CAD_01_SYMBOL = 0x00, 00247 LORA_CAD_02_SYMBOL = 0x01, 00248 LORA_CAD_04_SYMBOL = 0x02, 00249 LORA_CAD_08_SYMBOL = 0x03, 00250 LORA_CAD_16_SYMBOL = 0x04, 00251 }RadioLoRaCadSymbols_t; 00252 00253 /*! 00254 * \brief Represents the Channel Activity Detection actions after the CAD operation is finished 00255 */ 00256 typedef enum 00257 { 00258 LORA_CAD_ONLY = 0x00, 00259 LORA_CAD_RX = 0x01, 00260 LORA_CAD_LBT = 0x10, 00261 }RadioCadExitModes_t; 00262 00263 /*! 00264 * \brief Represents the modulation shaping parameter 00265 */ 00266 typedef enum 00267 { 00268 MOD_SHAPING_OFF = 0x00, 00269 MOD_SHAPING_G_BT_03 = 0x08, 00270 MOD_SHAPING_G_BT_05 = 0x09, 00271 MOD_SHAPING_G_BT_07 = 0x0A, 00272 MOD_SHAPING_G_BT_1 = 0x0B, 00273 }RadioModShapings_t; 00274 00275 /*! 00276 * \brief Represents the modulation shaping parameter 00277 */ 00278 typedef enum 00279 { 00280 RX_BW_4800 = 0x1F, 00281 RX_BW_5800 = 0x17, 00282 RX_BW_7300 = 0x0F, 00283 RX_BW_9700 = 0x1E, 00284 RX_BW_11700 = 0x16, 00285 RX_BW_14600 = 0x0E, 00286 RX_BW_19500 = 0x1D, 00287 RX_BW_23400 = 0x15, 00288 RX_BW_29300 = 0x0D, 00289 RX_BW_39000 = 0x1C, 00290 RX_BW_46900 = 0x14, 00291 RX_BW_58600 = 0x0C, 00292 RX_BW_78200 = 0x1B, 00293 RX_BW_93800 = 0x13, 00294 RX_BW_117300 = 0x0B, 00295 RX_BW_156200 = 0x1A, 00296 RX_BW_187200 = 0x12, 00297 RX_BW_234300 = 0x0A, 00298 RX_BW_312000 = 0x19, 00299 RX_BW_373600 = 0x11, 00300 RX_BW_467000 = 0x09, 00301 }RadioRxBandwidth_t; 00302 00303 /*! 00304 * \brief Represents the possible spreading factor values in LoRa packet types 00305 */ 00306 typedef enum 00307 { 00308 LORA_SF5 = 0x05, 00309 LORA_SF6 = 0x06, 00310 LORA_SF7 = 0x07, 00311 LORA_SF8 = 0x08, 00312 LORA_SF9 = 0x09, 00313 LORA_SF10 = 0x0A, 00314 LORA_SF11 = 0x0B, 00315 LORA_SF12 = 0x0C, 00316 }RadioLoRaSpreadingFactors_t; 00317 00318 /*! 00319 * \brief Represents the bandwidth values for LoRa packet type 00320 */ 00321 typedef enum 00322 { 00323 LORA_BW_500 = 6, 00324 LORA_BW_250 = 5, 00325 LORA_BW_125 = 4, 00326 LORA_BW_062 = 3, 00327 LORA_BW_041 = 10, 00328 LORA_BW_031 = 2, 00329 LORA_BW_020 = 9, 00330 LORA_BW_015 = 1, 00331 LORA_BW_010 = 8, 00332 LORA_BW_007 = 0, 00333 }RadioLoRaBandwidths_t; 00334 00335 /*! 00336 * \brief Represents the coding rate values for LoRa packet type 00337 */ 00338 typedef enum 00339 { 00340 LORA_CR_4_5 = 0x01, 00341 LORA_CR_4_6 = 0x02, 00342 LORA_CR_4_7 = 0x03, 00343 LORA_CR_4_8 = 0x04, 00344 }RadioLoRaCodingRates_t; 00345 00346 /*! 00347 * \brief Represents the preamble length used to detect the packet on Rx side 00348 */ 00349 typedef enum 00350 { 00351 RADIO_PREAMBLE_DETECTOR_OFF = 0x00, //!< Preamble detection length off 00352 RADIO_PREAMBLE_DETECTOR_08_BITS = 0x04, //!< Preamble detection length 8 bits 00353 RADIO_PREAMBLE_DETECTOR_16_BITS = 0x05, //!< Preamble detection length 16 bits 00354 RADIO_PREAMBLE_DETECTOR_24_BITS = 0x06, //!< Preamble detection length 24 bits 00355 RADIO_PREAMBLE_DETECTOR_32_BITS = 0x07, //!< Preamble detection length 32 bit 00356 }RadioPreambleDetection_t; 00357 00358 /*! 00359 * \brief Represents the possible combinations of SyncWord correlators activated 00360 */ 00361 typedef enum 00362 { 00363 RADIO_ADDRESSCOMP_FILT_OFF = 0x00, //!< No correlator turned on, i.e. do not search for SyncWord 00364 RADIO_ADDRESSCOMP_FILT_NODE = 0x01, 00365 RADIO_ADDRESSCOMP_FILT_NODE_BROAD = 0x02, 00366 }RadioAddressComp_t; 00367 00368 /*! 00369 * \brief Radio packet length mode 00370 */ 00371 typedef enum 00372 { 00373 RADIO_PACKET_FIXED_LENGTH = 0x00, //!< The packet is known on both sides, no header included in the packet 00374 RADIO_PACKET_VARIABLE_LENGTH = 0x01, //!< The packet is on variable size, header included 00375 }RadioPacketLengthModes_t; 00376 00377 /*! 00378 * \brief Represents the CRC length 00379 */ 00380 typedef enum 00381 { 00382 RADIO_CRC_OFF = 0x01, //!< No CRC in use 00383 RADIO_CRC_1_BYTES = 0x00, 00384 RADIO_CRC_2_BYTES = 0x02, 00385 RADIO_CRC_1_BYTES_INV = 0x04, 00386 RADIO_CRC_2_BYTES_INV = 0x06, 00387 RADIO_CRC_2_BYTES_IBM = 0xF1, 00388 RADIO_CRC_2_BYTES_CCIT = 0xF2, 00389 }RadioCrcTypes_t; 00390 00391 /*! 00392 * \brief Radio whitening mode activated or deactivated 00393 */ 00394 typedef enum 00395 { 00396 RADIO_DC_FREE_OFF = 0x00, 00397 RADIO_DC_FREEWHITENING = 0x01, 00398 }RadioDcFree_t; 00399 00400 /*! 00401 * \brief Holds the lengths mode of a LoRa packet type 00402 */ 00403 typedef enum 00404 { 00405 LORA_PACKET_VARIABLE_LENGTH = 0x00, //!< The packet is on variable size, header included 00406 LORA_PACKET_FIXED_LENGTH = 0x01, //!< The packet is known on both sides, no header included in the packet 00407 LORA_PACKET_EXPLICIT = LORA_PACKET_VARIABLE_LENGTH, 00408 LORA_PACKET_IMPLICIT = LORA_PACKET_FIXED_LENGTH, 00409 }RadioLoRaPacketLengthsMode_t; 00410 00411 /*! 00412 * \brief Represents the CRC mode for LoRa packet type 00413 */ 00414 typedef enum 00415 { 00416 LORA_CRC_ON = 0x01, //!< CRC activated 00417 LORA_CRC_OFF = 0x00, //!< CRC not used 00418 }RadioLoRaCrcModes_t; 00419 00420 /*! 00421 * \brief Represents the IQ mode for LoRa packet type 00422 */ 00423 typedef enum 00424 { 00425 LORA_IQ_NORMAL = 0x00, 00426 LORA_IQ_INVERTED = 0x01, 00427 }RadioLoRaIQModes_t; 00428 00429 /*! 00430 * \brief Represents the volatge used to control the TCXO on/off from DIO3 00431 */ 00432 typedef enum 00433 { 00434 TCXO_CTRL_1_6V = 0x00, 00435 TCXO_CTRL_1_7V = 0x01, 00436 TCXO_CTRL_1_8V = 0x02, 00437 TCXO_CTRL_2_2V = 0x03, 00438 TCXO_CTRL_2_4V = 0x04, 00439 TCXO_CTRL_2_7V = 0x05, 00440 TCXO_CTRL_3_0V = 0x06, 00441 TCXO_CTRL_3_3V = 0x07, 00442 }RadioTcxoCtrlVoltage_t; 00443 00444 /*! 00445 * \brief Represents the interruption masks available for the radio 00446 * 00447 * \remark Note that not all these interruptions are available for all packet types 00448 */ 00449 typedef enum 00450 { 00451 IRQ_RADIO_NONE = 0x0000, 00452 IRQ_TX_DONE = 0x0001, 00453 IRQ_RX_DONE = 0x0002, 00454 IRQ_PREAMBLE_DETECTED = 0x0004, 00455 IRQ_SYNCWORD_VALID = 0x0008, 00456 IRQ_HEADER_VALID = 0x0010, 00457 IRQ_HEADER_ERROR = 0x0020, 00458 IRQ_CRC_ERROR = 0x0040, 00459 IRQ_CAD_DONE = 0x0080, 00460 IRQ_CAD_ACTIVITY_DETECTED = 0x0100, 00461 IRQ_RX_TX_TIMEOUT = 0x0200, 00462 IRQ_RADIO_ALL = 0xFFFF, 00463 }RadioIrqMasks_t; 00464 00465 /*! 00466 * \brief Represents all possible opcode understood by the radio 00467 */ 00468 typedef enum RadioCommands_e 00469 { 00470 RADIO_GET_STATUS = 0xC0, 00471 RADIO_WRITE_REGISTER = 0x0D, 00472 RADIO_READ_REGISTER = 0x1D, 00473 RADIO_WRITE_BUFFER = 0x0E, 00474 RADIO_READ_BUFFER = 0x1E, 00475 RADIO_SET_SLEEP = 0x84, 00476 RADIO_SET_STANDBY = 0x80, 00477 RADIO_SET_FS = 0xC1, 00478 RADIO_SET_TX = 0x83, 00479 RADIO_SET_RX = 0x82, 00480 RADIO_SET_RXDUTYCYCLE = 0x94, 00481 RADIO_SET_CAD = 0xC5, 00482 RADIO_SET_TXCONTINUOUSWAVE = 0xD1, 00483 RADIO_SET_TXCONTINUOUSPREAMBLE = 0xD2, 00484 RADIO_SET_PACKETTYPE = 0x8A, 00485 RADIO_GET_PACKETTYPE = 0x11, 00486 RADIO_SET_RFFREQUENCY = 0x86, 00487 RADIO_SET_TXPARAMS = 0x8E, 00488 RADIO_SET_PACONFIG = 0x95, 00489 RADIO_SET_CADPARAMS = 0x88, 00490 RADIO_SET_BUFFERBASEADDRESS = 0x8F, 00491 RADIO_SET_MODULATIONPARAMS = 0x8B, 00492 RADIO_SET_PACKETPARAMS = 0x8C, 00493 RADIO_GET_RXBUFFERSTATUS = 0x13, 00494 RADIO_GET_PACKETSTATUS = 0x14, 00495 RADIO_GET_RSSIINST = 0x15, 00496 RADIO_GET_STATS = 0x10, 00497 RADIO_RESET_STATS = 0x00, 00498 RADIO_CFG_DIOIRQ = 0x08, 00499 RADIO_GET_IRQSTATUS = 0x12, 00500 RADIO_CLR_IRQSTATUS = 0x02, 00501 RADIO_CALIBRATE = 0x89, 00502 RADIO_CALIBRATEIMAGE = 0x98, 00503 RADIO_SET_REGULATORMODE = 0x96, 00504 RADIO_GET_ERROR = 0x17, 00505 RADIO_SET_TCXOMODE = 0x97, 00506 RADIO_SET_TXFALLBACKMODE = 0x93, 00507 RADIO_SET_RFSWITCHMODE = 0x9D, 00508 RADIO_SET_STOPRXTIMERONPREAMBLE = 0x9F, 00509 RADIO_SET_LORASYMBTIMEOUT = 0xA0, 00510 }RadioCommands_t; 00511 00512 /*! 00513 * \brief The type describing the modulation parameters for every packet types 00514 */ 00515 typedef struct 00516 { 00517 RadioPacketTypes_t PacketType; //!< Packet to which the modulation parameters are referring to. 00518 struct 00519 { 00520 struct 00521 { 00522 uint32_t BitRate; 00523 uint32_t Fdev; 00524 RadioModShapings_t ModulationShaping; 00525 uint8_t Bandwidth; 00526 }Gfsk; 00527 struct 00528 { 00529 RadioLoRaSpreadingFactors_t SpreadingFactor; //!< Spreading Factor for the LoRa modulation 00530 RadioLoRaBandwidths_t Bandwidth; //!< Bandwidth for the LoRa modulation 00531 RadioLoRaCodingRates_t CodingRate; //!< Coding rate for the LoRa modulation 00532 uint8_t LowDatarateOptimize; //!< Indicates if the modem uses the low datarate optimization 00533 }LoRa; 00534 }Params; //!< Holds the modulation parameters structure 00535 }ModulationParams_t; 00536 00537 /*! 00538 * \brief The type describing the packet parameters for every packet types 00539 */ 00540 typedef struct 00541 { 00542 RadioPacketTypes_t PacketType; //!< Packet to which the packet parameters are referring to. 00543 struct 00544 { 00545 /*! 00546 * \brief Holds the GFSK packet parameters 00547 */ 00548 struct 00549 { 00550 uint16_t PreambleLength; //!< The preamble Tx length for GFSK packet type in bit 00551 RadioPreambleDetection_t PreambleMinDetect; //!< The preamble Rx length minimal for GFSK packet type 00552 uint8_t SyncWordLength; //!< The synchronization word length for GFSK packet type 00553 RadioAddressComp_t AddrComp; //!< Activated SyncWord correlators 00554 RadioPacketLengthModes_t HeaderType; //!< If the header is explicit, it will be transmitted in the GFSK packet. If the header is implicit, it will not be transmitted 00555 uint8_t PayloadLength; //!< Size of the payload in the GFSK packet 00556 RadioCrcTypes_t CrcLength; //!< Size of the CRC block in the GFSK packet 00557 RadioDcFree_t DcFree; 00558 }Gfsk; 00559 /*! 00560 * \brief Holds the LoRa packet parameters 00561 */ 00562 struct 00563 { 00564 uint16_t PreambleLength; //!< The preamble length is the number of LoRa symbols in the preamble 00565 RadioLoRaPacketLengthsMode_t HeaderType; //!< If the header is explicit, it will be transmitted in the LoRa packet. If the header is implicit, it will not be transmitted 00566 uint8_t PayloadLength; //!< Size of the payload in the LoRa packet 00567 RadioLoRaCrcModes_t CrcMode; //!< Size of CRC block in LoRa packet 00568 RadioLoRaIQModes_t InvertIQ; //!< Allows to swap IQ for LoRa packet 00569 }LoRa; 00570 }Params; //!< Holds the packet parameters structure 00571 }PacketParams_t; 00572 00573 /*! 00574 * \brief Represents the packet status for every packet type 00575 */ 00576 typedef struct 00577 { 00578 RadioPacketTypes_t packetType; //!< Packet to which the packet status are referring to. 00579 struct 00580 { 00581 struct 00582 { 00583 uint8_t RxStatus; 00584 int8_t RssiAvg; //!< The averaged RSSI 00585 int8_t RssiSync; //!< The RSSI measured on last packet 00586 uint32_t FreqError; 00587 }Gfsk; 00588 struct 00589 { 00590 int8_t RssiPkt; //!< The RSSI of the last packet 00591 int8_t SnrPkt; //!< The SNR of the last packet 00592 int8_t SignalRssiPkt; 00593 uint32_t FreqError; 00594 }LoRa; 00595 }Params; 00596 }PacketStatus_t; 00597 00598 /*! 00599 * \brief Represents the Rx internal counters values when GFSK or LoRa packet type is used 00600 */ 00601 typedef struct 00602 { 00603 RadioPacketTypes_t packetType; //!< Packet to which the packet status are referring to. 00604 uint16_t PacketReceived; 00605 uint16_t CrcOk; 00606 uint16_t LengthError; 00607 }RxCounter_t; 00608 00609 /*! 00610 * \brief Represents a calibration configuration 00611 */ 00612 typedef union 00613 { 00614 struct 00615 { 00616 uint8_t RC64KEnable : 1; //!< Calibrate RC64K clock 00617 uint8_t RC13MEnable : 1; //!< Calibrate RC13M clock 00618 uint8_t PLLEnable : 1; //!< Calibrate PLL 00619 uint8_t ADCPulseEnable : 1; //!< Calibrate ADC Pulse 00620 uint8_t ADCBulkNEnable : 1; //!< Calibrate ADC bulkN 00621 uint8_t ADCBulkPEnable : 1; //!< Calibrate ADC bulkP 00622 uint8_t ImgEnable : 1; 00623 uint8_t : 1; 00624 }Fields; 00625 uint8_t Value; 00626 }CalibrationParams_t; 00627 00628 /*! 00629 * \brief Represents a sleep mode configuration 00630 */ 00631 typedef union 00632 { 00633 struct 00634 { 00635 uint8_t WakeUpRTC : 1; //!< Get out of sleep mode if wakeup signal received from RTC 00636 uint8_t Reset : 1; 00637 uint8_t WarmStart : 1; 00638 uint8_t Reserved : 5; 00639 }Fields; 00640 uint8_t Value; 00641 }SleepParams_t; 00642 00643 /*! 00644 * \brief Represents the possible radio system error states 00645 */ 00646 typedef union 00647 { 00648 struct 00649 { 00650 uint8_t Rc64kCalib : 1; //!< RC 64kHz oscillator calibration failed 00651 uint8_t Rc13mCalib : 1; //!< RC 13MHz oscillator calibration failed 00652 uint8_t PllCalib : 1; //!< PLL calibration failed 00653 uint8_t AdcCalib : 1; //!< ADC calibration failed 00654 uint8_t ImgCalib : 1; //!< Image calibration failed 00655 uint8_t XoscStart : 1; //!< XOSC oscillator failed to start 00656 uint8_t PllLock : 1; //!< PLL lock failed 00657 uint8_t BuckStart : 1; //!< Buck converter failed to start 00658 uint8_t PaRamp : 1; //!< PA ramp failed 00659 uint8_t : 7; //!< Reserved 00660 }Fields; 00661 uint16_t Value; 00662 }RadioError_t; 00663 00664 /*! 00665 * \brief Represents the SX126x and its features 00666 * 00667 * It implements the commands the SX126x can understands 00668 */ 00669 class SX126x : public Radio 00670 { 00671 public: 00672 /*! 00673 * \brief Instantiates a SX126x object and provides API functions to communicates with the radio 00674 * \param [in] callbacks Pointer to the callbacks structure defining 00675 * all callbacks function pointers 00676 */ 00677 SX126x( RadioCallbacks_t *callbacks ): 00678 Radio( callbacks ) 00679 { 00680 this->dioIrq = &SX126x::OnDioIrq; 00681 this->PacketType = PACKET_TYPE_NONE; 00682 this->PollingMode = false; 00683 this->IrqState = false; 00684 } 00685 00686 virtual ~SX126x( ) 00687 { 00688 } 00689 00690 private: 00691 /*! 00692 * \brief Holds the internal operating mode of the radio 00693 */ 00694 RadioOperatingModes_t OperatingMode; 00695 00696 /*! 00697 * \brief Stores the current packet type set in the radio 00698 */ 00699 RadioPacketTypes_t PacketType; 00700 00701 /*! 00702 * \brief Holds a flag raised on radio interrupt 00703 */ 00704 bool IrqState; 00705 00706 /*! 00707 * \brief Hardware DIO IRQ functions 00708 */ 00709 DioIrqHandler dioIrq; 00710 00711 /*! 00712 * \brief Holds the polling state of the driver 00713 */ 00714 bool PollingMode; 00715 00716 protected: 00717 00718 /*! 00719 * \brief Sets a function to be triggered on radio interrupt 00720 * 00721 * \param [in] irqHandler A pointer to a function to be run on interrupt 00722 * from the radio 00723 */ 00724 virtual void IoIrqInit( DioIrqHandler irqHandler ) = 0; 00725 00726 /*! 00727 * \brief DIOs interrupt callback 00728 * 00729 * \remark Called to handle all 3 DIOs pins 00730 */ 00731 void OnDioIrq( void ); 00732 public: 00733 /*! 00734 * \brief Initializes the radio driver 00735 */ 00736 void Init( void ); 00737 00738 /*! 00739 * \brief Gets the current Operation Mode of the Radip 00740 * 00741 * \retval RadioOperatingModes_t last operating mode 00742 */ 00743 virtual RadioOperatingModes_t GetOperatingMode( void ); 00744 00745 /*! 00746 * \brief Wakeup the radio if it is in Sleep mode and check that Busy is low 00747 */ 00748 virtual void CheckDeviceReady( void ); 00749 00750 /*! 00751 * \brief Saves the payload to be send in the radio buffer 00752 * 00753 * \param [in] payload A pointer to the payload 00754 * \param [in] size The size of the payload 00755 */ 00756 void SetPayload( uint8_t *payload, uint8_t size ); 00757 00758 /*! 00759 * \brief Reads the payload received. If the received payload is longer 00760 * than maxSize, then the method returns 1 and do not set size and payload. 00761 * 00762 * \param [out] payload A pointer to a buffer into which the payload will be copied 00763 * \param [out] size A pointer to the size of the payload received 00764 * \param [in] maxSize The maximal size allowed to copy into the buffer 00765 */ 00766 uint8_t GetPayload( uint8_t *payload, uint8_t *size, uint8_t maxSize ); 00767 00768 /*! 00769 * \brief Sends a payload 00770 * 00771 * \param [in] payload A pointer to the payload to send 00772 * \param [in] size The size of the payload to send 00773 * \param [in] timeout The timeout for Tx operation 00774 */ 00775 void SendPayload( uint8_t *payload, uint8_t size, uint32_t timeout ); 00776 00777 /*! 00778 * \brief Sets the Sync Word given by index used in GFSK 00779 * 00780 * \param [in] syncWord SyncWord bytes ( 8 bytes ) 00781 * 00782 * \retval status [0: OK, 1: NOK] 00783 */ 00784 uint8_t SetSyncWord( uint8_t *syncWord ); 00785 00786 /*! 00787 * \brief Sets the Initial value for the LFSR used for the CRC calculation 00788 * 00789 * \param [in] seed Initial LFSR value ( 2 bytes ) 00790 * 00791 */ 00792 void SetCrcSeed( uint16_t seed ); 00793 00794 /*! 00795 * \brief Sets the seed used for the CRC calculation 00796 * 00797 * \param [in] seed The seed value 00798 * 00799 */ 00800 void SetCrcPolynomial( uint16_t seed ); 00801 00802 /*! 00803 * \brief Sets the Initial value of the LFSR used for the whitening in GFSK protocols 00804 * 00805 * \param [in] seed Initial LFSR value 00806 */ 00807 void SetWhiteningSeed( uint16_t seed ); 00808 00809 /*! 00810 * \brief Gets a 32 bits random value generated by the radio 00811 * 00812 * \remark The radio must be in reception mode before executing this function 00813 * 00814 * \retval randomValue 32 bits random value 00815 */ 00816 uint32_t GetRandom( void ); 00817 00818 /*! 00819 * \brief Sets the radio in sleep mode 00820 * 00821 * \param [in] sleepConfig The sleep configuration describing data 00822 * retention and RTC wake-up 00823 */ 00824 void SetSleep( SleepParams_t sleepConfig ); 00825 00826 /*! 00827 * \brief Sets the radio in configuration mode 00828 * 00829 * \param [in] mode The standby mode to put the radio into 00830 */ 00831 void SetStandby( RadioStandbyModes_t mode ); 00832 00833 /*! 00834 * \brief Sets the radio in FS mode 00835 */ 00836 void SetFs( void ); 00837 00838 /*! 00839 * \brief Sets the radio in transmission mode 00840 * 00841 * \param [in] timeout Structure describing the transmission timeout value 00842 */ 00843 void SetTx( uint32_t timeout ); 00844 00845 /*! 00846 * \brief Sets the radio in reception Boosted mode 00847 * 00848 * \param [in] timeout Structure describing the transmission timeout value 00849 */ 00850 void SetRxBoosted( uint32_t timeout ); 00851 00852 /*! 00853 * \brief Sets the radio in reception mode 00854 * 00855 * \param [in] timeout Structure describing the reception timeout value 00856 */ 00857 void SetRx( uint32_t timeout ); 00858 00859 /*! 00860 * \brief Sets the Rx duty cycle management parameters 00861 * 00862 * \param [in] rxTime Structure describing reception timeout value 00863 * \param [in] sleepTime Structure describing sleep timeout value 00864 */ 00865 void SetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime ); 00866 00867 /*! 00868 * \brief Sets the radio in CAD mode 00869 */ 00870 void SetCad( void ); 00871 00872 /*! 00873 * \brief Sets the radio in continuous wave transmission mode 00874 */ 00875 void SetTxContinuousWave( void ); 00876 00877 /*! 00878 * \brief Sets the radio in continuous preamble transmission mode 00879 */ 00880 void SetTxInfinitePreamble( void ); 00881 00882 /*! 00883 * \brief Decide which interrupt will stop the internal radio rx timer. 00884 * 00885 * \param [in] enable [0: Timer stop after header/syncword detection 00886 * 1: Timer stop after preamble detection] 00887 */ 00888 void SetStopRxTimerOnPreambleDetect( bool enable ); 00889 00890 /*! 00891 * \brief Set the number of symbol the radio will wait to validate a reception 00892 * 00893 * \param [in] SymbNum number of LoRa symbols 00894 */ 00895 void SetLoRaSymbNumTimeout( uint8_t SymbNum ); 00896 00897 /*! 00898 * \brief Sets the power regulators operating mode 00899 * 00900 * \param [in] mode [0: LDO, 1:DC_DC] 00901 */ 00902 void SetRegulatorMode( RadioRegulatorMode_t mode ); 00903 00904 /*! 00905 * \brief Calibrates the given radio block 00906 * 00907 * \param [in] calibParam The description of blocks to be calibrated 00908 */ 00909 void Calibrate( CalibrationParams_t calibParam ); 00910 00911 /*! 00912 * \brief Calibrates the Image rejection depending of the frequency 00913 * 00914 * \param [in] freq The operating frequency 00915 */ 00916 void CalibrateImage( uint32_t freq ); 00917 00918 /*! 00919 * \brief Sets the transmission parameters 00920 * 00921 * \param [in] paDutyCycle Duty Cycle for the PA 00922 * \param [in] HpMax 0 for sx1261, 7 for sx1262 00923 * \param [in] deviceSel 1 for sx1261, 0 for sx1262 00924 * \param [in] paLUT 0 for 14dBm LUT, 1 for 22dBm LUT 00925 */ 00926 void SetPaConfig( uint8_t paDutyCycle, uint8_t HpMax, uint8_t deviceSel, uint8_t paLUT ); 00927 00928 /*! 00929 * \brief Defines into which mode the chip goes after a TX / RX done 00930 * 00931 * \param [in] fallbackMode The mode in which the radio goes 00932 */ 00933 void SetRxTxFallbackMode( uint8_t fallbackMode ); 00934 00935 /*! 00936 * \brief Sets the IRQ mask and DIO masks 00937 * 00938 * \param [in] irqMask General IRQ mask 00939 * \param [in] dio1Mask DIO1 mask 00940 * \param [in] dio2Mask DIO2 mask 00941 * \param [in] dio3Mask DIO3 mask 00942 */ 00943 void SetDioIrqParams( uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask ); 00944 00945 /*! 00946 * \brief Returns the current IRQ status 00947 * 00948 * \retval irqStatus IRQ status 00949 */ 00950 uint16_t GetIrqStatus( void ); 00951 00952 /* 00953 * \brief Indicates if DIO2 is used to control an RF Switch 00954 * 00955 * \param [in] enable true of false 00956 */ 00957 void SetDio2AsRfSwitchCtrl( uint8_t enable ); 00958 00959 /* 00960 * \brief Indicates if the Radio main clock is supplied from a tcxo 00961 * 00962 * \param [in] tcxoVoltage voltage used to control the TCXO 00963 * \param [in] timeout time given to the TCXO to go to 32MHz 00964 */ 00965 void SetDio3AsTcxoCtrl( RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout ); 00966 00967 /*! 00968 * \brief Sets the RF frequency 00969 * 00970 * \param [in] frequency RF frequency [Hz] 00971 */ 00972 void SetRfFrequency( uint32_t frequency ); 00973 00974 /*! 00975 * \brief Sets the radio for the given protocol 00976 * 00977 * \param [in] packetType [PACKET_TYPE_GFSK, PACKET_TYPE_LORA] 00978 * 00979 * \remark This method has to be called before SetRfFrequency, 00980 * SetModulationParams and SetPacketParams 00981 */ 00982 void SetPacketType( RadioPacketTypes_t packetType ); 00983 00984 /*! 00985 * \brief Gets the current radio protocol 00986 * 00987 * \retval packetType [PACKET_TYPE_GFSK, PACKET_TYPE_LORA] 00988 */ 00989 RadioPacketTypes_t GetPacketType( void ); 00990 00991 /*! 00992 * \brief Sets the transmission parameters 00993 * 00994 * \param [in] power RF output power [-18..13] dBm 00995 * \param [in] rampTime Transmission ramp up time 00996 */ 00997 void SetTxParams( int8_t power, RadioRampTimes_t rampTime ); 00998 00999 /*! 01000 * \brief Set the modulation parameters 01001 * 01002 * \param [in] modParams A structure describing the modulation parameters 01003 */ 01004 void SetModulationParams( ModulationParams_t *modParams ); 01005 01006 /*! 01007 * \brief Sets the packet parameters 01008 * 01009 * \param [in] packetParams A structure describing the packet parameters 01010 */ 01011 void SetPacketParams( PacketParams_t *packetParams ); 01012 01013 /*! 01014 * \brief Sets the Channel Activity Detection (CAD) parameters 01015 * 01016 * \param [in] cadSymbolNum The number of symbol to use for CAD operations 01017 * [LORA_CAD_01_SYMBOL, LORA_CAD_02_SYMBOL, 01018 * LORA_CAD_04_SYMBOL, LORA_CAD_08_SYMBOL, 01019 * LORA_CAD_16_SYMBOL] 01020 * \param [in] cadDetPeak Limite for detection of SNR peak used in the CAD 01021 * \param [in] cadDetMin Set the minimum symbol recognition for CAD 01022 * \param [in] cadExitMode Operation to be done at the end of CAD action 01023 * [LORA_CAD_ONLY, LORA_CAD_RX, LORA_CAD_LBT] 01024 * \param [in] cadTimeout Defines the timeout value to abort the CAD activity 01025 */ 01026 void SetCadParams( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, RadioCadExitModes_t cadExitMode, uint32_t cadTimeout ); 01027 01028 /*! 01029 * \brief Sets the data buffer base address for transmission and reception 01030 * 01031 * \param [in] txBaseAddress Transmission base address 01032 * \param [in] rxBaseAddress Reception base address 01033 */ 01034 void SetBufferBaseAddresses( uint8_t txBaseAddress, uint8_t rxBaseAddress ); 01035 01036 /*! 01037 * \brief Gets the current radio status 01038 * 01039 * \retval status Radio status 01040 */ 01041 virtual RadioStatus_t GetStatus( void ); 01042 01043 /*! 01044 * \brief Returns the instantaneous RSSI value for the last packet received 01045 * 01046 * \retval rssiInst Instantaneous RSSI 01047 */ 01048 int8_t GetRssiInst( void ); 01049 01050 /*! 01051 * \brief Gets the last received packet buffer status 01052 * 01053 * \param [out] payloadLength Last received packet payload length 01054 * \param [out] rxStartBuffer Last received packet buffer address pointer 01055 */ 01056 void GetRxBufferStatus( uint8_t *payloadLength, uint8_t *rxStartBuffer ); 01057 01058 /*! 01059 * \brief Gets the last received packet payload length 01060 * 01061 * \param [out] pktStatus A structure of packet status 01062 */ 01063 void GetPacketStatus( PacketStatus_t *pktStatus ); 01064 01065 /*! 01066 * \brief Returns the possible system erros 01067 * 01068 * \retval sysErrors Value representing the possible sys failures 01069 */ 01070 RadioError_t GetDeviceErrors( void ); 01071 01072 /*! 01073 * \brief Clears the IRQs 01074 * 01075 * \param [in] irq IRQ(s) to be cleared 01076 */ 01077 void ClearIrqStatus( uint16_t irq ); 01078 01079 /*! 01080 * \brief Set the driver in polling mode. 01081 * 01082 * In polling mode the application is responsible to call ProcessIrqs( ) to 01083 * execute callbacks functions. 01084 * The default mode is Interrupt Mode. 01085 * @code 01086 * // Initializations and callbacks declaration/definition 01087 * radio = SX126x( mosi, miso, sclk, nss, busy, int1, int2, int3, rst, &callbacks ); 01088 * radio.Init( ); 01089 * radio.SetPollingMode( ); 01090 * 01091 * while( true ) 01092 * { 01093 * // IRQ processing is automatically done 01094 * radio.ProcessIrqs( ); // <-- here, as well as callback functions 01095 * // calls 01096 * // Do some applicative work 01097 * } 01098 * @endcode 01099 * 01100 * \see SX126x::SetInterruptMode 01101 */ 01102 void SetPollingMode( void ); 01103 01104 /*! 01105 * \brief Set the driver in interrupt mode. 01106 * 01107 * In interrupt mode, the driver communicate with the radio during the 01108 * interruption by direct calls to ProcessIrqs( ). The main advantage is 01109 * the possibility to have low power application architecture. 01110 * This is the default mode. 01111 * @code 01112 * // Initializations and callbacks declaration/definition 01113 * radio = SX126x( mosi, miso, sclk, nss, busy, int1, int2, int3, rst, &callbacks ); 01114 * radio.Init( ); 01115 * radio.SetInterruptMode( ); // Optionnal. Driver default behavior 01116 * 01117 * while( true ) 01118 * { 01119 * // Do some applicative work 01120 * } 01121 * @endcode 01122 * 01123 * \see SX126x::SetPollingMode 01124 */ 01125 void SetInterruptMode( void ); 01126 01127 /*! 01128 * \brief Resets the radio 01129 */ 01130 virtual void Reset( void ) = 0; 01131 01132 /*! 01133 * \brief Wake-ups the radio from Sleep mode 01134 */ 01135 virtual void Wakeup( void ) = 0; 01136 01137 /*! 01138 * \brief Writes the given command to the radio 01139 * 01140 * \param [in] opcode Command opcode 01141 * \param [in] buffer Command parameters byte array 01142 * \param [in] size Command parameters byte array size 01143 */ 01144 virtual void WriteCommand( RadioCommands_t opcode, uint8_t *buffer, uint16_t size ) = 0; 01145 01146 /*! 01147 * \brief Reads the given command from the radio 01148 * 01149 * \param [in] opcode Command opcode 01150 * \param [in] buffer Command parameters byte array 01151 * \param [in] size Command parameters byte array size 01152 */ 01153 virtual void ReadCommand( RadioCommands_t opcode, uint8_t *buffer, uint16_t size ) = 0; 01154 01155 /*! 01156 * \brief Writes multiple radio registers starting at address 01157 * 01158 * \param [in] address First Radio register address 01159 * \param [in] buffer Buffer containing the new register's values 01160 * \param [in] size Number of registers to be written 01161 */ 01162 virtual void WriteRegister( uint16_t address, uint8_t *buffer, uint16_t size ) = 0; 01163 01164 /*! 01165 * \brief Writes the radio register at the specified address 01166 * 01167 * \param [in] address Register address 01168 * \param [in] value New register value 01169 */ 01170 virtual void WriteReg( uint16_t address, uint8_t value ) = 0; 01171 01172 /*! 01173 * \brief Reads multiple radio registers starting at address 01174 * 01175 * \param [in] address First Radio register address 01176 * \param [out] buffer Buffer where to copy the registers data 01177 * \param [in] size Number of registers to be read 01178 */ 01179 virtual void ReadRegister( uint16_t address, uint8_t *buffer, uint16_t size ) = 0; 01180 01181 /*! 01182 * \brief Reads the radio register at the specified address 01183 * 01184 * \param [in] address Register address 01185 * 01186 * \retval data Register value 01187 */ 01188 virtual uint8_t ReadReg( uint16_t address ) = 0; 01189 01190 /*! 01191 * \brief Writes Radio Data Buffer with buffer of size starting at offset. 01192 * 01193 * \param [in] offset Offset where to start writing 01194 * \param [in] buffer Buffer pointer 01195 * \param [in] size Buffer size 01196 */ 01197 virtual void WriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size ) = 0; 01198 01199 /*! 01200 * \brief Reads Radio Data Buffer at offset to buffer of size 01201 * 01202 * \param [in] offset Offset where to start reading 01203 * \param [out] buffer Buffer pointer 01204 * \param [in] size Buffer size 01205 */ 01206 virtual void ReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size ) = 0; 01207 01208 /*! 01209 * \brief Gets the current status of the radio DIOs 01210 * 01211 * \retval status [Bit #3: DIO3, Bit #2: DIO2, 01212 * Bit #1: DIO1, Bit #0: BUSY] 01213 */ 01214 virtual uint8_t GetDioStatus( void ) = 0; 01215 01216 /*! 01217 * \brief Returns the device type 01218 * 01219 * \retval 0: SX1261, 1: SX1262, 2: SX1268 01220 */ 01221 virtual uint8_t GetDeviceType( void ) = 0; 01222 01223 /*! 01224 * \brief Returns the matching frequency 01225 * 01226 * \retval 1: 868 MHz 01227 * 0: 915 MHz 01228 */ 01229 virtual uint8_t GetFreqSelect( void ) = 0; 01230 01231 /*! 01232 * \brief RF Switch power on 01233 */ 01234 virtual void AntSwOn( void ) = 0; 01235 01236 /*! 01237 * \brief RF Switch power off 01238 */ 01239 virtual void AntSwOff( void ) = 0; 01240 01241 /*! 01242 * \brief Process the analysis of radio IRQs and calls callback functions 01243 * depending on radio state 01244 */ 01245 void ProcessIrqs( void ); 01246 }; 01247 01248 #endif // __SX126x_H__
Generated on Tue Jul 12 2022 15:50:44 by
