doc: I2C, add more documentation for old API backends.
[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  * \addtogroup i2c_api
34  * \brief I2C generic driver functions.
35  *
36  * Some hardware requires you to declare the number of transferred
37  * bytes and the communication direction before actually reading or writing
38  * to the bus.
39  * Furthermore, sometimes you need to specify the first transferred byte
40  * before any data is sent over the bus.
41  *
42  * The usage pattern for writing is the following:
43  * \code
44  * i2c_init(args...);
45  * ...
46  * i2c_start_w(args...);
47  * i2c_write(i2c, buf, len);
48  * \endcode
49  * The flags in i2c_start_w determine if the stop command is sent after
50  * the data. Notice that you don't need to explicitly call a stop function
51  * after the write.
52  *
53  * Reading is a bit more complicated and it largely depends on the specific
54  * slave hardware.
55  * In general, the hardware may require you to first write something, then
56  * read the data without closing the communication. For example, EPROMs
57  * require first to write the reading address and then to read the actual
58  * data.
59  * Here is an example of how you can deal with such hardware:
60  *
61  * \code
62  * // init a session without closing it
63  * i2c_start_w(i2c, dev, bytes, I2C_NOSTOP);
64  * // write the address to read from
65  * i2c_write(i2c, addr, bytes);
66  * if (i2c_error(i2c))
67  *     // check for errors during setup
68  *     //...
69  * // now start the real data transfer
70  * i2c_start_r(i2c, dev, bytes, I2C_STOP);
71  * i2c_read(i2c, buf, bytes);
72  * // check for errors
73  * if (i2c_error(i2c))
74  *     //...
75  * \endcode
76  *
77  * It's not guaranteed that after a single call to i2c_putc, i2c_getc etc.
78  * data will pass on the bus (this is hardware dependent).
79  * However, it IS guaranteed after you have sent all the data.
80  *
81  * You can check error conditions by calling the function i2c_error after
82  * each function call. (This is similar to libc errno handling).
83  *
84  * \author Francesco Sacchi <batt@develer.com>
85  *
86  * $WIZ$ module_name = "i2c"
87  * $WIZ$ module_configuration = "bertos/cfg/cfg_i2c.h"
88  * $WIZ$ module_hw = "bertos/hw/hw_i2c_bitbang.h"
89  * $WIZ$ module_depends = "i2c_bitbang"
90  * $WIZ$ module_supports = "not atmega103 and not atmega168 and not at91"
91  */
92
93 #ifndef DRV_I2C_H
94 #define DRV_I2C_H
95
96 #include "cfg/cfg_i2c.h"
97
98 #include <cfg/compiler.h>
99 #include <cfg/macros.h>
100 #include <cfg/debug.h>
101
102 #include <cpu/attr.h>
103
104 #define I2C_READBIT BV(0)
105
106 /** \defgroup i2c_driver I2C driver
107  */
108
109 /*
110  * The following macros are needed to maintain compatibility with older i2c API.
111  * They can be safely removed once the old API is removed.
112  */
113
114  /**
115   * \addtogroup i2c_api
116   * \{
117   */
118 #if COMPILER_C99
119         #define i2c_init(...)           PP_CAT(i2c_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
120         #define i2c_start_w(...)        PP_CAT(i2c_start_w ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
121         #define i2c_start_r(...)        PP_CAT(i2c_start_r ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
122 #else
123         /**
124          * Initialize I2C module.
125          *
126          * To initialize the module you can write this code:
127          * \code
128          * I2c ctx;
129          * i2c_init(&ctx, 0, CONFIG_I2C_FREQ);
130          * \endcode
131          * This macro expands in two versions, depending on the number of
132          * parameters, to maintain compatibility with old API:
133          * \li i2c_init_3(I2c *i2c, int dev, uint32_t clock)
134          * \li i2c_init_0(void)
135          *
136          * Do NOT use the above functions directly, use i2c_init().
137          * \note Use the version with 3 parameters, the other one is only for
138          * legacy code.
139          */
140         #define i2c_init(args...)       PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
141
142         /**
143          * Start a write session.
144          *
145          * To start a write session, use the following code:
146          * \code
147          * i2c_start_w(i2c, dev, bytes, I2C_STOP);
148          * \endcode
149          *
150          * This macro expands in two versions, depending on the number of parameters:
151          * \li i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
152          * \li i2c_builtin_start_w(uint8_t id): Deprecated API, don't use in new projects
153          * \li i2c_bitbang_start_w(uint8_t id): Deprecated API, don't use in new projects
154          *
155          * Do NOT use the above functions directly, use i2c_start_w().
156          * \note Use the version with 4 parameters, the others are only for legacy code
157          */
158         #define i2c_start_w(args...)    PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)
159
160         /**
161          * Start a read session.
162          *
163          * To start a read session, use the following code:
164          * \code
165          * i2c_start_r(i2c, dev, bytes, I2C_STOP);
166          * \endcode
167          *
168          * This macro expands in two versions, depending on the number of parameters:
169          * \li i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
170          * \li i2c_builtin_start_r(uint8_t id): Deprecated API, don't use in new projects
171          * \li i2c_bitbang_start_r(uint8_t id): Deprecated API, don't use in new projects
172          *
173          * Do NOT use the above functions directly, use i2c_start_r().
174          * \note Use the version with 4 parameters, the others are only for legacy code
175          */
176         #define i2c_start_r(args...)    PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)
177 #endif
178 /**\}*/
179
180
181 /**
182  * \name I2C bitbang devices enum
183  */
184 enum
185 {
186         I2C_BITBANG_OLD = -1,
187         I2C_BITBANG0 = 1000, ///< Use bitbang on port 0
188         I2C_BITBANG1,        ///< Use bitbang on port 1
189         I2C_BITBANG2,
190         I2C_BITBANG3,
191         I2C_BITBANG4,
192         I2C_BITBANG5,
193         I2C_BITBANG6,
194         I2C_BITBANG7,
195         I2C_BITBANG8,
196         I2C_BITBANG9,
197
198         I2C_BITBANG_CNT  /**< Number of i2c ports */
199 };
200
201 /** \defgroup i2c_api I2C driver API
202  * \ingroup i2c_driver
203  * \{
204  */
205
206 /**
207  * \name I2C error flags
208  * \ingroup i2c_api
209  * @{
210  */
211 #define I2C_OK               0     ///< I2C no errors flag
212 #define I2C_DATA_NACK     BV(4)    ///< I2C generic error
213 #define I2C_ERR           BV(3)    ///< I2C generic error
214 #define I2C_ARB_LOST      BV(2)    ///< I2C arbitration lost error
215 #define I2C_START_TIMEOUT BV(0)    ///< I2C timeout error on start
216 #define I2C_NO_ACK        BV(1)    ///< I2C no ack for sla start
217 /**@}*/
218
219 /**
220  * \name I2C command flags
221  * \ingroup i2c_api
222  * @{
223  */
224 #define I2C_NOSTOP           0    ///< Do not program the stop for current transition
225 #define I2C_STOP          BV(0)   ///< Program the stop for current transition
226 /** @} */
227 #define I2C_START_R       BV(1)   // Start read command
228 #define I2C_START_W          0    // Start write command
229
230
231 #define I2C_TEST_START(flag)  ((flag) & I2C_START_R)
232 #define I2C_TEST_STOP(flag)   ((flag) & I2C_STOP)
233
234 struct I2cHardware;
235 struct I2c;
236
237 typedef void (*i2c_start_t)(struct I2c *i2c, uint16_t slave_addr);
238 typedef uint8_t (*i2c_getc_t)(struct I2c *i2c);
239 typedef void (*i2c_putc_t)(struct I2c *i2c, uint8_t data);
240 typedef void (*i2c_write_t)(struct I2c *i2c, const void *_buf, size_t count);
241 typedef void (*i2c_read_t)(struct I2c *i2c, void *_buf, size_t count);
242
243 typedef struct I2cVT
244 {
245         i2c_start_t start;
246         i2c_getc_t   getc;
247         i2c_putc_t   putc;
248         i2c_write_t  write;
249         i2c_read_t   read;
250 } I2cVT;
251
252 typedef struct I2c
253 {
254         int errors;
255         int flags;
256         size_t xfer_size;
257         struct I2cHardware* hw;
258         const struct I2cVT *vt;
259 } I2c;
260
261
262 #include CPU_HEADER(i2c)
263
264 /*
265  * Low level i2c  init implementation prototype.
266  */
267 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
268 void i2c_hw_bitbangInit(I2c *i2c, int dev);
269
270 void i2c_genericWrite(I2c *i2c, const void *_buf, size_t count);
271 void i2c_genericRead(I2c *i2c, void *_buf, size_t count);
272
273 /*
274  * Start a i2c transfer.
275  *
276  * \param i2c Context structure.
277  * \param slave_addr Address of slave device
278  * \param size Size of the transfer
279  */
280 INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
281 {
282         ASSERT(i2c->vt);
283         ASSERT(i2c->vt->start);
284
285         if (!i2c->errors)
286                 ASSERT(i2c->xfer_size == 0);
287
288         i2c->errors = 0;
289         i2c->xfer_size = size;
290
291         i2c->vt->start(i2c, slave_addr);
292 }
293
294 /**
295  * \name I2C interface functions
296  * \ingroup i2c_api
297  * @{
298  */
299
300 /**
301  * Start a read session.
302  * \param i2c I2C context
303  * \param slave_addr Address of the slave device
304  * \param size Number of bytes to be read from device
305  * \param flags Session flags (I2C command flags)
306  */
307 INLINE void i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
308 {
309         ASSERT(i2c);
310         i2c->flags = flags | I2C_START_R;
311         i2c_start(i2c, slave_addr, size);
312 }
313
314 /**
315  * Start a write session.
316  * \param i2c I2C context
317  * \param slave_addr Address of the slave device
318  * \param size Size to be transferred
319  * \param flags Session flags
320  */
321 INLINE void i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
322 {
323         ASSERT(i2c);
324         i2c->flags = flags & ~I2C_START_R;
325         i2c_start(i2c, slave_addr, size);
326 }
327
328 /**
329  * Read a byte from I2C bus.
330  * \param i2c I2C context
331  * \return Byte read
332  */
333 INLINE uint8_t i2c_getc(I2c *i2c)
334 {
335         ASSERT(i2c);
336         ASSERT(i2c->vt);
337         ASSERT(i2c->vt->getc);
338
339         ASSERT(i2c->xfer_size);
340
341         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
342
343         if (!i2c->errors)
344         {
345                 uint8_t data = i2c->vt->getc(i2c);
346                 i2c->xfer_size--;
347                 return data;
348         }
349         else
350                 return 0xFF;
351 }
352
353 /**
354  * Write the byte \a data into I2C port \a i2c.
355  * \param i2c I2C context
356  * \param data Byte to be written
357  */
358 INLINE void i2c_putc(I2c *i2c, uint8_t data)
359 {
360         ASSERT(i2c);
361         ASSERT(i2c->vt);
362         ASSERT(i2c->vt->putc);
363
364         ASSERT(i2c->xfer_size);
365
366         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
367
368         if (!i2c->errors)
369         {
370                 i2c->vt->putc(i2c, data);
371                 i2c->xfer_size--;
372         }
373 }
374
375 /**
376  * Write \a count bytes to port \a i2c, reading from \a _buf.
377  * \param i2c I2C context
378  * \param _buf User buffer to read from
379  * \param count Number of bytes to write
380  */
381 INLINE void i2c_write(I2c *i2c, const void *_buf, size_t count)
382 {
383         ASSERT(i2c);
384         ASSERT(i2c->vt);
385         ASSERT(i2c->vt->write);
386
387         ASSERT(_buf);
388         ASSERT(count);
389         ASSERT(count <= i2c->xfer_size);
390
391         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
392
393         if (!i2c->errors)
394                 i2c->vt->write(i2c, _buf, count);
395 }
396
397 /**
398  * Read \a count bytes into buffer \a _buf from device \a i2c.
399  * \param i2c Context structure
400  * \param _buf Buffer to fill
401  * \param count Number of bytes to read
402  */
403 INLINE void i2c_read(I2c *i2c, void *_buf, size_t count)
404 {
405         ASSERT(i2c);
406         ASSERT(i2c->vt);
407         ASSERT(i2c->vt->read);
408
409         ASSERT(_buf);
410         ASSERT(count);
411         ASSERT(count <= i2c->xfer_size);
412
413         ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
414
415         if (!i2c->errors)
416                 i2c->vt->read(i2c, _buf, count);
417 }
418
419 /**
420  * Return the error condition of the bus and clear errors.
421  */
422 INLINE int i2c_error(I2c *i2c)
423 {
424         ASSERT(i2c);
425         int err = i2c->errors;
426         i2c->errors = 0;
427
428         return err;
429 }
430
431 /**
432  * Initialize I2C context structure.
433  * \param i2c I2C context structure
434  * \param dev Number of device to be initialized. You can use I2C_BITBANG0
435  *            and similar if you want to activate the bitbang driver.
436  * \param clock Peripheral clock
437  */
438 #define i2c_init_3(i2c, dev, clock)   ((((dev) >= I2C_BITBANG0) | ((dev) == I2C_BITBANG_OLD)) ? \
439                                                                                 i2c_hw_bitbangInit((i2c), (dev)) : i2c_hw_init((i2c), (dev), (clock)))
440 /**@}*/
441 /**\}*/ // i2c_api
442
443 /**
444  * \defgroup old_i2c_api Old I2C API
445  * \ingroup i2c_driver
446  *
447  * This is the old and deprecated I2C API. It is maintained for backward
448  * compatibility only, don't use it in new projects.
449  * @{
450  */
451 #if !CONFIG_I2C_DISABLE_OLD_API
452
453 /**
454  * \ingroup old_i2c_api
455  * \name I2C Backends.
456  * Sometimes your cpu does not have a builtin
457  * i2c driver or you don't want, for some reason, to
458  * use that.
459  * With this you can choose, at compile time, which backend to use.
460  * Set the CONFIG_I2C_BACKEND configuration variable in cfg_i2c.h
461  * @{
462  */
463 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
464 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
465 /**@}*/
466
467 /**
468  * \name I2c builtin prototypes.
469  * \ingroup old_i2c_api
470  * Do NOT use these function directly, instead,
471  * you can call the ones named without "_builtin_"
472  * and specify in cfg_i2c.h (CONFIG_I2C_BACKEND)
473  * that you want the builtin backend.
474  * @{
475  */
476 bool i2c_builtin_start_w(uint8_t id);
477 bool i2c_builtin_start_r(uint8_t id);
478 void i2c_builtin_stop(void);
479 bool i2c_builtin_put(uint8_t _data);
480 int i2c_builtin_get(bool ack);
481 /**@}*/
482
483 /**
484  * \name I2c bitbang prototypes.
485  * \ingroup old_i2c_api
486  * Do NOT use these function directly, instead,
487  * you can call the ones named without "_bitbang_"
488  * and specify in cfg_i2c.h (CONFIG_I2C_BACKEND)
489  * that you want the bitbang backend.
490  * @{
491  */
492 bool i2c_bitbang_start_w(uint8_t id);
493 bool i2c_bitbang_start_r(uint8_t id);
494 void i2c_bitbang_stop(void);
495 bool i2c_bitbang_put(uint8_t _data);
496 int i2c_bitbang_get(bool ack);
497 /**@}*/
498
499 #ifndef CONFIG_I2C_BACKEND
500 #define  CONFIG_I2C_BACKEND  I2C_BACKEND_BUILTIN
501 #endif
502
503 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
504         #define i2c_start_w_1   i2c_builtin_start_w
505         #define i2c_start_r_1   i2c_builtin_start_r
506         #define i2c_stop        i2c_builtin_stop
507         #define i2c_put         i2c_builtin_put
508         #define i2c_get         i2c_builtin_get
509 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
510         #define i2c_start_w_1   i2c_bitbang_start_w
511         #define i2c_start_r_1   i2c_bitbang_start_r
512         #define i2c_stop        i2c_bitbang_stop
513         #define i2c_put         i2c_bitbang_put
514         #define i2c_get         i2c_bitbang_get
515 #else
516         #error Unsupported i2c backend.
517 #endif
518
519
520 bool i2c_send(const void *_buf, size_t count);
521 bool i2c_recv(void *_buf, size_t count);
522
523 /**@}*/
524
525
526 extern I2c local_i2c_old_api;
527
528 /**
529  * Initialize I2C module (old API).
530  * \attention This function is deprecated. Use i2c_init(args...) in new code
531  */
532 INLINE void i2c_init_0(void)
533 {
534         #if CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
535                 i2c_init_3(&local_i2c_old_api, I2C_BITBANG_OLD, CONFIG_I2C_FREQ);
536         #else
537                 i2c_init_3(&local_i2c_old_api, 0, CONFIG_I2C_FREQ);
538         #endif
539 }
540 #endif /* !CONFIG_I2C_DISABLE_OLD_API */
541
542
543
544 #endif