Update cpu.h dir.
[bertos.git] / 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 <cpu/cpu.h>
48 #include <cfg/compiler.h>
49
50 #include <drv/ser.h>
51
52 static Serial *spi_ser;
53
54 #define TC520_CONVERSION_TIMEOUT ms_to_ticks(1000)
55 #define INIT_LOAD_VALUE 0x00
56
57 /**
58  * Start an AD conversion and return result.
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  * First byte:
64  * bit | Value
65  * ----|-------
66  *  7  | Overrange
67  *  6  | Polarity
68  * 5:0 | data bits 15:10
69  *
70  * Second byte: data 9:2
71  *
72  * Third byte:
73  * bit | Value
74  * ----|-------
75  *  7  | data bit 1
76  *  6  | data bit 0
77  * 5:0 | '0'
78  *
79  * So, to get the result we must shift and recompose the bits.
80  * \Note Ovverrange bit is handled as the 17th data bit.
81  */
82 tc520_data_t tc520_read(void)
83 {
84         /* Start convertion and wait */
85         CE_LOW();
86         ticks_t start = timer_clock();
87         do
88         {
89                 /* timeout check */
90                 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
91                 {
92                         ASSERT(0);
93                         CE_HIGH();
94                         return TC520_MAX_VALUE;
95                 }
96         }
97         while(DV_LOW());
98
99         start = timer_clock();
100         do
101         {
102                 /* timeout check */
103                 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
104                 {
105                         ASSERT(0);
106                         CE_HIGH();
107                         return TC520_MAX_VALUE;
108                 }
109         }
110         while(DV_HIGH());
111
112         /* Ok, convertion finished, read result */
113         CE_HIGH();
114         READ_LOW();
115
116         /* RX buffer could be dirty...*/
117         ser_purge(spi_ser);
118
119         /* I/O buffer */
120         uint8_t buf[3] = "\x0\x0\x0";
121
122         /* Dummy write to activate recv */
123         ser_write(spi_ser, buf, sizeof(buf));
124         ser_drain(spi_ser);
125         READ_HIGH();
126
127         /* recv */
128         ASSERT(ser_read(spi_ser, buf, sizeof(buf)) == sizeof(buf));
129
130         tc520_data_t res;
131
132         /* Recompose data */
133         res = (((tc520_data_t)(buf[0] & 0x3F)) << 10) | (((tc520_data_t)buf[1]) << 2) | (((tc520_data_t)buf[2]) >> 6);
134
135         #define OVERRANGE_BIT BV(7)
136         /* Handle overrange bit as 17th bit */
137         if (buf[0] & OVERRANGE_BIT)
138                 res |= BV32(16);
139
140         return res;
141 }
142
143
144 /**
145  * Initialize tc520 A/D converter driver
146  */
147 void tc520_init(Serial *spi_port)
148 {
149         spi_ser = spi_port;
150         /* init io ports */
151         TC520_HW_INIT;
152         /* Send initial load value */
153         LOAD_LOW();
154         ser_putchar(INIT_LOAD_VALUE, spi_ser);
155         ser_drain(spi_ser);
156         LOAD_HIGH();
157 }