Zephyr API Documentation 4.5.99
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
311
317
323typedef void (*lora_recv_cb)(const struct device *dev, uint8_t *data, uint16_t size,
324 int16_t rssi, int8_t snr, void *user_data);
325
333typedef void (*lora_cad_cb)(const struct device *dev, bool activity_detected,
334 void *user_data);
335
341typedef int (*lora_api_config)(const struct device *dev,
342 const struct lora_modem_config *config);
343
349typedef int (*lora_api_config_gfsk)(const struct device *dev,
350 const struct lora_modem_config_gfsk *config);
351
357typedef uint32_t (*lora_api_airtime)(const struct device *dev, uint32_t data_len);
358
364typedef int (*lora_api_send)(const struct device *dev,
365 uint8_t *data, uint32_t data_len);
366
372typedef int (*lora_api_send_async)(const struct device *dev,
373 uint8_t *data, uint32_t data_len,
374 struct k_poll_signal *async);
375
381typedef int (*lora_api_recv)(const struct device *dev, uint8_t *data,
382 uint8_t size,
383 k_timeout_t timeout, int16_t *rssi, int8_t *snr);
384
392typedef int (*lora_api_recv_async)(const struct device *dev, lora_recv_cb cb,
393 void *user_data);
394
400typedef int (*lora_api_cad)(const struct device *dev, k_timeout_t timeout);
401
407typedef int (*lora_api_cad_async)(const struct device *dev, lora_cad_cb cb,
408 void *user_data);
409
415typedef int (*lora_api_rssi)(const struct device *dev, int16_t *rssi);
416
423typedef int (*lora_api_recv_duty_cycle)(const struct device *dev,
424 k_timeout_t rx_period,
425 k_timeout_t sleep_period,
426 uint8_t *data, uint8_t size,
427 k_timeout_t timeout,
428 int16_t *rssi, int8_t *snr);
429
436typedef int (*lora_api_recv_duty_cycle_async)(const struct device *dev,
437 k_timeout_t rx_period,
438 k_timeout_t sleep_period,
439 lora_recv_cb cb, void *user_data);
440
446typedef int (*lora_api_test_cw)(const struct device *dev, uint32_t frequency,
447 int8_t tx_power, uint16_t duration);
448
480
482
493static inline int lora_config(const struct device *dev,
494 const struct lora_modem_config *config)
495{
496 return DEVICE_API_GET(lora, dev)->config(dev, config);
497}
498
510static inline int lora_config_gfsk(const struct device *dev,
511 const struct lora_modem_config_gfsk *config)
512{
513 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
514
515 if (api->config_gfsk == NULL) {
516 return -ENOSYS;
517 }
518
519 return api->config_gfsk(dev, config);
520}
521
531static inline uint32_t lora_airtime(const struct device *dev, uint32_t data_len)
532{
533 return DEVICE_API_GET(lora, dev)->airtime(dev, data_len);
534}
535
548static inline int lora_send(const struct device *dev,
549 uint8_t *data, uint32_t data_len)
550{
551 return DEVICE_API_GET(lora, dev)->send(dev, data, data_len);
552}
553
570static inline int lora_send_async(const struct device *dev,
571 uint8_t *data, uint32_t data_len,
572 struct k_poll_signal *async)
573{
574 return DEVICE_API_GET(lora, dev)->send_async(dev, data, data_len, async);
575}
576
593static inline int lora_recv(const struct device *dev, uint8_t *data,
594 uint8_t size,
595 k_timeout_t timeout, int16_t *rssi, int8_t *snr)
596{
597 return DEVICE_API_GET(lora, dev)->recv(dev, data, size, timeout, rssi, snr);
598}
599
615static inline int lora_recv_async(const struct device *dev, lora_recv_cb cb,
616 void *user_data)
617{
618 return DEVICE_API_GET(lora, dev)->recv_async(dev, cb, user_data);
619}
620
637static inline int lora_cad(const struct device *dev, k_timeout_t timeout)
638{
639 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
640
641 if (api->cad == NULL) {
642 return -ENOSYS;
643 }
644
645 return api->cad(dev, timeout);
646}
647
662static inline int lora_cad_async(const struct device *dev, lora_cad_cb cb,
663 void *user_data)
664{
665 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
666
667 if (api->cad_async == NULL) {
668 return -ENOSYS;
669 }
670
671 return api->cad_async(dev, cb, user_data);
672}
673
691static inline int lora_rssi(const struct device *dev, int16_t *rssi)
692{
693 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
694
695 if (api->rssi == NULL) {
696 return -ENOSYS;
697 }
698
699 return api->rssi(dev, rssi);
700}
701
725int lora_energy_detect(const struct device *dev, int16_t rssi_threshold, k_timeout_t duration);
726
749static inline int lora_recv_duty_cycle(const struct device *dev,
750 k_timeout_t rx_period,
751 k_timeout_t sleep_period,
752 uint8_t *data, uint8_t size,
753 k_timeout_t timeout,
754 int16_t *rssi, int8_t *snr)
755{
756 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
757
758 if (api->recv_duty_cycle == NULL) {
759 return -ENOSYS;
760 }
761
762 return api->recv_duty_cycle(dev, rx_period, sleep_period,
763 data, size, timeout, rssi, snr);
764}
765
784static inline int lora_recv_duty_cycle_async(const struct device *dev,
785 k_timeout_t rx_period,
786 k_timeout_t sleep_period,
787 lora_recv_cb cb, void *user_data)
788{
789 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
790
791 if (api->recv_duty_cycle_async == NULL) {
792 return -ENOSYS;
793 }
794
795 return api->recv_duty_cycle_async(dev, rx_period, sleep_period, cb, user_data);
796}
797
810static inline int lora_test_cw(const struct device *dev, uint32_t frequency,
811 int8_t tx_power, uint16_t duration)
812{
813 const struct lora_driver_api *api = DEVICE_API_GET(lora, dev);
814
815 if (api->test_cw == NULL) {
816 return -ENOSYS;
817 }
818
819 return api->test_cw(dev, frequency, tx_power, duration);
820}
821
822#ifdef __cplusplus
823}
824#endif
825
829
830#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:1501
uint32_t(* lora_api_airtime)(const struct device *dev, uint32_t data_len)
Callback API for querying packet airtime.
Definition lora.h:357
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:381
int(* lora_api_rssi)(const struct device *dev, int16_t *rssi)
Callback API for reading the instantaneous RSSI.
Definition lora.h:415
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:372
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:407
void(* lora_cad_cb)(const struct device *dev, bool activity_detected, void *user_data)
Callback API for channel activity detection asynchronously.
Definition lora.h:333
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:446
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:323
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:349
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:423
int(* lora_api_config)(const struct device *dev, const struct lora_modem_config *config)
Callback API for configuring the LoRa module.
Definition lora.h:341
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:392
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:364
int(* lora_api_cad)(const struct device *dev, k_timeout_t timeout)
Callback API for channel activity detection.
Definition lora.h:400
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:436
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:784
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:810
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:593
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:662
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:570
static int lora_cad(const struct device *dev, k_timeout_t timeout)
Perform Channel Activity Detection.
Definition lora.h:637
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:749
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:691
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:510
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:548
static int lora_config(const struct device *dev, const struct lora_modem_config *config)
Configure the LoRa modem.
Definition lora.h:493
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:531
#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:615
@ 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:6814
Kernel timeout type.
Definition clock.h:65
<span class="mlabel">Driver Operations</span> LoRa driver operations
Definition lora.h:452
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:476
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:474
lora_api_recv recv
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:464
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:456
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:470
lora_api_cad cad
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:468
lora_api_recv_async recv_async
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:466
lora_api_airtime airtime
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:458
lora_api_send_async send_async
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:462
lora_api_send send
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:460
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:478
lora_api_config config
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition lora.h:454
lora_api_rssi rssi
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition lora.h:472
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
uint8_t implicit_packet_length
Fixed packet length when explicit lora header is disabled.
Definition lora.h:288
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 explicit_header_disable
Set to true to disable the explicit lora header.
Definition lora.h:280
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:302
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:296
enum lora_cad_mode mode
CAD mode.
Definition lora.h:293
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:308
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