4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
34 * \brief TC520 ADC driver (implementation)
37 * \author Francesco Sacchi <batt@develer.com>
38 * \author Marco Benelli <marco@develer.com>
41 #include <drv/tc520.h>
42 #include <drv/timer.h>
46 #include <cfg/macros.h>
47 #include <cfg/compiler.h>
51 static Serial *spi_ser;
53 #define TC520_CONVERSION_TIMEOUT ms_to_ticks(1000)
54 #define INIT_LOAD_VALUE 0x00
57 * Start an AD conversion and return result.
58 * To start a conversion first we must pull down CE pin.
59 * The ADC starts a convertion and keeps the DV pin high until the end.
60 * At this point, we can read the conversion value by SPI.
61 * The convertion result is yield in 3 bytes.
67 * 5:0 | data bits 15:10
69 * Second byte: data 9:2
78 * So, to get the result we must shift and recompose the bits.
79 * \Note Ovverrange bit is handled as the 17th data bit.
81 tc520_data_t tc520_read(void)
83 /* Start convertion and wait */
85 ticks_t start = timer_clock();
89 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
93 return TC520_MAX_VALUE;
98 start = timer_clock();
102 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
106 return TC520_MAX_VALUE;
111 /* Ok, convertion finished, read result */
115 /* RX buffer could be dirty...*/
119 uint8_t buf[3] = "\x0\x0\x0";
121 /* Dummy write to activate recv */
122 ser_write(spi_ser, buf, sizeof(buf));
127 ASSERT(ser_read(spi_ser, buf, sizeof(buf)) == sizeof(buf));
132 res = (((tc520_data_t)(buf[0] & 0x3F)) << 10) | (((tc520_data_t)buf[1]) << 2) | (((tc520_data_t)buf[2]) >> 6);
134 #define OVERRANGE_BIT BV(7)
135 /* Handle overrange bit as 17th bit */
136 if (buf[0] & OVERRANGE_BIT)
144 * Initialize tc520 A/D converter driver
146 void tc520_init(Serial *spi_port)
151 /* Send initial load value */
153 ser_putchar(INIT_LOAD_VALUE, spi_ser);