Skip to content

Commit d4c7854

Browse files
committed
[VIDORBL] Add timeouts to i2c operations
1 parent e66adea commit d4c7854

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

bootloaders/zero/board_driver_i2c.c

+24-3
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,34 @@ static inline void prepareAckBitWIRE( void )
246246
I2C_SERCOM->I2CM.CTRLB.bit.ACKACT = 0;
247247
}
248248

249-
static inline void prepareCommandBitsWire(uint8_t cmd)
249+
static inline int prepareCommandBitsWire(uint8_t cmd)
250250
{
251251
I2C_SERCOM->I2CM.CTRLB.bit.CMD = cmd;
252252

253+
int timeout = 50000;
253254
while(I2C_SERCOM->I2CM.SYNCBUSY.bit.SYSOP)
254255
{
256+
if (timeout -- < 0) {
257+
return -1;
258+
}
255259
// Waiting for synchronization
256260
}
261+
return 0;
257262
}
258263

259264
static inline bool startTransmissionWIRE(uint8_t address, SercomWireReadWriteFlag flag)
260265
{
261266
// 7-bits address + 1-bits R/W
262267
address = (address << 0x1ul) | flag;
263268

269+
int timeout = 50000;
264270

265271
// Wait idle or owner bus mode
266-
while ( !isBusIdleWIRE() && !isBusOwnerWIRE() );
272+
while ( !isBusIdleWIRE() && !isBusOwnerWIRE() ) {
273+
if (timeout -- < 0) {
274+
return false;
275+
}
276+
}
267277

268278
// Send start and address
269279
I2C_SERCOM->I2CM.ADDR.bit.ADDR = address;
@@ -273,13 +283,19 @@ static inline bool startTransmissionWIRE(uint8_t address, SercomWireReadWriteFla
273283
{
274284
while( !I2C_SERCOM->I2CM.INTFLAG.bit.MB )
275285
{
286+
if (timeout -- < 0) {
287+
return false;
288+
}
276289
// Wait transmission complete
277290
}
278291
}
279292
else // Read mode
280293
{
281294
while( !I2C_SERCOM->I2CM.INTFLAG.bit.SB )
282295
{
296+
if (timeout -- < 0) {
297+
return false;
298+
}
283299
// If the slave NACKS the address, the MB bit will be set.
284300
// In that case, send a stop condition and return false.
285301
if (I2C_SERCOM->I2CM.INTFLAG.bit.MB) {
@@ -310,9 +326,14 @@ static inline bool sendDataMasterWIRE(uint8_t data)
310326
//Send data
311327
I2C_SERCOM->I2CM.DATA.bit.DATA = data;
312328

329+
int timeout = 50000;
330+
313331
//Wait transmission successful
314332
while(!I2C_SERCOM->I2CM.INTFLAG.bit.MB) {
315333

334+
if (timeout -- < 0) {
335+
return false;
336+
}
316337
// If a bus error occurs, the MB bit may never be set.
317338
// Check the bus error bit and bail if it's set.
318339
if (I2C_SERCOM->I2CM.STATUS.bit.BUSERR) {
@@ -410,7 +431,7 @@ uint8_t i2c_endTransmission(bool stopBit)
410431
txBufferLen--;
411432
}
412433
}
413-
434+
414435
if (stopBit)
415436
{
416437
prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);

bootloaders/zero/board_driver_pmic.c

+17-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ extern uint8_t rxBuffer[1];
2727
uint8_t readRegister(uint8_t reg) {
2828
i2c_beginTransmission(PMIC_ADDRESS);
2929
i2c_write(reg);
30-
i2c_endTransmission(true);
30+
int ret = i2c_endTransmission(true);
31+
if (ret != 0) {
32+
return 0;
33+
}
3134

3235
i2c_requestFrom(PMIC_ADDRESS, 1, true);
3336
return rxBuffer[0];
@@ -44,8 +47,11 @@ uint8_t writeRegister(uint8_t reg, uint8_t data) {
4447

4548
bool disableWatchdog(void) {
4649
uint8_t DATA = readRegister(CHARGE_TIMER_CONTROL_REGISTER);
50+
if (DATA == 0) {
51+
return false;
52+
}
4753
writeRegister(CHARGE_TIMER_CONTROL_REGISTER, (DATA & 0b11001110));
48-
return 1;
54+
return true;
4955
}
5056

5157
bool setInputVoltageLimit(uint16_t voltage) {
@@ -217,22 +223,26 @@ bool disableCharge()
217223
return 1;
218224
}
219225

220-
void apply_pmic_newdefaults()
226+
int apply_pmic_newdefaults()
221227
{
222-
disableWatchdog();
228+
if (!disableWatchdog()) {
229+
return -1;
230+
}
223231

224232
//disableDPDM();
225233
disableCharge();
226234
setInputVoltageLimit(4360); // default
227-
setInputCurrentLimit(2000); // 2A
235+
setInputCurrentLimit(3000); // 2A
228236
setChargeCurrent(0,0,0,0,0,0); // 512mA
229237
setChargeVoltage(4112); // 4.112V termination voltage
238+
i2c_end();
239+
return 0;
230240
}
231241

232-
void configure_pmic()
242+
int configure_pmic()
233243
{
234244
i2c_init(100000);
235-
apply_pmic_newdefaults();
245+
return apply_pmic_newdefaults();
236246
}
237247

238248
#endif

bootloaders/zero/board_driver_pmic.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
#define FAULT_REGISTER 0x09
4141
#define PMIC_VERSION_REGISTER 0x0A
4242

43-
void configure_pmic();
43+
int configure_pmic();
4444

4545
#endif // _BOARD_DRIVER_PMIC_

0 commit comments

Comments
 (0)