This Library is to establish communication between a transmitter and receivers formed by the combination of SX1272MB2DAS shield and FRDM kl25z board that uses Cortex M0+ architecture.

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