RadioLib
Universal wireless communication library for Arduino
Loading...
Searching...
No Matches
PiHal.h
1#ifndef PI_HAL_LGPIO_H
2#define PI_HAL_LGPIO_H
3
4// include RadioLib
5#include <RadioLib.h>
6
7// include the library for Raspberry GPIO pins
8#include <lgpio.h>
9
10#define PI_RISING (LG_RISING_EDGE)
11#define PI_FALLING (LG_FALLING_EDGE)
12#define PI_INPUT (0)
13#define PI_OUTPUT (1)
14#define PI_MAX_USER_GPIO (31)
15
16// forward declaration of alert handler that will be used to emulate interrupts
17static void lgpioAlertHandler(int num_alerts, lgGpioAlert_p alerts, void *userdata);
18
19// create a new Raspberry Pi hardware abstraction layer
20// using the lgpio library
21// the HAL must inherit from the base RadioLibHal class
22// and implement all of its virtual methods
23class PiHal : public RadioLibHal {
24 public:
25 // default constructor - initializes the base HAL and any needed private members
26 PiHal(uint8_t spiChannel, uint32_t spiSpeed = 2000000, uint8_t spiDevice = 0, uint8_t gpioDevice = 0)
27 : RadioLibHal(PI_INPUT, PI_OUTPUT, LG_LOW, LG_HIGH, PI_RISING, PI_FALLING),
28 _gpioDevice(gpioDevice),
29 _spiDevice(spiDevice),
30 _spiSpeed(spiSpeed),
31 _spiChannel(spiChannel) {
32 }
33
34 void init() override {
35 if(_gpioHandle != -1) {
36 return;
37 }
38
39 // first initialise lgpio library
40 if((_gpioHandle = lgGpiochipOpen(_gpioDevice)) < 0) {
41 fprintf(stderr, "Could not open GPIO chip: %s\n", lguErrorText(_gpioHandle));
42 return;
43 }
44
45 // now the SPI
46 spiBegin();
47 }
48
49 void term() override {
50 // stop the SPI
51 spiEnd();
52
53 // finally, stop the lgpio library
54 lgGpiochipClose(_gpioHandle);
55 _gpioHandle = -1;
56 }
57
58 // GPIO-related methods (pinMode, digitalWrite etc.) should check
59 // RADIOLIB_NC as an alias for non-connected pins
60 void pinMode(uint32_t pin, uint32_t mode) override {
61 if(pin == RADIOLIB_NC) {
62 return;
63 }
64
65 int result;
66 int flags = 0;
67 switch(mode) {
68 case PI_INPUT:
69 result = lgGpioClaimInput(_gpioHandle, 0, pin);
70 break;
71 case PI_OUTPUT:
72 result = lgGpioClaimOutput(_gpioHandle, flags, pin, LG_HIGH);
73 break;
74 default:
75 fprintf(stderr, "Unknown pinMode mode %" PRIu32 "\n", mode);
76 return;
77 }
78
79 if(result < 0) {
80 fprintf(stderr, "Could not claim pin %" PRIu32 " for mode %" PRIu32 ": %s\n",
81 pin, mode, lguErrorText(result));
82 }
83 }
84
85 void digitalWrite(uint32_t pin, uint32_t value) override {
86 if(pin == RADIOLIB_NC) {
87 return;
88 }
89
90 int result = lgGpioWrite(_gpioHandle, pin, value);
91 if(result < 0) {
92 fprintf(stderr, "Error writing value to pin %" PRIu32 ": %s\n", pin, lguErrorText(result));
93 }
94 }
95
96 uint32_t digitalRead(uint32_t pin) override {
97 if(pin == RADIOLIB_NC) {
98 return(0);
99 }
100
101 int result = lgGpioRead(_gpioHandle, pin);
102 if(result < 0) {
103 fprintf(stderr, "Error writing reading from pin %" PRIu32 ": %s\n", pin, lguErrorText(result));
104 }
105 return result;
106 }
107
108 void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) override {
109 if((interruptNum == RADIOLIB_NC) || (interruptNum > PI_MAX_USER_GPIO)) {
110 return;
111 }
112
113 // set lgpio alert callback
114 int result = lgGpioClaimAlert(_gpioHandle, 0, mode, interruptNum, -1);
115 if(result < 0) {
116 fprintf(stderr, "Could not claim pin %" PRIu32 " for alert: %s\n", interruptNum, lguErrorText(result));
117 return;
118 }
119
120 // enable emulated interrupt
121 interruptEnabled[interruptNum] = true;
122 interruptCallbacks[interruptNum] = interruptCb;
123
124 // lpgio reports the value of level after an interrupt, not the actual direction
125 interruptModes[interruptNum] = (mode == this->GpioInterruptFalling) ? LG_LOW : LG_HIGH;
126
127 lgGpioSetAlertsFunc(_gpioHandle, interruptNum, lgpioAlertHandler, (void *)this);
128 }
129
130 void detachInterrupt(uint32_t interruptNum) override {
131 if((interruptNum == RADIOLIB_NC) || (interruptNum > PI_MAX_USER_GPIO)) {
132 return;
133 }
134
135 // clear emulated interrupt
136 interruptEnabled[interruptNum] = false;
137 interruptModes[interruptNum] = 0;
138 interruptCallbacks[interruptNum] = NULL;
139
140 // disable lgpio alert callback
141 lgGpioFree(_gpioHandle, interruptNum);
142 lgGpioSetAlertsFunc(_gpioHandle, interruptNum, NULL, NULL);
143 }
144
145 void delay(unsigned long ms) override {
146 if(ms == 0) {
147 sched_yield();
148 return;
149 }
150
151 lguSleep(ms / 1000.0);
152 }
153
154 void delayMicroseconds(unsigned long us) override {
155 if(us == 0) {
156 sched_yield();
157 return;
158 }
159
160 lguSleep(us / 1000000.0);
161 }
162
163 void yield() override {
164 sched_yield();
165 }
166
167 unsigned long millis() override {
168 uint32_t time = lguTimestamp() / 1000000UL;
169 return time;
170 }
171
172 unsigned long micros() override {
173 uint32_t time = lguTimestamp() / 1000UL;
174 return time;
175 }
176
177 long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override {
178 if(pin == RADIOLIB_NC) {
179 return(0);
180 }
181
182 this->pinMode(pin, PI_INPUT);
183 uint32_t start = this->micros();
184 uint32_t curtick = this->micros();
185
186 while(this->digitalRead(pin) == state) {
187 if((this->micros() - curtick) > timeout) {
188 return(0);
189 }
190 }
191
192 return(this->micros() - start);
193 }
194
195 void spiBegin() {
196 if(_spiHandle < 0) {
197 if((_spiHandle = lgSpiOpen(_spiDevice, _spiChannel, _spiSpeed, 0)) < 0) {
198 fprintf(stderr, "Could not open SPI handle on 0: %s\n", lguErrorText(_spiHandle));
199 }
200 }
201 }
202
204
205 void spiTransfer(uint8_t* out, size_t len, uint8_t* in) {
206 int result = lgSpiXfer(_spiHandle, (char *)out, (char*)in, len);
207 if(result < 0) {
208 fprintf(stderr, "Could not perform SPI transfer: %s\n", lguErrorText(result));
209 }
210 }
211
213
214 void spiEnd() {
215 if(_spiHandle >= 0) {
216 lgSpiClose(_spiHandle);
217 _spiHandle = -1;
218 }
219 }
220
221 void tone(uint32_t pin, unsigned int frequency, unsigned long duration = 0) {
222 lgTxPwm(_gpioHandle, pin, frequency, 50, 0, duration);
223 }
224
225 void noTone(uint32_t pin) {
226 lgTxPwm(_gpioHandle, pin, 0, 0, 0, 0);
227 }
228
229 // interrupt emulation
230 bool interruptEnabled[PI_MAX_USER_GPIO + 1];
231 uint32_t interruptModes[PI_MAX_USER_GPIO + 1];
232 typedef void (*RadioLibISR)(void);
233 RadioLibISR interruptCallbacks[PI_MAX_USER_GPIO + 1];
234
235 private:
236 // the HAL can contain any additional private members
237 const uint8_t _gpioDevice;
238 const uint8_t _spiDevice;
239 const unsigned int _spiSpeed;
240 const uint8_t _spiChannel;
241 int _gpioHandle = -1;
242 int _spiHandle = -1;
243};
244
245// this handler emulates interrupts
246static void lgpioAlertHandler(int num_alerts, lgGpioAlert_p alerts, void *userdata) {
247 if(!userdata)
248 return;
249
250 // PiHal instance is passed via the user data
251 PiHal* hal = (PiHal*)userdata;
252
253 // check the interrupt is enabled, the level matches and a callback exists
254 for(lgGpioAlert_t *alert = alerts; alert < (alerts + num_alerts); alert++) {
255 if((hal->interruptEnabled[alert->report.gpio]) &&
256 (hal->interruptModes[alert->report.gpio] == alert->report.level) &&
257 (hal->interruptCallbacks[alert->report.gpio])) {
258 hal->interruptCallbacks[alert->report.gpio]();
259 }
260 }
261}
262
263#endif
Definition PiHal.h:23
void noTone(uint32_t pin)
Method to stop producing a tone.
Definition PiHal.h:225
unsigned long millis() override
Get number of milliseconds since start. Must be implemented by the platform-specific hardware abstrac...
Definition PiHal.h:167
void digitalWrite(uint32_t pin, uint32_t value) override
Digital write method. Must be implemented by the platform-specific hardware abstraction!
Definition PiHal.h:85
long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override
Measure the length of incoming digital pulse in microseconds. Must be implemented by the platform-spe...
Definition PiHal.h:177
void spiBegin()
SPI initialization method.
Definition PiHal.h:195
void spiEnd()
SPI termination method.
Definition PiHal.h:214
void init() override
Module initialization method. This will be called by all radio modules at the beginning of startup....
Definition PiHal.h:34
void spiEndTransaction()
Method to end SPI transaction.
Definition PiHal.h:212
void tone(uint32_t pin, unsigned int frequency, unsigned long duration=0)
Method to produce a square-wave with 50% duty cycle ("tone") of a given frequency at some pin.
Definition PiHal.h:221
void spiTransfer(uint8_t *out, size_t len, uint8_t *in)
Method to transfer buffer over SPI.
Definition PiHal.h:205
unsigned long micros() override
Get number of microseconds since start. Must be implemented by the platform-specific hardware abstrac...
Definition PiHal.h:172
void spiBeginTransaction()
Method to start SPI transaction.
Definition PiHal.h:203
uint32_t digitalRead(uint32_t pin) override
Digital read method. Must be implemented by the platform-specific hardware abstraction!
Definition PiHal.h:96
void delayMicroseconds(unsigned long us) override
Blocking microsecond wait function. Must be implemented by the platform-specific hardware abstraction...
Definition PiHal.h:154
void attachInterrupt(uint32_t interruptNum, void(*interruptCb)(void), uint32_t mode) override
Method to attach function to an external interrupt. Must be implemented by the platform-specific hard...
Definition PiHal.h:108
void pinMode(uint32_t pin, uint32_t mode) override
GPIO pin mode (input/output/...) configuration method. Must be implemented by the platform-specific h...
Definition PiHal.h:60
void delay(unsigned long ms) override
Blocking wait function. Must be implemented by the platform-specific hardware abstraction!
Definition PiHal.h:145
void term() override
Module termination method. This will be called by all radio modules when the destructor is called....
Definition PiHal.h:49
void yield() override
Yield method, called from long loops in multi-threaded environment (to prevent blocking other threads...
Definition PiHal.h:163
void detachInterrupt(uint32_t interruptNum) override
Method to detach function from an external interrupt. Must be implemented by the platform-specific ha...
Definition PiHal.h:130
Hardware abstraction library base interface.
Definition Hal.h:16
const uint32_t GpioInterruptFalling
Value to be used as the "falling" GPIO level change direction.
Definition Hal.h:50