@@ -18,6 +18,8 @@ namespace
1818{
1919constexpr uint8_t kSpiBitsPerWord = 8 ;
2020constexpr uint8_t kSpiMode = SPI_MODE_0;
21+ constexpr unsigned int kCsGpio = 25 ; // GPIO25 on Raspberry Pi
22+ constexpr const char kGpioConsumer [] = " ZeDMDSpi" ;
2123constexpr const char kSpiBufSizePath [] = " /sys/module/spidev/parameters/bufsiz" ;
2224
2325uint32_t GetSpiKernelBufSize ()
@@ -96,18 +98,54 @@ bool ZeDMDSpi::Connect()
9698 }
9799 Log (" ZeDMDSpi: set SPI speed %d" , m_speed);
98100
99- m_connected = true ;
100- // Create a short CS signal to switch ZeDMD from loopback to SPI mode.
101- const uint8_t dummy[4 ] = {0 };
102- if (!SendChunks (dummy, 4 )) {
103- m_connected = false ;
101+ m_gpioChip = gpiod_chip_open (GPIO_CHIP);
102+ if (!m_gpioChip)
103+ {
104+ Log (" ZeDMDSpi: couldn't open gpio chip %s: %s" , GPIO_CHIP, strerror (errno));
105+ Disconnect ();
106+ return false ;
107+ }
108+
109+ m_csLine = gpiod_chip_get_line (m_gpioChip, kCsGpio );
110+ if (!m_csLine)
111+ {
112+ Log (" ZeDMDSpi: couldn't get CS gpio %d: %s" , kCsGpio , strerror (errno));
113+ Disconnect ();
114+ return false ;
115+ }
116+
117+ if (gpiod_line_request_output (m_csLine, kGpioConsumer , 1 ) < 0 )
118+ {
119+ Log (" ZeDMDSpi: couldn't request CS gpio %d as output: %s" , kCsGpio , strerror (errno));
120+ Disconnect ();
121+ return false ;
104122 }
105123
106- return m_connected;
124+ // Create a rising edge to switch ZeDMD from loopback to SPI mode.
125+ // Keep CS high when idle.
126+ gpiod_line_set_value (m_csLine, 0 );
127+ std::this_thread::sleep_for (std::chrono::microseconds (100 ));
128+ gpiod_line_set_value (m_csLine, 1 );
129+
130+ Log (" ZeDMDSpi: signaling via GPIO %d established" , kCsGpio );
131+
132+ m_connected = true ;
133+ return true ;
107134}
108135
109136void ZeDMDSpi::Disconnect ()
110137{
138+ if (m_csLine)
139+ {
140+ gpiod_line_set_value (m_csLine, 1 );
141+ gpiod_line_release (m_csLine);
142+ m_csLine = nullptr ;
143+ }
144+ if (m_gpioChip)
145+ {
146+ gpiod_chip_close (m_gpioChip);
147+ m_gpioChip = nullptr ;
148+ }
111149 if (m_fileDescriptor >= 0 )
112150 {
113151 close (m_fileDescriptor);
0 commit comments