Added CMake for NonArduino example

This commit is contained in:
jgromes 2023-04-30 22:10:48 +02:00
parent a9eaf5f2fb
commit e1853ddf04
5 changed files with 82 additions and 59 deletions

View file

@ -0,0 +1 @@
build/

View file

@ -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)

View file

@ -0,0 +1,8 @@
#!/bin/bash
set -e
mkdir -p build
cd build
cmake -G "CodeBlocks - Unix Makefiles" ..
make -j4
cd ..

View file

@ -0,0 +1,3 @@
#!/bin/bash
rm -rf ./build

View file

@ -1,8 +1,8 @@
/* /*
RadioLib Non-Arduino Raspberry Pi Example RadioLib Non-Arduino Raspberry Pi Example
This example shows how to use RadioLib without Arduino. 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. using the pigpio library.
Can be used as a starting point to port RadioLib to any platform! Can be used as a starting point to port RadioLib to any platform!
@ -14,7 +14,7 @@
*/ */
// include the library // include the library
#include "RadioLib.h" #include <RadioLib.h>
// include the library for Raspberry GPIO pins // include the library for Raspberry GPIO pins
#include "pigpio.h" #include "pigpio.h"
@ -26,11 +26,10 @@
class PiHal : public RadioLibHal { class PiHal : public RadioLibHal {
public: public:
// default constructor - initializes the base HAL and any needed private members // 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), : RadioLibHal(PI_INPUT, PI_OUTPUT, PI_LOW, PI_HIGH, RISING_EDGE, FALLING_EDGE),
_spiChannel(spiChannel), _spiChannel(spiChannel),
_spiSpeed(spiSpeed) { _spiSpeed(spiSpeed) {
} }
void init() override { void init() override {
@ -39,6 +38,10 @@ class PiHal : public RadioLibHal {
// now the SPI // now the SPI
spiBegin(); 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 { void term() override {
@ -47,6 +50,10 @@ class PiHal : public RadioLibHal {
// and now the pigpio library // and now the pigpio library
gpioTerminate(); gpioTerminate();
// finally, pull the enable pin low
gpioSetMode(18, PI_OUTPUT);
gpioWrite(18, PI_LOW);
} }
// GPIO-related methods (pinMode, digitalWrite etc.) should check // GPIO-related methods (pinMode, digitalWrite etc.) should check
@ -55,6 +62,7 @@ class PiHal : public RadioLibHal {
if(pin == RADIOLIB_NC) { if(pin == RADIOLIB_NC) {
return; return;
} }
gpioSetMode(pin, mode); gpioSetMode(pin, mode);
} }
@ -62,6 +70,7 @@ class PiHal : public RadioLibHal {
if(pin == RADIOLIB_NC) { if(pin == RADIOLIB_NC) {
return; return;
} }
gpioWrite(pin, value); gpioWrite(pin, value);
} }
@ -69,6 +78,7 @@ class PiHal : public RadioLibHal {
if(pin == RADIOLIB_NC) { if(pin == RADIOLIB_NC) {
return(0); return(0);
} }
return(gpioRead(pin)); return(gpioRead(pin));
} }
@ -76,6 +86,7 @@ class PiHal : public RadioLibHal {
if(interruptNum == RADIOLIB_NC) { if(interruptNum == RADIOLIB_NC) {
return; return;
} }
gpioSetISRFunc(interruptNum, mode, 0, (gpioISRFunc_t)interruptCb); gpioSetISRFunc(interruptNum, mode, 0, (gpioISRFunc_t)interruptCb);
} }
@ -83,6 +94,7 @@ class PiHal : public RadioLibHal {
if(interruptNum == RADIOLIB_NC) { if(interruptNum == RADIOLIB_NC) {
return; return;
} }
gpioSetISRFunc(interruptNum, NULL, NULL, nullptr); gpioSetISRFunc(interruptNum, NULL, NULL, nullptr);
} }
@ -119,8 +131,8 @@ class PiHal : public RadioLibHal {
return(gpioTick() - start); return(gpioTick() - start);
} }
void spiBegin() { void spiBegin() {
if(_spiHandle < 0) { if(_spiHandle < 0) {
_spiHandle = spiOpen(_spiChannel, _spiSpeed, 0); _spiHandle = spiOpen(_spiChannel, _spiSpeed, 0);
} }
@ -137,7 +149,7 @@ class PiHal : public RadioLibHal {
void spiEndTransaction() {} void spiEndTransaction() {}
void spiEnd() { void spiEnd() {
if (_spiHandle >= 0) { if(_spiHandle >= 0) {
spiClose(_spiHandle); spiClose(_spiHandle);
_spiHandle = -1; _spiHandle = -1;
} }
@ -150,71 +162,50 @@ class PiHal : public RadioLibHal {
int _spiHandle = -1; int _spiHandle = -1;
}; };
// now we can create the radio module // create a new instance of the HAL class
// the first argument is a new isntance of the HAL class defined above // use SPI channel 1, because on Waveshare LoRaWAN Hat,
// the others are pin numbers // the SX1261 CS is connected to CE1
CC1101 radio = new Module(new PiHal(), 8, 24, RADIOLIB_NC, 25); PiHal* hal = new PiHal(1);
// forward declaration of ISR function // now we can create the radio module
void onPacket(); // 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 // the entry point for the program
int main(int argc, char** argv) { int main(int argc, char** argv) {
// initialize just like with Arduino // initialize just like with Arduino
printf("[CC1101] Initializing ... "); printf("[SX1261] Initializing ... ");
int state = radio.begin(); int state = radio.begin();
if (state != RADIOLIB_ERR_NONE) { if (state != RADIOLIB_ERR_NONE) {
printf("failed, code %d", state ); printf("failed, code %d\n", state);
return(1); return(1);
} }
printf("success!\n");
// set the function that will be called // loop forever
// when new packet is received for(;;) {
// RISING_EDGE is from the pigpio library // send a packet
radio.setGdo0Action(onPacket, RISING_EDGE); 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 // wait for a second before transmitting again
printf(F("[CC1101] Starting to listen ... ")); hal->delay(1000);
state = radio.startReceive();
if(state != RADIOLIB_ERR_NONE) { } else {
printf("failed, code %d", state); printf("failed, code %d\n", state);
return(1);
}
}
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 return(0);
radio.startReceive();
} }