First version of new i2c bitbang API.
[bertos.git] / bertos / drv / i2c_bitbang.c
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 bitbang driver (implementation)
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  */
37
38 #include "i2c.h"
39 #include "cfg/cfg_i2c.h"
40
41 #define LOG_LEVEL  I2C_LOG_LEVEL
42 #define LOG_FORMAT I2C_LOG_FORMAT
43
44 #include <cfg/log.h>
45 #include <cfg/macros.h>
46 #include <cfg/module.h>
47
48 #include <drv/timer.h>
49 #include <cpu/irq.h>
50
51 #include "hw/hw_i2c_bitbang.h"
52
53 INLINE bool i2c_bitbang_start(void)
54 {
55         SDA_HI;
56         SCL_HI;
57         I2C_HALFBIT_DELAY();
58         SDA_LO;
59         I2C_HALFBIT_DELAY();
60
61         return !SDA_IN;
62 }
63
64 void i2c_bitbang_stop(void)
65 {
66         SDA_LO;
67         SCL_HI;
68         I2C_HALFBIT_DELAY();
69         SDA_HI;
70 }
71
72 bool i2c_bitbang_put(uint8_t _data)
73 {
74         /* Add ACK bit */
75         uint16_t data = (_data << 1) | 1;
76
77         for (uint16_t i = 0x100; i != 0; i >>= 1)
78         {
79                 SCL_LO;
80                 if (data & i)
81                         SDA_HI;
82                 else
83                         SDA_LO;
84                 I2C_HALFBIT_DELAY();
85
86                 SCL_HI;
87                 I2C_HALFBIT_DELAY();
88         }
89
90         bool ack = !SDA_IN;
91         SCL_LO;
92         I2C_HALFBIT_DELAY();
93         return ack;
94 }
95
96 bool i2c_bitbang_start_w(uint8_t id)
97 {
98         id &= ~I2C_READBIT;
99         /*
100          * Loop on the select write sequence: when the device is busy
101          * writing previously sent data it will reply to the SLA_W
102          * control byte with a NACK.  In this case, we must
103          * keep trying until the deveice responds with an ACK.
104          */
105         ticks_t start = timer_clock();
106         while (i2c_bitbang_start())
107         {
108                 if (i2c_bitbang_put(id))
109                         return true;
110                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
111                 {
112                         LOG_ERR("Timeout on I2C start write\n");
113                         break;
114                 }
115                 //LOG_INFO("Rep start\n");
116         }
117
118         return false;
119 }
120
121 bool i2c_bitbang_start_r(uint8_t id)
122 {
123         id |= I2C_READBIT;
124         if (i2c_bitbang_start())
125         {
126                 if (i2c_bitbang_put(id))
127                         return true;
128
129                 LOG_ERR("NACK on I2c start read\n");
130         }
131
132         return false;
133 }
134
135 int i2c_bitbang_get(bool ack)
136 {
137         uint8_t data = 0;
138         for (uint8_t i = 0x80; i != 0; i >>= 1)
139         {
140                 SCL_LO;
141                 I2C_HALFBIT_DELAY();
142                 SCL_HI;
143                 if (SDA_IN)
144                         data |= i;
145                 else
146                         data &= ~i;
147
148                 I2C_HALFBIT_DELAY();
149         }
150         SCL_LO;
151
152         if (ack)
153                 SDA_LO;
154         else
155                 SDA_HI;
156
157         I2C_HALFBIT_DELAY();
158         SCL_HI;
159         I2C_HALFBIT_DELAY();
160         SCL_LO;
161         SDA_HI;
162         /* avoid sign extension */
163         return (int)(uint8_t)data;
164 }
165
166 MOD_DEFINE(i2c);
167
168 /**
169  * Initialize i2c module.
170  */
171 void i2c_bitbang_init(void)
172 {
173         MOD_CHECK(timer);
174         I2C_BITBANG_HW_INIT;
175         SDA_HI;
176         SCL_HI;
177         MOD_INIT(i2c);
178 }
179
180
181 /*
182  * New I2C API
183  */
184
185 static void i2c_bitbang_stop(struct I2c *i2c)
186 {
187         SDA_LO;
188         SCL_HI;
189         I2C_HALFBIT_DELAY();
190         SDA_HI;
191 }
192
193
194 void i2c_bitbang_start(struct I2c *i2c, uint16_t slave_addr)
195 {
196         if (i2c->flags & I2C_START_R)
197                 slave_addr |= I2C_READBIT;
198         else
199                 slave_addr &= ~I2C_READBIT;
200
201         /*
202          * Loop on the select write sequence: when the device is busy
203          * writing previously sent data it will reply to the SLA_W
204          * control byte with a NACK.  In this case, we must
205          * keep trying until the deveice responds with an ACK.
206          */
207         ticks_t start = timer_clock();
208         while (i2c_bitbang_start())
209         {
210                 if (i2c_bitbang_put(slave_addr))
211                         return;
212                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
213                 {
214                         LOG_ERR("Timeout on I2C start\n");
215                         i2c->errors |= I2C_START_TIMEOUT;
216                         i2c_bitbang_stop(i2c);
217                         return;
218                 }
219         }
220
221         LOG_ERR("START arbitration lost\n");
222         i2c->errors |= I2C_ARB_LOST;
223         i2c_bitbang_stop(i2c);
224         return;
225 }
226
227 uint8_t i2c_bitbang_get(struct I2c *i2c)
228 {
229         uint8_t data = 0;
230         for (uint8_t i = 0x80; i != 0; i >>= 1)
231         {
232                 SCL_LO;
233                 I2C_HALFBIT_DELAY();
234                 SCL_HI;
235                 if (SDA_IN)
236                         data |= i;
237                 else
238                         data &= ~i;
239
240                 I2C_HALFBIT_DELAY();
241         }
242         SCL_LO;
243
244         /* Generate ACK/NACK */
245         if (i2c->xfer_size > 1)
246                 SDA_LO;
247         else
248                 SDA_HI;
249
250         I2C_HALFBIT_DELAY();
251         SCL_HI;
252         I2C_HALFBIT_DELAY();
253         SCL_LO;
254         SDA_HI;
255
256         /* Generate stop condition (if requested) */
257         if ((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP))
258                 i2c_bitbang_stop(i2c);
259
260         return data;
261 }
262
263 void i2c_bitbang_put(struct I2c *i2c, uint8_t _data)
264 {
265         /* Add ACK bit */
266         uint16_t data = (_data << 1) | 1;
267
268         for (uint16_t i = 0x100; i != 0; i >>= 1)
269         {
270                 SCL_LO;
271                 if (data & i)
272                         SDA_HI;
273                 else
274                         SDA_LO;
275                 I2C_HALFBIT_DELAY();
276
277                 SCL_HI;
278                 I2C_HALFBIT_DELAY();
279         }
280
281         bool ack = !SDA_IN;
282         SCL_LO;
283         I2C_HALFBIT_DELAY();
284
285         if (!ack)
286         {
287                 LOG_ERR("NO ACK received\n");
288                 i2c->errors |= I2C_NO_ACK;
289         }
290
291         /* Generate stop condition (if requested) */
292         if (((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP)) || i2c->errors)
293                 i2c_bitbang_stop(i2c);
294 }