|
@@ -0,0 +1,165 @@
|
|
1
|
+/**
|
|
2
|
+ * Marlin 3D Printer Firmware
|
|
3
|
+ * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
4
|
+ *
|
|
5
|
+ * This program is free software: you can redistribute it and/or modify
|
|
6
|
+ * it under the terms of the GNU General Public License as published by
|
|
7
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+ * (at your option) any later version.
|
|
9
|
+ *
|
|
10
|
+ * This program is distributed in the hope that it will be useful,
|
|
11
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+ * GNU General Public License for more details.
|
|
14
|
+ *
|
|
15
|
+ * You should have received a copy of the GNU General Public License
|
|
16
|
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+ *
|
|
18
|
+ */
|
|
19
|
+#ifdef __STM32F1__
|
|
20
|
+
|
|
21
|
+#include "../../inc/MarlinConfig.h"
|
|
22
|
+
|
|
23
|
+#if HAS_GRAPHICAL_LCD
|
|
24
|
+
|
|
25
|
+#include "HAL.h"
|
|
26
|
+#include <U8glib.h>
|
|
27
|
+
|
|
28
|
+#undef SPI_SPEED
|
|
29
|
+#define SPI_SPEED 0 // Fastest
|
|
30
|
+//#define SPI_SPEED 2 // Slower
|
|
31
|
+
|
|
32
|
+static uint8_t SPI_speed = SPI_SPEED;
|
|
33
|
+
|
|
34
|
+static inline uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) {
|
|
35
|
+ for (uint8_t i = 0; i < 8; i++) {
|
|
36
|
+ if (spi_speed == 0) {
|
|
37
|
+ WRITE(DOGLCD_MOSI, !!(b & 0x80));
|
|
38
|
+ WRITE(DOGLCD_SCK, HIGH);
|
|
39
|
+ b <<= 1;
|
|
40
|
+ if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
|
|
41
|
+ WRITE(DOGLCD_SCK, LOW);
|
|
42
|
+ }
|
|
43
|
+ else {
|
|
44
|
+ const uint8_t state = (b & 0x80) ? HIGH : LOW;
|
|
45
|
+ for (uint8_t j = 0; j < spi_speed; j++)
|
|
46
|
+ WRITE(DOGLCD_MOSI, state);
|
|
47
|
+
|
|
48
|
+ for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); j++)
|
|
49
|
+ WRITE(DOGLCD_SCK, HIGH);
|
|
50
|
+
|
|
51
|
+ b <<= 1;
|
|
52
|
+ if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
|
|
53
|
+
|
|
54
|
+ for (uint8_t j = 0; j < spi_speed; j++)
|
|
55
|
+ WRITE(DOGLCD_SCK, LOW);
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+ return b;
|
|
59
|
+}
|
|
60
|
+
|
|
61
|
+static inline uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t miso_pin=-1) {
|
|
62
|
+ for (uint8_t i = 0; i < 8; i++) {
|
|
63
|
+ const uint8_t state = (b & 0x80) ? HIGH : LOW;
|
|
64
|
+ if (spi_speed == 0) {
|
|
65
|
+ WRITE(DOGLCD_SCK, LOW);
|
|
66
|
+ WRITE(DOGLCD_MOSI, state);
|
|
67
|
+ WRITE(DOGLCD_MOSI, state); // need some setup time
|
|
68
|
+ WRITE(DOGLCD_SCK, HIGH);
|
|
69
|
+ }
|
|
70
|
+ else {
|
|
71
|
+ for (uint8_t j = 0; j < spi_speed + (miso_pin >= 0 ? 0 : 1); j++)
|
|
72
|
+ WRITE(DOGLCD_SCK, LOW);
|
|
73
|
+
|
|
74
|
+ for (uint8_t j = 0; j < spi_speed; j++)
|
|
75
|
+ WRITE(DOGLCD_MOSI, state);
|
|
76
|
+
|
|
77
|
+ for (uint8_t j = 0; j < spi_speed; j++)
|
|
78
|
+ WRITE(DOGLCD_SCK, HIGH);
|
|
79
|
+ }
|
|
80
|
+ b <<= 1;
|
|
81
|
+ if (miso_pin >= 0 && READ(miso_pin)) b |= 1;
|
|
82
|
+ }
|
|
83
|
+ return b;
|
|
84
|
+}
|
|
85
|
+
|
|
86
|
+static void u8g_sw_spi_HAL_STM32F1_shift_out(uint8_t val) {
|
|
87
|
+ #if ENABLED(FYSETC_MINI_12864)
|
|
88
|
+ swSpiTransfer_mode_3(val, SPI_speed);
|
|
89
|
+ #else
|
|
90
|
+ swSpiTransfer_mode_0(val, SPI_speed);
|
|
91
|
+ #endif
|
|
92
|
+}
|
|
93
|
+
|
|
94
|
+static uint8_t swSpiInit(const uint8_t spi_speed) {
|
|
95
|
+ #if PIN_EXISTS(LCD_RESET)
|
|
96
|
+ SET_OUTPUT(LCD_RESET_PIN);
|
|
97
|
+ #endif
|
|
98
|
+ SET_OUTPUT(DOGLCD_A0);
|
|
99
|
+ OUT_WRITE(DOGLCD_SCK, LOW);
|
|
100
|
+ OUT_WRITE(DOGLCD_MOSI, LOW);
|
|
101
|
+ OUT_WRITE(DOGLCD_CS, HIGH);
|
|
102
|
+ return spi_speed;
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+uint8_t u8g_com_HAL_STM32F1_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
|
|
106
|
+ switch (msg) {
|
|
107
|
+ case U8G_COM_MSG_INIT:
|
|
108
|
+ SPI_speed = swSpiInit(SPI_SPEED);
|
|
109
|
+ break;
|
|
110
|
+
|
|
111
|
+ case U8G_COM_MSG_STOP:
|
|
112
|
+ break;
|
|
113
|
+
|
|
114
|
+ case U8G_COM_MSG_RESET:
|
|
115
|
+ #if PIN_EXISTS(LCD_RESET)
|
|
116
|
+ WRITE(LCD_RESET_PIN, arg_val);
|
|
117
|
+ #endif
|
|
118
|
+ break;
|
|
119
|
+
|
|
120
|
+ case U8G_COM_MSG_CHIP_SELECT:
|
|
121
|
+ #if ENABLED(FYSETC_MINI_12864) // This LCD SPI is running mode 3 while SD card is running mode 0
|
|
122
|
+ if (arg_val) { // SCK idle state needs to be set to the proper idle state before
|
|
123
|
+ // the next chip select goes active
|
|
124
|
+ WRITE(DOGLCD_SCK, HIGH); // Set SCK to mode 3 idle state before CS goes active
|
|
125
|
+ WRITE(DOGLCD_CS, LOW);
|
|
126
|
+ }
|
|
127
|
+ else {
|
|
128
|
+ WRITE(DOGLCD_CS, HIGH);
|
|
129
|
+ WRITE(DOGLCD_SCK, LOW); // Set SCK to mode 0 idle state after CS goes inactive
|
|
130
|
+ }
|
|
131
|
+ #else
|
|
132
|
+ WRITE(DOGLCD_CS, !arg_val);
|
|
133
|
+ #endif
|
|
134
|
+ break;
|
|
135
|
+
|
|
136
|
+ case U8G_COM_MSG_WRITE_BYTE:
|
|
137
|
+ u8g_sw_spi_HAL_STM32F1_shift_out(arg_val);
|
|
138
|
+ break;
|
|
139
|
+
|
|
140
|
+ case U8G_COM_MSG_WRITE_SEQ: {
|
|
141
|
+ uint8_t *ptr = (uint8_t *)arg_ptr;
|
|
142
|
+ while (arg_val > 0) {
|
|
143
|
+ u8g_sw_spi_HAL_STM32F1_shift_out(*ptr++);
|
|
144
|
+ arg_val--;
|
|
145
|
+ }
|
|
146
|
+ } break;
|
|
147
|
+
|
|
148
|
+ case U8G_COM_MSG_WRITE_SEQ_P: {
|
|
149
|
+ uint8_t *ptr = (uint8_t *)arg_ptr;
|
|
150
|
+ while (arg_val > 0) {
|
|
151
|
+ u8g_sw_spi_HAL_STM32F1_shift_out(u8g_pgm_read(ptr));
|
|
152
|
+ ptr++;
|
|
153
|
+ arg_val--;
|
|
154
|
+ }
|
|
155
|
+ } break;
|
|
156
|
+
|
|
157
|
+ case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
|
|
158
|
+ WRITE(DOGLCD_A0, arg_val);
|
|
159
|
+ break;
|
|
160
|
+ }
|
|
161
|
+ return 1;
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+#endif // HAS_GRAPHICAL_LCD
|
|
165
|
+#endif // STM32F1
|