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)