From b087ed5502892de138cb5ea6624235e4fb678cff Mon Sep 17 00:00:00 2001 From: Wouter van Gulik Date: Fri, 6 Oct 2023 14:02:34 +0200 Subject: [PATCH 1/3] st7565.c: Move wait and data write to a single function. This makes the code much easier to follow and understand. --- driver/st7565.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/driver/st7565.c b/driver/st7565.c index b34a555..2aa7cf0 100644 --- a/driver/st7565.c +++ b/driver/st7565.c @@ -32,6 +32,13 @@ uint8_t g_frame_buffer[7][128]; uint8_t contrast = 31; // 0 ~ 63 #endif +static inline void ST7565_LowLevelWrite(uint8_t Value) +{ + /* Wait for space in the fifo */ + while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} + SPI0->WDR = Value; +} + void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const unsigned int Size, const uint8_t *pBitmap) { unsigned int i; @@ -46,16 +53,14 @@ void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const u { for (i = 0; i < Size; i++) { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = pBitmap[i]; + ST7565_LowLevelWrite(pBitmap[i]); } } else { for (i = 0; i < Size; i++) { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = 0; + ST7565_LowLevelWrite(0); } } @@ -83,8 +88,7 @@ void ST7565_BlitFullScreen(void) GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); for (Column = 0; Column < ARRAY_SIZE(g_frame_buffer[0]); Column++) { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = g_frame_buffer[Line][Column]; + ST7565_LowLevelWrite(g_frame_buffer[Line][Column]); } SPI_WaitForUndocumentedTxFifoStatusBit(); } @@ -114,8 +118,7 @@ void ST7565_BlitStatusLine(void) for (i = 0; i < ARRAY_SIZE(g_status_line); i++) { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = g_status_line[i]; + ST7565_LowLevelWrite(g_status_line[i]); } SPI_WaitForUndocumentedTxFifoStatusBit(); @@ -140,8 +143,7 @@ void ST7565_FillScreen(const uint8_t Value) GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); for (j = 0; j < 132; j++) { - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = Value; + ST7565_LowLevelWrite(Value); } SPI_WaitForUndocumentedTxFifoStatusBit(); } @@ -226,20 +228,16 @@ void ST7565_HardwareReset(void) void ST7565_SelectColumnAndLine(const uint8_t Column, const uint8_t Line) { GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = Line + 176; - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = ((Column >> 4) & 0x0F) | 0x10; - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = ((Column >> 0) & 0x0F); + ST7565_LowLevelWrite(Line + 176); + ST7565_LowLevelWrite(((Column >> 4) & 0x0F) | 0x10); + ST7565_LowLevelWrite((Column >> 0) & 0x0F); SPI_WaitForUndocumentedTxFifoStatusBit(); } void ST7565_WriteByte(const uint8_t Value) { GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {} - SPI0->WDR = Value; + ST7565_LowLevelWrite(Value); } #ifdef ENABLE_CONTRAST From 101297b3a0f4fab7e1aa02082727abd4821f44ed Mon Sep 17 00:00:00 2001 From: Wouter van Gulik Date: Wed, 11 Oct 2023 08:56:24 +0200 Subject: [PATCH 2/3] st7565: Make ST7565_WriteByte static aka local to file --- driver/st7565.c | 4 +++- driver/st7565.h | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/driver/st7565.c b/driver/st7565.c index 2aa7cf0..ccdce14 100644 --- a/driver/st7565.c +++ b/driver/st7565.c @@ -32,6 +32,8 @@ uint8_t g_frame_buffer[7][128]; uint8_t contrast = 31; // 0 ~ 63 #endif +static void ST7565_WriteByte(uint8_t Value); + static inline void ST7565_LowLevelWrite(uint8_t Value) { /* Wait for space in the fifo */ @@ -234,7 +236,7 @@ void ST7565_SelectColumnAndLine(const uint8_t Column, const uint8_t Line) SPI_WaitForUndocumentedTxFifoStatusBit(); } -void ST7565_WriteByte(const uint8_t Value) +static void ST7565_WriteByte(uint8_t Value) { GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); ST7565_LowLevelWrite(Value); diff --git a/driver/st7565.h b/driver/st7565.h index 467bce8..38de4cb 100644 --- a/driver/st7565.h +++ b/driver/st7565.h @@ -33,11 +33,9 @@ void ST7565_FillScreen(const uint8_t Value); void ST7565_Init(const bool full); void ST7565_HardwareReset(void); void ST7565_SelectColumnAndLine(const uint8_t Column, const uint8_t Line); -void ST7565_WriteByte(const uint8_t Value); #ifdef ENABLE_CONTRAST void ST7565_SetContrast(const uint8_t value); uint8_t ST7565_GetContrast(void); #endif - #endif From 3e534cee183b81bd3f487c08ab4af8e3f8b546de Mon Sep 17 00:00:00 2001 From: Wouter van Gulik Date: Wed, 11 Oct 2023 08:56:46 +0200 Subject: [PATCH 3/3] st7565: Use | 0xB0 instead of +176 --- driver/st7565.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver/st7565.c b/driver/st7565.c index ccdce14..9863dc5 100644 --- a/driver/st7565.c +++ b/driver/st7565.c @@ -230,7 +230,7 @@ void ST7565_HardwareReset(void) void ST7565_SelectColumnAndLine(const uint8_t Column, const uint8_t Line) { GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0); - ST7565_LowLevelWrite(Line + 176); + ST7565_LowLevelWrite(Line + 0xB0); ST7565_LowLevelWrite(((Column >> 4) & 0x0F) | 0x10); ST7565_LowLevelWrite((Column >> 0) & 0x0F); SPI_WaitForUndocumentedTxFifoStatusBit();