]> git.gag.com Git - fw/altos/blob - src/drivers/ao_as1107.c
doc: Update TeleMini v3 photos to production unit
[fw/altos] / src / drivers / ao_as1107.c
1 /*
2  * Copyright © 2017 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
15 #include <ao.h>
16 #include <ao_as1107.h>
17
18 static uint8_t  as1107_configured;
19 static uint8_t  as1107_mutex;
20
21 static void
22 ao_as1107_start(void) {
23         ao_spi_get_bit(AO_AS1107_CS_PORT, AO_AS1107_CS_PIN, AO_AS1107_CS, AO_AS1107_SPI_INDEX, AO_AS1107_SPI_SPEED);
24 }
25
26 static void
27 ao_as1107_stop(void) {
28         ao_spi_put_bit(AO_AS1107_CS_PORT, AO_AS1107_CS_PIN, AO_AS1107_CS, AO_AS1107_SPI_INDEX);
29 }
30
31 static void
32 _ao_as1107_cmd(uint8_t addr, uint8_t value)
33 {
34         uint8_t packet[2] = { addr, value };
35
36         ao_as1107_start();
37         ao_spi_send(packet, 2, AO_AS1107_SPI_INDEX);
38         ao_as1107_stop();
39 }
40
41 static void
42 _ao_as1107_setup(void)
43 {
44         if (!as1107_configured) {
45                 as1107_configured = 1;
46                 _ao_as1107_cmd(AO_AS1107_SHUTDOWN, AO_AS1107_SHUTDOWN_SHUTDOWN_RESET);
47                 _ao_as1107_cmd(AO_AS1107_SHUTDOWN, AO_AS1107_SHUTDOWN_SHUTDOWN_NOP);
48                 _ao_as1107_cmd(AO_AS1107_DECODE_MODE, AO_AS1107_DECODE);
49                 _ao_as1107_cmd(AO_AS1107_SCAN_LIMIT, AO_AS1107_NUM_DIGITS - 1);
50                 _ao_as1107_cmd(AO_AS1107_INTENSITY, 0x0f);
51                 _ao_as1107_cmd(AO_AS1107_FEATURE,
52                                (0 << AO_AS1107_FEATURE_CLK_EN) |
53                                (0 << AO_AS1107_FEATURE_REG_RES) |
54                                (1 << AO_AS1107_FEATURE_DECODE_SEL) |
55                                (1 << AO_AS1107_FEATURE_SPI_EN) |
56                                (0 << AO_AS1107_FEATURE_BLINK_EN) |
57                                (0 << AO_AS1107_FEATURE_BLINK_FREQ) |
58                                (0 << AO_AS1107_FEATURE_SYNC) |
59                                (0 << AO_AS1107_FEATURE_BLINK_START));
60                 _ao_as1107_cmd(AO_AS1107_SHUTDOWN, AO_AS1107_SHUTDOWN_NORMAL_NOP);
61         }
62 }
63
64 void
65 ao_as1107_write(uint8_t start, uint8_t count, uint8_t *values)
66 {
67         uint8_t i;
68         ao_mutex_get(&as1107_mutex);
69         _ao_as1107_setup();
70         for (i = 0; i < count; i++)
71         {
72                 _ao_as1107_cmd(AO_AS1107_DIGIT(start + i),
73                                values[i]);
74         }
75         ao_mutex_put(&as1107_mutex);
76 }
77
78 void
79 ao_as1107_write_8(uint8_t start, uint8_t value)
80 {
81         uint8_t values[2];
82
83         values[0] = (value >> 4);
84         values[1] = value & 0xf;
85         ao_as1107_write(start, 2, values);
86 }
87
88 void
89 ao_as1107_write_16(uint8_t start, uint16_t value)
90 {
91         uint8_t values[4];
92
93         values[0] = (value >> 12);
94         values[1] = (value >> 8) & 0xf;
95         values[2] = (value >> 4) & 0xf;
96         values[3] = (value) & 0xf;
97         ao_as1107_write(start, 4, values);
98 }
99
100 void
101 ao_as1107_init(void)
102 {
103         as1107_configured = 0;
104         ao_spi_init_cs(AO_AS1107_CS_PORT, (1 << AO_AS1107_CS_PIN));
105 }