Fork of SX1276 Lib

Fork of SX1276Lib by Semtech

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 #ifndef __SX1276_H__
00016 #define __SX1276_H__
00017 
00018 #include "radio.h"
00019 #include "./registers/sx1276Regs-Fsk.h"
00020 #include "./registers/sx1276Regs-LoRa.h"
00021 #include "./typedefs/typedefs.h"
00022 
00023 
00024 
00025 /*!
00026  * Radio wakeup time from SLEEP mode
00027  */
00028 #define RADIO_WAKEUP_TIME                           1000 // [us]
00029 
00030 /*!
00031  * SX1276 definitions
00032  */
00033 #define XTAL_FREQ                                   32000000
00034 #define FREQ_STEP                                   61.03515625
00035 
00036 #define RX_BUFFER_SIZE                              256
00037 
00038 /*!
00039  * Constant values need to compute the RSSI value
00040  */
00041 #define RSSI_OFFSET_LF                              -164.0
00042 #define RSSI_OFFSET_HF                              -157.0
00043 
00044 #define RF_MID_BAND_THRESH                          525000000
00045 
00046 /*! 
00047  * Actual implementation of a SX1276 radio, inherits Radio
00048  */
00049 class SX1276  : public Radio 
00050 {
00051 protected:
00052     /*!
00053     * SPI Interface
00054     */
00055     SPI spi ; // mosi, miso, sclk
00056     DigitalOut nss;  
00057 
00058     /*!
00059      * SX1276 Reset pin
00060      */
00061     DigitalInOut reset ;
00062 
00063     /*!
00064      * SX1276 DIO pins
00065      */
00066     InterruptIn dio0 ;
00067     InterruptIn dio1;
00068     InterruptIn dio2; 
00069     InterruptIn dio3;
00070     InterruptIn dio4;
00071     DigitalIn dio5;
00072     
00073     bool isRadioActive;
00074     
00075     uint8_t boardConnected; //1 = SX1276MB1LAS; 0 = SX1276MB1MAS
00076     
00077     uint8_t *rxBuffer;
00078     
00079     uint8_t previousOpMode;
00080     
00081     /*!
00082      * Hardware DIO IRQ functions
00083      */
00084     DioIrqHandler *dioIrq ;
00085     
00086     /*!
00087      * Tx and Rx timers
00088      */
00089     Timeout txTimeoutTimer ;
00090     Timeout rxTimeoutTimer;
00091     Timeout rxTimeoutSyncWord;
00092     
00093     /*!
00094      *  rxTx: [1: Tx, 0: Rx]
00095      */
00096     uint8_t rxTx ;
00097     
00098     RadioSettings_t  settings;
00099     
00100     static const FskBandwidth_t  FskBandwidths[] ;
00101 protected:
00102 
00103     /*!
00104     * Performs the Rx chain calibration for LF and HF bands
00105     * \remark Must be called just after the reset so all registers are at their
00106     *         default values
00107     */
00108     void RxChainCalibration ( void );
00109 
00110 public:
00111     SX1276 ( RadioEvents_t *events,
00112             PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset ,
00113             PinName dio0 , PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 ); 
00114     SX1276 ( RadioEvents_t *events );
00115     virtual ~SX1276 ( );
00116     
00117     //-------------------------------------------------------------------------
00118     //                        Redefined Radio functions
00119     //-------------------------------------------------------------------------
00120     /*!
00121      * @brief Initializes the radio
00122      *
00123      * @param [IN] events Structure containing the driver callback functions
00124      */
00125     virtual void Init( RadioEvents_t *events );
00126     /*!
00127      * Return current radio status
00128      *
00129      * @param status Radio status. [RF_IDLE, RX_RUNNING, TX_RUNNING]
00130      */
00131     virtual RadioState GetStatus ( void ); 
00132     
00133     /*!
00134      * @brief Configures the SX1276 with the given modem
00135      *
00136      * @param [IN] modem Modem to be used [0: FSK, 1: LoRa] 
00137      */
00138     virtual void SetModem( RadioModems_t modem );
00139 
00140     /*!
00141      * @brief Sets the channel frequency
00142      *
00143      * @param [IN] freq         Channel RF frequency
00144      */
00145     virtual void SetChannel( uint32_t freq );
00146     
00147     /*!
00148      * @brief Sets the channels configuration
00149      *
00150      * @param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
00151      * @param [IN] freq       Channel RF frequency
00152      * @param [IN] rssiThresh RSSI threshold
00153      *
00154      * @retval isFree         [true: Channel is free, false: Channel is not free]
00155      */
00156     virtual bool IsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh );
00157     
00158     /*!
00159      * @brief Generates a 32 bits random value based on the RSSI readings
00160      *
00161      * \remark This function sets the radio in LoRa modem mode and disables
00162      *         all interrupts.
00163      *         After calling this function either Radio.SetRxConfig or
00164      *         Radio.SetTxConfig functions must be called.
00165      *
00166      * @retval randomValue    32 bits random value
00167      */
00168     virtual uint32_t Random( void );
00169     
00170     /*!
00171      * @brief Sets the reception parameters
00172      *
00173      * @param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
00174      * @param [IN] bandwidth    Sets the bandwidth
00175      *                          FSK : >= 2600 and <= 250000 Hz
00176      *                          LoRa: [0: 125 kHz, 1: 250 kHz,
00177      *                                 2: 500 kHz, 3: Reserved]
00178      * @param [IN] datarate     Sets the Datarate
00179      *                          FSK : 600..300000 bits/s
00180      *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
00181      *                                10: 1024, 11: 2048, 12: 4096  chips]
00182      * @param [IN] coderate     Sets the coding rate ( LoRa only )
00183      *                          FSK : N/A ( set to 0 )
00184      *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
00185      * @param [IN] bandwidthAfc Sets the AFC Bandwidth ( FSK only )
00186      *                          FSK : >= 2600 and <= 250000 Hz
00187      *                          LoRa: N/A ( set to 0 )
00188      * @param [IN] preambleLen  Sets the Preamble length ( LoRa only )
00189      *                          FSK : N/A ( set to 0 )
00190      *                          LoRa: Length in symbols ( the hardware adds 4 more symbols )
00191      * @param [IN] symbTimeout  Sets the RxSingle timeout value ( LoRa only )
00192      *                          FSK : N/A ( set to 0 )
00193      *                          LoRa: timeout in symbols
00194      * @param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
00195      * @param [IN] payloadLen   Sets payload length when fixed lenght is used
00196      * @param [IN] crcOn        Enables/Disables the CRC [0: OFF, 1: ON]
00197      * @param [IN] freqHopOn    Enables disables the intra-packet frequency hopping  [0: OFF, 1: ON] (LoRa only)
00198      * @param [IN] hopPeriod    Number of symbols bewteen each hop (LoRa only)
00199      * @param [IN] iqInverted   Inverts IQ signals ( LoRa only )
00200      *                          FSK : N/A ( set to 0 )
00201      *                          LoRa: [0: not inverted, 1: inverted]
00202      * @param [IN] rxContinuous Sets the reception in continuous mode
00203      *                          [false: single mode, true: continuous mode]
00204      */
00205     virtual void SetRxConfig ( RadioModems_t modem, uint32_t bandwidth,
00206                                uint32_t datarate, uint8_t coderate,
00207                                uint32_t bandwidthAfc, uint16_t preambleLen,
00208                                uint16_t symbTimeout, bool fixLen,
00209                                uint8_t payloadLen,
00210                                bool crcOn, bool freqHopOn, uint8_t hopPeriod,
00211                                bool iqInverted, bool rxContinuous );
00212     
00213     /*!
00214      * @brief Sets the transmission parameters
00215      *
00216      * @param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
00217      * @param [IN] power        Sets the output power [dBm]
00218      * @param [IN] fdev         Sets the frequency deviation ( FSK only )
00219      *                          FSK : [Hz]
00220      *                          LoRa: 0
00221      * @param [IN] bandwidth    Sets the bandwidth ( LoRa only )
00222      *                          FSK : 0
00223      *                          LoRa: [0: 125 kHz, 1: 250 kHz,
00224      *                                 2: 500 kHz, 3: Reserved]
00225      * @param [IN] datarate     Sets the Datarate
00226      *                          FSK : 600..300000 bits/s
00227      *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
00228      *                                10: 1024, 11: 2048, 12: 4096  chips]
00229      * @param [IN] coderate     Sets the coding rate ( LoRa only )
00230      *                          FSK : N/A ( set to 0 )
00231      *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
00232      * @param [IN] preambleLen  Sets the preamble length
00233      * @param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
00234      * @param [IN] crcOn        Enables disables the CRC [0: OFF, 1: ON]
00235      * @param [IN] freqHopOn    Enables disables the intra-packet frequency hopping  [0: OFF, 1: ON] (LoRa only)
00236      * @param [IN] hopPeriod    Number of symbols bewteen each hop (LoRa only)
00237      * @param [IN] iqInverted   Inverts IQ signals ( LoRa only )
00238      *                          FSK : N/A ( set to 0 )
00239      *                          LoRa: [0: not inverted, 1: inverted]
00240      * @param [IN] timeout      Transmission timeout [us]
00241      */
00242     virtual void SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
00243                               uint32_t bandwidth, uint32_t datarate,
00244                               uint8_t coderate, uint16_t preambleLen,
00245                               bool fixLen, bool crcOn, bool freqHopOn,
00246                               uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
00247     
00248     /*!
00249      * @brief Computes the packet time on air for the given payload
00250      *
00251      * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
00252      *
00253      * @param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
00254      * @param [IN] pktLen     Packet payload length
00255      *
00256      * @retval airTime        Computed airTime for the given packet payload length
00257      */
00258     virtual double TimeOnAir ( RadioModems_t modem, uint8_t pktLen );
00259     
00260     /*!
00261      * @brief Sends the buffer of size. Prepares the packet to be sent and sets
00262      *        the radio in transmission
00263      *
00264      * @param [IN]: buffer     Buffer pointer
00265      * @param [IN]: size       Buffer size
00266      */
00267     virtual void Send( uint8_t *buffer, uint8_t size );
00268     
00269     /*!
00270      * @brief Sets the radio in sleep mode
00271      */
00272     virtual void Sleep( void );
00273     
00274     /*!
00275      * @brief Sets the radio in standby mode
00276      */
00277     virtual void Standby( void );
00278     
00279     /*!
00280      * @brief Sets the radio in reception mode for the given time
00281      * @param [IN] timeout Reception timeout [us]
00282      *                     [0: continuous, others timeout]
00283      */
00284     virtual void Rx( uint32_t timeout );
00285     
00286     /*!
00287      * @brief Sets the radio in transmission mode for the given time
00288      * @param [IN] timeout Transmission timeout [us]
00289      *                     [0: continuous, others timeout]
00290      */
00291     virtual void Tx( uint32_t timeout );
00292     
00293     /*!
00294      * @brief Start a Channel Activity Detection
00295      */
00296     virtual void StartCad( void );    
00297     
00298     /*!
00299      * @brief Reads the current RSSI value
00300      *
00301      * @retval rssiValue Current RSSI value in [dBm]
00302      */
00303     virtual int16_t GetRssi ( RadioModems_t modem );
00304     
00305     /*!
00306      * @brief Writes the radio register at the specified address
00307      *
00308      * @param [IN]: addr Register address
00309      * @param [IN]: data New register value
00310      */
00311     virtual void Write ( uint8_t addr, uint8_t data ) = 0;
00312     
00313     /*!
00314      * @brief Reads the radio register at the specified address
00315      *
00316      * @param [IN]: addr Register address
00317      * @retval data Register value
00318      */
00319     virtual uint8_t Read ( uint8_t addr ) = 0;
00320     
00321     /*!
00322      * @brief Writes multiple radio registers starting at address
00323      *
00324      * @param [IN] addr   First Radio register address
00325      * @param [IN] buffer Buffer containing the new register's values
00326      * @param [IN] size   Number of registers to be written
00327      */
00328     virtual void Write( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
00329     
00330     /*!
00331      * @brief Reads multiple radio registers starting at address
00332      *
00333      * @param [IN] addr First Radio register address
00334      * @param [OUT] buffer Buffer where to copy the registers data
00335      * @param [IN] size Number of registers to be read
00336      */
00337     virtual void Read ( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
00338     
00339     /*!
00340      * @brief Writes the buffer contents to the SX1276 FIFO
00341      *
00342      * @param [IN] buffer Buffer containing data to be put on the FIFO.
00343      * @param [IN] size Number of bytes to be written to the FIFO
00344      */
00345     virtual void WriteFifo( uint8_t *buffer, uint8_t size ) = 0;
00346 
00347     /*!
00348      * @brief Reads the contents of the SX1276 FIFO
00349      *
00350      * @param [OUT] buffer Buffer where to copy the FIFO read data.
00351      * @param [IN] size Number of bytes to be read from the FIFO
00352      */
00353     virtual void ReadFifo( uint8_t *buffer, uint8_t size ) = 0;
00354     /*!
00355      * @brief Resets the SX1276
00356      */
00357     virtual void Reset( void ) = 0;
00358     
00359     /*!
00360      * @brief Sets the maximum payload length.
00361      *
00362      * @param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
00363      * @param [IN] max        Maximum payload length in bytes
00364      */
00365     virtual void SetMaxPayloadLength( RadioModems_t modem, uint8_t max );
00366     
00367     //-------------------------------------------------------------------------
00368     //                        Board relative functions
00369     //-------------------------------------------------------------------------
00370     
00371 protected:
00372     /*!
00373      * @brief Initializes the radio I/Os pins interface
00374      */
00375     virtual void IoInit( void ) = 0;
00376     
00377     /*!
00378      *    @brief Initializes the radio registers
00379      */
00380     virtual void RadioRegistersInit( ) = 0;
00381     
00382     /*!
00383      * @brief Initializes the radio SPI
00384      */
00385     virtual void SpiInit( void ) = 0;
00386     
00387     /*!
00388      * @brief Initializes DIO IRQ handlers
00389      *
00390      * @param [IN] irqHandlers Array containing the IRQ callback functions
00391      */
00392     virtual void IoIrqInit( DioIrqHandler *irqHandlers ) = 0;
00393 
00394     /*!
00395      * @brief De-initializes the radio I/Os pins interface. 
00396      *
00397      * \remark Useful when going in MCU lowpower modes
00398      */
00399     virtual void IoDeInit( void ) = 0;
00400 
00401     /*!
00402      * @brief Gets the board PA selection configuration
00403      *
00404      * @param [IN] channel Channel frequency in Hz
00405      * @retval PaSelect RegPaConfig PaSelect value
00406      */
00407     virtual uint8_t GetPaSelect( uint32_t channel ) = 0;
00408 
00409     /*!
00410      * @brief Set the RF Switch I/Os pins in Low Power mode
00411      *
00412      * @param [IN] status enable or disable
00413      */
00414     virtual void SetAntSwLowPower( bool status ) = 0;
00415 
00416     /*!
00417      * @brief Initializes the RF Switch I/Os pins interface
00418      */
00419     virtual void AntSwInit( void ) = 0;
00420 
00421     /*!
00422      * @brief De-initializes the RF Switch I/Os pins interface 
00423      *
00424      * \remark Needed to decrease the power consumption in MCU lowpower modes
00425      */
00426     virtual void AntSwDeInit( void ) = 0;
00427 
00428     /*!
00429      * @brief Controls the antena switch if necessary.
00430      *
00431      * \remark see errata note
00432      *
00433      * @param [IN] rxTx [1: Tx, 0: Rx]
00434      */
00435     virtual void SetAntSw( uint8_t rxTx  ) = 0;
00436     
00437     /*!
00438      * @brief Checks if the given RF frequency is supported by the hardware
00439      *
00440      * @param [IN] frequency RF frequency to be checked
00441      * @retval isSupported [true: supported, false: unsupported]
00442      */
00443     virtual bool CheckRfFrequency( uint32_t frequency ) = 0;
00444 protected:
00445 
00446     /*!
00447      * @brief Sets the SX1276 operating mode
00448      *
00449      * @param [IN] opMode New operating mode
00450      */
00451     virtual void SetOpMode( uint8_t opMode );
00452 
00453     /*
00454      * SX1276 DIO IRQ callback functions prototype
00455      */
00456 
00457     /*!
00458      * @brief DIO 0 IRQ callback
00459      */
00460     virtual void OnDio0Irq( void );
00461 
00462     /*!
00463      * @brief DIO 1 IRQ callback
00464      */
00465     virtual void OnDio1Irq( void );
00466 
00467     /*!
00468      * @brief DIO 2 IRQ callback
00469      */
00470     virtual void OnDio2Irq( void );
00471 
00472     /*!
00473      * @brief DIO 3 IRQ callback
00474      */
00475     virtual void OnDio3Irq( void );
00476 
00477     /*!
00478      * @brief DIO 4 IRQ callback
00479      */
00480     virtual void OnDio4Irq( void );
00481 
00482     /*!
00483      * @brief DIO 5 IRQ callback
00484      */
00485     virtual void OnDio5Irq( void );
00486 
00487     /*!
00488      * @brief Tx & Rx timeout timer callback
00489      */
00490     virtual void OnTimeoutIrq( void );
00491     
00492     /*!
00493      * Returns the known FSK bandwidth registers value
00494      *
00495      * \param [IN] bandwidth Bandwidth value in Hz
00496      * \retval regValue Bandwidth register value.
00497      */
00498     static uint8_t GetFskBandwidthRegValue ( uint32_t bandwidth );
00499 };
00500 
00501 #endif // __SX1276_H__