1

Committer:
Wayne Roberts
Date:
Sun Nov 25 17:12:03 2018 -0800
Revision:
5:ab124d3842a8
Parent:
2:c321b5919516
Child:
6:b7bbf31e06e4
add PA disable 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 0:9c052ff8dd6a 81 static void Rx(unsigned timeout); // timeout 0 for continuous rx
Wayne Roberts 0:9c052ff8dd6a 82 static void Standby(void);
Wayne Roberts 0:9c052ff8dd6a 83 static bool CheckRfFrequency(unsigned hz);
Wayne Roberts 0:9c052ff8dd6a 84 static void Init(const RadioEvents_t*);
Wayne Roberts 0:9c052ff8dd6a 85 static int Send(uint8_t size, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh);
Wayne Roberts 0:9c052ff8dd6a 86 //static void PrintStatus(void);
Wayne Roberts 0:9c052ff8dd6a 87 static void service(void);
Wayne Roberts 0:9c052ff8dd6a 88 static uint32_t lora_toa_us(uint8_t pktLen);
Wayne Roberts 0:9c052ff8dd6a 89 static volatile us_timestamp_t irqAt;
Wayne Roberts 0:9c052ff8dd6a 90 #ifdef DUTY_ENABLE
Wayne Roberts 0:9c052ff8dd6a 91 static us_timestamp_t TimeOnAir(RadioModems_t, uint8_t);
Wayne Roberts 0:9c052ff8dd6a 92 #endif /* DUTY_ENABLE */
Wayne Roberts 0:9c052ff8dd6a 93
Wayne Roberts 0:9c052ff8dd6a 94 static void LoRaModemConfig(unsigned bwKHz, uint8_t sf, uint8_t cr);
Wayne Roberts 0:9c052ff8dd6a 95 static void LoRaPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn, bool invIQ);
Wayne Roberts 0:9c052ff8dd6a 96 static void SetLoRaSymbolTimeout(uint8_t symbs);
Wayne Roberts 0:9c052ff8dd6a 97
Wayne Roberts 0:9c052ff8dd6a 98 static void GFSKModemConfig(unsigned bps, unsigned bwKHz, unsigned fdev_hz);
Wayne Roberts 0:9c052ff8dd6a 99 static void GFSKPacketConfig(unsigned preambleLen, bool fixLen, bool crcOn);
Wayne Roberts 0:9c052ff8dd6a 100
Wayne Roberts 0:9c052ff8dd6a 101 static void set_tx_dbm(int8_t dbm);
Wayne Roberts 0:9c052ff8dd6a 102
Wayne Roberts 0:9c052ff8dd6a 103 static LowPowerTimer lpt;
Wayne Roberts 0:9c052ff8dd6a 104 #ifdef SX127x_H
Wayne Roberts 0:9c052ff8dd6a 105 static SX127x radio;
Wayne Roberts 1:e79b0a55135f 106 static SX127x_lora lora;
Wayne Roberts 1:e79b0a55135f 107 static SX127x_fsk fsk;
Wayne Roberts 0:9c052ff8dd6a 108 #elif defined(SX126x_H)
Wayne Roberts 0:9c052ff8dd6a 109 static SX126x radio;
Wayne Roberts 2:c321b5919516 110 #ifdef TARGET_FF_ARDUINO
Wayne Roberts 2:c321b5919516 111 #define CHIP_TYPE_SX1262 0
Wayne Roberts 2:c321b5919516 112 #define CHIP_TYPE_SX1261 1
Wayne Roberts 2:c321b5919516 113 static DigitalIn chipType;
Wayne Roberts 2:c321b5919516 114 #endif /* TARGET_FF_ARDUINO */
Wayne Roberts 0:9c052ff8dd6a 115 #elif defined(SX128x_H)
Wayne Roberts 0:9c052ff8dd6a 116 static SX128x radio;
Wayne Roberts 0:9c052ff8dd6a 117 #else
Wayne Roberts 0:9c052ff8dd6a 118 #error import radio driver library
Wayne Roberts 0:9c052ff8dd6a 119 #endif
Wayne Roberts 0:9c052ff8dd6a 120
Wayne Roberts 0:9c052ff8dd6a 121 private:
Wayne Roberts 1:e79b0a55135f 122 static void print_buf(const uint8_t* buf, uint8_t size, const char* txt);
Wayne Roberts 0:9c052ff8dd6a 123 static void boardInit(void);
Wayne Roberts 0:9c052ff8dd6a 124 #ifdef SX126x_H
Wayne Roberts 0:9c052ff8dd6a 125 static RadioModems_t _m_;
Wayne Roberts 0:9c052ff8dd6a 126 static void rx_done(uint8_t, float, float);
Wayne Roberts 0:9c052ff8dd6a 127 static void txDoneBottom(void);
Wayne Roberts 0:9c052ff8dd6a 128 static void timeout_callback(bool);
Wayne Roberts 0:9c052ff8dd6a 129 static void dio1_top_half(void);
Wayne Roberts 0:9c052ff8dd6a 130 static PacketParams_t pp;
Wayne Roberts 5:ab124d3842a8 131 static bool paOff;
Wayne Roberts 0:9c052ff8dd6a 132 #endif /* SX126x_H */
Wayne Roberts 0:9c052ff8dd6a 133 #ifdef SX128x_H
Wayne Roberts 0:9c052ff8dd6a 134 static void readChip(void);
Wayne Roberts 0:9c052ff8dd6a 135 static PacketParams_t ppGFSK, ppLORA, ppFLRC;
Wayne Roberts 0:9c052ff8dd6a 136 static ModulationParams_t mpFLRC, mpBLE_GFSK, mpLORA;
Wayne Roberts 0:9c052ff8dd6a 137 static void diox_top_half(void);
Wayne Roberts 0:9c052ff8dd6a 138 static void rxDone(uint8_t size, const pktStatus_t*);
Wayne Roberts 0:9c052ff8dd6a 139 static void txDoneBottom(void);
Wayne Roberts 0:9c052ff8dd6a 140 static void timeout_callback(bool);
Wayne Roberts 0:9c052ff8dd6a 141 #endif /* SX128x_H */
Wayne Roberts 0:9c052ff8dd6a 142 static void chipModeChange(void);
Wayne Roberts 0:9c052ff8dd6a 143 static void rfsw_callback(void);
Wayne Roberts 0:9c052ff8dd6a 144 static void ocp(uint8_t ma);
Wayne Roberts 0:9c052ff8dd6a 145
Wayne Roberts 0:9c052ff8dd6a 146 static InterruptIn dio0;
Wayne Roberts 0:9c052ff8dd6a 147 static InterruptIn dio1;
Wayne Roberts 0:9c052ff8dd6a 148 static void dio0isr(void);
Wayne Roberts 0:9c052ff8dd6a 149 static void dio1isr(void);
Wayne Roberts 0:9c052ff8dd6a 150 static void dio0UserContext(void);
Wayne Roberts 0:9c052ff8dd6a 151 static void dio1UserContext(void);
Wayne Roberts 0:9c052ff8dd6a 152 };
Wayne Roberts 0:9c052ff8dd6a 153
Wayne Roberts 0:9c052ff8dd6a 154