fota lib for mdot

Dependents:   UQ_LoraWAN

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SxRadio.h Source File

SxRadio.h

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2013 Semtech
00008 
00009 Description: Generic radio driver definition
00010 
00011 License: Revised BSD License, see LICENSE.TXT file include in the project
00012 
00013 Maintainer: Miguel Luis and Gregory Cristian
00014 */
00015 #ifndef __SXRADIO_H__
00016 #define __SXRADIO_H__
00017 
00018 #include <stdint.h>
00019 #include "rtos.h"
00020 #include "SxRadioEvents.h"
00021 
00022 /*!
00023  * \brief Radio driver definition
00024  */
00025 class SxRadio
00026 {
00027 public:
00028     /*!
00029      * Radio driver supported modems
00030      */
00031     typedef enum
00032     {
00033         MODEM_FSK = 0,
00034         MODEM_LORA,
00035     }RadioModems_t ;
00036 
00037     /*!
00038      * Radio driver internal state machine states definition
00039      */
00040     typedef enum
00041     {
00042         RF_IDLE = 0,
00043         RF_RX_RUNNING,
00044         RF_TX_RUNNING,
00045         RF_CAD,
00046     }RadioState_t ;
00047 
00048     SxRadio(uint32_t WakeupTime) : WakeupTime(WakeupTime), State(RF_IDLE), Modem(MODEM_LORA) { }
00049     virtual ~SxRadio() {};
00050 
00051     /*!
00052      * \brief Initializes the radio
00053      *
00054      * \param [IN] events Structure containing the driver callback functions
00055      */
00056     virtual void Init( SxRadioEvents *events ) = 0;
00057     /*!
00058      * \brief Prepares the radio for destruction
00059      */
00060     virtual void Terminate( void ) = 0;
00061     /*!
00062      * Return current radio status
00063      *
00064      * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
00065      */
00066     virtual RadioState_t  Status ( void ) { return State; }
00067     /*!
00068      * \brief Configures the radio with the given modem
00069      *
00070      * \param [IN] modem Modem to be used [0: FSK, 1: LoRa] 
00071      */
00072     virtual void SetModem( RadioModems_t  modem ) = 0;
00073     /*!
00074      * \brief Sets the channel frequency
00075      *
00076      * \param [IN] freq         Channel RF frequency
00077      */
00078     virtual void SetChannel( uint32_t freq ) = 0;
00079     /*!
00080      * \brief Sets the channels configuration
00081      *
00082      * \param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
00083      * \param [IN] freq       Channel RF frequency
00084      * \param [IN] rssiThresh RSSI threshold
00085      * \param [IN] rssiVal    pointer to variable to hold RSSI value if desired - ignored if NULL
00086      *
00087      * \retval isFree         [true: Channel is free, false: Channel is not free]
00088      */
00089     virtual bool IsChannelFree( RadioModems_t  modem, uint32_t freq, uint8_t datarate, int16_t rssiThresh, uint8_t bandwidth, uint32_t timeout = 5000, int16_t *rssiVal = NULL ) = 0;
00090     /*!
00091      * \brief Generates a 32 bits random value based on the RSSI readings
00092      *
00093      * \remark This function sets the radio in LoRa modem mode and disables 
00094      *         all interrupts.
00095      *         After calling this function either Radio.SetRxConfig or
00096      *         Radio.SetTxConfig functions must be called.
00097      *
00098      * \retval randomValue    32 bits random value
00099      */
00100     virtual uint32_t Random( void ) = 0;
00101     /*!
00102      * \brief Sets the reception parameters
00103      *
00104      * \param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa]
00105      * \param [IN] bandwidth    Sets the bandwidth
00106      *                          FSK : >= 2600 and <= 250000 Hz
00107      *                          LoRa: [0: 125 kHz, 1: 250 kHz,
00108      *                                 2: 500 kHz, 3: Reserved] 
00109      * \param [IN] datarate     Sets the Datarate
00110      *                          FSK : 600..300000 bits/s
00111      *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
00112      *                                10: 1024, 11: 2048, 12: 4096  chips]
00113      * \param [IN] coderate     Sets the coding rate (LoRa only)
00114      *                          FSK : N/A ( set to 0 )
00115      *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] 
00116      * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only) 
00117      *                          FSK : >= 2600 and <= 250000 Hz
00118      *                          LoRa: N/A ( set to 0 ) 
00119      * \param [IN] preambleLen  Sets the Preamble length
00120      *                          FSK : Number of bytes 
00121      *                          LoRa: Length in symbols (the hardware adds 4 more symbols)
00122      * \param [IN] symbTimeout  Sets the RxSingle timeout value (LoRa only) 
00123      *                          FSK : N/A ( set to 0 ) 
00124      *                          LoRa: timeout in symbols
00125      * \param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
00126      * \param [IN] payloadLen   Sets payload length when fixed length is used
00127      * \param [IN] crcOn        Enables/Disables the CRC [0: OFF, 1: ON]
00128      * \param [IN] FreqHopOn    Enables disables the intra-packet frequency hopping
00129      *                          FSK : N/A ( set to 0 )
00130      *                          LoRa: [0: OFF, 1: ON]
00131      * \param [IN] HopPeriod    Number of symbols bewteen each hop
00132      *                          FSK : N/A ( set to 0 )
00133      *                          LoRa: Number of symbols
00134      * \param [IN] iqInverted   Inverts IQ signals (LoRa only)
00135      *                          FSK : N/A ( set to 0 )
00136      *                          LoRa: [0: not inverted, 1: inverted]
00137      * \param [IN] rxContinuous Sets the reception in continuous mode
00138      *                          [false: single mode, true: continuous mode]
00139      */
00140     virtual void SetRxConfig( RadioModems_t  modem, uint32_t bandwidth,
00141                               uint32_t datarate, uint8_t coderate,
00142                               uint32_t bandwidthAfc, uint16_t preambleLen,
00143                               uint16_t symbTimeout, bool fixLen,
00144                               uint8_t payloadLen,
00145                               bool crcOn, bool FreqHopOn, uint8_t HopPeriod,
00146                               bool iqInverted, bool rxContinuous ) = 0;
00147     /*!
00148      * \brief Sets the transmission parameters
00149      *
00150      * \param [IN] modem        Radio modem to be used [0: FSK, 1: LoRa] 
00151      * \param [IN] power        Sets the output power [dBm]
00152      * \param [IN] fdev         Sets the frequency deviation (FSK only)
00153      *                          FSK : [Hz]
00154      *                          LoRa: 0
00155      * \param [IN] bandwidth    Sets the bandwidth (LoRa only)
00156      *                          FSK : 0
00157      *                          LoRa: [0: 125 kHz, 1: 250 kHz,
00158      *                                 2: 500 kHz, 3: Reserved] 
00159      * \param [IN] datarate     Sets the Datarate
00160      *                          FSK : 600..300000 bits/s
00161      *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
00162      *                                10: 1024, 11: 2048, 12: 4096  chips]
00163      * \param [IN] coderate     Sets the coding rate (LoRa only)
00164      *                          FSK : N/A ( set to 0 )
00165      *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] 
00166      * \param [IN] preambleLen  Sets the preamble length
00167      *                          FSK : Number of bytes 
00168      *                          LoRa: Length in symbols (the hardware adds 4 more symbols)
00169      * \param [IN] fixLen       Fixed length packets [0: variable, 1: fixed]
00170      * \param [IN] crcOn        Enables disables the CRC [0: OFF, 1: ON]
00171      * \param [IN] FreqHopOn    Enables disables the intra-packet frequency hopping
00172      *                          FSK : N/A ( set to 0 )
00173      *                          LoRa: [0: OFF, 1: ON]
00174      * \param [IN] HopPeriod    Number of symbols bewteen each hop
00175      *                          FSK : N/A ( set to 0 )
00176      *                          LoRa: Number of symbols
00177      * \param [IN] iqInverted   Inverts IQ signals (LoRa only)
00178      *                          FSK : N/A ( set to 0 )
00179      *                          LoRa: [0: not inverted, 1: inverted]
00180      * \param [IN] timeout      Transmission timeout [us]
00181      */
00182     virtual void SetTxConfig( RadioModems_t  modem, int8_t power, uint32_t fdev, 
00183                               uint32_t bandwidth, uint32_t datarate,
00184                               uint8_t coderate, uint16_t preambleLen,
00185                               bool fixLen, bool crcOn, bool FreqHopOn,
00186                               uint8_t HopPeriod, bool iqInverted, uint32_t timeout ) = 0;
00187 
00188     virtual void SetTxContinuous(bool enable) = 0;
00189 
00190     /*!
00191      * \brief Checks if the given RF frequency is supported by the hardware
00192      *
00193      * \param [IN] frequency RF frequency to be checked
00194      * \retval isSupported [true: supported, false: unsupported]
00195      */
00196     virtual bool CheckRfFrequency( uint32_t frequency ) { return true; }
00197     /*!
00198      * \brief Computes the packet time on air for the given payload
00199      *
00200      * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
00201      *
00202      * \param [IN] modem      Radio modem to be used [0: FSK, 1: LoRa]
00203      * \param [IN] pktLen     Packet payload length
00204      *
00205      * \retval airTime        Computed airTime for the given packet payload length
00206      */
00207     virtual double TimeOnAir( RadioModems_t  modem, uint8_t pktLen ) = 0;
00208     /*!
00209      * \brief Sends the buffer of size. Prepares the packet to be sent and sets
00210      *        the radio in transmission
00211      *
00212      * \param [IN]: buffer     Buffer pointer
00213      * \param [IN]: size       Buffer size
00214      */
00215     virtual void Send( const uint8_t *buffer, uint8_t size ) = 0;
00216     /*!
00217      * \brief Sets the radio in sleep mode
00218      */
00219     virtual void Sleep( void ) = 0;
00220     /*!
00221      * \brief Sets the radio in standby mode
00222      */
00223     virtual void Standby( void ) = 0;
00224     /*!
00225      * \brief Sets the radio in reception mode for the given time
00226      * \param [IN] timeout Reception timeout [us]
00227      *                     [0: continuous, others timeout]
00228      */
00229     virtual void Rx( uint32_t timeout ) = 0;
00230     /*!
00231      * \brief Start a Channel Activity Detection
00232      */
00233     virtual void StartCad( void ) = 0;
00234     /*!
00235      * \brief Reads the current RSSI value
00236      *
00237      * \retval rssiValue Current RSSI value in [dBm]
00238      */
00239     virtual int16_t Rssi( RadioModems_t  modem ) = 0;
00240     /*!
00241      * \brief Writes the radio register at the specified address
00242      *
00243      * \param [IN]: addr Register address
00244      * \param [IN]: data New register value
00245      */
00246     virtual void Write( uint8_t addr, uint8_t data ) = 0;
00247     /*!
00248      * \brief Reads the radio register at the specified address
00249      *
00250      * \param [IN]: addr Register address
00251      * \retval data Register value
00252      */
00253     virtual uint8_t Read ( uint8_t addr ) = 0;
00254     /*!
00255      * \brief Writes multiple radio registers starting at address
00256      *
00257      * \param [IN] addr   First Radio register address
00258      * \param [IN] buffer Buffer containing the new register's values
00259      * \param [IN] size   Number of registers to be written
00260      */
00261     virtual void WriteBuffer( uint8_t addr, const uint8_t *buffer, uint8_t size ) = 0;
00262     /*!
00263      * \brief Reads multiple radio registers starting at address
00264      *
00265      * \param [IN] addr First Radio register address
00266      * \param [OUT] buffer Buffer where to copy the registers data
00267      * \param [IN] size Number of registers to be read
00268      */
00269     virtual void ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
00270 
00271     virtual void SignalMacEvent(void) {};
00272 
00273     virtual void ResetRadio(void) {};
00274 
00275     virtual uint32_t GetTimeOnAir(void) = 0;
00276 
00277     void GrabMutex(void) { mutex .lock(); }
00278     void ReleaseMutex(void) { mutex .unlock(); }
00279 
00280     const uint32_t WakeupTime;
00281 
00282 protected:
00283     RadioState_t  State;
00284 
00285     RadioModems_t  Modem;
00286 
00287     /*!
00288      * Access protection
00289      */
00290     Mutex mutex ;
00291 };
00292 
00293 #endif // __SXRADIO_H__
00294