Remove old api that using i2c. Add interface to allow the compatibily with old 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  * \author Daniele Basile <asterix@develer.com>
37  */
38
39 #include "hw/hw_i2c_bitbang.h"
40
41 #include "cfg/cfg_i2c.h"
42
43 #define LOG_LEVEL  I2C_LOG_LEVEL
44 #define LOG_FORMAT I2C_LOG_FORMAT
45
46 #include <cfg/log.h>
47 #include <cfg/macros.h>
48 #include <cfg/module.h>
49
50 #include <drv/timer.h>
51 #include <drv/i2c.h>
52
53 #include <cpu/irq.h>
54
55 #include <cpu/attr.h>
56
57 #if !CONFIG_I2C_DISABLE_OLD_API
58
59 INLINE bool i2c_bitbang_start(void)
60 {
61         SDA_HI;
62         SCL_HI;
63         I2C_HALFBIT_DELAY();
64         SDA_LO;
65         I2C_HALFBIT_DELAY();
66
67         return !SDA_IN;
68 }
69
70 void i2c_bitbang_stop(void)
71 {
72         SDA_LO;
73         SCL_HI;
74         I2C_HALFBIT_DELAY();
75         SDA_HI;
76 }
77
78 bool i2c_bitbang_put(uint8_t _data)
79 {
80         /* Add ACK bit */
81         uint16_t data = (_data << 1) | 1;
82
83         for (uint16_t i = 0x100; i != 0; i >>= 1)
84         {
85                 SCL_LO;
86                 if (data & i)
87                         SDA_HI;
88                 else
89                         SDA_LO;
90                 I2C_HALFBIT_DELAY();
91
92                 SCL_HI;
93                 I2C_HALFBIT_DELAY();
94         }
95
96         bool ack = !SDA_IN;
97         SCL_LO;
98         I2C_HALFBIT_DELAY();
99         return ack;
100 }
101
102 bool i2c_bitbang_start_w(uint8_t id)
103 {
104         id &= ~I2C_READBIT;
105         /*
106          * Loop on the select write sequence: when the device is busy
107          * writing previously sent data it will reply to the SLA_W
108          * control byte with a NACK.  In this case, we must
109          * keep trying until the deveice responds with an ACK.
110          */
111         ticks_t start = timer_clock();
112         while (i2c_bitbang_start())
113         {
114                 if (i2c_bitbang_put(id))
115                         return true;
116                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
117                 {
118                         LOG_ERR("Timeout on I2C start write\n");
119                         break;
120                 }
121                 //LOG_INFO("Rep start\n");
122         }
123
124         return false;
125 }
126
127 bool i2c_bitbang_start_r(uint8_t id)
128 {
129         id |= I2C_READBIT;
130         if (i2c_bitbang_start())
131         {
132                 if (i2c_bitbang_put(id))
133                         return true;
134
135                 LOG_ERR("NACK on I2c start read\n");
136         }
137
138         return false;
139 }
140
141 int i2c_bitbang_get(bool ack)
142 {
143         uint8_t data = 0;
144         for (uint8_t i = 0x80; i != 0; i >>= 1)
145         {
146                 SCL_LO;
147                 I2C_HALFBIT_DELAY();
148                 SCL_HI;
149                 if (SDA_IN)
150                         data |= i;
151                 else
152                         data &= ~i;
153
154                 I2C_HALFBIT_DELAY();
155         }
156         SCL_LO;
157
158         if (ack)
159                 SDA_LO;
160         else
161                 SDA_HI;
162
163         I2C_HALFBIT_DELAY();
164         SCL_HI;
165         I2C_HALFBIT_DELAY();
166         SCL_LO;
167         SDA_HI;
168         /* avoid sign extension */
169         return (int)(uint8_t)data;
170 }
171
172 #endif /* !CONFIG_I2C_DISABLE_OLD_API */
173
174 /*
175  * New I2C API
176  */
177 static bool old_api = false;
178 #define I2C_DEV(i2c)            ((int)((i2c)->hw))
179
180 static void i2c_bitbang_stop_1(struct I2c *i2c)
181 {
182         if (old_api)
183         {
184                 SDA_LO;
185                 SCL_HI;
186                 I2C_HALFBIT_DELAY();
187                 SDA_HI;
188         }
189         else
190         {
191                 i2c_sdaLo(I2C_DEV(i2c));
192                 i2c_sclHi(I2C_DEV(i2c));
193                 i2c_halfbitDelay(I2C_DEV(i2c));
194                 i2c_sdaHi(I2C_DEV(i2c));
195         }
196 }
197
198 INLINE bool i2c_bitbang_start_1(struct I2c *i2c)
199 {
200         bool ret;
201         if (old_api)
202         {
203                 SDA_HI;
204                 SCL_HI;
205                 I2C_HALFBIT_DELAY();
206                 SDA_LO;
207                 I2C_HALFBIT_DELAY();
208
209                 ret = !SDA_IN;
210         }
211         else
212         {
213                 i2c_sdaHi(I2C_DEV(i2c));
214                 i2c_sclHi(I2C_DEV(i2c));
215                 i2c_halfbitDelay(I2C_DEV(i2c));
216                 i2c_sdaLo(I2C_DEV(i2c));
217                 i2c_halfbitDelay(I2C_DEV(i2c));
218                 ret = !i2c_sdaIn(I2C_DEV(i2c));
219         }
220
221         return ret;
222 }
223
224
225 static uint8_t i2c_bitbang_getc(struct I2c *i2c)
226 {
227         uint8_t data = 0;
228         if (old_api)
229         {
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         else
257         {
258                 for (uint8_t i = 0x80; i != 0; i >>= 1)
259                 {
260                         i2c_sclLo(I2C_DEV(i2c));
261                         i2c_halfbitDelay(I2C_DEV(i2c));
262                         i2c_sclHi(I2C_DEV(i2c));
263                         if (i2c_sdaIn(I2C_DEV(i2c)))
264                                 data |= i;
265                         else
266                                 data &= ~i;
267
268                         i2c_halfbitDelay(I2C_DEV(i2c));
269                 }
270                 i2c_sclLo(I2C_DEV(i2c));
271
272                 /* Generate ACK/NACK */
273                 if (i2c->xfer_size > 1)
274                         i2c_sdaLo(I2C_DEV(i2c));
275                 else
276                         i2c_sdaHi(I2C_DEV(i2c));
277
278                 i2c_halfbitDelay(I2C_DEV(i2c));
279                 i2c_sclHi(I2C_DEV(i2c));
280                 i2c_halfbitDelay(I2C_DEV(i2c));
281                 i2c_sclLo(I2C_DEV(i2c));
282                 i2c_sdaHi(I2C_DEV(i2c));
283         }
284
285         /* Generate stop condition (if requested) */
286         if ((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP))
287                 i2c_bitbang_stop_1(i2c);
288
289         return data;
290 }
291
292 static void i2c_bitbang_putc(struct I2c *i2c, uint8_t _data)
293 {
294         /* Add ACK bit */
295         uint16_t data = (_data << 1) | 1;
296         bool ack;
297
298         if (old_api)
299         {
300                 for (uint16_t i = 0x100; i != 0; i >>= 1)
301                 {
302                         SCL_LO;
303                         if (data & i)
304                                 SDA_HI;
305                         else
306                                 SDA_LO;
307                         I2C_HALFBIT_DELAY();
308
309                         SCL_HI;
310                         I2C_HALFBIT_DELAY();
311                 }
312
313                 ack = !SDA_IN;
314                 SCL_LO;
315                 I2C_HALFBIT_DELAY();
316         }
317         else
318         {
319                 for (uint16_t i = 0x100; i != 0; i >>= 1)
320                 {
321                         i2c_sclLo(I2C_DEV(i2c));
322                         if (data & i)
323                                 i2c_sdaHi(I2C_DEV(i2c));
324                         else
325                                 i2c_sdaLo(I2C_DEV(i2c));
326                         i2c_halfbitDelay(I2C_DEV(i2c));
327
328                         i2c_sclHi(I2C_DEV(i2c));
329                         i2c_halfbitDelay(I2C_DEV(i2c));
330                 }
331
332                 ack = !i2c_sdaIn(I2C_DEV(i2c));
333                 i2c_sclLo(I2C_DEV(i2c));
334                 i2c_halfbitDelay(I2C_DEV(i2c));
335         }
336
337         if (!ack)
338         {
339                 LOG_ERR("NO ACK received\n");
340                 i2c->errors |= I2C_NO_ACK;
341         }
342
343         /* Generate stop condition (if requested) */
344         if (((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP)) || i2c->errors)
345                 i2c_bitbang_stop_1(i2c);
346 }
347
348
349 static void i2c_bitbang_start_2(struct I2c *i2c, uint16_t slave_addr)
350 {
351         if (i2c->flags & I2C_START_R)
352                 slave_addr |= I2C_READBIT;
353         else
354                 slave_addr &= ~I2C_READBIT;
355
356         /*
357          * Loop on the select write sequence: when the device is busy
358          * writing previously sent data it will reply to the SLA_W
359          * control byte with a NACK.  In this case, we must
360          * keep trying until the deveice responds with an ACK.
361          */
362         ticks_t start = timer_clock();
363         while (i2c_bitbang_start_1(i2c))
364         {
365                 i2c_bitbang_putc(i2c, slave_addr);
366
367                 if (!(i2c->errors & I2C_NO_ACK))
368                         return;
369                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
370                 {
371                         LOG_ERR("Timeout on I2C start\n");
372                         i2c->errors |= I2C_START_TIMEOUT;
373                         i2c_bitbang_stop_1(i2c);
374                         return;
375                 }
376         }
377
378         LOG_ERR("START arbitration lost\n");
379         i2c->errors |= I2C_ARB_LOST;
380         i2c_bitbang_stop_1(i2c);
381         return;
382 }
383
384
385 static const I2cVT i2c_bitbang_vt =
386 {
387         .start = i2c_bitbang_start_2,
388         .getc = i2c_bitbang_getc,
389         .putc = i2c_bitbang_putc,
390         .write = i2c_genericWrite,
391         .read = i2c_genericRead,
392 };
393
394
395 /**
396  * Initialize i2c module.
397  */
398 void i2c_hw_bitbangInit(I2c *i2c, int dev)
399 {
400         MOD_CHECK(timer);
401         if (dev == I2C_BITBANG_OLD)
402                 old_api = true;
403         else
404                 i2c->hw = (struct I2cHardware *)(dev - I2C_BITBANG0);
405
406         i2c->vt = &i2c_bitbang_vt;
407
408         if (old_api)
409         {
410                 I2C_BITBANG_HW_INIT;
411                 SDA_HI;
412                 SCL_HI;
413         }
414         else
415         {
416                 i2c_bitbangInit(I2C_DEV(i2c));
417                 i2c_sdaHi(I2C_DEV(i2c));
418                 i2c_sclHi(I2C_DEV(i2c));
419         }
420 }
421