Skip to content

Commit d903a5e

Browse files
✨ Initial support for HC32 U8G LCD (MarlinFirmware#26568)
Co-authored-by: Chris <52449218+shadow578@users.noreply.github.com>
1 parent 15f26b4 commit d903a5e

File tree

4 files changed

+177
-8
lines changed

4 files changed

+177
-8
lines changed

Marlin/src/HAL/HC32/inc/Conditionals_LCD.h

+4
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,7 @@
2020
*
2121
*/
2222
#pragma once
23+
24+
#if ALL(HAS_MARLINUI_U8GLIB, FORCE_SOFT_SPI)
25+
#define U8G_SW_SPI_HC32 1
26+
#endif

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

+3
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@
2424
/**
2525
* HC32 LCD-specific defines
2626
*/
27+
28+
uint8_t u8g_com_HAL_HC32_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr);
29+
#define U8G_COM_HAL_SW_SPI_FN u8g_com_HAL_HC32_sw_spi_fn
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
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 ARDUINO_ARCH_HC32
24+
25+
#include "../../../inc/MarlinConfig.h"
26+
27+
#if U8G_SW_SPI_HC32
28+
29+
#warning "Software SPI for U8Glib is experimental on HC32F460. Please share your experiences at https://github.com/shadow578/Marlin-H32/issues/35"
30+
31+
#include <U8glib-HAL.h>
32+
#include "../../shared/HAL_SPI.h"
33+
34+
#ifndef LCD_SPI_SPEED
35+
#define LCD_SPI_SPEED SPI_FULL_SPEED // Fastest
36+
//#define LCD_SPI_SPEED SPI_QUARTER_SPEED // Slower
37+
#endif
38+
39+
static uint8_t SPI_speed = LCD_SPI_SPEED;
40+
41+
static inline uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) {
42+
for (i = 0; i < 8; ++i) {
43+
if (spi_speed == 0) {
44+
WRITE(DOGLCD_MOSI, !!(b & 0x80));
45+
WRITE(DOGLCD_SCK, HIGH);
46+
b <<= 1;
47+
if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
48+
WRITE(DOGLCD_SCK, LOW);
49+
}
50+
else {
51+
const uint8_t state = (b & 0x80) ? HIGH : LOW;
52+
for (j = 0; j < spi_speed; ++j) WRITE(DOGLCD_MOSI, state);
53+
for (j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1; ++j)) WRITE(DOGLCD_SCK, HIGH);
54+
55+
b <<= 1;
56+
if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
57+
58+
for (j = 0; j < spi_speed; ++j) WRITE(DOGLCD_SCK, LOW);
59+
}
60+
}
61+
return b;
62+
}
63+
64+
static inline uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) {
65+
for (i = 0; i < 8; ++i) {
66+
const uint8_t state = (b & 0x80) ? HIGH : LOW;
67+
if (spi_speed == 0) {
68+
WRITE(DOGLCD_SCK, LOW);
69+
WRITE(DOGLCD_MOSI, state);
70+
WRITE(DOGLCD_MOSI, state); // need some setup time
71+
WRITE(DOGLCD_SCK, HIGH);
72+
}
73+
else {
74+
for (j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); ++j) WRITE(DOGLCD_SCK, LOW);
75+
for (j = 0; j < spi_speed; ++j) WRITE(DOGLCD_MOSI, state);
76+
for (j = 0; j < spi_speed; ++j) WRITE(DOGLCD_SCK, HIGH);
77+
}
78+
b <<= 1;
79+
if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
80+
}
81+
return b;
82+
}
83+
84+
static void u8g_sw_spi_shift_out(uint8_t val) {
85+
#if ANY(FYSETC_MINI_12864, MKS_MINI_12864)
86+
swSpiTransfer_mode_3(val, SPI_speed);
87+
#else
88+
swSpiTransfer_mode_0(val, SPI_speed);
89+
#endif
90+
}
91+
92+
static uint8_t swSpiInit(const uint8_t spi_speed) {
93+
#if PIN_EXISTS(LCD_RESET)
94+
SET_OUTPUT(LCD_RESET_PIN);
95+
#endif
96+
SET_OUTPUT(DOGLCD_A0);
97+
OUT_WRITE(DOGLCD_SCK, LOW);
98+
OUT_WRITE(DOGLCD_MOSI, LOW);
99+
OUT_WRITE(DOGLCD_CS, HIGH);
100+
return spi_speed;
101+
}
102+
103+
uint8_t u8g_com_HAL_HC32_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
104+
switch (msg) {
105+
case U8G_COM_MSG_INIT:
106+
SPI_speed = swSpiInit(LCD_SPI_SPEED);
107+
break;
108+
109+
case U8G_COM_MSG_STOP:
110+
break;
111+
112+
case U8G_COM_MSG_RESET:
113+
#if PIN_EXISTS(LCD_RESET)
114+
WRITE(LCD_RESET_PIN, arg_val);
115+
#endif
116+
break;
117+
118+
case U8G_COM_MSG_CHIP_SELECT:
119+
#if ANY(FYSETC_MINI_12864, MKS_MINI_12864) // This LCD SPI is running mode 3 while SD card is running mode 0
120+
if (arg_val) { // SCK idle state needs to be set to the proper idle state before
121+
// the next chip select goes active
122+
WRITE(DOGLCD_SCK, HIGH); // Set SCK to mode 3 idle state before CS goes active
123+
WRITE(DOGLCD_CS, LOW);
124+
}
125+
else {
126+
WRITE(DOGLCD_CS, HIGH);
127+
WRITE(DOGLCD_SCK, LOW); // Set SCK to mode 0 idle state after CS goes inactive
128+
}
129+
#else
130+
WRITE(DOGLCD_CS, !arg_val);
131+
#endif
132+
break;
133+
134+
case U8G_COM_MSG_WRITE_BYTE:
135+
u8g_sw_spi_shift_out(arg_val);
136+
break;
137+
138+
case U8G_COM_MSG_WRITE_SEQ: {
139+
uint8_t *ptr = (uint8_t *)arg_ptr;
140+
while (arg_val > 0) {
141+
u8g_sw_spi_shift_out(*ptr++);
142+
arg_val--;
143+
}
144+
} break;
145+
146+
case U8G_COM_MSG_WRITE_SEQ_P: {
147+
uint8_t *ptr = (uint8_t *)arg_ptr;
148+
while (arg_val > 0) {
149+
u8g_sw_spi_shift_out(u8g_pgm_read(ptr));
150+
ptr++;
151+
arg_val--;
152+
}
153+
} break;
154+
155+
case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
156+
WRITE(DOGLCD_A0, arg_val);
157+
break;
158+
}
159+
return 1;
160+
}
161+
162+
#endif // U8G_SW_SPI_HC32
163+
#endif // ARDUINO_ARCH_HC32

Marlin/src/pins/hc32f4/pins_AQUILA_101.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,21 @@
153153
#define EXP1_08_PIN PB15 // EN1
154154

155155
#if ENABLED(CR10_STOCKDISPLAY) // LCD used for C2
156-
#undef LCD_SERIAL_PORT
157-
#define LCD_SERIAL_PORT 1
158156

159-
#define LCD_PINS_RS EXP1_07_PIN
160-
#define LCD_PINS_EN EXP1_08_PIN
161-
#define LCD_PINS_D4 EXP1_06_PIN
157+
#ifndef HAS_PIN_27_BOARD
158+
#define BEEPER_PIN EXP1_01_PIN
159+
#endif
162160

163161
#define BTN_ENC EXP1_02_PIN
164162
#define BTN_EN1 EXP1_03_PIN
165163
#define BTN_EN2 EXP1_05_PIN
166164

167-
#ifndef HAS_PIN_27_BOARD
168-
#define BEEPER_PIN EXP1_01_PIN
169-
#endif
165+
#define LCD_PINS_RS EXP1_07_PIN
166+
#define LCD_PINS_EN EXP1_08_PIN
167+
#define LCD_PINS_D4 EXP1_06_PIN
170168

171169
#elif ANY(HAS_DWIN_E3V2, IS_DWIN_MARLINUI) // LCD used for X2
170+
172171
/**
173172
* Display pinout (display side, so RX/TX are swapped)
174173
*

0 commit comments

Comments
 (0)