Add definition and function for new i2c api.
[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  * \brief I2C generic driver functions.
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
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"
41  */
42
43 #ifndef DRV_I2C_H
44 #define DRV_I2C_H
45
46 #include "cfg/cfg_i2c.h"
47 #include <cfg/compiler.h>
48
49 #define I2C_READBIT BV(0)
50
51 /**
52  * I2C Backends.
53  * Sometimes your cpu does not have a builtin
54  * i2c driver or you don't want, for some reason, to
55  * use that.
56  * With this you can choose, at compile time, which backend to use.
57  *
58  * $WIZ$ i2c_backend = "I2C_BACKEND_BUILTIN", "I2C_BACKEND_BITBANG"
59  */
60 #define I2C_BACKEND_BUILTIN 0 ///< Uses cpu builtin i2c driver
61 #define I2C_BACKEND_BITBANG 1 ///< Uses emulated bitbang driver
62
63
64 /**
65  * I2c builtin prototypes.
66  * Do NOT use these function directly, instead,
67  * you can call the ones named without "_builtin_"
68  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
69  * that you want the builtin backend.
70  * \{
71  */
72 void i2c_builtin_init(void);
73 bool i2c_builtin_start_w(uint8_t id);
74 bool i2c_builtin_start_r(uint8_t id);
75 void i2c_builtin_stop(void);
76 bool i2c_builtin_put(uint8_t _data);
77 int i2c_builtin_get(bool ack);
78 /*\}*/
79
80 /**
81  * I2c bitbang prototypes.
82  * Same thing here: do NOT use these function directly, instead,
83  * you can call the ones named without "_bitbang_"
84  * and specify in cfg_i2c.h ( \see CONFIG_I2C_BACKEND)
85  * that you want the bitbang backend.
86  * \{
87  */
88 void i2c_bitbang_init(void);
89 bool i2c_bitbang_start_w(uint8_t id);
90 bool i2c_bitbang_start_r(uint8_t id);
91 void i2c_bitbang_stop(void);
92 bool i2c_bitbang_put(uint8_t _data);
93 int i2c_bitbang_get(bool ack);
94 /*\}*/
95
96 #if CONFIG_I2C_BACKEND == I2C_BACKEND_BUILTIN
97         #define i2c_init    i2c_builtin_init
98         #define i2c_start_w i2c_builtin_start_w
99         #define i2c_start_r i2c_builtin_start_r
100         #define i2c_stop    i2c_builtin_stop
101         #define i2c_put     i2c_builtin_put
102         #define i2c_get     i2c_builtin_get
103 #elif CONFIG_I2C_BACKEND == I2C_BACKEND_BITBANG
104         #define i2c_init    i2c_bitbang_init
105         #define i2c_start_w i2c_bitbang_start_w
106         #define i2c_start_r i2c_bitbang_start_r
107         #define i2c_stop    i2c_bitbang_stop
108         #define i2c_put     i2c_bitbang_put
109         #define i2c_get     i2c_bitbang_get
110 #else
111         #error Unsupported i2c backend.
112 #endif
113
114 bool i2c_send(const void *_buf, size_t count);
115 bool i2c_recv(void *_buf, size_t count);
116
117
118
119
120 /*
121  * I2c new api
122  *
123  */
124 #include CPU_HEADER(i2c)
125
126 struct I2cHardware;
127 typedef int (*i2c_writeRope_t)(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len, ...);
128 typedef int (*i2c_readRope_t)(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len, ...);
129
130 typedef struct I2c
131 {
132         int dev;
133         i2c_writeRope_t write;
134         i2c_readRope_t read;
135
136         struct I2cHardware* hw;
137 } I2c;
138
139 void i2c_init(I2c *i2c, int dev, uint32_t clock);
140
141 #define i2c_write(FN_ARGS) CAT(fn ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS)
142 #define i2c_read(FN_ARGS) CAT(fn ## _, COUNT_PARMS(FN_ARGS)) (FN_ARGS)
143
144 /*
145  * Overloaded functions definition.
146  */
147 int i2c_write_5(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len);
148 int i2c_read_5(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len);
149
150 int i2c_write_7(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
151                                                                                                                   const void *buf1, size_t len1);
152 int i2c_read_7(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
153                                                                                                                  void *buf1, size_t len1);
154
155 int i2c_write_9(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
156                                                                                                                   const void *buf1, size_t len1,
157                                                                                                                   const void *buf2, size_t len2);
158 int i2c_read_9(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
159                                                                                                                  void *buf1, size_t len1,
160                                                                                                                  void *buf2, size_t len2);
161
162 int i2c_write_11(I2c *i2c, uint16_t slave_addr, int flags, const void *buf, size_t len,
163                                                                                                                   const void *buf1, size_t len1,
164                                                                                                                   const void *buf2, size_t len2
165                                                                                                                   const void *buf3, size_t len3);
166 int i2c_read_11(I2c *i2c, uint16_t slave_addr, int flags, void *buf, size_t len,
167                                                                                                                  void *buf1, size_t len1,
168                                                                                                                  void *buf2, size_t len2
169                                                                                                                  void *buf3, size_t len3);
170
171
172 #endif