Add data nack error.
[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 #else
59         #define i2c_init(args...)     PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
60 #endif
61
62 /**
63  * I2C Backends.
64  * Sometimes your cpu does not have a builtin
65  * i2c driver or you don't want, for some reason, to
66  * use that.
67  * With this you can choose, at compile time, which backend to use.
68  *
69  * $WIZ$ i2c_backend = "I2C_BACKEND_BUILTIN", "I2C_BACKEND_BITBANG"
70  */
71 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
72 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
73
74 #if 0
75
76 /**
77  * I2c builtin prototypes.
78  * Do NOT use these function directly, instead,
79  * you can call the ones named without "_builtin_"
80  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
81  * that you want the builtin backend.
82  * \{
83  */
84 void i2c_builtin_init(void);
85 bool i2c_builtin_start_w(uint8_t id);
86 bool i2c_builtin_start_r(uint8_t id);
87 void i2c_builtin_stop(void);
88 bool i2c_builtin_put(uint8_t _data);
89 int i2c_builtin_get(bool ack);
90 /*\}*/
91
92 /**
93  * I2c bitbang prototypes.
94  * Same thing here: do NOT use these function directly, instead,
95  * you can call the ones named without "_bitbang_"
96  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
97  * that you want the bitbang backend.
98  * \{
99  */
100 void i2c_bitbang_init(void);
101 bool i2c_bitbang_start_w(uint8_t id);
102 bool i2c_bitbang_start_r(uint8_t id);
103 void i2c_bitbang_stop(void);
104 bool i2c_bitbang_put(uint8_t _data);
105 int i2c_bitbang_get(bool ack);
106 /*\}*/
107
108 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
109         #define i2c_init_0    i2c_builtin_init
110         #define i2c_start_w   i2c_builtin_start_w
111         #define i2c_start_r   i2c_builtin_start_r
112         #define i2c_stop      i2c_builtin_stop
113         #define i2c_put       i2c_builtin_put
114         #define i2c_get       i2c_builtin_get
115 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
116         #define i2c_init_0    i2c_bitbang_init
117         #define i2c_start_w   i2c_bitbang_start_w
118         #define i2c_start_r   i2c_bitbang_start_r
119         #define i2c_stop      i2c_bitbang_stop
120         #define i2c_put       i2c_bitbang_put
121         #define i2c_get       i2c_bitbang_get
122 #else
123         #error Unsupported i2c backend.
124 #endif
125
126
127 bool i2c_send(const void *_buf, size_t count);
128 bool i2c_recv(void *_buf, size_t count);
129
130 #endif
131
132 /*
133  * I2c new api
134  */
135
136  /*
137   * I2C error flags
138   */
139 #define I2C_OK               0     ///< I2C no errors flag
140 #define I2C_DATA_NACK     BV(4)    ///< I2C generic error
141 #define I2C_ERR           BV(3)    ///< I2C generic error
142 #define I2C_ARB_LOST      BV(2)    ///< I2C arbitration lost error
143 #define I2C_START_TIMEOUT BV(0)    ///< I2C timeout error on start
144 #define I2C_NO_ACK        BV(1)    ///< I2C no ack for sla start
145
146 /*
147  * I2C command flags
148  */
149 #define I2C_NOSTOP           0    ///< Do not program the stop for current transition
150 #define I2C_STOP          BV(0)   ///< Program the stop for current transition
151 #define I2C_START_R       BV(1)   ///< Start read command
152 #define I2C_START_W          0    ///< Start write command
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 /*
189  * Low level i2c  init implementation prototype.
190  */
191 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
192
193 void i2c_swSend(struct I2c *i2c, const void *_buf, size_t count);
194 void i2c_swRecv(struct I2c *i2c, void *_buf, size_t count);
195
196 INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
197 {
198         ASSERT(i2c->vt);
199         ASSERT(i2c->vt->start);
200
201         if (!i2c->errors)
202                 ASSERT(i2c->xfer_size == 0);
203
204         i2c->errors = 0;
205         i2c->xfer_size = size;
206
207         i2c->vt->start(i2c, slave_addr);
208 }
209
210 INLINE void i2c_start_r(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
211 {
212         ASSERT(i2c);
213         i2c->flags = flags | I2C_START_R;
214         i2c_start(i2c, slave_addr, size);
215 }
216
217 INLINE void i2c_start_w(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
218 {
219         ASSERT(i2c);
220         i2c->flags = flags & ~I2C_START_R;
221         i2c_start(i2c, slave_addr, size);
222 }
223
224 INLINE uint8_t i2c_get(I2c *i2c)
225 {
226         ASSERT(i2c);
227         ASSERT(i2c->vt);
228         ASSERT(i2c->vt->get);
229
230         ASSERT(i2c->xfer_size);
231
232         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
233
234         if (!i2c->errors)
235         {
236                 uint8_t data = i2c->vt->get(i2c);
237                 i2c->xfer_size--;
238                 return data;
239         }
240         else
241                 return 0xFF;
242 }
243
244 INLINE void i2c_put(I2c *i2c, uint8_t data)
245 {
246         ASSERT(i2c);
247         ASSERT(i2c->vt);
248         ASSERT(i2c->vt->put);
249
250         ASSERT(i2c->xfer_size);
251
252         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
253
254         if (!i2c->errors)
255         {
256                 i2c->vt->put(i2c, data);
257                 i2c->xfer_size--;
258         }
259 }
260
261 INLINE void i2c_send(I2c *i2c, const void *_buf, size_t count)
262 {
263         ASSERT(i2c);
264         ASSERT(i2c->vt);
265         ASSERT(i2c->vt->send);
266
267         ASSERT(_buf);
268         ASSERT(count);
269         ASSERT(count <= i2c->xfer_size);
270
271         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
272
273         if (!i2c->errors)
274                 i2c->vt->send(i2c, _buf, count);
275 }
276
277
278 INLINE void i2c_recv(I2c *i2c, void *_buf, size_t count)
279 {
280         ASSERT(i2c);
281         ASSERT(i2c->vt);
282         ASSERT(i2c->vt->recv);
283
284         ASSERT(_buf);
285         ASSERT(count);
286         ASSERT(count <= i2c->xfer_size);
287
288         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
289
290         if (!i2c->errors)
291                 i2c->vt->recv(i2c, _buf, count);
292 }
293
294 INLINE int i2c_error(I2c *i2c)
295 {
296         ASSERT(i2c);
297         int err = i2c->errors;
298         i2c->errors = 0;
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