first draft

Dependents:   LoRaWAN-demo-72_tjm frdm_LoRa_Connect_Woodstream_Demo_tjm frdm_LoRa_Connect_Woodstream_Demo_jlc

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