Para o mario

Dependencies:   mbed SoftSerial USBDevice Buffer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sx1276.h Source File

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