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

Committer:
Wayne Roberts
Date:
Fri May 31 10:21:30 2019 -0700
Revision:
13:a354f82d12d9
Parent:
11:5111788c4298
Child:
15:e1c04ec39aa4
symbol timeout to 16bits.  Add spi clk freq initialization option.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wayne Roberts 0:9c052ff8dd6a 1 #include "sx12xx.h"
Wayne Roberts 0:9c052ff8dd6a 2 #ifdef SX127x_H
Wayne Roberts 0:9c052ff8dd6a 3 #include "sx127x_lora.h"
Wayne Roberts 0:9c052ff8dd6a 4 #include "sx127x_fsk.h"
Wayne Roberts 0:9c052ff8dd6a 5 #endif /* SX127x_H */
Wayne Roberts 0:9c052ff8dd6a 6
Wayne Roberts 5:ab124d3842a8 7 #define PA_OFF_DBM -127
Wayne Roberts 5:ab124d3842a8 8
Wayne Roberts 0:9c052ff8dd6a 9 #define RADIO_OSC_STARTUP_us 1000 // [ms]
Wayne Roberts 0:9c052ff8dd6a 10 #define RADIO_SLEEP_TO_RX_us 2000 // [ms]
Wayne Roberts 0:9c052ff8dd6a 11 #define RADIO_WAKEUP_TIME_us ( RADIO_OSC_STARTUP_us + RADIO_SLEEP_TO_RX_us )
Wayne Roberts 0:9c052ff8dd6a 12
Wayne Roberts 0:9c052ff8dd6a 13 typedef enum
Wayne Roberts 0:9c052ff8dd6a 14 {
Wayne Roberts 0:9c052ff8dd6a 15 MODEM_FSK = 0,
Wayne Roberts 0:9c052ff8dd6a 16 MODEM_LORA,
Wayne Roberts 0:9c052ff8dd6a 17 MODEM_FLRC,
Wayne Roberts 0:9c052ff8dd6a 18 } RadioModems_t;
Wayne Roberts 0:9c052ff8dd6a 19
Wayne Roberts 0:9c052ff8dd6a 20 /*!
Wayne Roberts 0:9c052ff8dd6a 21 * \brief Radio driver callback functions
Wayne Roberts 0:9c052ff8dd6a 22 */
Wayne Roberts 0:9c052ff8dd6a 23 typedef struct
Wayne Roberts 0:9c052ff8dd6a 24 {
Wayne Roberts 0:9c052ff8dd6a 25 void (*Dio0_top_half)(us_timestamp_t curTime);
Wayne Roberts 0:9c052ff8dd6a 26 /*!
Wayne Roberts 0:9c052ff8dd6a 27 * \brief Tx Done callback prototype.
Wayne Roberts 0:9c052ff8dd6a 28 */
Wayne Roberts 0:9c052ff8dd6a 29 void (*TxDone_topHalf)(void); // read irqAt for timestamp of interrupt
Wayne Roberts 0:9c052ff8dd6a 30 void (*TxDone_botHalf)(void); // read irqAt for timestamp of interrupt
Wayne Roberts 0:9c052ff8dd6a 31 /*!
Wayne Roberts 0:9c052ff8dd6a 32 * \brief Tx Timeout callback prototype.
Wayne Roberts 0:9c052ff8dd6a 33 */
Wayne Roberts 0:9c052ff8dd6a 34 void ( *TxTimeout )( void );
Wayne Roberts 0:9c052ff8dd6a 35 /*!
Wayne Roberts 0:9c052ff8dd6a 36 * \brief Rx Done callback prototype.
Wayne Roberts 0:9c052ff8dd6a 37 *
Wayne Roberts 0:9c052ff8dd6a 38 * \param [IN] payload Received buffer pointer
Wayne Roberts 0:9c052ff8dd6a 39 * \param [IN] size Received buffer size
Wayne Roberts 0:9c052ff8dd6a 40 * \param [IN] rssi RSSI value computed while receiving the frame [dBm]
Wayne Roberts 0:9c052ff8dd6a 41 * \param [IN] snr Raw SNR value given by the radio hardware
Wayne Roberts 0:9c052ff8dd6a 42 * FSK : N/A ( set to 0 )
Wayne Roberts 0:9c052ff8dd6a 43 * LoRa: SNR value in dB
Wayne Roberts 0:9c052ff8dd6a 44 * \param [IN] curTime captured time at RxDone event occurance
Wayne Roberts 0:9c052ff8dd6a 45 */
Wayne Roberts 0:9c052ff8dd6a 46 //void ( *RxDone )(uint16_t size, int16_t rssi, int8_t snr);
Wayne Roberts 0:9c052ff8dd6a 47 void ( *RxDone )(uint8_t size, float rssi, float snr); // read radio.rx_buf for payload, irqAt for timestamp of interrupt
Wayne Roberts 0:9c052ff8dd6a 48 /*!
Wayne Roberts 0:9c052ff8dd6a 49 * \brief Rx Timeout callback prototype.
Wayne Roberts 0:9c052ff8dd6a 50 */
Wayne Roberts 0:9c052ff8dd6a 51 void ( *RxTimeout )( void );
Wayne Roberts 0:9c052ff8dd6a 52 /*!
Wayne Roberts 0:9c052ff8dd6a 53 * \brief Rx Error callback prototype.
Wayne Roberts 0:9c052ff8dd6a 54 */
Wayne Roberts 0:9c052ff8dd6a 55 void ( *RxError )( void );
Wayne Roberts 0:9c052ff8dd6a 56 /*!
Wayne Roberts 0:9c052ff8dd6a 57 * \brief FHSS Change Channel callback prototype.
Wayne Roberts 0:9c052ff8dd6a 58 *
Wayne Roberts 0:9c052ff8dd6a 59 * \param [IN] currentChannel Index number of the current channel
Wayne Roberts 0:9c052ff8dd6a 60 */
Wayne Roberts 0:9c052ff8dd6a 61 void ( *FhssChangeChannel )( uint8_t currentChannel );
Wayne Roberts 0:9c052ff8dd6a 62
Wayne Roberts 0:9c052ff8dd6a 63 /*!
Wayne Roberts 0:9c052ff8dd6a 64 * \brief CAD Done callback prototype.
Wayne Roberts 0:9c052ff8dd6a 65 *
Wayne Roberts 0:9c052ff8dd6a 66 * \param [IN] channelDetected Channel Activity detected during the CAD
Wayne Roberts 0:9c052ff8dd6a 67 */
Wayne Roberts 0:9c052ff8dd6a 68 void ( *CadDone ) ( bool channelActivityDetected );
Wayne Roberts 1:e79b0a55135f 69
Wayne Roberts 0:9c052ff8dd6a 70 } RadioEvents_t;
Wayne Roberts 0:9c052ff8dd6a 71
Wayne Roberts 0:9c052ff8dd6a 72 class Radio {
Wayne Roberts 0:9c052ff8dd6a 73 public:
Wayne Roberts 0:9c052ff8dd6a 74 static void SetTxContinuousWave(unsigned hz, int8_t txPower, unsigned timeout);
Wayne Roberts 0:9c052ff8dd6a 75 static uint32_t Random(void);
Wayne Roberts 0:9c052ff8dd6a 76 static void SetPublicNetwork(bool);
Wayne Roberts 0:9c052ff8dd6a 77 static void Sleep(void);
Wayne Roberts 0:9c052ff8dd6a 78 static void SetChannel(unsigned hz);
Wayne Roberts 0:9c052ff8dd6a 79 static float getFrfMHz(void);
Wayne Roberts 1:e79b0a55135f 80 static void SetRxMaxPayloadLength(uint8_t);
Wayne Roberts 6:b7bbf31e06e4 81 static void SetFixedPayloadLength(uint8_t); // implicit mode
Wayne Roberts 0:9c052ff8dd6a 82 static void Rx(unsigned timeout); // timeout 0 for continuous rx
Wayne Roberts 0:9c052ff8dd6a 83 static void Standby(void);
Wayne Roberts 0:9c052ff8dd6a 84 static bool CheckRfFrequency(unsigned hz);
Wayne Roberts 7:ba81f66e56d1 85 static float GetRssiInst(void);
Wayne Roberts 13:a354f82d12d9 86 static void Init(const RadioEvents_t*, unsigned spiHz=1000000);
Wayne Roberts 0:9c052ff8dd6a 87 static int Send(uint8_t size, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh);
Wayne Roberts 0:9c052ff8dd6a 88 //static void PrintStatus(void);
Wayne Roberts 0:9c052ff8dd6a 89 static void service(void);
Wayne Roberts 0:9c052ff8dd6a 90 static uint32_t lora_toa_us(uint8_t pktLen);
Wayne Roberts 0:9c052ff8dd6a 91 static volatile us_timestamp_t irqAt;
Wayne Roberts 0:9c052ff8dd6a 92 #ifdef DUTY_ENABLE
Wayne Roberts 0:9c052ff8dd6a 93 static us_timestamp_t TimeOnAir(RadioModems_t, uint8_t);
Wayne Roberts 0:9c052ff8dd6a 94 #endif /* DUTY_ENABLE */
Wayne Roberts 0:9c052ff8dd6a 95
Wayne Roberts 0:9c052ff8dd6a 96 static void LoRaModemConfig(unsigned bwKHz, uint8_t sf, uint8_t cr);
Wayne Roberts 0:9c052ff8dd6a 97 static void LoRaPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ);
Wayne Roberts 13:a354f82d12d9 98 static void SetLoRaSymbolTimeout(uint16_t symbs);
Wayne Roberts 0:9c052ff8dd6a 99
Wayne Roberts 0:9c052ff8dd6a 100 static void GFSKModemConfig(unsigned bps, unsigned bwKHz, unsigned fdev_hz);
Wayne Roberts 0:9c052ff8dd6a 101 static void GFSKPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn);
Wayne Roberts 0:9c052ff8dd6a 102
Wayne Roberts 0:9c052ff8dd6a 103 static void set_tx_dbm(int8_t dbm);
Wayne Roberts 0:9c052ff8dd6a 104
Wayne Roberts 0:9c052ff8dd6a 105 static LowPowerTimer lpt;
Wayne Roberts 0:9c052ff8dd6a 106 #ifdef SX127x_H
Wayne Roberts 0:9c052ff8dd6a 107 static SX127x radio;
Wayne Roberts 1:e79b0a55135f 108 static SX127x_lora lora;
Wayne Roberts 1:e79b0a55135f 109 static SX127x_fsk fsk;
Wayne Roberts 0:9c052ff8dd6a 110 #elif defined(SX126x_H)
Wayne Roberts 6:b7bbf31e06e4 111 #define SetFixedPayloadLength SetRxMaxPayloadLength
Wayne Roberts 0:9c052ff8dd6a 112 static SX126x radio;
Wayne Roberts 2:c321b5919516 113 #ifdef TARGET_FF_ARDUINO
Wayne Roberts 2:c321b5919516 114 #define CHIP_TYPE_SX1262 0
Wayne Roberts 2:c321b5919516 115 #define CHIP_TYPE_SX1261 1
Wayne Roberts 2:c321b5919516 116 static DigitalIn chipType;
Wayne Roberts 2:c321b5919516 117 #endif /* TARGET_FF_ARDUINO */
Wayne Roberts 0:9c052ff8dd6a 118 #elif defined(SX128x_H)
Wayne Roberts 7:ba81f66e56d1 119 #define SetFixedPayloadLength SetRxMaxPayloadLength
Wayne Roberts 9:97a6de3dbc86 120 static unsigned symbolPeriodUs;
Wayne Roberts 9:97a6de3dbc86 121 static unsigned nSymbs;
Wayne Roberts 9:97a6de3dbc86 122 static unsigned rxTimeoutMs;
Wayne Roberts 0:9c052ff8dd6a 123 static SX128x radio;
Wayne Roberts 0:9c052ff8dd6a 124 #else
Wayne Roberts 0:9c052ff8dd6a 125 #error import radio driver library
Wayne Roberts 0:9c052ff8dd6a 126 #endif
Wayne Roberts 0:9c052ff8dd6a 127
Wayne Roberts 0:9c052ff8dd6a 128 private:
Wayne Roberts 1:e79b0a55135f 129 static void print_buf(const uint8_t* buf, uint8_t size, const char* txt);
Wayne Roberts 0:9c052ff8dd6a 130 static void boardInit(void);
Wayne Roberts 0:9c052ff8dd6a 131 #ifdef SX126x_H
Wayne Roberts 0:9c052ff8dd6a 132 static RadioModems_t _m_;
Wayne Roberts 0:9c052ff8dd6a 133 static void rx_done(uint8_t, float, float);
Wayne Roberts 0:9c052ff8dd6a 134 static void txDoneBottom(void);
Wayne Roberts 0:9c052ff8dd6a 135 static void timeout_callback(bool);
Wayne Roberts 0:9c052ff8dd6a 136 static void dio1_top_half(void);
Wayne Roberts 0:9c052ff8dd6a 137 static PacketParams_t pp;
Wayne Roberts 5:ab124d3842a8 138 static bool paOff;
Wayne Roberts 11:5111788c4298 139 static uint8_t loraTimeoutSymbols;
Wayne Roberts 0:9c052ff8dd6a 140 #endif /* SX126x_H */
Wayne Roberts 0:9c052ff8dd6a 141 #ifdef SX128x_H
Wayne Roberts 0:9c052ff8dd6a 142 static void readChip(void);
Wayne Roberts 0:9c052ff8dd6a 143 static PacketParams_t ppGFSK, ppLORA, ppFLRC;
Wayne Roberts 0:9c052ff8dd6a 144 static ModulationParams_t mpFLRC, mpBLE_GFSK, mpLORA;
Wayne Roberts 0:9c052ff8dd6a 145 static void diox_top_half(void);
Wayne Roberts 0:9c052ff8dd6a 146 static void rxDone(uint8_t size, const pktStatus_t*);
Wayne Roberts 0:9c052ff8dd6a 147 static void txDoneBottom(void);
Wayne Roberts 0:9c052ff8dd6a 148 static void timeout_callback(bool);
Wayne Roberts 0:9c052ff8dd6a 149 #endif /* SX128x_H */
Wayne Roberts 0:9c052ff8dd6a 150 static void chipModeChange(void);
Wayne Roberts 0:9c052ff8dd6a 151 static void rfsw_callback(void);
Wayne Roberts 0:9c052ff8dd6a 152 static void ocp(uint8_t ma);
Wayne Roberts 0:9c052ff8dd6a 153
Wayne Roberts 0:9c052ff8dd6a 154 static InterruptIn dio0;
Wayne Roberts 0:9c052ff8dd6a 155 static InterruptIn dio1;
Wayne Roberts 0:9c052ff8dd6a 156 static void dio0isr(void);
Wayne Roberts 0:9c052ff8dd6a 157 static void dio1isr(void);
Wayne Roberts 0:9c052ff8dd6a 158 static void dio0UserContext(void);
Wayne Roberts 0:9c052ff8dd6a 159 static void dio1UserContext(void);
Wayne Roberts 0:9c052ff8dd6a 160 };
Wayne Roberts 0:9c052ff8dd6a 161
Wayne Roberts 0:9c052ff8dd6a 162