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