blob: 4c5a8c3440b6c409f5a25e2e28a9b5078d796aea [file] [log] [blame]
/*
* Copyright (c) 2019 Manivannan Sadhasivam
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <drivers/lora.h>
#include <errno.h>
#include <sys/util.h>
#include <zephyr.h>
#define DEFAULT_RADIO_NODE DT_ALIAS(lora0)
BUILD_ASSERT(DT_NODE_HAS_STATUS(DEFAULT_RADIO_NODE, okay),
"No default LoRa radio specified in DT");
#define MAX_DATA_LEN 255
#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
#include <logging/log.h>
LOG_MODULE_REGISTER(lora_receive);
void lora_receive_cb(const struct device *dev, uint8_t *data, uint16_t size,
int16_t rssi, int8_t snr)
{
static int cnt;
ARG_UNUSED(dev);
ARG_UNUSED(size);
LOG_INF("Received data: %s (RSSI:%ddBm, SNR:%ddBm)",
log_strdup(data), rssi, snr);
/* Stop receiving after 10 packets */
if (++cnt == 10) {
LOG_INF("Stopping packet receptions");
lora_recv_async(dev, NULL);
}
}
void main(void)
{
const struct device *lora_dev = DEVICE_DT_GET(DEFAULT_RADIO_NODE);
struct lora_modem_config config;
int ret, len;
uint8_t data[MAX_DATA_LEN] = {0};
int16_t rssi;
int8_t snr;
if (!device_is_ready(lora_dev)) {
LOG_ERR("%s Device not ready", lora_dev->name);
return;
}
config.frequency = 865100000;
config.bandwidth = BW_125_KHZ;
config.datarate = SF_10;
config.preamble_len = 8;
config.coding_rate = CR_4_5;
config.tx_power = 14;
config.tx = false;
ret = lora_config(lora_dev, &config);
if (ret < 0) {
LOG_ERR("LoRa config failed");
return;
}
/* Receive 4 packets synchronously */
LOG_INF("Synchronous reception");
for (int i = 0; i < 4; i++) {
/* Block until data arrives */
len = lora_recv(lora_dev, data, MAX_DATA_LEN, K_FOREVER,
&rssi, &snr);
if (len < 0) {
LOG_ERR("LoRa receive failed");
return;
}
LOG_INF("Received data: %s (RSSI:%ddBm, SNR:%ddBm)",
log_strdup(data), rssi, snr);
}
/* Enable asynchronous reception */
LOG_INF("Asynchronous reception");
lora_recv_async(lora_dev, lora_receive_cb);
k_sleep(K_FOREVER);
}