summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 0280998760be06fa509fd46397d00fc702a033d1 (plain)
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
#include <string.h>
#include <zephyr/device.h>
#include <zephyr/drivers/lora.h>
#include <zephyr/kernel.h>
#include <zephyr/shell/shell.h>
#include <zephyr/sys/util.h>

#define DEFAULT_RADIO_NODE DT_ALIAS(lora0)
BUILD_ASSERT(DT_NODE_HAS_STATUS(DEFAULT_RADIO_NODE, okay),
             "Board has no LoRa modem");
const struct device *const lora_dev = DEVICE_DT_GET(DEFAULT_RADIO_NODE);

#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(lora);

static struct lora_modem_config modem_config = {
	.frequency = 433175000,
	.bandwidth = BW_125_KHZ,
	.datarate = SF_10,
	.preamble_len = 8,
	.coding_rate = CR_4_5,
	.iq_inverted = false,
	.public_network = false,
	.tx_power = 4,
	.tx = true
};

void lora_rx_cb(const struct device *dev, uint8_t *data, uint16_t size,
                int16_t rssi, int8_t snr)
{
	ARG_UNUSED(dev);
	ARG_UNUSED(size);

	LOG_INF("rx: %s (RSSI: %d dBm, SNR: %d dBm)", data, rssi, snr);
}

void lora_tx_cb(const struct shell *sh, size_t argc, char *argv[])
{
	if (argc != 2) {
		LOG_ERR("lora_tx requires exactly one argument");
		return;
	}

	modem_config.tx = true;
	int ret = lora_config(lora_dev, &modem_config);
	if (ret < 0) {
		shell_error(sh, "LoRa config to TX mode failed");
		return;
	}
	lora_recv_async(lora_dev, NULL);

	ret = lora_send(lora_dev, argv[1], strlen(argv[1]));
	if (ret < 0) {
		LOG_ERR("LoRa send failed");
		return;
	}
	LOG_INF("tx: %s", argv[1]);

	modem_config.tx = false;
	ret = lora_config(lora_dev, &modem_config);
	if (ret < 0) {
		shell_error(sh, "LoRa config to RX mode failed");
		return;
	}
	lora_recv_async(lora_dev, lora_rx_cb);
}
SHELL_CMD_ARG_REGISTER(tx, NULL, "send via LoRa", lora_tx_cb, 2, 0);

int main(void)
{
	if (!device_is_ready(lora_dev)) {
		LOG_ERR("%s Device not ready", lora_dev->name);
		return 0;
	}

	int ret = lora_config(lora_dev, &modem_config);
	if (ret < 0) {
		LOG_ERR("LoRa config failed");
		return 0;
	}

	lora_recv_async(lora_dev, lora_rx_cb);
	k_sleep(K_FOREVER);
	return 0;
}