X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fcpu%2Favr%2Fdrv%2Fi2c_avr.c;h=eb8b8da90d90a6c05aa01935c721014e59947e9b;hb=f1fab319eb3fe91c157b3a9564841aef62a03554;hp=739e4ab371c3205722956b154f6972a00aaa8a3f;hpb=f062d0cd413c738978ac8781da89a12301615754;p=bertos.git diff --git a/bertos/cpu/avr/drv/i2c_avr.c b/bertos/cpu/avr/drv/i2c_avr.c index 739e4ab3..eb8b8da9 100644 --- a/bertos/cpu/avr/drv/i2c_avr.c +++ b/bertos/cpu/avr/drv/i2c_avr.c @@ -34,12 +34,14 @@ * * \author Stefano Fedrigo * \author Bernie Innocenti + * \author Daniele Basile */ -#include /* CPU_FREQ */ #include "cfg/cfg_i2c.h" +#include /* CPU_FREQ */ + #define LOG_LEVEL I2C_LOG_LEVEL #define LOG_FORMAT I2C_LOG_FORMAT @@ -58,6 +60,7 @@ #include +#if !CONFIG_I2C_DISABLE_OLD_API /* Wait for TWINT flag set: bus is ready */ #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT))) @@ -207,57 +210,11 @@ int i2c_builtin_get(bool ack) return (int)(uint8_t)TWDR; } - -MOD_DEFINE(i2c); - -/** - * Initialize TWI module. - */ -void i2c_builtin_init(void) -{ - ATOMIC( - /* - * This is pretty useless according to AVR's datasheet, - * but it helps us driving the TWI data lines on boards - * where the bus pull-up resistors are missing. This is - * probably due to some unwanted interaction between the - * port pin and the TWI lines. - */ -#if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281 - PORTD |= BV(PD0) | BV(PD1); - DDRD |= BV(PD0) | BV(PD1); -#elif CPU_AVR_ATMEGA8 - PORTC |= BV(PC4) | BV(PC5); - DDRC |= BV(PC4) | BV(PC5); -#elif CPU_AVR_ATMEGA32 - PORTC |= BV(PC1) | BV(PC0); - DDRC |= BV(PC1) | BV(PC0); -#else - #error Unsupported architecture -#endif - - /* - * Set speed: - * F = CPU_FREQ / (16 + 2*TWBR * 4^TWPS) - */ - #ifndef CONFIG_I2C_FREQ - #warning Using default value of 300000L for CONFIG_I2C_FREQ - #define CONFIG_I2C_FREQ 300000L /* ~300 kHz */ - #endif - #define TWI_PRESC 1 /* 4 ^ TWPS */ - - TWBR = (CPU_FREQ / (2 * CONFIG_I2C_FREQ * TWI_PRESC)) - (8 / TWI_PRESC); - TWSR = 0; - TWCR = BV(TWEN); - ); - MOD_INIT(i2c); -} +#endif /* !CONFIG_I2C_DISABLE_OLD_API */ /* * New Api */ - - struct I2cHardware { }; @@ -378,7 +335,7 @@ static uint8_t i2c_avr_getc(I2c *i2c) WAIT_READY(); - if (TW_STATUS != TW_MR_DATA_ACK) + if (TW_STATUS != data_flag) { LOG_ERR("Data nack[%x]\n", TWSR); i2c->errors |= I2C_DATA_NACK; @@ -427,7 +384,7 @@ void i2c_hw_init(I2c *i2c, int dev, uint32_t clock) * probably due to some unwanted interaction between the * port pin and the TWI lines. */ - #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281 + #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA1280 || CPU_AVR_ATMEGA2560 PORTD |= BV(PD0) | BV(PD1); DDRD |= BV(PD0) | BV(PD1); #elif CPU_AVR_ATMEGA8 @@ -451,6 +408,4 @@ void i2c_hw_init(I2c *i2c, int dev, uint32_t clock) TWSR = 0; TWCR = BV(TWEN); ); - - MOD_INIT(i2c); }