Skip to content

Commit b74f62c

Browse files
authored
Merge pull request ARMmbed#14659 from arduino/i2c_slave_patch
STM32: make i2c_salve_read return the number of bytes read
2 parents 7620120 + cd35063 commit b74f62c

File tree

16 files changed

+88
-6
lines changed

16 files changed

+88
-6
lines changed

Diff for: targets/TARGET_STM/TARGET_STM32F0/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ struct i2c_s {
9898
uint8_t slave;
9999
volatile uint8_t pending_slave_tx_master_rx;
100100
volatile uint8_t pending_slave_rx_maxter_tx;
101+
uint8_t *slave_rx_buffer;
102+
volatile uint8_t slave_rx_buffer_size;
103+
volatile uint8_t slave_rx_count;
101104
#endif
102105
#if DEVICE_I2C_ASYNCH
103106
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32F1/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ struct i2c_s {
123123
uint8_t slave;
124124
volatile uint8_t pending_slave_tx_master_rx;
125125
volatile uint8_t pending_slave_rx_maxter_tx;
126+
uint8_t *slave_rx_buffer;
127+
volatile uint8_t slave_rx_buffer_size;
128+
volatile uint8_t slave_rx_count;
126129
#endif
127130
#if DEVICE_I2C_ASYNCH
128131
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32F2/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ struct i2c_s {
128128
uint8_t slave;
129129
volatile uint8_t pending_slave_tx_master_rx;
130130
volatile uint8_t pending_slave_rx_maxter_tx;
131+
uint8_t *slave_rx_buffer;
132+
volatile uint8_t slave_rx_buffer_size;
133+
volatile uint8_t slave_rx_count;
131134
#endif
132135
#if DEVICE_I2C_ASYNCH
133136
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32F3/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ struct i2c_s {
113113
uint8_t slave;
114114
volatile uint8_t pending_slave_tx_master_rx;
115115
volatile uint8_t pending_slave_rx_maxter_tx;
116+
uint8_t *slave_rx_buffer;
117+
volatile uint8_t slave_rx_buffer_size;
118+
volatile uint8_t slave_rx_count;
116119
#endif
117120
#if DEVICE_I2C_ASYNCH
118121
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32F4/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ struct i2c_s {
112112
uint8_t slave;
113113
volatile uint8_t pending_slave_tx_master_rx;
114114
volatile uint8_t pending_slave_rx_maxter_tx;
115+
uint8_t *slave_rx_buffer;
116+
volatile uint8_t slave_rx_buffer_size;
117+
volatile uint8_t slave_rx_count;
115118
#endif
116119
#if DEVICE_I2C_ASYNCH
117120
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32F7/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ struct i2c_s {
129129
uint8_t slave;
130130
volatile uint8_t pending_slave_tx_master_rx;
131131
volatile uint8_t pending_slave_rx_maxter_tx;
132+
uint8_t *slave_rx_buffer;
133+
volatile uint8_t slave_rx_buffer_size;
134+
volatile uint8_t slave_rx_count;
132135
#endif
133136
#if DEVICE_I2C_ASYNCH
134137
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32G0/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ struct i2c_s {
112112
uint8_t slave;
113113
volatile uint8_t pending_slave_tx_master_rx;
114114
volatile uint8_t pending_slave_rx_maxter_tx;
115+
uint8_t *slave_rx_buffer;
116+
volatile uint8_t slave_rx_buffer_size;
117+
volatile uint8_t slave_rx_count;
115118
#endif
116119
#if DEVICE_I2C_ASYNCH
117120
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32G4/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ struct i2c_s {
111111
uint8_t slave;
112112
volatile uint8_t pending_slave_tx_master_rx;
113113
volatile uint8_t pending_slave_rx_maxter_tx;
114+
uint8_t *slave_rx_buffer;
115+
volatile uint8_t slave_rx_buffer_size;
116+
volatile uint8_t slave_rx_count;
114117
#endif
115118
#if DEVICE_I2C_ASYNCH
116119
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32H7/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ struct i2c_s {
118118
uint8_t slave;
119119
volatile uint8_t pending_slave_tx_master_rx;
120120
volatile uint8_t pending_slave_rx_maxter_tx;
121+
uint8_t *slave_rx_buffer;
122+
volatile uint8_t slave_rx_buffer_size;
123+
volatile uint8_t slave_rx_count;
121124
#endif
122125
#if DEVICE_I2C_ASYNCH
123126
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32L0/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ struct i2c_s {
114114
uint8_t slave;
115115
volatile uint8_t pending_slave_tx_master_rx;
116116
volatile uint8_t pending_slave_rx_maxter_tx;
117+
uint8_t *slave_rx_buffer;
118+
volatile uint8_t slave_rx_buffer_size;
119+
volatile uint8_t slave_rx_count;
117120
#endif
118121
#if DEVICE_I2C_ASYNCH
119122
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32L1/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ struct i2c_s {
110110
uint8_t slave;
111111
volatile uint8_t pending_slave_tx_master_rx;
112112
volatile uint8_t pending_slave_rx_maxter_tx;
113+
uint8_t *slave_rx_buffer;
114+
volatile uint8_t slave_rx_buffer_size;
115+
volatile uint8_t slave_rx_count;
113116
#endif
114117
#if DEVICE_I2C_ASYNCH
115118
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32L4/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ struct i2c_s {
110110
uint8_t slave;
111111
volatile uint8_t pending_slave_tx_master_rx;
112112
volatile uint8_t pending_slave_rx_maxter_tx;
113+
uint8_t *slave_rx_buffer;
114+
volatile uint8_t slave_rx_buffer_size;
115+
volatile uint8_t slave_rx_count;
113116
#endif
114117
#if DEVICE_I2C_ASYNCH
115118
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32L5/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ struct i2c_s {
118118
uint8_t slave;
119119
volatile uint8_t pending_slave_tx_master_rx;
120120
volatile uint8_t pending_slave_rx_maxter_tx;
121+
uint8_t *slave_rx_buffer;
122+
volatile uint8_t slave_rx_buffer_size;
123+
volatile uint8_t slave_rx_count;
121124
#endif
122125
#if DEVICE_I2C_ASYNCH
123126
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32WB/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ struct i2c_s {
101101
uint8_t slave;
102102
volatile uint8_t pending_slave_tx_master_rx;
103103
volatile uint8_t pending_slave_rx_maxter_tx;
104+
uint8_t *slave_rx_buffer;
105+
volatile uint8_t slave_rx_buffer_size;
106+
volatile uint8_t slave_rx_count;
104107
#endif
105108
#if DEVICE_I2C_ASYNCH
106109
uint32_t address;

Diff for: targets/TARGET_STM/TARGET_STM32WL/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ struct i2c_s {
104104
uint8_t slave;
105105
volatile uint8_t pending_slave_tx_master_rx;
106106
volatile uint8_t pending_slave_rx_maxter_tx;
107+
uint8_t *slave_rx_buffer;
108+
volatile uint8_t slave_rx_buffer_size;
109+
volatile uint8_t slave_rx_count;
107110
#endif
108111
#if DEVICE_I2C_ASYNCH
109112
uint32_t address;

Diff for: targets/TARGET_STM/i2c_api.c

+43-6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ static I2C_HandleTypeDef *i2c_handles[I2C_NUM];
199199
#define I2C_STATE_NONE ((uint32_t)(HAL_I2C_MODE_NONE))
200200
#endif
201201

202+
#define SLAVE_MODE_RECEIVE 1
203+
#define SLAVE_MODE_LISTEN 2
204+
#define DEFAULT_SLAVE_MODE SLAVE_MODE_LISTEN
205+
202206
/* Declare i2c_init_internal to be used in this file */
203207
void i2c_init_internal(i2c_t *obj, const i2c_pinmap_t *pinmap);
204208

@@ -1199,7 +1203,7 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
11991203
#if DEVICE_I2CSLAVE
12001204
/* restore slave address */
12011205
if (address != 0) {
1202-
obj_s->slave = 1;
1206+
obj_s->slave = DEFAULT_SLAVE_MODE;
12031207
i2c_slave_address(obj, 0, address, 0);
12041208
}
12051209
#endif
@@ -1251,7 +1255,7 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave)
12511255
I2C_HandleTypeDef *handle = &(obj_s->handle);
12521256

12531257
if (enable_slave) {
1254-
obj_s->slave = 1;
1258+
obj_s->slave = DEFAULT_SLAVE_MODE;
12551259
HAL_I2C_EnableListen_IT(handle);
12561260
} else {
12571261
obj_s->slave = 0;
@@ -1295,11 +1299,28 @@ void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2cHandle)
12951299
/* Get object ptr based on handler ptr */
12961300
i2c_t *obj = get_i2c_obj(I2cHandle);
12971301
struct i2c_s *obj_s = I2C_S(obj);
1298-
obj_s->pending_slave_rx_maxter_tx = 0;
1302+
1303+
if (obj_s->slave == SLAVE_MODE_LISTEN) {
1304+
obj_s->slave_rx_count++;
1305+
if (obj_s->slave_rx_count < obj_s->slave_rx_buffer_size){
1306+
HAL_I2C_Slave_Seq_Receive_IT(I2cHandle, &(obj_s->slave_rx_buffer[obj_s->slave_rx_count]), 1, I2C_NEXT_FRAME);
1307+
} else {
1308+
obj_s->pending_slave_rx_maxter_tx = 0;
1309+
}
1310+
} else {
1311+
obj_s->pending_slave_rx_maxter_tx = 0;
1312+
}
12991313
}
13001314

13011315
void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
13021316
{
1317+
i2c_t *obj = get_i2c_obj(hi2c);
1318+
struct i2c_s *obj_s = I2C_S(obj);
1319+
1320+
if (obj_s->slave == SLAVE_MODE_LISTEN) {
1321+
obj_s->pending_slave_rx_maxter_tx = 0;
1322+
}
1323+
13031324
/* restart listening for master requests */
13041325
HAL_I2C_EnableListen_IT(hi2c);
13051326
}
@@ -1328,18 +1349,34 @@ int i2c_slave_read(i2c_t *obj, char *data, int length)
13281349
int count = 0;
13291350
int ret = 0;
13301351
uint32_t timeout = 0;
1352+
int _length = 0;
1353+
1354+
if (obj_s->slave == SLAVE_MODE_LISTEN) {
1355+
/* We don't know in advance how many bytes will be sent by master so
1356+
* we'll fetch one by one until master ends the sequence */
1357+
_length = 1;
1358+
obj_s->slave_rx_buffer_size = length;
1359+
obj_s->slave_rx_count = 0;
1360+
obj_s->slave_rx_buffer = (uint8_t*)data;
1361+
} else {
1362+
_length = length;
1363+
}
13311364

13321365
/* Always use I2C_NEXT_FRAME as slave will just adapt to master requests */
1333-
ret = HAL_I2C_Slave_Seq_Receive_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME);
1366+
ret = HAL_I2C_Slave_Seq_Receive_IT(handle, (uint8_t *) data, _length, I2C_NEXT_FRAME);
13341367

13351368
if (ret == HAL_OK) {
1336-
timeout = BYTE_TIMEOUT_US * (length + 1);
1369+
timeout = BYTE_TIMEOUT_US * (_length + 1);
13371370
while (obj_s->pending_slave_rx_maxter_tx && (--timeout != 0)) {
13381371
wait_us(1);
13391372
}
13401373

13411374
if (timeout != 0) {
1342-
count = length;
1375+
if (obj_s->slave == SLAVE_MODE_LISTEN) {
1376+
count = obj_s->slave_rx_count;
1377+
} else {
1378+
count = _length;
1379+
}
13431380
} else {
13441381
DEBUG_PRINTF("TIMEOUT or error in i2c_slave_read\r\n");
13451382
}

0 commit comments

Comments
 (0)