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/)
33 * \brief TC520 ADC driver (implementation)
36 * \author Francesco Sacchi <batt@develer.com>
37 * \author Marco Benelli <marco@develer.com>
41 #include "hw/hw_tc520.h"
43 #include <cfg/macros.h>
44 #include <cfg/compiler.h>
46 #include <drv/tc520.h>
47 #include <drv/timer.h>
49 #warning FIXME:This implementation is obsolete. Refactor with KFile interface.
55 static Serial *spi_ser;
57 #define TC520_CONVERSION_TIMEOUT ms_to_ticks(1000)
58 #define INIT_LOAD_VALUE 0x00
61 * Start an AD conversion and return result.
63 * To start a conversion first we must pull down CE pin.
64 * The ADC starts a convertion and keeps the DV pin high until the end.
65 * At this point, we can read the conversion value by SPI.
66 * The convertion result is yield in 3 bytes.
75 * 5:0 | data bits 15:10
77 * Second byte: data 9:2
88 * So, to get the result we must shift and recompose the bits.
89 * \note Overrange bit is handled as the 17th data bit.
91 tc520_data_t tc520_read(void)
93 /* Start convertion and wait */
95 ticks_t start = timer_clock();
99 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
103 return TC520_MAX_VALUE;
108 start = timer_clock();
112 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
116 return TC520_MAX_VALUE;
121 /* Ok, convertion finished, read result */
125 /* RX buffer could be dirty...*/
129 uint8_t buf[3] = "\x0\x0\x0";
131 /* Dummy write to activate recv */
132 ser_write(spi_ser, buf, sizeof(buf));
137 ASSERT(ser_read(spi_ser, buf, sizeof(buf)) == sizeof(buf));
142 res = (((tc520_data_t)(buf[0] & 0x3F)) << 10) | (((tc520_data_t)buf[1]) << 2) | (((tc520_data_t)buf[2]) >> 6);
144 #define OVERRANGE_BIT BV(7)
145 /* Handle overrange bit as 17th bit */
146 if (buf[0] & OVERRANGE_BIT)
154 * Initialize tc520 A/D converter driver
156 void tc520_init(Serial *spi_port)
161 /* Send initial load value */
163 ser_putchar(INIT_LOAD_VALUE, spi_ser);