altos/telefireone-v1.0: Track ao_led_init API change
[fw/altos] / src / drivers / ao_ads124s0x.c
1 /*
2  * Copyright © 2019 Bdale Garbee <bdale@gag.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_ads124s0x.h"
17
18 #define DEBUG_LOW       1
19 #define DEBUG_HIGH      2
20
21 #define DEBUG           0
22
23 #if DEBUG
24 #define PRINTD(l, ...) do { if (DEBUG & (l)) { printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } } while(0)
25 #else
26 #define PRINTD(l,...)
27 #endif
28
29 struct ao_ads124s0x_sample      ao_ads124s0x_current;
30
31 static void
32 ao_ads124s0x_start(void) {
33         ao_spi_get_bit(AO_ADS124S0X_SPI_CS_PORT,
34                        AO_ADS124S0X_SPI_CS_PIN,
35                        AO_ADS124S0X_SPI_BUS,
36                        AO_ADS124S0X_SPI_SPEED);
37 }
38
39 static void
40 ao_ads124s0x_stop(void) {
41         ao_spi_put_bit(AO_ADS124S0X_SPI_CS_PORT,
42                        AO_ADS124S0X_SPI_CS_PIN,
43                        AO_ADS124S0X_SPI_BUS);
44 }
45
46 /*
47 static uint8_t
48 ao_ads124s0x_reg_read(uint8_t addr)
49 {
50         uint8_t d[2];
51
52         d[0] = addr | AO_ADS124S0X_RREG;
53         d[1] = 0;                       
54         ao_ads124s0x_start();
55         ao_spi_duplex(d, d, 2, AO_ADS124S0X_SPI_BUS);
56         ao_ads124s0x_stop();
57
58         PRINTD(DEBUG_LOW, "read %x = %x\n", addr, d);
59
60         return d[1];
61 }
62
63 static void
64 ao_ads124s0x_reg_write(uint8_t addr, uint8_t value)
65 {
66         uint8_t d[3];
67
68         PRINTD(DEBUG_LOW, "write %x %x\n", addr, value);
69         d[0] = addr | AO_ADS124S0X_WREG;
70         d[1] = 0;                       
71         d[2] = value;
72         ao_ads124s0x_start();
73         ao_spi_send(d, 3, AO_ADS124S0X_SPI_BUS);
74         ao_ads124s0x_stop();
75
76 #if DEBUG & DEBUG_LOW
77         d[0] = addr | AO_ADS124S0X_RREG
78         d[1] = 0;
79         ao_ads124s0x_start();
80         ao_spi_duplex(d, d, 2, AO_ADS124S0X_SPI_BUS);
81         ao_ads124s0x_stop();
82         PRINTD(DEBUG_LOW, "readback %x %x\n", d[0], d[1]);
83 #endif
84 }
85 */
86
87 // FIXME 
88 //      We need to be in continuous conversion mode, and use the WREG
89 //      command to set the next conversion input while reading each 
90 //      which I don't see an example for elsewhere?
91
92 static void
93 ao_ads124s0x_setup(void)
94 {
95         uint8_t d[20];
96
97 /*      we have nowhere to report this error to since ao_sensor_errors is
98         normally part of ao_flight?
99
100         uint8_t devid = ao_ads124s0x_reg_read(AO_ADS124S0X_ID);
101         if (devid != AO_ADS124S0X_ID_ADS124S06)
102                 ao_sensor_errors = 1;
103 */
104
105         /* 1ksps each across 4 inputs using full duplex ala 9.5.4.3 */
106
107         d[0] = AO_ADS124S0X_INPMUX | AO_ADS124S0X_WREG;
108         d[1] = 8;       /* write 8 registers starting with INPMUX */
109         d[2] = 0x0c;    /* input mux AIN0 relative to AINCOM */
110         d[3] = 0x00;    /* default first conversion delay, pga disabled */
111         d[4] = 0x1e;    /* gchop disabled, internal clock, continuous 
112                            conversion, low-latency filter, 4000 SPS */
113         d[5] = 0x00;    /* ref monitor disabled, ref buffers bypassed, ref 
114                            set to REFP0/REFN0, internal reference off */
115         d[6] = 0x00;    /* pga otuput rail, low side power switch, excitation
116                            current source all off */
117         d[7] = 0xff;    /* idac1 and idac2 disconnected */
118         d[8] = 0x00;    /* all vbias disconnected */
119         d[9] = 0x10;    /* sys monitor off, spi timeout disabled, crc disabled,
120                            prepending status byte disabled */
121         ao_ads124s0x_start();
122         ao_spi_send(d, 10, AO_ADS124S0X_SPI_BUS);
123         ao_ads124s0x_stop();
124
125         /* start conversions */
126         
127         d[0] = AO_ADS124S0X_START;
128         ao_ads124s0x_start();
129         ao_spi_send(d, 1, AO_ADS124S0X_SPI_BUS);
130         ao_ads124s0x_stop();
131 }
132
133 static void
134 ao_ads124s0x(void)
135 {
136         ao_ads124s0x_setup();
137 /*
138         for (;;) {
139                 ao_ads124s0x_value(&ao_ads124s0x_current);
140                 ao_arch_critical(
141                         AO_DATA_PRESENT(AO_DATA_ADS124S0X);
142                         AO_DATA_WAIT();
143                         );
144         }
145 */
146 }
147
148 static struct ao_task ao_ads124s0x_task;
149
150 static void
151 ao_ads124s0x_dump(void)                 // FIXME
152 {
153         printf ("ADS124S0X value %d %d %d %d\n",
154                 ao_ads124s0x_current.ain0,
155                 ao_ads124s0x_current.ain1,
156                 ao_ads124s0x_current.ain2,
157                 ao_ads124s0x_current.ain3);
158 }
159
160 const struct ao_cmds ao_ads124s0x_cmds[] = {
161         { ao_ads124s0x_dump,    "I\0Display ADS124S0X data" },
162         { 0, NULL },
163 };
164
165 void
166 ao_ads124s0x_init(void)
167 {
168         ao_cmd_register(ao_ads124s0x_cmds);
169         ao_spi_init_cs(AO_ADS124S0X_SPI_CS_PORT, 
170                 (1 << AO_ADS124S0X_SPI_CS_PIN));
171
172         ao_add_task(&ao_ads124s0x_task, ao_ads124s0x, "ads124s0x");
173 }