Simvik

Fast-mode I2C and the EEPROM's address, from three documents

Compose TIMINGR from the reference manual's timing table, work out the EEPROM's address from its datasheet and the schematic, and probe for it. A medium…

medium · Reading datasheets and schematics · datasheets · driver · i2c

Working from the Datasheet excerpt below, write:

  • i2c_fast_init(), which sets I2C1's TIMINGR for the fastest bus speed the EEPROM supports on this board, then enables the peripheral (PE).
  • eeprom_present(), which sends the EEPROM's address with no data bytes (an address-only write, ended by AUTOEND) and returns 1 if it acknowledged, 0 if not. Clear the flags it leaves, so it can be called again.

The 7-bit address goes in SADD shifted left by one, as I2C_CR2_SADD(a) does.

Datasheet excerpt

EEPROM (24C02 datasheet)

ParameterValue
Device address byte1 0 1 0 A2 A1 A0 R/W (the A bits must match the levels on pins A2 to A0)
SCL frequency400 kHz maximum at VCC 2.5 V to 5.5 V; 100 kHz at 1.8 V

Board schematic

U3 (24C02): A0, A1 and A2 tied to GND. VCC is 3.3 V.

I2C timing settings (reference manual), I2CCLK = 16 MHz

FieldStandard-mode, 100 kHzFast-mode, 400 kHz
PRESC31
SCLDEL0x40x3
SDADEL0x20x2
SCLH0x0F0x03
SCLL0x130x09

TIMINGR (0x10) layout

Bits31:2827:2423:2019:1615:87:0
FieldPRESCreservedSCLDELSDADELSCLHSCLL

Starter code

void i2c_fast_init(void) {
  I2C1->TIMINGR = 0x30420F13;
  I2C1->CR1 = 1;
}

int eeprom_present(void) {
  I2C1->CR2 = I2C_CR2_SADD(0xA0) | I2C_CR2_NBYTES(0) | I2C_CR2_AUTOEND | I2C_CR2_START;
  while (!(I2C1->ISR & I2C_ISR_STOPF)) {
  }
  return !(I2C1->ISR & I2C_ISR_NACKF);
}

Example tests

Fast mode, and the EEPROM answers

i2c_fast_init();
printf("TIMINGR 0x%08X present %d\n", (unsigned)I2C1->TIMINGR, eeprom_present());

Expected output:

i2c1: write 0x50 []
TIMINGR 0x10320309 present 1

Learn it first

More Reading datasheets and schematics problems