Add stop flag defition.
[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
51 #include <cpu/attr.h>
52
53 #define I2C_READBIT BV(0)
54
55 #define i2c_init(FN_ARGS) PP_CAT(i2c_init ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS)
56
57 /**
58  * I2C Backends.
59  * Sometimes your cpu does not have a builtin
60  * i2c driver or you don't want, for some reason, to
61  * use that.
62  * With this you can choose, at compile time, which backend to use.
63  *
64  * $WIZ$ i2c_backend = "I2C_BACKEND_BUILTIN", "I2C_BACKEND_BITBANG"
65  */
66 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
67 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
68
69
70 /**
71  * I2c builtin prototypes.
72  * Do NOT use these function directly, instead,
73  * you can call the ones named without "_builtin_"
74  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
75  * that you want the builtin backend.
76  * \{
77  */
78 void i2c_builtin_init(void);
79 bool i2c_builtin_start_w(uint8_t id);
80 bool i2c_builtin_start_r(uint8_t id);
81 void i2c_builtin_stop(void);
82 bool i2c_builtin_put(uint8_t _data);
83 int i2c_builtin_get(bool ack);
84 /*\}*/
85
86 /**
87  * I2c bitbang prototypes.
88  * Same thing here: do NOT use these function directly, instead,
89  * you can call the ones named without "_bitbang_"
90  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
91  * that you want the bitbang backend.
92  * \{
93  */
94 void i2c_bitbang_init(void);
95 bool i2c_bitbang_start_w(uint8_t id);
96 bool i2c_bitbang_start_r(uint8_t id);
97 void i2c_bitbang_stop(void);
98 bool i2c_bitbang_put(uint8_t _data);
99 int i2c_bitbang_get(bool ack);
100 /*\}*/
101
102 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
103         #define i2c_init_0    i2c_builtin_init
104         #define i2c_start_w   i2c_builtin_start_w
105         #define i2c_start_r   i2c_builtin_start_r
106         #define i2c_stop      i2c_builtin_stop
107         #define i2c_put       i2c_builtin_put
108         #define i2c_get       i2c_builtin_get
109 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
110         #define i2c_init_0    i2c_bitbang_init
111         #define i2c_start_w   i2c_bitbang_start_w
112         #define i2c_start_r   i2c_bitbang_start_r
113         #define i2c_stop      i2c_bitbang_stop
114         #define i2c_put       i2c_bitbang_put
115         #define i2c_get       i2c_bitbang_get
116 #else
117         #error Unsupported i2c backend.
118 #endif
119
120 bool i2c_send(const void *_buf, size_t count);
121 bool i2c_recv(void *_buf, size_t count);
122
123
124 /*
125  * I2c new api
126  *
127  */
128
129 #define I2C_STOP          BV(0)    ///< Says to driver to generate the stop after i2c tranfer.
130
131 struct I2cHardware;
132 struct I2c;
133
134 typedef int (*i2c_writeRope_t)(struct I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len, ...);
135 typedef int (*i2c_readRope_t)(struct I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len, ...);
136
137 typedef struct I2c
138 {
139         int dev;
140         i2c_writeRope_t write;
141         i2c_readRope_t read;
142
143         struct I2cHardware* hw;
144 } I2c;
145
146
147 #include CPU_HEADER(i2c)
148
149 INLINE void i2c_init_3(I2c *i2c, int dev, uint32_t clock)
150 {
151         i2c_hw_init(i2c, dev, clock);
152 }
153
154 #define i2c_write(FN_ARGS) PP_CAT(i2c_write ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS)
155 #define i2c_read(FN_ARGS) PP_CAT(i2c_read ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS)
156
157 /*
158  * Overloaded functions definition.
159  */
160 INLINE int i2c_write_5(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len)
161 {
162         return i2c->write(i2c, slave_addr, flags, buf, len, NULL);
163 }
164
165 INLINE int i2c_read_5(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len)
166 {
167         return i2c->read(i2c, slave_addr, flags, buf, len, NULL);
168 }
169
170
171 INLINE int i2c_write_7(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
172                                                                                                                   const void *buf1, size_t len1)
173 {
174         return i2c->write(i2c, slave_addr, flags, buf, len,
175                                                                                           buf1, len1, NULL);
176 }
177
178 INLINE int i2c_read_7(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
179                                                                                                                  void *buf1, size_t len1)
180
181 {
182         return i2c->read(i2c, slave_addr, flags, buf, len,
183                                                                                          buf1, len1, NULL);
184 }
185
186
187 INLINE int i2c_write_9(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
188                                                                                                                   const void *buf1, size_t len1,
189                                                                                                                   const void *buf2, size_t len2)
190 {
191         return i2c->write(i2c, slave_addr, flags, buf, len,
192                                                                                           buf1, len1,
193                                                                                           buf2, len2, NULL);
194 }
195
196 INLINE int i2c_read_9(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
197                                                                                                                  void *buf1, size_t len1,
198                                                                                                                  void *buf2, size_t len2)
199 {
200         return i2c->read(i2c, slave_addr, flags, buf, len,
201                                                                                           buf1, len1,
202                                                                                           buf2, len2, NULL);
203 }
204
205 INLINE int i2c_write_11(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
206                                                                                                                   const void *buf1, size_t len1,
207                                                                                                                   const void *buf2, size_t len2,
208                                                                                                                   const void *buf3, size_t len3)
209 {
210         return i2c->write(i2c, slave_addr, flags, buf, len,
211                                                                                           buf1, len1,
212                                                                                           buf2, len2,
213                                                                                           buf3, len3, NULL);
214 }
215
216 INLINE int i2c_read_11(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
217                                                                                                                  void *buf1, size_t len1,
218                                                                                                                  void *buf2, size_t len2,
219                                                                                                                  void *buf3, size_t len3)
220
221 {
222         return i2c->read(i2c, slave_addr, flags, buf, len,
223                                                                                           buf1, len1,
224                                                                                           buf2, len2,
225                                                                                           buf3, len3, NULL);
226 }
227
228 #endif