Merged from external project:
[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/module.h>
47
48 #include "hw/hw_i2c_bitbang.h"
49
50 INLINE bool i2c_start(void)
51 {
52         SDA_HI;
53         SCL_HI;
54         I2C_HALFBIT_DELAY();
55         SDA_LO;
56         I2C_HALFBIT_DELAY();
57         return !SDA_IN;
58 }
59
60 void i2c_stop(void)
61 {
62         SDA_LO;
63         SCL_HI;
64         I2C_HALFBIT_DELAY();
65         SDA_HI;
66 }
67
68 bool i2c_put(uint8_t _data)
69 {
70         /* Add ACK bit */
71         uint16_t data = (_data << 1) | 1;
72
73         for (uint16_t i = 0x100; i != 0; i >>= 1)
74         {
75                 SCL_LO;
76                 if (data & i)
77                         SDA_HI;
78                 else
79                         SDA_LO;
80                 I2C_HALFBIT_DELAY();
81
82                 SCL_HI;
83                 I2C_HALFBIT_DELAY();
84         }
85
86         bool ack = !SDA_IN;
87         SCL_LO;
88         I2C_HALFBIT_DELAY();
89         return ack;
90 }
91
92 bool i2c_start_w(uint8_t id)
93 {
94         id &= ~I2C_READBIT;
95         /*
96          * Loop on the select write sequence: when the device is busy
97          * writing previously sent data it will reply to the SLA_W
98          * control byte with a NACK.  In this case, we must
99          * keep trying until the deveice responds with an ACK.
100          */
101         ticks_t start = timer_clock();
102         while (i2c_start())
103         {
104                 if (i2c_put(id))
105                         return true;
106                 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
107                 {
108                         LOG_ERR("Timeout on I2C start write\n");
109                         break;
110                 }
111                 //LOG_INFO("Rep start\n");
112         }
113
114         return false;
115 }
116
117 bool i2c_start_r(uint8_t id)
118 {
119         id |= I2C_READBIT;
120         if (i2c_start())
121         {
122                 if (i2c_put(id))
123                         return true;
124
125                 LOG_ERR("NACK on I2c start read\n");
126         }
127
128         return false;
129 }
130
131 int i2c_get(bool ack)
132 {
133         uint8_t data = 0;
134         for (uint8_t i = 0x80; i != 0; i >>= 1)
135         {
136                 SCL_LO;
137                 I2C_HALFBIT_DELAY();
138                 SCL_HI;
139                 if (SDA_IN)
140                         data |= i;
141                 else
142                         data &= ~i;
143
144                 I2C_HALFBIT_DELAY();
145         }
146         SCL_LO;
147
148         if (ack)
149                 SDA_LO;
150         else
151                 SDA_HI;
152
153         I2C_HALFBIT_DELAY();
154         SCL_HI;
155         I2C_HALFBIT_DELAY();
156         SCL_LO;
157         SDA_HI;
158         /* avoid sign extension */
159         return (int)(uint8_t)data;
160 }
161
162 MOD_DEFINE(i2c);
163
164 /**
165  * Initialize i2c module.
166  */
167 void i2c_init(void)
168 {
169         I2C_BITBANG_HW_INIT;
170         SDA_HI;
171         SCL_HI;
172         MOD_INIT(i2c);
173 }
174