191 lines
4.4 KiB
C
191 lines
4.4 KiB
C
/*
|
|
* 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_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_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 is a non-blocking call.
|
|
*
|
|
* @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 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 Timeout value in milliseconds. API also accepts, 0
|
|
for no wait time and SYS_FOREVER_MS for blocking until
|
|
data arrives.
|
|
* @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_ */
|