|
|
|
|
@ -2,14 +2,30 @@
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
#include <util/twi.h>
|
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
|
|
|
|
|
#include "i2c_simple.h"
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
//#define I2C_WAIT() while(!(TWCR & _BV(TWINT)))
|
|
|
|
|
#define I2C_WAIT() i2c_wait();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void inline i2c_wait(){
|
|
|
|
|
uint16_t cntr=0;
|
|
|
|
|
while(!(TWCR & _BV(TWINT))){
|
|
|
|
|
cntr++;
|
|
|
|
|
if(cntr >= 1000){
|
|
|
|
|
dbgLog("i2c waittime exceeded!\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define I2C_WAIT() while(!(TWCR & _BV(TWINT)))
|
|
|
|
|
|
|
|
|
|
void i2c_init()
|
|
|
|
|
{
|
|
|
|
|
TWBR = 32;//bit rate
|
|
|
|
|
TWBR = 32;//bit rate: 100kbit
|
|
|
|
|
TWSR = 0;//Prescaler
|
|
|
|
|
|
|
|
|
|
// TWAR = 0x80;//our address 1000 000, don't listen to general call
|
|
|
|
|
@ -53,6 +69,7 @@ uint8_t i2c_read(uint8_t addr, uint8_t len, uint8_t *data)
|
|
|
|
|
data[done] = TWDR;
|
|
|
|
|
done++;
|
|
|
|
|
TWCR |= _BV(TWINT) | _BV(TWSTO);
|
|
|
|
|
_delay_us(20); // wait about 2 bit periods
|
|
|
|
|
return done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -91,6 +108,7 @@ uint8_t i2c_write_i(uint8_t addr, uint8_t len, uint8_t *data, uint8_t stop)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(stop) {TWCR |= _BV(TWINT) | _BV(TWSTO);}
|
|
|
|
|
_delay_us(20); // wait about 2 bit periods
|
|
|
|
|
return done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|