Add definition for generic send and recv.
[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 #define LOG_LEVEL  I2C_LOG_LEVEL
49 #define LOG_FORMAT I2C_LOG_FORMAT
50
51 #include <cfg/log.h>
52
53 #include <cfg/compiler.h>
54 #include <cfg/macros.h>
55 #include <cfg/debug.h>
56
57 #include <cpu/attr.h>
58
59 #define I2C_READBIT BV(0)
60
61 #if COMPILER_C99
62         #define i2c_init(...)     PP_CAT(i2c_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
63 #else
64         #define i2c_init(args...)     PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
65 #endif
66
67 /**
68  * I2C Backends.
69  * Sometimes your cpu does not have a builtin
70  * i2c driver or you don't want, for some reason, to
71  * use that.
72  * With this you can choose, at compile time, which backend to use.
73  *
74  * $WIZ$ i2c_backend = "I2C_BACKEND_BUILTIN", "I2C_BACKEND_BITBANG"
75  */
76 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
77 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
78
79 #if 0
80
81 /**
82  * I2c builtin prototypes.
83  * Do NOT use these function directly, instead,
84  * you can call the ones named without "_builtin_"
85  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
86  * that you want the builtin backend.
87  * \{
88  */
89 void i2c_builtin_init(void);
90 bool i2c_builtin_start_w(uint8_t id);
91 bool i2c_builtin_start_r(uint8_t id);
92 void i2c_builtin_stop(void);
93 bool i2c_builtin_put(uint8_t _data);
94 int i2c_builtin_get(bool ack);
95 /*\}*/
96
97 /**
98  * I2c bitbang prototypes.
99  * Same thing here: do NOT use these function directly, instead,
100  * you can call the ones named without "_bitbang_"
101  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
102  * that you want the bitbang backend.
103  * \{
104  */
105 void i2c_bitbang_init(void);
106 bool i2c_bitbang_start_w(uint8_t id);
107 bool i2c_bitbang_start_r(uint8_t id);
108 void i2c_bitbang_stop(void);
109 bool i2c_bitbang_put(uint8_t _data);
110 int i2c_bitbang_get(bool ack);
111 /*\}*/
112
113 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
114         #define i2c_init_0    i2c_builtin_init
115         #define i2c_start_w   i2c_builtin_start_w
116         #define i2c_start_r   i2c_builtin_start_r
117         #define i2c_stop      i2c_builtin_stop
118         #define i2c_put       i2c_builtin_put
119         #define i2c_get       i2c_builtin_get
120 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
121         #define i2c_init_0    i2c_bitbang_init
122         #define i2c_start_w   i2c_bitbang_start_w
123         #define i2c_start_r   i2c_bitbang_start_r
124         #define i2c_stop      i2c_bitbang_stop
125         #define i2c_put       i2c_bitbang_put
126         #define i2c_get       i2c_bitbang_get
127 #else
128         #error Unsupported i2c backend.
129 #endif
130
131
132 bool i2c_send(const void *_buf, size_t count);
133 bool i2c_recv(void *_buf, size_t count);
134
135 #endif
136
137 /*
138  * I2c new api
139  *
140  */
141 #define I2C_OK               0
142 #define I2C_ERR           BV(3)
143 #define I2C_ARB_LOST      BV(2)
144 #define I2C_START_TIMEOUT BV(0)
145 #define I2C_NO_ACK        BV(1)
146
147
148
149 #define I2C_NOSTOP           0
150 #define I2C_STOP          BV(0)
151 #define I2C_START_R       BV(1)
152 #define I2C_START_W          0
153
154
155 #define I2C_TEST_START(flag)  ((flag) & I2C_START_R)
156 #define I2C_TEST_STOP(flag)   ((flag) & I2C_STOP)
157
158 struct I2cHardware;
159 struct I2c;
160
161 typedef void (*i2c_start_t)(struct I2c *i2c, uint16_t slave_addr);
162 typedef uint8_t (*i2c_get_t)(struct I2c *i2c);
163 typedef void (*i2c_put_t)(struct I2c *i2c, uint8_t data);
164 typedef void (*i2c_send_t)(struct I2c *i2c, const void *_buf, size_t count);
165 typedef void (*i2c_recv_t)(struct I2c *i2c, void *_buf, size_t count);
166
167 typedef struct I2cVT
168 {
169         i2c_start_t start;
170         i2c_get_t   get;
171         i2c_put_t   put;
172         i2c_send_t  send;
173         i2c_recv_t  recv;
174 } I2cVT;
175
176 typedef struct I2c
177 {
178         int errors;
179         int flags;
180         size_t xfer_size;
181         struct I2cHardware* hw;
182         const struct I2cVT *vt;
183 } I2c;
184
185
186 #include CPU_HEADER(i2c)
187
188 void i2c_swSend(struct I2c *i2c, const void *_buf, size_t count);
189 void i2c_swRecv(struct I2c *i2c, void *_buf, size_t count);
190
191 INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
192 {
193         ASSERT(i2c->vt);
194         ASSERT(i2c->vt->start);
195
196         if (!i2c->errors)
197                 ASSERT(i2c->xfer_size == 0);
198
199         i2c->errors = 0;
200         i2c->xfer_size = size;
201
202         i2c->vt->start(i2c, slave_addr);
203 }
204
205 INLINE void i2c_start_r(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
206 {
207         ASSERT(i2c);
208         i2c->flags = flags | I2C_START_R;
209         i2c_start(i2c, slave_addr, size);
210 }
211
212 INLINE void i2c_start_w(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
213 {
214         ASSERT(i2c);
215         i2c->flags = flags & ~I2C_START_R;
216         i2c_start(i2c, slave_addr, size);
217 }
218
219 INLINE uint8_t i2c_get(I2c *i2c)
220 {
221         ASSERT(i2c);
222         ASSERT(i2c->vt);
223         ASSERT(i2c->vt->get);
224
225         ASSERT(i2c->xfer_size);
226
227         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
228
229         if (!i2c->errors)
230         {
231                 uint8_t data = i2c->vt->get(i2c);
232                 i2c->xfer_size--;
233                 return data;
234         }
235         else
236                 return 0xFF;
237 }
238
239 INLINE void i2c_put(I2c *i2c, uint8_t data)
240 {
241         ASSERT(i2c);
242         ASSERT(i2c->vt);
243         ASSERT(i2c->vt->put);
244
245         ASSERT(i2c->xfer_size);
246
247         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
248
249         if (!i2c->errors)
250         {
251                 i2c->vt->put(i2c, data);
252                 i2c->xfer_size--;
253         }
254 }
255
256 INLINE void i2c_send(I2c *i2c, const void *_buf, size_t count)
257 {
258         ASSERT(i2c);
259         ASSERT(i2c->vt);
260         ASSERT(i2c->vt->send);
261
262         ASSERT(_buf);
263         ASSERT(count);
264         ASSERT(count <= i2c->xfer_size);
265
266         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
267
268         if (!i2c->errors)
269                 i2c->vt->send(i2c, _buf, count);
270 }
271
272
273 INLINE void i2c_recv(I2c *i2c, void *_buf, size_t count)
274 {
275         ASSERT(i2c);
276         ASSERT(i2c->vt);
277         ASSERT(i2c->vt->recv);
278
279         ASSERT(_buf);
280         ASSERT(count);
281         ASSERT(count <= i2c->xfer_size);
282
283         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
284
285         if (!i2c->errors)
286                 i2c->vt->recv(i2c, _buf, count);
287 }
288
289 INLINE int i2c_error(I2c *i2c)
290 {
291         ASSERT(i2c);
292         int err = i2c->errors;
293         i2c->errors = 0;
294
295         LOG_ERRB(
296                 if (err)
297                         LOG_ERR("err[%02x]\n", err);
298         );
299
300         return err;
301 }
302
303 INLINE void i2c_init_3(I2c *i2c, int dev, uint32_t clock)
304 {
305         i2c_hw_init(i2c, dev, clock);
306 }
307
308 #endif