20171208

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