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 */
202 * \defgroup old_i2c_api Old I2C API
203 * \ingroup i2c_driver
205 * This is the old and deprecated I2C API. It is maintained for backward
206 * compatibility only, don't use it in new projects.
209 #if !CONFIG_I2C_DISABLE_OLD_API
212 * \ingroup old_i2c_api
214 * Sometimes your cpu does not have a builtin
215 * i2c driver or you don't want, for some reason, to
217 * With this you can choose, at compile time, which backend to use.
220 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
221 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
225 * \name I2c builtin prototypes.
226 * \ingroup old_i2c_api
227 * Do NOT use these function directly, instead,
228 * you can call the ones named without "_builtin_"
229 * and specify in cfg_i2c.h (CONFIG_I2C_BACKEND)
230 * that you want the builtin backend.
233 bool i2c_builtin_start_w(uint8_t id);
234 bool i2c_builtin_start_r(uint8_t id);
235 void i2c_builtin_stop(void);
236 bool i2c_builtin_put(uint8_t _data);
237 int i2c_builtin_get(bool ack);
241 * \name I2c bitbang prototypes.
242 * \ingroup old_i2c_api
243 * Do NOT use these function directly, instead,
244 * you can call the ones named without "_bitbang_"
245 * and specify in cfg_i2c.h (CONFIG_I2C_BACKEND)
246 * that you want the bitbang backend.
249 bool i2c_bitbang_start_w(uint8_t id);
250 bool i2c_bitbang_start_r(uint8_t id);
251 void i2c_bitbang_stop(void);
252 bool i2c_bitbang_put(uint8_t _data);
253 int i2c_bitbang_get(bool ack);
256 #ifndef CONFIG_I2C_BACKEND
257 #define CONFIG_I2C_BACKEND I2C_BACKEND_BUILTIN
260 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
261 #define i2c_start_w_1 i2c_builtin_start_w
262 #define i2c_start_r_1 i2c_builtin_start_r
263 #define i2c_stop i2c_builtin_stop
264 #define i2c_put i2c_builtin_put
265 #define i2c_get i2c_builtin_get
266 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
267 #define i2c_start_w_1 i2c_bitbang_start_w
268 #define i2c_start_r_1 i2c_bitbang_start_r
269 #define i2c_stop i2c_bitbang_stop
270 #define i2c_put i2c_bitbang_put
271 #define i2c_get i2c_bitbang_get
273 #error Unsupported i2c backend.
277 bool i2c_send(const void *_buf, size_t count);
278 bool i2c_recv(void *_buf, size_t count);
280 #endif /* !CONFIG_I2C_DISABLE_OLD_API */
283 /** \defgroup i2c_api I2C driver API
284 * \ingroup i2c_driver
289 * \name I2C error flags
293 #define I2C_OK 0 ///< I2C no errors flag
294 #define I2C_DATA_NACK BV(4) ///< I2C generic error
295 #define I2C_ERR BV(3) ///< I2C generic error
296 #define I2C_ARB_LOST BV(2) ///< I2C arbitration lost error
297 #define I2C_START_TIMEOUT BV(0) ///< I2C timeout error on start
298 #define I2C_NO_ACK BV(1) ///< I2C no ack for sla start
302 * \name I2C command flags
306 #define I2C_NOSTOP 0 ///< Do not program the stop for current transition
307 #define I2C_STOP BV(0) ///< Program the stop for current transition
309 #define I2C_START_R BV(1) // Start read command
310 #define I2C_START_W 0 // Start write command
313 #define I2C_TEST_START(flag) ((flag) & I2C_START_R)
314 #define I2C_TEST_STOP(flag) ((flag) & I2C_STOP)
319 typedef void (*i2c_start_t)(struct I2c *i2c, uint16_t slave_addr);
320 typedef uint8_t (*i2c_getc_t)(struct I2c *i2c);
321 typedef void (*i2c_putc_t)(struct I2c *i2c, uint8_t data);
322 typedef void (*i2c_write_t)(struct I2c *i2c, const void *_buf, size_t count);
323 typedef void (*i2c_read_t)(struct I2c *i2c, void *_buf, size_t count);
339 struct I2cHardware* hw;
340 const struct I2cVT *vt;
344 #include CPU_HEADER(i2c)
347 * Low level i2c init implementation prototype.
349 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
350 void i2c_hw_bitbangInit(I2c *i2c, int dev);
352 void i2c_genericWrite(I2c *i2c, const void *_buf, size_t count);
353 void i2c_genericRead(I2c *i2c, void *_buf, size_t count);
356 * Start a i2c transfer.
358 * \param i2c Context structure.
359 * \param slave_addr Address of slave device
360 * \param size Size of the transfer
362 INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
365 ASSERT(i2c->vt->start);
368 ASSERT(i2c->xfer_size == 0);
371 i2c->xfer_size = size;
373 i2c->vt->start(i2c, slave_addr);
377 * \name I2C interface functions
383 * Start a read session.
384 * \param i2c I2C context
385 * \param slave_addr Address of the slave device
386 * \param size Number of bytes to be read from device
387 * \param flags Session flags (I2C command flags)
389 INLINE void i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
392 i2c->flags = flags | I2C_START_R;
393 i2c_start(i2c, slave_addr, size);
397 * Start a write session.
398 * \param i2c I2C context
399 * \param slave_addr Address of the slave device
400 * \param size Size to be transferred
401 * \param flags Session flags
403 INLINE void i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
406 i2c->flags = flags & ~I2C_START_R;
407 i2c_start(i2c, slave_addr, size);
411 * Read a byte from I2C bus.
412 * \param i2c I2C context
415 INLINE uint8_t i2c_getc(I2c *i2c)
419 ASSERT(i2c->vt->getc);
421 ASSERT(i2c->xfer_size);
423 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
427 uint8_t data = i2c->vt->getc(i2c);
436 * Write the byte \a data into I2C port \a i2c.
437 * \param i2c I2C context
438 * \param data Byte to be written
440 INLINE void i2c_putc(I2c *i2c, uint8_t data)
444 ASSERT(i2c->vt->putc);
446 ASSERT(i2c->xfer_size);
448 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
452 i2c->vt->putc(i2c, data);
458 * Write \a count bytes to port \a i2c, reading from \a _buf.
459 * \param i2c I2C context
460 * \param _buf User buffer to read from
461 * \param count Number of bytes to write
463 INLINE void i2c_write(I2c *i2c, const void *_buf, size_t count)
467 ASSERT(i2c->vt->write);
471 ASSERT(count <= i2c->xfer_size);
473 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
476 i2c->vt->write(i2c, _buf, count);
480 * Read \a count bytes into buffer \a _buf from device \a i2c.
481 * \param i2c Context structure
482 * \param _buf Buffer to fill
483 * \param count Number of bytes to read
485 INLINE void i2c_read(I2c *i2c, void *_buf, size_t count)
489 ASSERT(i2c->vt->read);
493 ASSERT(count <= i2c->xfer_size);
495 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
498 i2c->vt->read(i2c, _buf, count);
502 * Return the error condition of the bus and clear errors.
504 INLINE int i2c_error(I2c *i2c)
507 int err = i2c->errors;
514 * Initialize I2C context structure.
515 * \param i2c I2C context structure
516 * \param dev Number of device to be initialized. You can use I2C_BITBANG0
517 * and similar if you want to activate the bitbang driver.
518 * \param clock Peripheral clock
520 #define i2c_init_3(i2c, dev, clock) ((((dev) >= I2C_BITBANG0) | ((dev) == I2C_BITBANG_OLD)) ? \
521 i2c_hw_bitbangInit((i2c), (dev)) : i2c_hw_init((i2c), (dev), (clock)))
525 #if !CONFIG_I2C_DISABLE_OLD_API
527 extern I2c local_i2c_old_api;
530 * Initialize I2C module (old API).
531 * \attention This function is deprecated. Use i2c_init(args...) in new code
533 INLINE void i2c_init_0(void)
535 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
536 i2c_init_3(&local_i2c_old_api, I2C_BITBANG_OLD, CONFIG_I2C_FREQ);
538 i2c_init_3(&local_i2c_old_api, 0, CONFIG_I2C_FREQ);
541 #endif /* !CONFIG_I2C_DISABLE_OLD_API */