@@ -287,8 +287,13 @@ uint32_t i2cGetFrequency(i2c_t * i2c)
287
287
if (i2c == NULL ){
288
288
return 0 ;
289
289
}
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 ;
292
297
}
293
298
294
299
/*
@@ -313,6 +318,8 @@ i2c_t * i2cInit(uint8_t i2c_num) //before this is called, pins should be detache
313
318
}
314
319
}
315
320
#endif
321
+ I2C_MUTEX_LOCK ();
322
+ uint32_t old_clock = i2cGetFrequency (i2c );
316
323
317
324
if (i2c_num == 0 ) {
318
325
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
323
330
DPORT_SET_PERI_REG_MASK (DPORT_PERIP_CLK_EN_REG ,DPORT_I2C_EXT1_CLK_EN );
324
331
DPORT_CLEAR_PERI_REG_MASK (DPORT_PERIP_RST_EN_REG ,DPORT_I2C_EXT1_RST );
325
332
}
326
-
327
- I2C_MUTEX_LOCK ();
328
333
i2c -> dev -> ctr .val = 0 ;
329
334
i2c -> dev -> ctr .ms_mode = 1 ;
330
335
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
338
343
339
344
i2c -> dev -> slave_addr .val = 0 ;
340
345
I2C_MUTEX_UNLOCK ();
346
+
347
+ if (old_clock ) i2cSetFrequency (i2c ,old_clock ); // reconfigure
341
348
342
349
return i2c ;
343
350
}
@@ -967,7 +974,11 @@ i2c_err_t i2cProcQueue(i2c_t * i2c, uint32_t *readCount, uint16_t timeOutMillis)
967
974
if (i2c == NULL ){
968
975
return I2C_ERROR_DEV ;
969
976
}
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
+ }
971
982
I2C_MUTEX_LOCK ();
972
983
/* what about co-existance with SLAVE mode?
973
984
Should I check if a slaveMode xfer is in progress and hang
0 commit comments