1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-20 05:18:08 +00:00

feat(core): Measure Optiga response timeout in ms not number of attempts.

[no changelog]
This commit is contained in:
Andrew Kozlik 2024-04-30 11:08:23 +02:00 committed by Andrew Kozlik
parent 594d7425cd
commit d453dac45c

View File

@ -65,8 +65,8 @@ static const uint16_t OPTIGA_ADDRESS = (BASE_ADDR << 1);
static const uint32_t I2C_TIMEOUT = 15; static const uint32_t I2C_TIMEOUT = 15;
static const int I2C_MAX_RETRY_COUNT = 10; static const int I2C_MAX_RETRY_COUNT = 10;
// Maximum number of times to retry reading Optiga's response to a command. // Maximum time in millisecods to retry reading Optiga's response to a command.
static const int MAX_RETRY_READ = 10000; static const int MAX_RETRY_READ_MS = 300;
// Maximum number of times to retry reading Optiga's response to a command when // Maximum number of times to retry reading Optiga's response to a command when
// it claims it's not busy executing a command. // it claims it's not busy executing a command.
@ -274,7 +274,8 @@ static optiga_result optiga_check_ack(void) {
static optiga_result optiga_ensure_ready(void) { static optiga_result optiga_ensure_ready(void) {
optiga_result ret = OPTIGA_SUCCESS; optiga_result ret = OPTIGA_SUCCESS;
for (int i = 0; i < MAX_RESPONSE_PACKET_COUNT; ++i) { for (int i = 0; i < MAX_RESPONSE_PACKET_COUNT; ++i) {
for (int j = 0; j < MAX_RETRY_READ; ++j) { uint32_t deadline = hal_ticks_ms() + MAX_RETRY_READ_MS;
do {
ret = optiga_i2c_write(&REG_I2C_STATE, 1); ret = optiga_i2c_write(&REG_I2C_STATE, 1);
if (OPTIGA_SUCCESS != ret) { if (OPTIGA_SUCCESS != ret) {
return ret; return ret;
@ -295,7 +296,7 @@ static optiga_result optiga_ensure_ready(void) {
return OPTIGA_SUCCESS; return OPTIGA_SUCCESS;
} }
ret = OPTIGA_ERR_BUSY; ret = OPTIGA_ERR_BUSY;
} } while (hal_ticks_ms() < deadline);
if (ret != OPTIGA_SUCCESS) { if (ret != OPTIGA_SUCCESS) {
// Optiga is busy even after maximum retries at reading the I2C state. // Optiga is busy even after maximum retries at reading the I2C state.
@ -379,7 +380,8 @@ static optiga_result optiga_read(void) {
// it claimed it's not busy executing a command. // it claimed it's not busy executing a command.
int not_busy_count = 0; int not_busy_count = 0;
for (int i = 0; i < MAX_RETRY_READ; ++i) { uint32_t deadline = hal_ticks_ms() + MAX_RETRY_READ_MS;
do {
optiga_result ret = optiga_i2c_write(&REG_I2C_STATE, 1); optiga_result ret = optiga_i2c_write(&REG_I2C_STATE, 1);
if (OPTIGA_SUCCESS != ret) { if (OPTIGA_SUCCESS != ret) {
return ret; return ret;
@ -425,7 +427,7 @@ static optiga_result optiga_read(void) {
} }
not_busy_count += 1; not_busy_count += 1;
} }
} } while (hal_ticks_ms() < deadline);
return OPTIGA_ERR_TIMEOUT; return OPTIGA_ERR_TIMEOUT;
} }