Skip to content

Commit 6b682d2

Browse files
authored
Forgot to reset clockrate after hardware reset
After hardware reset, all i2c hardware register are back to zero, so the clockrate needs to be recovered.
1 parent fb05a59 commit 6b682d2

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

cores/esp32/esp32-hal-i2c.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,13 @@ uint32_t i2cGetFrequency(i2c_t * i2c)
287287
if(i2c == NULL){
288288
return 0;
289289
}
290-
291-
return APB_CLK_FREQ/(i2c->dev->scl_low_period.period+i2c->dev->scl_high_period.period);
290+
uint32_t result = 0;
291+
uint32_t old_count = (i2c->dev->scl_low_period.period+i2c->dev->scl_high_period.period);
292+
if(old_count>0){
293+
result = APB_CLK_FREQ / old_count;
294+
}
295+
else result = 0;
296+
return result;
292297
}
293298

294299
/*
@@ -313,6 +318,8 @@ i2c_t * i2cInit(uint8_t i2c_num) //before this is called, pins should be detache
313318
}
314319
}
315320
#endif
321+
I2C_MUTEX_LOCK();
322+
uint32_t old_clock = i2cGetFrequency(i2c);
316323

317324
if(i2c_num == 0) {
318325
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG,DPORT_I2C_EXT0_RST); //reset hardware
@@ -323,8 +330,6 @@ i2c_t * i2cInit(uint8_t i2c_num) //before this is called, pins should be detache
323330
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG,DPORT_I2C_EXT1_CLK_EN);
324331
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG,DPORT_I2C_EXT1_RST);
325332
}
326-
327-
I2C_MUTEX_LOCK();
328333
i2c->dev->ctr.val = 0;
329334
i2c->dev->ctr.ms_mode = 1;
330335
i2c->dev->ctr.sda_force_out = 1 ;
@@ -338,6 +343,8 @@ i2c_t * i2cInit(uint8_t i2c_num) //before this is called, pins should be detache
338343

339344
i2c->dev->slave_addr.val = 0;
340345
I2C_MUTEX_UNLOCK();
346+
347+
if(old_clock) i2cSetFrequency(i2c,old_clock); // reconfigure
341348

342349
return i2c;
343350
}
@@ -967,7 +974,11 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
967974
if(i2c == NULL){
968975
return I2C_ERROR_DEV;
969976
}
970-
977+
if (i2c->dev->status_reg.bus_busy){
978+
log_e("Bus busy, reinit");
979+
i2cInit(i2c->num);
980+
return I2C_ERROR_BUSY;
981+
}
971982
I2C_MUTEX_LOCK();
972983
/* what about co-existance with SLAVE mode?
973984
Should I check if a slaveMode xfer is in progress and hang

0 commit comments

Comments
 (0)