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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | /*
* Copyright (c) 2019 Manivannan Sadhasivam
* Copyright (c) 2020 Grinn
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/lora.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/kernel.h>
/* LoRaMac-node specific includes */
#include <radio.h>
#include "sx12xx_common.h"
#define STATE_FREE 0
#define STATE_BUSY 1
#define STATE_CLEANUP 2
LOG_MODULE_REGISTER(sx12xx_common, CONFIG_LORA_LOG_LEVEL);
struct sx12xx_rx_params {
uint8_t *buf;
uint8_t *size;
int16_t *rssi;
int8_t *snr;
};
static struct sx12xx_data {
const struct device *dev;
struct k_poll_signal *operation_done;
lora_recv_cb async_rx_cb;
RadioEvents_t events;
struct lora_modem_config tx_cfg;
atomic_t modem_usage;
struct sx12xx_rx_params rx_params;
} dev_data;
int __sx12xx_configure_pin(const struct gpio_dt_spec *gpio, gpio_flags_t flags)
{
int err;
if (!device_is_ready(gpio->port)) {
LOG_ERR("GPIO device not ready %s", gpio->port->name);
return -ENODEV;
}
err = gpio_pin_configure_dt(gpio, flags);
if (err) {
LOG_ERR("Cannot configure gpio %s %d: %d", gpio->port->name,
gpio->pin, err);
return err;
}
return 0;
}
/**
* @brief Attempt to acquire the modem for operations
*
* @param data common sx12xx data struct
*
* @retval true if modem was acquired
* @retval false otherwise
*/
static inline bool modem_acquire(struct sx12xx_data *data)
{
return atomic_cas(&data->modem_usage, STATE_FREE, STATE_BUSY);
}
/**
* @brief Safely release the modem from any context
*
* This function can be called from any context and guarantees that the
* release operations will only be run once.
*
* @param data common sx12xx data struct
*
* @retval true if modem was released by this function
* @retval false otherwise
*/
static bool modem_release(struct sx12xx_data *data)
{
/* Increment atomic so both acquire and release will fail */
if (!atomic_cas(&data->modem_usage, STATE_BUSY, STATE_CLEANUP)) {
return false;
}
/* Put radio back into sleep mode */
Radio.Sleep();
/* Completely release modem */
data->operation_done = NULL;
atomic_clear(&data->modem_usage);
return true;
}
static void sx12xx_ev_rx_done(uint8_t *payload, uint16_t size, int16_t rssi,
int8_t snr)
{
struct k_poll_signal *sig = dev_data.operation_done;
/* Receiving in asynchronous mode */
if (dev_data.async_rx_cb) {
/* Start receiving again */
Radio.Rx(0);
/* Run the callback */
dev_data.async_rx_cb(dev_data.dev, payload, size, rssi, snr);
/* Don't run the synchronous code */
return;
}
/* Manually release the modem instead of just calling modem_release
* as we need to perform cleanup operations while still ensuring
* others can't use the modem.
*/
if (!atomic_cas(&dev_data.modem_usage, STATE_BUSY, STATE_CLEANUP)) {
return;
}
/* We can make two observations here:
* 1. lora_recv hasn't already exited due to a timeout.
* (modem_release would have been successfully called)
* 2. If the k_poll in lora_recv times out before we raise the signal,
* but while this code is running, it will block on the
* signal again.
* This lets us guarantee that the operation_done signal and pointers
* in rx_params are always valid in this function.
*/
/* Store actual size */
if (size < *dev_data.rx_params.size) {
*dev_data.rx_params.size = size;
}
/* Copy received data to output buffer */
memcpy(dev_data.rx_params.buf, payload,
*dev_data.rx_params.size);
/* Output RSSI and SNR */
if (dev_data.rx_params.rssi) {
*dev_data.rx_params.rssi = rssi;
}
if (dev_data.rx_params.snr) {
*dev_data.rx_params.snr = snr;
}
/* Put radio back into sleep mode */
Radio.Sleep();
/* Completely release modem */
dev_data.operation_done = NULL;
atomic_clear(&dev_data.modem_usage);
/* Notify caller RX is complete */
k_poll_signal_raise(sig, 0);
}
static void sx12xx_ev_tx_done(void)
{
struct k_poll_signal *sig = dev_data.operation_done;
if (modem_release(&dev_data)) {
/* Raise signal if provided */
if (sig) {
k_poll_signal_raise(sig, 0);
}
}
}
static void sx12xx_ev_tx_timed_out(void)
{
/* Just release the modem */
modem_release(&dev_data);
}
int sx12xx_lora_send(const struct device *dev, uint8_t *data,
uint32_t data_len)
{
struct k_poll_signal done = K_POLL_SIGNAL_INITIALIZER(done);
struct k_poll_event evt = K_POLL_EVENT_INITIALIZER(
K_POLL_TYPE_SIGNAL,
K_POLL_MODE_NOTIFY_ONLY,
&done);
uint32_t air_time;
int ret;
/* Validate that we have a TX configuration */
if (!dev_data.tx_cfg.frequency) {
return -EINVAL;
}
ret = sx12xx_lora_send_async(dev, data, data_len, &done);
if (ret < 0) {
return ret;
}
/* Calculate expected airtime of the packet */
air_time = Radio.TimeOnAir(MODEM_LORA,
dev_data.tx_cfg.bandwidth,
dev_data.tx_cfg.datarate,
dev_data.tx_cfg.coding_rate,
dev_data.tx_cfg.preamble_len,
0, data_len, true);
LOG_DBG("Expected air time of %d bytes = %dms", data_len, air_time);
/* Wait for the packet to finish transmitting.
* Use twice the tx duration to ensure that we are actually detecting
* a failed transmission, and not some minor timing variation between
* modem and driver.
*/
ret = k_poll(&evt, 1, K_MSEC(2 * air_time));
if (ret < 0) {
LOG_ERR("Packet transmission failed!");
if (!modem_release(&dev_data)) {
/* TX done interrupt is currently running */
k_poll(&evt, 1, K_FOREVER);
}
}
return ret;
}
int sx12xx_lora_send_async(const struct device *dev, uint8_t *data,
uint32_t data_len, struct k_poll_signal *async)
{
/* Ensure available, freed by sx12xx_ev_tx_done */
if (!modem_acquire(&dev_data)) {
return -EBUSY;
}
/* Store signal */
dev_data.operation_done = async;
Radio.SetMaxPayloadLength(MODEM_LORA, data_len);
Radio.Send(data, data_len);
return 0;
}
int sx12xx_lora_recv(const struct device *dev, uint8_t *data, uint8_t size,
k_timeout_t timeout, int16_t *rssi, int8_t *snr)
{
struct k_poll_signal done = K_POLL_SIGNAL_INITIALIZER(done);
struct k_poll_event evt = K_POLL_EVENT_INITIALIZER(
K_POLL_TYPE_SIGNAL,
K_POLL_MODE_NOTIFY_ONLY,
&done);
int ret;
/* Ensure available, decremented by sx12xx_ev_rx_done or on timeout */
if (!modem_acquire(&dev_data)) {
return -EBUSY;
}
dev_data.async_rx_cb = NULL;
/* Store operation signal */
dev_data.operation_done = &done;
/* Set data output location */
dev_data.rx_params.buf = data;
dev_data.rx_params.size = &size;
dev_data.rx_params.rssi = rssi;
dev_data.rx_params.snr = snr;
Radio.SetMaxPayloadLength(MODEM_LORA, 255);
Radio.Rx(0);
ret = k_poll(&evt, 1, timeout);
if (ret < 0) {
if (!modem_release(&dev_data)) {
/* Releasing the modem failed, which means that
* the RX callback is currently running. Wait until
* the RX callback finishes and we get our packet.
*/
k_poll(&evt, 1, K_FOREVER);
/* We did receive a packet */
return size;
}
LOG_INF("Receive timeout");
return ret;
}
return size;
}
int sx12xx_lora_recv_async(const struct device *dev, lora_recv_cb cb)
{
/* Cancel ongoing reception */
if (cb == NULL) {
if (!modem_release(&dev_data)) {
/* Not receiving or already being stopped */
return -EINVAL;
}
return 0;
}
/* Ensure available */
if (!modem_acquire(&dev_data)) {
return -EBUSY;
}
/* Store parameters */
dev_data.async_rx_cb = cb;
/* Start reception */
Radio.SetMaxPayloadLength(MODEM_LORA, 255);
Radio.Rx(0);
return 0;
}
int sx12xx_lora_config(const struct device *dev,
struct lora_modem_config *config)
{
/* Ensure available, decremented after configuration */
if (!modem_acquire(&dev_data)) {
return -EBUSY;
}
Radio.SetChannel(config->frequency);
if (config->tx) {
/* Store TX config locally for airtime calculations */
memcpy(&dev_data.tx_cfg, config, sizeof(dev_data.tx_cfg));
/* Configure radio driver */
Radio.SetTxConfig(MODEM_LORA, config->tx_power, 0,
config->bandwidth, config->datarate,
config->coding_rate, config->preamble_len,
false, true, 0, 0, config->iq_inverted, 4000);
} else {
/* TODO: Get symbol timeout value from config parameters */
Radio.SetRxConfig(MODEM_LORA, config->bandwidth,
config->datarate, config->coding_rate,
0, config->preamble_len, 10, false, 0,
false, 0, 0, config->iq_inverted, true);
}
Radio.SetPublicNetwork(config->public_network);
modem_release(&dev_data);
return 0;
}
int sx12xx_lora_test_cw(const struct device *dev, uint32_t frequency,
int8_t tx_power,
uint16_t duration)
{
/* Ensure available, freed in sx12xx_ev_tx_done */
if (!modem_acquire(&dev_data)) {
return -EBUSY;
}
Radio.SetTxContinuousWave(frequency, tx_power, duration);
return 0;
}
int sx12xx_init(const struct device *dev)
{
atomic_set(&dev_data.modem_usage, 0);
dev_data.dev = dev;
dev_data.events.TxDone = sx12xx_ev_tx_done;
dev_data.events.RxDone = sx12xx_ev_rx_done;
/* TX timeout event raises at the end of the test CW transmission */
dev_data.events.TxTimeout = sx12xx_ev_tx_timed_out;
Radio.Init(&dev_data.events);
/*
* Automatically place the radio into sleep mode upon boot.
* The required `lora_config` call before transmission or reception
* will bring the radio out of sleep mode before it is used. The radio
* is automatically placed back into sleep mode upon TX or RX
* completion.
*/
Radio.Sleep();
return 0;
}
|