pin pong

Dependents:   SX1272PingPong

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