Specific the directory for all hw and cfg module. Use double quote for cfg and hw...
[bertos.git] / bertos / drv / tc520.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  *
33  * \brief TC520 ADC driver (implementation)
34  *
35  * \version $Id$
36  *
37  * \author Francesco Sacchi <batt@develer.com>
38  * \author Marco Benelli <marco@develer.com>
39  */
40
41
42 #include "hw/hw_tc520.h"
43
44 #include <cfg/macros.h>
45 #include <cfg/compiler.h>
46
47 #include <drv/tc520.h>
48 #include <drv/timer.h>
49
50 #warning FIXME:This implementation is obsolete. Refactor with KFile interface.
51
52 #if 0
53
54 #include <drv/ser.h>
55
56 static Serial *spi_ser;
57
58 #define TC520_CONVERSION_TIMEOUT ms_to_ticks(1000)
59 #define INIT_LOAD_VALUE 0x00
60
61 /**
62  * Start an AD conversion and return result.
63  *
64  * To start a conversion first we must pull down CE pin.
65  * The ADC starts a convertion and keeps the DV pin high until the end.
66  * At this point, we can read the conversion value by SPI.
67  * The convertion result is yield in 3 bytes.
68  *
69  * \verbatim
70  *
71  * First byte:
72  * bit | Value
73  * ----|-------
74  *  7  | Overrange
75  *  6  | Polarity
76  * 5:0 | data bits 15:10
77  *
78  * Second byte: data 9:2
79  *
80  * Third byte:
81  * bit | Value
82  * ----|-------
83  *  7  | data bit 1
84  *  6  | data bit 0
85  * 5:0 | '0'
86  *
87  * \endverbatim
88  *
89  * So, to get the result we must shift and recompose the bits.
90  * \note Overrange bit is handled as the 17th data bit.
91  */
92 tc520_data_t tc520_read(void)
93 {
94         /* Start convertion and wait */
95         CE_LOW();
96         ticks_t start = timer_clock();
97         do
98         {
99                 /* timeout check */
100                 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
101                 {
102                         ASSERT(0);
103                         CE_HIGH();
104                         return TC520_MAX_VALUE;
105                 }
106         }
107         while(DV_LOW());
108
109         start = timer_clock();
110         do
111         {
112                 /* timeout check */
113                 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
114                 {
115                         ASSERT(0);
116                         CE_HIGH();
117                         return TC520_MAX_VALUE;
118                 }
119         }
120         while(DV_HIGH());
121
122         /* Ok, convertion finished, read result */
123         CE_HIGH();
124         READ_LOW();
125
126         /* RX buffer could be dirty...*/
127         ser_purge(spi_ser);
128
129         /* I/O buffer */
130         uint8_t buf[3] = "\x0\x0\x0";
131
132         /* Dummy write to activate recv */
133         ser_write(spi_ser, buf, sizeof(buf));
134         ser_drain(spi_ser);
135         READ_HIGH();
136
137         /* recv */
138         ASSERT(ser_read(spi_ser, buf, sizeof(buf)) == sizeof(buf));
139
140         tc520_data_t res;
141
142         /* Recompose data */
143         res = (((tc520_data_t)(buf[0] & 0x3F)) << 10) | (((tc520_data_t)buf[1]) << 2) | (((tc520_data_t)buf[2]) >> 6);
144
145         #define OVERRANGE_BIT BV(7)
146         /* Handle overrange bit as 17th bit */
147         if (buf[0] & OVERRANGE_BIT)
148                 res |= BV32(16);
149
150         return res;
151 }
152
153
154 /**
155  * Initialize tc520 A/D converter driver
156  */
157 void tc520_init(Serial *spi_port)
158 {
159         spi_ser = spi_port;
160         /* init io ports */
161         TC520_HW_INIT;
162         /* Send initial load value */
163         LOAD_LOW();
164         ser_putchar(INIT_LOAD_VALUE, spi_ser);
165         ser_drain(spi_ser);
166         LOAD_HIGH();
167 }
168
169 #endif