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.
17 #include "ao_ads131a0x.h"
25 #define PRINTD(l, ...) do { if (DEBUG & (l)) { printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } } while(0)
30 struct ao_ads131a0x_sample ao_ads131a0x_current;
32 uint8_t ao_ads131a0x_drdy;
35 ao_ads131a0x_start(void) {
36 ao_spi_get_bit(AO_ADS131A0X_SPI_CS_PORT,
37 AO_ADS131A0X_SPI_CS_PIN,
39 AO_ADS131A0X_SPI_SPEED);
43 ao_ads131a0x_stop(void) {
44 ao_spi_put_bit(AO_ADS131A0X_SPI_CS_PORT,
45 AO_ADS131A0X_SPI_CS_PIN,
46 AO_ADS131A0X_SPI_BUS);
50 ao_ads131a0x_reg_read(uint8_t addr)
54 d[0] = addr | (AO_ADS131A0X_RREG >> 8);
70 ao_spi_send(d, 15, AO_ADS131A0X_SPI_BUS);
73 /* data isn't actually returned until the next frame */
74 d[0] = AO_ADS131A0X_NULL >> 8;
75 d[1] = AO_ADS131A0X_NULL & 0xff;
77 ao_spi_duplex(d, d, 15, AO_ADS131A0X_SPI_BUS);
80 /* first byte is register number, second byte is the data */
81 PRINTD(DEBUG_LOW, "read %x = %x\n", addr, d[1]);
88 ao_ads131a0x_reg_write(uint8_t addr, uint8_t value)
92 PRINTD(DEBUG_LOW, "write %x %x\n", addr, value);
93 d[0] = addr | (AO_ADS131A0X_WREG >> 8);
108 ao_ads131a0x_start();
109 ao_spi_send(d, 15, AO_ADS131A0X_SPI_BUS);
115 ao_ads131a0x_isr(void)
117 ao_ads131a0x_drdy = 1;
118 ao_wakeup(&ao_ads131a0x_drdy);
122 ao_ads131a0x_setup(void)
128 ao_gpio_set(AO_ADS131A0X_RESET_PORT, AO_ADS131A0X_RESET_PIN, 1);
132 /* send unlock command? why, if we've not locked it? */
134 uint8_t devid = ao_ads131a0x_reg_read(AO_ADS131A0X_ID_MSB);
135 if (devid != AO_ADS131A0X_ID_ADS131A04)
136 ao_panic(AO_PANIC_SELF_TEST_ADS);
138 ao_exti_setup(AO_ADS131A0X_DRDY_PORT, AO_ADS131A0X_DRDY_PIN,
139 AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_HIGH,
142 /* register writes use top 16 bits of each 24-bit word */
144 d[0] = AO_ADS131A0X_A_SYS_CFG | (AO_ADS131A0X_WREGS >> 8);
145 d[1] = 8; /* write (n-1) registers starting with A_SYS_CFG */
148 d[3] = 0x78; /* A_SYS_CFG - charge pump off, high res, 4.0V ref,
149 internal ref on, 5/95% fault thresholds */
150 d[4] = 0x00; /* D_SYS_CFG - watchdog off, crc on device words,
151 shortest done and hi-z delays, non-fixed size,
155 d[6] = 0x02; /* CLK1 - crystal osc on, ICLK = CLKIN / 2 */
156 d[7] = 0x20; /* CLK2 - MOD = ICLK / 2, DATA = MOD / 4096 */
159 d[9] = 0x0f; /* ADC_ENA - all channels powered up */
160 d[10] = 0; /* reserved */
163 d[12] = 0x00; /* ADC1 - gain = 1 */
164 d[13] = 0x00; /* ADC2 - gain = 1 */
167 d[15] = 0x00; /* ADC3 - gain = 1 */
168 d[16] = 0x00; /* ADC4 - gain = 1 */
171 ao_ads131a0x_start();
172 ao_spi_send(d, 18, AO_ADS131A0X_SPI_BUS);
175 /* start conversions */
177 d[0] = AO_ADS131A0X_WAKEUP >> 8;
178 d[1] = AO_ADS131A0X_WAKEUP & 0xff;
180 ao_ads131a0x_start();
181 ao_spi_send(d, 3, AO_ADS131A0X_SPI_BUS);
184 /* documented initialization sends the LOCK command here, but I can
185 see no value in doing that! */
193 ao_ads131a0x_setup();
195 ao_exti_enable(AO_ADS131A0X_DRDY_PORT, AO_ADS131A0X_DRDY_PIN);
198 ao_arch_block_interrupts();
199 ao_ads131a0x_drdy = 0;
200 while (ao_ads131a0x_drdy == 0)
201 ao_sleep(&ao_ads131a0x_drdy);
202 ao_arch_release_interrupts();
204 d[0] = AO_ADS131A0X_NULL >> 8;
205 d[1] = AO_ADS131A0X_NULL & 0xff;
220 ao_ads131a0x_start();
221 ao_spi_duplex(d, d, 15, AO_ADS131A0X_SPI_BUS);
224 ao_ads131a0x_current.ain[0] =
225 d[3] << 16 | d[4] << 8 | d[5];
226 ao_ads131a0x_current.ain[1] =
227 d[6] << 16 | d[7] << 8 | d[8];
228 ao_ads131a0x_current.ain[2] =
229 d[9] << 16 | d[10] << 8 | d[11];
230 ao_ads131a0x_current.ain[3] =
231 d[12] << 16 | d[13] << 8 | d[14];
234 // we need to log this data somewhere
236 ao_ads131a0x_drdy = 0;
240 static struct ao_task ao_ads131a0x_task;
243 ao_ads131a0x_dump(void)
249 ao_add_task(&ao_ads131a0x_task, ao_ads131a0x, "ads131a0x");
252 printf ("ADS131A0X value %d %d %d %d\n",
253 ao_ads131a0x_current.ain[0],
254 ao_ads131a0x_current.ain[1],
255 ao_ads131a0x_current.ain[2],
256 ao_ads131a0x_current.ain[3]);
259 const struct ao_cmds ao_ads131a0x_cmds[] = {
260 { ao_ads131a0x_dump, "I\0Display ADS131A0X data" },
265 ao_ads131a0x_init(void)
267 ao_cmd_register(ao_ads131a0x_cmds);
269 ao_enable_output(AO_ADS131A0X_RESET_PORT, AO_ADS131A0X_RESET_PIN, 0);
271 ao_spi_init_cs(AO_ADS131A0X_SPI_CS_PORT,
272 (1 << AO_ADS131A0X_SPI_CS_PIN));
274 // ao_add_task(&ao_ads131a0x_task, ao_ads131a0x, "ads131a0x");