-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lora: Initial support for sensor example
Signed-off-by: Alistair Francis <alistair@alistair23.me>
- Loading branch information
1 parent
c5286a7
commit 4505330
Showing
11 changed files
with
284 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
LoRa | ||
==== | ||
|
||
LoRa (Long Range) is a radio communication technique that uses | ||
spread spectrum modulation technique derived from chirp spread | ||
spectrum (CSS) technology. | ||
|
||
LoRa provides a number of properties including: | ||
|
||
* long range, can cover tens of kilometres | ||
* low power, devices can run for years | ||
* reasonably secure | ||
* standardised | ||
* relativity cheap | ||
|
||
This directory contains a range of examples using | ||
[RadioLib](https://github.com/jgromes/RadioLib) to | ||
support LoRa on Tock. | ||
|
||
Before the examples will work you first need to build | ||
RadioLib. | ||
|
||
Note that the Makefiles will do this automatically when | ||
you run `make` in a subdirectory, but if you want to do | ||
it manually you can run the `build-RadioLib.sh` script. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/bin/bash | ||
|
||
pushd RadioLib/examples/NonArduino/Tock/ | ||
|
||
rm -rf build | ||
|
||
# Change the source to point to the current libtock-c directory | ||
# Note, x-platform `sed -i` has odd, but particular syntax | ||
# https://stackoverflow.com/questions/5694228/sed-in-place-flag-that-works-both-on-mac-bsd-and-linux | ||
sed -i._SED_HACK 's|${CMAKE_CURRENT_SOURCE_DIR}/libtock-c|../../../../../../../|g' CMakeLists.txt | ||
sed -i._SED_HACK 's|target_include_directories(${PROJECT_NAME} PUBLIC|target_include_directories(\${PROJECT_NAME}\n PUBLIC ../../../../../../|g' CMakeLists.txt | ||
|
||
find . -name '*._SED_HACK' -delete | ||
|
||
mkdir -p build | ||
cd build | ||
|
||
cmake -G "CodeBlocks - Unix Makefiles" .. | ||
|
||
# Build the Tock example application | ||
# This will fail to link, as it can't find the libtock-c libraries | ||
# That's fine for us though, as we just need to build the RadioLib | ||
# library, not the entire example application | ||
make -j4 2> /dev/null | ||
|
||
popd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Makefile for user application | ||
|
||
# Specify this directory relative to the current application. | ||
TOCK_USERLAND_BASE_DIR = ../../../ | ||
|
||
# We only compile this for Cortex-M platforms because we only have | ||
# libraries for Cortex-M. | ||
TOCK_TARGETS := cortex-m0 cortex-m3 cortex-m4 cortex-m7 | ||
|
||
# Which files to compile. | ||
CXX_SRCS := $(wildcard *.cc) | ||
|
||
# Include the core RadioLib headers | ||
override CPPFLAGS += -I../RadioLib/src | ||
|
||
# Include the Tock specific headers | ||
override CPPFLAGS += -I../RadioLib/examples/NonArduino/Tock | ||
|
||
# Include the base of libtock-c to fix the libtock/ includes from RadioLib | ||
override CPPFLAGS += -I$(TOCK_USERLAND_BASE_DIR)/ | ||
|
||
override LEGACY_LIBS_cortex-m += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
override LEGACY_LIBS_cortex-m0 += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
override LEGACY_LIBS_cortex-m3 += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
override LEGACY_LIBS_cortex-m4 += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
override LEGACY_LIBS_cortex-m7 += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
|
||
# Include userland master makefile. Contains rules and flags for actually | ||
# building the application. | ||
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk | ||
|
||
../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a: | ||
cd ../ && ./build-RadioLib.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Sensor Receive | ||
============== | ||
|
||
This example builds an application to receiver sensor data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
RadioLib Non-Arduino Tock Library test application | ||
Licensed under the MIT or Apache License | ||
Copyright (c) 2023 Alistair Francis <alistair@alistair23.me> | ||
*/ | ||
|
||
// include the library | ||
#include <RadioLib.h> | ||
|
||
// include the hardware abstraction layer | ||
#include "libtockHal.h" | ||
|
||
// Include some libtock-c helpers | ||
#include <humidity.h> | ||
#include <temperature.h> | ||
|
||
#define BUFFER_LEN 64 | ||
|
||
// the entry point for the program | ||
int main(void) { | ||
char buffer[BUFFER_LEN]; | ||
|
||
printf("[SX1261] Initialising Radio ... \n"); | ||
|
||
// create a new instance of the HAL class | ||
TockHal* hal = new TockHal(); | ||
|
||
// now we can create the radio module | ||
// pinout corresponds to the SparkFun LoRa Thing Plus - expLoRaBLE | ||
// NSS pin: 0 | ||
// DIO1 pin: 2 | ||
// NRST pin: 4 | ||
// BUSY pin: 1 | ||
Module* tock_module = new Module(hal, RADIO_NSS, RADIO_DIO_1, RADIO_RESET, RADIO_BUSY); | ||
SX1262* radio = new SX1262(tock_module); | ||
|
||
// Setup the radio | ||
// The settings here work for the SparkFun LoRa Thing Plus - expLoRaBLE | ||
radio->XTAL = true; | ||
int state = radio->begin(915.0); | ||
|
||
if (state != RADIOLIB_ERR_NONE) { | ||
printf("failed, code %d\r\n", state); | ||
return 1; | ||
} | ||
printf("success!\r\n"); | ||
|
||
printf("[SX1261] Receiving...\r\n"); | ||
|
||
// loop forever | ||
for ( ;;) { | ||
// Ensure there are no pending callbacks | ||
yield_no_wait(); | ||
|
||
state = radio->receive((uint8_t*)buffer, BUFFER_LEN); | ||
|
||
if (state == RADIOLIB_ERR_NONE) { | ||
// the packet was successfully transmitted | ||
printf("success!: %s\r\n", buffer); | ||
|
||
// wait for a second before transmitting again | ||
hal->delay(1000); | ||
} else { | ||
printf("failed, code %d\r\n", state); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Makefile for user application | ||
|
||
# Specify this directory relative to the current application. | ||
TOCK_USERLAND_BASE_DIR = ../../../ | ||
|
||
# We only compile this for Cortex-M platforms because we only have | ||
# libraries for Cortex-M. | ||
TOCK_TARGETS := cortex-m0 cortex-m3 cortex-m4 cortex-m7 | ||
|
||
# Which files to compile. | ||
CXX_SRCS := $(wildcard *.cc) | ||
|
||
# Include the core RadioLib headers | ||
override CPPFLAGS += -I../RadioLib/src | ||
|
||
# Include the Tock specific headers | ||
override CPPFLAGS += -I../RadioLib/examples/NonArduino/Tock | ||
|
||
# Include the base of libtock-c to fix the libtock/ includes from RadioLib | ||
override CPPFLAGS += -I$(TOCK_USERLAND_BASE_DIR)/ | ||
|
||
override LEGACY_LIBS_cortex-m += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
override LEGACY_LIBS_cortex-m0 += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
override LEGACY_LIBS_cortex-m3 += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
override LEGACY_LIBS_cortex-m4 += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
override LEGACY_LIBS_cortex-m7 += ../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a | ||
|
||
# Include userland master makefile. Contains rules and flags for actually | ||
# building the application. | ||
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk | ||
|
||
../RadioLib/examples/NonArduino/Tock/build/RadioLib/libRadioLib.a: | ||
cd ../ && ./build-RadioLib.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Sensor Transmitter | ||
================== | ||
|
||
This example builds an application to transmit sensor data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
RadioLib Non-Arduino Tock Library test application | ||
Licensed under the MIT or Apache License | ||
Copyright (c) 2023 Alistair Francis <alistair@alistair23.me> | ||
*/ | ||
|
||
// include the library | ||
#include <RadioLib.h> | ||
|
||
// include the hardware abstraction layer | ||
#include "libtockHal.h" | ||
|
||
// Include some libtock-c helpers | ||
#include <humidity.h> | ||
#include <temperature.h> | ||
|
||
#define BUFFER_LEN 64 | ||
|
||
// the entry point for the program | ||
int main(void) { | ||
char buffer[BUFFER_LEN]; | ||
|
||
printf("[SX1261] Initialising Radio ... \n"); | ||
|
||
// create a new instance of the HAL class | ||
TockHal* hal = new TockHal(); | ||
|
||
// now we can create the radio module | ||
// pinout corresponds to the SparkFun LoRa Thing Plus - expLoRaBLE | ||
// NSS pin: 0 | ||
// DIO1 pin: 2 | ||
// NRST pin: 4 | ||
// BUSY pin: 1 | ||
Module* tock_module = new Module(hal, RADIO_NSS, RADIO_DIO_1, RADIO_RESET, RADIO_BUSY); | ||
SX1262* radio = new SX1262(tock_module); | ||
|
||
// Setup the radio | ||
// The settings here work for the SparkFun LoRa Thing Plus - expLoRaBLE | ||
radio->XTAL = true; | ||
int state = radio->begin(915.0); | ||
|
||
if (state != RADIOLIB_ERR_NONE) { | ||
printf("failed, code %d\r\n", state); | ||
return 1; | ||
} | ||
printf("success!\r\n"); | ||
|
||
int temp = 0; | ||
unsigned humi = 0; | ||
|
||
// loop forever | ||
for ( ;;) { | ||
// Ensure there are no pending callbacks | ||
yield_no_wait(); | ||
|
||
// Read some sensor data from the board | ||
temperature_read_sync(&temp); | ||
humidity_read_sync(&humi); | ||
|
||
snprintf(buffer, BUFFER_LEN, "Temp: %d, Hum: %u", temp, humi); | ||
|
||
// send a packet | ||
printf("[SX1261] Transmitting '%s' \r\n", buffer); | ||
|
||
state = radio->transmit(buffer); | ||
|
||
if (state == RADIOLIB_ERR_NONE) { | ||
// the packet was successfully transmitted | ||
printf("success!\r\n"); | ||
|
||
// wait for a second before transmitting again | ||
hal->delay(1000); | ||
} else { | ||
printf("failed, code %d\r\n", state); | ||
} | ||
} | ||
|
||
return 0; | ||
} |