Skip to content

Commit 5750f31

Browse files
authored
Merge pull request #3238 from LMESTM/dev_i2c_stm32f4hal
Dev i2c stm32f4hal
2 parents b36944f + e293e07 commit 5750f31

File tree

4 files changed

+369
-281
lines changed

4 files changed

+369
-281
lines changed

features/unsupported/tests/mbed/i2c_master_slave_asynch/main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ I2CSlave slave(D3, D6);
4545
volatile int why;
4646
volatile bool master_complete = false;
4747
void cbmaster_done(int event) {
48-
printf("cbmaster_done\n");
4948
master_complete = true;
5049
why = event;
5150
}
@@ -70,7 +69,8 @@ int main()
7069

7170
// First transfer: master to slave
7271
printf("\nFirst transfer: Master Tx, Repeated Start\n");
73-
master.transfer(ADDR, buf_master, SIZE, 0, 0, callback, I2C_EVENT_ALL, true);
72+
if(master.transfer(ADDR, buf_master, SIZE, 0, 0, callback, I2C_EVENT_ALL, true) != 0)
73+
notify_completion(false);
7474

7575
while (!master_complete) {
7676
if(slave.receive() == I2CSlave::WriteAddressed) {
@@ -90,7 +90,8 @@ int main()
9090

9191
// Second transfer: slave to master
9292
printf("\nSecond transfer: Master Rx\n");
93-
master.transfer(ADDR, 0, 0, res_master, SIZE, callback, I2C_EVENT_ALL, true);
93+
if(master.transfer(ADDR, 0, 0, res_master, SIZE, callback, I2C_EVENT_ALL, true) != 0)
94+
notify_completion(false);
9495

9596
while (!master_complete) {
9697
if(slave.receive() == I2CSlave::ReadAddressed) {
@@ -118,7 +119,8 @@ int main()
118119

119120
// Third transfer: Tx/Rx
120121
printf("\nThird transfer: Master Tx/Rx\n");
121-
master.transfer(ADDR, buf_master_tx, SIZE, buf_master_rx, SIZE, callback, I2C_EVENT_ALL, false);
122+
if(master.transfer(ADDR, buf_master_tx, SIZE, buf_master_rx, SIZE, callback, I2C_EVENT_ALL, false) != 0)
123+
notify_completion(false);
122124

123125
while (!master_complete) {
124126

@@ -130,6 +132,7 @@ int main()
130132
buf_slave_txrx[i]++;
131133
}
132134
}
135+
133136
if((i == I2CSlave::ReadAddressed) ) {
134137
slave.write(buf_slave_txrx, SIZE);
135138
}

targets/TARGET_STM/TARGET_STM32F4/common_objects.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,29 @@ struct spi_s {
8484
};
8585

8686
struct i2c_s {
87+
/* The 1st 2 members I2CName i2c
88+
* and I2C_HandleTypeDef handle should
89+
* be kept as the first members of this struct
90+
* to have get_i2c_obj() function work as expected
91+
*/
8792
I2CName i2c;
8893
I2C_HandleTypeDef handle;
94+
uint8_t index;
95+
PinName sda;
96+
PinName scl;
8997
IRQn_Type event_i2cIRQ;
9098
IRQn_Type error_i2cIRQ;
99+
uint8_t XferOperation;
100+
volatile uint8_t event;
101+
#if DEVICE_I2CSLAVE
91102
uint8_t slave;
103+
volatile uint8_t pending_slave_tx_master_rx;
104+
volatile uint8_t pending_slave_rx_maxter_tx;
105+
#endif
92106
#if DEVICE_I2C_ASYNCH
93107
uint32_t address;
94-
uint8_t event;
95108
uint8_t stop;
96109
uint8_t available_events;
97-
uint8_t XferOperation;
98110
#endif
99111
};
100112

targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_i2c.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3960,7 +3960,8 @@ static HAL_StatusTypeDef I2C_MasterReceive_BTF(I2C_HandleTypeDef *hi2c)
39603960
__HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_ERR);
39613961

39623962
hi2c->State = HAL_I2C_STATE_READY;
3963-
3963+
hi2c->PreviousState = I2C_STATE_NONE;
3964+
39643965
if(hi2c->Mode == HAL_I2C_MODE_MEM)
39653966
{
39663967
hi2c->Mode = HAL_I2C_MODE_NONE;

0 commit comments

Comments
 (0)