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