Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SPI TFT for STM32F1/F4 #26052

Merged
merged 39 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
aed88a7
Fix SPI TFT for STM32F4
jmz52 Jul 5, 2023
69705bc
Fix SPI TFT for STM32F4
jmz52 Jul 5, 2023
4f9f889
Merge branch 'stm32f4-tft-spi-fix' of https://github.com/jmz52/Marlin…
jmz52 Jul 24, 2023
46b05fd
Merge remote-tracking branch 'upstream/bugfix-2.1.x' into stm32f4-tft…
jmz52 Jul 24, 2023
9d40118
Update tft_spi.cpp
jmz52 Aug 2, 2023
81b216d
Fix lowercase issue introduced in PR#25939
jmz52 Aug 2, 2023
dce700d
Update tft_string.cpp
jmz52 Aug 2, 2023
75af24f
Merge remote-tracking branch 'MarlinFirmware/bugfix-2.1.x' into stm32…
thisiskeithb Aug 2, 2023
e6ee56b
Add debug messages to TFT_SPI::getID()
jmz52 Aug 5, 2023
cac799d
Update tft_spi.cpp
jmz52 Aug 5, 2023
6ae35fe
Update tft_spi.cpp
jmz52 Aug 5, 2023
e619ad9
Merge branch 'bugfix-2.1.x' into pr/26052
thinkyhead Aug 7, 2023
7b05cb6
TFT debug off
thinkyhead Aug 7, 2023
dc79513
Merge remote-tracking branch 'MarlinFirmware/bugfix-2.1.x' into stm32…
thisiskeithb Sep 22, 2023
537c67c
Merge remote-tracking branch 'MarlinFirmware/bugfix-2.1.x' into stm32…
thisiskeithb Oct 4, 2023
d282abd
Merge remote-tracking branch 'MarlinFirmware/bugfix-2.1.x' into stm32…
thisiskeithb Oct 9, 2023
a828692
Merge remote-tracking branch 'MarlinFirmware/bugfix-2.1.x' into stm32…
thisiskeithb Oct 21, 2023
bcc5383
Merge branch 'bugfix-2.1.x' into pr/26052
thinkyhead Oct 26, 2023
55360d0
Use TFT_DEFAULT_DRIVER
thinkyhead Oct 26, 2023
abe0885
or… try it this way
thinkyhead Oct 27, 2023
33cd243
use REMEMBER
thinkyhead Oct 27, 2023
0888bb0
Merge remote-tracking branch 'MarlinFirmware/bugfix-2.1.x' into stm32…
thisiskeithb Nov 21, 2023
ec5a964
Update tft_spi.cpp
jmz52 Nov 22, 2023
64a3258
Fix SPI TFT for STM32F4
jmz52 Jul 5, 2023
234c30a
Update tft_spi.cpp
jmz52 Aug 2, 2023
09f94d6
Update tft_string.cpp
jmz52 Aug 2, 2023
acce304
Add debug messages to TFT_SPI::getID()
jmz52 Aug 5, 2023
1bf6a6b
Update tft_spi.cpp
jmz52 Aug 5, 2023
cc23e90
Update tft_spi.cpp
jmz52 Aug 5, 2023
7afc4c8
TFT debug off
thinkyhead Aug 7, 2023
c4b775d
Use TFT_DEFAULT_DRIVER
thinkyhead Oct 26, 2023
bb54ec9
or… try it this way
thinkyhead Oct 27, 2023
e88d343
use REMEMBER
thinkyhead Oct 27, 2023
3c05cc9
Update tft_spi.cpp
jmz52 Nov 22, 2023
47325d3
Merge branch 'stm32f4-tft-spi-fix' of https://github.com/jmz52/Marlin…
jmz52 Dec 4, 2023
2483e3c
Disable DEBUG_TFT_IO by default
thisiskeithb Dec 16, 2023
20e7229
Merge remote-tracking branch 'MarlinFirmware/bugfix-2.1.x' into stm32…
thisiskeithb Dec 16, 2023
54f10bf
Merge branch 'bugfix-2.1.x' into pr/26052
thinkyhead Jan 8, 2024
2e7a9c4
hint that empty while is intentional
thinkyhead Jan 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 63 additions & 44 deletions Marlin/src/HAL/STM32/tft/tft_spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "tft_spi.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;
DMA_HandleTypeDef TFT_SPI::DMAtx;

Expand All @@ -43,8 +47,8 @@ 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_MOSI_PIN), PinMap_SPI_MOSI)) return;

#if PIN_EXISTS(TFT_MISO) && TFT_MISO_PIN != TFT_MOSI_PIN
if (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO)) return;
#if PIN_EXISTS(TFT_MISO)
if ((TFT_MISO_PIN != TFT_MOSI_PIN) && (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO))) return;
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
#endif

SPIx.Instance = spiInstance;
Expand Down Expand Up @@ -76,10 +80,12 @@ void TFT_SPI::init() {

pinmap_pinout(digitalPinToPinName(TFT_SCK_PIN), PinMap_SPI_SCLK);
pinmap_pinout(digitalPinToPinName(TFT_MOSI_PIN), PinMap_SPI_MOSI);
#if PIN_EXISTS(TFT_MISO) && TFT_MISO_PIN != TFT_MOSI_PIN
pinmap_pinout(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO);
#if PIN_EXISTS(TFT_MISO)
if (TFT_MISO_PIN != TFT_MOSI_PIN) pinmap_pinout(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO);
#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
if (SPIx.Instance == SPI1) {
__HAL_RCC_SPI1_CLK_ENABLE();
Expand Down Expand Up @@ -151,29 +157,47 @@ void TFT_SPI::dataTransferBegin(uint16_t dataSize) {
WRITE(TFT_CS_PIN, LOW);
}

#ifdef TFT_DEFAULT_DRIVER
#include "../../../lcd/tft_io/tft_ids.h"
#endif
#include "../../../lcd/tft_io/tft_ids.h"

uint32_t TFT_SPI::getID() {
uint32_t id;
id = readID(LCD_READ_ID);
DEBUG_ECHOLNPGM("TFT_SPI::getID()");

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) {
id = readID(LCD_READ_ID4);
#ifdef TFT_DEFAULT_DRIVER
if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF)
id = TFT_DEFAULT_DRIVER;
#if ENABLED(DEBUG_TFT_IO)
sprintf_P(debug_register, PSTR("%02X"), LCD_READ_ID4);
sprintf_P(debug_value, PSTR("%04X"), uint16_t(id));
DEBUG_ECHOLNPGM(" readID(0x", debug_register, ") : 0x", debug_value);
#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;
}

uint32_t TFT_SPI::readID(const uint16_t inReg) {
uint32_t data = 0;
#if PIN_EXISTS(TFT_MISO)
const uint32_t oldPrescaler = SPIx.Init.BaudRatePrescaler;

uint32_t BaudRatePrescaler = SPIx.Init.BaudRatePrescaler;
SPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;

dataTransferBegin(DATASIZE_8BIT);
writeReg(inReg);

Expand All @@ -185,10 +209,8 @@ uint32_t TFT_SPI::readID(const uint16_t inReg) {
__HAL_SPI_ENABLE(&SPIx);
SET_BIT(SPIx.Instance->CR1, SPI_CR1_CSTART);

#if TFT_MISO_PIN != TFT_MOSI_PIN
SPIx.Instance->TXDR = 0;
#endif
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_EOT)) {}
if (SPIx.Init.Direction == SPI_DIRECTION_2LINES) SPIx.Instance->TXDR = 0;
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_EOT));
data = (data << 8) | SPIx.Instance->RXDR;
__HAL_SPI_DISABLE(&SPIx);
__HAL_SPI_CLEAR_EOTFLAG(&SPIx);
Expand All @@ -197,19 +219,22 @@ uint32_t TFT_SPI::readID(const uint16_t inReg) {
#else
__HAL_SPI_ENABLE(&SPIx);
for (uint32_t i = 0; i < 4; i++) {
#if TFT_MISO_PIN != TFT_MOSI_PIN
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE)) {}
if (SPIx.Init.Direction == SPI_DIRECTION_2LINES) {
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE));
SPIx.Instance->DR = 0;
#endif
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_RXNE)) {}
}
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_RXNE));
data = (data << 8) | SPIx.Instance->DR;
}
#endif

dataTransferEnd();
SPIx.Init.BaudRatePrescaler = oldPrescaler;
#if DISABLED(DEBUG_TFT_IO)
SPIx.Init.BaudRatePrescaler = BaudRatePrescaler;
#endif
#endif

DEBUG_ECHOLNPGM(" raw data : ", data);
return data >> 7;
}

Expand Down Expand Up @@ -238,13 +263,13 @@ bool TFT_SPI::isBusy() {
// Check if SPI data transfer is completed
if (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_EOT)) return true;
#else
// Check if SPI is idle
if (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)) return true;
// Check if SPI transmit butter is empty and SPI is idle
if ((!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE)) || (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY))) return true;
#endif
}

abort();
return false;
return true;
}

void TFT_SPI::abort() {
Expand All @@ -263,9 +288,7 @@ void TFT_SPI::abort() {
}

void TFT_SPI::transmit(uint16_t data) {
#if TFT_MISO_PIN == TFT_MOSI_PIN
SPI_1LINE_TX(&SPIx);
#endif
if (SPIx.Init.Direction == SPI_DIRECTION_1LINE) SPI_1LINE_TX(&SPIx);

#ifdef STM32H7xx
MODIFY_REG(SPIx.Instance->CR2, SPI_CR2_TSIZE, 1);
Expand All @@ -274,30 +297,26 @@ void TFT_SPI::transmit(uint16_t data) {

SPIx.Instance->TXDR = data;

while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_SR_EOT)) {}
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_SR_EOT));

__HAL_SPI_CLEAR_EOTFLAG(&SPIx);
__HAL_SPI_CLEAR_TXTFFLAG(&SPIx);
__HAL_SPI_DISABLE(&SPIx);
#else
__HAL_SPI_ENABLE(&SPIx);
SPIx.Instance->DR = data;
while (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)) {}
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE)); // Wait for data transfer to actually start
while (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)); // Wait until SPI is idle
#endif

__HAL_SPI_DISABLE(&SPIx);

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

void TFT_SPI::transmitDMA(uint32_t memoryIncrease, uint16_t *data, uint16_t count) {
DMAtx.Init.MemInc = memoryIncrease;
HAL_DMA_Init(&DMAtx);

#if TFT_MISO_PIN == TFT_MOSI_PIN
SPI_1LINE_TX(&SPIx);
#endif
if (SPIx.Init.Direction == SPI_DIRECTION_1LINE) SPI_1LINE_TX(&SPIx);

dataTransferBegin();

Expand All @@ -324,9 +343,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);
#ifdef STM32H7xx
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_SR_EOT)) {}
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_SR_EOT));
#else
while (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY)) {}
while (!__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_TXE));
while (__HAL_SPI_GET_FLAG(&SPIx, SPI_FLAG_BSY));
#endif
abort();
}
Expand All @@ -337,8 +357,7 @@ void TFT_SPI::transmit(uint32_t memoryIncrease, uint16_t *data, uint16_t count)
DMAtx.Init.MemInc = memoryIncrease;
HAL_DMA_Init(&DMAtx);

if (TFT_MISO_PIN == TFT_MOSI_PIN)
SPI_1LINE_TX(&SPIx);
if (SPIx.Init.Direction == SPI_DIRECTION_1LINE) SPI_1LINE_TX(&SPIx);

dataTransferBegin();

Expand Down
4 changes: 1 addition & 3 deletions Marlin/src/lcd/extui/mks_ui/SPI_TFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ void TFT::lcdInit() {
#if PIN_EXISTS(TFT_BACKLIGHT)
OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH);
#endif
#if HAS_LOGO_IN_FLASH
delay(2000);
#endif
TERN_(HAS_LOGO_IN_FLASH, delay(2000));
}

void TFT::lcdClear(uint16_t color) {
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/tft/tft_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void TFT_String::set_font(const uint8_t *font) {
for (glyph = 0; glyph < EXTRA_GLYPHS; glyph++) glyphs_extra[glyph] = nullptr;
#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("fontStartEncoding: ", ((unifont_t *)font_header)->fontStartEncoding);
DEBUG_ECHOLNPGM("fontEndEncoding: ", ((unifont_t *)font_header)->fontEndEncoding);
Expand Down
32 changes: 16 additions & 16 deletions Marlin/src/lcd/tft_io/tft_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,37 +111,37 @@ void TFT_IO::initTFT() {
switch (lcd_id) {
case LTDC_RGB:
break;
case ST7796: // ST7796S 480x320
DEBUG_ECHO_MSG(" ST7796S");
case ST7796:
DEBUG_ECHO_MSG(" ST7796S"); // 480x320
write_esc_sequence(st7796s_init);
break;
case ST7789: // ST7789V 320x240
DEBUG_ECHO_MSG(" ST7789V");
case ST7789:
DEBUG_ECHO_MSG(" ST7789V"); // 320x240
write_esc_sequence(st7789v_init);
break;
case SSD1963: // SSD1963
case SSD1963:
DEBUG_ECHO_MSG(" SSD1963");
write_esc_sequence(ssd1963_init);
break;
case ST7735: // ST7735 160x128
DEBUG_ECHO_MSG(" ST7735");
case ST7735:
DEBUG_ECHO_MSG(" ST7735"); // 160x128
write_esc_sequence(st7735_init);
break;
case R61505: // R61505U 320x240
DEBUG_ECHO_MSG(" R61505U");
case R61505:
DEBUG_ECHO_MSG(" R61505U"); // 320x240
write_esc_sequence(r61505_init);
break;
case ILI9328: // ILI9328 320x240
DEBUG_ECHO_MSG(" ILI9328");
case ILI9328:
DEBUG_ECHO_MSG(" ILI9328"); // 320x240
write_esc_sequence(ili9328_init);
break;
case ILI9341: // ILI9341 320x240
DEBUG_ECHO_MSG(" ILI9341");
case ILI9341:
DEBUG_ECHO_MSG(" ILI9341"); // 320x240
write_esc_sequence(ili9341_init);
break;
case ILI9488: // ILI9488 480x320
case ILI9488_ID1: // 0x8066 ILI9488 480x320
DEBUG_ECHO_MSG(" ILI9488");
case ILI9488:
case ILI9488_ID1:
DEBUG_ECHO_MSG(" ILI9488"); // 480x320
write_esc_sequence(ili9488_init);
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/tft_io/tft_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#endif

#ifndef TFT_DRIVER
#define TFT_DRIVER AUTO
#define TFT_DRIVER AUTO
#endif

#define ESC_REG(x) 0xFFFF, 0x00FF & (uint16_t)x
Expand Down