0b83ab2cbd3d88bffe5aafe00b5babcc5c891dae
[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_DECODE_MODE, AO_AS1107_DECODE);
48                 _ao_as1107_cmd(AO_AS1107_SCAN_LIMIT, AO_AS1107_NUM_DIGITS - 1);
49                 _ao_as1107_cmd(AO_AS1107_FEATURE,
50                                (0 << AO_AS1107_FEATURE_CLK_EN) |
51                                (0 << AO_AS1107_FEATURE_REG_RES) |
52                                (1 << AO_AS1107_FEATURE_DECODE_SEL) |
53                                (1 << AO_AS1107_FEATURE_SPI_EN) |
54                                (0 << AO_AS1107_FEATURE_BLINK_EN) |
55                                (0 << AO_AS1107_FEATURE_BLINK_FREQ) |
56                                (0 << AO_AS1107_FEATURE_SYNC) |
57                                (0 << AO_AS1107_FEATURE_BLINK_START));
58         }
59 }
60
61 void
62 ao_as1107_write(uint8_t start, uint8_t count, uint8_t *values)
63 {
64         uint8_t i;
65         ao_mutex_get(&as1107_mutex);
66         _ao_as1107_setup();
67         for (i = 0; i < count; i++)
68         {
69                 _ao_as1107_cmd(AO_AS1107_DIGIT(start + i),
70                                values[i]);
71         }
72         ao_mutex_put(&as1107_mutex);
73 }
74
75 void
76 ao_as1107_write_8(uint8_t start, uint8_t value)
77 {
78         uint8_t values[2];
79
80         values[0] = (value >> 4);
81         values[1] = value & 0xf;
82         ao_as1107_write(start, 2, values);
83 }
84
85 void
86 ao_as1107_write_16(uint8_t start, uint16_t value)
87 {
88         uint8_t values[4];
89
90         values[0] = (value >> 12);
91         values[1] = (value >> 8) & 0xf;
92         values[2] = (value >> 4) & 0xf;
93         values[3] = (value) & 0xf;
94         ao_as1107_write(start, 4, values);
95 }
96
97 void
98 ao_as1107_init(void)
99 {
100         as1107_configured = 0;
101         ao_spi_init_cs(AO_AS1107_CS_PORT, (1 << AO_AS1107_CS_PIN));
102 }