altos: first cut at ADS124S0X driver interrupt handling
[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_exti.h>
17 #include "ao_ads124s0x.h"
18
19 #define DEBUG_LOW       1
20 #define DEBUG_HIGH      2
21
22 #define DEBUG           0
23
24 #if DEBUG
25 #define PRINTD(l, ...) do { if (DEBUG & (l)) { printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } } while(0)
26 #else
27 #define PRINTD(l,...)
28 #endif
29
30 struct ao_ads124s0x_sample      ao_ads124s0x_current;
31 uint8_t         nextchan = 0;
32 uint8_t         ao_ads124s0x_drdy;
33
34 static void
35 ao_ads124s0x_start(void) {
36         ao_spi_get_bit(AO_ADS124S0X_SPI_CS_PORT,
37                        AO_ADS124S0X_SPI_CS_PIN,
38                        AO_ADS124S0X_SPI_BUS,
39                        AO_ADS124S0X_SPI_SPEED);
40 }
41
42 static void
43 ao_ads124s0x_stop(void) {
44         ao_spi_put_bit(AO_ADS124S0X_SPI_CS_PORT,
45                        AO_ADS124S0X_SPI_CS_PIN,
46                        AO_ADS124S0X_SPI_BUS);
47 }
48
49 /*
50 static uint8_t
51 ao_ads124s0x_reg_read(uint8_t addr)
52 {
53         uint8_t d[2];
54
55         d[0] = addr | AO_ADS124S0X_RREG;
56         d[1] = 0;                       
57         ao_ads124s0x_start();
58         ao_spi_duplex(d, d, 2, AO_ADS124S0X_SPI_BUS);
59         ao_ads124s0x_stop();
60
61         PRINTD(DEBUG_LOW, "read %x = %x\n", addr, d);
62
63         return d[1];
64 }
65
66 static void
67 ao_ads124s0x_reg_write(uint8_t addr, uint8_t value)
68 {
69         uint8_t d[3];
70
71         PRINTD(DEBUG_LOW, "write %x %x\n", addr, value);
72         d[0] = addr | AO_ADS124S0X_WREG;
73         d[1] = 0;                       
74         d[2] = value;
75         ao_ads124s0x_start();
76         ao_spi_send(d, 3, AO_ADS124S0X_SPI_BUS);
77         ao_ads124s0x_stop();
78
79 #if DEBUG & DEBUG_LOW
80         d[0] = addr | AO_ADS124S0X_RREG
81         d[1] = 0;
82         ao_ads124s0x_start();
83         ao_spi_duplex(d, d, 2, AO_ADS124S0X_SPI_BUS);
84         ao_ads124s0x_stop();
85         PRINTD(DEBUG_LOW, "readback %x %x\n", d[0], d[1]);
86 #endif
87 }
88 */
89
90 // FIXME 
91 //      We need to be in continuous conversion mode, and use the WREG
92 //      command to set the next conversion input while reading each 
93 //      which I don't see an example for elsewhere?
94
95 static void
96 ao_ads124s0x_setup(void)
97 {
98         uint8_t d[20];
99
100         uint8_t devid = ao_ads124s0x_reg_read(AO_ADS124S0X_ID);
101         if (devid != AO_ADS124S0X_ID_ADS124S06)
102                 ao_panic(AO_PANIC_SELF_TEST_ADS124S0X);
103
104         ao_exti_setup(AO_ADS124S0x_DRDY_PORT, AO_ADS124S0X_DRDY_PIN,
105                 AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_HIGH,
106                 ao_ads124s0x_isr);
107
108         /* run converter at 4ksps so we can scan 4 channels at 1ksps using
109            full duplex ala datasheet section 9.5.4.3 */
110
111         d[0] = AO_ADS124S0X_INPMUX | AO_ADS124S0X_WREG;
112         d[1] = 8;       /* write 8 registers starting with INPMUX */
113         d[2] = 0x0c;    /* input mux AIN0 relative to AINCOM */
114         d[3] = 0x00;    /* default first conversion delay, pga disabled */
115         d[4] = 0x1e;    /* gchop disabled, internal clock, continuous 
116                            conversion, low-latency filter, 4000 SPS */
117         d[5] = 0x00;    /* ref monitor disabled, ref buffers bypassed, ref 
118                            set to REFP0/REFN0, internal reference off */
119         d[6] = 0x00;    /* pga otuput rail, low side power switch, excitation
120                            current source all off */
121         d[7] = 0xff;    /* idac1 and idac2 disconnected */
122         d[8] = 0x00;    /* all vbias disconnected */
123         d[9] = 0x10;    /* sys monitor off, spi timeout disabled, crc disabled,
124                            prepending status byte disabled */
125         ao_ads124s0x_start();
126         ao_spi_send(d, 10, AO_ADS124S0X_SPI_BUS);
127         ao_ads124s0x_stop();
128
129         /* start conversions */
130         
131         d[0] = AO_ADS124S0X_START;
132         ao_ads124s0x_start();
133         ao_spi_send(d, 1, AO_ADS124S0X_SPI_BUS);
134         ao_ads124s0x_stop();
135 }
136
137 static void
138 ao_ads124s0x(void)
139 {
140         uint8_t d[3], curchan;
141
142         ao_ads124s0x_setup();
143
144         ao_exti_enable(AO_ADS124S0X_DRDY_PORT, AO_ADS124S0X_DRDY_PIN);
145
146         for (;;) {
147                 ao_sleep(&ao_ads124s0x_drdy);
148
149                 curchan = nextchan;
150                 nextchan = (nextchan + 1) % AO_ADS124S0X_CHANNELS;
151
152                 d[0] = AO_ADS124S0X_INPMUX | AO_ADS124S0X_WREG;
153                 d[1] = 1;                       
154                 d[2] = nextchan << 4 | 0x0c; ;  /* relative to AINCOM */
155                 ao_ads124s0x_start();
156                 ao_spi_duplex(d, d, 3, AO_ADS124S0X_SPI_BUS);
157                 ao_ads124s0x_stop();
158
159                 ao_ads124s0x_current.ain[curchan] = 
160                         d[0] << 16 | d[1] << 8 | d[2];
161
162                 // FIXME
163                 //      If nextchan == 0, we have a complete set of inputs
164                 //      and we need to log them somewhere
165
166         }
167 }
168
169 void ao_ads124s0x_isr(void)
170 {
171         ao_wakeup(&ao_ads124s0x_drdy);
172 }
173
174 static struct ao_task ao_ads124s0x_task;
175
176 static void
177 ao_ads124s0x_dump(void)                 // FIXME
178 {
179         printf ("ADS124S0X value %d %d %d %d\n",
180                 ao_ads124s0x_current.ain[0],
181                 ao_ads124s0x_current.ain[1],
182                 ao_ads124s0x_current.ain[2],
183                 ao_ads124s0x_current.ain[3]);
184 }
185
186 const struct ao_cmds ao_ads124s0x_cmds[] = {
187         { ao_ads124s0x_dump,    "I\0Display ADS124S0X data" },
188         { 0, NULL },
189 };
190
191 void
192 ao_ads124s0x_init(void)
193 {
194         ao_cmd_register(ao_ads124s0x_cmds);
195
196         ao_spi_init_cs(AO_ADS124S0X_SPI_CS_PORT, 
197                 (1 << AO_ADS124S0X_SPI_CS_PIN));
198
199         ao_add_task(&ao_ads124s0x_task, ao_ads124s0x, "ads124s0x");
200 }