Add support for i2c bitbang.
[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
58 INLINE bool i2c_bitbang_start(void)
59 {
60         SDA_HI;
61         SCL_HI;
62         I2C_HALFBIT_DELAY();
63         SDA_LO;
64         I2C_HALFBIT_DELAY();
65
66         return !SDA_IN;
67 }
68
69 void i2c_bitbang_stop(void)
70 {
71         SDA_LO;
72         SCL_HI;
73         I2C_HALFBIT_DELAY();
74         SDA_HI;
75 }
76
77 bool i2c_bitbang_put(uint8_t _data)
78 {
79         /* Add ACK bit */
80         uint16_t data = (_data << 1) | 1;
81
82         for (uint16_t i = 0x100; i != 0; i >>= 1)
83         {
84                 SCL_LO;
85                 if (data & i)
86                         SDA_HI;
87                 else
88                         SDA_LO;
89                 I2C_HALFBIT_DELAY();
90
91                 SCL_HI;
92                 I2C_HALFBIT_DELAY();
93         }
94
95         bool ack = !SDA_IN;
96         SCL_LO;
97         I2C_HALFBIT_DELAY();
98         return ack;
99 }
100
101 bool i2c_bitbang_start_w(uint8_t id)
102 {
103         id &= ~I2C_READBIT;
104         /*
105          * Loop on the select write sequence: when the device is busy
106          * writing previously sent data it will reply to the SLA_W
107          * control byte with a NACK.  In this case, we must
108          * keep trying until the deveice responds with an ACK.
109          */
110         ticks_t start = timer_clock();
111         while (i2c_bitbang_start())
112         {
113                 if (i2c_bitbang_put(id))
114                         return true;
115                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
116                 {
117                         LOG_ERR("Timeout on I2C start write\n");
118                         break;
119                 }
120                 //LOG_INFO("Rep start\n");
121         }
122
123         return false;
124 }
125
126 bool i2c_bitbang_start_r(uint8_t id)
127 {
128         id |= I2C_READBIT;
129         if (i2c_bitbang_start())
130         {
131                 if (i2c_bitbang_put(id))
132                         return true;
133
134                 LOG_ERR("NACK on I2c start read\n");
135         }
136
137         return false;
138 }
139
140 int i2c_bitbang_get(bool ack)
141 {
142         uint8_t data = 0;
143         for (uint8_t i = 0x80; i != 0; i >>= 1)
144         {
145                 SCL_LO;
146                 I2C_HALFBIT_DELAY();
147                 SCL_HI;
148                 if (SDA_IN)
149                         data |= i;
150                 else
151                         data &= ~i;
152
153                 I2C_HALFBIT_DELAY();
154         }
155         SCL_LO;
156
157         if (ack)
158                 SDA_LO;
159         else
160                 SDA_HI;
161
162         I2C_HALFBIT_DELAY();
163         SCL_HI;
164         I2C_HALFBIT_DELAY();
165         SCL_LO;
166         SDA_HI;
167         /* avoid sign extension */
168         return (int)(uint8_t)data;
169 }
170
171 MOD_DEFINE(i2c);
172
173 /**
174  * Initialize i2c module.
175  */
176 void i2c_bitbang_init(void)
177 {
178         MOD_CHECK(timer);
179         I2C_BITBANG_HW_INIT;
180         SDA_HI;
181         SCL_HI;
182         MOD_INIT(i2c);
183 }
184
185
186 /*
187  * New I2C API
188  */
189 #define I2C_DEV(i2c)            ((int)((i2c)->hw))
190
191 static void i2c_bitbang_stop_1(struct I2c *i2c)
192 {
193         i2c_hw_sdaLo(I2C_DEV(i2c));
194         i2c_hw_sclHi(I2C_DEV(i2c));
195         i2c_hw_halfbitDelay(I2C_DEV(i2c));
196         i2c_hw_sdaHi(I2C_DEV(i2c));
197 }
198
199 INLINE bool i2c_bitbang_start_1(struct I2c *i2c)
200 {
201         i2c_hw_sdaHi(I2C_DEV(i2c));
202         i2c_hw_sclHi(I2C_DEV(i2c));
203         i2c_hw_halfbitDelay(I2C_DEV(i2c));
204         i2c_hw_sdaLo(I2C_DEV(i2c));
205         i2c_hw_halfbitDelay(I2C_DEV(i2c));
206
207         return !i2c_hw_sdaIn(I2C_DEV(i2c));
208 }
209
210
211 static uint8_t i2c_bitbang_getc(struct I2c *i2c)
212 {
213         uint8_t data = 0;
214         for (uint8_t i = 0x80; i != 0; i >>= 1)
215         {
216                 i2c_hw_sclLo(I2C_DEV(i2c));
217                 i2c_hw_halfbitDelay(I2C_DEV(i2c));
218                 i2c_hw_sclHi(I2C_DEV(i2c));
219                 if (i2c_hw_sdaIn(I2C_DEV(i2c)))
220                         data |= i;
221                 else
222                         data &= ~i;
223
224                 i2c_hw_halfbitDelay(I2C_DEV(i2c));
225         }
226         i2c_hw_sclLo(I2C_DEV(i2c));
227
228         /* Generate ACK/NACK */
229         if (i2c->xfer_size > 1)
230                 i2c_hw_sdaLo(I2C_DEV(i2c));
231         else
232                 i2c_hw_sdaHi(I2C_DEV(i2c));
233
234         i2c_hw_halfbitDelay(I2C_DEV(i2c));
235         i2c_hw_sclHi(I2C_DEV(i2c));
236         i2c_hw_halfbitDelay(I2C_DEV(i2c));
237         i2c_hw_sclLo(I2C_DEV(i2c));
238         i2c_hw_sdaHi(I2C_DEV(i2c));
239
240         /* Generate stop condition (if requested) */
241         if ((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP))
242                 i2c_bitbang_stop_1(i2c);
243
244         return data;
245 }
246
247 static void i2c_bitbang_putc(struct I2c *i2c, uint8_t _data)
248 {
249         /* Add ACK bit */
250         uint16_t data = (_data << 1) | 1;
251
252         for (uint16_t i = 0x100; i != 0; i >>= 1)
253         {
254                 i2c_hw_sclLo(I2C_DEV(i2c));
255                 if (data & i)
256                         i2c_hw_sdaHi(I2C_DEV(i2c));
257                 else
258                         i2c_hw_sdaLo(I2C_DEV(i2c));
259                 i2c_hw_halfbitDelay(I2C_DEV(i2c));
260
261                 i2c_hw_sclHi(I2C_DEV(i2c));
262                 i2c_hw_halfbitDelay(I2C_DEV(i2c));
263         }
264
265         bool ack = !i2c_hw_sdaIn(I2C_DEV(i2c));
266         i2c_hw_sclLo(I2C_DEV(i2c));
267         i2c_hw_halfbitDelay(I2C_DEV(i2c));
268
269         if (!ack)
270         {
271                 LOG_ERR("NO ACK received\n");
272                 i2c->errors |= I2C_NO_ACK;
273         }
274
275         /* Generate stop condition (if requested) */
276         if (((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP)) || i2c->errors)
277                 i2c_bitbang_stop_1(i2c);
278 }
279
280
281 static void i2c_bitbang_start_2(struct I2c *i2c, uint16_t slave_addr)
282 {
283         if (i2c->flags & I2C_START_R)
284                 slave_addr |= I2C_READBIT;
285         else
286                 slave_addr &= ~I2C_READBIT;
287
288         /*
289          * Loop on the select write sequence: when the device is busy
290          * writing previously sent data it will reply to the SLA_W
291          * control byte with a NACK.  In this case, we must
292          * keep trying until the deveice responds with an ACK.
293          */
294         ticks_t start = timer_clock();
295         while (i2c_bitbang_start_1(i2c))
296         {
297                 i2c_bitbang_putc(i2c, slave_addr);
298
299                 if (!(i2c->errors & I2C_NO_ACK))
300                         return;
301                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
302                 {
303                         LOG_ERR("Timeout on I2C start\n");
304                         i2c->errors |= I2C_START_TIMEOUT;
305                         i2c_bitbang_stop_1(i2c);
306                         return;
307                 }
308         }
309
310         LOG_ERR("START arbitration lost\n");
311         i2c->errors |= I2C_ARB_LOST;
312         i2c_bitbang_stop_1(i2c);
313         return;
314 }
315
316
317 static const I2cVT i2c_bitbang_vt =
318 {
319         .start = i2c_bitbang_start_2,
320         .getc = i2c_bitbang_getc,
321         .putc = i2c_bitbang_putc,
322         .write = i2c_genericWrite,
323         .read = i2c_genericRead,
324 };
325
326
327 /**
328  * Initialize i2c module.
329  */
330 void i2c_hw_bitbangInit(I2c *i2c, int dev)
331 {
332         MOD_CHECK(timer);
333         i2c->hw = (struct I2cHardware *)(dev - I2C_BITBANG0);
334         i2c->vt = &i2c_bitbang_vt;
335
336         i2c_hw_bitbang_init(I2C_DEV(i2c));
337         i2c_hw_sdaHi(I2C_DEV(i2c));
338         i2c_hw_sclHi(I2C_DEV(i2c));
339 }
340