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