wayne roberts / lorawan1v1

Dependencies:   sx12xx_hal

Dependents:   LoRaWAN-SanJose_Bootcamp LoRaWAN-grove-cayenne LoRaWAN-classC-demo LoRaWAN-grove-cayenne ... more

Committer:
Wayne Roberts
Date:
Fri May 04 11:00:06 2018 -0700
Revision:
7:4b6f960dcca2
Parent:
4:e4bfe9183f94
Child:
8:5a5ea7cc946f
Add class-C support, and LBT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wayne Roberts 0:6b3ac9c5a042 1 #include "sx127x_lora.h"
Wayne Roberts 0:6b3ac9c5a042 2 #include "sx127x_fsk.h"
Wayne Roberts 0:6b3ac9c5a042 3
Wayne Roberts 0:6b3ac9c5a042 4 #define RADIO_OSC_STARTUP_us 1000 // [ms]
Wayne Roberts 0:6b3ac9c5a042 5 #define RADIO_SLEEP_TO_RX_us 2000 // [ms]
Wayne Roberts 0:6b3ac9c5a042 6 #define RADIO_WAKEUP_TIME_us ( RADIO_OSC_STARTUP_us + RADIO_SLEEP_TO_RX_us )
Wayne Roberts 0:6b3ac9c5a042 7
Wayne Roberts 0:6b3ac9c5a042 8 typedef enum
Wayne Roberts 0:6b3ac9c5a042 9 {
Wayne Roberts 0:6b3ac9c5a042 10 MODEM_FSK = 0,
Wayne Roberts 0:6b3ac9c5a042 11 MODEM_LORA,
Wayne Roberts 0:6b3ac9c5a042 12 } RadioModems_t;
Wayne Roberts 0:6b3ac9c5a042 13
Wayne Roberts 0:6b3ac9c5a042 14 typedef enum
Wayne Roberts 0:6b3ac9c5a042 15 {
Wayne Roberts 0:6b3ac9c5a042 16 RF_IDLE = 0,
Wayne Roberts 0:6b3ac9c5a042 17 RF_RX_RUNNING,
Wayne Roberts 0:6b3ac9c5a042 18 RF_TX_RUNNING,
Wayne Roberts 0:6b3ac9c5a042 19 RF_CAD,
Wayne Roberts 0:6b3ac9c5a042 20 } RadioState_t;
Wayne Roberts 0:6b3ac9c5a042 21
Wayne Roberts 0:6b3ac9c5a042 22 /*!
Wayne Roberts 0:6b3ac9c5a042 23 * \brief Radio driver callback functions
Wayne Roberts 0:6b3ac9c5a042 24 */
Wayne Roberts 0:6b3ac9c5a042 25 typedef struct
Wayne Roberts 0:6b3ac9c5a042 26 {
Wayne Roberts 0:6b3ac9c5a042 27 void (*Dio0_top_half)(us_timestamp_t curTime);
Wayne Roberts 0:6b3ac9c5a042 28 /*!
Wayne Roberts 0:6b3ac9c5a042 29 * \brief Tx Done callback prototype.
Wayne Roberts 0:6b3ac9c5a042 30 */
Wayne Roberts 0:6b3ac9c5a042 31 void ( *TxDone )(us_timestamp_t curTime);
Wayne Roberts 0:6b3ac9c5a042 32 /*!
Wayne Roberts 0:6b3ac9c5a042 33 * \brief Tx Timeout callback prototype.
Wayne Roberts 0:6b3ac9c5a042 34 */
Wayne Roberts 0:6b3ac9c5a042 35 void ( *TxTimeout )( void );
Wayne Roberts 0:6b3ac9c5a042 36 /*!
Wayne Roberts 0:6b3ac9c5a042 37 * \brief Rx Done callback prototype.
Wayne Roberts 0:6b3ac9c5a042 38 *
Wayne Roberts 0:6b3ac9c5a042 39 * \param [IN] payload Received buffer pointer
Wayne Roberts 0:6b3ac9c5a042 40 * \param [IN] size Received buffer size
Wayne Roberts 0:6b3ac9c5a042 41 * \param [IN] rssi RSSI value computed while receiving the frame [dBm]
Wayne Roberts 0:6b3ac9c5a042 42 * \param [IN] snr Raw SNR value given by the radio hardware
Wayne Roberts 0:6b3ac9c5a042 43 * FSK : N/A ( set to 0 )
Wayne Roberts 0:6b3ac9c5a042 44 * LoRa: SNR value in dB
Wayne Roberts 0:6b3ac9c5a042 45 * \param [IN] curTime captured time at RxDone event occurance
Wayne Roberts 0:6b3ac9c5a042 46 */
Wayne Roberts 0:6b3ac9c5a042 47 void ( *RxDone )( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr, us_timestamp_t curTime);
Wayne Roberts 0:6b3ac9c5a042 48 /*!
Wayne Roberts 0:6b3ac9c5a042 49 * \brief Rx Timeout callback prototype.
Wayne Roberts 0:6b3ac9c5a042 50 */
Wayne Roberts 0:6b3ac9c5a042 51 void ( *RxTimeout )( void );
Wayne Roberts 0:6b3ac9c5a042 52 /*!
Wayne Roberts 0:6b3ac9c5a042 53 * \brief Rx Error callback prototype.
Wayne Roberts 0:6b3ac9c5a042 54 */
Wayne Roberts 0:6b3ac9c5a042 55 void ( *RxError )( void );
Wayne Roberts 0:6b3ac9c5a042 56 /*!
Wayne Roberts 0:6b3ac9c5a042 57 * \brief FHSS Change Channel callback prototype.
Wayne Roberts 0:6b3ac9c5a042 58 *
Wayne Roberts 0:6b3ac9c5a042 59 * \param [IN] currentChannel Index number of the current channel
Wayne Roberts 0:6b3ac9c5a042 60 */
Wayne Roberts 0:6b3ac9c5a042 61 void ( *FhssChangeChannel )( uint8_t currentChannel );
Wayne Roberts 0:6b3ac9c5a042 62
Wayne Roberts 0:6b3ac9c5a042 63 /*!
Wayne Roberts 0:6b3ac9c5a042 64 * \brief CAD Done callback prototype.
Wayne Roberts 0:6b3ac9c5a042 65 *
Wayne Roberts 0:6b3ac9c5a042 66 * \param [IN] channelDetected Channel Activity detected during the CAD
Wayne Roberts 0:6b3ac9c5a042 67 */
Wayne Roberts 0:6b3ac9c5a042 68 void ( *CadDone ) ( bool channelActivityDetected );
Wayne Roberts 0:6b3ac9c5a042 69 } RadioEvents_t;
Wayne Roberts 0:6b3ac9c5a042 70
Wayne Roberts 0:6b3ac9c5a042 71 class Radio {
Wayne Roberts 0:6b3ac9c5a042 72 public:
Wayne Roberts 0:6b3ac9c5a042 73 static void SetTxContinuousWave(unsigned hz, int8_t txPower, unsigned timeout);
Wayne Roberts 0:6b3ac9c5a042 74 static uint32_t Random(void);
Wayne Roberts 0:6b3ac9c5a042 75 static void SetPublicNetwork(bool);
Wayne Roberts 0:6b3ac9c5a042 76 static void Sleep(void);
Wayne Roberts 0:6b3ac9c5a042 77 static void SetChannel(unsigned hz);
Wayne Roberts 0:6b3ac9c5a042 78 static void SetRxMaxPayloadLength(RadioModems_t, uint8_t);
Wayne Roberts 0:6b3ac9c5a042 79 static void Rx(unsigned timeout);
Wayne Roberts 0:6b3ac9c5a042 80 static void Standby(void);
Wayne Roberts 0:6b3ac9c5a042 81 static bool CheckRfFrequency(unsigned hz);
Wayne Roberts 0:6b3ac9c5a042 82 static void Init(const RadioEvents_t*);
Wayne Roberts 7:4b6f960dcca2 83 static int Send(uint8_t size, timestamp_t maxListenTime, timestamp_t channelFreeTime, int rssiThresh);
Wayne Roberts 0:6b3ac9c5a042 84 static void PrintStatus(void);
Wayne Roberts 4:e4bfe9183f94 85 static void UserContext(void);
Wayne Roberts 0:6b3ac9c5a042 86 #ifdef DUTY_ENABLE
Wayne Roberts 0:6b3ac9c5a042 87 static us_timestamp_t TimeOnAir(RadioModems_t, uint8_t);
Wayne Roberts 0:6b3ac9c5a042 88 #endif /* DUTY_ENABLE */
Wayne Roberts 0:6b3ac9c5a042 89
Wayne Roberts 0:6b3ac9c5a042 90 static void SetRxConfig(
Wayne Roberts 0:6b3ac9c5a042 91 RadioModems_t modem,
Wayne Roberts 0:6b3ac9c5a042 92 uint32_t bandwidth,
Wayne Roberts 0:6b3ac9c5a042 93 uint32_t datarate,
Wayne Roberts 0:6b3ac9c5a042 94 uint8_t coderate,
Wayne Roberts 0:6b3ac9c5a042 95 uint32_t bandwidthAfc,
Wayne Roberts 0:6b3ac9c5a042 96 uint16_t preambleLen,
Wayne Roberts 0:6b3ac9c5a042 97 uint16_t symbTimeout,
Wayne Roberts 0:6b3ac9c5a042 98 bool fixLen,
Wayne Roberts 0:6b3ac9c5a042 99 uint8_t payloadLen,
Wayne Roberts 0:6b3ac9c5a042 100 bool crcOn,
Wayne Roberts 0:6b3ac9c5a042 101 bool iqInverted
Wayne Roberts 0:6b3ac9c5a042 102 );
Wayne Roberts 0:6b3ac9c5a042 103
Wayne Roberts 0:6b3ac9c5a042 104
Wayne Roberts 0:6b3ac9c5a042 105 static void SetTxConfig(
Wayne Roberts 0:6b3ac9c5a042 106 RadioModems_t modem,
Wayne Roberts 0:6b3ac9c5a042 107 int8_t power,
Wayne Roberts 0:6b3ac9c5a042 108 uint32_t fdev,
Wayne Roberts 0:6b3ac9c5a042 109 uint32_t bandwidth,
Wayne Roberts 0:6b3ac9c5a042 110 uint32_t datarate,
Wayne Roberts 0:6b3ac9c5a042 111 uint8_t coderate,
Wayne Roberts 0:6b3ac9c5a042 112 uint16_t preambleLen,
Wayne Roberts 0:6b3ac9c5a042 113 bool fixLen,
Wayne Roberts 0:6b3ac9c5a042 114 bool crcOn,
Wayne Roberts 0:6b3ac9c5a042 115 bool iqInverted
Wayne Roberts 0:6b3ac9c5a042 116 );
Wayne Roberts 0:6b3ac9c5a042 117
Wayne Roberts 0:6b3ac9c5a042 118 static LowPowerTimer lpt;
Wayne Roberts 0:6b3ac9c5a042 119 static SX127x radio;
Wayne Roberts 0:6b3ac9c5a042 120
Wayne Roberts 0:6b3ac9c5a042 121 private:
Wayne Roberts 0:6b3ac9c5a042 122 static void LoRaConfig(uint32_t bandwidth, uint8_t datarate, uint8_t coderate, uint16_t preambleLen, bool fixLen, bool crcOn);
Wayne Roberts 0:6b3ac9c5a042 123 static void boardInit(void);
Wayne Roberts 0:6b3ac9c5a042 124 static SX127x_lora lora;
Wayne Roberts 0:6b3ac9c5a042 125 static SX127x_fsk fsk;
Wayne Roberts 0:6b3ac9c5a042 126 static void rfsw_callback(void);
Wayne Roberts 4:e4bfe9183f94 127 static void set_tx_dbm(int8_t dbm);
Wayne Roberts 4:e4bfe9183f94 128 static void ocp(uint8_t ma);
Wayne Roberts 4:e4bfe9183f94 129
Wayne Roberts 0:6b3ac9c5a042 130 static InterruptIn dio0;
Wayne Roberts 0:6b3ac9c5a042 131 static InterruptIn dio1;
Wayne Roberts 4:e4bfe9183f94 132 static void dio0isr(void);
Wayne Roberts 4:e4bfe9183f94 133 static void dio1isr(void);
Wayne Roberts 4:e4bfe9183f94 134 static void dio0UserContext(void);
Wayne Roberts 4:e4bfe9183f94 135 static void dio1UserContext(void);
Wayne Roberts 0:6b3ac9c5a042 136
Wayne Roberts 0:6b3ac9c5a042 137 };
Wayne Roberts 0:6b3ac9c5a042 138
Wayne Roberts 0:6b3ac9c5a042 139