Add configurable option to choose at compile time which i2c backend to use.
[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  * \version $Id: adc.c 1604 2008-08-10 17:19:51Z bernie $
36  * \author Francesco Sacchi <batt@develer.com>
37  */
38
39 #include "i2c.h"
40 #include "cfg/cfg_i2c.h"
41
42 #define LOG_LEVEL  I2C_LOG_LEVEL
43 #define LOG_FORMAT I2C_LOG_FORMAT
44
45 #include <cfg/log.h>
46 #include <cfg/macros.h>
47 #include <cfg/module.h>
48
49 #include <drv/timer.h>
50 #include <cpu/irq.h>
51
52 #include "hw/hw_i2c_bitbang.h"
53
54 INLINE bool i2c_bitbang_start(void)
55 {
56         SDA_HI;
57         SCL_HI;
58         I2C_HALFBIT_DELAY();
59         SDA_LO;
60         I2C_HALFBIT_DELAY();
61         ASSERT(!SDA_IN);
62         return !SDA_IN;
63 }
64
65 void i2c_bitbang_stop(void)
66 {
67         SDA_LO;
68         SCL_HI;
69         I2C_HALFBIT_DELAY();
70         SDA_HI;
71 }
72
73 bool i2c_bitbang_put(uint8_t _data)
74 {
75         /* Add ACK bit */
76         uint16_t data = (_data << 1) | 1;
77
78         for (uint16_t i = 0x100; i != 0; i >>= 1)
79         {
80                 SCL_LO;
81                 if (data & i)
82                         SDA_HI;
83                 else
84                         SDA_LO;
85                 I2C_HALFBIT_DELAY();
86
87                 SCL_HI;
88                 I2C_HALFBIT_DELAY();
89         }
90
91         bool ack = !SDA_IN;
92         SCL_LO;
93         I2C_HALFBIT_DELAY();
94         return ack;
95 }
96
97 bool i2c_bitbang_start_w(uint8_t id)
98 {
99         id &= ~I2C_READBIT;
100         /*
101          * Loop on the select write sequence: when the device is busy
102          * writing previously sent data it will reply to the SLA_W
103          * control byte with a NACK.  In this case, we must
104          * keep trying until the deveice responds with an ACK.
105          */
106         ticks_t start = timer_clock();
107         while (i2c_bitbang_start())
108         {
109                 if (i2c_bitbang_put(id))
110                         return true;
111                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
112                 {
113                         LOG_ERR("Timeout on I2C start write\n");
114                         break;
115                 }
116                 //LOG_INFO("Rep start\n");
117         }
118
119         return false;
120 }
121
122 bool i2c_bitbang_start_r(uint8_t id)
123 {
124         id |= I2C_READBIT;
125         if (i2c_bitbang_start())
126         {
127                 if (i2c_bitbang_put(id))
128                         return true;
129
130                 LOG_ERR("NACK on I2c start read\n");
131         }
132
133         return false;
134 }
135
136 int i2c_bitbang_get(bool ack)
137 {
138         uint8_t data = 0;
139         for (uint8_t i = 0x80; i != 0; i >>= 1)
140         {
141                 SCL_LO;
142                 I2C_HALFBIT_DELAY();
143                 SCL_HI;
144                 if (SDA_IN)
145                         data |= i;
146                 else
147                         data &= ~i;
148
149                 I2C_HALFBIT_DELAY();
150         }
151         SCL_LO;
152
153         if (ack)
154                 SDA_LO;
155         else
156                 SDA_HI;
157
158         I2C_HALFBIT_DELAY();
159         SCL_HI;
160         I2C_HALFBIT_DELAY();
161         SCL_LO;
162         SDA_HI;
163         /* avoid sign extension */
164         return (int)(uint8_t)data;
165 }
166
167 MOD_DEFINE(i2c);
168
169 /**
170  * Initialize i2c module.
171  */
172 void i2c_bitbang_init(void)
173 {
174         MOD_CHECK(timer);
175         I2C_BITBANG_HW_INIT;
176         SDA_HI;
177         SCL_HI;
178         MOD_INIT(i2c);
179 }
180