Zephyr API Documentation 4.5.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
lora.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Manivannan Sadhasivam
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12#ifndef ZEPHYR_INCLUDE_DRIVERS_LORA_H_
13#define ZEPHYR_INCLUDE_DRIVERS_LORA_H_
14
23
24#include <stdint.h>
25#include <zephyr/kernel.h>
26#include <zephyr/device.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
57
67 SF_5 = 5,
68 SF_6 = 6,
69 SF_7 = 7,
70 SF_8 = 8,
71 SF_9 = 9,
72 SF_10 = 10,
73 SF_11 = 11,
74 SF_12 = 12,
75};
76
87 CR_4_5 = 1,
88 CR_4_6 = 2,
89 CR_4_7 = 3,
90 CR_4_8 = 4,
91};
92
106
131
145
147#define LORA_GFSK_SYNC_WORD_MAX 8
148
155#define LORA_GFSK_PREAMBLE_MAX (UINT16_MAX / BITS_PER_BYTE)
156
222
300
306
312typedef void (*lora_recv_cb)(const struct device *dev, uint8_t *data, uint16_t size,
313 int16_t rssi, int8_t snr, void *user_data);
314
322typedef void (*lora_cad_cb)(const struct device *dev, bool activity_detected,
323 void *user_data);
324
330typedef int (*lora_api_config)(const struct device *dev,
331 const struct lora_modem_config *config);
332
338typedef int (*lora_api_config_gfsk)(const struct device *dev,
339 const struct lora_modem_config_gfsk *config);
340
346typedef uint32_t (*lora_api_airtime)(const struct device *dev, uint32_t data_len);
347
353typedef int (*lora_api_send)(const struct device *dev,
354 uint8_t *data, uint32_t data_len);
355
361typedef int (*lora_api_send_async)(const struct device *dev,
362 uint8_t *data, uint32_t data_len,
363 struct k_poll_signal *async);
364
370typedef int (*lora_api_recv)(const struct device *dev, uint8_t *data,
371 uint8_t size,
372 k_timeout_t timeout, int16_t *rssi, int8_t *snr);
373
381typedef int (*lora_api_recv_async)(const struct device *dev, lora_recv_cb cb,
382 void *user_data);
383
389typedef int (*lora_api_cad)(const struct device *dev, k_timeout_t timeout);
390
396typedef int (*lora_api_cad_async)(const struct device *dev, lora_cad_cb cb,
397 void *user_data);
398
404typedef int (*lora_api_rssi)(const struct device *dev, int16_t *rssi);
405
412typedef int (*lora_api_recv_duty_cycle)(const struct device *dev,
413 k_timeout_t rx_period,
414 k_timeout_t sleep_period,
415 uint8_t *data, uint8_t size,
416 k_timeout_t timeout,
417 int16_t *rssi, int8_t *snr);
418
425typedef int (*lora_api_recv_duty_cycle_async)(const struct device *dev,
426 k_timeout_t rx_period,
427 k_timeout_t sleep_period,
428 lora_recv_cb cb, void *user_data);
429
435typedef int (*lora_api_test_cw)(const struct device *dev, uint32_t frequency,
436 int8_t tx_power, uint16_t duration);
437
469
471
482static inline int lora_config(const struct device *dev,
483 const struct lora_modem_config *config)
484{
485 return DEVICE_API_GET(lora, dev)->config(dev, config);
486}
487
499static inline int lora_config_gfsk(const struct device *dev,
500 const struct lora_modem_config_gfsk *config)
501{
502 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
503
504 if (api->config_gfsk == NULL) {
505 return -ENOSYS;
506 }
507
508 return api->config_gfsk(dev, config);
509}
510
520static inline uint32_t lora_airtime(const struct device *dev, uint32_t data_len)
521{
522 return DEVICE_API_GET(lora, dev)->airtime(dev, data_len);
523}
524
537static inline int lora_send(const struct device *dev,
538 uint8_t *data, uint32_t data_len)
539{
540 return DEVICE_API_GET(lora, dev)->send(dev, data, data_len);
541}
542
559static inline int lora_send_async(const struct device *dev,
560 uint8_t *data, uint32_t data_len,
561 struct k_poll_signal *async)
562{
563 return DEVICE_API_GET(lora, dev)->send_async(dev, data, data_len, async);
564}
565
582static inline int lora_recv(const struct device *dev, uint8_t *data,
583 uint8_t size,
584 k_timeout_t timeout, int16_t *rssi, int8_t *snr)
585{
586 return DEVICE_API_GET(lora, dev)->recv(dev, data, size, timeout, rssi, snr);
587}
588
604static inline int lora_recv_async(const struct device *dev, lora_recv_cb cb,
605 void *user_data)
606{
607 return DEVICE_API_GET(lora, dev)->recv_async(dev, cb, user_data);
608}
609
626static inline int lora_cad(const struct device *dev, k_timeout_t timeout)
627{
628 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
629
630 if (api->cad == NULL) {
631 return -ENOSYS;
632 }
633
634 return api->cad(dev, timeout);
635}
636
651static inline int lora_cad_async(const struct device *dev, lora_cad_cb cb,
652 void *user_data)
653{
654 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
655
656 if (api->cad_async == NULL) {
657 return -ENOSYS;
658 }
659
660 return api->cad_async(dev, cb, user_data);
661}
662
680static inline int lora_rssi(const struct device *dev, int16_t *rssi)
681{
682 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
683
684 if (api->rssi == NULL) {
685 return -ENOSYS;
686 }
687
688 return api->rssi(dev, rssi);
689}
690
714int lora_energy_detect(const struct device *dev, int16_t rssi_threshold, k_timeout_t duration);
715
738static inline int lora_recv_duty_cycle(const struct device *dev,
739 k_timeout_t rx_period,
740 k_timeout_t sleep_period,
741 uint8_t *data, uint8_t size,
742 k_timeout_t timeout,
743 int16_t *rssi, int8_t *snr)
744{
745 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
746
747 if (api->recv_duty_cycle == NULL) {
748 return -ENOSYS;
749 }
750
751 return api->recv_duty_cycle(dev, rx_period, sleep_period,
752 data, size, timeout, rssi, snr);
753}
754
773static inline int lora_recv_duty_cycle_async(const struct device *dev,
774 k_timeout_t rx_period,
775 k_timeout_t sleep_period,
776 lora_recv_cb cb, void *user_data)
777{
778 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
779
780 if (api->recv_duty_cycle_async == NULL) {
781 return -ENOSYS;
782 }
783
784 return api->recv_duty_cycle_async(dev, rx_period, sleep_period, cb, user_data);
785}
786
799static inline int lora_test_cw(const struct device *dev, uint32_t frequency,
800 int8_t tx_power, uint16_t duration)
801{
802 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
803
804 if (api->test_cw == NULL) {
805 return -ENOSYS;
806 }
807
808 return api->test_cw(dev, frequency, tx_power, duration);
809}
810
811#ifdef __cplusplus
812}
813#endif
814
818
819#endif /* ZEPHYR_INCLUDE_DRIVERS_LORA_H_ */
APIs and macros for the Zephyr device model.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1486
uint32_t(* lora_api_airtime)(const struct device *dev, uint32_t data_len)
Callback API for querying packet airtime.
Definition lora.h:346
int(* lora_api_recv)(const struct device *dev, uint8_t *data, uint8_t size, k_timeout_t timeout, int16_t *rssi, int8_t *snr)
Callback API for receiving data over LoRa.
Definition lora.h:370
int(* lora_api_rssi)(const struct device *dev, int16_t *rssi)
Callback API for reading the instantaneous RSSI.
Definition lora.h:404
int(* lora_api_send_async)(const struct device *dev, uint8_t *data, uint32_t data_len, struct k_poll_signal *async)
Callback API for sending data asynchronously over LoRa.
Definition lora.h:361
int(* lora_api_cad_async)(const struct device *dev, lora_cad_cb cb, void *user_data)
Callback API for channel activity detection asynchronously.
Definition lora.h:396
void(* lora_cad_cb)(const struct device *dev, bool activity_detected, void *user_data)
Callback API for channel activity detection asynchronously.
Definition lora.h:322
int(* lora_api_test_cw)(const struct device *dev, uint32_t frequency, int8_t tx_power, uint16_t duration)
Callback API for transmitting a continuous wave.
Definition lora.h:435
void(* lora_recv_cb)(const struct device *dev, uint8_t *data, uint16_t size, int16_t rssi, int8_t snr, void *user_data)
Callback API for receiving data asynchronously.
Definition lora.h:312
int(* lora_api_config_gfsk)(const struct device *dev, const struct lora_modem_config_gfsk *config)
Callback API for configuring the GFSK modem.
Definition lora.h:338
int(* lora_api_recv_duty_cycle)(const struct device *dev, k_timeout_t rx_period, k_timeout_t sleep_period, uint8_t *data, uint8_t size, k_timeout_t timeout, int16_t *rssi, int8_t *snr)
Callback API for blocking receive with duty cycling.
Definition lora.h:412
int(* lora_api_config)(const struct device *dev, const struct lora_modem_config *config)
Callback API for configuring the LoRa module.
Definition lora.h:330
int(* lora_api_recv_async)(const struct device *dev, lora_recv_cb cb, void *user_data)
Callback API for receiving data asynchronously over LoRa.
Definition lora.h:381
int(* lora_api_send)(const struct device *dev, uint8_t *data, uint32_t data_len)
Callback API for sending data over LoRa.
Definition lora.h:353
int(* lora_api_cad)(const struct device *dev, k_timeout_t timeout)
Callback API for channel activity detection.
Definition lora.h:389
int(* lora_api_recv_duty_cycle_async)(const struct device *dev, k_timeout_t rx_period, k_timeout_t sleep_period, lora_recv_cb cb, void *user_data)
Callback API for receive duty cycling (wake-on-radio).
Definition lora.h:425
lora_cad_symbol_num
Number of symbols used for Channel Activity Detection.
Definition lora.h:99
static int lora_recv_duty_cycle_async(const struct device *dev, k_timeout_t rx_period, k_timeout_t sleep_period, lora_recv_cb cb, void *user_data)
Start receive duty cycling (wake-on-radio).
Definition lora.h:773
lora_cad_mode
Channel Activity Detection mode.
Definition lora.h:113
static int lora_test_cw(const struct device *dev, uint32_t frequency, int8_t tx_power, uint16_t duration)
Transmit an unmodulated continuous wave at a given frequency.
Definition lora.h:799
static int lora_recv(const struct device *dev, uint8_t *data, uint8_t size, k_timeout_t timeout, int16_t *rssi, int8_t *snr)
Receive data over LoRa.
Definition lora.h:582
lora_datarate
LoRa data-rate.
Definition lora.h:66
static int lora_cad_async(const struct device *dev, lora_cad_cb cb, void *user_data)
Perform Channel Activity Detection asynchronously.
Definition lora.h:651
static int lora_send_async(const struct device *dev, uint8_t *data, uint32_t data_len, struct k_poll_signal *async)
Asynchronously send data over LoRa.
Definition lora.h:559
static int lora_cad(const struct device *dev, k_timeout_t timeout)
Perform Channel Activity Detection.
Definition lora.h:626
lora_coding_rate
LoRa coding rate.
Definition lora.h:86
static int lora_recv_duty_cycle(const struct device *dev, k_timeout_t rx_period, k_timeout_t sleep_period, uint8_t *data, uint8_t size, k_timeout_t timeout, int16_t *rssi, int8_t *snr)
Receive data using duty cycling (wake-on-radio).
Definition lora.h:738
lora_gfsk_pulse_shape
Gaussian filter applied to the GFSK transmit pulse.
Definition lora.h:133
static int lora_rssi(const struct device *dev, int16_t *rssi)
Read the instantaneous RSSI of the current channel.
Definition lora.h:680
lora_signal_bandwidth
LoRa signal bandwidth.
Definition lora.h:40
static int lora_config_gfsk(const struct device *dev, const struct lora_modem_config_gfsk *config)
Configure the GFSK modem.
Definition lora.h:499
int lora_energy_detect(const struct device *dev, int16_t rssi_threshold, k_timeout_t duration)
Perform energy-detection carrier sense.
static int lora_send(const struct device *dev, uint8_t *data, uint32_t data_len)
Send data over LoRa.
Definition lora.h:537
static int lora_config(const struct device *dev, const struct lora_modem_config *config)
Configure the LoRa modem.
Definition lora.h:482
static uint32_t lora_airtime(const struct device *dev, uint32_t data_len)
Query the airtime of a packet with a given length.
Definition lora.h:520
#define LORA_GFSK_SYNC_WORD_MAX
Longest sync word the GFSK modem will match on, in bytes.
Definition lora.h:147
static int lora_recv_async(const struct device *dev, lora_recv_cb cb, void *user_data)
Receive data asynchronously over LoRa.
Definition lora.h:604
@ LORA_CAD_SYMB_8
8 symbols
Definition lora.h:103
@ LORA_CAD_SYMB_16
16 symbols
Definition lora.h:104
@ LORA_CAD_SYMB_2
2 symbols
Definition lora.h:101
@ LORA_CAD_SYMB_4
4 symbols
Definition lora.h:102
@ LORA_CAD_SYMB_1
1 symbol
Definition lora.h:100
@ LORA_CAD_MODE_RX
CAD before receive: lora_recv() performs CAD first and returns 0 immediately if no activity is detect...
Definition lora.h:123
@ LORA_CAD_MODE_NONE
No CAD (default).
Definition lora.h:115
@ LORA_CAD_MODE_LBT
Listen Before Talk: lora_send() performs CAD before transmitting and returns -EBUSY if the channel is...
Definition lora.h:129
@ SF_5
Spreading factor 5 (fastest, shortest range).
Definition lora.h:67
@ SF_12
Spreading factor 12 (slowest, longest range).
Definition lora.h:74
@ SF_8
Spreading factor 8.
Definition lora.h:70
@ SF_11
Spreading factor 11.
Definition lora.h:73
@ SF_10
Spreading factor 10.
Definition lora.h:72
@ SF_9
Spreading factor 9.
Definition lora.h:71
@ SF_6
Spreading factor 6.
Definition lora.h:68
@ SF_7
Spreading factor 7.
Definition lora.h:69
@ CR_4_5
Coding rate 4/5 (4 information bits, 1 error correction bit).
Definition lora.h:87
@ CR_4_6
Coding rate 4/6 (4 information bits, 2 error correction bits).
Definition lora.h:88
@ CR_4_8
Coding rate 4/8 (4 information bits, 4 error correction bits).
Definition lora.h:90
@ CR_4_7
Coding rate 4/7 (4 information bits, 3 error correction bits).
Definition lora.h:89
@ LORA_GFSK_PULSE_SHAPE_BT_0_5
Gaussian, BT = 0.5.
Definition lora.h:139
@ LORA_GFSK_PULSE_SHAPE_BT_0_3
Gaussian, BT = 0.3.
Definition lora.h:137
@ LORA_GFSK_PULSE_SHAPE_BT_0_7
Gaussian, BT = 0.7.
Definition lora.h:141
@ LORA_GFSK_PULSE_SHAPE_BT_1_0
Gaussian, BT = 1.0.
Definition lora.h:143
@ LORA_GFSK_PULSE_SHAPE_NONE
No filtering.
Definition lora.h:135
@ BW_200_KHZ
203 kHz
Definition lora.h:49
@ BW_250_KHZ
250 kHz
Definition lora.h:50
@ BW_10_KHZ
10.42 kHz
Definition lora.h:42
@ BW_31_KHZ
31.25 kHz
Definition lora.h:45
@ BW_125_KHZ
125 kHz
Definition lora.h:48
@ BW_400_KHZ
406 kHz
Definition lora.h:51
@ BW_800_KHZ
812 kHz
Definition lora.h:53
@ BW_41_KHZ
41.67 kHz
Definition lora.h:46
@ BW_500_KHZ
500 kHz
Definition lora.h:52
@ BW_62_KHZ
62.5 kHz
Definition lora.h:47
@ BW_7_KHZ
7.81 kHz
Definition lora.h:41
@ BW_1600_KHZ
1625 kHz
Definition lora.h:55
@ BW_15_KHZ
15.63 kHz
Definition lora.h:43
@ BW_20_KHZ
20.83 kHz
Definition lora.h:44
@ BW_1000_KHZ
1000 kHz
Definition lora.h:54
#define ENOSYS
Function not implemented.
Definition errno.h:83
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT8_TYPE__ int8_t
Definition stdint.h:72
__INT16_TYPE__ int16_t
Definition stdint.h:73
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
void * data
Address of the device instance private data.
Definition device.h:553
Poll signal object.
Definition kernel.h:6798
Kernel timeout type.
Definition clock.h:65
<span class="mlabel">Driver Operations</span> LoRa driver operations
Definition lora.h:441
lora_api_recv_duty_cycle recv_duty_cycle
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:465
lora_api_recv_duty_cycle_async recv_duty_cycle_async
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:463
lora_api_recv recv
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:453
lora_api_config_gfsk config_gfsk
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:445
lora_api_cad_async cad_async
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:459
lora_api_cad cad
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:457
lora_api_recv_async recv_async
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:455
lora_api_airtime airtime
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:447
lora_api_send_async send_async
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:451
lora_api_send send
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:449
lora_api_test_cw test_cw
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:467
lora_api_config config
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:443
lora_api_rssi rssi
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:461
Structure containing the configuration of a GFSK modem.
Definition lora.h:161
uint32_t bitrate
Bit rate in bits per second.
Definition lora.h:166
bool whitening
Whiten the payload to keep the transmitted spectrum flat.
Definition lora.h:203
uint32_t freq_deviation
Transmit frequency deviation in Hz.
Definition lora.h:169
uint8_t payload_len
Payload length in bytes, used only when fixed_len is set.
Definition lora.h:217
uint8_t sync_word_len
Sync word length in bytes; 0 leaves the sync word out.
Definition lora.h:191
bool tx
Set to true for transmission, false for receiving.
Definition lora.h:200
uint16_t preamble_len
Length of the preamble in bytes, at most LORA_GFSK_PREAMBLE_MAX.
Definition lora.h:194
uint32_t frequency
Frequency in Hz to use for transceiving.
Definition lora.h:163
uint32_t bandwidth
Receive bandwidth in Hz, measured across both sidebands.
Definition lora.h:182
int8_t tx_power
TX-power in dBm to use for transmission.
Definition lora.h:197
enum lora_gfsk_pulse_shape pulse_shape
Pulse shaping filter.
Definition lora.h:185
bool packet_crc_disable
Set to true to disable the CRC-16-CCITT over the payload.
Definition lora.h:220
uint8_t sync_word[8]
Sync word to transmit and to match on.
Definition lora.h:188
bool fixed_len
Send and expect a fixed payload length rather than a length byte ahead of the payload.
Definition lora.h:209
Structure containing the configuration of a LoRa modem.
Definition lora.h:227
struct lora_modem_config::@115122161215247245016110057201332052344206077034 cad
Channel Activity Detection parameters.
uint32_t frequency
Frequency in Hz to use for transceiving.
Definition lora.h:229
enum lora_signal_bandwidth bandwidth
The bandwidth to use for transceiving.
Definition lora.h:232
bool tx
Set to true for transmission, false for receiving.
Definition lora.h:254
bool packet_crc_disable
Set to true to disable the 16-bit payload CRC.
Definition lora.h:277
int8_t tx_power
TX-power in dBm to use for transmission.
Definition lora.h:244
uint8_t detection_peak
Detection peak threshold (hardware-specific, dimensionless).
Definition lora.h:291
bool iq_inverted
Invert the In-Phase and Quadrature (IQ) signals.
Definition lora.h:263
enum lora_coding_rate coding_rate
The coding rate to use for transceiving.
Definition lora.h:238
uint16_t preamble_len
Length of the preamble.
Definition lora.h:241
enum lora_cad_symbol_num symbol_num
Number of symbols for CAD detection.
Definition lora.h:285
enum lora_cad_mode mode
CAD mode.
Definition lora.h:282
enum lora_datarate datarate
The data-rate to use for transceiving.
Definition lora.h:235
uint8_t detection_minimum
Minimum detection threshold (hardware-specific, dimensionless).
Definition lora.h:297
uint8_t sync_word
Override 'public_network' with an explicit sync word.
Definition lora.h:251
bool public_network
Sets the sync-byte to use:
Definition lora.h:274