Skip to content

Commit

Permalink
fixup! feat(core/embed): introduce non-blocking i2c drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
cepetr committed Sep 19, 2024
1 parent ff2c0cb commit 98995cb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 48 deletions.
55 changes: 31 additions & 24 deletions core/embed/trezorhal/stm32f4/i2c_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,30 @@ static void i2c_bus_reset(i2c_bus_t* bus) {
regs->CR1 |= I2C_CR1_PE;
}

static bool i2c_bus_init(i2c_bus_t* bus, int bus_index) {
memset(bus, 0, sizeof(i2c_bus_t));
static void i2c_bus_deinit(i2c_bus_t* bus) {
const i2c_bus_def_t* def = bus->def;

const i2c_bus_def_t* def = &g_i2c_bus_def[bus_index];
systimer_delete(bus->timer);

bus->def = def;
if (bus->def == NULL) {
return;
}

NVIC_DisableIRQ(def->ev_irq);
NVIC_DisableIRQ(def->er_irq);

I2C_TypeDef* regs = def->regs;

// Disable I2C peripheral
regs->CR1 = 0;

// Reset I2C peripheral
*def->reset_reg |= def->reset_bit;
*def->reset_reg &= ~def->reset_bit;
}

static bool i2c_bus_init(i2c_bus_t* bus, int bus_index) {
memset(bus, 0, sizeof(i2c_bus_t));

switch (bus_index) {
case 0:
Expand All @@ -272,9 +290,13 @@ static bool i2c_bus_init(i2c_bus_t* bus, int bus_index) {
break;
#endif
default:
return false;
goto cleanup;
}

const i2c_bus_def_t* def = &g_i2c_bus_def[bus_index];

bus->def = def;

// Unlocks potentialy locked I2C bus by
// generating 9 clock pulses on SCL while SDA is low
i2c_bus_unlock(bus);
Expand Down Expand Up @@ -305,28 +327,14 @@ static bool i2c_bus_init(i2c_bus_t* bus, int bus_index) {

bus->timer = systimer_create(i2c_bus_timer_callback, bus);
if (bus->timer == NULL) {
return false;
goto cleanup;
}

return true;
}

static void i2c_bus_deinit(i2c_bus_t* bus) {
const i2c_bus_def_t* def = bus->def;

systimer_delete(bus->timer);

NVIC_DisableIRQ(def->ev_irq);
NVIC_DisableIRQ(def->er_irq);

I2C_TypeDef* regs = def->regs;

// Disable I2C peripheral
regs->CR1 = 0;

// Reset I2C peripheral
*def->reset_reg |= def->reset_bit;
*def->reset_reg &= ~def->reset_bit;
cleanup:
i2c_bus_deinit(bus);
return false;
}

i2c_bus_t* i2c_bus_open(uint8_t bus_index) {
Expand All @@ -338,7 +346,6 @@ i2c_bus_t* i2c_bus_open(uint8_t bus_index) {

if (bus->refcount == 0) {
if (!i2c_bus_init(bus, bus_index)) {
i2c_bus_deinit(bus);
return NULL;
}
}
Expand Down
55 changes: 31 additions & 24 deletions core/embed/trezorhal/stm32u5/i2c_bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,30 @@ static void i2c_bus_unlock(i2c_bus_t* bus) {
}
}

static bool i2c_bus_init(i2c_bus_t* bus, int bus_index) {
memset(bus, 0, sizeof(i2c_bus_t));
static void i2c_bus_deinit(i2c_bus_t* bus) {
const i2c_bus_def_t* def = bus->def;

const i2c_bus_def_t* def = &g_i2c_bus_def[bus_index];
systimer_delete(bus->timer);

bus->def = def;
if (bus->def == NULL) {
return;
}

NVIC_DisableIRQ(def->ev_irq);
NVIC_DisableIRQ(def->er_irq);

I2C_TypeDef* regs = def->regs;

// Disable I2C peripheral
regs->CR1 = 0;

// Reset I2C peripheral
*def->reset_reg |= def->reset_bit;
*def->reset_reg &= ~def->reset_bit;
}

static bool i2c_bus_init(i2c_bus_t* bus, int bus_index) {
memset(bus, 0, sizeof(i2c_bus_t));

switch (bus_index) {
case 0:
Expand All @@ -256,9 +274,13 @@ static bool i2c_bus_init(i2c_bus_t* bus, int bus_index) {
break;
#endif
default:
return false;
goto cleanup;
}

const i2c_bus_def_t* def = &g_i2c_bus_def[bus_index];

bus->def = def;

// Unlocks potentialy locked I2C bus by
// generating 9 clock pulses on SCL while SDA is low
i2c_bus_unlock(bus);
Expand Down Expand Up @@ -305,28 +327,14 @@ static bool i2c_bus_init(i2c_bus_t* bus, int bus_index) {

bus->timer = systimer_create(i2c_bus_timer_callback, bus);
if (bus->timer == NULL) {
return false;
goto cleanup;
}

return true;
}

static void i2c_bus_deinit(i2c_bus_t* bus) {
const i2c_bus_def_t* def = bus->def;

systimer_delete(bus->timer);

NVIC_DisableIRQ(def->ev_irq);
NVIC_DisableIRQ(def->er_irq);

I2C_TypeDef* regs = def->regs;

// Disable I2C peripheral
regs->CR1 = 0;

// Reset I2C peripheral
*def->reset_reg |= def->reset_bit;
*def->reset_reg &= ~def->reset_bit;
cleanup:
i2c_bus_deinit(bus);
return false;
}

i2c_bus_t* i2c_bus_open(uint8_t bus_index) {
Expand All @@ -338,7 +346,6 @@ i2c_bus_t* i2c_bus_open(uint8_t bus_index) {

if (bus->refcount == 0) {
if (!i2c_bus_init(bus, bus_index)) {
i2c_bus_deinit(bus);
return NULL;
}
}
Expand Down

0 comments on commit 98995cb

Please sign in to comment.