Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | /*
* Copyright (c) 2019 Manivannan Sadhasivam
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DRIVERS_LORA_H_
#define ZEPHYR_INCLUDE_DRIVERS_LORA_H_
/**
* @file
* @brief Public LoRa APIs
*/
#include <zephyr/types.h>
#include <device.h>
#ifdef __cplusplus
extern "C" {
#endif
enum lora_signal_bandwidth {
BW_125_KHZ = 0,
BW_250_KHZ,
BW_500_KHZ,
};
enum lora_datarate {
SF_6 = 6,
SF_7,
SF_8,
SF_9,
SF_10,
SF_11,
SF_12,
};
enum lora_coding_rate {
CR_4_5 = 1,
CR_4_6 = 2,
CR_4_7 = 3,
CR_4_8 = 4,
};
struct lora_modem_config {
uint32_t frequency;
enum lora_signal_bandwidth bandwidth;
enum lora_datarate datarate;
enum lora_coding_rate coding_rate;
uint16_t preamble_len;
int8_t tx_power;
bool tx;
};
/**
* @typedef lora_api_config()
* @brief Callback API for configuring the LoRa module
*
* @see lora_config() for argument descriptions.
*/
typedef int (*lora_api_config)(const struct device *dev,
struct lora_modem_config *config);
/**
* @typedef lora_api_send()
* @brief Callback API for sending data over LoRa
*
* @see lora_send() for argument descriptions.
*/
typedef int (*lora_api_send)(const struct device *dev,
uint8_t *data, uint32_t data_len);
/**
* @typedef lora_api_send_async()
* @brief Callback API for sending data asynchronously over LoRa
*
* @see lora_send_async() for argument descriptions.
*/
typedef int (*lora_api_send_async)(const struct device *dev,
uint8_t *data, uint32_t data_len,
struct k_poll_signal *async);
/**
* @typedef lora_api_recv()
* @brief Callback API for receiving data over LoRa
*
* @see lora_recv() for argument descriptions.
*/
typedef int (*lora_api_recv)(const struct device *dev, uint8_t *data,
uint8_t size,
k_timeout_t timeout, int16_t *rssi, int8_t *snr);
/**
* @typedef lora_api_test_cw()
* @brief Callback API for transmitting a continuous wave
*
* @see lora_test_cw() for argument descriptions.
*/
typedef int (*lora_api_test_cw)(const struct device *dev, uint32_t frequency,
int8_t tx_power, uint16_t duration);
struct lora_driver_api {
lora_api_config config;
lora_api_send send;
lora_api_send_async send_async;
lora_api_recv recv;
lora_api_test_cw test_cw;
};
/**
* @brief Configure the LoRa modem
*
* @param dev LoRa device
* @param config Data structure containing the intended configuration for the
modem
* @return 0 on success, negative on error
*/
static inline int lora_config(const struct device *dev,
struct lora_modem_config *config)
{
const struct lora_driver_api *api =
(const struct lora_driver_api *)dev->api;
return api->config(dev, config);
}
/**
* @brief Send data over LoRa
*
* @note This blocks until transmission is complete.
*
* @param dev LoRa device
* @param data Data to be sent
* @param data_len Length of the data to be sent
* @return 0 on success, negative on error
*/
static inline int lora_send(const struct device *dev,
uint8_t *data, uint32_t data_len)
{
const struct lora_driver_api *api =
(const struct lora_driver_api *)dev->api;
return api->send(dev, data, data_len);
}
/**
* @brief Asynchronously send data over LoRa
*
* @note This returns immediately after starting transmission, and locks
* the LoRa modem until the transmission completes.
*
* @param dev LoRa device
* @param data Data to be sent
* @param data_len Length of the data to be sent
* @param async A pointer to a valid and ready to be signaled
* struct k_poll_signal. (Note: if NULL this function will not
* notify the end of the transmission).
* @return 0 on success, negative on error
*/
static inline int lora_send_async(const struct device *dev,
uint8_t *data, uint32_t data_len,
struct k_poll_signal *async)
{
const struct lora_driver_api *api =
(const struct lora_driver_api *)dev->api;
return api->send_async(dev, data, data_len, async);
}
/**
* @brief Receive data over LoRa
*
* @note This is a blocking call.
*
* @param dev LoRa device
* @param data Buffer to hold received data
* @param size Size of the buffer to hold the received data. Max size
allowed is 255.
* @param timeout Duration to wait for a packet.
* @param rssi RSSI of received data
* @param snr SNR of received data
* @return Length of the data received on success, negative on error
*/
static inline int lora_recv(const struct device *dev, uint8_t *data,
uint8_t size,
k_timeout_t timeout, int16_t *rssi, int8_t *snr)
{
const struct lora_driver_api *api =
(const struct lora_driver_api *)dev->api;
return api->recv(dev, data, size, timeout, rssi, snr);
}
/**
* @brief Transmit an unmodulated continuous wave at a given frequency
*
* @note Only use this functionality in a test setup where the
* transmission does not interfere with other devices.
*
* @param dev LoRa device
* @param frequency Output frequency (Hertz)
* @param tx_power TX power (dBm)
* @param duration Transmission duration in seconds.
* @return 0 on success, negative on error
*/
static inline int lora_test_cw(const struct device *dev, uint32_t frequency,
int8_t tx_power, uint16_t duration)
{
const struct lora_driver_api *api =
(const struct lora_driver_api *)dev->api;
if (api->test_cw == NULL) {
return -ENOSYS;
}
return api->test_cw(dev, frequency, tx_power, duration);
}
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_INCLUDE_DRIVERS_LORA_H_ */
|