Skip to content

Commit 103da28

Browse files
committed
Added libgpiod v2 compatibility so the SPI path builds against newer headers while keeping the v1 path intact. This replaces the v1 gpiod_line usage with the v2 line-request API when available and keeps the original code for older libgpiod.
1 parent cb96927 commit 103da28

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/ZeDMDSpi.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,62 @@ bool ZeDMDSpi::Connect()
106106
return false;
107107
}
108108

109+
#if defined(ZEDMD_GPIOD_API_V2)
110+
gpiod_line_settings* lineSettings = gpiod_line_settings_new();
111+
if (!lineSettings)
112+
{
113+
Log("ZeDMDSpi: couldn't allocate GPIO line settings: %s", strerror(errno));
114+
Disconnect();
115+
return false;
116+
}
117+
gpiod_line_settings_set_direction(lineSettings, GPIOD_LINE_DIRECTION_OUTPUT);
118+
gpiod_line_settings_set_output_value(lineSettings, GPIOD_LINE_VALUE_ACTIVE);
119+
120+
gpiod_line_config* lineConfig = gpiod_line_config_new();
121+
if (!lineConfig)
122+
{
123+
Log("ZeDMDSpi: couldn't allocate GPIO line config: %s", strerror(errno));
124+
gpiod_line_settings_free(lineSettings);
125+
Disconnect();
126+
return false;
127+
}
128+
if (gpiod_line_config_add_line_settings(lineConfig, &kCsGpio, 1, lineSettings) < 0)
129+
{
130+
Log("ZeDMDSpi: couldn't configure CS gpio %d: %s", kCsGpio, strerror(errno));
131+
gpiod_line_config_free(lineConfig);
132+
gpiod_line_settings_free(lineSettings);
133+
Disconnect();
134+
return false;
135+
}
136+
137+
gpiod_request_config* requestConfig = gpiod_request_config_new();
138+
if (!requestConfig)
139+
{
140+
Log("ZeDMDSpi: couldn't allocate GPIO request config: %s", strerror(errno));
141+
gpiod_line_config_free(lineConfig);
142+
gpiod_line_settings_free(lineSettings);
143+
Disconnect();
144+
return false;
145+
}
146+
gpiod_request_config_set_consumer(requestConfig, kGpioConsumer);
147+
148+
m_csLine = gpiod_chip_request_lines(m_gpioChip, requestConfig, lineConfig);
149+
gpiod_request_config_free(requestConfig);
150+
gpiod_line_config_free(lineConfig);
151+
gpiod_line_settings_free(lineSettings);
152+
if (!m_csLine)
153+
{
154+
Log("ZeDMDSpi: couldn't request CS gpio %d: %s", kCsGpio, strerror(errno));
155+
Disconnect();
156+
return false;
157+
}
158+
159+
// Create a rising edge to switch ZeDMD from loopback to SPI mode.
160+
// Keep CS high when idle.
161+
gpiod_line_request_set_value(m_csLine, kCsGpio, GPIOD_LINE_VALUE_INACTIVE);
162+
std::this_thread::sleep_for(std::chrono::microseconds(100));
163+
gpiod_line_request_set_value(m_csLine, kCsGpio, GPIOD_LINE_VALUE_ACTIVE);
164+
#else
109165
m_csLine = gpiod_chip_get_line(m_gpioChip, kCsGpio);
110166
if (!m_csLine)
111167
{
@@ -126,6 +182,7 @@ bool ZeDMDSpi::Connect()
126182
gpiod_line_set_value(m_csLine, 0);
127183
std::this_thread::sleep_for(std::chrono::microseconds(100));
128184
gpiod_line_set_value(m_csLine, 1);
185+
#endif
129186

130187
Log("ZeDMDSpi: signaling via GPIO %d established", kCsGpio);
131188

@@ -137,8 +194,13 @@ void ZeDMDSpi::Disconnect()
137194
{
138195
if (m_csLine)
139196
{
197+
#if defined(ZEDMD_GPIOD_API_V2)
198+
gpiod_line_request_set_value(m_csLine, kCsGpio, GPIOD_LINE_VALUE_ACTIVE);
199+
gpiod_line_request_release(m_csLine);
200+
#else
140201
gpiod_line_set_value(m_csLine, 1);
141202
gpiod_line_release(m_csLine);
203+
#endif
142204
m_csLine = nullptr;
143205
}
144206
if (m_gpioChip)

src/ZeDMDSpi.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include <gpiod.h>
1313
#include <linux/spi/spidev.h>
1414

15+
#if (defined(GPIOD_API_VERSION) && (GPIOD_API_VERSION >= 2)) || (defined(GPIOD_VERSION_MAJOR) && (GPIOD_VERSION_MAJOR >= 2))
16+
#define ZEDMD_GPIOD_API_V2 1
17+
#endif
18+
1519
#define GPIO_CHIP "/dev/gpiochip0"
1620
#define SPI_DEVICE "/dev/spidev1.0"
1721
#else
@@ -54,7 +58,11 @@ class ZeDMDSpi : public ZeDMDComm
5458
int m_fileDescriptor = -1;
5559
#if defined(SPI_SUPPORT)
5660
gpiod_chip* m_gpioChip = nullptr;
61+
#if defined(ZEDMD_GPIOD_API_V2)
62+
gpiod_line_request* m_csLine = nullptr;
63+
#else
5764
gpiod_line* m_csLine = nullptr;
65+
#endif
5866
#endif
5967
bool m_connected = false;
6068
};

0 commit comments

Comments
 (0)