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