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  // first initialise lgpio library
36  if((_gpioHandle = lgGpiochipOpen(_gpioDevice)) < 0) {
37  fprintf(stderr, "Could not open GPIO chip: %s\n", lguErrorText(_gpioHandle));
38  return;
39  }
40 
41  // now the SPI
42  spiBegin();
43  }
44 
45  void term() override {
46  // stop the SPI
47  spiEnd();
48 
49  // finally, stop the lgpio library
50  lgGpiochipClose(_gpioHandle);
51  }
52 
53  // GPIO-related methods (pinMode, digitalWrite etc.) should check
54  // RADIOLIB_NC as an alias for non-connected pins
55  void pinMode(uint32_t pin, uint32_t mode) override {
56  if(pin == RADIOLIB_NC) {
57  return;
58  }
59 
60  int result;
61  int flags = 0;
62  switch(mode) {
63  case PI_INPUT:
64  result = lgGpioClaimInput(_gpioHandle, 0, pin);
65  break;
66  case PI_OUTPUT:
67  result = lgGpioClaimOutput(_gpioHandle, flags, pin, LG_HIGH);
68  break;
69  default:
70  fprintf(stderr, "Unknown pinMode mode %" PRIu32 "\n", mode);
71  return;
72  }
73 
74  if(result < 0) {
75  fprintf(stderr, "Could not claim pin %" PRIu32 " for mode %" PRIu32 ": %s\n",
76  pin, mode, lguErrorText(result));
77  }
78  }
79 
80  void digitalWrite(uint32_t pin, uint32_t value) override {
81  if(pin == RADIOLIB_NC) {
82  return;
83  }
84 
85  int result = lgGpioWrite(_gpioHandle, pin, value);
86  if(result < 0) {
87  fprintf(stderr, "Error writing value to pin %" PRIu32 ": %s\n", pin, lguErrorText(result));
88  }
89  }
90 
91  uint32_t digitalRead(uint32_t pin) override {
92  if(pin == RADIOLIB_NC) {
93  return(0);
94  }
95 
96  int result = lgGpioRead(_gpioHandle, pin);
97  if(result < 0) {
98  fprintf(stderr, "Error writing reading from pin %" PRIu32 ": %s\n", pin, lguErrorText(result));
99  }
100  return result;
101  }
102 
103  void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) override {
104  if((interruptNum == RADIOLIB_NC) || (interruptNum > PI_MAX_USER_GPIO)) {
105  return;
106  }
107 
108  // set lgpio alert callback
109  int result = lgGpioClaimAlert(_gpioHandle, 0, mode, interruptNum, -1);
110  if(result < 0) {
111  fprintf(stderr, "Could not claim pin %" PRIu32 " for alert: %s\n", interruptNum, lguErrorText(result));
112  return;
113  }
114 
115  // enable emulated interrupt
116  interruptEnabled[interruptNum] = true;
117  interruptModes[interruptNum] = mode;
118  interruptCallbacks[interruptNum] = interruptCb;
119 
120  lgGpioSetAlertsFunc(_gpioHandle, interruptNum, lgpioAlertHandler, (void *)this);
121  }
122 
123  void detachInterrupt(uint32_t interruptNum) override {
124  if((interruptNum == RADIOLIB_NC) || (interruptNum > PI_MAX_USER_GPIO)) {
125  return;
126  }
127 
128  // clear emulated interrupt
129  interruptEnabled[interruptNum] = false;
130  interruptModes[interruptNum] = 0;
131  interruptCallbacks[interruptNum] = NULL;
132 
133  // disable lgpio alert callback
134  lgGpioFree(_gpioHandle, interruptNum);
135  lgGpioSetAlertsFunc(_gpioHandle, interruptNum, NULL, NULL);
136  }
137 
138  void delay(unsigned long ms) override {
139  if(ms == 0) {
140  sched_yield();
141  return;
142  }
143 
144  lguSleep(ms / 1000.0);
145  }
146 
147  void delayMicroseconds(unsigned long us) override {
148  if(us == 0) {
149  sched_yield();
150  return;
151  }
152 
153  lguSleep(us / 1000000.0);
154  }
155 
156  void yield() override {
157  sched_yield();
158  }
159 
160  unsigned long millis() override {
161  uint32_t time = lguTimestamp() / 1000000UL;
162  return time;
163  }
164 
165  unsigned long micros() override {
166  uint32_t time = lguTimestamp() / 1000UL;
167  return time;
168  }
169 
170  long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override {
171  if(pin == RADIOLIB_NC) {
172  return(0);
173  }
174 
175  this->pinMode(pin, PI_INPUT);
176  uint32_t start = this->micros();
177  uint32_t curtick = this->micros();
178 
179  while(this->digitalRead(pin) == state) {
180  if((this->micros() - curtick) > timeout) {
181  return(0);
182  }
183  }
184 
185  return(this->micros() - start);
186  }
187 
188  void spiBegin() {
189  if(_spiHandle < 0) {
190  if((_spiHandle = lgSpiOpen(_spiDevice, _spiChannel, _spiSpeed, 0)) < 0) {
191  fprintf(stderr, "Could not open SPI handle on 0: %s\n", lguErrorText(_spiHandle));
192  }
193  }
194  }
195 
197 
198  void spiTransfer(uint8_t* out, size_t len, uint8_t* in) {
199  int result = lgSpiXfer(_spiHandle, (char *)out, (char*)in, len);
200  if(result < 0) {
201  fprintf(stderr, "Could not perform SPI transfer: %s\n", lguErrorText(result));
202  }
203  }
204 
206 
207  void spiEnd() {
208  if(_spiHandle >= 0) {
209  lgSpiClose(_spiHandle);
210  _spiHandle = -1;
211  }
212  }
213 
214  void tone(uint32_t pin, unsigned int frequency, unsigned long duration = 0) {
215  lgTxPwm(_gpioHandle, pin, frequency, 50, 0, duration);
216  }
217 
218  void noTone(uint32_t pin) {
219  lgTxPwm(_gpioHandle, pin, 0, 0, 0, 0);
220  }
221 
222  // interrupt emulation
223  bool interruptEnabled[PI_MAX_USER_GPIO + 1];
224  uint32_t interruptModes[PI_MAX_USER_GPIO + 1];
225  typedef void (*RadioLibISR)(void);
226  RadioLibISR interruptCallbacks[PI_MAX_USER_GPIO + 1];
227 
228  private:
229  // the HAL can contain any additional private members
230  const unsigned int _spiSpeed;
231  const uint8_t _gpioDevice;
232  const uint8_t _spiDevice;
233  const uint8_t _spiChannel;
234  int _gpioHandle = -1;
235  int _spiHandle = -1;
236 };
237 
238 // this handler emulates interrupts
239 static void lgpioAlertHandler(int num_alerts, lgGpioAlert_p alerts, void *userdata) {
240  if(!userdata)
241  return;
242 
243  // PiHal instance is passed via the user data
244  PiHal* hal = (PiHal*)userdata;
245 
246  // check the interrupt is enabled, the level matches and a callback exists
247  for(lgGpioAlert_t *alert = alerts; alert < (alerts + num_alerts); alert++) {
248  if((hal->interruptEnabled[alert->report.gpio]) &&
249  (hal->interruptModes[alert->report.gpio] == alert->report.level) &&
250  (hal->interruptCallbacks[alert->report.gpio])) {
251  hal->interruptCallbacks[alert->report.gpio]();
252  }
253  }
254 }
255 
256 #endif
Definition: PiHal.h:23
void noTone(uint32_t pin)
Method to stop producing a tone.
Definition: PiHal.h:218
unsigned long millis() override
Get number of milliseconds since start. Must be implemented by the platform-specific hardware abstrac...
Definition: PiHal.h:160
void digitalWrite(uint32_t pin, uint32_t value) override
Digital write method. Must be implemented by the platform-specific hardware abstraction!
Definition: PiHal.h:80
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:170
void spiBegin()
SPI initialization method.
Definition: PiHal.h:188
void spiEnd()
SPI termination method.
Definition: PiHal.h:207
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:205
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:214
void spiTransfer(uint8_t *out, size_t len, uint8_t *in)
Method to transfer buffer over SPI.
Definition: PiHal.h:198
unsigned long micros() override
Get number of microseconds since start. Must be implemented by the platform-specific hardware abstrac...
Definition: PiHal.h:165
void spiBeginTransaction()
Method to start SPI transaction.
Definition: PiHal.h:196
uint32_t digitalRead(uint32_t pin) override
Digital read method. Must be implemented by the platform-specific hardware abstraction!
Definition: PiHal.h:91
void delayMicroseconds(unsigned long us) override
Blocking microsecond wait function. Must be implemented by the platform-specific hardware abstraction...
Definition: PiHal.h:147
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:103
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:55
void delay(unsigned long ms) override
Blocking wait function. Must be implemented by the platform-specific hardware abstraction!
Definition: PiHal.h:138
void term() override
Module termination method. This will be called by all radio modules when the destructor is called....
Definition: PiHal.h:45
void yield() override
Yield method, called from long loops in multi-threaded environment (to prevent blocking other threads...
Definition: PiHal.h:156
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:123
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