Lorenzo Maiorfi / SX1272Lib_RTOS

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 
00101     // <RTOS>
00102     //Thread* _thread_events_queue;
00103     EventQueue* _eq_events;
00104     // </RTOS>
00105 
00106 protected:
00107 
00108     /*!
00109     * Performs the Rx chain calibration for LF and HF bands
00110     * \remark Must be called just after the reset so all registers are at their
00111     *         default values
00112     */
00113     void RxChainCalibration ( void );
00114 
00115 public:
00116     SX1272 ( RadioEvents_t *events,
00117             PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset ,
00118             PinName dio0 , PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 );
00119     SX1272 ( RadioEvents_t *events );
00120     virtual ~SX1272 ( );
00121 
00122     // <RTOS>
00123     //void assign_events_queue_thread(Thread* thread) {_thread_events_queue=thread;}
00124     void assign_events_queue(EventQueue* event_queue) {_eq_events=event_queue;}
00125     // </RTOS>
00126     
00127     //-------------------------------------------------------------------------
00128     //                        Redefined Radio functions
00129     //-------------------------------------------------------------------------
00130     /*!
00131      * @brief Initializes the radio
00132      *
00133      * @param [IN] events Structure containing the driver callback functions
00134      */
00135     virtual void Init( RadioEvents_t *events );
00136     /*!
00137      * Return current radio status
00138      *
00139      * @param status Radio status. [RF_IDLE, RX_RUNNING, TX_RUNNING]
00140      */
00141     virtual RadioState GetStatus ( void ); 
00142     /*!
00143      * @brief Configures the SX1272 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      * @brief Sets the channel frequency
00150      *
00151      * @param [IN] freq         Channel RF frequency
00152      */
00153     virtual void SetChannel( uint32_t freq );
00154     /*!
00155      * @brief Sets the channels configuration
00156      *
00157      * @param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
00158      * @param [IN] freq       Channel RF frequency
00159      * @param [IN] rssiThresh RSSI threshold
00160      *
00161      * @retval isFree         [true: Channel is free, false: Channel is not free]
00162      */
00163     virtual bool IsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh );
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      * @brief Sets the reception parameters
00177      *
00178      * @param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
00179      * @param [IN] bandwidth    Sets the bandwidth
00180      *                          FSK : >= 2600 and <= 250000 Hz
00181      *                          LoRa: [0: 125 kHz, 1: 250 kHz,
00182      *                                 2: 500 kHz, 3: Reserved]
00183      * @param [IN] datarate     Sets the Datarate
00184      *                          FSK : 600..300000 bits/s
00185      *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
00186      *                                10: 1024, 11: 2048, 12: 4096  chips]
00187      * @param [IN] coderate     Sets the coding rate ( LoRa only )
00188      *                          FSK : N/A ( set to 0 )
00189      *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
00190      * @param [IN] bandwidthAfc Sets the AFC Bandwidth ( FSK only )
00191      *                          FSK : >= 2600 and <= 250000 Hz
00192      *                          LoRa: N/A ( set to 0 )
00193      * @param [IN] preambleLen  Sets the Preamble length ( LoRa only )
00194      *                          FSK : N/A ( set to 0 )
00195      *                          LoRa: Length in symbols ( the hardware adds 4 more symbols )
00196      * @param [IN] symbTimeout  Sets the RxSingle timeout value
00197      *                          FSK : timeout number of bytes
00198      *                          LoRa: timeout in symbols
00199      * @param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
00200      * @param [IN] payloadLen   Sets payload length when fixed lenght is used
00201      * @param [IN] crcOn        Enables/Disables the CRC [0: OFF, 1: ON]
00202      * @param [IN] freqHopOn    Enables disables the intra-packet frequency hopping  [0: OFF, 1: ON] (LoRa only)
00203      * @param [IN] hopPeriod    Number of symbols bewteen each hop (LoRa only)
00204      * @param [IN] iqInverted   Inverts IQ signals ( LoRa only )
00205      *                          FSK : N/A ( set to 0 )
00206      *                          LoRa: [0: not inverted, 1: inverted]
00207      * @param [IN] rxContinuous Sets the reception in continuous mode
00208      *                          [false: single mode, true: continuous mode]
00209      */
00210     virtual void SetRxConfig ( RadioModems_t modem, uint32_t bandwidth,
00211                                uint32_t datarate, uint8_t coderate,
00212                                uint32_t bandwidthAfc, uint16_t preambleLen,
00213                                uint16_t symbTimeout, bool fixLen,
00214                                uint8_t payloadLen,
00215                                bool crcOn, bool freqHopOn, uint8_t hopPeriod,
00216                                bool iqInverted, bool rxContinuous );
00217     /*!
00218      * @brief Sets the transmission parameters
00219      *
00220      * @param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
00221      * @param [IN] power        Sets the output power [dBm]
00222      * @param [IN] fdev         Sets the frequency deviation ( FSK only )
00223      *                          FSK : [Hz]
00224      *                          LoRa: 0
00225      * @param [IN] bandwidth    Sets the bandwidth ( LoRa only )
00226      *                          FSK : 0
00227      *                          LoRa: [0: 125 kHz, 1: 250 kHz,
00228      *                                 2: 500 kHz, 3: Reserved]
00229      * @param [IN] datarate     Sets the Datarate
00230      *                          FSK : 600..300000 bits/s
00231      *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
00232      *                                10: 1024, 11: 2048, 12: 4096  chips]
00233      * @param [IN] coderate     Sets the coding rate ( LoRa only )
00234      *                          FSK : N/A ( set to 0 )
00235      *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
00236      * @param [IN] preambleLen  Sets the preamble length
00237      * @param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
00238      * @param [IN] crcOn        Enables disables the CRC [0: OFF, 1: ON]
00239      * @param [IN] freqHopOn    Enables disables the intra-packet frequency hopping  [0: OFF, 1: ON] (LoRa only)
00240      * @param [IN] hopPeriod    Number of symbols bewteen each hop (LoRa only)
00241      * @param [IN] iqInverted   Inverts IQ signals ( LoRa only )
00242      *                          FSK : N/A ( set to 0 )
00243      *                          LoRa: [0: not inverted, 1: inverted]
00244      * @param [IN] timeout      Transmission timeout [ms]
00245      */
00246     virtual void SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
00247                               uint32_t bandwidth, uint32_t datarate,
00248                               uint8_t coderate, uint16_t preambleLen,
00249                               bool fixLen, bool crcOn, bool freqHopOn,
00250                               uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
00251     /*!
00252      * @brief Checks if the given RF frequency is supported by the hardware
00253      *
00254      * @param [IN] frequency RF frequency to be checked
00255      * @retval isSupported [true: supported, false: unsupported]
00256      */
00257     virtual bool CheckRfFrequency( uint32_t frequency ) = 0;
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 uint32_t TimeOnAir ( RadioModems_t modem, uint8_t pktLen );
00269     /*!
00270      * @brief Sends the buffer of size. Prepares the packet to be sent and sets
00271      *        the radio in transmission
00272      *
00273      * @param [IN]: buffer     Buffer pointer
00274      * @param [IN]: size       Buffer size
00275      */
00276     virtual void Send( uint8_t *buffer, uint8_t size );
00277     /*!
00278      * @brief Sets the radio in sleep mode
00279      */
00280     virtual void Sleep( void );
00281     /*!
00282      * @brief Sets the radio in standby mode
00283      */
00284     virtual void Standby( void );
00285     /*!
00286      * @brief Sets the radio in CAD mode
00287      */
00288     virtual void StartCad( void );
00289     /*!
00290      * @brief Sets the radio in reception mode for the given time
00291      * @param [IN] timeout Reception timeout [ms]
00292      *                     [0: continuous, others timeout]
00293      */
00294     virtual void Rx( uint32_t timeout );
00295     /*!
00296      * @brief Sets the radio in transmission mode for the given time
00297      * @param [IN] timeout Transmission timeout [ms]
00298      *                     [0: continuous, others timeout]
00299      */
00300     virtual void Tx( uint32_t timeout );
00301     /*!
00302      * @brief Sets the radio in continuous wave transmission mode
00303      *
00304      * @param [IN]: freq       Channel RF frequency
00305      * @param [IN]: power      Sets the output power [dBm]
00306      * @param [IN]: time       Transmission mode timeout [s]
00307      */
00308     virtual void SetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time );
00309     /*!
00310      * @brief Reads the current RSSI value
00311      *
00312      * @retval rssiValue Current RSSI value in [dBm]
00313      */
00314     virtual int16_t GetRssi ( RadioModems_t modem );
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      * @brief Reads the radio register at the specified address
00324      *
00325      * @param [IN]: addr Register address
00326      * @retval data Register value
00327      */
00328     virtual uint8_t Read ( uint8_t addr ) = 0;
00329     /*!
00330      * @brief Writes multiple radio registers starting at address
00331      *
00332      * @param [IN] addr   First Radio register address
00333      * @param [IN] buffer Buffer containing the new register's values
00334      * @param [IN] size   Number of registers to be written
00335      */
00336     virtual void Write( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
00337     /*!
00338      * @brief Reads multiple radio registers starting at address
00339      *
00340      * @param [IN] addr First Radio register address
00341      * @param [OUT] buffer Buffer where to copy the registers data
00342      * @param [IN] size Number of registers to be read
00343      */
00344     virtual void Read ( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
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      * @brief Reads the contents of the SX1272 FIFO
00354      *
00355      * @param [OUT] buffer Buffer where to copy the FIFO read data.
00356      * @param [IN] size Number of bytes to be read from the FIFO
00357      */
00358     virtual void ReadFifo( uint8_t *buffer, uint8_t size ) = 0;
00359     /*!
00360      * @brief Resets the SX1272
00361      */
00362     virtual void Reset( void ) = 0;
00363 
00364     /*!
00365      * @brief Sets the maximum payload length.
00366      *
00367      * @param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
00368      * @param [IN] max        Maximum payload length in bytes
00369      */
00370     virtual void SetMaxPayloadLength( RadioModems_t modem, uint8_t max );
00371 
00372     /*!
00373      * \brief Sets the network to public or private. Updates the sync byte.
00374      *
00375      * \remark Applies to LoRa modem only
00376      *
00377      * \param [IN] enable if true, it enables a public network
00378      */
00379     virtual void SetPublicNetwork( bool enable );
00380 
00381     //-------------------------------------------------------------------------
00382     //                        Board relative functions
00383     //-------------------------------------------------------------------------
00384 
00385 protected:
00386     /*!
00387      * @brief Initializes the radio I/Os pins interface
00388      */
00389     virtual void IoInit( void ) = 0;
00390     
00391     /*!
00392      *    @brief Initializes the radio registers
00393      */
00394     virtual void RadioRegistersInit( ) = 0;
00395     
00396     /*!
00397      * @brief Initializes the radio SPI
00398      */
00399     virtual void SpiInit( void ) = 0;
00400 
00401     /*!
00402      * @brief Initializes DIO IRQ handlers
00403      *
00404      * @param [IN] irqHandlers Array containing the IRQ callback functions
00405      */
00406     virtual void IoIrqInit( DioIrqHandler *irqHandlers ) = 0;
00407 
00408     /*!
00409      * @brief De-initializes the radio I/Os pins interface. 
00410      *
00411      * \remark Useful when going in MCU lowpower modes
00412      */
00413     virtual void IoDeInit( void ) = 0;
00414 
00415     /*!
00416      * @brief Sets the radio output power.
00417      *
00418      * @param [IN] power Sets the RF output power
00419      */
00420     virtual void SetRfTxPower( int8_t power ) = 0;
00421 
00422     /*!
00423      * @brief Gets the board PA selection configuration
00424      *
00425      * @param [IN] channel Channel frequency in Hz
00426      * @retval PaSelect RegPaConfig PaSelect value
00427      */
00428     virtual uint8_t GetPaSelect( uint32_t channel ) = 0;
00429 
00430     /*!
00431      * @brief Set the RF Switch I/Os pins in Low Power mode
00432      *
00433      * @param [IN] status enable or disable
00434      */
00435     virtual void SetAntSwLowPower( bool status ) = 0;
00436 
00437     /*!
00438      * @brief Initializes the RF Switch I/Os pins interface
00439      */
00440     virtual void AntSwInit( void ) = 0;
00441 
00442     /*!
00443      * @brief De-initializes the RF Switch I/Os pins interface 
00444      *
00445      * \remark Needed to decrease the power consumption in MCU lowpower modes
00446      */
00447     virtual void AntSwDeInit( void ) = 0;
00448 
00449     /*!
00450      * @brief Controls the antenna switch if necessary.
00451      *
00452      * \remark see errata note
00453      *
00454      * @param [IN] opMode Current radio operating mode
00455      */
00456     virtual void SetAntSw( uint8_t opMode ) = 0;
00457 protected:
00458 
00459     /*!
00460      * @brief Sets the SX1272 operating mode
00461      *
00462      * @param [IN] opMode New operating mode
00463      */
00464     virtual void SetOpMode( uint8_t opMode );
00465 
00466     /*
00467      * SX1272 DIO IRQ callback functions prototype
00468      */
00469 
00470     /*!
00471      * @brief DIO 0 IRQ callback
00472      */
00473     virtual void OnDio0Irq( void );
00474 
00475     /*!
00476      * @brief DIO 1 IRQ callback
00477      */
00478     virtual void OnDio1Irq( void );
00479 
00480     /*!
00481      * @brief DIO 2 IRQ callback
00482      */
00483     virtual void OnDio2Irq( void );
00484 
00485     /*!
00486      * @brief DIO 3 IRQ callback
00487      */
00488     virtual void OnDio3Irq( void );
00489 
00490     /*!
00491      * @brief DIO 4 IRQ callback
00492      */
00493     virtual void OnDio4Irq( void );
00494 
00495     /*!
00496      * @brief DIO 5 IRQ callback
00497      */
00498     virtual void OnDio5Irq( void );
00499 
00500     /*!
00501      * @brief Tx & Rx timeout timer callback
00502      */
00503     virtual void OnTimeoutIrq( void );
00504 
00505     /*!
00506      * Returns the known FSK bandwidth registers value
00507      *
00508      * \param [IN] bandwidth Bandwidth value in Hz
00509      * \retval regValue Bandwidth register value.
00510      */
00511     static uint8_t GetFskBandwidthRegValue ( uint32_t bandwidth );
00512 
00513     // Inizio nuova parte per RTOS
00514 
00515     virtual void enqueueOnTimeoutIrq();
00516 
00517     virtual void enqueueRadioEvent_RxTimeout();
00518     virtual void enqueueRadioEvent_TxTimeout();
00519     virtual void enqueueRadioEvent_RxError();
00520     virtual void enqueueRadioEvent_RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
00521     virtual void enqueueRadioEvent_TxDone();
00522     virtual void enqueueRadioEvent_FhssChangeChannel(uint8_t currentChannel);
00523     virtual void enqueueRadioEvent_CadDone(bool channelActivityDetected);
00524     virtual void enqueueOnDio0Irq();
00525     virtual void enqueueOnDio1Irq();
00526     virtual void enqueueOnDio2Irq();
00527     virtual void enqueueOnDio3Irq();
00528     virtual void enqueueOnDio4Irq();
00529     virtual void enqueueOnDio5Irq();
00530 
00531 private:
00532 
00533     void radioEvent_RxTimeout();
00534     void radioEvent_TxTimeout();
00535     void radioEvent_RxError();
00536     void radioEvent_RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
00537     void radioEvent_TxDone();
00538     void radioEvent_FhssChangeChannel(uint8_t currentChannel);
00539     void radioEvent_CadDone(bool channelActivityDetected);
00540 
00541     // Fine nuova parte per RTOS
00542 };
00543 
00544 #endif // __SX1272_H__