1

Committer:
Wayne Roberts
Date:
Mon Jun 01 15:59:56 2020 -0700
Revision:
17:5f34cbe2ac53
Parent:
15:e1c04ec39aa4
Child:
18:78c5e644d37a
support platforms without LowPowerTimer, add LR1110

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 14:94993ae5b164 25 void (*DioPin_top_half)(void);
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 service(void);
Wayne Roberts 0:9c052ff8dd6a 89 static uint32_t lora_toa_us(uint8_t pktLen);
Wayne Roberts 0:9c052ff8dd6a 90 static volatile us_timestamp_t irqAt;
Wayne Roberts 0:9c052ff8dd6a 91 #ifdef DUTY_ENABLE
Wayne Roberts 0:9c052ff8dd6a 92 static us_timestamp_t TimeOnAir(RadioModems_t, uint8_t);
Wayne Roberts 0:9c052ff8dd6a 93 #endif /* DUTY_ENABLE */
Wayne Roberts 0:9c052ff8dd6a 94
Wayne Roberts 0:9c052ff8dd6a 95 static void LoRaModemConfig(unsigned bwKHz, uint8_t sf, uint8_t cr);
Wayne Roberts 0:9c052ff8dd6a 96 static void LoRaPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ);
Wayne Roberts 13:a354f82d12d9 97 static void SetLoRaSymbolTimeout(uint16_t symbs);
Wayne Roberts 0:9c052ff8dd6a 98
Wayne Roberts 0:9c052ff8dd6a 99 static void GFSKModemConfig(unsigned bps, unsigned bwKHz, unsigned fdev_hz);
Wayne Roberts 0:9c052ff8dd6a 100 static void GFSKPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn);
Wayne Roberts 0:9c052ff8dd6a 101
Wayne Roberts 0:9c052ff8dd6a 102 static void set_tx_dbm(int8_t dbm);
Wayne Roberts 0:9c052ff8dd6a 103
Wayne Roberts 17:5f34cbe2ac53 104 #ifdef DEVICE_LPTICKER
Wayne Roberts 0:9c052ff8dd6a 105 static LowPowerTimer lpt;
Wayne Roberts 17:5f34cbe2ac53 106 #else
Wayne Roberts 17:5f34cbe2ac53 107 static Timer lpt;
Wayne Roberts 17:5f34cbe2ac53 108 #endif
Wayne Roberts 17:5f34cbe2ac53 109
Wayne Roberts 0:9c052ff8dd6a 110 #ifdef SX127x_H
Wayne Roberts 0:9c052ff8dd6a 111 static SX127x radio;
Wayne Roberts 1:e79b0a55135f 112 static SX127x_lora lora;
Wayne Roberts 1:e79b0a55135f 113 static SX127x_fsk fsk;
Wayne Roberts 0:9c052ff8dd6a 114 #elif defined(SX126x_H)
Wayne Roberts 6:b7bbf31e06e4 115 #define SetFixedPayloadLength SetRxMaxPayloadLength
Wayne Roberts 0:9c052ff8dd6a 116 static SX126x radio;
Wayne Roberts 2:c321b5919516 117 #ifdef TARGET_FF_ARDUINO
Wayne Roberts 2:c321b5919516 118 #define CHIP_TYPE_SX1262 0
Wayne Roberts 2:c321b5919516 119 #define CHIP_TYPE_SX1261 1
Wayne Roberts 2:c321b5919516 120 static DigitalIn chipType;
Wayne Roberts 2:c321b5919516 121 #endif /* TARGET_FF_ARDUINO */
Wayne Roberts 0:9c052ff8dd6a 122 #elif defined(SX128x_H)
Wayne Roberts 7:ba81f66e56d1 123 #define SetFixedPayloadLength SetRxMaxPayloadLength
Wayne Roberts 9:97a6de3dbc86 124 static unsigned symbolPeriodUs;
Wayne Roberts 9:97a6de3dbc86 125 static unsigned nSymbs;
Wayne Roberts 9:97a6de3dbc86 126 static unsigned rxTimeoutMs;
Wayne Roberts 0:9c052ff8dd6a 127 static SX128x radio;
Wayne Roberts 17:5f34cbe2ac53 128 #elif defined(SX1265_H)
Wayne Roberts 17:5f34cbe2ac53 129 static SX1265 radio;
Wayne Roberts 17:5f34cbe2ac53 130 static void dio9_top_half(void);
Wayne Roberts 17:5f34cbe2ac53 131 static void timeout_callback(bool);
Wayne Roberts 17:5f34cbe2ac53 132 static void rx_done(uint8_t, float, float);
Wayne Roberts 17:5f34cbe2ac53 133 static void txDoneBottom(void);
Wayne Roberts 17:5f34cbe2ac53 134 static uint8_t loraTimeoutSymbols;
Wayne Roberts 0:9c052ff8dd6a 135 #else
Wayne Roberts 0:9c052ff8dd6a 136 #error import radio driver library
Wayne Roberts 0:9c052ff8dd6a 137 #endif
Wayne Roberts 0:9c052ff8dd6a 138
Wayne Roberts 0:9c052ff8dd6a 139 private:
Wayne Roberts 1:e79b0a55135f 140 static void print_buf(const uint8_t* buf, uint8_t size, const char* txt);
Wayne Roberts 0:9c052ff8dd6a 141 static void boardInit(void);
Wayne Roberts 0:9c052ff8dd6a 142 #ifdef SX126x_H
Wayne Roberts 0:9c052ff8dd6a 143 static RadioModems_t _m_;
Wayne Roberts 0:9c052ff8dd6a 144 static void rx_done(uint8_t, float, float);
Wayne Roberts 0:9c052ff8dd6a 145 static void txDoneBottom(void);
Wayne Roberts 0:9c052ff8dd6a 146 static void timeout_callback(bool);
Wayne Roberts 0:9c052ff8dd6a 147 static void dio1_top_half(void);
Wayne Roberts 0:9c052ff8dd6a 148 static PacketParams_t pp;
Wayne Roberts 5:ab124d3842a8 149 static bool paOff;
Wayne Roberts 11:5111788c4298 150 static uint8_t loraTimeoutSymbols;
Wayne Roberts 0:9c052ff8dd6a 151 #endif /* SX126x_H */
Wayne Roberts 0:9c052ff8dd6a 152 #ifdef SX128x_H
Wayne Roberts 0:9c052ff8dd6a 153 static void readChip(void);
Wayne Roberts 0:9c052ff8dd6a 154 static PacketParams_t ppGFSK, ppLORA, ppFLRC;
Wayne Roberts 0:9c052ff8dd6a 155 static ModulationParams_t mpFLRC, mpBLE_GFSK, mpLORA;
Wayne Roberts 0:9c052ff8dd6a 156 static void diox_top_half(void);
Wayne Roberts 0:9c052ff8dd6a 157 static void rxDone(uint8_t size, const pktStatus_t*);
Wayne Roberts 0:9c052ff8dd6a 158 static void txDoneBottom(void);
Wayne Roberts 0:9c052ff8dd6a 159 static void timeout_callback(bool);
Wayne Roberts 0:9c052ff8dd6a 160 #endif /* SX128x_H */
Wayne Roberts 0:9c052ff8dd6a 161 static void chipModeChange(void);
Wayne Roberts 0:9c052ff8dd6a 162 static void rfsw_callback(void);
Wayne Roberts 0:9c052ff8dd6a 163 static void ocp(uint8_t ma);
Wayne Roberts 0:9c052ff8dd6a 164
Wayne Roberts 0:9c052ff8dd6a 165 static InterruptIn dio0;
Wayne Roberts 0:9c052ff8dd6a 166 static InterruptIn dio1;
Wayne Roberts 0:9c052ff8dd6a 167 static void dio0isr(void);
Wayne Roberts 0:9c052ff8dd6a 168 static void dio1isr(void);
Wayne Roberts 0:9c052ff8dd6a 169 static void dio0UserContext(void);
Wayne Roberts 0:9c052ff8dd6a 170 static void dio1UserContext(void);
Wayne Roberts 0:9c052ff8dd6a 171 };
Wayne Roberts 0:9c052ff8dd6a 172
Wayne Roberts 0:9c052ff8dd6a 173