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)
35 * \author Stefano Fedrigo <aleph@develer.com>
36 * \author Bernie Innocenti <bernie@codewiz.org>
37 * \author Daniele Basile <asterix@develer.com>
41 #include "cfg/cfg_i2c.h"
43 #include <hw/hw_cpufreq.h> /* CPU_FREQ */
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 <cpu/power.h>
61 #include <compat/twi.h>
63 #if !CONFIG_I2C_DISABLE_OLD_API
65 /* Wait for TWINT flag set: bus is ready */
66 #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
69 * Send START condition on the bus.
71 * \return true on success, false otherwise.
73 static bool i2c_builtin_start(void)
75 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
78 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
81 LOG_ERR("!TW_(REP)START: %x\n", TWSR);
87 * Send START condition and select slave for write.
88 * \c id is the device id comprehensive of address left shifted by 1.
89 * The LSB of \c id is ignored and reset to 0 for write operation.
91 * \return true on success, false otherwise.
93 bool i2c_builtin_start_w(uint8_t id)
96 * Loop on the select write sequence: when the eeprom is busy
97 * writing previously sent data it will reply to the SLA_W
98 * control byte with a NACK. In this case, we must
99 * keep trying until the eeprom responds with an ACK.
101 ticks_t start = timer_clock();
102 while (i2c_builtin_start())
104 TWDR = id & ~I2C_READBIT;
105 TWCR = BV(TWINT) | BV(TWEN);
108 if (TW_STATUS == TW_MT_SLA_ACK)
110 else if (TW_STATUS != TW_MT_SLA_NACK)
112 LOG_ERR("!TW_MT_SLA_(N)ACK: %x\n", TWSR);
115 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
117 LOG_ERR("Timeout on TWI_MT_START\n");
127 * Send START condition and select slave for read.
128 * \c id is the device id comprehensive of address left shifted by 1.
129 * The LSB of \c id is ignored and set to 1 for read operation.
131 * \return true on success, false otherwise.
133 bool i2c_builtin_start_r(uint8_t id)
135 if (i2c_builtin_start())
137 TWDR = id | I2C_READBIT;
138 TWCR = BV(TWINT) | BV(TWEN);
141 if (TW_STATUS == TW_MR_SLA_ACK)
144 LOG_ERR("!TW_MR_SLA_ACK: %x\n", TWSR);
152 * Send STOP condition.
154 void i2c_builtin_stop(void)
156 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
161 * Put a single byte in master transmitter mode
162 * to the selected slave device through the TWI bus.
164 * \return true on success, false on error.
166 bool i2c_builtin_put(const uint8_t data)
169 TWCR = BV(TWINT) | BV(TWEN);
171 if (TW_STATUS != TW_MT_DATA_ACK)
173 LOG_ERR("!TW_MT_DATA_ACK: %x\n", TWSR);
180 * Get 1 byte from slave in master transmitter mode
181 * to the selected slave device through the TWI bus.
182 * If \a ack is true issue a ACK after getting the byte,
183 * otherwise a NACK is issued.
185 * \return the byte read if ok, EOF on errors.
187 int i2c_builtin_get(bool ack)
189 TWCR = BV(TWINT) | BV(TWEN) | (ack ? BV(TWEA) : 0);
194 if (TW_STATUS != TW_MR_DATA_ACK)
196 LOG_ERR("!TW_MR_DATA_ACK: %x\n", TWSR);
202 if (TW_STATUS != TW_MR_DATA_NACK)
204 LOG_ERR("!TW_MR_DATA_NACK: %x\n", TWSR);
209 /* avoid sign extension */
210 return (int)(uint8_t)TWDR;
213 #endif /* !CONFIG_I2C_DISABLE_OLD_API */
223 /* Wait for TWINT flag set: bus is ready */
224 #define WAIT_READY() \
226 while (!(TWCR & BV(TWINT))) \
231 * Send START condition on the bus.
233 INLINE bool i2c_hw_start(void)
235 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
238 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
245 * Send STOP condition.
247 INLINE void i2c_hw_stop(void)
249 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
252 static void i2c_avr_start(I2c *i2c, uint16_t slave_addr)
255 * Loop on the select write sequence: when the eeprom is busy
256 * writing previously sent data it will reply to the SLA_W
257 * control byte with a NACK. In this case, we must
258 * keep trying until the slave responds with an ACK.
260 ticks_t start = timer_clock();
261 while (i2c_hw_start())
264 uint8_t sla_nack = 0;
265 if (I2C_TEST_START(i2c->flags) == I2C_START_W)
267 TWDR = slave_addr & ~I2C_READBIT;
268 sla_ack = TW_MT_SLA_ACK;
269 sla_nack = TW_MT_SLA_NACK;
273 TWDR = slave_addr | I2C_READBIT;
274 sla_ack = TW_MR_SLA_ACK;
275 sla_nack = TW_MR_SLA_NACK;
278 TWCR = BV(TWINT) | BV(TWEN);
281 if (TW_STATUS == sla_ack)
283 else if (TW_STATUS != sla_nack)
285 LOG_ERR("Start addr NACK[%x]\n", TWSR);
286 i2c->errors |= I2C_NO_ACK;
290 else if (timer_clock() - start > ms_to_ticks(CONFIG_I2C_START_TIMEOUT))
292 LOG_ERR("Start timeout\n");
293 i2c->errors |= I2C_START_TIMEOUT;
299 LOG_ERR("I2c error\n");
300 i2c->errors |= I2C_ERR;
304 static void i2c_avr_putc(I2c *i2c, const uint8_t data)
308 TWCR = BV(TWINT) | BV(TWEN);
311 if (TW_STATUS != TW_MT_DATA_ACK)
313 LOG_ERR("Data nack[%x]\n", TWSR);
314 i2c->errors |= I2C_DATA_NACK;
318 if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
322 static uint8_t i2c_avr_getc(I2c *i2c)
324 uint8_t data_flag = 0;
325 if (i2c->xfer_size == 1)
327 TWCR = BV(TWINT) | BV(TWEN);
328 data_flag = TW_MR_DATA_NACK;
332 TWCR = BV(TWINT) | BV(TWEN) | BV(TWEA);
333 data_flag = TW_MR_DATA_ACK;
338 if (TW_STATUS != data_flag)
340 LOG_ERR("Data nack[%x]\n", TWSR);
341 i2c->errors |= I2C_DATA_NACK;
349 if ((i2c->xfer_size == 1) && (I2C_TEST_STOP(i2c->flags) == I2C_STOP))
356 static const I2cVT i2c_avr_vt =
358 .start = i2c_avr_start,
359 .getc = i2c_avr_getc,
360 .putc = i2c_avr_putc,
361 .write = i2c_genericWrite,
362 .read = i2c_genericRead,
365 struct I2cHardware i2c_avr_hw[] =
372 * Initialize I2C module.
374 void i2c_hw_init(I2c *i2c, int dev, uint32_t clock)
376 i2c->hw = &i2c_avr_hw[dev];
377 i2c->vt = &i2c_avr_vt;
381 * This is pretty useless according to AVR's datasheet,
382 * but it helps us driving the TWI data lines on boards
383 * where the bus pull-up resistors are missing. This is
384 * probably due to some unwanted interaction between the
385 * port pin and the TWI lines.
387 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA1280
388 PORTD |= BV(PD0) | BV(PD1);
389 DDRD |= BV(PD0) | BV(PD1);
390 #elif CPU_AVR_ATMEGA8
391 PORTC |= BV(PC4) | BV(PC5);
392 DDRC |= BV(PC4) | BV(PC5);
393 #elif CPU_AVR_ATMEGA32
394 PORTC |= BV(PC1) | BV(PC0);
395 DDRC |= BV(PC1) | BV(PC0);
397 #error Unsupported architecture
402 * F = CPU_FREQ / (16 + 2*TWBR * 4^TWPS)
405 #define TWI_PRESC 1 /* 4 ^ TWPS */
407 TWBR = (CPU_FREQ / (2 * clock * TWI_PRESC)) - (8 / TWI_PRESC);