Uttam Bhat / SX1272Lib

Fork of SX1272Lib by Semtech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sx1272.h Source File

sx1272.h

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