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:
Tue Jul 17 16:18:06 2018 -0700
Revision:
2:c321b5919516
Parent:
1:e79b0a55135f
Child:
5:ab124d3842a8
expose sx126x chipType pin to application

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