Added TC520 ADC driver.
[bertos.git] / drv / tc520.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5  * -->
6  *
7  * \version $Id$
8  *
9  * \brief TC520 ADC driver (implementation)
10  *
11  * \version $Id$
12  * \author Francesco Sacchi <batt@develer.com>
13  * \author Marco Benelli <marco@develer.com>
14  */
15
16 #include <drv/tc520.h>
17 #include <drv/timer.h>
18
19 #include <hw_tc520.h>
20
21 #include <cfg/macros.h>
22 #include <cfg/cpu.h>
23 #include <cfg/compiler.h>
24
25 #include <drv/ser.h>
26
27 static Serial *spi_ser;
28
29 #define TC520_CONVERSION_TIMEOUT ms_to_ticks(1000)
30 #define INIT_LOAD_VALUE 0x00
31
32 /**
33  * Start an AD conversion and return result.
34  * To start a conversion first we must pull down CE pin.
35  * The ADC starts a convertion and keeps the DV pin high until the end.
36  * At this point, we can read the conversion value by SPI.
37  * The convertion result is yield in 3 bytes.
38  * First byte:
39  * bit | Value
40  * ----|-------
41  *  7  | Overrange
42  *  6  | Polarity
43  * 5:0 | data bits 15:10
44  *
45  * Second byte: data 9:2
46  *
47  * Third byte:
48  * bit | Value
49  * ----|-------
50  *  7  | data bit 1
51  *  6  | data bit 0
52  * 5:0 | '0'
53  *
54  * So, to get the result we must shift and recompose the bits.
55  * \Note Ovverrange bit is handled as the 17th data bit.
56  */
57 tc520_data_t tc520_read(void)
58 {
59         /* Start convertion and wait */
60         CE_LOW();
61         ticks_t start = timer_clock();
62         do
63         {
64                 /* timeout check */
65                 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
66                 {
67                         ASSERT(0);
68                         CE_HIGH();
69                         return TC520_MAX_VALUE;
70                 }
71         }
72         while(DV_LOW());
73
74         start = timer_clock();
75         do
76         {
77                 /* timeout check */
78                 if (timer_clock() - start >= TC520_CONVERSION_TIMEOUT)
79                 {
80                         ASSERT(0);
81                         CE_HIGH();
82                         return TC520_MAX_VALUE;
83                 }
84         }
85         while(DV_HIGH());
86
87         /* Ok, convertion finished, read result */
88         CE_HIGH();
89         READ_LOW();
90
91         /* RX buffer could be dirty...*/
92         ser_purge(spi_ser);
93
94         /* I/O buffer */
95         uint8_t buf[3] = "\x0\x0\x0";
96
97         /* Dummy write to activate recv */
98         ser_write(spi_ser, buf, sizeof(buf));
99         ser_drain(spi_ser);
100         READ_HIGH();
101
102         /* recv */
103         ASSERT(ser_read(spi_ser, buf, sizeof(buf)) == sizeof(buf));
104
105         tc520_data_t res;
106
107         /* Recompose data */
108         res = (((tc520_data_t)(buf[0] & 0x3F)) << 10) | (((tc520_data_t)buf[1]) << 2) | (((tc520_data_t)buf[2]) >> 6);
109
110         #define OVERRANGE_BIT BV(7)
111         /* Handle overrange bit as 17th bit */
112         if (buf[0] & OVERRANGE_BIT)
113                 res |= BV32(16);
114
115         return res;
116 }
117
118
119 /**
120  * Initialize tc520 A/D converter driver
121  */
122 void tc520_init(Serial *spi_port)
123 {
124         spi_ser = spi_port;
125         /* init io ports */
126         TC520_HW_INIT;
127         /* Send initial load value */
128         LOAD_LOW();
129         ser_putchar(INIT_LOAD_VALUE, spi_ser);
130         ser_drain(spi_ser);
131         LOAD_HIGH();
132 }