From 92b2994335201ff561c90e71c1e5bfc08f1fbeac Mon Sep 17 00:00:00 2001 From: Manivannan Sadhasivam Date: Sun, 21 Jul 2019 01:39:22 +0530 Subject: [PATCH] include: Add initial LoRa API Add initial LoRa API for P2P mode. Signed-off-by: Manivannan Sadhasivam --- include/drivers/lora.h | 140 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 include/drivers/lora.h diff --git a/include/drivers/lora.h b/include/drivers/lora.h new file mode 100644 index 00000000000..62e92f7745f --- /dev/null +++ b/include/drivers/lora.h @@ -0,0 +1,140 @@ +/* + * 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 +#include + +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 { + u32_t frequency; + enum lora_signal_bandwidth bandwidth; + enum lora_datarate datarate; + enum lora_coding_rate coding_rate; + u16_t preamble_len; + s8_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)(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)(struct device *dev, + u8_t *data, u32_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)(struct device *dev, u8_t *data, u8_t size, + s32_t timeout); + +struct lora_driver_api { + lora_api_config config; + lora_api_send send; + lora_api_recv recv; +}; + +/** + * @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(struct device *dev, + struct lora_modem_config *config) +{ + const struct lora_driver_api *api = dev->driver_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(struct device *dev, + u8_t *data, u32_t data_len) +{ + const struct lora_driver_api *api = dev->driver_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, K_NO_WAIT + for no wait time and K_FOREVER for blocking until + data arrives. + * @return Length of the data received on success, negative on error + */ +static inline int lora_recv(struct device *dev, u8_t *data, u8_t size, + s32_t timeout) +{ + const struct lora_driver_api *api = dev->driver_api; + + return api->recv(dev, data, size, timeout); +} + +#endif /* ZEPHYR_INCLUDE_DRIVERS_LORA_H_ */