Skip to content

Commit

Permalink
Allow setting of more advanced SPI parameters: mode and clock.
Browse files Browse the repository at this point in the history
  • Loading branch information
diyelectromusic committed May 6, 2024
1 parent 120d1bd commit 425b191
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ void CConfig::Load (void)
m_bSSD1306LCDMirror = m_Properties.GetNumber ("SSD1306LCDMirror", 0) != 0;

m_nSPIBus = m_Properties.GetNumber ("SPIBus", SPI_INACTIVE); // Disabled by default
m_nSPIMode = m_Properties.GetNumber ("SPIMode", SPI_DEF_MODE);
m_nSPIClockKHz = m_Properties.GetNumber ("SPIClockKHz", SPI_DEF_CLOCK);

m_bST7789Enabled = m_Properties.GetNumber ("ST7789Enabled", 0) != 0;
m_nST7789Data = m_Properties.GetNumber ("ST7789Data", 0);
m_nST7789Select = m_Properties.GetNumber ("ST7789Select", 0);
Expand Down Expand Up @@ -314,6 +317,16 @@ unsigned CConfig::GetSPIBus (void) const
return m_nSPIBus;
}

unsigned CConfig::GetSPIMode (void) const
{
return m_nSPIMode;
}

unsigned CConfig::GetSPIClockKHz (void) const
{
return m_nSPIClockKHz;
}

bool CConfig::GetST7789Enabled (void) const
{
return m_bST7789Enabled;
Expand Down
15 changes: 10 additions & 5 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
#include <string>

#define SPI_INACTIVE 255
#define SPI_CLOCK_SPEED 15000000 // Hz
#define SPI_CPOL 0 // Taken from circle sample application
#define SPI_CPHA 0 // Apparently Mode 0 (0,0) is common...?

#define SPI_DEF_CLOCK 15000 // kHz
#define SPI_DEF_MODE 0 // Default mode (0,1,2,3)

class CConfig // Configuration for MiniDexed
{
Expand Down Expand Up @@ -109,8 +107,12 @@ class CConfig // Configuration for MiniDexed
bool GetSSD1306LCDRotate (void) const;
bool GetSSD1306LCDMirror (void) const;

// ST7789 LCD
// SPI support
unsigned GetSPIBus (void) const;
unsigned GetSPIMode (void) const;
unsigned GetSPIClockKHz (void) const;

// ST7789 LCD
bool GetST7789Enabled (void) const;
unsigned GetST7789Data (void) const;
unsigned GetST7789Select (void) const;
Expand Down Expand Up @@ -223,6 +225,9 @@ class CConfig // Configuration for MiniDexed
bool m_bSSD1306LCDMirror;

unsigned m_nSPIBus;
unsigned m_nSPIMode;
unsigned m_nSPIClockKHz;

bool m_bST7789Enabled;
unsigned m_nST7789Data;
unsigned m_nST7789Select;
Expand Down
6 changes: 5 additions & 1 deletion src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ bool CKernel::Initialize (void)
m_Config.Load ();

unsigned nSPIMaster = m_Config.GetSPIBus();
unsigned nSPIMode = m_Config.GetSPIMode();
unsigned long nSPIClock = 1000 * m_Config.GetSPIClockKHz();
#if RASPPI<4
// By default older RPI versions use SPI 0.
// It is possible to build circle to support SPI 1 for
Expand All @@ -81,7 +83,9 @@ bool CKernel::Initialize (void)
if (nSPIMaster == 0 || nSPIMaster == 3 || nSPIMaster == 4 || nSPIMaster == 5 || nSPIMaster == 6)
#endif
{
m_pSPIMaster = new CSPIMaster (SPI_CLOCK_SPEED, SPI_CPOL, SPI_CPHA, nSPIMaster);
unsigned nCPHA = (nSPIMode & 1) ? 1 : 0;
unsigned nCPOL = (nSPIMode & 2) ? 1 : 0;
m_pSPIMaster = new CSPIMaster (nSPIClock, nCPOL, nCPHA, nSPIMaster);
if (!m_pSPIMaster->Initialize())
{
delete (m_pSPIMaster);
Expand Down
7 changes: 6 additions & 1 deletion src/userinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,18 @@ bool CUserInterface::Initialize (void)
return false;
}

unsigned long nSPIClock = 1000 * m_pConfig->GetSPIClockKHz();
unsigned nSPIMode = m_pConfig->GetSPIMode();
unsigned nCPHA = (nSPIMode & 1) ? 1 : 0;
unsigned nCPOL = (nSPIMode & 2) ? 1 : 0;
LOGDBG("SPI: CPOL=%u; CPHA=%u; CLK=%u",nCPOL,nCPHA,nSPIClock);
m_pST7789Display = new CST7789Display (m_pSPIMaster,
m_pConfig->GetST7789Data(),
m_pConfig->GetST7789Reset(),
m_pConfig->GetST7789Backlight(),
m_pConfig->GetST7789Width(),
m_pConfig->GetST7789Height(),
SPI_CPOL, SPI_CPHA, SPI_CLOCK_SPEED,
nCPOL, nCPHA, nSPIClock,
m_pConfig->GetST7789Select());
if (m_pST7789Display->Initialize())
{
Expand Down

0 comments on commit 425b191

Please sign in to comment.