Skip to content

Commit 2b53dfc

Browse files
committed
STM32: I2C: configure slave address with HAL API
Instead of direct registers access, let's use HAL API. This makes the code more generic accross STM32 families.
1 parent fa88776 commit 2b53dfc

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

targets/TARGET_STM/TARGET_STM32F4/i2c_api.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -404,19 +404,13 @@ void i2c_reset(i2c_t *obj) {
404404
#if DEVICE_I2CSLAVE
405405

406406
void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
407-
408-
uint16_t tmpreg = 0;
409407
struct i2c_s *obj_s = I2C_S(obj);
408+
I2C_HandleTypeDef *handle = &(obj_s->handle);
410409
I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c;
411410

412-
// Get the old register value
413-
tmpreg = i2c->OAR1;
414-
// Reset address bits
415-
tmpreg &= 0xFC00;
416-
// Set new address
417-
tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
418-
// Store the new register value
419-
i2c->OAR1 = tmpreg;
411+
// I2C configuration
412+
handle->Init.OwnAddress1 = address;
413+
HAL_I2C_Init(handle);
420414
}
421415

422416
void i2c_slave_mode(i2c_t *obj, int enable_slave) {
@@ -429,6 +423,8 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
429423

430424
/* Enable Address Acknowledge */
431425
i2c->CR1 |= I2C_CR1_ACK;
426+
} else {
427+
obj_s->slave = 0;
432428
}
433429
}
434430

0 commit comments

Comments
 (0)