Skip to content

Commit

Permalink
Support for ST7789 based (SPI) displays (#652)
Browse files Browse the repository at this point in the history
* Initial build with basic structure to support st7789device once added to circle.

* Implementation of ST7789 display - requires updated circle with new ST7789 character driver.

* Added more details to minidexed.ini about ST7789 and SPI options.

* Update to develop branch of circle that now supports st7789 character mode display.

* Minor formatting fixes to tidy up.

* Allow setting of more advanced SPI parameters: mode and clock.

* Update to allow for font size as an option.
  • Loading branch information
diyelectromusic authored May 9, 2024
1 parent 4fa9e16 commit d08280b
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 14 deletions.
73 changes: 73 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ void CConfig::Load (void)
m_bSSD1306LCDRotate = m_Properties.GetNumber ("SSD1306LCDRotate", 0) != 0;
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);
m_nST7789Reset = m_Properties.GetNumber ("ST7789Reset", 0); // optional
m_nST7789Backlight = m_Properties.GetNumber ("ST7789Backlight", 0); // optional
m_nST7789Width = m_Properties.GetNumber ("ST7789Width", 240);
m_nST7789Height = m_Properties.GetNumber ("ST7789Height", 240);
m_nST7789Rotation = m_Properties.GetNumber ("ST7789Rotation", 0);
m_bST7789SmallFont = m_Properties.GetNumber ("ST7789SmallFont", 0) != 0;

m_nLCDColumns = m_Properties.GetNumber ("LCDColumns", 16);
m_nLCDRows = m_Properties.GetNumber ("LCDRows", 2);

Expand Down Expand Up @@ -299,6 +313,65 @@ bool CConfig::GetSSD1306LCDMirror (void) const
return m_bSSD1306LCDMirror;
}

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;
}

unsigned CConfig::GetST7789Data (void) const
{
return m_nST7789Data;
}

unsigned CConfig::GetST7789Select (void) const
{
return m_nST7789Select;
}

unsigned CConfig::GetST7789Reset (void) const
{
return m_nST7789Reset;
}

unsigned CConfig::GetST7789Backlight (void) const
{
return m_nST7789Backlight;
}

unsigned CConfig::GetST7789Width (void) const
{
return m_nST7789Width;
}

unsigned CConfig::GetST7789Height (void) const
{
return m_nST7789Height;
}

unsigned CConfig::GetST7789Rotation (void) const
{
return m_nST7789Rotation;
}

bool CConfig::GetST7789SmallFont (void) const
{
return m_bST7789SmallFont;
}
unsigned CConfig::GetLCDColumns (void) const
{
return m_nLCDColumns;
Expand Down
36 changes: 35 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include <circle/sysconfig.h>
#include <string>

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

class CConfig // Configuration for MiniDexed
{
public:
Expand Down Expand Up @@ -103,6 +107,22 @@ class CConfig // Configuration for MiniDexed
bool GetSSD1306LCDRotate (void) const;
bool GetSSD1306LCDMirror (void) const;

// 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;
unsigned GetST7789Reset (void) const;
unsigned GetST7789Backlight (void) const;
unsigned GetST7789Width (void) const;
unsigned GetST7789Height (void) const;
unsigned GetST7789Rotation (void) const;
bool GetST7789SmallFont (void) const;

unsigned GetLCDColumns (void) const;
unsigned GetLCDRows (void) const;

Expand Down Expand Up @@ -204,7 +224,21 @@ class CConfig // Configuration for MiniDexed
unsigned m_nSSD1306LCDHeight;
bool m_bSSD1306LCDRotate;
bool m_bSSD1306LCDMirror;


unsigned m_nSPIBus;
unsigned m_nSPIMode;
unsigned m_nSPIClockKHz;

bool m_bST7789Enabled;
unsigned m_nST7789Data;
unsigned m_nST7789Select;
unsigned m_nST7789Reset;
unsigned m_nST7789Backlight;
unsigned m_nST7789Width;
unsigned m_nST7789Height;
unsigned m_nST7789Rotation;
unsigned m_bST7789SmallFont;

unsigned m_nLCDColumns;
unsigned m_nLCDRows;

Expand Down
29 changes: 28 additions & 1 deletion src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ CKernel::CKernel (void)
m_Config (&mFileSystem),
m_GPIOManager (&mInterrupt),
m_I2CMaster (CMachineInfo::Get ()->GetDevice (DeviceI2CMaster), TRUE),
m_pSPIMaster (nullptr),
m_pDexed (0)
{
s_pThis = this;
Expand Down Expand Up @@ -66,6 +67,32 @@ 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
// devices that use the 40-pin header, but that isn't
// enabled at present...
if (nSPIMaster == 0)
#else
// RPI 4+ has several possible SPI Bus Configurations.
// As mentioned above, SPI 1 is not built by default.
// See circle/include/circle/spimaster.h
if (nSPIMaster == 0 || nSPIMaster == 3 || nSPIMaster == 4 || nSPIMaster == 5 || nSPIMaster == 6)
#endif
{
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);
m_pSPIMaster = nullptr;
}
}

if (m_Config.GetUSBGadgetMode())
{
#if RASPPI==5
Expand All @@ -86,7 +113,7 @@ bool CKernel::Initialize (void)
return FALSE;
}

m_pDexed = new CMiniDexed (&m_Config, &mInterrupt, &m_GPIOManager, &m_I2CMaster,
m_pDexed = new CMiniDexed (&m_Config, &mInterrupt, &m_GPIOManager, &m_I2CMaster, m_pSPIMaster,
&mFileSystem);
assert (m_pDexed);

Expand Down
2 changes: 2 additions & 0 deletions src/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <circle/cputhrottle.h>
#include <circle/gpiomanager.h>
#include <circle/i2cmaster.h>
#include <circle/spimaster.h>
#include <circle/usb/usbcontroller.h>
#include "config.h"
#include "minidexed.h"
Expand Down Expand Up @@ -54,6 +55,7 @@ class CKernel : public CStdlibAppStdio
CCPUThrottle m_CPUThrottle;
CGPIOManager m_GPIOManager;
CI2CMaster m_I2CMaster;
CSPIMaster *m_pSPIMaster;
CMiniDexed *m_pDexed;
CUSBController *m_pUSB;

Expand Down
4 changes: 2 additions & 2 deletions src/minidexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
LOGMODULE ("minidexed");

CMiniDexed::CMiniDexed (CConfig *pConfig, CInterruptSystem *pInterrupt,
CGPIOManager *pGPIOManager, CI2CMaster *pI2CMaster, FATFS *pFileSystem)
CGPIOManager *pGPIOManager, CI2CMaster *pI2CMaster, CSPIMaster *pSPIMaster, FATFS *pFileSystem)
:
#ifdef ARM_ALLOW_MULTI_CORE
CMultiCoreSupport (CMemorySystem::Get ()),
#endif
m_pConfig (pConfig),
m_UI (this, pGPIOManager, pI2CMaster, pConfig),
m_UI (this, pGPIOManager, pI2CMaster, pSPIMaster, pConfig),
m_PerformanceConfig (pFileSystem),
m_PCKeyboard (this, pConfig, &m_UI),
m_SerialMIDI (this, pInterrupt, pConfig, &m_UI),
Expand Down
3 changes: 2 additions & 1 deletion src/minidexed.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <circle/interrupt.h>
#include <circle/gpiomanager.h>
#include <circle/i2cmaster.h>
#include <circle/spimaster.h>
#include <circle/multicore.h>
#include <circle/sound/soundbasedevice.h>
#include <circle/spinlock.h>
Expand All @@ -51,7 +52,7 @@ class CMiniDexed
{
public:
CMiniDexed (CConfig *pConfig, CInterruptSystem *pInterrupt,
CGPIOManager *pGPIOManager, CI2CMaster *pI2CMaster, FATFS *pFileSystem);
CGPIOManager *pGPIOManager, CI2CMaster *pI2CMaster, CSPIMaster *pSPIMaster, FATFS *pFileSystem);

bool Initialize (void);

Expand Down
21 changes: 21 additions & 0 deletions src/minidexed.ini
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ SSD1306LCDHeight=32
SSD1306LCDRotate=0
SSD1306LCDMirror=0

# ST7789 LCD
# SPIBus=0 for any RPi (GPIO 10,11,8,7).
# Note: Leave blank (default) if no SPI device required.
# Select=0|1 for CE0 or CE1
# Data = GPIO pin number
# Optional: Reset, Backlight = GPIO pin numbers
# Rotation=0,90,180,270
# SmallFont=0 (default), 1
#
# For a 240 wide display set LCDColumns=15 with LCDRows=2
SPIBus=
ST7789Enabled=0
ST7789Data=
ST7789Select=
ST7789Reset=
ST7789Backlight=
ST7789Width=240
ST7789Height=240
ST7789Rotation=0
ST7789SmallFont=0

# Default is 16x2 display (e.g. HD44780)
LCDColumns=16
LCDRows=2
Expand Down
Loading

0 comments on commit d08280b

Please sign in to comment.