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/)
33 * \brief I2C generic driver functions.
35 * \author Francesco Sacchi <batt@develer.com>
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_depends = "i2c_bitbang"
41 * $WIZ$ module_supports = "not atmega103 and not atmega168 and not at91"
47 #include "cfg/cfg_i2c.h"
49 #include <cfg/compiler.h>
50 #include <cfg/macros.h>
51 #include <cfg/debug.h>
55 #define I2C_READBIT BV(0)
58 #define i2c_init(...) PP_CAT(i2c_init ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
59 #define i2c_start_w(...) PP_CAT(i2c_start_w ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
60 #define i2c_start_r(...) PP_CAT(i2c_start_r ## _, COUNT_PARMS(__VA_ARGS__)) (__VA_ARGS__)
62 #define i2c_init(args...) PP_CAT(i2c_init ## _, COUNT_PARMS(args)) (args)
63 #define i2c_start_w(args...) PP_CAT(i2c_start_w ## _, COUNT_PARMS(args)) (args)
64 #define i2c_start_r(args...) PP_CAT(i2c_start_r ## _, COUNT_PARMS(args)) (args)
68 #if !CONFIG_I2C_DISABLE_OLD_API
72 * Sometimes your cpu does not have a builtin
73 * i2c driver or you don't want, for some reason, to
75 * With this you can choose, at compile time, which backend to use.
77 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
78 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
81 * I2c builtin prototypes.
82 * Do NOT use these function directly, instead,
83 * you can call the ones named without "_builtin_"
84 * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
85 * that you want the builtin backend.
88 void i2c_builtin_init(void);
89 bool i2c_builtin_start_w(uint8_t id);
90 bool i2c_builtin_start_r(uint8_t id);
91 void i2c_builtin_stop(void);
92 bool i2c_builtin_put(uint8_t _data);
93 int i2c_builtin_get(bool ack);
97 * I2c bitbang prototypes.
98 * Same thing here: do NOT use these function directly, instead,
99 * you can call the ones named without "_bitbang_"
100 * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
101 * that you want the bitbang backend.
104 void i2c_bitbang_init(void);
105 bool i2c_bitbang_start_w(uint8_t id);
106 bool i2c_bitbang_start_r(uint8_t id);
107 void i2c_bitbang_stop(void);
108 bool i2c_bitbang_put(uint8_t _data);
109 int i2c_bitbang_get(bool ack);
112 #ifndef CONFIG_I2C_BACKEND
113 #define CONFIG_I2C_BACKEND I2C_BACKEND_BUILTIN
116 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
117 #define i2c_init_0 i2c_builtin_init
118 #define i2c_start_w_1 i2c_builtin_start_w
119 #define i2c_start_r_1 i2c_builtin_start_r
120 #define i2c_stop i2c_builtin_stop
121 #define i2c_put i2c_builtin_put
122 #define i2c_get i2c_builtin_get
123 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
124 #define i2c_init_0 i2c_bitbang_init
125 #define i2c_start_w_1 i2c_bitbang_start_w
126 #define i2c_start_r_1 i2c_bitbang_start_r
127 #define i2c_stop i2c_bitbang_stop
128 #define i2c_put i2c_bitbang_put
129 #define i2c_get i2c_bitbang_get
131 #error Unsupported i2c backend.
135 bool i2c_send(const void *_buf, size_t count);
136 bool i2c_recv(void *_buf, size_t count);
138 #endif /* !CONFIG_I2C_DISABLE_OLD_API */
146 * \name I2C bitbang devices enum
161 I2C_BITBANG_CNT /**< Number of serial ports */
167 #define I2C_OK 0 ///< I2C no errors flag
168 #define I2C_DATA_NACK BV(4) ///< I2C generic error
169 #define I2C_ERR BV(3) ///< I2C generic error
170 #define I2C_ARB_LOST BV(2) ///< I2C arbitration lost error
171 #define I2C_START_TIMEOUT BV(0) ///< I2C timeout error on start
172 #define I2C_NO_ACK BV(1) ///< I2C no ack for sla start
177 #define I2C_NOSTOP 0 ///< Do not program the stop for current transition
178 #define I2C_STOP BV(0) ///< Program the stop for current transition
179 #define I2C_START_R BV(1) ///< Start read command
180 #define I2C_START_W 0 ///< Start write command
183 #define I2C_TEST_START(flag) ((flag) & I2C_START_R)
184 #define I2C_TEST_STOP(flag) ((flag) & I2C_STOP)
189 typedef void (*i2c_start_t)(struct I2c *i2c, uint16_t slave_addr);
190 typedef uint8_t (*i2c_getc_t)(struct I2c *i2c);
191 typedef void (*i2c_putc_t)(struct I2c *i2c, uint8_t data);
192 typedef void (*i2c_write_t)(struct I2c *i2c, const void *_buf, size_t count);
193 typedef void (*i2c_read_t)(struct I2c *i2c, void *_buf, size_t count);
209 struct I2cHardware* hw;
210 const struct I2cVT *vt;
214 #include CPU_HEADER(i2c)
217 * Low level i2c init implementation prototype.
219 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
220 void i2c_hw_bitbangInit(I2c *i2c, int dev);
222 void i2c_genericWrite(I2c *i2c, const void *_buf, size_t count);
223 void i2c_genericRead(I2c *i2c, void *_buf, size_t count);
225 INLINE void i2c_start(I2c *i2c, uint16_t slave_addr, size_t size)
228 ASSERT(i2c->vt->start);
231 ASSERT(i2c->xfer_size == 0);
234 i2c->xfer_size = size;
236 i2c->vt->start(i2c, slave_addr);
239 INLINE void i2c_start_r_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
242 i2c->flags = flags | I2C_START_R;
243 i2c_start(i2c, slave_addr, size);
246 INLINE void i2c_start_w_4(I2c *i2c, uint16_t slave_addr, size_t size, int flags)
249 i2c->flags = flags & ~I2C_START_R;
250 i2c_start(i2c, slave_addr, size);
253 INLINE uint8_t i2c_getc(I2c *i2c)
257 ASSERT(i2c->vt->getc);
259 ASSERT(i2c->xfer_size);
261 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
265 uint8_t data = i2c->vt->getc(i2c);
273 INLINE void i2c_putc(I2c *i2c, uint8_t data)
277 ASSERT(i2c->vt->putc);
279 ASSERT(i2c->xfer_size);
281 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
285 i2c->vt->putc(i2c, data);
290 INLINE void i2c_write(I2c *i2c, const void *_buf, size_t count)
294 ASSERT(i2c->vt->write);
298 ASSERT(count <= i2c->xfer_size);
300 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_W);
303 i2c->vt->write(i2c, _buf, count);
307 INLINE void i2c_read(I2c *i2c, void *_buf, size_t count)
311 ASSERT(i2c->vt->read);
315 ASSERT(count <= i2c->xfer_size);
317 ASSERT(I2C_TEST_START(i2c->flags) == I2C_START_R);
320 i2c->vt->read(i2c, _buf, count);
323 INLINE int i2c_error(I2c *i2c)
326 int err = i2c->errors;
332 #define i2c_init_3(i2c, dev, clock) (dev > I2C_BITBANG0) ? i2c_hw_bitbangInit(i2c, dev) : i2c_hw_init(i2c, dev, clock)