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_supports = "not atmega103 and not atmega168 and not at91"
46 #include "cfg/cfg_i2c.h"
47 #include <cfg/compiler.h>
49 #define I2C_READBIT BV(0)
51 #define i2c_init(FN_ARGS) CAT(fn ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS)
55 * Sometimes your cpu does not have a builtin
56 * i2c driver or you don't want, for some reason, to
58 * With this you can choose, at compile time, which backend to use.
60 * $WIZ$ i2c_backend = "I2C_BACKEND_BUILTIN", "I2C_BACKEND_BITBANG"
62 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
63 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
67 * I2c builtin prototypes.
68 * Do NOT use these function directly, instead,
69 * you can call the ones named without "_builtin_"
70 * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
71 * that you want the builtin backend.
74 void i2c_builtin_init(void);
75 bool i2c_builtin_start_w(uint8_t id);
76 bool i2c_builtin_start_r(uint8_t id);
77 void i2c_builtin_stop(void);
78 bool i2c_builtin_put(uint8_t _data);
79 int i2c_builtin_get(bool ack);
83 * I2c bitbang prototypes.
84 * Same thing here: do NOT use these function directly, instead,
85 * you can call the ones named without "_bitbang_"
86 * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
87 * that you want the bitbang backend.
90 void i2c_bitbang_init(void);
91 bool i2c_bitbang_start_w(uint8_t id);
92 bool i2c_bitbang_start_r(uint8_t id);
93 void i2c_bitbang_stop(void);
94 bool i2c_bitbang_put(uint8_t _data);
95 int i2c_bitbang_get(bool ack);
98 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
99 #define i2c_init_0 i2c_builtin_init
100 #define i2c_start_w i2c_builtin_start_w
101 #define i2c_start_r i2c_builtin_start_r
102 #define i2c_stop i2c_builtin_stop
103 #define i2c_put i2c_builtin_put
104 #define i2c_get i2c_builtin_get
105 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
106 #define i2c_init_0 i2c_bitbang_init
107 #define i2c_start_w i2c_bitbang_start_w
108 #define i2c_start_r i2c_bitbang_start_r
109 #define i2c_stop i2c_bitbang_stop
110 #define i2c_put i2c_bitbang_put
111 #define i2c_get i2c_bitbang_get
113 #error Unsupported i2c backend.
116 bool i2c_send(const void *_buf, size_t count);
117 bool i2c_recv(void *_buf, size_t count);
126 #include CPU_HEADER(i2c)
129 typedef int (*i2c_writeRope_t)(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len, ...);
130 typedef int (*i2c_readRope_t)(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len, ...);
135 i2c_writeRope_t write;
138 struct I2cHardware* hw;
141 void i2c_init_3(I2c *i2c, int dev, uint32_t clock)
143 i2c_hw_init(I2c *i2c, int dev, uint32_t clock);
146 #define i2c_write(FN_ARGS) CAT(fn ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS)
147 #define i2c_read(FN_ARGS) CAT(fn ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS)
150 * Overloaded functions definition.
152 int i2c_write_5(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len)
154 return i2c->write(i2c, slave_addr, flags, buf, len, NULL);
157 int i2c_read_5(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len);
159 return i2c->read(i2c, slave_addr, flags, buf, len, NULL);
163 int i2c_write_7(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
164 const void *buf1, size_t len1);
166 return i2c->write(i2c, slave_addr, flags, buf, len,
170 int i2c_read_7(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
171 void *buf1, size_t len1);
174 return i2c->read(i2c, slave_addr, flags, buf, len,
179 int i2c_write_9(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
180 const void *buf1, size_t len1,
181 const void *buf2, size_t len2);
183 return i2c->write(i2c, slave_addr, flags, buf, len,
188 int i2c_read_9(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
189 void *buf1, size_t len1,
190 void *buf2, size_t len2);
192 return i2c->read(i2c, slave_addr, flags, buf, len,
197 int i2c_write_11(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
198 const void *buf1, size_t len1,
199 const void *buf2, size_t len2
200 const void *buf3, size_t len3);
202 return i2c->write(i2c, slave_addr, flags, buf, len,
208 int i2c_read_11(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
209 void *buf1, size_t len1,
210 void *buf2, size_t len2
211 void *buf3, size_t len3);
214 return i2c->read(i2c, slave_addr, flags, buf, len,