4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
34 * \brief I2C generic driver functions.
36 * Some hardware requires you to declare the number of transferred
37 * bytes and the communication direction before actually reading or writing
39 * Furthermore, sometimes you need to specify the first transferred byte
40 * before any data is sent over the bus.
42 * The usage pattern for writing is the following:
46 * i2c_start_w(args...);
47 * i2c_write(i2c, buf, len);
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
53 * Reading is a bit more complicated and it largely depends on the specific
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
59 * Here is an example of how you can deal with such hardware:
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);
67 * // check for errors during setup
69 * // now start the real data transfer
70 * i2c_start_r(i2c, dev, bytes, I2C_STOP);
71 * i2c_read(i2c, buf, bytes);
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.
81 * You can check error conditions by calling the function i2c_error after
82 * each function call. (This is similar to libc errno handling).
84 * \author Francesco Sacchi <batt@develer.com>
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"
96 #include "cfg/cfg_i2c.h"
98 #include <cfg/compiler.h>
99 #include <cfg/macros.h>
100 #include <cfg/debug.h>
102 #include <cpu/attr.h>
104 #define I2C_READBIT BV(0)
106 /** \defgroup i2c_driver I2C driver
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.
115 * \addtogroup i2c_api
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__)
124 * Initialize I2C module.
126 * To initialize the module you can write this code:
129 * i2c_init(&ctx, 0, CONFIG_I2C_FREQ);
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)
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
140 #define i2c_init(args...) PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
143 * Start a write session.
145 * To start a write session, use the following code:
147 * i2c_start_w(i2c, dev, bytes, I2C_STOP);
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
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
158 #define i2c_start_w(args...) PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)
161 * Start a read session.
163 * To start a read session, use the following code:
165 * i2c_start_r(i2c, dev, bytes, I2C_STOP);
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
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
176 #define i2c_start_r(args...) PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)
182 * \name I2C bitbang devices enum
186 I2C_BITBANG_OLD = -1,
187 I2C_BITBANG0 = 1000, ///< Use bitbang on port 0
188 I2C_BITBANG1, ///< Use bitbang on port 1
198 I2C_BITBANG_CNT /**< Number of i2c ports */
201 /** \defgroup i2c_api I2C driver API
202 * \ingroup i2c_driver
207 * \name I2C error flags
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
220 * \name I2C command flags
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
227 #define I2C_START_R BV(1) // Start read command
228 #define I2C_START_W 0 // Start write command
231 #define I2C_TEST_START(flag) ((flag) & I2C_START_R)
232 #define I2C_TEST_STOP(flag) ((flag) & I2C_STOP)
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);
257 struct I2cHardware* hw;
258 const struct I2cVT *vt;
262 #include CPU_HEADER(i2c)
265 * Low level i2c init implementation prototype.
267 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
268 void i2c_hw_bitbangInit(I2c *i2c, int dev);
270 void i2c_genericWrite(I2c *i2c, const void *_buf, size_t count);
271 void i2c_genericRead(I2c *i2c, void *_buf, size_t count);
274 * Start a i2c transfer.
276 * \param i2c Context structure.
277 * \param slave_addr Address of slave device
278 * \param size Size of the transfer
280 INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
283 ASSERT(i2c->vt->start);
286 ASSERT(i2c->xfer_size == 0);
289 i2c->xfer_size = size;
291 i2c->vt->start(i2c, slave_addr);
295 * \name I2C interface functions
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)
307 INLINE void i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
310 i2c->flags = flags | I2C_START_R;
311 i2c_start(i2c, slave_addr, size);
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
321 INLINE void i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
324 i2c->flags = flags & ~I2C_START_R;
325 i2c_start(i2c, slave_addr, size);
329 * Read a byte from I2C bus.
330 * \param i2c I2C context
333 INLINE uint8_t i2c_getc(I2c *i2c)
337 ASSERT(i2c->vt->getc);
339 ASSERT(i2c->xfer_size);
341 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
345 uint8_t data = i2c->vt->getc(i2c);
354 * Write the byte \a data into I2C port \a i2c.
355 * \param i2c I2C context
356 * \param data Byte to be written
358 INLINE void i2c_putc(I2c *i2c, uint8_t data)
362 ASSERT(i2c->vt->putc);
364 ASSERT(i2c->xfer_size);
366 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
370 i2c->vt->putc(i2c, data);
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
381 INLINE void i2c_write(I2c *i2c, const void *_buf, size_t count)
385 ASSERT(i2c->vt->write);
389 ASSERT(count <= i2c->xfer_size);
391 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
394 i2c->vt->write(i2c, _buf, count);
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
403 INLINE void i2c_read(I2c *i2c, void *_buf, size_t count)
407 ASSERT(i2c->vt->read);
411 ASSERT(count <= i2c->xfer_size);
413 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
416 i2c->vt->read(i2c, _buf, count);
420 * Return the error condition of the bus and clear errors.
422 INLINE int i2c_error(I2c *i2c)
425 int err = i2c->errors;
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
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)))
444 * \defgroup old_i2c_api Old I2C API
445 * \ingroup i2c_driver
447 * This is the old and deprecated I2C API. It is maintained for backward
448 * compatibility only, don't use it in new projects.
451 #if !CONFIG_I2C_DISABLE_OLD_API
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
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
463 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
464 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
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.
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);
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.
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);
499 #ifndef CONFIG_I2C_BACKEND
500 #define CONFIG_I2C_BACKEND I2C_BACKEND_BUILTIN
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
516 #error Unsupported i2c backend.
520 bool i2c_send(const void *_buf, size_t count);
521 bool i2c_recv(void *_buf, size_t count);
526 extern I2c local_i2c_old_api;
529 * Initialize I2C module (old API).
530 * \attention This function is deprecated. Use i2c_init(args...) in new code
532 INLINE void i2c_init_0(void)
534 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
535 i2c_init_3(&local_i2c_old_api, I2C_BITBANG_OLD, CONFIG_I2C_FREQ);
537 i2c_init_3(&local_i2c_old_api, 0, CONFIG_I2C_FREQ);
540 #endif /* !CONFIG_I2C_DISABLE_OLD_API */