/* ANY-LICENSE V1.0 ---------------- You can use these files under any license approved by the Open Source Initiative, preferrably one of the popular licenses, as long as the license you choose is compatible to the dependencies of these files. See http://www.opensource.org/licenses/ for a list of approved licenses. Author: Martin Furter Project: Tins AVR Lib Repository: http://repos.borg.ch/projects/tins_avr_lib/trunk Copyright: 2016 */ #include #include #include #if 0 #include #include extern void flash_print( const char* PROGMEM str ); #define P(s) do { static const char tmp_str[] PROGMEM = s; flash_print( tmp_str ); } while(0) #else #define P(x) #define usart_add_hexbyte(x) #endif uint8_t i2c_last_status; uint8_t i2c_exp_status; void i2c_init( bool fast ) { // SCL-freq = F_CPU / 16+2(TWBR) // => 16+2(TWBR) = F_CPU / SCL-Freq // => 2(TWBR) = F_CPU / SCL-Freq - 16 // => TWBR = (F_CPU / SCL-Freq - 16) / 2 if( fast ) { TWBR = (F_CPU / 400000 - 16) / 2; } else { TWBR = (F_CPU / 125000 - 16) / 2; } // TWPS1 TWPS0 prescaler values TWSR = 0; // TWSR = (1< 0 || count2 > 0 ) { // Load SLA_W into TWDR Register. Clear TWINT bit in TWCR to start // transmission of address TWDR = addr; TWCR = (1< 0 ) { // initiate transfer if( count > 1 ) { TWCR = (1< 1 ? TW_MR_DATA_ACK : TW_MR_DATA_NACK; if( i2c_last_status != i2c_exp_status ) { return false; } // got a data byte *data = TWDR; data++; count--; } // Transmit STOP condition P( "stop\r\n" ); TWCR = (1<