🐛 Fix SPI TFT for STM32F1/F4 (#26052)

This commit is contained in:
Alexander Gavrilenko 2024-01-08 03:18:18 +03:00 committed by GitHub
parent 2a8c00bdeb
commit 4cddc61eda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 66 deletions

View file

@ -31,6 +31,10 @@
#include "tft_spi.h" #include "tft_spi.h"
#include "pinconfig.h" #include "pinconfig.h"
//#define DEBUG_TFT_IO
#define DEBUG_OUT ENABLED(DEBUG_TFT_IO)
#include "../../../core/debug_out.h"
SPI_HandleTypeDef TFT_SPI::SPIx; SPI_HandleTypeDef TFT_SPI::SPIx;
DMA_HandleTypeDef TFT_SPI::DMAtx; DMA_HandleTypeDef TFT_SPI::DMAtx;
@ -43,8 +47,9 @@ void TFT_SPI::init() {
if ((spiInstance = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_SCK_PIN), PinMap_SPI_SCLK)) == NP) return; if ((spiInstance = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_SCK_PIN), PinMap_SPI_SCLK)) == NP) return;
if (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_MOSI_PIN), PinMap_SPI_MOSI)) return; if (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_MOSI_PIN), PinMap_SPI_MOSI)) return;
#if PIN_EXISTS(TFT_MISO) && TFT_MISO_PIN != TFT_MOSI_PIN #if PIN_EXISTS(TFT_MISO)
if (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO)) return; // Check these pins in code because they are sometimes defined as analog pin references
if ((TFT_MISO_PIN != TFT_MOSI_PIN) && (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO))) return;
#endif #endif
SPIx.Instance = spiInstance; SPIx.Instance = spiInstance;
@ -76,10 +81,13 @@ void TFT_SPI::init() {
pinmap_pinout(digitalPinToPinName(TFT_SCK_PIN), PinMap_SPI_SCLK); pinmap_pinout(digitalPinToPinName(TFT_SCK_PIN), PinMap_SPI_SCLK);
pinmap_pinout(digitalPinToPinName(TFT_MOSI_PIN), PinMap_SPI_MOSI); pinmap_pinout(digitalPinToPinName(TFT_MOSI_PIN), PinMap_SPI_MOSI);
#if PIN_EXISTS(TFT_MISO) && TFT_MISO_PIN != TFT_MOSI_PIN #if PIN_EXISTS(TFT_MISO)
pinmap_pinout(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO); // Check these pins in code because they are sometimes defined as analog pin references
if (TFT_MISO_PIN != TFT_MOSI_PIN) pinmap_pinout(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO);
#endif #endif
//pin_PullConfig(get_GPIO_Port(STM_PORT(digitalPinToPinName(TFT_SCK_PIN))), STM_LL_GPIO_PIN(digitalPinToPinName(TFT_SCK_PIN)), GPIO_PULLDOWN);
#ifdef SPI1_BASE #ifdef SPI1_BASE
if (SPIx.Instance == SPI1) { if (SPIx.Instance == SPI1) {
__HAL_RCC_SPI1_CLK_ENABLE(); __HAL_RCC_SPI1_CLK_ENABLE();
@ -151,29 +159,47 @@ void TFT_SPI::dataTransferBegin(uint16_t dataSize) {
WRITE(TFT_CS_PIN, LOW); WRITE(TFT_CS_PIN, LOW);
} }
#ifdef TFT_DEFAULT_DRIVER #include "../../../lcd/tft_io/tft_ids.h"
#include "../../../lcd/tft_io/tft_ids.h"
#endif
uint32_t TFT_SPI::getID() { uint32_t TFT_SPI::getID() {
uint32_t id; DEBUG_ECHOLNPGM("TFT_SPI::getID()");
id = readID(LCD_READ_ID);
uint32_t id = readID(LCD_READ_ID);
#if ENABLED(DEBUG_TFT_IO)
char debug_register[3], debug_value[5];
sprintf_P(debug_register, PSTR("%02X"), LCD_READ_ID);
sprintf_P(debug_value, PSTR("%04X"), uint16_t(id));
DEBUG_ECHOLNPGM(" readID(0x", debug_register, ") : 0x", debug_value);
#endif
if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) { if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) {
id = readID(LCD_READ_ID4); id = readID(LCD_READ_ID4);
#ifdef TFT_DEFAULT_DRIVER #if ENABLED(DEBUG_TFT_IO)
if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) sprintf_P(debug_register, PSTR("%02X"), LCD_READ_ID4);
id = TFT_DEFAULT_DRIVER; sprintf_P(debug_value, PSTR("%04X"), uint16_t(id));
DEBUG_ECHOLNPGM(" readID(0x", debug_register, ") : 0x", debug_value);
#endif #endif
} }
#ifdef TFT_DEFAULT_DRIVER
if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) {
id = TFT_DEFAULT_DRIVER;
#if ENABLED(DEBUG_TFT_IO)
sprintf_P(debug_value, PSTR("%04X"), uint16_t(id));
DEBUG_ECHOLNPGM(" Fallback to TFT_DEFAULT_DRIVER : 0x", debug_value);
#endif
}
#endif
return id; return id;
} }
uint32_t TFT_SPI::readID(const uint16_t inReg) { uint32_t TFT_SPI::readID(const uint16_t inReg) {
uint32_t data = 0; uint32_t data = 0;
#if PIN_EXISTS(TFT_MISO) #if PIN_EXISTS(TFT_MISO)
const uint32_t oldPrescaler = SPIx.Init.BaudRatePrescaler; uint32_t BaudRatePrescaler = SPIx.Init.BaudRatePrescaler;
SPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; SPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
dataTransferBegin(DATASIZE_8BIT); dataTransferBegin(DATASIZE_8BIT);
writeReg(inReg); writeReg(inReg);
@ -185,10 +211,8 @@ uint32_t TFT_SPI::readID(const uint16_t inReg) {
__HAL_SPI_ENABLE(&SPIx); __HAL_SPI_ENABLE(&SPIx);
SET_BIT(SPIx.Instance->CR1, SPI_CR1_CSTART); SET_BIT(SPIx.Instance->CR1, SPI_CR1_CSTART);
#if TFT_MISO_PIN != TFT_MOSI_PIN if (SPIx.Init.Direction == SPI_DIRECTION_2LINES) SPIx.Instance->TXDR = 0;
SPIx.Instance->TXDR = 0; while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_EOT)) { /* nada */ }
#endif
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_EOT)) {}
data = (data << 8) | SPIx.Instance->RXDR; data = (data << 8) | SPIx.Instance->RXDR;
__HAL_SPI_DISABLE(&SPIx); __HAL_SPI_DISABLE(&SPIx);
__HAL_SPI_CLEAR_EOTFLAG(&SPIx); __HAL_SPI_CLEAR_EOTFLAG(&SPIx);
@ -197,19 +221,22 @@ uint32_t TFT_SPI::readID(const uint16_t inReg) {
#else #else
__HAL_SPI_ENABLE(&SPIx); __HAL_SPI_ENABLE(&SPIx);
for (uint32_t i = 0; i < 4; i++) { for (uint32_t i = 0; i < 4; i++) {
#if TFT_MISO_PIN != TFT_MOSI_PIN if (SPIx.Init.Direction == SPI_DIRECTION_2LINES) {
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE)) {} while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE)) { /* nada */ }
SPIx.Instance->DR = 0; SPIx.Instance->DR = 0;
#endif }
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_RXNE)) {} while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_RXNE)) { /* nada */ }
data = (data << 8) | SPIx.Instance->DR; data = (data << 8) | SPIx.Instance->DR;
} }
#endif #endif
dataTransferEnd(); dataTransferEnd();
SPIx.Init.BaudRatePrescaler = oldPrescaler; #if DISABLED(DEBUG_TFT_IO)
SPIx.Init.BaudRatePrescaler = BaudRatePrescaler;
#endif
#endif #endif
DEBUG_ECHOLNPGM(" raw data : ", data);
return data >> 7; return data >> 7;
} }
@ -238,13 +265,13 @@ bool TFT_SPI::isBusy() {
// Check if SPI data transfer is completed // Check if SPI data transfer is completed
if (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_EOT)) return true; if (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_EOT)) return true;
#else #else
// Check if SPI is idle // Check if SPI transmit butter is empty and SPI is idle
if (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)) return true; if ((!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE)) || (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY))) return true;
#endif #endif
} }
abort(); abort();
return false; return true;
} }
void TFT_SPI::abort() { void TFT_SPI::abort() {
@ -263,9 +290,7 @@ void TFT_SPI::abort() {
} }
void TFT_SPI::transmit(uint16_t data) { void TFT_SPI::transmit(uint16_t data) {
#if TFT_MISO_PIN == TFT_MOSI_PIN if (SPIx.Init.Direction == SPI_DIRECTION_1LINE) SPI_1LINE_TX(&SPIx);
SPI_1LINE_TX(&SPIx);
#endif
#ifdef STM32H7xx #ifdef STM32H7xx
MODIFY_REG(SPIx.Instance->CR2, SPI_CR2_TSIZE, 1); MODIFY_REG(SPIx.Instance->CR2, SPI_CR2_TSIZE, 1);
@ -274,30 +299,26 @@ void TFT_SPI::transmit(uint16_t data) {
SPIx.Instance->TXDR = data; SPIx.Instance->TXDR = data;
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_SR_EOT)) {} while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_SR_EOT)) { /* nada */ }
__HAL_SPI_CLEAR_EOTFLAG(&SPIx); __HAL_SPI_CLEAR_EOTFLAG(&SPIx);
__HAL_SPI_CLEAR_TXTFFLAG(&SPIx); __HAL_SPI_CLEAR_TXTFFLAG(&SPIx);
__HAL_SPI_DISABLE(&SPIx);
#else #else
__HAL_SPI_ENABLE(&SPIx); __HAL_SPI_ENABLE(&SPIx);
SPIx.Instance->DR = data; SPIx.Instance->DR = data;
while (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)) {} while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE)) { /* nada */ } // Wait for data transfer to actually start
while ( __HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)) { /* nada */ } // Wait until SPI is idle
#endif #endif
__HAL_SPI_DISABLE(&SPIx); if (SPIx.Init.Direction == SPI_DIRECTION_2LINES) __HAL_SPI_CLEAR_OVRFLAG(&SPIx); // Clear overrun flag in 2 Lines communication mode because received data is not read
#if TFT_MISO_PIN != TFT_MOSI_PIN
__HAL_SPI_CLEAR_OVRFLAG(&SPIx); // Clear overrun flag in 2 Lines communication mode because received data is not read
#endif
} }
void TFT_SPI::transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count) { void TFT_SPI::transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count) {
DMAtx.Init.MemInc = memoryIncrease; DMAtx.Init.MemInc = memoryIncrease;
HAL_DMA_Init(&DMAtx); HAL_DMA_Init(&DMAtx);
#if TFT_MISO_PIN == TFT_MOSI_PIN if (SPIx.Init.Direction == SPI_DIRECTION_1LINE) SPI_1LINE_TX(&SPIx);
SPI_1LINE_TX(&SPIx);
#endif
dataTransferBegin(); dataTransferBegin();
@ -316,7 +337,7 @@ void TFT_SPI::transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t coun
SET_BIT(SPIx.Instance->CR2, SPI_CR2_TXDMAEN); // Enable Tx DMA Request SET_BIT(SPIx.Instance->CR2, SPI_CR2_TXDMAEN); // Enable Tx DMA Request
#endif #endif
TERN_(TFT_SHARED_IO, while (isBusy())); TERN_(TFT_SHARED_IO, while (isBusy()) { /* nada */ });
} }
void TFT_SPI::transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count) { void TFT_SPI::transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count) {
@ -324,9 +345,10 @@ void TFT_SPI::transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count)
HAL_DMA_PollForTransfer(&DMAtx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY); HAL_DMA_PollForTransfer(&DMAtx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
#ifdef STM32H7xx #ifdef STM32H7xx
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_SR_EOT)) {} while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_SR_EOT)) { /* nada */ }
#else #else
while (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)) {} while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE)) { /* nada */ }
while (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)) { /* nada */ }
#endif #endif
abort(); abort();
} }
@ -337,8 +359,7 @@ void TFT_SPI::transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count)
DMAtx.Init.MemInc = memoryIncrease; DMAtx.Init.MemInc = memoryIncrease;
HAL_DMA_Init(&DMAtx); HAL_DMA_Init(&DMAtx);
if (TFT_MISO_PIN == TFT_MOSI_PIN) if (SPIx.Init.Direction == SPI_DIRECTION_1LINE) SPI_1LINE_TX(&SPIx);
SPI_1LINE_TX(&SPIx);
dataTransferBegin(); dataTransferBegin();

View file

@ -63,9 +63,7 @@ void TFT::lcdInit() {
#if PIN_EXISTS(TFT_BACKLIGHT) #if PIN_EXISTS(TFT_BACKLIGHT)
OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH); OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH);
#endif #endif
#if HAS_LOGO_IN_FLASH TERN_(HAS_LOGO_IN_FLASH, delay(2000));
delay(2000);
#endif
} }
void TFT::lcdClear(uint16_t color) { void TFT::lcdClear(uint16_t color) {

View file

@ -58,7 +58,7 @@ void TFT_String::set_font(const uint8_t *font) {
for (glyph = 0; glyph < EXTRA_GLYPHS; glyph++) glyphs_extra[glyph] = nullptr; for (glyph = 0; glyph < EXTRA_GLYPHS; glyph++) glyphs_extra[glyph] = nullptr;
#endif #endif
DEBUG_ECHOLNPGM("Format: ", ((unifont_t *)font_header)->format); DEBUG_ECHOLNPGM("format: ", ((unifont_t *)font_header)->format);
DEBUG_ECHOLNPGM("capitalAHeight: ", ((unifont_t *)font_header)->capitalAHeight); DEBUG_ECHOLNPGM("capitalAHeight: ", ((unifont_t *)font_header)->capitalAHeight);
DEBUG_ECHOLNPGM("fontStartEncoding: ", ((unifont_t *)font_header)->fontStartEncoding); DEBUG_ECHOLNPGM("fontStartEncoding: ", ((unifont_t *)font_header)->fontStartEncoding);
DEBUG_ECHOLNPGM("fontEndEncoding: ", ((unifont_t *)font_header)->fontEndEncoding); DEBUG_ECHOLNPGM("fontEndEncoding: ", ((unifont_t *)font_header)->fontEndEncoding);

View file

@ -111,37 +111,37 @@ void TFT_IO::initTFT() {
switch (lcd_id) { switch (lcd_id) {
case LTDC_RGB: case LTDC_RGB:
break; break;
case ST7796: // ST7796S 480x320 case ST7796:
DEBUG_ECHO_MSG(" ST7796S"); DEBUG_ECHO_MSG(" ST7796S"); // 480x320
write_esc_sequence(st7796s_init); write_esc_sequence(st7796s_init);
break; break;
case ST7789: // ST7789V 320x240 case ST7789:
DEBUG_ECHO_MSG(" ST7789V"); DEBUG_ECHO_MSG(" ST7789V"); // 320x240
write_esc_sequence(st7789v_init); write_esc_sequence(st7789v_init);
break; break;
case SSD1963: // SSD1963 case SSD1963:
DEBUG_ECHO_MSG(" SSD1963"); DEBUG_ECHO_MSG(" SSD1963");
write_esc_sequence(ssd1963_init); write_esc_sequence(ssd1963_init);
break; break;
case ST7735: // ST7735 160x128 case ST7735:
DEBUG_ECHO_MSG(" ST7735"); DEBUG_ECHO_MSG(" ST7735"); // 160x128
write_esc_sequence(st7735_init); write_esc_sequence(st7735_init);
break; break;
case R61505: // R61505U 320x240 case R61505:
DEBUG_ECHO_MSG(" R61505U"); DEBUG_ECHO_MSG(" R61505U"); // 320x240
write_esc_sequence(r61505_init); write_esc_sequence(r61505_init);
break; break;
case ILI9328: // ILI9328 320x240 case ILI9328:
DEBUG_ECHO_MSG(" ILI9328"); DEBUG_ECHO_MSG(" ILI9328"); // 320x240
write_esc_sequence(ili9328_init); write_esc_sequence(ili9328_init);
break; break;
case ILI9341: // ILI9341 320x240 case ILI9341:
DEBUG_ECHO_MSG(" ILI9341"); DEBUG_ECHO_MSG(" ILI9341"); // 320x240
write_esc_sequence(ili9341_init); write_esc_sequence(ili9341_init);
break; break;
case ILI9488: // ILI9488 480x320 case ILI9488:
case ILI9488_ID1: // 0x8066 ILI9488 480x320 case ILI9488_ID1:
DEBUG_ECHO_MSG(" ILI9488"); DEBUG_ECHO_MSG(" ILI9488"); // 480x320
write_esc_sequence(ili9488_init); write_esc_sequence(ili9488_init);
break; break;
default: default:

View file

@ -38,7 +38,7 @@
#endif #endif
#ifndef TFT_DRIVER #ifndef TFT_DRIVER
#define TFT_DRIVER AUTO #define TFT_DRIVER AUTO
#endif #endif
#define ESC_REG(x) 0xFFFF, 0x00FF & (uint16_t)x #define ESC_REG(x) 0xFFFF, 0x00FF & (uint16_t)x