Remove svn tag.
[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         ASSERT(!SDA_IN);
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