/*! * \brief Initialize TWI interface. * * The specified slave address is used only, if the local system * is running as a slave. Anyway, care must be taken that it doesn't * conflict with another connected device. * * \note This function is only available on ATmega128 systems. * * \param sla Slave address, must be specified as a 7-bit address, * always lower than 128. */ int TwInit(u_char sla) { #ifndef __AVR_ENHANCED__ return -1; #else u_long speed = 2400; if (NutRegisterIrqHandler(&sig_2WIRE_SERIAL, TwInterrupt, 0)) { return -1; } /* * Set address register, enable general call address, set transfer * speed and enable interface. */ outb(TWAR, (sla << 1) | 1); TwIOCtl(TWI_SETSPEED, &speed); outb(TWCR, _BV(TWINT)); outb(TWCR, _BV(TWEN) | _BV(TWIE)); /* * Initialize mutex semaphores. */ NutEventPost(&tw_mm_mutex); return 0; #endif /* __AVR_ENHANCED__ */ }
void initI2C(void) { static uint8_t runOnce = 0; if (!runOnce) { runOnce = 1; u_long baud = 100000; TwInit(0); TwIOCtl(TWI_SETSPEED, &baud); DDRD &= ~( _BV(PD0) | _BV(PD1)); PORTD |= _BV(PD0) | _BV(PD1); } }
/* The speed of our happy I2C bus */ static const int i2c_speed = 2400; static u_char myaddr = 0; /* ??? What address do I use? */ void i2c_init() { log(1, "i2c_init()"); /* Set the port high first, or it won't work... is this a bug? */ outb(PORTD, 1); /* or 0xff */ /* Initialize TWI/I2C interface */ TwInit(myaddr); /* Set the bus speed to the correct speed.... */ TwIOCtl(TWI_SETSPEED, (void *)&i2c_speed); i2c_findslaves(); //i2c_start_threads(); }