Skip to content

Commit acefbe9

Browse files
thinkyheadshadow578
andcommitted
Initial support for HC32 U8G LCD
Co-Authored-By: Chris <52449218+shadow578@users.noreply.github.com>
1 parent 18b0dbb commit acefbe9

33 files changed

+553
-172
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#pragma once
23+
24+
/**
25+
* AVR LCD-specific defines
26+
*/
27+
28+
uint8_t u8g_com_HAL_AVR_sw_sp_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_AVR_sw_sp_fn

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

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#pragma once
23+
24+
/**
25+
* DUE (SAM3X8E) LCD-specific defines
26+
*/
27+
28+
uint8_t u8g_com_HAL_DUE_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr);
29+
uint8_t u8g_com_HAL_DUE_shared_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr);
30+
uint8_t u8g_com_HAL_DUE_ST7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr);
31+
32+
#define U8G_COM_HAL_SW_SPI_FN u8g_com_HAL_DUE_sw_spi_fn
33+
#define U8G_COM_HAL_HW_SPI_FN u8g_com_HAL_DUE_shared_hw_spi_fn
34+
#define U8G_COM_ST7920_HAL_SW_SPI u8g_com_HAL_DUE_ST7920_sw_spi_fn

Marlin/src/HAL/ESP32/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 ANY(MKS_MINI_12864, FYSETC_MINI_12864_2_1)
25+
#define U8G_HW_SPI_ESP32 1
26+
#endif
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#pragma once
23+
24+
/**
25+
* ESP32 LCD-specific defines
26+
*/
27+
28+
uint8_t u8g_esp32_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr);
29+
#define U8G_COM_HAL_HW_SPI_FN u8g_esp32_hw_spi_fn

Marlin/src/HAL/ESP32/u8g_esp32_spi.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#include "../../inc/MarlinConfig.h"
2727

28-
#if ANY(MKS_MINI_12864, FYSETC_MINI_12864_2_1)
28+
#if U8G_HW_SPI_ESP32
2929

3030
#include <U8glib-HAL.h>
3131
#include "../shared/HAL_SPI.h"
@@ -41,7 +41,6 @@
4141

4242
static SPISettings spiConfig;
4343

44-
4544
#ifndef LCD_SPI_SPEED
4645
#ifdef SD_SPI_SPEED
4746
#define LCD_SPI_SPEED SD_SPI_SPEED // Assume SPI speed shared with SD
@@ -50,7 +49,7 @@ static SPISettings spiConfig;
5049
#endif
5150
#endif
5251

53-
uint8_t u8g_eps_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
52+
uint8_t u8g_esp32_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
5453
static uint8_t msgInitCount = 2; // Ignore all messages until 2nd U8G_COM_MSG_INIT
5554

5655
#if ENABLED(PAUSE_LCD_FOR_BUSY_SD)
@@ -101,6 +100,5 @@ uint8_t u8g_eps_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_pt
101100
return 1;
102101
}
103102

104-
#endif // MKS_MINI_12864 || FYSETC_MINI_12864_2_1
105-
103+
#endif // U8G_HW_SPI_ESP32
106104
#endif // ARDUINO_ARCH_ESP32

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 BOTH(HAS_MARLINUI_U8GLIB, FORCE_SOFT_SPI)
25+
#define U8G_SW_SPI_HC32 1
26+
#endif

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

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#pragma once
23+
24+
/**
25+
* HC32 LCD-specific defines
26+
*/
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
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#pragma once
23+
24+
/**
25+
* Linux LCD-specific defines
26+
*/

Marlin/src/HAL/LPC1768/Servo.h

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
* License along with this library; if not, write to the Free Software
4040
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
4141
*/
42-
#pragma once
4342

4443
/**
4544
* Based on "servo.h - Interrupt driven Servo library for Arduino using 16 bit timers -

0 commit comments

Comments
 (0)