Hardware Abstraction Layer, permitting any LoRa application to use any LoRa radio chip

Dependents:   alarm_slave alarm_master lora_p2p lorawan1v1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers radio.h Source File

radio.h

00001 #include "sx12xx.h"
00002 #ifdef SX127x_H 
00003 #include "sx127x_lora.h"
00004 #include "sx127x_fsk.h"
00005 #endif /* SX127x_H */
00006 
00007 #define PA_OFF_DBM      -127
00008 
00009 #define RADIO_OSC_STARTUP_us                           1000 // [ms]
00010 #define RADIO_SLEEP_TO_RX_us                           2000 // [ms]
00011 #define RADIO_WAKEUP_TIME_us                           ( RADIO_OSC_STARTUP_us + RADIO_SLEEP_TO_RX_us )
00012 
00013 typedef enum
00014 {
00015     MODEM_FSK = 0,
00016     MODEM_LORA,
00017     MODEM_FLRC,
00018 } RadioModems_t;
00019 
00020 /*!
00021  * \brief Radio driver callback functions
00022  */
00023 typedef struct
00024 {
00025     void (*DioPin_top_half)(void);
00026     /*!
00027      * \brief  Tx Done callback prototype.
00028      */
00029     void    (*TxDone_topHalf)(void);    // read irqAt for timestamp of interrupt
00030     void    (*TxDone_botHalf)(void);    // read irqAt for timestamp of interrupt
00031     /*!
00032      * \brief  Tx Timeout callback prototype.
00033      */
00034     void    ( *TxTimeout )( void );
00035     /*!
00036      * \brief Rx Done callback prototype.
00037      *
00038      * \param [IN] payload Received buffer pointer
00039      * \param [IN] size    Received buffer size
00040      * \param [IN] rssi    RSSI value computed while receiving the frame [dBm]
00041      * \param [IN] snr     Raw SNR value given by the radio hardware
00042      *                     FSK : N/A ( set to 0 )
00043      *                     LoRa: SNR value in dB
00044      * \param [IN] curTime captured time at RxDone event occurance
00045      */
00046     //void    ( *RxDone )(uint16_t size, int16_t rssi, int8_t snr);
00047     void    ( *RxDone )(uint8_t size, float rssi, float snr);    // read radio.rx_buf for payload, irqAt for timestamp of interrupt
00048     /*!
00049      * \brief  Rx Timeout callback prototype.
00050      */
00051     void    ( *RxTimeout )( void );
00052     /*!
00053      * \brief Rx Error callback prototype.
00054      */
00055     void    ( *RxError )( void );
00056     /*!
00057      * \brief  FHSS Change Channel callback prototype.
00058      *
00059      * \param [IN] currentChannel   Index number of the current channel
00060      */
00061     void ( *FhssChangeChannel )( uint8_t currentChannel );
00062 
00063     /*!
00064      * \brief CAD Done callback prototype.
00065      *
00066      * \param [IN] channelDetected    Channel Activity detected during the CAD
00067      */
00068     void ( *CadDone ) ( bool channelActivityDetected );
00069 
00070 } RadioEvents_t;
00071 
00072 class Radio {
00073     public:
00074         static void SetTxContinuousWave(unsigned hz, int8_t txPower, unsigned timeout);
00075         static uint32_t Random(void);
00076         static void SetPublicNetwork(bool);
00077         static void Sleep(void);
00078         static void SetChannel(unsigned hz);
00079         static float getFrfMHz(void);
00080         static void SetRxMaxPayloadLength(uint8_t);
00081         static void SetFixedPayloadLength(uint8_t); // implicit mode
00082         static void Rx(unsigned timeout); // timeout 0 for continuous rx
00083         static void Standby(void);
00084         static bool CheckRfFrequency(unsigned hz);
00085         static float GetRssiInst(void);
00086         static void Init(const RadioEvents_t*, unsigned spiHz=1000000);
00087         static int Send(uint8_t size, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh);
00088         static void service(void);
00089         static uint32_t lora_toa_us(uint8_t pktLen);
00090     #if (MBED_MAJOR_VERSION < 6)
00091         static volatile us_timestamp_t irqAt;
00092     #else
00093         static LowPowerClock::time_point irqAt;
00094     #endif
00095 #ifdef DUTY_ENABLE
00096         static us_timestamp_t TimeOnAir(RadioModems_t, uint8_t);
00097 #endif /* DUTY_ENABLE */
00098 
00099         static void LoRaModemConfig(unsigned bwKHz, uint8_t sf, uint8_t cr);
00100         static void LoRaPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ);
00101         static void SetLoRaSymbolTimeout(uint16_t symbs);
00102 
00103         static void GFSKModemConfig(unsigned bps, unsigned bwKHz, unsigned fdev_hz);
00104         static void GFSKPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn);
00105 
00106         static void set_tx_dbm(int8_t dbm);
00107 
00108 #ifdef DEVICE_LPTICKER
00109         static LowPowerTimer lpt;
00110 #else
00111         static Timer lpt;
00112 #endif
00113 
00114 #ifdef SX127x_H 
00115         static SX127x radio;
00116         static SX127x_lora lora;
00117         static SX127x_fsk fsk;
00118 #elif defined(SX126x_H)
00119     #define SetFixedPayloadLength      SetRxMaxPayloadLength
00120         static SX126x radio;
00121     #ifdef TARGET_FF_ARDUINO
00122         #define CHIP_TYPE_SX1262        0
00123         #define CHIP_TYPE_SX1261        1
00124         #ifndef TARGET_DISCO_L072CZ_LRWAN1
00125             static DigitalIn chipType;
00126         #endif
00127     #endif /* TARGET_FF_ARDUINO */
00128 #elif defined(SX128x_H)
00129     #define SetFixedPayloadLength      SetRxMaxPayloadLength
00130         static unsigned symbolPeriodUs;
00131         static unsigned nSymbs;
00132         static unsigned rxTimeoutMs;
00133         static SX128x radio;
00134 #elif defined(SX1265_H)
00135         static SX1265 radio;
00136         static void dio9_top_half(void);
00137         static void timeout_callback(bool);
00138         static void rx_done(uint8_t, float, float);
00139         static void txDoneBottom(void);
00140         static uint8_t loraTimeoutSymbols;
00141 #else
00142         #error import radio driver library
00143 #endif
00144 
00145     private:
00146         static void print_buf(const uint8_t* buf, uint8_t size, const char* txt);
00147         static void boardInit(void);
00148 #ifdef SX126x_H 
00149         static RadioModems_t _m_;
00150         static void rx_done(uint8_t, float, float);
00151         static void txDoneBottom(void);
00152         static void timeout_callback(bool);
00153         static void dio1_top_half(void);
00154         static PacketParams_t pp;
00155         static bool paOff;
00156         static uint8_t loraTimeoutSymbols;
00157 #endif /* SX126x_H */
00158 #ifdef SX128x_H 
00159         static void readChip(void);
00160         static PacketParams_t ppGFSK, ppLORA, ppFLRC;
00161         static ModulationParams_t mpFLRC, mpBLE_GFSK, mpLORA;
00162         static void diox_top_half(void);
00163         static void rxDone(uint8_t size, const pktStatus_t*);
00164         static void txDoneBottom(void);
00165         static void timeout_callback(bool);
00166 #endif /* SX128x_H */
00167 #if defined(SX1265_H)
00168         static void my_irq_handler(void);
00169         static bool init_irq;
00170 #endif /* SX1265_H */
00171         static void chipModeChange(void);
00172         static void rfsw_callback(void);
00173         static void ocp(uint8_t ma);
00174 
00175         static InterruptIn dio0;
00176         static InterruptIn dio1;
00177         static void dio0isr(void);
00178         static void dio1isr(void);
00179         static void dio0UserContext(void);
00180         static void dio1UserContext(void);
00181 };
00182 
00183