Skip to content

Commit

Permalink
Merge pull request #15499 from agausmann/i2c-slave-flags
Browse files Browse the repository at this point in the history
STM: Add separate flags for I2C slave transfer in progress
  • Loading branch information
0xc0170 authored May 3, 2024
2 parents 95fee2f + dc49c2b commit 1060154
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 @@ -123,6 +123,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 @@ -128,6 +128,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 @@ -112,6 +112,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 @@ -110,6 +110,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 @@ -535,6 +535,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 @@ -1573,6 +1575,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 @@ -1587,9 +1590,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 @@ -1643,12 +1648,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 @@ -1673,12 +1679,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 1060154

Please sign in to comment.