69
69
#define MAXIMUM_TIMEOUT_US (10000) // timeout for waiting for RX
70
70
#define I2C_READ_BIT 0x01 // read bit
71
71
72
+ static uint32_t tick2us = 1 ;
73
+
72
74
/* Keep track of what mode the peripheral is in. On NRF52, Driver mode can use TWIM. */
73
75
typedef enum {
74
76
NORDIC_I2C_MODE_NONE ,
@@ -106,6 +108,9 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
106
108
struct i2c_s * config = obj ;
107
109
#endif
108
110
111
+ const ticker_info_t * ti = lp_ticker_get_info ();
112
+ tick2us = 1000000 / ti -> frequency ;
113
+
109
114
/* Get instance from pin configuration. */
110
115
int instance = pin_instance_i2c (sda , scl );
111
116
MBED_ASSERT (instance < NRFX_TWI_ENABLED_COUNT );
@@ -379,13 +384,13 @@ int i2c_byte_write(i2c_t *obj, int data)
379
384
}
380
385
381
386
/* Setup stop watch for timeout. */
382
- uint32_t start_us = lp_ticker_read ();
387
+ uint32_t start_us = tick2us * lp_ticker_read ();
383
388
uint32_t now_us = start_us ;
384
389
385
390
/* Block until timeout or an address error has been detected. */
386
391
while (((now_us - start_us ) < DEFAULT_TIMEOUT_US ) &&
387
392
!(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 ();
389
394
}
390
395
391
396
/* 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)
399
404
nrf_twi_txd_set (nordic_nrf5_twi_register [instance ], data );
400
405
401
406
/* Setup stop watch for timeout. */
402
- uint32_t start_us = lp_ticker_read ();
407
+ uint32_t start_us = tick2us * lp_ticker_read ();
403
408
uint32_t now_us = start_us ;
404
409
405
410
/* Block until timeout or the byte has been sent. */
406
411
while (((now_us - start_us ) < MAXIMUM_TIMEOUT_US ) &&
407
412
!(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 ();
409
414
}
410
415
411
416
/* Check the error code to see if the byte was acknowledged. */
@@ -466,13 +471,13 @@ int i2c_byte_read(i2c_t *obj, int last)
466
471
nrf_twi_task_trigger (nordic_nrf5_twi_register [instance ], NRF_TWI_TASK_RESUME );
467
472
468
473
/* Setup timeout */
469
- start_us = lp_ticker_read ();
474
+ start_us = tick2us * lp_ticker_read ();
470
475
now_us = start_us ;
471
476
472
477
/* Block until timeout or data ready event has been signaled. */
473
478
while (((now_us - start_us ) < MAXIMUM_TIMEOUT_US ) &&
474
479
!(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 ();
476
481
}
477
482
478
483
/* Retrieve data from buffer. */
@@ -506,12 +511,12 @@ int i2c_stop(i2c_t *obj)
506
511
nrf_twi_task_trigger (nordic_nrf5_twi_register [instance ], NRF_TWI_TASK_STOP );
507
512
508
513
/* Block until stop signal has been generated. */
509
- uint32_t start_us = lp_ticker_read ();
514
+ uint32_t start_us = tick2us * lp_ticker_read ();
510
515
uint32_t now_us = start_us ;
511
516
512
517
while (((now_us - start_us ) < MAXIMUM_TIMEOUT_US ) &&
513
518
!(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 ();
515
520
}
516
521
517
522
/* Reset state. */
0 commit comments