diff --git a/sw/airborne/arch/lpc21/mcu_periph/i2c_arch.c b/sw/airborne/arch/lpc21/mcu_periph/i2c_arch.c index 49c46ed1ab..eb990fdebe 100644 --- a/sw/airborne/arch/lpc21/mcu_periph/i2c_arch.c +++ b/sw/airborne/arch/lpc21/mcu_periph/i2c_arch.c @@ -342,6 +342,43 @@ bool_t i2c_submit(struct i2c_periph* p, struct i2c_transaction* t) { } void i2c_event(void) { } -void i2c2_setbitrate(int bitrate __attribute__ ((unused))) { } +void i2c_setbitrate(struct i2c_periph* p, int bitrate) +{ + int period = 15000000 / 2 / bitrate; + // Max 400kpbs + if (period < 19) + period = 19; + // Min 5kbps + if (period > 1500) + period = 1500; + +/* default clock speed 37.5KHz with our 15MHz PCLK + I2C1_CLOCK = PCLK / (I2C1_SCLL + I2C1_SCLH) */ + +#if (PCLK == 30000000) + period *= 2; +#endif + +#if (PCLK == 60000000) + period *= 4; +#endif + +#ifdef USE_I2C0 + if (p == &i2c0) + { + /* set bitrate */ + I2C0SCLL = period; + I2C0SCLH = period; + } +#endif +#ifdef USE_I2C1 + if (p == &i2c1) + { + /* set bitrate */ + I2C1SCLL = period; + I2C1SCLH = period; + } +#endif +}