1

Committer:
Wayne Roberts
Date:
Thu Jul 05 17:31:54 2018 -0700
Revision:
0:9c052ff8dd6a
Child:
1:e79b0a55135f
initial commit

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 0:9c052ff8dd6a 67 } RadioEvents_t;
Wayne Roberts 0:9c052ff8dd6a 68
Wayne Roberts 0:9c052ff8dd6a 69 class Radio {
Wayne Roberts 0:9c052ff8dd6a 70 public:
Wayne Roberts 0:9c052ff8dd6a 71 static void SetTxContinuousWave(unsigned hz, int8_t txPower, unsigned timeout);
Wayne Roberts 0:9c052ff8dd6a 72 static uint32_t Random(void);
Wayne Roberts 0:9c052ff8dd6a 73 static void SetPublicNetwork(bool);
Wayne Roberts 0:9c052ff8dd6a 74 static void Sleep(void);
Wayne Roberts 0:9c052ff8dd6a 75 static void SetChannel(unsigned hz);
Wayne Roberts 0:9c052ff8dd6a 76 static float getFrfMHz(void);
Wayne Roberts 0:9c052ff8dd6a 77 static void SetRxMaxPayloadLength(RadioModems_t, uint8_t);
Wayne Roberts 0:9c052ff8dd6a 78 static void Rx(unsigned timeout); // timeout 0 for continuous rx
Wayne Roberts 0:9c052ff8dd6a 79 static void Standby(void);
Wayne Roberts 0:9c052ff8dd6a 80 static bool CheckRfFrequency(unsigned hz);
Wayne Roberts 0:9c052ff8dd6a 81 static void Init(const RadioEvents_t*);
Wayne Roberts 0:9c052ff8dd6a 82 static int Send(uint8_t size, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh);
Wayne Roberts 0:9c052ff8dd6a 83 //static void PrintStatus(void);
Wayne Roberts 0:9c052ff8dd6a 84 static void service(void);
Wayne Roberts 0:9c052ff8dd6a 85 static uint32_t lora_toa_us(uint8_t pktLen);
Wayne Roberts 0:9c052ff8dd6a 86 static volatile us_timestamp_t irqAt;
Wayne Roberts 0:9c052ff8dd6a 87 #ifdef DUTY_ENABLE
Wayne Roberts 0:9c052ff8dd6a 88 static us_timestamp_t TimeOnAir(RadioModems_t, uint8_t);
Wayne Roberts 0:9c052ff8dd6a 89 #endif /* DUTY_ENABLE */
Wayne Roberts 0:9c052ff8dd6a 90
Wayne Roberts 0:9c052ff8dd6a 91 static void LoRaModemConfig(unsigned bwKHz, uint8_t sf, uint8_t cr);
Wayne Roberts 0:9c052ff8dd6a 92 static void LoRaPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ);
Wayne Roberts 0:9c052ff8dd6a 93 static void SetLoRaSymbolTimeout(uint8_t symbs);
Wayne Roberts 0:9c052ff8dd6a 94
Wayne Roberts 0:9c052ff8dd6a 95 static void GFSKModemConfig(unsigned bps, unsigned bwKHz, unsigned fdev_hz);
Wayne Roberts 0:9c052ff8dd6a 96 static void GFSKPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn);
Wayne Roberts 0:9c052ff8dd6a 97
Wayne Roberts 0:9c052ff8dd6a 98 static void set_tx_dbm(int8_t dbm);
Wayne Roberts 0:9c052ff8dd6a 99
Wayne Roberts 0:9c052ff8dd6a 100 static LowPowerTimer lpt;
Wayne Roberts 0:9c052ff8dd6a 101 #ifdef SX127x_H
Wayne Roberts 0:9c052ff8dd6a 102 static SX127x radio;
Wayne Roberts 0:9c052ff8dd6a 103 #elif defined(SX126x_H)
Wayne Roberts 0:9c052ff8dd6a 104 static SX126x radio;
Wayne Roberts 0:9c052ff8dd6a 105 #elif defined(SX128x_H)
Wayne Roberts 0:9c052ff8dd6a 106 static SX128x radio;
Wayne Roberts 0:9c052ff8dd6a 107 #else
Wayne Roberts 0:9c052ff8dd6a 108 #error import radio driver library
Wayne Roberts 0:9c052ff8dd6a 109 #endif
Wayne Roberts 0:9c052ff8dd6a 110
Wayne Roberts 0:9c052ff8dd6a 111 private:
Wayne Roberts 0:9c052ff8dd6a 112 static void boardInit(void);
Wayne Roberts 0:9c052ff8dd6a 113 #ifdef SX127x_H
Wayne Roberts 0:9c052ff8dd6a 114 static SX127x_lora lora;
Wayne Roberts 0:9c052ff8dd6a 115 static SX127x_fsk fsk;
Wayne Roberts 0:9c052ff8dd6a 116 #endif /* SX127x_H */
Wayne Roberts 0:9c052ff8dd6a 117 #ifdef SX126x_H
Wayne Roberts 0:9c052ff8dd6a 118 static RadioModems_t _m_;
Wayne Roberts 0:9c052ff8dd6a 119 static void rx_done(uint8_t, float, float);
Wayne Roberts 0:9c052ff8dd6a 120 static void txDoneBottom(void);
Wayne Roberts 0:9c052ff8dd6a 121 static void timeout_callback(bool);
Wayne Roberts 0:9c052ff8dd6a 122 static void dio1_top_half(void);
Wayne Roberts 0:9c052ff8dd6a 123 static PacketParams_t pp;
Wayne Roberts 0:9c052ff8dd6a 124 #endif /* SX126x_H */
Wayne Roberts 0:9c052ff8dd6a 125 #ifdef SX128x_H
Wayne Roberts 0:9c052ff8dd6a 126 static RadioModems_t _m_;
Wayne Roberts 0:9c052ff8dd6a 127 static void readChip(void);
Wayne Roberts 0:9c052ff8dd6a 128 static PacketParams_t ppGFSK, ppLORA, ppFLRC;
Wayne Roberts 0:9c052ff8dd6a 129 static ModulationParams_t mpFLRC, mpBLE_GFSK, mpLORA;
Wayne Roberts 0:9c052ff8dd6a 130 static void diox_top_half(void);
Wayne Roberts 0:9c052ff8dd6a 131 static void rxDone(uint8_t size, const pktStatus_t*);
Wayne Roberts 0:9c052ff8dd6a 132 static void txDoneBottom(void);
Wayne Roberts 0:9c052ff8dd6a 133 static void timeout_callback(bool);
Wayne Roberts 0:9c052ff8dd6a 134 #endif /* SX128x_H */
Wayne Roberts 0:9c052ff8dd6a 135 static void chipModeChange(void);
Wayne Roberts 0:9c052ff8dd6a 136 static void rfsw_callback(void);
Wayne Roberts 0:9c052ff8dd6a 137 static void ocp(uint8_t ma);
Wayne Roberts 0:9c052ff8dd6a 138
Wayne Roberts 0:9c052ff8dd6a 139 static InterruptIn dio0;
Wayne Roberts 0:9c052ff8dd6a 140 static InterruptIn dio1;
Wayne Roberts 0:9c052ff8dd6a 141 static void dio0isr(void);
Wayne Roberts 0:9c052ff8dd6a 142 static void dio1isr(void);
Wayne Roberts 0:9c052ff8dd6a 143 static void dio0UserContext(void);
Wayne Roberts 0:9c052ff8dd6a 144 static void dio1UserContext(void);
Wayne Roberts 0:9c052ff8dd6a 145 };
Wayne Roberts 0:9c052ff8dd6a 146
Wayne Roberts 0:9c052ff8dd6a 147