Skip to content

Commit e2a8017

Browse files
committed
added signaling
1 parent 1d930c3 commit e2a8017

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

src/ZeDMDSpi.cpp

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace
1818
{
1919
constexpr uint8_t kSpiBitsPerWord = 8;
2020
constexpr uint8_t kSpiMode = SPI_MODE_0;
21+
constexpr unsigned int kCsGpio = 25; // GPIO25 on Raspberry Pi
22+
constexpr const char kGpioConsumer[] = "ZeDMDSpi";
2123
constexpr const char kSpiBufSizePath[] = "/sys/module/spidev/parameters/bufsiz";
2224

2325
uint32_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

109136
void 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);

src/ZeDMDSpi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ZeDMDSpi : public ZeDMDComm
4444
bool IsSupportedPlatform() const;
4545

4646
uint32_t m_speed = 72000000; // 72MHz
47-
uint8_t m_framePause = 2; // 2ms
47+
uint8_t m_framePause = 0; // 0ms
4848
int m_fileDescriptor = -1;
4949
bool m_connected = false;
5050
};

0 commit comments

Comments
 (0)