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_ads1256.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_ads1256_sample ao_ads1256_current;
32 uint8_t ao_ads1256_drdy;
35 ao_ads1256_start(void) {
36 ao_spi_get_bit(AO_ADS1256_SPI_CS_PORT,
37 AO_ADS1256_SPI_CS_PIN,
39 AO_ADS1256_SPI_SPEED);
43 ao_ads1256_stop(void) {
44 ao_spi_put_bit(AO_ADS1256_SPI_CS_PORT,
45 AO_ADS1256_SPI_CS_PIN,
51 ao_ads1256_reg_read(uint8_t addr)
55 d[0] = addr | AO_ADS1256_RREG;
58 ao_spi_send(d, 2, AO_ADS1256_SPI_BUS);
60 ao_spi_recv(d, 1, AO_ADS1256_SPI_BUS);
63 PRINTD(DEBUG_LOW, "read %x = %x\n", addr, d[0]);
70 ao_ads1256_reg_write(uint8_t addr, uint8_t value)
74 PRINTD(DEBUG_LOW, "write %x %x\n", addr, value);
75 d[0] = addr | AO_ADS1256_WREG;
79 ao_spi_send(d, 3, AO_ADS1256_SPI_BUS);
89 ao_wakeup(&ao_ads1256_drdy);
93 ao_ads1256_setup(void)
99 /* set up interrupt on DRDY going low */
101 ao_exti_setup(AO_ADS1256_DRDY_PORT, AO_ADS1256_DRDY_PIN,
102 AO_EXTI_MODE_FALLING|AO_EXTI_PRIORITY_HIGH,
105 /* read status register and confirm device id matches */
107 uint8_t devid = ao_ads1256_reg_read(AO_ADS1256_STATUS) >> 4;
108 if (devid != AO_ADS1256_ID_ADS1256)
109 ao_panic(AO_PANIC_SELF_TEST_ADS);
111 /* write configuration registers, tell converter to start working */
113 d[0] = AO_ADS1256_STATUS | AO_ADS1256_WREG;
114 d[1] = 4; /* write 5 registers starting with STATUS */
115 d[2] = 0x00; /* msb first, auto-cal off, analog buffer disabled */
116 d[3] = 0x08; /* mux AIN0 relative to AINCOM */
117 d[4] = 0x20; /* clock out = fclkin, sensor detect off, pga gain 1 */
118 d[5] = 0xf0; /* data rate 7500 SPS */
119 d[6] = 0xf0; /* all gpio pins are inputs */
120 d[7] = AO_ADS1256_SYNC;
121 d[8] = AO_ADS1256_WAKEUP;
123 ao_spi_send(d, 9, AO_ADS1256_SPI_BUS);
130 uint8_t d[6], curchan;
134 ao_exti_enable(AO_ADS1256_DRDY_PORT, AO_ADS1256_DRDY_PIN);
137 ao_arch_block_interrupts();
139 while (ao_ads1256_drdy == 0)
140 ao_sleep(&ao_ads1256_drdy);
141 ao_arch_release_interrupts();
144 nextchan = (nextchan + 1) % AO_ADS1256_CHANNELS;
146 d[0] = AO_ADS1256_MUX | AO_ADS1256_WREG;
147 d[1] = 0; /* write one register */
148 d[2] = nextchan << 4 | 0x08; ; /* relative to AINCOM */
149 d[3] = AO_ADS1256_SYNC;
150 d[4] = AO_ADS1256_WAKEUP;
151 d[5] = AO_ADS1256_RDATA;
153 ao_spi_send(d, 6, AO_ADS1256_SPI_BUS);
154 ao_spi_recv(d, 3, AO_ADS1256_SPI_BUS);
157 ao_ads1256_current.ain[curchan] =
158 d[0] << 16 | d[1] << 8 | d[2];
161 // If nextchan == 0, we have a complete set of inputs
162 // and we need to log them somewhere
168 static struct ao_task ao_ads1256_task;
171 ao_ads1256_dump(void)
177 ao_add_task(&ao_ads1256_task, ao_ads1256, "ads1256");
180 printf ("ADS1256 value 0x%x 0x%x 0x%x 0x%x\n",
181 ao_ads1256_current.ain[0],
182 ao_ads1256_current.ain[1],
183 ao_ads1256_current.ain[2],
184 ao_ads1256_current.ain[3]);
187 const struct ao_cmds ao_ads1256_cmds[] = {
188 { ao_ads1256_dump, "D\0Display ADS1256 data" },
193 ao_ads1256_init(void)
195 ao_cmd_register(ao_ads1256_cmds);
197 // ao_enable_output(AO_ADS1256_RESET_PORT, AO_ADS1256_RESET_PIN, 0);
199 ao_spi_init_cs(AO_ADS1256_SPI_CS_PORT,
200 (1 << AO_ADS1256_SPI_CS_PIN));
202 // ao_add_task(&ao_ads1256_task, ao_ads1256, "ads1256");