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)
| Parameter | Value |
|---|---|
| Device address byte | 1 0 1 0 A2 A1 A0 R/W (the A bits must match the levels on pins A2 to A0) |
| SCL frequency | 400 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
| Field | Standard-mode, 100 kHz | Fast-mode, 400 kHz |
|---|---|---|
| PRESC | 3 | 1 |
| SCLDEL | 0x4 | 0x3 |
| SDADEL | 0x2 | 0x2 |
| SCLH | 0x0F | 0x03 |
| SCLL | 0x13 | 0x09 |
TIMINGR (0x10) layout
| Bits | 31:28 | 27:24 | 23:20 | 19:16 | 15:8 | 7:0 |
|---|---|---|---|---|---|---|
| Field | PRESC | reserved | SCLDEL | SDADEL | SCLH | SCLL |
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 1Learn it first
- Reading datasheets and schematics · Where the answers are, min/typ/max, calculating from the tables and reading register maps.
More Reading datasheets and schematics problems
- Build a register from its datasheet table · Assemble the ADS1115 ADC's 16-bit config register from its fields.
- Baud rate divider from the reference manual · Turn the reference manual's baud rate formula into a BRR register value, in both oversampling modes.
- ADC timing from the electrical characteristics · Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time.
- Bit-bang SPI from a timing diagram · Read the clock polarity and phase off a sensor's timing diagram and drive the pins to match.
- A sensor frame, its CRC and its transfer function · Check the CRC the datasheet specifies, then convert raw readings to temperature and humidity.
- Apply the errata sheet's workarounds · An ADC driver that follows the reference manual still reads wrong values. The errata sheet explains why.
- Route a UART to its pins from the alternate-function table · Find USART2's pins and function number in the datasheet, then set MODER, AFRL, OSPEEDR and PUPDR without disturbing other pins.
- An open-drain interrupt line, from the sensor's pin description · The datasheet says INT is open-drain and active low. Configure the pin so it reads correctly, and report when the sensor asserts it.
- A 10 ms tick, from the clock tree · Work out the timer's input clock from the clock tree, including the APB timer rule, then set the prescaler and auto-reload for a 10 ms period.
- Configure a sensor over SPI from its datasheet · Pick the SPI mode and the fastest clock the sensor allows, then set its data rate and range from the register map, including a non-obvious field encoding.
- Receive from a GPS module, from its interface description · Set the baud rate divider and frame format the module's datasheet gives, with only the receiver enabled, and read one NMEA sentence.
- An LED and a button, from the board schematic · The schematic decides which level lights the LED and which level means pressed. Write the three functions that hide it.
- Battery voltage through a resistor divider · Read the divider ratio and the ADC's reference voltage off the schematic, and turn a raw reading into millivolts.
- Trace an expansion header to the microcontroller's pins · Follow each header pin's net through resistors, a level shifter and solder bridges to the pin it ends at.
- A relay and a sensor's power switch, from the transistors · One transistor turns its load on with a high pin, the other with a low pin. Work out which from the schematic, and keep both off from reset.
- Route a pin's interrupt, from the reference manual · Use the SYSCFG and EXTI register descriptions and the vector table to send any pin's edges to its interrupt.
- Choose a DMA stream from the request mapping table · Find a free stream and channel for a peripheral's DMA request in the reference manual's table, and compose the stream's control register.