first cut at turnon scripts for EasyTimer v2
[fw/altos] / src / drivers / ao_st7565.c
1 /*
2  * Copyright © 2023 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_st7565.h>
21
22 static void
23 ao_st7565_reset(void)
24 {
25         ao_gpio_set(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 0);
26         ao_delay(AO_MS_TO_TICKS(100));
27         ao_gpio_set(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 1);
28         ao_delay(AO_MS_TO_TICKS(100));
29 }
30
31
32 static void
33 ao_st7565_start(uint8_t a0)
34 {
35         ao_gpio_set(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, a0);
36         ao_spi_get_bit(AO_ST7565_CS_PORT,
37                        AO_ST7565_CS_PIN,
38                        AO_ST7565_SPI_BUS,
39                        AO_ST7565_SPI_SPEED);
40 }
41
42 static void
43 ao_st7565_stop(void)
44 {
45         ao_spi_put_bit(AO_ST7565_CS_PORT,
46                        AO_ST7565_CS_PIN,
47                        AO_ST7565_SPI_BUS);
48         ao_gpio_set(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, 1);
49 }
50
51
52 static void
53 ao_st7565_instruction(uint8_t cmd)
54 {
55         ao_st7565_start(0);
56         ao_spi_send(&cmd, 1, AO_ST7565_SPI_BUS);
57         ao_st7565_stop();
58 }
59
60 static void
61 ao_st7565_instruction_param(uint8_t cmd, uint8_t param)
62 {
63         uint8_t b[2] = { cmd, param };
64         ao_st7565_start(0);
65         ao_spi_send(b, 2, AO_ST7565_SPI_BUS);
66         ao_st7565_stop();
67 }
68
69 static void
70 ao_st7565_instructions(const uint8_t *cmd, uint16_t len)
71 {
72         ao_st7565_start(0);
73         ao_spi_send(cmd, len, AO_ST7565_SPI_BUS);
74         ao_st7565_stop();
75 }
76
77 static void
78 ao_st7565_data(const void *base, uint16_t len)
79 {
80         ao_st7565_start(1);
81         ao_spi_send(base, len, AO_ST7565_SPI_BUS);
82         ao_st7565_stop();
83 }
84
85 static uint8_t brightness;
86
87 void
88 ao_st7565_set_brightness(uint8_t val)
89 {
90         if (val > 63)
91                 val = 63;
92         brightness = val;
93         ao_st7565_instruction_param(ST7565_ELECTRONIC_VOLUME_SET, val);
94 }
95
96 uint8_t
97 ao_st7565_get_brightness(void)
98 {
99         return brightness;
100 }
101
102 static bool setup_done;
103
104 static void
105 ao_st7565_setup(void)
106 {
107         static const uint8_t init[] = {
108                 /*
109                  * Should be set to one of ST7565_LCD_BIAS_1_9 or
110                  * ST7565_LCD_BIAS_1_7
111                  */
112                 AO_ST7565_BIAS,
113                 ST7565_ADC_SELECT_NORMAL,
114                 ST7565_COMMON_MODE_NORMAL,
115                 ST7565_DISPLAY_START_LINE_SET(0),
116                 ST7565_POWER_CONTROL_SET(0x4),
117         };
118
119         if (setup_done)
120                 return;
121         setup_done = true;
122         ao_st7565_reset();
123         ao_st7565_instructions(init, sizeof(init));
124         ao_delay(AO_MS_TO_TICKS(50));
125         ao_st7565_instruction(ST7565_POWER_CONTROL_SET(0x6));
126         ao_delay(AO_MS_TO_TICKS(50));
127         ao_st7565_instruction(ST7565_POWER_CONTROL_SET(0x7));
128         ao_delay(AO_MS_TO_TICKS(10));
129         ao_st7565_instruction(ST7565_RESISTOR_RATIO_SET(5));
130         ao_st7565_instruction(ST7565_DISPLAY_ON);
131         ao_st7565_set_brightness(13);
132 }
133
134 static uint8_t  rotbuf[AO_ST7565_WIDTH];
135
136 #define WIDTH   AO_ST7565_WIDTH
137 #define HEIGHT  AO_ST7565_HEIGHT
138 #define STRIDE  AO_BITMAP_STRIDE(WIDTH)
139
140 static uint32_t previous_image[STRIDE * HEIGHT];
141
142 void
143 ao_st7565_update(struct ao_bitmap *bitmap)
144 {
145         int16_t         col, c, page;
146         int16_t         row;
147         uint32_t        *line, *prev, *l;
148         uint32_t        bits;
149         uint8_t         *r;
150         int16_t         min_col, min_row, max_col, max_row;
151         int16_t         min_page, max_page;
152
153         ao_st7565_setup();
154
155         min_col = STRIDE - 1;
156         max_col = 0;
157         min_row = HEIGHT - 1;
158         max_row = 0;
159         line = bitmap->base;
160         prev = previous_image;
161         for (row = 0; row < HEIGHT; row++) {
162                 for (col = 0; col < STRIDE; col++) {
163                         bits = *line++;
164                         if (bits != *prev) {
165                                 *prev = bits;
166                                 if (row < min_row)
167                                         min_row = row;
168                                 if (row > max_row)
169                                         max_row = row;
170                                 if (col < min_col)
171                                         min_col = col;
172                                 if (col > max_col)
173                                         max_col = col;
174                         }
175                         prev++;
176                 }
177         }
178
179         if (min_col > max_col || min_row > max_row)
180                 return;
181
182         min_page = min_row >> 3;
183         max_page = max_row >> 3;
184         line = bitmap->base + min_page * 8 * STRIDE + min_col;
185
186         uint8_t first_col = (uint8_t) (min_col * 32);
187         uint8_t num_col = (uint8_t) (max_col + 1 - min_col) * 32;
188
189         for (page = min_page; page <= max_page; page++) {
190                 uint8_t         i[4] = {
191                         ST7565_PAGE_ADDRESS_SET(7-(uint8_t) page),
192                         ST7565_COLUMN_ADDRESS_SET_MSN(first_col >> 4),
193                         ST7565_COLUMN_ADDRESS_SET_LSN(first_col & 0xf),
194                         ST7565_RMW
195                 };
196                 memset(rotbuf, 0, num_col);
197                 for (row = 7; row >= 0; row--) {
198                         r = rotbuf;
199                         l = line;
200                         line += STRIDE;
201                         for (col = min_col; col <= max_col; col++) {
202                                 bits = ~*l++;
203                                 for (c = 0; c < 32; c++) {
204                                         *r++ |= ((bits >> c) & 1) << row;
205                                 }
206                         }
207                 }
208                 ao_st7565_instructions(i, 4);
209                 ao_st7565_data(rotbuf, num_col);
210         }
211 }
212
213 void
214 ao_st7565_init(void)
215 {
216         memset(previous_image, 0xff, sizeof(previous_image));
217         ao_enable_output(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 1);
218         ao_enable_output(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, 1);
219
220         ao_enable_cs(AO_ST7565_CS_PORT, AO_ST7565_CS_PIN);
221 }