Skip to content

Commit

Permalink
lora: Initial support for sensor example
Browse files Browse the repository at this point in the history
Signed-off-by: Alistair Francis <[email protected]>
  • Loading branch information
alistair23 committed Sep 11, 2023
1 parent c5286a7 commit 387ebc9
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "lvgl/lvgl"]
path = lvgl/lvgl
url = https://github.com/littlevgl/lvgl.git
[submodule "examples/lora/RadioLib"]
path = examples/lora/RadioLib
url = https://github.com/jgromes/RadioLib.git
25 changes: 25 additions & 0 deletions examples/lora/README.md
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.
1 change: 1 addition & 0 deletions examples/lora/RadioLib
Submodule RadioLib added at 0ef554
26 changes: 26 additions & 0 deletions examples/lora/build-RadioLib.sh
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
33 changes: 33 additions & 0 deletions examples/lora/sensor-receive/Makefile
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
4 changes: 4 additions & 0 deletions examples/lora/sensor-receive/README.md
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.
71 changes: 71 additions & 0 deletions examples/lora/sensor-receive/main.cc
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 <[email protected]>
*/

// 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;
}
33 changes: 33 additions & 0 deletions examples/lora/sensor-transmit/Makefile
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
4 changes: 4 additions & 0 deletions examples/lora/sensor-transmit/README.md
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.
81 changes: 81 additions & 0 deletions examples/lora/sensor-transmit/main.cc
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 <[email protected]>
*/

// 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;
}

0 comments on commit 387ebc9

Please sign in to comment.