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 * \version $Id: adc.c 1604 2008-08-10 17:19:51Z bernie $
36 * \author Francesco Sacchi <batt@develer.com>
41 #include "hw/hw_i2c_bitbang.h"
43 #define I2C_PERIOD DIV_ROUND(500000UL / CONFIG_I2C_FREQ)
45 INLINE bool i2c_start(void)
49 timer_udelay(I2C_PERIOD);
51 timer_udelay(I2C_PERIOD);
55 INLINE void i2c_stop(void)
58 timer_udelay(I2C_PERIOD);
62 bool i2c_put(uint8_t _data)
64 uint16_t data = (_data << 1) | 1;
65 for (uint16_t i = 0x100; i >= 0; i >>= 1)
68 timer_udelay(I2C_PERIOD);
74 timer_udelay(I2C_PERIOD);
75 ASSERT(SDA == (data & i));
80 timer_udelay(I2C_PERIOD);
84 bool i2c_start_w(uint8_t id)
88 * Loop on the select write sequence: when the device is busy
89 * writing previously sent data it will reply to the SLA_W
90 * control byte with a NACK. In this case, we must
91 * keep trying until the deveice responds with an ACK.
93 ticks_t start = timer_clock();
98 else if (timer_clock() - start > ms_to_ticks(CONFIG_TWI_START_TIMEOUT))
100 LOG_ERR("Timeout on I2C start write\n");
108 bool i2c_start_r(uint8_t id)
116 LOG_ERR("NACK on I2c start read\n");
122 int i2c_get(bool ack)
125 for (uint8_t i = 0x80; i >= 0; i >>= 1)
128 timer_udelay(I2C_PERIOD);
130 timer_udelay(I2C_PERIOD);
135 timer_udelay(I2C_PERIOD);
143 /* avoid sign extension */
144 return (int)(uint8_t)data;
148 * Send a sequence of bytes in master transmitter mode
149 * to the selected slave device through the I2C bus.
151 * \return true on success, false on error.
153 bool i2c_send(const void *_buf, size_t count)
155 const uint8_t *buf = (const uint8_t *)_buf;
159 if (!i2c_put(*buf++))
166 * Receive a sequence of one or more bytes from the
167 * selected slave device in master receive mode through
170 * Received data is placed in \c buf.
172 * \note a NACK is automatically given on the last received
175 * \return true on success, false on error
177 bool i2c_recv(void *_buf, size_t count)
179 uint8_t *buf = (uint8_t *)_buf;
184 * The last byte read does not has an ACK
185 * to stop communication.
187 int c = i2c_get(count);