I-O DATA DEV2 / Private_lora_SX1276

Dependents:   Nucleo_Private_LoRa

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