From e1853ddf04df7fba95c905de5714407706612f02 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 30 Apr 2023 22:10:48 +0200 Subject: [PATCH] Added CMake for NonArduino example --- examples/NonArduino/Raspberry/.gitignore | 1 + examples/NonArduino/Raspberry/CMakeLists.txt | 20 ++++ examples/NonArduino/Raspberry/build.sh | 8 ++ examples/NonArduino/Raspberry/clean.sh | 3 + examples/NonArduino/Raspberry/main.cpp | 109 +++++++++---------- 5 files changed, 82 insertions(+), 59 deletions(-) create mode 100644 examples/NonArduino/Raspberry/.gitignore create mode 100644 examples/NonArduino/Raspberry/CMakeLists.txt create mode 100644 examples/NonArduino/Raspberry/build.sh create mode 100644 examples/NonArduino/Raspberry/clean.sh diff --git a/examples/NonArduino/Raspberry/.gitignore b/examples/NonArduino/Raspberry/.gitignore new file mode 100644 index 00000000..567609b1 --- /dev/null +++ b/examples/NonArduino/Raspberry/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/examples/NonArduino/Raspberry/CMakeLists.txt b/examples/NonArduino/Raspberry/CMakeLists.txt new file mode 100644 index 00000000..5853fe9e --- /dev/null +++ b/examples/NonArduino/Raspberry/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.18) + +# create the project +project(rpi-sx1261) + +# when using debuggers such as gdb, the following line can be used +#set(CMAKE_BUILD_TYPE Debug) + +# add the RadioLib source directory +# this is a bit of a hack because the example is being built within a library +add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../RadioLib" "${CMAKE_CURRENT_BINARY_DIR}/RadioLib") + +# add the executable +add_executable(${PROJECT_NAME} main.cpp) + +# link both libraries +target_link_libraries(${PROJECT_NAME} RadioLib pigpio) + +# you can also specify RadioLib compile-time flags here +#target_compile_definitions(${PROJECT_NAME} PUBLIC RADIOLIB_DEBUG RADIOLIB_VERBOSE) diff --git a/examples/NonArduino/Raspberry/build.sh b/examples/NonArduino/Raspberry/build.sh new file mode 100644 index 00000000..a46bbdf4 --- /dev/null +++ b/examples/NonArduino/Raspberry/build.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +set -e +mkdir -p build +cd build +cmake -G "CodeBlocks - Unix Makefiles" .. +make -j4 +cd .. diff --git a/examples/NonArduino/Raspberry/clean.sh b/examples/NonArduino/Raspberry/clean.sh new file mode 100644 index 00000000..27cfe264 --- /dev/null +++ b/examples/NonArduino/Raspberry/clean.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +rm -rf ./build diff --git a/examples/NonArduino/Raspberry/main.cpp b/examples/NonArduino/Raspberry/main.cpp index 442145c8..a7e59c15 100644 --- a/examples/NonArduino/Raspberry/main.cpp +++ b/examples/NonArduino/Raspberry/main.cpp @@ -1,8 +1,8 @@ /* RadioLib Non-Arduino Raspberry Pi Example - + This example shows how to use RadioLib without Arduino. - In this case, a CC1101 module is connected to Raspberry Pi + In this case, a Raspberry Pi with WaveShare SX1302 LoRaWAN Hat using the pigpio library. Can be used as a starting point to port RadioLib to any platform! @@ -14,7 +14,7 @@ */ // include the library -#include "RadioLib.h" +#include // include the library for Raspberry GPIO pins #include "pigpio.h" @@ -26,11 +26,10 @@ class PiHal : public RadioLibHal { public: // default constructor - initializes the base HAL and any needed private members - PiHal(uint8_t spiChannel = 0, uint32_t spiSpeed = 2000000) + PiHal(uint8_t spiChannel, uint32_t spiSpeed = 2000000) : RadioLibHal(PI_INPUT, PI_OUTPUT, PI_LOW, PI_HIGH, RISING_EDGE, FALLING_EDGE), _spiChannel(spiChannel), _spiSpeed(spiSpeed) { - } void init() override { @@ -39,6 +38,10 @@ class PiHal : public RadioLibHal { // now the SPI spiBegin(); + + // Waveshare LoRaWAN Hat also needs pin 18 to be pulled high to enable the radio + gpioSetMode(18, PI_OUTPUT); + gpioWrite(18, PI_HIGH); } void term() override { @@ -47,6 +50,10 @@ class PiHal : public RadioLibHal { // and now the pigpio library gpioTerminate(); + + // finally, pull the enable pin low + gpioSetMode(18, PI_OUTPUT); + gpioWrite(18, PI_LOW); } // GPIO-related methods (pinMode, digitalWrite etc.) should check @@ -55,6 +62,7 @@ class PiHal : public RadioLibHal { if(pin == RADIOLIB_NC) { return; } + gpioSetMode(pin, mode); } @@ -62,6 +70,7 @@ class PiHal : public RadioLibHal { if(pin == RADIOLIB_NC) { return; } + gpioWrite(pin, value); } @@ -69,6 +78,7 @@ class PiHal : public RadioLibHal { if(pin == RADIOLIB_NC) { return(0); } + return(gpioRead(pin)); } @@ -76,6 +86,7 @@ class PiHal : public RadioLibHal { if(interruptNum == RADIOLIB_NC) { return; } + gpioSetISRFunc(interruptNum, mode, 0, (gpioISRFunc_t)interruptCb); } @@ -83,6 +94,7 @@ class PiHal : public RadioLibHal { if(interruptNum == RADIOLIB_NC) { return; } + gpioSetISRFunc(interruptNum, NULL, NULL, nullptr); } @@ -119,8 +131,8 @@ class PiHal : public RadioLibHal { return(gpioTick() - start); } - - void spiBegin() { + + void spiBegin() { if(_spiHandle < 0) { _spiHandle = spiOpen(_spiChannel, _spiSpeed, 0); } @@ -137,7 +149,7 @@ class PiHal : public RadioLibHal { void spiEndTransaction() {} void spiEnd() { - if (_spiHandle >= 0) { + if(_spiHandle >= 0) { spiClose(_spiHandle); _spiHandle = -1; } @@ -150,71 +162,50 @@ class PiHal : public RadioLibHal { int _spiHandle = -1; }; -// now we can create the radio module -// the first argument is a new isntance of the HAL class defined above -// the others are pin numbers -CC1101 radio = new Module(new PiHal(), 8, 24, RADIOLIB_NC, 25); +// create a new instance of the HAL class +// use SPI channel 1, because on Waveshare LoRaWAN Hat, +// the SX1261 CS is connected to CE1 +PiHal* hal = new PiHal(1); -// forward declaration of ISR function -void onPacket(); +// now we can create the radio module +// the first argument is a new instance of the HAL class defined above +// the others are pin numbers +// pinout corresponds to the Waveshare LoRaWAN Hat +// NSS pin: 7 +// DIO1 pin: 17 +// NRST pin: 22 +// BUSY pin: 4 +SX1261 radio = new Module(hal, 7, 17, 22, 4); // the entry point for the program int main(int argc, char** argv) { // initialize just like with Arduino - printf("[CC1101] Initializing ... "); + printf("[SX1261] Initializing ... "); int state = radio.begin(); if (state != RADIOLIB_ERR_NONE) { - printf("failed, code %d", state ); + printf("failed, code %d\n", state); return(1); } + printf("success!\n"); - // set the function that will be called - // when new packet is received - // RISING_EDGE is from the pigpio library - radio.setGdo0Action(onPacket, RISING_EDGE); + // loop forever + for(;;) { + // send a packet + printf("[SX1261] Transmitting packet ... "); + state = radio.transmit("Hello World!"); + if(state == RADIOLIB_ERR_NONE) { + // the packet was successfully transmitted + printf("success!"); - // start listening for packets - printf(F("[CC1101] Starting to listen ... ")); - state = radio.startReceive(); - if(state != RADIOLIB_ERR_NONE) { - printf("failed, code %d", state); - return(1); - } -} + // wait for a second before transmitting again + hal->delay(1000); + + } else { + printf("failed, code %d\n", state); -void onPacket() { - // packet received, read the data - uint8_t byteArr[128]; - int state = radio.readData(byteArr, sizeof(byteArr)); - - if (state == RADIOLIB_ERR_NONE) { - // packet was successfully received - printf("[CC1101] Received packet!"); - - // print the data of the packet - printf("[CC1101] Data:\t\t"); - for (int b = 0; b < sizeof(byteArr); b++){ - printf("%X", byteArr[b]); } - printf("\n"); - - // print RSSI (Received Signal Strength Indicator) - // of the last received packet - printf("[CC1101] RSSI:\t\t%d dBm\n", radio.getRSSI()); - - // print LQI (Link Quality Indicator) - // of the last received packet, lower is better - printf("[CC1101] LQI:\t\t%d\n", radio.getLQI()); - } else if (state == RADIOLIB_ERR_CRC_MISMATCH) { - // packet was received, but is malformed - printf("[CC1101] CRC error!\n"); - - } else { - // some other error occurred - printf("[CC1101] Failed, code %d\n", state); } - // put module back to listen mode - radio.startReceive(); + return(0); }