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)
36 * \author Francesco Sacchi <batt@develer.com>
40 #include "cfg/cfg_i2c.h"
42 #define LOG_LEVEL I2C_LOG_LEVEL
43 #define LOG_FORMAT I2C_LOG_FORMAT
46 #include <cfg/macros.h>
47 #include <cfg/module.h>
49 #include <drv/timer.h>
52 #include "hw/hw_i2c_bitbang.h"
54 INLINE bool i2c_bitbang_start(void)
65 void i2c_bitbang_stop(void)
73 bool i2c_bitbang_put(uint8_t _data)
76 uint16_t data = (_data << 1) | 1;
78 for (uint16_t i = 0x100; i != 0; i >>= 1)
97 bool i2c_bitbang_start_w(uint8_t id)
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.
106 ticks_t start = timer_clock();
107 while (i2c_bitbang_start())
109 if (i2c_bitbang_put(id))
111 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
113 LOG_ERR("Timeout on I2C start write\n");
116 //LOG_INFO("Rep start\n");
122 bool i2c_bitbang_start_r(uint8_t id)
125 if (i2c_bitbang_start())
127 if (i2c_bitbang_put(id))
130 LOG_ERR("NACK on I2c start read\n");
136 int i2c_bitbang_get(bool ack)
139 for (uint8_t i = 0x80; i != 0; i >>= 1)
163 /* avoid sign extension */
164 return (int)(uint8_t)data;
170 * Initialize i2c module.
172 void i2c_bitbang_init(void)