Plant Monitor SAM3X8E
Loading...
Searching...
No Matches
st7789.c
Go to the documentation of this file.
1
9
10#include "drivers/st7789.h"
11
12static const spi_cfg_t st7789_cfg =
13{
14 .role = SPI_MASTER,
15 .mode = SPI_MODE_0,
16 .select_mode = SPI_FIXED_SELECT,
17 .chip_select = 0,
18 .bits = SPI_BITS_8,
19 .mck_divisor = 2
20};
21
28static void st7789_reset(void)
29{
30 /* Assert RESX */
31 tc_delay_ms(64);
33 tc_delay_ms(64);
35 tc_delay_ms(64);
36}
37
46static void st7789_write_cmd(const uint8_t cmd)
47{
50 spi_transfer(&cmd, NULL, 1, st7789_cfg.bits);
52}
53
63static void st7789_write_data(const uint8_t* data, size_t len)
64{
67 spi_transfer(data, NULL, len, st7789_cfg.bits);
69}
70
82static void st7789_set_window(uint16_t xs, uint16_t xe, uint16_t ys, uint16_t ye)
83{
84 uint8_t caset_data[] =
85 {
86 xs >> 8u,
87 xs & 0xFF,
88 xe >> 8u,
89 xe & 0xFF
90 };
91 uint8_t raset_data[] =
92 {
93 ys >> 8u,
94 ys & 0xFF,
95 ye >> 8u,
96 ye & 0xFF
97 };
98
99 st7789_write_cmd(CASET);
100 st7789_write_data(caset_data, sizeof(caset_data));
101
102 st7789_write_cmd(RASET);
103 st7789_write_data(raset_data, sizeof(raset_data));
104
105 st7789_write_cmd(RAMWR);
106}
107
108void st7789_init(void)
109{
110 /* Initialize timer counter */
111 tc_init();
112
113 /* Disable watchdog timer */
114 wdt_disable();
115
116 /* Initialize SPI */
117 spi_init(&st7789_cfg);
118
119 /* Initialize GPIO */
123
124 /* Perform HW reset */
125 st7789_reset();
126
127 /* Perform SW reset */
128 st7789_write_cmd(SWRESET);
129 tc_delay_ms(5);
130
131 /* Configure row/column addressing order */
132 st7789_write_cmd(MADCTL);
133 st7789_write_data(madctl_init, sizeof(madctl_init));
134
135 st7789_write_cmd(COLMOD);
136 st7789_write_data(colmod_init, sizeof(colmod_init));
137
138 /* Turn on inversion */
139 st7789_write_cmd(INVON);
140
141 /* Column & row address windows */
142 st7789_write_cmd(CASET);
143 st7789_write_data(caset_init, sizeof(caset_init));
144 st7789_write_cmd(RASET);
145 st7789_write_data(raset_init, sizeof(raset_init));
146
147 /* Porch settings */
148 st7789_write_cmd(PORCTRL);
149 st7789_write_data(porctrl_init, sizeof(porctrl_init));
150
151 /* Gate control */
152 st7789_write_cmd(GCTRL);
153 st7789_write_data(gctrl_init, sizeof(gctrl_init));
154
155 /* Common electrode voltage across LCD panel */
156 st7789_write_cmd(VCOMS);
157 st7789_write_data(vcoms_init, sizeof(vcoms_init));
158
159 st7789_write_cmd(LCMCTRL);
160 st7789_write_data(lcmctrl_init, sizeof(lcmctrl_init));
161
162 st7789_write_cmd(VDVVRHEN);
163 st7789_write_data(vdvvrhen_init, sizeof(vdvvrhen_init));
164
165 st7789_write_cmd(VRHS);
166 st7789_write_data(vrhs_init, sizeof(vrhs_init));
167
168 st7789_write_cmd(VDVSET);
169 st7789_write_data(vdvset_init, sizeof(vdvset_init));
170
171 st7789_write_cmd(FRCTRL2);
172 st7789_write_data(frctrl2_init, sizeof(frctrl2_init));
173
174 st7789_write_cmd(PWCTRL1);
175 st7789_write_data(pwctrl1_init, sizeof(pwctrl1_init));
176
177 st7789_write_cmd(PVGAMCTRL);
178 st7789_write_data(pvgamctrl_init, sizeof(pvgamctrl_init));
179
180 st7789_write_cmd(NVGAMCTRL);
181 st7789_write_data(nvgamctrl_init, sizeof(nvgamctrl_init));
182
183 st7789_write_cmd(SLPOUT);
184 tc_delay_ms(120);
185
186 st7789_write_cmd(DISPON);
187 tc_delay_ms(20);
188}
189
190void st7789_clear(const uint16_t color)
191{
192 st7789_set_window(0, DISPLAY_WIDTH - 1, 0, DISPLAY_HEIGHT - 1);
193
194 uint8_t pixel_buffer[DISPLAY_WIDTH * 2u];
195
196 for (size_t i = 0; i < sizeof(pixel_buffer); i += 2 )
197 {
198 pixel_buffer[i] = color >> 8u;
199 pixel_buffer[i+1] = color & 0xFFu;
200 }
201
202 for (size_t j = 0; j < DISPLAY_HEIGHT; j++)
203 st7789_write_data(pixel_buffer, sizeof(pixel_buffer));
204}
205
206void st7789_draw_glyph(const uint16_t xs, const uint16_t ys, const uint16_t glyph_width, const uint16_t glyph_height, const uint16_t fg_color, const uint16_t bg_color, const uint8_t* bitmap_data)
207{
208 st7789_set_window(xs, (xs + glyph_width) - 1, ys, (ys + glyph_height) - 1);
209
210 uint8_t glyph_buffer[(glyph_width * glyph_height) * 2u];
211
212 for (size_t i = 0; i < glyph_height; i++)
213 {
214 for (size_t j = 0; j < glyph_width; j++)
215 {
216 uint16_t color = (bitmap_data[i] & (1u << (7u - j))) ? fg_color : bg_color;
217 size_t pixel_index = ((i * glyph_width) + j) * 2u;
218
219 glyph_buffer[pixel_index] = color >> 8u;
220 glyph_buffer[pixel_index + 1] = color & 0xFF;
221 }
222 }
223
224 st7789_write_data(glyph_buffer, sizeof(glyph_buffer));
225}
void pio_write(const pio_id_t pio_id, const uint32_t pin, const pio_state_t state)
Writes to GPIO pin.
Definition pio.c:42
void pio_init(const pio_id_t pio_id, const uint32_t pin)
Initializes GPIO pin.
Definition pio.c:12
@ PIO_SET
Definition pio.h:38
@ PIO_CLEAR
Definition pio.h:37
@ PIO_B
Definition pio.h:24
void spi_transfer(const void *tx, void *rx, size_t count, const spi_bits_t bits)
Performs a specified amount of SPI transfers.
Definition spi.c:76
void spi_init(const spi_cfg_t *cfg)
Initializes SPI0 peripheral of SAM3X8E.
Definition spi.c:13
@ SPI_FIXED_SELECT
Definition spi.h:40
@ SPI_MASTER
Definition spi.h:28
@ SPI_BITS_8
Definition spi.h:67
@ SPI_MODE_0
Definition spi.h:53
void st7789_init(void)
ST7789 initialization.
Definition st7789.c:108
void st7789_clear(const uint16_t color)
Clears display.
Definition st7789.c:190
void st7789_draw_glyph(const uint16_t xs, const uint16_t ys, const uint16_t glyph_width, const uint16_t glyph_height, const uint16_t fg_color, const uint16_t bg_color, const uint8_t *bitmap_data)
Draws a character glyph to the display.
Definition st7789.c:206
Interface for ST7789 driver.
#define VDVSET
Definition st7789.h:133
#define RAMWR
Definition st7789.h:95
#define RST_PIN
Definition st7789.h:31
#define PORCTRL
Definition st7789.h:124
#define COLMOD
Definition st7789.h:105
#define DC_PIN
Definition st7789.h:30
#define PVGAMCTRL
Definition st7789.h:143
#define DISPLAY_HEIGHT
Definition st7789.h:26
#define DISPLAY_WIDTH
Definition st7789.h:23
#define FRCTRL2
Definition st7789.h:135
#define PWCTRL1
Definition st7789.h:140
#define DISPON
Definition st7789.h:92
#define SLPOUT
Definition st7789.h:85
#define VCOMS
Definition st7789.h:128
#define MADCTL
Definition st7789.h:101
#define LCMCTRL
Definition st7789.h:129
#define VRHS
Definition st7789.h:132
#define CASET
Definition st7789.h:93
#define NVGAMCTRL
Definition st7789.h:144
#define INVON
Definition st7789.h:89
#define CS_PIN
Definition st7789.h:29
#define RASET
Definition st7789.h:94
#define VDVVRHEN
Definition st7789.h:131
#define GCTRL
Definition st7789.h:126
#define SWRESET
Definition st7789.h:75
SPI peripheral configuration parameters.
Definition spi.h:85
void tc_delay_ms(uint32_t ms)
Creates a hardware delay in milliseconds.
Definition tc.c:32
void tc_init(void)
Initializes TC0->Channel0.
Definition tc.c:12
void wdt_disable(void)
Disables SAM3X8E watchdog timer.
Definition tc.c:41