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 <alistair@alistair23.me>
  • Loading branch information
alistair23 committed Aug 30, 2023
1 parent 7f29208 commit 4f7f9ea
Show file tree
Hide file tree
Showing 7 changed files with 199 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.

```shell
$ build-RadioLib.sh
```
1 change: 1 addition & 0 deletions examples/lora/RadioLib
Submodule RadioLib added at ada400
22 changes: 22 additions & 0 deletions examples/lora/build-RadioLib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

pushd RadioLib/examples/NonArduino/Tock/

rm -rf build

# Change the source to point to the current libtock-c directory
sed -i 's|${CMAKE_CURRENT_SOURCE_DIR}/libtock-c|../../../../../../../|g' CMakeLists.txt
sed -i 's|target_include_directories(${PROJECT_NAME} PUBLIC|target_include_directories(\${PROJECT_NAME}\n PUBLIC ../../../../../../|g' CMakeLists.txt

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
26 changes: 26 additions & 0 deletions examples/lora/sensor-transmit/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../../

# 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
16 changes: 16 additions & 0 deletions examples/lora/sensor-transmit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Sensor Transmitter
==================

This example builds an application that can either transmit sensor data
or receive sensor data, depending on the state of the `SEND` macro in
`main.cc`.

Before the examples will work you first need to build
RadioLib.

```shell
cd ../
$ build-RadioLib.sh
cd sensor-transmit
make
```
106 changes: 106 additions & 0 deletions examples/lora/sensor-transmit/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
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 <temperature.h>
#include <humidity.h>

#define BUFFER_LEN 64

// Define if we should send or receieve data
#define SEND

// 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");

#ifdef SEND
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);
}
}
#else /* SEND */
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);
}
}
#endif

return 0;
}

0 comments on commit 4f7f9ea

Please sign in to comment.