Skip to content

Commit c0202f9

Browse files
authored
Merge pull request #456 from alistair23/alistair/lora-wan
lora: Initial commit of LoRaWAN example
2 parents 7f8181d + 2b8e837 commit c0202f9

File tree

14 files changed

+1410
-8
lines changed

14 files changed

+1410
-8
lines changed

RadioLib/Makefile.app

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
override CPPFLAGS += -I$(TOCK_USERLAND_BASE_DIR)/RadioLib/RadioLib/src
33

44
# Include the Tock specific headers
5-
override CPPFLAGS += -I$(TOCK_USERLAND_BASE_DIR)/RadioLib/RadioLib/examples/NonArduino/Tock
5+
override CPPFLAGS += -I$(TOCK_USERLAND_BASE_DIR)/RadioLib

RadioLib/RadioLib

Submodule RadioLib updated 221 files

RadioLib/libtockHal.h

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
RadioLib Non-Arduino Tock Library helper functions
3+
4+
Licensed under the MIT License
5+
6+
Copyright (c) 2023 Alistair Francis <[email protected]>
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
#ifndef TOCK_RADIOLIB_HAL_H
28+
#define TOCK_RADIOLIB_HAL_H
29+
30+
// include RadioLib
31+
#include <RadioLib.h>
32+
33+
// include all the dependencies
34+
#include "libtock/net/lora_phy.h"
35+
#include "libtock/net/syscalls/lora_phy_syscalls.h"
36+
#include "libtock-sync/net/lora_phy.h"
37+
#include "libtock/peripherals/gpio.h"
38+
#include "libtock-sync/services/alarm.h"
39+
#include "libtock/kernel/read_only_state.h"
40+
41+
#define RADIOLIB_RADIO_BUSY 1
42+
#define RADIOLIB_RADIO_DIO_1 2
43+
#define RADIOLIB_RADIO_DIO_3 3
44+
#define RADIOLIB_RADIO_RESET 4
45+
// Skip the chips select as Tock handles this for us
46+
#define RADIOLIB_RADIO_NSS RADIOLIB_NC
47+
48+
// define Arduino-style macros
49+
#define TOCK_RADIOLIB_PIN_LOW (0x0)
50+
#define TOCK_RADIOLIB_PIN_HIGH (0x1)
51+
#define TOCK_RADIOLIB_PIN_INPUT (0x01)
52+
#define TOCK_RADIOLIB_PIN_OUTPUT (0x03)
53+
#define TOCK_RADIOLIB_PIN_RISING (0x01)
54+
#define TOCK_RADIOLIB_PIN_FALLING (0x02)
55+
56+
typedef void (*gpioIrqFn)(void);
57+
58+
gpioIrqFn gpio_funcs[4] = { NULL, NULL, NULL, NULL};
59+
60+
static void lora_phy_gpio_Callback (int gpioPin,
61+
__attribute__ ((unused)) int arg2,
62+
__attribute__ ((unused)) int arg3,
63+
void* userdata)
64+
{
65+
gpioIrqFn fn = gpio_funcs[gpioPin - 1];
66+
67+
if (fn != NULL ) {
68+
fn();
69+
}
70+
}
71+
72+
class TockRadioLibHal : public RadioLibHal {
73+
public:
74+
// default constructor - initializes the base HAL and any needed private members
75+
TockRadioLibHal()
76+
: RadioLibHal(TOCK_RADIOLIB_PIN_INPUT, TOCK_RADIOLIB_PIN_OUTPUT, TOCK_RADIOLIB_PIN_LOW, TOCK_RADIOLIB_PIN_HIGH, TOCK_RADIOLIB_PIN_RISING, TOCK_RADIOLIB_PIN_FALLING) {
77+
}
78+
79+
void init() override {
80+
}
81+
82+
void term() override {
83+
}
84+
85+
// GPIO-related methods (pinMode, digitalWrite etc.) should check
86+
// RADIOLIB_NC as an alias for non-connected pins
87+
void pinMode(uint32_t pin, uint32_t mode) override {
88+
if(pin == RADIOLIB_NC) {
89+
return;
90+
}
91+
92+
if (mode == TOCK_RADIOLIB_PIN_OUTPUT) {
93+
libtock_lora_phy_gpio_enable_output(pin);
94+
} else if (mode == TOCK_RADIOLIB_PIN_INPUT) {
95+
libtock_lora_phy_gpio_enable_input(pin, libtock_pull_down);
96+
}
97+
}
98+
99+
void digitalWrite(uint32_t pin, uint32_t value) override {
100+
if(pin == RADIOLIB_NC) {
101+
return;
102+
}
103+
104+
if (value) {
105+
libtock_lora_phy_gpio_set(pin);
106+
} else {
107+
libtock_lora_phy_gpio_clear(pin);
108+
}
109+
}
110+
111+
uint32_t digitalRead(uint32_t pin) override {
112+
int value;
113+
114+
if(pin == RADIOLIB_NC) {
115+
return 0;
116+
}
117+
118+
libtock_lora_phy_gpio_read(pin, &value);
119+
120+
return value;
121+
}
122+
123+
void attachInterrupt(uint32_t interruptNum, gpioIrqFn interruptCb, uint32_t mode) override {
124+
if(interruptNum == RADIOLIB_NC) {
125+
return;
126+
}
127+
128+
gpio_funcs[interruptNum - 1] = interruptCb;
129+
libtock_lora_phy_gpio_command_interrupt_callback(lora_phy_gpio_Callback, NULL);
130+
131+
// set GPIO as input and enable interrupts on it
132+
libtock_lora_phy_gpio_enable_input(interruptNum, libtock_pull_down);
133+
libtock_lora_phy_gpio_enable_interrupt(interruptNum, libtock_change);
134+
}
135+
136+
void detachInterrupt(uint32_t interruptNum) override {
137+
if(interruptNum == RADIOLIB_NC) {
138+
return;
139+
}
140+
141+
gpio_funcs[interruptNum - 1] = NULL;
142+
libtock_lora_phy_gpio_disable_interrupt(interruptNum);
143+
libtock_lora_phy_gpio_enable_input(interruptNum, libtock_pull_down);
144+
}
145+
146+
void delay(unsigned long ms) override {
147+
#if !defined(RADIOLIB_CLOCK_DRIFT_MS)
148+
libtocksync_alarm_delay_ms(ms);
149+
#else
150+
libtocksync_alarm_delay_ms(ms * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS));
151+
#endif
152+
}
153+
154+
void delayMicroseconds(unsigned long us) override {
155+
#if !defined(RADIOLIB_CLOCK_DRIFT_MS)
156+
libtocksync_alarm_delay_ms(us / 1000);
157+
#else
158+
libtocksync_alarm_delay_ms((us * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS)) / 1000);
159+
#endif
160+
}
161+
162+
unsigned long millis() override {
163+
struct timeval tv;
164+
unsigned long ms;
165+
166+
libtock_alarm_gettimeasticks(&tv);
167+
ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
168+
169+
#if !defined(RADIOLIB_CLOCK_DRIFT_MS)
170+
return ms;
171+
#else
172+
return ms * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS);
173+
#endif
174+
}
175+
176+
unsigned long micros() override {
177+
struct timeval tv;
178+
179+
libtock_alarm_gettimeasticks(&tv);
180+
return tv.tv_sec * 1000000 + tv.tv_usec;
181+
}
182+
183+
long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override {
184+
return 0;
185+
}
186+
187+
void spiBegin() {
188+
}
189+
190+
void spiBeginTransaction() {
191+
}
192+
193+
void spiTransfer(uint8_t* out, size_t len, uint8_t* in) {
194+
libtocksync_lora_phy_read_write(out, in, len);
195+
}
196+
197+
void spiEndTransaction() {
198+
}
199+
200+
void spiEnd() {
201+
}
202+
203+
void yield() {
204+
::yield_no_wait();
205+
}
206+
207+
private:
208+
};
209+
210+
#endif

examples/build_all.sh

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
NUM_JOBS=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || 4)
44

5+
# Sentinel flag to indicate to apps that a `build all` is running.
6+
#
7+
# Generally, apps should not be sensitive to this flag. Build all is
8+
# usually only invoked by a user when they're looking for local, CI-like
9+
# behavior. However, a local repo checkout is not quite the same as a
10+
# pristine CI environment, and certain test apps may need slightly
11+
# different behavior for a local "build all" versus CI.
12+
#
13+
# For example: Some apps, e.g. LoRa-based ones, require radio
14+
# configuration that end users *must* set themselves [e.g. to select
15+
# operating region]. For users of these apps, we *do not* want them to
16+
# build successfully 'out of the box' without explicit user
17+
# intervention, but we do still want them to be compile-tested in CI. An
18+
# app may ship with a `radioConfig_example.h` but include (and
19+
# `.gitignore`) `radioConfig.h`. CI can unconditionally swap in the
20+
# `_example` version, but a local checkout should use a real config file
21+
# it is available. At the same time, apps which are not used in a local
22+
# checkout should not throw compile errors for a local "build all", and
23+
# should thus swap in the `_example` if a real config is not present. An
24+
# app build which is sensitive to `TOCK_BUILDALL` can capture this
25+
# subtlety.
26+
export TOCK_BUILDALL=1
27+
528
set -e
629
set -u
730
set -o pipefail
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
radioConfig.h

0 commit comments

Comments
 (0)