Skip to content

Commit 69664c5

Browse files
Hasnain Virktheotherjimmy
authored andcommitted
Adding base class for all LoRa radio drivers
All Mbed-OS drivers for LoRa radio devices must implement this pure virtual class in order to be compliant with Mbed-OS applications. This class comes loaded with all necessary data structures. The implementations of this class can come out of tree.
1 parent 04f0f2b commit 69664c5

File tree

1 file changed

+399
-0
lines changed

1 file changed

+399
-0
lines changed

features/netsocket/LoRaRadio.h

Lines changed: 399 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,399 @@
1+
/**
2+
* Copyright (c) 2017, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef LORARADIO_H_
19+
#define LORARADIO_H_
20+
21+
/** Radio driver internal state.
22+
* State machine states definition.
23+
*/
24+
typedef enum radio_state {
25+
RF_IDLE = 0,
26+
RF_RX_RUNNING,
27+
RF_TX_RUNNING,
28+
RF_CAD,
29+
} radio_state_t;
30+
31+
/** Type of the modem.
32+
* [LORA/FSK]
33+
*/
34+
typedef enum modem_type {
35+
MODEM_FSK = 0,
36+
MODEM_LORA
37+
} radio_modems_t;
38+
39+
/** Radio FSK modem parameters.
40+
*
41+
*/
42+
typedef struct radio_fsk_settings {
43+
int8_t power;
44+
uint32_t f_dev;
45+
uint32_t bandwidth;
46+
uint32_t bandwidth_afc;
47+
uint32_t datarate;
48+
uint16_t preamble_len;
49+
bool fix_len;
50+
uint8_t payload_len;
51+
bool crc_on;
52+
bool iq_inverted;
53+
bool rx_continuous;
54+
uint32_t tx_timeout;
55+
uint32_t rx_single_timeout;
56+
} radio_fsk_settings_t;
57+
58+
/** Radio FSK packet handler state.
59+
*
60+
*/
61+
typedef struct radio_fsk_packet_handler {
62+
uint8_t preamble_detected;
63+
uint8_t sync_word_detected;
64+
int8_t rssi_value;
65+
int32_t afc_value;
66+
uint8_t rx_gain;
67+
uint16_t size;
68+
uint16_t nb_bytes;
69+
uint8_t fifo_thresh;
70+
uint8_t chunk_size;
71+
} radio_fsk_packet_handler_t;
72+
73+
/** Radio LoRa modem parameters.
74+
*
75+
*/
76+
typedef struct radio_lora_settings {
77+
int8_t power;
78+
uint32_t bandwidth;
79+
uint32_t datarate;
80+
bool low_datarate_optimize;
81+
uint8_t coderate;
82+
uint16_t preamble_len;
83+
bool fix_len;
84+
uint8_t payload_len;
85+
bool crc_on;
86+
bool freq_hop_on;
87+
uint8_t hop_period;
88+
bool iq_inverted;
89+
bool rx_continuous;
90+
uint32_t tx_timeout;
91+
bool public_network;
92+
} radio_lora_settings_t;
93+
94+
/** Radio LoRa packet handler state.
95+
*
96+
*/
97+
typedef struct radio_lora_packet_handler {
98+
int8_t snr_value;
99+
int8_t rssi_value;
100+
uint8_t size;
101+
} radio_lora_packet_handler_t;
102+
103+
/** Radio settings.
104+
*
105+
*/
106+
typedef struct radio_settings {
107+
uint8_t state;
108+
uint8_t modem;
109+
uint32_t channel;
110+
radio_fsk_settings_t fsk;
111+
radio_fsk_packet_handler_t fsk_packet_handler;
112+
radio_lora_settings_t lora;
113+
radio_lora_packet_handler_t lora_packet_handler;
114+
} radio_settings_t;
115+
116+
/** Radio driver callback functions.
117+
*
118+
*/
119+
typedef struct radio_events {
120+
/* Tx Done callback prototype.
121+
*
122+
*/
123+
void (*tx_done) (void);
124+
125+
/* Tx Timeout callback prototype.
126+
*
127+
*/
128+
void (*tx_timeout) (void);
129+
130+
/* Rx Done callback prototype.
131+
*
132+
* @param payload Received buffer pointer.
133+
* @param size Received buffer size.
134+
* @param rssi RSSI value computed while receiving the frame [dBm].
135+
* @param snr Raw SNR value given by the radio hardware.
136+
* FSK : N/A (set to 0)
137+
* LoRa: SNR value in dB
138+
*/
139+
void (*rx_done) (uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
140+
141+
/* Rx Timeout callback prototype.
142+
*
143+
*/
144+
void (*rx_timeout) (void);
145+
146+
/* Rx Error callback prototype.
147+
*
148+
*/
149+
void (*rx_error) (void);
150+
151+
/* FHSS Change Channel callback prototype.
152+
*
153+
* @param current_channel The index number of the current channel.
154+
*/
155+
void (*fhss_change_channel) (uint8_t current_channel);
156+
157+
/* CAD Done callback prototype.
158+
*
159+
* @param channel_activity_detected Channel activity detected during the CAD.
160+
*/
161+
void (*cad_done) (bool channel_activity_detected);
162+
} radio_events_t;
163+
164+
/**
165+
* Interface for the radios, contains the main functions that a radio needs, and five callback functions.
166+
*/
167+
class LoRaRadio
168+
{
169+
170+
public:
171+
172+
/**
173+
* Registers radio events with the Mbed LoRaWAN stack and undergoes the initialization steps if any.
174+
*
175+
* @param events The structure containing the driver callback functions.
176+
*/
177+
virtual void init_radio(radio_events_t *events) = 0;
178+
179+
/**
180+
* Resets the radio module.
181+
*/
182+
virtual void radio_reset() = 0;
183+
184+
/**
185+
* Put the RF module in the sleep mode.
186+
*/
187+
virtual void sleep(void) = 0;
188+
189+
/**
190+
* Sets the radio in the standby mode.
191+
*/
192+
virtual void standby(void) = 0;
193+
194+
/**
195+
* Sets the reception parameters.
196+
*
197+
* @param modem The radio modem to be used [0: FSK, 1: LoRa].
198+
* @param bandwidth Sets the bandwidth.
199+
* FSK : >= 2600 and <= 250000 Hz
200+
* LoRa: [0: 125 kHz, 1: 250 kHz,
201+
* 2: 500 kHz, 3: Reserved]
202+
* @param datarate Sets the datarate.
203+
* FSK : 600..300000 bits/s
204+
* LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
205+
* 10: 1024, 11: 2048, 12: 4096 chips]
206+
* @param coderate Sets the coding rate (LoRa only).
207+
* FSK : N/A ( set to 0 )
208+
* LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
209+
* @param bandwidth_afc Sets the AFC bandwidth (FSK only).
210+
* FSK : >= 2600 and <= 250000 Hz
211+
* LoRa: N/A (set to 0)
212+
* @param preamble_len Sets the preamble length (LoRa only).
213+
* FSK : N/A (set to 0)
214+
* LoRa: Length in symbols (the hardware adds four more symbols).
215+
* @param symb_timeout Sets the RxSingle timeout value.
216+
* FSK : Timeout number of bytes
217+
* LoRa: Timeout in symbols
218+
* @param fix_len Fixed length packets [0: variable, 1: fixed].
219+
* @param payload_len Sets the payload length when fixed length is used.
220+
* @param crc_on Enables/disables the CRC [0: OFF, 1: ON].
221+
* @param freq_hop_on Enables/disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only).
222+
* @param hop_period The number of symbols bewteen each hop (LoRa only).
223+
* @param iq_inverted Inverts the IQ signals (LoRa only).
224+
* FSK : N/A (set to 0)
225+
* LoRa: [0: not inverted, 1: inverted]
226+
* @param rx_continuous Sets the reception in continuous mode.
227+
* [false: single mode, true: continuous mode]
228+
*/
229+
virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth,
230+
uint32_t datarate, uint8_t coderate,
231+
uint32_t bandwidth_afc, uint16_t preamble_len,
232+
uint16_t symb_timeout, bool fix_len,
233+
uint8_t payload_len,
234+
bool crc_on, bool freq_hop_on, uint8_t hop_period,
235+
bool iq_inverted, bool rx_continuous) = 0;
236+
237+
/**
238+
* Sets the transmission parameters.
239+
*
240+
* @param modem The radio modem to be used [0: FSK, 1: LoRa].
241+
* @param power Sets the output power [dBm].
242+
* @param fdev Sets the frequency deviation (FSK only).
243+
* FSK : [Hz]
244+
* LoRa: 0
245+
* @param bandwidth Sets the bandwidth (LoRa only).
246+
* FSK : 0
247+
* LoRa: [0: 125 kHz, 1: 250 kHz,
248+
* 2: 500 kHz, 3: Reserved]
249+
* @param datarate Sets the datarate.
250+
* FSK : 600..300000 bits/s
251+
* LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
252+
* 10: 1024, 11: 2048, 12: 4096 chips]
253+
* @param coderate Sets the coding rate (LoRa only).
254+
* FSK : N/A ( set to 0 )
255+
* LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
256+
* @param preamble_len Sets the preamble length.
257+
* @param fix_len Fixed length packets [0: variable, 1: fixed].
258+
* @param crc_on Enables/disables the CRC [0: OFF, 1: ON].
259+
* @param freq_hop_on Enables/disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only).
260+
* @param hop_period The number of symbols between each hop (LoRa only).
261+
* @param iq_inverted Inverts IQ signals (LoRa only)
262+
* FSK : N/A (set to 0).
263+
* LoRa: [0: not inverted, 1: inverted]
264+
* @param timeout The transmission timeout [us].
265+
*/
266+
virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev,
267+
uint32_t bandwidth, uint32_t datarate,
268+
uint8_t coderate, uint16_t preamble_len,
269+
bool fix_len, bool crc_on, bool freq_hop_on,
270+
uint8_t hop_period, bool iq_inverted, uint32_t timeout) = 0;
271+
272+
/**
273+
* Sends the buffer of size
274+
*
275+
* Prepares the packet to be sent and sets the radio in transmission.
276+
*
277+
* @param buffer A pointer to the buffer.
278+
* @param size The buffer size.
279+
*/
280+
virtual void send(uint8_t *buffer, uint8_t size) = 0;
281+
282+
/**
283+
* Sets the radio in reception mode for a given time.
284+
*
285+
* If the timeout is set to 0, it essentially puts the receiver in continuous mode and it should
286+
* be treated as if in continuous mode. However, an appropriate way to set the receiver in continuous mode is
287+
* to use the `set_rx_config()` API.
288+
*
289+
* @param timeout Reception timeout [ms].
290+
*
291+
*/
292+
virtual void receive(uint32_t timeout) = 0;
293+
294+
/**
295+
* Sets the carrier frequency
296+
*
297+
* @param freq Channel RF frequency.
298+
*/
299+
virtual void set_channel(uint32_t freq) = 0;
300+
301+
/**
302+
* Generates a 32 bit random value based on the RSSI readings.
303+
*
304+
* \remark This function sets the radio in LoRa modem mode and disables all interrupts.
305+
* After calling this function, either `Radio.SetRxConfig` or
306+
* `Radio.SetTxConfig` functions must be called.
307+
*
308+
* @return A 32 bit random value.
309+
*/
310+
virtual uint32_t random(void) = 0;
311+
312+
/**
313+
* Gets the radio status.
314+
*
315+
* @return The current radio status.
316+
*/
317+
virtual uint8_t get_status(void) = 0;
318+
319+
/**
320+
* Sets the maximum payload length.
321+
*
322+
* @param modem The radio modem to be used [0: FSK, 1: LoRa].
323+
* @param max The maximum payload length in bytes.
324+
*/
325+
virtual void set_max_payload_length(radio_modems_t modem, uint8_t max) = 0;
326+
327+
/**
328+
* Sets the network to public or private.
329+
*
330+
* Updates the sync byte. Applies to LoRa modem only.
331+
*
332+
* @param enable If true, it enables a public network.
333+
*/
334+
virtual void set_public_network(bool enable) = 0;
335+
336+
/**
337+
* Computes the packet time on air for the given payload.
338+
*
339+
* \remark This can only be called once `SetRxConfig` or `SetTxConfig` have been called.
340+
*
341+
* @param modem The radio modem to be used [0: FSK, 1: LoRa].
342+
* @param pkt_len The packet payload length.
343+
* @return The computed `airTime` for the given packet payload length.
344+
*/
345+
virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len) = 0;
346+
347+
/**
348+
* Performs carrier sensing.
349+
*
350+
* Checks for a certain time if the RSSI is above a given threshold.
351+
* This threshold determines whether or not there is a transmission going on
352+
* in the channel already.
353+
*
354+
* @param modem The type of the radio modem.
355+
* @param freq The carrier frequency.
356+
* @param rssi_threshold The threshold value of RSSI.
357+
* @param max_carrier_sense_time The time set for sensing the channel (ms).
358+
*
359+
* @return True if there is no active transmission
360+
* in the channel, otherwise false.
361+
*/
362+
virtual bool perform_carrier_sense(radio_modems_t modem,
363+
uint32_t freq,
364+
int16_t rssi_threshold,
365+
uint32_t max_carrier_sense_time) = 0;
366+
367+
/**
368+
* Sets the radio in CAD mode.
369+
*
370+
*/
371+
virtual void start_cad(void) = 0;
372+
373+
/**
374+
* Checks whether the given RF is in range.
375+
*
376+
* @param frequency The frequency to be checked.
377+
*/
378+
virtual bool check_rf_frequency(uint32_t frequency) = 0;
379+
380+
/** Sets the radio in continuous wave transmission mode.
381+
*
382+
* @param freq The RF frequency of the channel.
383+
* @param power The output power [dBm].
384+
* @param time The transmission mode timeout [s].
385+
*/
386+
virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time) = 0;
387+
388+
/**
389+
* Acquires exclusive access to this radio.
390+
*/
391+
virtual void lock(void) = 0;
392+
393+
/**
394+
* Releases the exclusive access to this radio.
395+
*/
396+
virtual void unlock(void) = 0;
397+
};
398+
399+
#endif // LORARADIO_H_

0 commit comments

Comments
 (0)