38 #include <avr/pgmspace.h>
39 #include <avr/pgmspace.h>
40 #include <avr/sleep.h>
42 #include "dev/watchdog.h"
45 #include <compat/twi.h>
48 #include "dev/co2_sa_kxx-sensor.h"
56 TWBR = ((F_CPU / speed) - 16) / 2;
65 TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
68 for(n = 0; n < 100000 && !(TWCR & (1 << TWINT)); n++) {
75 twst = TW_STATUS & 0xF8;
76 if((twst != TW_START) && (twst != TW_REP_START)) {
82 TWCR = (1 << TWINT) | (1 << TWEN);
85 for(n = 0; n < 100000 && !(TWCR & (1 << TWINT)); n++) {
92 twst = TW_STATUS & 0xF8;
93 if((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
100 i2c_start_wait(uint8_t addr)
106 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
108 while(!(TWCR & (1<<TWINT)));
110 twst = TW_STATUS & 0xF8;
111 if ( (twst != TW_START) && (twst != TW_REP_START))
continue;
114 TWCR = (1<<TWINT) | (1<<TWEN);
116 while(!(TWCR & (1<<TWINT)));
118 twst = TW_STATUS & 0xF8;
119 if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
122 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
124 while(TWCR & (1<<TWSTO));
134 TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
136 while(TWCR & (1<<TWSTO));
142 TWCR = (1 << TWINT) | (1 << TWEN);
143 while((TWCR & (1 << TWINT)) == 0) ;
148 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
149 while((TWCR & (1 << TWINT)) == 0) ;
155 TWCR = (1 << TWINT) | (1 << TWEN);
156 while((TWCR & (1 << TWINT)) == 0) ;
160 print_delim(
int p,
char *s,
const char *d)
168 i2c_write_mem(uint8_t addr, uint8_t reg, uint8_t value)
176 i2c_read_mem(uint8_t addr, uint8_t reg, uint8_t buf[], uint8_t bytes)
182 for(i = 0; i < bytes; i++) {
184 buf[i] = i2c_readNak();
186 buf[i] = i2c_readAck();
192 i2c_at24mac_read(
char *buf, uint8_t eui64)
195 i2c_read_mem(I2C_AT24MAC_ADDR, 0x98, (uint8_t *)buf, 8);
199 i2c_read_mem(I2C_AT24MAC_ADDR, 0x80, (uint8_t *)buf, 16);
207 const char *del =
",";
212 probed |= I2C_AT24MAC;
213 print_delim(p++,
"AT24MAC", del);
219 print_delim(p++,
"SHT2X", del);
225 print_delim(p++,
"CO2SA", del);
void i2c_start(void)
Generate I2C START condition.
static uip_ds6_addr_t * addr
Pointer to a router list entry.
void i2c_stop(void)
Generate I2C STOP condition.
void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed)
Initialize the I2C peripheral and pins.
void watchdog_periodic(void)
Writes the WDT clear sequence.
void i2c_write(uint8_t data)
Send a byte to I2C bus.