Skip to content

Commit

Permalink
Add separate flags for I2C slave transfer in progress
Browse files Browse the repository at this point in the history
Fixes ARMmbed#15498

Adds 2 boolean flags to the STM32 `i2c_s` object
to indicate whether a transfer is in progress,
separate from the existing "transfer pending" flags.

`i2c_slave_write`, `i2c_slave_read` and their associated callbacks
are modified to use these flags in addition to the pending flags.
The original behavior of the pending flags is preserved.
  • Loading branch information
agausmann authored and multiplemonomials committed Jul 20, 2024
1 parent 8879390 commit ec21609
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions targets/TARGET_STM/TARGET_STM32F1/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ struct i2c_s {
uint8_t slave;
volatile uint8_t pending_slave_tx_master_rx;
volatile uint8_t pending_slave_rx_maxter_tx;
volatile uint8_t slave_tx_transfer_in_progress;
volatile uint8_t slave_rx_transfer_in_progress;
uint8_t *slave_rx_buffer;
volatile uint16_t slave_rx_buffer_size;
volatile uint16_t slave_rx_count;
Expand Down
2 changes: 2 additions & 0 deletions targets/TARGET_STM/TARGET_STM32F2/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ struct i2c_s {
uint8_t slave;
volatile uint8_t pending_slave_tx_master_rx;
volatile uint8_t pending_slave_rx_maxter_tx;
volatile uint8_t slave_tx_transfer_in_progress;
volatile uint8_t slave_rx_transfer_in_progress;
uint8_t *slave_rx_buffer;
volatile uint16_t slave_rx_buffer_size;
volatile uint16_t slave_rx_count;
Expand Down
2 changes: 2 additions & 0 deletions targets/TARGET_STM/TARGET_STM32F4/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ struct i2c_s {
uint8_t slave;
volatile uint8_t pending_slave_tx_master_rx;
volatile uint8_t pending_slave_rx_maxter_tx;
volatile uint8_t slave_tx_transfer_in_progress;
volatile uint8_t slave_rx_transfer_in_progress;
uint8_t *slave_rx_buffer;
volatile uint16_t slave_rx_buffer_size;
volatile uint16_t slave_rx_count;
Expand Down
2 changes: 2 additions & 0 deletions targets/TARGET_STM/TARGET_STM32L1/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ struct i2c_s {
uint8_t slave;
volatile uint8_t pending_slave_tx_master_rx;
volatile uint8_t pending_slave_rx_maxter_tx;
volatile uint8_t slave_tx_transfer_in_progress;
volatile uint8_t slave_rx_transfer_in_progress;
uint8_t *slave_rx_buffer;
volatile uint16_t slave_rx_buffer_size;
volatile uint16_t slave_rx_count;
Expand Down
11 changes: 9 additions & 2 deletions targets/TARGET_STM/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap)
obj_s->slave = 0;
obj_s->pending_slave_tx_master_rx = 0;
obj_s->pending_slave_rx_maxter_tx = 0;
obj_s->slave_tx_transfer_in_progress = 0;
obj_s->slave_rx_transfer_in_progress = 0;
#endif

obj_s->event = 0;
Expand Down Expand Up @@ -1618,6 +1620,7 @@ void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *I2cHandle)
i2c_t *obj = get_i2c_obj(I2cHandle);
struct i2c_s *obj_s = I2C_S(obj);
obj_s->pending_slave_tx_master_rx = 0;
obj_s->slave_tx_transfer_in_progress = 0;
}

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2cHandle)
Expand All @@ -1632,9 +1635,11 @@ void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2cHandle)
HAL_I2C_Slave_Seq_Receive_IT(I2cHandle, &(obj_s->slave_rx_buffer[obj_s->slave_rx_count]), 1, I2C_NEXT_FRAME);
} else {
obj_s->pending_slave_rx_maxter_tx = 0;
obj_s->slave_rx_transfer_in_progress = 0;
}
} else {
obj_s->pending_slave_rx_maxter_tx = 0;
obj_s->slave_rx_transfer_in_progress = 0;
}
}

Expand Down Expand Up @@ -1688,12 +1693,13 @@ int i2c_slave_read(i2c_t *obj, char *data, int length)
_length = length;
}

obj_s->slave_rx_transfer_in_progress = 1;
/* Always use I2C_NEXT_FRAME as slave will just adapt to master requests */
ret = HAL_I2C_Slave_Seq_Receive_IT(handle, (uint8_t *) data, _length, I2C_NEXT_FRAME);

if (ret == HAL_OK) {
timeout = BYTE_TIMEOUT_US * (_length + 1);
while (obj_s->pending_slave_rx_maxter_tx && (--timeout != 0)) {
while (obj_s->slave_rx_transfer_in_progress && (--timeout != 0)) {
wait_us(1);
}

Expand All @@ -1718,12 +1724,13 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length)
int ret = 0;
uint32_t timeout = 0;

obj_s->slave_tx_transfer_in_progress = 1;
/* Always use I2C_NEXT_FRAME as slave will just adapt to master requests */
ret = HAL_I2C_Slave_Seq_Transmit_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME);

if (ret == HAL_OK) {
timeout = BYTE_TIMEOUT_US * (length + 1);
while (obj_s->pending_slave_tx_master_rx && (--timeout != 0)) {
while (obj_s->slave_tx_transfer_in_progress && (--timeout != 0)) {
wait_us(1);
}

Expand Down
2 changes: 2 additions & 0 deletions targets/TARGET_STM/stm_i2c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ struct i2c_s {
uint8_t slave;
volatile uint8_t pending_slave_tx_master_rx;
volatile uint8_t pending_slave_rx_maxter_tx;
volatile uint8_t slave_tx_transfer_in_progress;
volatile uint8_t slave_rx_transfer_in_progress;
uint8_t *slave_rx_buffer;
volatile uint16_t slave_rx_buffer_size;
volatile uint16_t slave_rx_count;
Expand Down

0 comments on commit ec21609

Please sign in to comment.