4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
33 * \brief I2C bitbang driver (implementation)
35 * \author Francesco Sacchi <batt@develer.com>
39 #include "cfg/cfg_i2c.h"
41 #define LOG_LEVEL I2C_LOG_LEVEL
42 #define LOG_FORMAT I2C_LOG_FORMAT
45 #include <cfg/macros.h>
46 #include <cfg/module.h>
48 #include <drv/timer.h>
51 #include "hw/hw_i2c_bitbang.h"
53 INLINE bool i2c_bitbang_start(void)
64 void i2c_bitbang_stop(void)
72 bool i2c_bitbang_put(uint8_t _data)
75 uint16_t data = (_data << 1) | 1;
77 for (uint16_t i = 0x100; i != 0; i >>= 1)
96 bool i2c_bitbang_start_w(uint8_t id)
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.
105 ticks_t start = timer_clock();
106 while (i2c_bitbang_start())
108 if (i2c_bitbang_put(id))
110 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
112 LOG_ERR("Timeout on I2C start write\n");
115 //LOG_INFO("Rep start\n");
121 bool i2c_bitbang_start_r(uint8_t id)
124 if (i2c_bitbang_start())
126 if (i2c_bitbang_put(id))
129 LOG_ERR("NACK on I2c start read\n");
135 int i2c_bitbang_get(bool ack)
138 for (uint8_t i = 0x80; i != 0; i >>= 1)
162 /* avoid sign extension */
163 return (int)(uint8_t)data;
169 * Initialize i2c module.
171 void i2c_bitbang_init(void)
185 static void i2c_bitbang_stop(struct I2c *i2c)
194 void i2c_bitbang_start(struct I2c *i2c, uint16_t slave_addr)
196 if (i2c->flags & I2C_START_R)
197 slave_addr |= I2C_READBIT;
199 slave_addr &= ~I2C_READBIT;
202 * Loop on the select write sequence: when the device is busy
203 * writing previously sent data it will reply to the SLA_W
204 * control byte with a NACK. In this case, we must
205 * keep trying until the deveice responds with an ACK.
207 ticks_t start = timer_clock();
208 while (i2c_bitbang_start())
210 if (i2c_bitbang_put(slave_addr))
212 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
214 LOG_ERR("Timeout on I2C start\n");
215 i2c->errors |= I2C_START_TIMEOUT;
216 i2c_bitbang_stop(i2c);
221 LOG_ERR("START arbitration lost\n");
222 i2c->errors |= I2C_ARB_LOST;
223 i2c_bitbang_stop(i2c);
227 uint8_t i2c_bitbang_get(struct I2c *i2c)
230 for (uint8_t i = 0x80; i != 0; i >>= 1)
244 /* Generate ACK/NACK */
245 if (i2c->xfer_size > 1)
256 /* Generate stop condition (if requested) */
257 if ((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP))
258 i2c_bitbang_stop(i2c);
263 void i2c_bitbang_put(struct I2c *i2c, uint8_t _data)
266 uint16_t data = (_data << 1) | 1;
268 for (uint16_t i = 0x100; i != 0; i >>= 1)
287 LOG_ERR("NO ACK received\n");
288 i2c->errors |= I2C_NO_ACK;
291 /* Generate stop condition (if requested) */
292 if (((i2c->xfer_size == 1) && (i2c->flags & I2C_STOP)) || i2c->errors)
293 i2c_bitbang_stop(i2c);