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
442
443
444
445
446
447
|
/*
* Copyright © 2012 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include <ao.h>
#include <ao_lcd_stm.h>
#ifndef LCD_DEBUG
#define LCD_DEBUG 0
#endif
struct ao_lcd_segment {
uint8_t reg;
uint8_t bit;
};
#define A 0
#define B 1
#define C 2
#define D 3
#define E 4
static struct stm_gpio *gpios[] = {
&stm_gpioa,
&stm_gpiob,
&stm_gpioc,
&stm_gpiod,
&stm_gpioe
};
static inline int ao_lcd_stm_seg_enabled(int seg) {
if (seg < 32)
return (AO_LCD_STM_SEG_ENABLED_0 >> seg) & 1;
else
return (AO_LCD_STM_SEG_ENABLED_1 >> (seg - 32)) & 1;
}
static inline int ao_lcd_stm_com_enabled(int com) {
return (AO_LCD_STM_COM_ENABLED >> com) & 1;
}
#define AO_LCD_STM_GPIOA_SEGS_0 ( \
(1 << 0) | \
(1 << 1) | \
(1 << 2) | \
(1 << 3) | \
(1 << 4) | \
(1 << 17))
#define AO_LCD_STM_GPIOA_SEGS_1 0
#define AO_LCD_STM_USES_GPIOA (!!((AO_LCD_STM_GPIOA_SEGS_0 & AO_LCD_STM_SEG_ENABLED_0) | \
(AO_LCD_STM_GPIOA_SEGS_1 & AO_LCD_STM_SEG_ENABLED_1)))
#define AO_LCD_STM_GPIOB_SEGS_0 ( \
(1 << 5) | \
(1 << 6) | \
(1 << 7) | \
(1 << 8) | \
(1 << 9) | \
(1 << 10) | \
(1 << 11) | \
(1 << 12) | \
(1 << 13) | \
(1 << 14) | \
(1 << 15) | \
(1 << 16))
#define AO_LCD_STM_GPIOB_SEGS_1 0
#if AO_LCD_28_ON_C
#define AO_LCD_STM_GPIOC_28_SEGS ( \
(1 << 28) | \
(1 << 29) | \
(1 << 30))
#define AO_LCD_STM_GPIOD_28_SEGS ( \
(1 << 31))
#else
#define AO_LCD_STM_GPIOC_28_SEGS 0
#define AO_LCD_STM_GPIOD_28_SEGS ( \
(1 << 28) | \
(1 << 29) | \
(1 << 30) | \
(1 << 31))
#endif
#define AO_LCD_STM_GPIOC_SEGS_0 ( \
(1 << 18) | \
(1 << 19) | \
(1 << 20) | \
(1 << 21) | \
(1 << 22) | \
(1 << 23) | \
(1 << 24) | \
(1 << 25) | \
(1 << 26) | \
(1 << 27) | \
AO_LCD_STM_GPIOC_28_SEGS)
#define AO_LCD_STM_GPIOC_SEGS_1 ( \
(1 << (40 - 32)) | \
(1 << (41 - 32)) | \
(1 << (42 - 32)))
#define AO_LCD_STM_GPIOD_SEGS_0 ( \
AO_LCD_STM_GPIOD_28_SEGS)
#define AO_LCD_STM_GPIOD_SEGS_1 ( \
(1 << (32 - 32)) | \
(1 << (33 - 32)) | \
(1 << (34 - 32)) | \
(1 << (35 - 32)) | \
(1 << (43 - 32)))
#define AO_LCD_STM_GPIOE_SEGS_0 0
#define AO_LCD_STM_GPIOE_SEGS_1 ( \
(1 << (36 - 32)) | \
(1 << (37 - 32)) | \
(1 << (38 - 32)) | \
(1 << (39 - 32)))
#define AO_LCD_STM_USES_GPIOA (!!((AO_LCD_STM_GPIOA_SEGS_0 & AO_LCD_STM_SEG_ENABLED_0) | \
(AO_LCD_STM_GPIOA_SEGS_1 & AO_LCD_STM_SEG_ENABLED_1)))
#define AO_LCD_STM_USES_GPIOB (!!((AO_LCD_STM_GPIOB_SEGS_0 & AO_LCD_STM_SEG_ENABLED_0) | \
(AO_LCD_STM_GPIOB_SEGS_1 & AO_LCD_STM_SEG_ENABLED_1)))
#define AO_LCD_STM_USES_GPIOC (!!((AO_LCD_STM_GPIOC_SEGS_0 & AO_LCD_STM_SEG_ENABLED_0) | \
(AO_LCD_STM_GPIOC_SEGS_1 & AO_LCD_STM_SEG_ENABLED_1)))
#define AO_LCD_STM_USES_GPIOD (!!((AO_LCD_STM_GPIOD_SEGS_0 & AO_LCD_STM_SEG_ENABLED_0) | \
(AO_LCD_STM_GPIOD_SEGS_1 & AO_LCD_STM_SEG_ENABLED_1)))
#define AO_LCD_STM_USES_GPIOE (!!((AO_LCD_STM_GPIOE_SEGS_0 & AO_LCD_STM_SEG_ENABLED_0) | \
(AO_LCD_STM_GPIOE_SEGS_1 & AO_LCD_STM_SEG_ENABLED_1)))
static const struct ao_lcd_segment segs[] = {
{ A, 1 }, /* 0 */
{ A, 2 },
{ A, 3 },
{ A, 6 },
{ A, 7 }, /* 4 */
{ B, 0 },
{ B, 1 },
{ B, 3 },
{ B, 4 }, /* 8 */
{ B, 5 },
{ B, 10 },
{ B, 11 },
{ B, 12 }, /* 12 */
{ B, 13 },
{ B, 14 },
{ B, 15 },
{ B, 8 }, /* 16 */
{ A, 15 },
{ C, 0 },
{ C, 1 },
{ C, 2 }, /* 20 */
{ C, 3 },
{ C, 4 },
{ C, 5 },
{ C, 6 }, /* 24 */
{ C, 7 },
{ C, 8 },
{ C, 9 },
#if AO_LCD_28_ON_C
{ C, 10 }, /* 28 */
{ C, 11 },
{ C, 12 },
{ D, 2 },
#else
{ D, 8 }, /* 28 */
{ D, 9 },
{ D, 10 },
{ D, 11 },
#endif
{ D, 12 }, /* 32 */
{ D, 13 },
{ D, 14 },
{ D, 15 },
{ E, 0 }, /* 36 */
{ E, 1 },
{ E, 2 },
{ E, 3 },
{ C, 10 }, /* 40 */
{ C, 11 },
{ C, 12 },
{ D, 2 },
};
static const struct ao_lcd_segment coms[] = {
{ A, 8 }, /* 0 */
{ A, 9 }, /* 1 */
{ A, 10 }, /* 2 */
{ B, 9 }, /* 3 */
{ C, 10 }, /* 4 */
{ C, 11 }, /* 5 */
{ C, 12 }, /* 6 */
};
#define NSEG (sizeof segs/sizeof segs[0])
#define NCOM (sizeof coms/sizeof coms[0])
static uint8_t ao_lcd_update_active;
void
stm_lcd_isr(void)
{
if (stm_lcd.sr & (1 << STM_LCD_SR_UDD)) {
stm_lcd.clr = (1 << STM_LCD_CLR_UDDC);
if (ao_lcd_update_active) {
ao_lcd_update_active = 0;
ao_wakeup(&ao_lcd_update_active);
}
}
}
static void
ao_lcd_stm_fcr_sync(void)
{
while ((stm_lcd.sr & (1 << STM_LCD_SR_FCRSF)) == 0)
asm("nop");
}
void
ao_lcd_flush(void)
{
ao_arch_block_interrupts();
ao_lcd_update_active = 1;
stm_lcd.sr = (1 << STM_LCD_SR_UDR);
while (ao_lcd_update_active)
ao_sleep(&ao_lcd_update_active);
ao_arch_release_interrupts();
}
void
ao_lcd_clear(void)
{
uint8_t i;
for (i = 0; i < sizeof (stm_lcd.ram) / 4; i++)
stm_lcd.ram[i] = 0;
ao_lcd_flush();
}
#ifdef AO_SEGMENT_MAP
#if AO_LCD_PER_DIGIT
static const struct ao_lcd_map {
uint8_t com, seg;
} ao_lcd_map[AO_LCD_DIGITS * AO_LCD_SEGMENTS] = AO_SEGMENT_MAP;
#else
static const uint8_t ao_lcd_map[] = AO_SEGMENT_MAP;
#endif
#endif
void
ao_lcd_set(uint8_t digit, uint8_t segment, uint8_t value)
{
uint8_t n;
uint8_t com, seg;
#ifdef AO_SEGMENT_MAP
#if AO_LCD_PER_DIGIT
n = digit * AO_LCD_SEGMENTS + segment;
com = ao_lcd_map[n].com;
seg = ao_lcd_map[n].seg;
#else
com = digit;
seg = ao_lcd_map[segment];
#endif
#else
com = digit;
seg = segment;
#endif
if (com >= NCOM)
com = NCOM-1;
if (seg >= NSEG)
seg = NSEG-1;
#if LCD_DEBUG
printf ("digit %d segment %d -> com %d seg %d\n", digit, segment, com, seg);
#endif
n = (seg >> 5) & 1;
if (value)
stm_lcd.ram[com * 2 + n] |= (1 << (seg & 0x1f));
else
stm_lcd.ram[com * 2 + n] &= ~(1 << (seg & 0x1f));
}
#if LCD_DEBUG
static void
ao_lcd_stm_seg_set(void)
{
int com, seg, val;
int n, bit;
ao_cmd_decimal();
com = ao_cmd_lex_i;
ao_cmd_decimal();
seg = ao_cmd_lex_u32;
ao_cmd_decimal();
val = ao_cmd_lex_i;
printf ("com: %d seg: %d val: %d\n", com, seg, val);
ao_lcd_set(com, seg, val);
ao_lcd_flush();
}
static const struct ao_cmds ao_lcd_stm_cmds[] = {
{ ao_lcd_stm_seg_set, "s <com> <seg> <value>\0Set LCD segment" },
{ ao_lcd_clear, "x\0Clear LCD" },
{ 0, NULL },
};
#endif
void
ao_lcd_stm_init(void)
{
unsigned int s, c;
uint32_t csr;
stm_rcc.ahbenr |= ((AO_LCD_STM_USES_GPIOA << STM_RCC_AHBENR_GPIOAEN) |
(AO_LCD_STM_USES_GPIOB << STM_RCC_AHBENR_GPIOBEN) |
(AO_LCD_STM_USES_GPIOC << STM_RCC_AHBENR_GPIOCEN) |
(AO_LCD_STM_USES_GPIOD << STM_RCC_AHBENR_GPIODEN) |
(AO_LCD_STM_USES_GPIOE << STM_RCC_AHBENR_GPIOEEN));
stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_LCDEN);
/* Turn on the LSI clock */
if ((stm_rcc.csr & (1 << STM_RCC_CSR_LSIRDY)) == 0) {
stm_rcc.csr |= (1 << STM_RCC_CSR_LSION);
while ((stm_rcc.csr & (1 << STM_RCC_CSR_LSIRDY)) == 0)
asm("nop");
}
/* Enable RTC clock config (required to change the RTC/LCD clock */
stm_pwr.cr |= (1 << STM_PWR_CR_DBP);
/* Configure the LCDCLK - use the LSI clock */
csr = stm_rcc.csr;
csr &= ~(STM_RCC_CSR_RTCSEL_MASK << STM_RCC_CSR_RTCSEL);
csr |= (STM_RCC_CSR_RTCSEL_LSI << STM_RCC_CSR_RTCSEL);
stm_rcc.csr = csr;
for (s = 0; s < NSEG; s++) {
uint8_t reg = segs[s].reg;
uint8_t bit = segs[s].bit;
if (ao_lcd_stm_seg_enabled(s)) {
stm_moder_set(gpios[reg], bit, STM_MODER_ALTERNATE);
stm_afr_set(gpios[reg], bit, STM_AFR_AF11);
}
}
for (c = 0; c < NCOM; c++) {
uint8_t reg = coms[c].reg;
uint8_t bit = coms[c].bit;
if (ao_lcd_stm_com_enabled(c)) {
stm_moder_set(gpios[reg], bit, STM_MODER_ALTERNATE);
stm_afr_set(gpios[reg], bit, STM_AFR_AF11);
}
}
/* Disable the LCD */
stm_lcd.cr = 0;
/* duty cycle 1/3, radio 352, frame rate about 33Hz */
stm_lcd.fcr = ((STM_LCD_FCR_PS_8 << STM_LCD_FCR_PS) |
(STM_LCD_FCR_DIV_20 << STM_LCD_FCR_DIV) |
(7 << STM_LCD_FCR_CC) |
(0 << STM_LCD_FCR_DEAD) |
(1 << STM_LCD_FCR_PON) |
(1 << STM_LCD_FCR_UDDIE) |
(0 << STM_LCD_FCR_SOFIE) |
(1 << STM_LCD_FCR_HD));
ao_lcd_stm_fcr_sync();
/* Program desired DUTY in LCD_CR */
/* Program desired BIAS in LCD_CR */
/* Enable mux seg */
/* Internal voltage source */
stm_lcd.cr = ((AO_LCD_DUTY << STM_LCD_CR_DUTY) |
(STM_LCD_CR_BIAS_1_2 << STM_LCD_CR_BIAS) |
(0 << STM_LCD_CR_VSEL) |
(0 << STM_LCD_CR_MUX_SEG));
ao_lcd_stm_fcr_sync();
/* Enable the display (LCDEN bit in LCD_CR) */
stm_lcd.cr |= (1 << STM_LCD_CR_LCDEN);
while ((stm_lcd.sr & (1 << STM_LCD_SR_RDY)) == 0)
asm("nop");
/* Load initial data into LCD_RAM and set the
* UDR bit in the LCD_SR register */
/* Program desired frame rate (PS and DIV bits in LCD_FCR) */
/* Program the contrast (CC bits in LCD_FCR) */
/* Program optional features (BLINK, BLINKF, PON, DEAD, HD) */
/* Program the required interrupts */
stm_nvic_set_enable(STM_ISR_LCD_POS);
stm_nvic_set_priority(STM_ISR_LCD_POS, AO_STM_NVIC_LOW_PRIORITY);
/* All done */
#if LCD_DEBUG
ao_cmd_register(ao_lcd_stm_cmds);
#endif
}
|