Remove from wizart i2c backend selection. Add deprecate switch to disable old i2c...
[bertos.git] / bertos / drv / i2c.h
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 I2C generic driver functions.
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * $WIZ$ module_name = "i2c"
38  * $WIZ$ module_configuration = "bertos/cfg/cfg_i2c.h"
39  * $WIZ$ module_hw = "bertos/hw/hw_i2c_bitbang.h"
40  * $WIZ$ module_depends = "i2c_bitbang"
41  * $WIZ$ module_supports = "not atmega103 and not atmega168 and not at91"
42  */
43
44 #ifndef DRV_I2C_H
45 #define DRV_I2C_H
46
47 #include "cfg/cfg_i2c.h"
48
49 #include <cfg/compiler.h>
50 #include <cfg/macros.h>
51 #include <cfg/debug.h>
52
53 #include <cpu/attr.h>
54
55 #define I2C_READBIT BV(0)
56
57 #if COMPILER_C99
58         #define i2c_init(...)           PP_CAT(i2c_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
59         #define i2c_start_w(...)        PP_CAT(i2c_start_w ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
60         #define i2c_start_r(...)        PP_CAT(i2c_start_r ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
61 #else
62         #define i2c_init(args...)       PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
63         #define i2c_start_w(args...)    PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)
64         #define i2c_start_r(args...)    PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)
65 #endif
66
67
68 #if !CONFIG_I2C_DISABLE_OLD_API
69 /**
70  * I2C Backends.
71  * Sometimes your cpu does not have a builtin
72  * i2c driver or you don't want, for some reason, to
73  * use that.
74  * With this you can choose, at compile time, which backend to use.
75  */
76 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
77 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
78
79 /**
80  * I2c builtin prototypes.
81  * Do NOT use these function directly, instead,
82  * you can call the ones named without "_builtin_"
83  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
84  * that you want the builtin backend.
85  * \{
86  */
87 void i2c_builtin_init(void);
88 bool i2c_builtin_start_w(uint8_t id);
89 bool i2c_builtin_start_r(uint8_t id);
90 void i2c_builtin_stop(void);
91 bool i2c_builtin_put(uint8_t _data);
92 int i2c_builtin_get(bool ack);
93 /*\}*/
94
95 /**
96  * I2c bitbang prototypes.
97  * Same thing here: do NOT use these function directly, instead,
98  * you can call the ones named without "_bitbang_"
99  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
100  * that you want the bitbang backend.
101  * \{
102  */
103 void i2c_bitbang_init(void);
104 bool i2c_bitbang_start_w(uint8_t id);
105 bool i2c_bitbang_start_r(uint8_t id);
106 void i2c_bitbang_stop(void);
107 bool i2c_bitbang_put(uint8_t _data);
108 int i2c_bitbang_get(bool ack);
109 /*\}*/
110
111 #ifndef CONFIG_I2C_BACKEND
112 #define  CONFIG_I2C_BACKEND  I2C_BACKEND_BUILTIN
113 #endif
114
115 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
116         #define i2c_init_0      i2c_builtin_init
117         #define i2c_start_w_1   i2c_builtin_start_w
118         #define i2c_start_r_1   i2c_builtin_start_r
119         #define i2c_stop        i2c_builtin_stop
120         #define i2c_put         i2c_builtin_put
121         #define i2c_get         i2c_builtin_get
122 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
123         #define i2c_init_0      i2c_bitbang_init
124         #define i2c_start_w_1   i2c_bitbang_start_w
125         #define i2c_start_r_1   i2c_bitbang_start_r
126         #define i2c_stop        i2c_bitbang_stop
127         #define i2c_put         i2c_bitbang_put
128         #define i2c_get         i2c_bitbang_get
129 #else
130         #error Unsupported i2c backend.
131 #endif
132
133
134 bool i2c_send(const void *_buf, size_t count);
135 bool i2c_recv(void *_buf, size_t count);
136
137 #endif
138
139 /*
140  * I2c new api
141  */
142
143 /**
144  * \name I2C bitbang devices enum
145  */
146 enum
147 {
148         I2C_BITBANG0 = 1000,
149         I2C_BITBANG1,
150         I2C_BITBANG2,
151         I2C_BITBANG3,
152         I2C_BITBANG4,
153         I2C_BITBANG5,
154         I2C_BITBANG6,
155         I2C_BITBANG7,
156         I2C_BITBANG8,
157         I2C_BITBANG9,
158
159         I2C_BITBANG_CNT  /**< Number of serial ports */
160 };
161
162  /*
163   * I2C error flags
164   */
165 #define I2C_OK               0     ///< I2C no errors flag
166 #define I2C_DATA_NACK     BV(4)    ///< I2C generic error
167 #define I2C_ERR           BV(3)    ///< I2C generic error
168 #define I2C_ARB_LOST      BV(2)    ///< I2C arbitration lost error
169 #define I2C_START_TIMEOUT BV(0)    ///< I2C timeout error on start
170 #define I2C_NO_ACK        BV(1)    ///< I2C no ack for sla start
171
172 /*
173  * I2C command flags
174  */
175 #define I2C_NOSTOP           0    ///< Do not program the stop for current transition
176 #define I2C_STOP          BV(0)   ///< Program the stop for current transition
177 #define I2C_START_R       BV(1)   ///< Start read command
178 #define I2C_START_W          0    ///< Start write command
179
180
181 #define I2C_TEST_START(flag)  ((flag) & I2C_START_R)
182 #define I2C_TEST_STOP(flag)   ((flag) & I2C_STOP)
183
184 struct I2cHardware;
185 struct I2c;
186
187 typedef void (*i2c_start_t)(struct I2c *i2c, uint16_t slave_addr);
188 typedef uint8_t (*i2c_getc_t)(struct I2c *i2c);
189 typedef void (*i2c_putc_t)(struct I2c *i2c, uint8_t data);
190 typedef void (*i2c_write_t)(struct I2c *i2c, const void *_buf, size_t count);
191 typedef void (*i2c_read_t)(struct I2c *i2c, void *_buf, size_t count);
192
193 typedef struct I2cVT
194 {
195         i2c_start_t start;
196         i2c_getc_t   getc;
197         i2c_putc_t   putc;
198         i2c_write_t  write;
199         i2c_read_t   read;
200 } I2cVT;
201
202 typedef struct I2c
203 {
204         int errors;
205         int flags;
206         size_t xfer_size;
207         struct I2cHardware* hw;
208         const struct I2cVT *vt;
209 } I2c;
210
211
212 #include CPU_HEADER(i2c)
213
214 /*
215  * Low level i2c  init implementation prototype.
216  */
217 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
218 void i2c_hw_bitbangInit(I2c *i2c, int dev);
219
220 void i2c_genericWrite(I2c *i2c, const void *_buf, size_t count);
221 void i2c_genericRead(I2c *i2c, void *_buf, size_t count);
222
223 INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
224 {
225         ASSERT(i2c->vt);
226         ASSERT(i2c->vt->start);
227
228         if (!i2c->errors)
229                 ASSERT(i2c->xfer_size == 0);
230
231         i2c->errors = 0;
232         i2c->xfer_size = size;
233
234         i2c->vt->start(i2c, slave_addr);
235 }
236
237 INLINE void i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
238 {
239         ASSERT(i2c);
240         i2c->flags = flags | I2C_START_R;
241         i2c_start(i2c, slave_addr, size);
242 }
243
244 INLINE void i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
245 {
246         ASSERT(i2c);
247         i2c->flags = flags & ~I2C_START_R;
248         i2c_start(i2c, slave_addr, size);
249 }
250
251 INLINE uint8_t i2c_getc(I2c *i2c)
252 {
253         ASSERT(i2c);
254         ASSERT(i2c->vt);
255         ASSERT(i2c->vt->getc);
256
257         ASSERT(i2c->xfer_size);
258
259         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
260
261         if (!i2c->errors)
262         {
263                 uint8_t data = i2c->vt->getc(i2c);
264                 i2c->xfer_size--;
265                 return data;
266         }
267         else
268                 return 0xFF;
269 }
270
271 INLINE void i2c_putc(I2c *i2c, uint8_t data)
272 {
273         ASSERT(i2c);
274         ASSERT(i2c->vt);
275         ASSERT(i2c->vt->putc);
276
277         ASSERT(i2c->xfer_size);
278
279         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
280
281         if (!i2c->errors)
282         {
283                 i2c->vt->putc(i2c, data);
284                 i2c->xfer_size--;
285         }
286 }
287
288 INLINE void i2c_write(I2c *i2c, const void *_buf, size_t count)
289 {
290         ASSERT(i2c);
291         ASSERT(i2c->vt);
292         ASSERT(i2c->vt->write);
293
294         ASSERT(_buf);
295         ASSERT(count);
296         ASSERT(count <= i2c->xfer_size);
297
298         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
299
300         if (!i2c->errors)
301                 i2c->vt->write(i2c, _buf, count);
302 }
303
304
305 INLINE void i2c_read(I2c *i2c, void *_buf, size_t count)
306 {
307         ASSERT(i2c);
308         ASSERT(i2c->vt);
309         ASSERT(i2c->vt->read);
310
311         ASSERT(_buf);
312         ASSERT(count);
313         ASSERT(count <= i2c->xfer_size);
314
315         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
316
317         if (!i2c->errors)
318                 i2c->vt->read(i2c, _buf, count);
319 }
320
321 INLINE int i2c_error(I2c *i2c)
322 {
323         ASSERT(i2c);
324         int err = i2c->errors;
325         i2c->errors = 0;
326
327         return err;
328 }
329
330 #define i2c_init_3(i2c, dev, clock)   (dev > I2C_BITBANG0) ?  i2c_hw_bitbangInit(i2c, dev) : i2c_hw_init(i2c, dev, clock)
331
332
333 #endif