RadioLib
Universal wireless communication library for Arduino
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
17 static 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
23 class 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  _spiChannel(spiChannel),
31  _spiSpeed(spiSpeed) {
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  interruptModes[interruptNum] = mode;
123  interruptCallbacks[interruptNum] = interruptCb;
124 
125  lgGpioSetAlertsFunc(_gpioHandle, interruptNum, lgpioAlertHandler, (void *)this);
126  }
127 
128  void detachInterrupt(uint32_t interruptNum) override {
129  if((interruptNum == RADIOLIB_NC) || (interruptNum > PI_MAX_USER_GPIO)) {
130  return;
131  }
132 
133  // clear emulated interrupt
134  interruptEnabled[interruptNum] = false;
135  interruptModes[interruptNum] = 0;
136  interruptCallbacks[interruptNum] = NULL;
137 
138  // disable lgpio alert callback
139  lgGpioFree(_gpioHandle, interruptNum);
140  lgGpioSetAlertsFunc(_gpioHandle, interruptNum, NULL, NULL);
141  }
142 
143  void delay(unsigned long ms) override {
144  if(ms == 0) {
145  sched_yield();
146  return;
147  }
148 
149  lguSleep(ms / 1000.0);
150  }
151 
152  void delayMicroseconds(unsigned long us) override {
153  if(us == 0) {
154  sched_yield();
155  return;
156  }
157 
158  lguSleep(us / 1000000.0);
159  }
160 
161  void yield() override {
162  sched_yield();
163  }
164 
165  unsigned long millis() override {
166  uint32_t time = lguTimestamp() / 1000000UL;
167  return time;
168  }
169 
170  unsigned long micros() override {
171  uint32_t time = lguTimestamp() / 1000UL;
172  return time;
173  }
174 
175  long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override {
176  if(pin == RADIOLIB_NC) {
177  return(0);
178  }
179 
180  this->pinMode(pin, PI_INPUT);
181  uint32_t start = this->micros();
182  uint32_t curtick = this->micros();
183 
184  while(this->digitalRead(pin) == state) {
185  if((this->micros() - curtick) > timeout) {
186  return(0);
187  }
188  }
189 
190  return(this->micros() - start);
191  }
192 
193  void spiBegin() {
194  if(_spiHandle < 0) {
195  if((_spiHandle = lgSpiOpen(_spiDevice, _spiChannel, _spiSpeed, 0)) < 0) {
196  fprintf(stderr, "Could not open SPI handle on 0: %s\n", lguErrorText(_spiHandle));
197  }
198  }
199  }
200 
202 
203  void spiTransfer(uint8_t* out, size_t len, uint8_t* in) {
204  int result = lgSpiXfer(_spiHandle, (char *)out, (char*)in, len);
205  if(result < 0) {
206  fprintf(stderr, "Could not perform SPI transfer: %s\n", lguErrorText(result));
207  }
208  }
209 
211 
212  void spiEnd() {
213  if(_spiHandle >= 0) {
214  lgSpiClose(_spiHandle);
215  _spiHandle = -1;
216  }
217  }
218 
219  void tone(uint32_t pin, unsigned int frequency, unsigned long duration = 0) {
220  lgTxPwm(_gpioHandle, pin, frequency, 50, 0, duration);
221  }
222 
223  void noTone(uint32_t pin) {
224  lgTxPwm(_gpioHandle, pin, 0, 0, 0, 0);
225  }
226 
227  // interrupt emulation
228  bool interruptEnabled[PI_MAX_USER_GPIO + 1];
229  uint32_t interruptModes[PI_MAX_USER_GPIO + 1];
230  typedef void (*RadioLibISR)(void);
231  RadioLibISR interruptCallbacks[PI_MAX_USER_GPIO + 1];
232 
233  private:
234  // the HAL can contain any additional private members
235  const unsigned int _spiSpeed;
236  const uint8_t _gpioDevice;
237  const uint8_t _spiDevice;
238  const uint8_t _spiChannel;
239  int _gpioHandle = -1;
240  int _spiHandle = -1;
241 };
242 
243 // this handler emulates interrupts
244 static void lgpioAlertHandler(int num_alerts, lgGpioAlert_p alerts, void *userdata) {
245  if(!userdata)
246  return;
247 
248  // PiHal instance is passed via the user data
249  PiHal* hal = (PiHal*)userdata;
250 
251  // check the interrupt is enabled, the level matches and a callback exists
252  for(lgGpioAlert_t *alert = alerts; alert < (alerts + num_alerts); alert++) {
253  if((hal->interruptEnabled[alert->report.gpio]) &&
254  (hal->interruptModes[alert->report.gpio] == alert->report.level) &&
255  (hal->interruptCallbacks[alert->report.gpio])) {
256  hal->interruptCallbacks[alert->report.gpio]();
257  }
258  }
259 }
260 
261 #endif
Definition: PiHal.h:23
void noTone(uint32_t pin)
Method to stop producing a tone.
Definition: PiHal.h:223
unsigned long millis() override
Get number of milliseconds since start. Must be implemented by the platform-specific hardware abstrac...
Definition: PiHal.h:165
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:175
void spiBegin()
SPI initialization method.
Definition: PiHal.h:193
void spiEnd()
SPI termination method.
Definition: PiHal.h:212
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:210
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:219
void spiTransfer(uint8_t *out, size_t len, uint8_t *in)
Method to transfer buffer over SPI.
Definition: PiHal.h:203
unsigned long micros() override
Get number of microseconds since start. Must be implemented by the platform-specific hardware abstrac...
Definition: PiHal.h:170
void spiBeginTransaction()
Method to start SPI transaction.
Definition: PiHal.h:201
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:152
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:143
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:161
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:128
Hardware abstraction library base interface.
Definition: Hal.h:13
RadioLibHal(const uint32_t input, const uint32_t output, const uint32_t low, const uint32_t high, const uint32_t rising, const uint32_t falling)
Default constructor.
Definition: Hal.cpp:3