Help writing I2C library

Hi @c0ntrarian :wave:

Well the prescaler really depends on the system clock you are using. In my example I’m running a core clock of 200 MHz which results in a prescaler of 399 (0x18F) for 100 kHz I2C clock.

One thing that immediately is obvious to me is that you are just enabling the I2C core (btw. writing 0x80 to the control register is enough since its set’s bit #7 which enables the I2C core). In any way - in order to get data to be clocked out on the bus you need to generate a start condition by writing to the command register (and first filling the slave address into the data register), e.g.

DATA = my_slave_addr;
COMMAND_STATUS = (1<<7) | (1<<4); /* STA (Bit 7) + WR (Bit 4) */

Also the GPIO special function register GPIO_IOF_EN and GPIO_IOF_SEL for the I2C must be configured to route I2C to the actual MCU pins and not the GPIO ports (see here) with

I2C0_SDA = 12, /* I2C0 SDA */
I2C0_SCL = 13, /* I2C0 SCL */

Have fun :wink:

1 Like