4 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
9 * \brief TC520 ADC driver (implementation)
12 * \author Francesco Sacchi <batt@develer.com>
13 * \author Marco Benelli <marco@develer.com>
16 #include <drv/tc520.h>
17 #include <drv/timer.h>
21 #include <cfg/macros.h>
23 #include <cfg/compiler.h>
27 static Serial *spi_ser;
29 #define TC520_CONVERSION_TIMEOUT ms_to_ticks(1000)
30 #define INIT_LOAD_VALUE 0x00
33 * Start an AD conversion and return result.
34 * To start a conversion first we must pull down CE pin.
35 * The ADC starts a convertion and keeps the DV pin high until the end.
36 * At this point, we can read the conversion value by SPI.
37 * The convertion result is yield in 3 bytes.
43 * 5:0 | data bits 15:10
45 * Second byte: data 9:2
54 * So, to get the result we must shift and recompose the bits.
55 * \Note Ovverrange bit is handled as the 17th data bit.
57 tc520_data_t tc520_read(void)
59 /* Start convertion and wait */
61 ticks_t start = timer_clock();
65 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
69 return TC520_MAX_VALUE;
74 start = timer_clock();
78 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
82 return TC520_MAX_VALUE;
87 /* Ok, convertion finished, read result */
91 /* RX buffer could be dirty...*/
95 uint8_t buf[3] = "\x0\x0\x0";
97 /* Dummy write to activate recv */
98 ser_write(spi_ser, buf, sizeof(buf));
103 ASSERT(ser_read(spi_ser, buf, sizeof(buf)) == sizeof(buf));
108 res = (((tc520_data_t)(buf[0] & 0x3F)) << 10) | (((tc520_data_t)buf[1]) << 2) | (((tc520_data_t)buf[2]) >> 6);
110 #define OVERRANGE_BIT BV(7)
111 /* Handle overrange bit as 17th bit */
112 if (buf[0] & OVERRANGE_BIT)
120 * Initialize tc520 A/D converter driver
122 void tc520_init(Serial *spi_port)
127 /* Send initial load value */
129 ser_putchar(INIT_LOAD_VALUE, spi_ser);