Skip to content

Commit 3075bc3

Browse files
authored
🚸🐛 SW SPI Mode 3 for U8G on STM32 (#27111)
1 parent 842d0b1 commit 3075bc3

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

Marlin/src/HAL/STM32/HAL_SPI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static SPISettings spiConfig;
3737
// Public functions
3838
// ------------------------
3939

40-
#if ENABLED(SOFTWARE_SPI)
40+
#if ANY(SOFTWARE_SPI, FORCE_SOFT_SPI)
4141

4242
// ------------------------
4343
// Software SPI
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2024 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm / Ryan Power
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
#ifdef HAL_STM32
24+
25+
#include "../../../inc/MarlinConfig.h"
26+
27+
#if ALL(HAS_MARLINUI_U8GLIB, FORCE_SOFT_SPI)
28+
29+
#include <U8glib-HAL.h>
30+
#include "../../shared/HAL_SPI.h"
31+
32+
#define nop asm volatile ("\tnop\n")
33+
34+
static inline uint8_t swSpiTransfer_mode_0(uint8_t b) {
35+
for (uint8_t i = 0; i < 8; ++i) {
36+
const uint8_t state = (b & 0x80) ? HIGH : LOW;
37+
WRITE(DOGLCD_SCK, HIGH);
38+
WRITE(DOGLCD_MOSI, state);
39+
b <<= 1;
40+
WRITE(DOGLCD_SCK, LOW);
41+
}
42+
return b;
43+
}
44+
45+
static inline uint8_t swSpiTransfer_mode_3(uint8_t b) {
46+
for (uint8_t i = 0; i < 8; ++i) {
47+
const uint8_t state = (b & 0x80) ? HIGH : LOW;
48+
WRITE(DOGLCD_SCK, LOW);
49+
WRITE(DOGLCD_MOSI, state);
50+
b <<= 1;
51+
WRITE(DOGLCD_SCK, HIGH);
52+
}
53+
return b;
54+
}
55+
56+
static void u8g_sw_spi_shift_out(uint8_t val) {
57+
#if U8G_SPI_USE_MODE_3
58+
swSpiTransfer_mode_3(val);
59+
#else
60+
swSpiTransfer_mode_0(val);
61+
#endif
62+
}
63+
64+
static void swSpiInit() {
65+
#if PIN_EXISTS(LCD_RESET)
66+
SET_OUTPUT(LCD_RESET_PIN);
67+
#endif
68+
SET_OUTPUT(DOGLCD_A0);
69+
OUT_WRITE(DOGLCD_SCK, LOW);
70+
OUT_WRITE(DOGLCD_MOSI, LOW);
71+
OUT_WRITE(DOGLCD_CS, HIGH);
72+
}
73+
74+
uint8_t u8g_com_HAL_STM32_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
75+
switch (msg) {
76+
case U8G_COM_MSG_INIT:
77+
swSpiInit();
78+
break;
79+
80+
case U8G_COM_MSG_STOP:
81+
break;
82+
83+
case U8G_COM_MSG_RESET:
84+
#if PIN_EXISTS(LCD_RESET)
85+
WRITE(LCD_RESET_PIN, arg_val);
86+
#endif
87+
break;
88+
89+
case U8G_COM_MSG_CHIP_SELECT:
90+
#if U8G_SPI_USE_MODE_3 // This LCD SPI is running mode 3 while SD card is running mode 0
91+
if (arg_val) { // SCK idle state needs to be set to the proper idle state before
92+
// the next chip select goes active
93+
WRITE(DOGLCD_SCK, HIGH); // Set SCK to mode 3 idle state before CS goes active
94+
WRITE(DOGLCD_CS, LOW);
95+
nop; // hold SCK high for a few ns
96+
nop;
97+
}
98+
else {
99+
WRITE(DOGLCD_CS, HIGH);
100+
WRITE(DOGLCD_SCK, LOW); // Set SCK to mode 0 idle state after CS goes inactive
101+
}
102+
#else
103+
WRITE(DOGLCD_CS, !arg_val);
104+
#endif
105+
break;
106+
107+
case U8G_COM_MSG_WRITE_BYTE:
108+
u8g_sw_spi_shift_out(arg_val);
109+
break;
110+
111+
case U8G_COM_MSG_WRITE_SEQ: {
112+
uint8_t *ptr = (uint8_t *)arg_ptr;
113+
while (arg_val > 0) {
114+
u8g_sw_spi_shift_out(*ptr++);
115+
arg_val--;
116+
}
117+
} break;
118+
119+
case U8G_COM_MSG_WRITE_SEQ_P: {
120+
uint8_t *ptr = (uint8_t *)arg_ptr;
121+
while (arg_val > 0) {
122+
u8g_sw_spi_shift_out(u8g_pgm_read(ptr));
123+
ptr++;
124+
arg_val--;
125+
}
126+
} break;
127+
128+
case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
129+
WRITE(DOGLCD_A0, arg_val);
130+
break;
131+
}
132+
return 1;
133+
}
134+
135+
#endif // HAS_MARLINUI_U8GLIB && FORCE_SOFT_SPI
136+
#endif // HAL_STM32

Marlin/src/HAL/STM32/u8g/LCD_defines.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
* STM32 LCD-specific defines
2626
*/
2727

28-
uint8_t u8g_com_std_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); // See U8glib-HAL
29-
uint8_t u8g_com_stm32duino_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); // See U8glib-HAL
28+
uint8_t u8g_com_HAL_STM32_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); // u8g_com_stm32duino_swspi.cpp
29+
#define U8G_COM_HAL_SW_SPI_FN u8g_com_HAL_STM32_sw_spi_fn
3030

31-
#define U8G_COM_HAL_SW_SPI_FN u8g_com_std_sw_spi_fn
31+
uint8_t u8g_com_stm32duino_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr); // See U8glib-HAL
3232
#define U8G_COM_HAL_HW_SPI_FN u8g_com_stm32duino_hw_spi_fn

0 commit comments

Comments
 (0)