Update Raspberry example

This commit is contained in:
jgromes 2023-05-06 18:34:41 +01:00
parent a21cabf3e3
commit 32cef2699d
3 changed files with 162 additions and 153 deletions

View file

@ -6,9 +6,10 @@ project(rpi-sx1261)
# when using debuggers such as gdb, the following line can be used # when using debuggers such as gdb, the following line can be used
#set(CMAKE_BUILD_TYPE Debug) #set(CMAKE_BUILD_TYPE Debug)
# add the RadioLib source directory # if you did not build RadioLib as shared library (see README),
# this is a bit of a hack because the example is being built within a library # you will have to add it as source directory
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../RadioLib" "${CMAKE_CURRENT_BINARY_DIR}/RadioLib") # the following is just an example, yours will likely be different
#add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../../../RadioLib" "${CMAKE_CURRENT_BINARY_DIR}/RadioLib")
# add the executable # add the executable
add_executable(${PROJECT_NAME} main.cpp) add_executable(${PROJECT_NAME} main.cpp)

View file

@ -0,0 +1,153 @@
#ifndef PI_HAL_H
#define PI_HAL_H
// include RadioLib
#include <RadioLib/RadioLib.h>
// include the library for Raspberry GPIO pins
#include "pigpio.h"
// create a new Raspberry Pi hardware abstraction layer
// using the pigpio library
// the HAL must inherit from the base RadioLibHal class
// and implement all of its virtual methods
class PiHal : public RadioLibHal {
public:
// default constructor - initializes the base HAL and any needed private members
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 {
// first initialise pigpio library
gpioInitialise();
// 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 {
// stop the SPI
spiEnd();
// 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
// RADIOLIB_NC as an alias for non-connected pins
void pinMode(uint32_t pin, uint32_t mode) override {
if(pin == RADIOLIB_NC) {
return;
}
gpioSetMode(pin, mode);
}
void digitalWrite(uint32_t pin, uint32_t value) override {
if(pin == RADIOLIB_NC) {
return;
}
gpioWrite(pin, value);
}
uint32_t digitalRead(uint32_t pin) override {
if(pin == RADIOLIB_NC) {
return(0);
}
return(gpioRead(pin));
}
void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) override {
if(interruptNum == RADIOLIB_NC) {
return;
}
gpioSetISRFunc(interruptNum, mode, 0, (gpioISRFunc_t)interruptCb);
}
void detachInterrupt(uint32_t interruptNum) override {
if(interruptNum == RADIOLIB_NC) {
return;
}
gpioSetISRFunc(interruptNum, 0, 0, NULL);
}
void delay(unsigned long ms) override {
gpioDelay(ms * 1000);
}
void delayMicroseconds(unsigned long us) override {
gpioDelay(us);
}
unsigned long millis() override {
return(gpioTick() / 1000);
}
unsigned long micros() override {
return(gpioTick());
}
long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override {
if(pin == RADIOLIB_NC) {
return(0);
}
gpioSetMode(pin, PI_INPUT);
uint32_t start = gpioTick();
uint32_t curtick = gpioTick();
while(gpioRead(pin) == state) {
if((gpioTick() - curtick) > timeout) {
return(0);
}
}
return(gpioTick() - start);
}
void spiBegin() {
if(_spiHandle < 0) {
_spiHandle = spiOpen(_spiChannel, _spiSpeed, 0);
}
}
void spiBeginTransaction() {}
uint8_t spiTransfer(uint8_t b) {
char ret;
spiXfer(_spiHandle, (char*)&b, &ret, 1);
return(ret);
}
void spiEndTransaction() {}
void spiEnd() {
if(_spiHandle >= 0) {
spiClose(_spiHandle);
_spiHandle = -1;
}
}
private:
// the HAL can contain any additional private members
const unsigned int _spiSpeed;
const uint8_t _spiChannel;
int _spiHandle = -1;
};
#endif

View file

@ -14,153 +14,10 @@
*/ */
// include the library // include the library
#include <RadioLib.h> #include <RadioLib/RadioLib.h>
// include the library for Raspberry GPIO pins // include the hardware abstraction layer
#include "pigpio.h" #include "PiHal.h"
// create a new Raspberry Pi hardware abstraction layer
// using the pigpio library
// the HAL must inherit from the base RadioLibHal class
// and implement all of its virtual methods
class PiHal : public RadioLibHal {
public:
// default constructor - initializes the base HAL and any needed private members
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 {
// first initialise pigpio library
gpioInitialise();
// 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 {
// stop the SPI
spiEnd();
// 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
// RADIOLIB_NC as an alias for non-connected pins
void pinMode(uint32_t pin, uint32_t mode) override {
if(pin == RADIOLIB_NC) {
return;
}
gpioSetMode(pin, mode);
}
void digitalWrite(uint32_t pin, uint32_t value) override {
if(pin == RADIOLIB_NC) {
return;
}
gpioWrite(pin, value);
}
uint32_t digitalRead(uint32_t pin) override {
if(pin == RADIOLIB_NC) {
return(0);
}
return(gpioRead(pin));
}
void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) override {
if(interruptNum == RADIOLIB_NC) {
return;
}
gpioSetISRFunc(interruptNum, mode, 0, (gpioISRFunc_t)interruptCb);
}
void detachInterrupt(uint32_t interruptNum) override {
if(interruptNum == RADIOLIB_NC) {
return;
}
gpioSetISRFunc(interruptNum, NULL, NULL, nullptr);
}
void delay(unsigned long ms) override {
gpioDelay(ms * 1000);
}
void delayMicroseconds(unsigned long us) override {
gpioDelay(us);
}
unsigned long millis() override {
return(gpioTick() / 1000);
}
unsigned long micros() override {
return(gpioTick());
}
long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override {
if(pin == RADIOLIB_NC) {
return(0);
}
gpioSetMode(pin, PI_INPUT);
uint32_t start = gpioTick();
uint32_t curtick = gpioTick();
while(gpioRead(pin) == state) {
if((gpioTick() - curtick) > timeout) {
return(0);
}
}
return(gpioTick() - start);
}
void spiBegin() {
if(_spiHandle < 0) {
_spiHandle = spiOpen(_spiChannel, _spiSpeed, 0);
}
}
void spiBeginTransaction() {}
uint8_t spiTransfer(uint8_t b) {
char ret;
spiXfer(_spiHandle, (char*)&b, &ret, 1);
return(ret);
}
void spiEndTransaction() {}
void spiEnd() {
if(_spiHandle >= 0) {
spiClose(_spiHandle);
_spiHandle = -1;
}
}
private:
// the HAL can contain any additional private members
const unsigned int _spiSpeed;
const uint8_t _spiChannel;
int _spiHandle = -1;
};
// create a new instance of the HAL class // create a new instance of the HAL class
// use SPI channel 1, because on Waveshare LoRaWAN Hat, // use SPI channel 1, because on Waveshare LoRaWAN Hat,
@ -168,8 +25,6 @@ class PiHal : public RadioLibHal {
PiHal* hal = new PiHal(1); PiHal* hal = new PiHal(1);
// now we can create the radio module // 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 // pinout corresponds to the Waveshare LoRaWAN Hat
// NSS pin: 7 // NSS pin: 7
// DIO1 pin: 17 // DIO1 pin: 17
@ -199,12 +54,12 @@ int main(int argc, char** argv) {
// wait for a second before transmitting again // wait for a second before transmitting again
hal->delay(1000); hal->delay(1000);
} else { } else {
printf("failed, code %d\n", state); printf("failed, code %d\n", state);
} }
} }
return(0); return(0);