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