Skip to content

Commit a391f54

Browse files
authored
Merge pull request #427 from ChibiOS/i2c_eeprom_freq
hal_ee24xx: add bus frequency parameter
2 parents b396c79 + 2c04fcc commit a391f54

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

os/hal/include/hal_ee24xx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ typedef struct {
2626
* Address of IC on I2C bus.
2727
*/
2828
i2caddr_t addr;
29+
/**
30+
* I2C bus frequency in Hz. Used to compute transaction timeouts.
31+
* If zero, a conservative default will be used by the driver.
32+
*/
33+
uint32_t bus_hz;
2934
/**
3035
* Pointer to write buffer. The safest size is (pagesize + 2)
3136
* Declare with CC_SECTION(".nocache") ... if using I2C with DMA to avoid

os/hal/src/hal_ee24xx.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ Write cycle time (byte or page) - 5 ms
5959
#define EEPROM_I2C_CLOCK (i2cp->config->clock_speed)
6060
#endif
6161
*/
62-
#define EEPROM_I2C_CLOCK 400000
62+
/* Fallback frequency if not provided via config and not detectable */
63+
#define EEPROM_I2C_CLOCK_DEFAULT 400000U
6364

6465
/*
6566
******************************************************************************
@@ -98,12 +99,13 @@ Write cycle time (byte or page) - 5 ms
9899
/**
99100
* @brief Calculates requred timeout.
100101
*/
101-
static systime_t calc_timeout(I2CDriver *i2cp, size_t txbytes, size_t rxbytes) {
102+
static systime_t calc_timeout(I2CDriver *i2cp, size_t txbytes, size_t rxbytes, uint32_t bus_hz) {
102103
(void)i2cp;
103104
const uint32_t bitsinbyte = 10;
104105
uint32_t tmo;
106+
uint32_t freq = bus_hz ? bus_hz : EEPROM_I2C_CLOCK_DEFAULT;
105107
tmo = ((txbytes + rxbytes + 1) * bitsinbyte * 1000);
106-
tmo /= EEPROM_I2C_CLOCK;
108+
tmo /= freq;
107109
tmo += 10; /* some additional milliseconds to be safer */
108110
return TIME_MS2I(tmo);
109111
}
@@ -126,7 +128,7 @@ static msg_t eeprom_read(const I2CEepromFileConfig *eepcfg,
126128
const bool one_byte_addr = (eepcfg->size <= 2048U);
127129
const uint32_t eff_off = offset + eepcfg->barrier_low;
128130
const size_t addr_bytes = one_byte_addr ? 1U : 2U;
129-
systime_t tmo = calc_timeout(eepcfg->i2cp, addr_bytes, len);
131+
systime_t tmo = calc_timeout(eepcfg->i2cp, addr_bytes, len, eepcfg->bus_hz);
130132

131133
osalDbgAssert(((len <= eepcfg->size) && ((offset + len) <= eepcfg->size)),
132134
"out of device bounds");
@@ -175,7 +177,7 @@ static msg_t eeprom_write(const I2CEepromFileConfig *eepcfg, uint32_t offset,
175177
const bool one_byte_addr = (eepcfg->size <= 2048U);
176178
const uint32_t eff_off = offset + eepcfg->barrier_low;
177179
const size_t addr_bytes = one_byte_addr ? 1U : 2U;
178-
systime_t tmo = calc_timeout(eepcfg->i2cp, (len + addr_bytes), 0);
180+
systime_t tmo = calc_timeout(eepcfg->i2cp, (len + addr_bytes), 0, eepcfg->bus_hz);
179181

180182
osalDbgAssert(((len <= eepcfg->size) && ((offset + len) <= eepcfg->size)),
181183
"out of device bounds");

0 commit comments

Comments
 (0)