2 * Copyright © 2019 Bdale Garbee <bdale@gag.com>
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.
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.
16 #include "ao_ads124s0x.h"
24 #define PRINTD(l, ...) do { if (DEBUG & (l)) { printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } } while(0)
29 struct ao_ads124s0x_sample ao_ads124s0x_current;
32 ao_ads124s0x_start(void) {
33 ao_spi_get_bit(AO_ADS124S0X_SPI_CS_PORT,
34 AO_ADS124S0X_SPI_CS_PIN,
36 AO_ADS124S0X_SPI_SPEED);
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);
48 ao_ads124s0x_reg_read(uint8_t addr)
52 d[0] = addr | AO_ADS124S0X_RREG;
55 ao_spi_duplex(d, d, 2, AO_ADS124S0X_SPI_BUS);
58 PRINTD(DEBUG_LOW, "read %x = %x\n", addr, d);
64 ao_ads124s0x_reg_write(uint8_t addr, uint8_t value)
68 PRINTD(DEBUG_LOW, "write %x %x\n", addr, value);
69 d[0] = addr | AO_ADS124S0X_WREG;
73 ao_spi_send(d, 3, AO_ADS124S0X_SPI_BUS);
77 d[0] = addr | AO_ADS124S0X_RREG
80 ao_spi_duplex(d, d, 2, AO_ADS124S0X_SPI_BUS);
82 PRINTD(DEBUG_LOW, "readback %x %x\n", d[0], d[1]);
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?
93 ao_ads124s0x_setup(void)
97 /* we have nowhere to report this error to since ao_sensor_errors is
98 normally part of ao_flight?
100 uint8_t devid = ao_ads124s0x_reg_read(AO_ADS124S0X_ID);
101 if (devid != AO_ADS124S0X_ID_ADS124S06)
102 ao_sensor_errors = 1;
105 /* 1ksps each across 4 inputs using full duplex ala 9.5.4.3 */
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);
125 /* start conversions */
127 d[0] = AO_ADS124S0X_START;
128 ao_ads124s0x_start();
129 ao_spi_send(d, 1, AO_ADS124S0X_SPI_BUS);
136 ao_ads124s0x_setup();
139 ao_ads124s0x_value(&ao_ads124s0x_current);
141 AO_DATA_PRESENT(AO_DATA_ADS124S0X);
148 static struct ao_task ao_ads124s0x_task;
151 ao_ads124s0x_dump(void) // FIXME
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);
160 const struct ao_cmds ao_ads124s0x_cmds[] = {
161 { ao_ads124s0x_dump, "I\0Display ADS124S0X data" },
166 ao_ads124s0x_init(void)
168 ao_cmd_register(ao_ads124s0x_cmds);
169 ao_spi_init_cs(AO_ADS124S0X_SPI_CS_PORT,
170 (1 << AO_ADS124S0X_SPI_CS_PIN));
172 ao_add_task(&ao_ads124s0x_task, ao_ads124s0x, "ads124s0x");