Skip to content

Commit 7d5f6cf

Browse files
NRF52: fix i2c timeouts
add tick to us conversion
1 parent 97ef25f commit 7d5f6cf

File tree

1 file changed

+13
-8
lines changed
  • targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52

1 file changed

+13
-8
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/i2c_api.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
#define MAXIMUM_TIMEOUT_US (10000) // timeout for waiting for RX
7070
#define I2C_READ_BIT 0x01 // read bit
7171

72+
static uint32_t tick2us = 1;
73+
7274
/* Keep track of what mode the peripheral is in. On NRF52, Driver mode can use TWIM. */
7375
typedef enum {
7476
NORDIC_I2C_MODE_NONE,
@@ -106,6 +108,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
106108
struct i2c_s *config = obj;
107109
#endif
108110

111+
const ticker_info_t *ti = lp_ticker_get_info();
112+
tick2us = 1000000 / ti->frequency;
113+
109114
/* Get instance from pin configuration. */
110115
int instance = pin_instance_i2c(sda, scl);
111116
MBED_ASSERT(instance < NRFX_TWI_ENABLED_COUNT);
@@ -379,13 +384,13 @@ int i2c_byte_write(i2c_t *obj, int data)
379384
}
380385

381386
/* Setup stop watch for timeout. */
382-
uint32_t start_us = lp_ticker_read();
387+
uint32_t start_us = tick2us * lp_ticker_read();
383388
uint32_t now_us = start_us;
384389

385390
/* Block until timeout or an address error has been detected. */
386391
while (((now_us - start_us) < DEFAULT_TIMEOUT_US) &&
387392
!(nrf_twi_event_check(nordic_nrf5_twi_register[instance], NRF_TWI_EVENT_ERROR))) {
388-
now_us = lp_ticker_read();
393+
now_us = tick2us * lp_ticker_read();
389394
}
390395

391396
/* Check error register and update return value if an address NACK was detected. */
@@ -399,13 +404,13 @@ int i2c_byte_write(i2c_t *obj, int data)
399404
nrf_twi_txd_set(nordic_nrf5_twi_register[instance], data);
400405

401406
/* Setup stop watch for timeout. */
402-
uint32_t start_us = lp_ticker_read();
407+
uint32_t start_us = tick2us * lp_ticker_read();
403408
uint32_t now_us = start_us;
404409

405410
/* Block until timeout or the byte has been sent. */
406411
while (((now_us - start_us) < MAXIMUM_TIMEOUT_US) &&
407412
!(nrf_twi_event_check(nordic_nrf5_twi_register[instance], NRF_TWI_EVENT_TXDSENT))) {
408-
now_us = lp_ticker_read();
413+
now_us = tick2us * lp_ticker_read();
409414
}
410415

411416
/* Check the error code to see if the byte was acknowledged. */
@@ -466,13 +471,13 @@ int i2c_byte_read(i2c_t *obj, int last)
466471
nrf_twi_task_trigger(nordic_nrf5_twi_register[instance], NRF_TWI_TASK_RESUME);
467472

468473
/* Setup timeout */
469-
start_us = lp_ticker_read();
474+
start_us = tick2us * lp_ticker_read();
470475
now_us = start_us;
471476

472477
/* Block until timeout or data ready event has been signaled. */
473478
while (((now_us - start_us) < MAXIMUM_TIMEOUT_US) &&
474479
!(nrf_twi_event_check(nordic_nrf5_twi_register[instance], NRF_TWI_EVENT_RXDREADY))) {
475-
now_us = lp_ticker_read();
480+
now_us = tick2us * lp_ticker_read();
476481
}
477482

478483
/* Retrieve data from buffer. */
@@ -506,12 +511,12 @@ int i2c_stop(i2c_t *obj)
506511
nrf_twi_task_trigger(nordic_nrf5_twi_register[instance], NRF_TWI_TASK_STOP);
507512

508513
/* Block until stop signal has been generated. */
509-
uint32_t start_us = lp_ticker_read();
514+
uint32_t start_us = tick2us * lp_ticker_read();
510515
uint32_t now_us = start_us;
511516

512517
while (((now_us - start_us) < MAXIMUM_TIMEOUT_US) &&
513518
!(nrf_twi_event_check(nordic_nrf5_twi_register[instance], NRF_TWI_EVENT_STOPPED))) {
514-
now_us = lp_ticker_read();
519+
now_us = tick2us * lp_ticker_read();
515520
}
516521

517522
/* Reset state. */

0 commit comments

Comments
 (0)