|
@@ -0,0 +1,269 @@
|
|
1
|
+/*
|
|
2
|
+ * lcd.c
|
|
3
|
+ *
|
|
4
|
+ * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
|
|
5
|
+ *
|
|
6
|
+ * This program is free software: you can redistribute it and/or modify
|
|
7
|
+ * it under the terms of the GNU General Public License as published by
|
|
8
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+ * (at your option) any later version.
|
|
10
|
+ *
|
|
11
|
+ * This program is distributed in the hope that it will be useful,
|
|
12
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+ * GNU General Public License for more details.
|
|
15
|
+ *
|
|
16
|
+ * See <http://www.gnu.org/licenses/>.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+#include "pico/stdlib.h"
|
|
20
|
+#include "hardware/spi.h"
|
|
21
|
+#include "hardware/pwm.h"
|
|
22
|
+#include "driver_st7789.h"
|
|
23
|
+
|
|
24
|
+#include "config.h"
|
|
25
|
+#include "log.h"
|
|
26
|
+#include "lcd.h"
|
|
27
|
+
|
|
28
|
+#define LCD_PIN_DC 8
|
|
29
|
+#define LCD_PIN_CS 9
|
|
30
|
+#define LCD_PIN_CLK 10
|
|
31
|
+#define LCD_PIN_DIN 11
|
|
32
|
+#define LCD_PIN_RST 12
|
|
33
|
+#define LCD_PIN_BL 13
|
|
34
|
+
|
|
35
|
+#define ST7789_PICO_COLUMN 240
|
|
36
|
+#define ST7789_PICO_ROW 240
|
|
37
|
+
|
|
38
|
+#define ST7789_PICO_ACCESS (ST7789_ORDER_PAGE_TOP_TO_BOTTOM | \
|
|
39
|
+ ST7789_ORDER_COLUMN_LEFT_TO_RIGHT | \
|
|
40
|
+ ST7789_ORDER_PAGE_COLUMN_NORMAL | \
|
|
41
|
+ ST7789_ORDER_LINE_TOP_TO_BOTTOM | \
|
|
42
|
+ ST7789_ORDER_COLOR_RGB | \
|
|
43
|
+ ST7789_ORDER_REFRESH_LEFT_TO_RIGHT)
|
|
44
|
+
|
|
45
|
+#define ST7789_PICO_RGB_INTERFACE_COLOR_FORMAT 0
|
|
46
|
+#define ST7789_PICO_CONTROL_INTERFACE_COLOR_FORMAT ST7789_CONTROL_INTERFACE_COLOR_FORMAT_16_BIT
|
|
47
|
+
|
|
48
|
+#define ST7789_PICO_PORCH_NORMAL_BACK 0x0C
|
|
49
|
+#define ST7789_PICO_PORCH_NORMAL_FRONT 0x0C
|
|
50
|
+#define ST7789_PICO_PORCH_ENABLE ST7789_BOOL_FALSE
|
|
51
|
+#define ST7789_PICO_PORCH_IDEL_BACK 0x03
|
|
52
|
+#define ST7789_PICO_PORCH_IDEL_FRONT 0x03
|
|
53
|
+#define ST7789_PICO_PORCH_PART_BACK 0x03
|
|
54
|
+#define ST7789_PICO_PORCH_PART_FRONT 0x03
|
|
55
|
+
|
|
56
|
+#define ST7789_PICO_VGHS ST7789_VGHS_13P26_V
|
|
57
|
+#define ST7789_PICO_VGLS_NEGATIVE ST7789_VGLS_NEGATIVE_10P43
|
|
58
|
+
|
|
59
|
+#define ST7789_PICO_VCOMS 0.725f
|
|
60
|
+
|
|
61
|
+#define ST7789_PICO_XMY ST7789_BOOL_FALSE
|
|
62
|
+#define ST7789_PICO_XBGR ST7789_BOOL_TRUE
|
|
63
|
+#define ST7789_PICO_XINV ST7789_BOOL_FALSE
|
|
64
|
+#define ST7789_PICO_XMX ST7789_BOOL_TRUE
|
|
65
|
+#define ST7789_PICO_XMH ST7789_BOOL_TRUE
|
|
66
|
+#define ST7789_PICO_XMV ST7789_BOOL_FALSE
|
|
67
|
+#define ST7789_PICO_XGS ST7789_BOOL_FALSE
|
|
68
|
+
|
|
69
|
+#define ST7789_PICO_VDV_VRH_FROM ST7789_VDV_VRH_FROM_CMD
|
|
70
|
+#define ST7789_PICO_VRHS 4.45f
|
|
71
|
+#define ST7789_PICO_VDV 0.0f
|
|
72
|
+
|
|
73
|
+#define ST7789_PICO_INVERSION_SELECTION ST7789_INVERSION_SELECTION_DOT
|
|
74
|
+#define ST7789_PICO_FRAME_RATE ST7789_FRAME_RATE_60_HZ
|
|
75
|
+
|
|
76
|
+#define ST7789_PICO_AVDD ST7789_AVDD_6P8_V
|
|
77
|
+#define ST7789_PICO_AVCL_NEGTIVE ST7789_AVCL_NEGTIVE_4P8_V
|
|
78
|
+#define ST7789_PICO_VDS ST7789_VDS_2P3_V
|
|
79
|
+
|
|
80
|
+#define ST7789_PICO_POSITIVE_VOLTAGE_GAMMA {0xD0, 0x04, 0x0D, 0x11, 0x13, 0x2B, 0x3F, \
|
|
81
|
+ 0x54, 0x4C, 0x18, 0x0D, 0x0B, 0x1F, 0x23}
|
|
82
|
+#define ST7789_PICO_NEGATIVA_VOLTAGE_GAMMA {0xD0, 0x04, 0x0C, 0x11, 0x13, 0x2C, 0x3F, \
|
|
83
|
+ 0x44, 0x51, 0x2F, 0x1F, 0x1F, 0x20, 0x23}
|
|
84
|
+
|
|
85
|
+static st7789_handle_t gs_handle;
|
|
86
|
+static uint16_t bl_value = 0;
|
|
87
|
+
|
|
88
|
+static uint8_t st7789_interface_spi_init(void) {
|
|
89
|
+ // Use SPI1 at 100MHz
|
|
90
|
+ spi_init(spi1, 100 * 1000 * 1000);
|
|
91
|
+ gpio_set_function(LCD_PIN_CLK, GPIO_FUNC_SPI);
|
|
92
|
+ gpio_set_function(LCD_PIN_DIN, GPIO_FUNC_SPI);
|
|
93
|
+
|
|
94
|
+ // Chip select is active-low, so we'll initialise it to a driven-high state
|
|
95
|
+ gpio_init(LCD_PIN_CS);
|
|
96
|
+ gpio_set_dir(LCD_PIN_CS, GPIO_OUT);
|
|
97
|
+ gpio_put(LCD_PIN_CS, 1);
|
|
98
|
+
|
|
99
|
+ spi_set_format(spi1,
|
|
100
|
+ 8, // Number of bits per transfer
|
|
101
|
+ 0, // Polarity (CPOL)
|
|
102
|
+ 0, // Phase (CPHA)
|
|
103
|
+ SPI_MSB_FIRST);
|
|
104
|
+
|
|
105
|
+ return 0;
|
|
106
|
+}
|
|
107
|
+
|
|
108
|
+static uint8_t st7789_interface_spi_deinit(void) {
|
|
109
|
+ spi_deinit(spi1);
|
|
110
|
+ gpio_deinit(LCD_PIN_CS);
|
|
111
|
+ return 0;
|
|
112
|
+}
|
|
113
|
+
|
|
114
|
+static uint8_t st7789_interface_spi_write_cmd(uint8_t *buf, uint16_t len) {
|
|
115
|
+ gpio_put(LCD_PIN_CS, 0);
|
|
116
|
+ spi_write_blocking(spi1, buf, len);
|
|
117
|
+ gpio_put(LCD_PIN_CS, 1);
|
|
118
|
+ return 0;
|
|
119
|
+}
|
|
120
|
+
|
|
121
|
+static void st7789_interface_delay_ms(uint32_t ms) {
|
|
122
|
+ sleep_ms(ms);
|
|
123
|
+}
|
|
124
|
+
|
|
125
|
+static void st7789_interface_debug_print(const char *const fmt, ...) {
|
|
126
|
+ va_list args;
|
|
127
|
+ va_start(args, fmt);
|
|
128
|
+ debug_log_va(true, fmt, args);
|
|
129
|
+ va_end(args);
|
|
130
|
+}
|
|
131
|
+
|
|
132
|
+static uint8_t st7789_interface_cmd_data_gpio_init(void) {
|
|
133
|
+ gpio_init(LCD_PIN_DC);
|
|
134
|
+ gpio_set_dir(LCD_PIN_DC, GPIO_OUT);
|
|
135
|
+ return 0;
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+static uint8_t st7789_interface_cmd_data_gpio_deinit(void) {
|
|
139
|
+ gpio_deinit(LCD_PIN_DC);
|
|
140
|
+ return 0;
|
|
141
|
+}
|
|
142
|
+
|
|
143
|
+static uint8_t st7789_interface_cmd_data_gpio_write(uint8_t value) {
|
|
144
|
+ gpio_put(LCD_PIN_DC, value);
|
|
145
|
+ return 0;
|
|
146
|
+}
|
|
147
|
+
|
|
148
|
+static uint8_t st7789_interface_reset_gpio_init(void) {
|
|
149
|
+ gpio_init(LCD_PIN_RST);
|
|
150
|
+ gpio_set_dir(LCD_PIN_RST, GPIO_OUT);
|
|
151
|
+ return 0;
|
|
152
|
+}
|
|
153
|
+
|
|
154
|
+static uint8_t st7789_interface_reset_gpio_deinit(void) {
|
|
155
|
+ gpio_deinit(LCD_PIN_RST);
|
|
156
|
+ return 0;
|
|
157
|
+}
|
|
158
|
+
|
|
159
|
+static uint8_t st7789_interface_reset_gpio_write(uint8_t value) {
|
|
160
|
+ gpio_put(LCD_PIN_RST, value);
|
|
161
|
+ return 0;
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+void lcd_init(void) {
|
|
165
|
+ uint8_t reg;
|
|
166
|
+
|
|
167
|
+ DRIVER_ST7789_LINK_INIT(&gs_handle, st7789_handle_t);
|
|
168
|
+ DRIVER_ST7789_LINK_SPI_INIT(&gs_handle, st7789_interface_spi_init);
|
|
169
|
+ DRIVER_ST7789_LINK_SPI_DEINIT(&gs_handle, st7789_interface_spi_deinit);
|
|
170
|
+ DRIVER_ST7789_LINK_SPI_WRITE_COMMAND(&gs_handle, st7789_interface_spi_write_cmd);
|
|
171
|
+ DRIVER_ST7789_LINK_COMMAND_DATA_GPIO_INIT(&gs_handle, st7789_interface_cmd_data_gpio_init);
|
|
172
|
+ DRIVER_ST7789_LINK_COMMAND_DATA_GPIO_DEINIT(&gs_handle, st7789_interface_cmd_data_gpio_deinit);
|
|
173
|
+ DRIVER_ST7789_LINK_COMMAND_DATA_GPIO_WRITE(&gs_handle, st7789_interface_cmd_data_gpio_write);
|
|
174
|
+ DRIVER_ST7789_LINK_RESET_GPIO_INIT(&gs_handle, st7789_interface_reset_gpio_init);
|
|
175
|
+ DRIVER_ST7789_LINK_RESET_GPIO_DEINIT(&gs_handle, st7789_interface_reset_gpio_deinit);
|
|
176
|
+ DRIVER_ST7789_LINK_RESET_GPIO_WRITE(&gs_handle, st7789_interface_reset_gpio_write);
|
|
177
|
+ DRIVER_ST7789_LINK_DELAY_MS(&gs_handle, st7789_interface_delay_ms);
|
|
178
|
+ DRIVER_ST7789_LINK_DEBUG_PRINT(&gs_handle, st7789_interface_debug_print);
|
|
179
|
+
|
|
180
|
+ st7789_init(&gs_handle);
|
|
181
|
+
|
|
182
|
+ st7789_set_column(&gs_handle, ST7789_PICO_COLUMN);
|
|
183
|
+
|
|
184
|
+ st7789_set_row(&gs_handle, ST7789_PICO_ROW);
|
|
185
|
+
|
|
186
|
+ st7789_set_memory_data_access_control(&gs_handle, ST7789_PICO_ACCESS);
|
|
187
|
+
|
|
188
|
+ st7789_set_interface_pixel_format(&gs_handle,
|
|
189
|
+ ST7789_PICO_RGB_INTERFACE_COLOR_FORMAT,
|
|
190
|
+ ST7789_PICO_CONTROL_INTERFACE_COLOR_FORMAT);
|
|
191
|
+
|
|
192
|
+ st7789_set_porch(&gs_handle,
|
|
193
|
+ ST7789_PICO_PORCH_NORMAL_BACK,
|
|
194
|
+ ST7789_PICO_PORCH_NORMAL_FRONT,
|
|
195
|
+ ST7789_PICO_PORCH_ENABLE,
|
|
196
|
+ ST7789_PICO_PORCH_IDEL_BACK,
|
|
197
|
+ ST7789_PICO_PORCH_IDEL_FRONT,
|
|
198
|
+ ST7789_PICO_PORCH_PART_BACK,
|
|
199
|
+ ST7789_PICO_PORCH_PART_FRONT);
|
|
200
|
+
|
|
201
|
+ st7789_set_gate_control(&gs_handle, ST7789_PICO_VGHS, ST7789_PICO_VGLS_NEGATIVE);
|
|
202
|
+
|
|
203
|
+ st7789_vcom_convert_to_register(&gs_handle, ST7789_PICO_VCOMS, ®);
|
|
204
|
+ st7789_set_vcoms(&gs_handle, reg);
|
|
205
|
+
|
|
206
|
+ st7789_set_lcm_control(&gs_handle,
|
|
207
|
+ ST7789_PICO_XMY,
|
|
208
|
+ ST7789_PICO_XBGR,
|
|
209
|
+ ST7789_PICO_XINV,
|
|
210
|
+ ST7789_PICO_XMX,
|
|
211
|
+ ST7789_PICO_XMH,
|
|
212
|
+ ST7789_PICO_XMV,
|
|
213
|
+ ST7789_PICO_XGS);
|
|
214
|
+
|
|
215
|
+ st7789_set_vdv_vrh_from(&gs_handle, ST7789_PICO_VDV_VRH_FROM);
|
|
216
|
+
|
|
217
|
+ st7789_vrhs_convert_to_register(&gs_handle, ST7789_PICO_VRHS, ®);
|
|
218
|
+ st7789_set_vrhs(&gs_handle, reg);
|
|
219
|
+
|
|
220
|
+ st7789_vdv_convert_to_register(&gs_handle, ST7789_PICO_VDV, ®);
|
|
221
|
+ st7789_set_vdv(&gs_handle, reg);
|
|
222
|
+
|
|
223
|
+ st7789_set_frame_rate(&gs_handle, ST7789_PICO_INVERSION_SELECTION, ST7789_PICO_FRAME_RATE);
|
|
224
|
+
|
|
225
|
+ st7789_set_power_control_1(&gs_handle,
|
|
226
|
+ ST7789_PICO_AVDD,
|
|
227
|
+ ST7789_PICO_AVCL_NEGTIVE,
|
|
228
|
+ ST7789_PICO_VDS);
|
|
229
|
+
|
|
230
|
+ uint8_t param_positive[14] = ST7789_PICO_POSITIVE_VOLTAGE_GAMMA;
|
|
231
|
+ st7789_set_positive_voltage_gamma_control(&gs_handle, param_positive);
|
|
232
|
+
|
|
233
|
+ uint8_t param_negative[14] = ST7789_PICO_NEGATIVA_VOLTAGE_GAMMA;
|
|
234
|
+ st7789_set_negative_voltage_gamma_control(&gs_handle, param_negative);
|
|
235
|
+
|
|
236
|
+ st7789_display_inversion_on(&gs_handle);
|
|
237
|
+
|
|
238
|
+ st7789_sleep_out(&gs_handle);
|
|
239
|
+
|
|
240
|
+ st7789_display_on(&gs_handle);
|
|
241
|
+
|
|
242
|
+ st7789_clear(&gs_handle);
|
|
243
|
+
|
|
244
|
+ // backlight
|
|
245
|
+ uint bl_slice = pwm_gpio_to_slice_num(LCD_PIN_BL);
|
|
246
|
+ uint bl_channel = pwm_gpio_to_channel(LCD_PIN_BL);
|
|
247
|
+ gpio_set_function(LCD_PIN_BL, GPIO_FUNC_PWM);
|
|
248
|
+ pwm_set_wrap(bl_slice, 0xFFFF);
|
|
249
|
+ pwm_set_clkdiv(bl_slice, 1.0f);
|
|
250
|
+ pwm_set_chan_level(bl_slice, bl_channel, bl_value);
|
|
251
|
+ pwm_set_enabled(bl_slice, true);
|
|
252
|
+}
|
|
253
|
+
|
|
254
|
+uint16_t lcd_get_backlight(void) {
|
|
255
|
+ return bl_value;
|
|
256
|
+}
|
|
257
|
+
|
|
258
|
+void lcd_set_backlight(uint16_t value) {
|
|
259
|
+ uint bl_slice = pwm_gpio_to_slice_num(LCD_PIN_BL);
|
|
260
|
+ uint bl_channel = pwm_gpio_to_channel(LCD_PIN_BL);
|
|
261
|
+ bl_value = value;
|
|
262
|
+ pwm_set_chan_level(bl_slice, bl_channel, bl_value);
|
|
263
|
+}
|
|
264
|
+
|
|
265
|
+void lcd_write_point(uint16_t x, uint16_t y, uint32_t color) {
|
|
266
|
+ st7789_draw_point(&gs_handle, x, y, color);
|
|
267
|
+
|
|
268
|
+ st7789_display_on(&gs_handle);
|
|
269
|
+}
|