@@ -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)
0 commit comments