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 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
33 * \brief Driver for the AVR ATMega TWI (implementation)
37 * \author Stefano Fedrigo <aleph@develer.com>
38 * \author Bernie Innocenti <bernie@codewiz.org>
41 #include <hw/hw_cpufreq.h> /* CPU_FREQ */
43 #include "cfg/cfg_i2c.h"
45 #define LOG_LEVEL I2C_LOG_LEVEL
46 #define LOG_FORMAT I2C_LOG_FORMAT
50 #include <cfg/debug.h>
51 #include <cfg/macros.h> // BV()
52 #include <cfg/module.h>
54 #include <cpu/detect.h>
56 #include <drv/timer.h>
59 #include <compat/twi.h>
62 /* Wait for TWINT flag set: bus is ready */
63 #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
66 * Send START condition on the bus.
68 * \return true on success, false otherwise.
70 static bool i2c_builtin_start(void)
72 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
75 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
78 LOG_ERR("!TW_(REP)START: %x\n", TWSR);
84 * Send START condition and select slave for write.
85 * \c id is the device id comprehensive of address left shifted by 1.
86 * The LSB of \c id is ignored and reset to 0 for write operation.
88 * \return true on success, false otherwise.
90 bool i2c_builtin_start_w(uint8_t id)
93 * Loop on the select write sequence: when the eeprom is busy
94 * writing previously sent data it will reply to the SLA_W
95 * control byte with a NACK. In this case, we must
96 * keep trying until the eeprom responds with an ACK.
98 ticks_t start = timer_clock();
99 while (i2c_builtin_start())
101 TWDR = id & ~I2C_READBIT;
102 TWCR = BV(TWINT) | BV(TWEN);
105 if (TW_STATUS == TW_MT_SLA_ACK)
107 else if (TW_STATUS != TW_MT_SLA_NACK)
109 LOG_ERR("!TW_MT_SLA_(N)ACK: %x\n", TWSR);
112 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
114 LOG_ERR("Timeout on TWI_MT_START\n");
124 * Send START condition and select slave for read.
125 * \c id is the device id comprehensive of address left shifted by 1.
126 * The LSB of \c id is ignored and set to 1 for read operation.
128 * \return true on success, false otherwise.
130 bool i2c_builtin_start_r(uint8_t id)
132 if (i2c_builtin_start())
134 TWDR = id | I2C_READBIT;
135 TWCR = BV(TWINT) | BV(TWEN);
138 if (TW_STATUS == TW_MR_SLA_ACK)
141 LOG_ERR("!TW_MR_SLA_ACK: %x\n", TWSR);
149 * Send STOP condition.
151 void i2c_builtin_stop(void)
153 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
158 * Put a single byte in master transmitter mode
159 * to the selected slave device through the TWI bus.
161 * \return true on success, false on error.
163 bool i2c_builtin_put(const uint8_t data)
166 TWCR = BV(TWINT) | BV(TWEN);
168 if (TW_STATUS != TW_MT_DATA_ACK)
170 LOG_ERR("!TW_MT_DATA_ACK: %x\n", TWSR);
177 * Get 1 byte from slave in master transmitter mode
178 * to the selected slave device through the TWI bus.
179 * If \a ack is true issue a ACK after getting the byte,
180 * otherwise a NACK is issued.
182 * \return the byte read if ok, EOF on errors.
184 int i2c_builtin_get(bool ack)
186 TWCR = BV(TWINT) | BV(TWEN) | (ack ? BV(TWEA) : 0);
191 if (TW_STATUS != TW_MR_DATA_ACK)
193 LOG_ERR("!TW_MR_DATA_ACK: %x\n", TWSR);
199 if (TW_STATUS != TW_MR_DATA_NACK)
201 LOG_ERR("!TW_MR_DATA_NACK: %x\n", TWSR);
206 /* avoid sign extension */
207 return (int)(uint8_t)TWDR;
213 * Initialize TWI module.
215 void i2c_builtin_init(void)
219 * This is pretty useless according to AVR's datasheet,
220 * but it helps us driving the TWI data lines on boards
221 * where the bus pull-up resistors are missing. This is
222 * probably due to some unwanted interaction between the
223 * port pin and the TWI lines.
225 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281
226 PORTD |= BV(PD0) | BV(PD1);
227 DDRD |= BV(PD0) | BV(PD1);
228 #elif CPU_AVR_ATMEGA8
229 PORTC |= BV(PC4) | BV(PC5);
230 DDRC |= BV(PC4) | BV(PC5);
231 #elif CPU_AVR_ATMEGA32
232 PORTC |= BV(PC1) | BV(PC0);
233 DDRC |= BV(PC1) | BV(PC0);
235 #error Unsupported architecture
240 * F = CPU_FREQ / (16 + 2*TWBR * 4^TWPS)
242 #ifndef CONFIG_I2C_FREQ
243 #warning Using default value of 300000L for CONFIG_I2C_FREQ
244 #define CONFIG_I2C_FREQ 300000L /* ~300 kHz */
246 #define TWI_PRESC 1 /* 4 ^ TWPS */
248 TWBR = (CPU_FREQ / (2 * CONFIG_I2C_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);