-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathst7735.c
441 lines (350 loc) · 10.7 KB
/
st7735.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* Author, Copyright: Oleg Borodin <[email protected]> 2018 */
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/spi.h>
#include <stdlib.h>
#include <stdio.h>
#include <st7735.h>
#ifndef USE_SPI
#define USE_SPI 3
#endif
#if (USE_SPI == 1)
#define LCD_SPI SPI1
#define LCD_SPI_PORT GPIOA
#define LCD_SDA_PIN GPIO7
#define LCD_SCL_PIN GPIO5
#define LCD_CS_PIN GPIO4
#define LCD_A0_PORT GPIOA
#define LCD_A0_PIN GPIO6
#define LCD_RESET_PORT GPIOA
#define LCD_RESET_PIN GPIO3
#elif (USE_SPI == 2)
#define LCD_SPI SPI2
#define LCD_SPI_PORT GPIOB
#define LCD_SDA_PIN GPIO15
#define LCD_SCL_PIN GPIO13
#define LCD_CS_PIN GPIO12
#define LCD_A0_PORT GPIOB
#define LCD_A0_PIN GPIO1
#elif (USE_SPI == 3)
#define LCD_SPI SPI2
#define LCD_SPI_PORT GPIOB
#define LCD_SDA_PIN GPIO15
#define LCD_SCL_PIN GPIO13
#define LCD_CS_PIN GPIO12
#define LCD_A0_PORT GPIOC
#define LCD_A0_PIN GPIO6
//#define LCD_RESET_PORT GPIOB
//#define LCD_RESET_PIN GPIO2
#else
#error Please define USE_SPI=1 or USE_SPI=2
#endif
//#define LCD_RESET_PORT GPIOB
//#define LCD_RESET_PIN GPIO2
void lcd_spi_setup(void) {
gpio_set_mode(LCD_SPI_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, LCD_CS_PIN);
gpio_set_mode(LCD_SPI_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, LCD_SCL_PIN);
gpio_set_mode(LCD_SPI_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, LCD_SDA_PIN);
gpio_set_mode(LCD_A0_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LCD_A0_PIN);
#if (USE_SPI == 1)
gpio_set_mode(LCD_RESET_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LCD_RESET_PIN);
#endif
spi_reset(LCD_SPI);
spi_disable(LCD_SPI);
spi_init_master(LCD_SPI,
SPI_CR1_BAUDRATE_FPCLK_DIV_2,
SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE,
SPI_CR1_CPHA_CLK_TRANSITION_2,
SPI_CR1_DFF_8BIT,
SPI_CR1_MSBFIRST);
spi_disable_software_slave_management(LCD_SPI);
spi_set_nss_high(LCD_SPI);
spi_set_master_mode(LCD_SPI);
spi_enable_ss_output(LCD_SPI);
spi_disable_crc(LCD_SPI);
spi_enable(LCD_SPI);
}
void _delay_ms (uint16_t ms) {
for (volatile int i = 0; i < ms * 800; i++)
__asm__("nop");
}
void _delay (uint16_t ms) {
for (volatile int i = 0; i < ms; i++)
__asm__("nop");
}
void spi_wait_busy(uint32_t spi) {
while ((SPI_SR(spi) & SPI_SR_BSY));
_delay(1);
}
void lcd_write_byte(uint8_t c) {
spi_wait_busy(LCD_SPI);
gpio_set(LCD_A0_PORT, LCD_A0_PIN);
spi_send(LCD_SPI, c);
}
void lcd_write_word(uint16_t w) {
spi_wait_busy(LCD_SPI);
gpio_set(LCD_A0_PORT, LCD_A0_PIN);
spi_send(LCD_SPI, (uint8_t)(w >> 8));
spi_send(LCD_SPI, (uint8_t)(w & 0xff));
}
static void lcd_write_command(uint8_t c) {
spi_wait_busy(LCD_SPI);
gpio_clear(LCD_A0_PORT, LCD_A0_PIN);
spi_send(LCD_SPI, c);
}
void spi_write_word_array(uint16_t w, uint16_t n) {
spi_wait_busy(LCD_SPI);
gpio_set(LCD_A0_PORT, LCD_A0_PIN);
while (n--) {
spi_send(LCD_SPI, (uint8_t)(w >> 8));
spi_send(LCD_SPI, (uint8_t)(w & 0xff));
}
}
void lcd_hard_reset(void) {
#if (USE_SPI == 1)
gpio_set(LCD_RESET_PORT, LCD_RESET_PIN);
_delay_ms(100);
gpio_clear(LCD_RESET_PORT, LCD_RESET_PIN);
_delay_ms(100);
gpio_set(LCD_RESET_PORT, LCD_RESET_PIN);
#endif
}
void lcd_setup(void) {
lcd_hard_reset();
_delay_ms(500);
/* 1: Software reset, 0 args, w/delay */
lcd_write_command(LCD_SWRESET);
/* 150 ms delay */
_delay_ms(150);
/* 2: Out of sleep mode, 0 args, w/delay */
lcd_write_command(LCD_SLPOUT);
/* 500 ms delay */
_delay_ms(500);
/* 3: Frame rate ctrl - normal mode, 3 args: */
lcd_write_command(LCD_FRMCTR1);
/* Rate = fosc/(1x2+40) * (LINE+2C+2D) */
lcd_write_byte(0x01);
lcd_write_byte(0x2C);
lcd_write_byte(0x2D);
/* 4: Frame rate control - idle mode, 3 args: */
lcd_write_command(LCD_FRMCTR2);
/* Rate = fosc/(1x2+40) * (LINE+2C+2D) */
lcd_write_byte(0x01);
lcd_write_byte(0x2C);
lcd_write_byte(0x2D);
/* 5: Frame rate ctrl - partial mode, 6 args: */
lcd_write_command(LCD_FRMCTR3);
/* Dot inversion mode */
lcd_write_byte(0x01);
lcd_write_byte(0x2C);
lcd_write_byte(0x2D);
/* Line inversion mode */
lcd_write_byte(0x01);
lcd_write_byte(0x2C);
lcd_write_byte(0x2D);
/* 6: Display inversion ctrl, 1 arg, no delay: */
lcd_write_command(LCD_INVCTR);
/* No inversion */
lcd_write_byte(0x07);
/* 7: Power control, 3 args, no delay: */
lcd_write_command(LCD_PWCTR1);
lcd_write_byte(0xA2);
/* -4.6V */
lcd_write_byte(0x02);
/* AUTO mode */
lcd_write_byte(0x84);
/* 8: Power control, 1 arg, no delay: */
lcd_write_command(LCD_PWCTR2);
/* VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD */
lcd_write_byte(0xC5);
/* 9: Power control, 2 args, no delay: */
lcd_write_command(LCD_PWCTR3);
/* Opamp current small */
lcd_write_byte(0x0A);
/* Boost frequency */
lcd_write_byte(0x00);
/* 10: Power control, 2 args, no delay: */
lcd_write_command(LCD_PWCTR4);
/* BCLK/2, Opamp current small & Medium low */
lcd_write_byte(0x8A);
lcd_write_byte(0x2A);
/* 11: Power control, 2 args, no delay: */
lcd_write_command(LCD_PWCTR5);
lcd_write_byte(0x8A);
lcd_write_byte(0xEE);
/* 12: Power control, 1 arg, no delay: */
lcd_write_command(LCD_VMCTR1);
lcd_write_byte(0x0E);
/* 13: Don't invert display, no args, no delay */
lcd_write_command(LCD_INVOFF);
/* 14: Memory access control (directions), 1 arg: */
lcd_write_command(LCD_MADCTL);
/* row addr/col addr, bottom to top refresh, RGB order */
lcd_write_byte(/* 0xC0 */ LCD_MADCTL_MX | LCD_MADCTL_MY | LCD_MADCTL_BGR | LCD_MADCTL_ML | LCD_MADCTL_MH);
/* 15: Set color mode, 1 arg + delay: */
lcd_write_command(LCD_COLMOD);
/* 16-bit color 5-6-5 color format */
lcd_write_byte(0x05);
/* 10 ms delay */
_delay_ms(10);
/* 1: Column addr set, 4 args, no delay */
lcd_write_command(LCD_CASET);
/* XSTART = 0 */
lcd_write_byte(0x00);
lcd_write_byte(0x00);
/* XEND = 127 */
lcd_write_byte(0x00);
lcd_write_byte(LCD_TFTWIDTH /* 0x7F */);
/* 2: Row addr set, 4 args, no delay: */
lcd_write_command(LCD_RASET);
/* XSTART = 0 */
lcd_write_byte(0x00);
lcd_write_byte(0x00);
/* XEND = 127 */
lcd_write_byte(0x00);
lcd_write_byte(LCD_TFTHEIGHT /* 0x7F */);
/* 1: Magical unicorn dust, 16 args, no delay: */
lcd_write_command(LCD_GMCTRP1);
lcd_write_byte(0x02);
lcd_write_byte(0x1c);
lcd_write_byte(0x07);
lcd_write_byte(0x12);
lcd_write_byte(0x37);
lcd_write_byte(0x32);
lcd_write_byte(0x29);
lcd_write_byte(0x2d);
lcd_write_byte(0x29);
lcd_write_byte(0x25);
lcd_write_byte(0x2B);
lcd_write_byte(0x39);
lcd_write_byte(0x00);
lcd_write_byte(0x01);
lcd_write_byte(0x03);
lcd_write_byte(0x10);
/* 2: Sparkles and rainbows, 16 args, no delay: */
lcd_write_command(LCD_GMCTRN1);
lcd_write_byte(0x03);
lcd_write_byte(0x1d);
lcd_write_byte(0x07);
lcd_write_byte(0x06);
lcd_write_byte(0x2E);
lcd_write_byte(0x2C);
lcd_write_byte(0x29);
lcd_write_byte(0x2D);
lcd_write_byte(0x2E);
lcd_write_byte(0x2E);
lcd_write_byte(0x37);
lcd_write_byte(0x3F);
lcd_write_byte(0x00);
lcd_write_byte(0x00);
lcd_write_byte(0x02);
lcd_write_byte(0x10);
/* 3: Normal display on, no args, w/delay */
lcd_write_command(LCD_NORON);
_delay_ms(10);
/* 4: Main screen turn on, no args w/delay */
lcd_write_command(LCD_DISPON);
_delay_ms(100);
}
void lcd_write_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint32_t color) {
uint16_t c = lcd_rgb2c(color);
lcd_addr_window(x, y, (x + w), (y + h));
spi_write_word_array(c, (w + 1) * (h + 1));
}
#if (USE_SPI == 1)
#define LCD_DELTA_X 0
#define LCD_DELTA_Y 0
#else
#define LCD_DELTA_X 2
#define LCD_DELTA_Y 3
#endif
void lcd_addr_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
lcd_write_command(LCD_CASET);
lcd_write_word(x0 + LCD_DELTA_X);
lcd_write_word(x1 + LCD_DELTA_X);
lcd_write_command(LCD_RASET);
lcd_write_word(y0 + LCD_DELTA_Y);
lcd_write_word(y1 + LCD_DELTA_Y);
lcd_write_command(LCD_RAMWR);
}
void lcd_draw_pixel(uint8_t x, uint8_t y, uint32_t color) {
lcd_addr_window(x, y, x, y);
uint16_t c = lcd_rgb2c(color);
lcd_write_word(c);
}
#define swap(a, b) { int16_t t = a; a = b; b = t; }
void lcd_draw_line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint32_t color) {
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1)
ystep = 1;
else
ystep = -1;
while (x0 <= x1) {
if (steep) {
lcd_draw_pixel(y0, x0, color);
} else {
lcd_draw_pixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
x0++;
}
}
void lcd_draw_vline(int16_t x, int16_t y, int16_t l, uint32_t color) {
lcd_write_rect(x, y, (l - 1), 0, color);
}
void lcd_draw_hline(int16_t x, int16_t y, int16_t l, uint32_t color) {
lcd_write_rect(x, y, 0, (l - 1), color);
}
void lcd_draw_rect(uint16_t x1, uint16_t y1, uint16_t w, uint16_t h, uint32_t color) {
lcd_draw_hline(x1, y1, w, color);
lcd_draw_vline(x1, y1, h, color);
lcd_draw_hline((x1 + h) - 0, y1, w, color);
lcd_draw_vline(x1, (y1 + w) - 0, h, color);
}
uint16_t lcd_rgb2c(uint32_t c) {
#if 0
return (uint16_t)
(((c & 0xF80000) >> 19) |
((c & 0x00FC00 ) >> 5) |
((c & 0x0000F8) << 8));
#else
return (uint16_t)
(((c & 0xF80000) >> 11) |
((c & 0x00FC00 ) >> 5) |
((c & 0x0000F8) >> 3));
#endif
}
void lcd_clear(void) {
lcd_write_rect(0, 0, LCD_TFTWIDTH, LCD_TFTHEIGHT, LCD_BLACK);
}
void lcd_draw_char(uint16_t xbase, uint16_t ybase, font_t *font, uint8_t c) {
if (c < font->start || c > (font->start + font->length))
c = ' ';
c = c - font->start;
lcd_addr_window(xbase, ybase, xbase + font->height - 1, ybase + font->width - 1);
for (uint8_t w = font->width; w > 0; w--) {
for (uint8_t h = font->height; h > 0; h--) {
if ((font->bitmap[(c) * font->height + (h - 1)]) & (1 << (w - 1)))
lcd_write_word(0xffff);
else
lcd_write_word(0x0000);
}
}
}
/* EOF */