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

Dependents:   alarm_slave alarm_master lora_p2p lorawan1v1 ... more

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
if you're using LR1110, then import LR1110 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.
If you're using Type1SJ select target DISCO_L072CZ_LRWAN1 and import sx126x driver into your program.

Pin assigned to arduino LoRa radio shield form-factor

radio.h

Committer:
Wayne Roberts
Date:
2019-09-11
Revision:
15:e1c04ec39aa4
Parent:
14:94993ae5b164
Parent:
13:a354f82d12d9
Child:
17:5f34cbe2ac53

File content as of revision 15:e1c04ec39aa4:

#include "sx12xx.h"
#ifdef SX127x_H 
#include "sx127x_lora.h"
#include "sx127x_fsk.h"
#endif /* SX127x_H */

#define PA_OFF_DBM      -127

#define RADIO_OSC_STARTUP_us                           1000 // [ms]
#define RADIO_SLEEP_TO_RX_us                           2000 // [ms]
#define RADIO_WAKEUP_TIME_us                           ( RADIO_OSC_STARTUP_us + RADIO_SLEEP_TO_RX_us )

typedef enum
{
    MODEM_FSK = 0,
    MODEM_LORA,
    MODEM_FLRC,
} RadioModems_t;

/*!
 * \brief Radio driver callback functions
 */
typedef struct
{
    void (*DioPin_top_half)(void);
    /*!
     * \brief  Tx Done callback prototype.
     */
    void    (*TxDone_topHalf)(void);    // read irqAt for timestamp of interrupt
    void    (*TxDone_botHalf)(void);    // read irqAt for timestamp of interrupt
    /*!
     * \brief  Tx Timeout callback prototype.
     */
    void    ( *TxTimeout )( void );
    /*!
     * \brief Rx Done callback prototype.
     *
     * \param [IN] payload Received buffer pointer
     * \param [IN] size    Received buffer size
     * \param [IN] rssi    RSSI value computed while receiving the frame [dBm]
     * \param [IN] snr     Raw SNR value given by the radio hardware
     *                     FSK : N/A ( set to 0 )
     *                     LoRa: SNR value in dB
     * \param [IN] curTime captured time at RxDone event occurance
     */
    //void    ( *RxDone )(uint16_t size, int16_t rssi, int8_t snr);
    void    ( *RxDone )(uint8_t size, float rssi, float snr);    // read radio.rx_buf for payload, irqAt for timestamp of interrupt
    /*!
     * \brief  Rx Timeout callback prototype.
     */
    void    ( *RxTimeout )( void );
    /*!
     * \brief Rx Error callback prototype.
     */
    void    ( *RxError )( void );
    /*!
     * \brief  FHSS Change Channel callback prototype.
     *
     * \param [IN] currentChannel   Index number of the current channel
     */
    void ( *FhssChangeChannel )( uint8_t currentChannel );

    /*!
     * \brief CAD Done callback prototype.
     *
     * \param [IN] channelDetected    Channel Activity detected during the CAD
     */
    void ( *CadDone ) ( bool channelActivityDetected );

} RadioEvents_t;

class Radio {
    public:
        static void SetTxContinuousWave(unsigned hz, int8_t txPower, unsigned timeout);
        static uint32_t Random(void);
        static void SetPublicNetwork(bool);
        static void Sleep(void);
        static void SetChannel(unsigned hz);
        static float getFrfMHz(void);
        static void SetRxMaxPayloadLength(uint8_t);
        static void SetFixedPayloadLength(uint8_t); // implicit mode
        static void Rx(unsigned timeout); // timeout 0 for continuous rx
        static void Standby(void);
        static bool CheckRfFrequency(unsigned hz);
        static float GetRssiInst(void);
        static void Init(const RadioEvents_t*, unsigned spiHz=1000000);
        static int Send(uint8_t size, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh);
        //static void PrintStatus(void);
        static void service(void);
		static uint32_t lora_toa_us(uint8_t pktLen);
        static volatile us_timestamp_t irqAt;
#ifdef DUTY_ENABLE
        static us_timestamp_t TimeOnAir(RadioModems_t, uint8_t);
#endif /* DUTY_ENABLE */

        static void LoRaModemConfig(unsigned bwKHz, uint8_t sf, uint8_t cr);
        static void LoRaPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ);
        static void SetLoRaSymbolTimeout(uint16_t symbs);

        static void GFSKModemConfig(unsigned bps, unsigned bwKHz, unsigned fdev_hz);
        static void GFSKPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn);

        static void set_tx_dbm(int8_t dbm);

        static LowPowerTimer lpt;
#ifdef SX127x_H 
        static SX127x radio;
        static SX127x_lora lora;
        static SX127x_fsk fsk;
#elif defined(SX126x_H)
    #define SetFixedPayloadLength      SetRxMaxPayloadLength
        static SX126x radio;
    #ifdef TARGET_FF_ARDUINO
        #define CHIP_TYPE_SX1262        0
        #define CHIP_TYPE_SX1261        1
        static DigitalIn chipType;
    #endif /* TARGET_FF_ARDUINO */
#elif defined(SX128x_H)
    #define SetFixedPayloadLength      SetRxMaxPayloadLength
        static unsigned symbolPeriodUs;
        static unsigned nSymbs;
        static unsigned rxTimeoutMs;
        static SX128x radio;
#else
        #error import radio driver library
#endif

    private:
        static void print_buf(const uint8_t* buf, uint8_t size, const char* txt);
        static void boardInit(void);
#ifdef SX126x_H 
        static RadioModems_t _m_;
        static void rx_done(uint8_t, float, float);
        static void txDoneBottom(void);
        static void timeout_callback(bool);
        static void dio1_top_half(void);
        static PacketParams_t pp;
        static bool paOff;
        static uint8_t loraTimeoutSymbols;
#endif /* SX126x_H */
#ifdef SX128x_H 
        static void readChip(void);
        static PacketParams_t ppGFSK, ppLORA, ppFLRC;
        static ModulationParams_t mpFLRC, mpBLE_GFSK, mpLORA;
        static void diox_top_half(void);
        static void rxDone(uint8_t size, const pktStatus_t*);
        static void txDoneBottom(void);
        static void timeout_callback(bool);
#endif /* SX128x_H */
        static void chipModeChange(void);
        static void rfsw_callback(void);
        static void ocp(uint8_t ma);

        static InterruptIn dio0;
        static InterruptIn dio1;
        static void dio0isr(void);
        static void dio1isr(void);
        static void dio0UserContext(void);
        static void dio1UserContext(void);
};