Skip to content

Commit 2280453

Browse files
jmchiappafpistm
authored andcommitted
Added the repeated-start feature in I2C driver (#590)
1 parent eb90a45 commit 2280453

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Diff for: cores/arduino/stm32/twi.c

+16
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,16 @@ i2c_status_e i2c_master_write(i2c_t *obj, uint8_t dev_address,
740740
return i2c_IsDeviceReady(obj, dev_address, 1);
741741
}
742742

743+
#if defined(I2C_OTHER_FRAME)
744+
uint32_t XferOptions = obj->handle.XferOptions; // save XferOptions value, because handle can be modified by HAL, which cause issue in case of NACK from slave
745+
#endif
746+
743747
do {
748+
#if defined(I2C_OTHER_FRAME)
749+
if (HAL_I2C_Master_Seq_Transmit_IT(&(obj->handle), dev_address, data, size, XferOptions) == HAL_OK) {
750+
#else
744751
if (HAL_I2C_Master_Transmit_IT(&(obj->handle), dev_address, data, size) == HAL_OK) {
752+
#endif
745753
ret = I2C_OK;
746754
// wait for transfer completion
747755
while ((HAL_I2C_GetState(&(obj->handle)) != HAL_I2C_STATE_READY)
@@ -803,8 +811,16 @@ i2c_status_e i2c_master_read(i2c_t *obj, uint8_t dev_address, uint8_t *data, uin
803811
uint32_t tickstart = HAL_GetTick();
804812
uint32_t delta = 0;
805813

814+
#if defined(I2C_OTHER_FRAME)
815+
uint32_t XferOptions = obj->handle.XferOptions; // save XferOptions value, because handle can be modified by HAL, which cause issue in case of NACK from slave
816+
#endif
817+
806818
do {
819+
#if defined(I2C_OTHER_FRAME)
820+
if (HAL_I2C_Master_Seq_Receive_IT(&(obj->handle), dev_address, data, size, XferOptions) == HAL_OK) {
821+
#else
807822
if (HAL_I2C_Master_Receive_IT(&(obj->handle), dev_address, data, size) == HAL_OK) {
823+
#endif
808824
ret = I2C_OK;
809825
// wait for transfer completion
810826
while ((HAL_I2C_GetState(&(obj->handle)) != HAL_I2C_STATE_READY)

Diff for: libraries/Wire/src/Wire.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ void TwoWire::setClock(uint32_t frequency)
116116

117117
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
118118
{
119+
#if !defined(I2C_OTHER_FRAME)
119120
UNUSED(sendStop);
121+
#endif
122+
120123
if (_i2c.isMaster == 1) {
121124
allocateRxBuffer(quantity);
122125
// error if no memory block available to allocate the buffer
@@ -146,6 +149,15 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddres
146149

147150
// perform blocking read into buffer
148151
uint8_t read = 0;
152+
153+
#if defined(I2C_OTHER_FRAME)
154+
if (sendStop == 0) {
155+
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
156+
} else {
157+
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
158+
}
159+
#endif
160+
149161
if (I2C_OK == i2c_master_read(&_i2c, address << 1, rxBuffer, quantity)) {
150162
read = quantity;
151163
}
@@ -211,8 +223,18 @@ void TwoWire::beginTransmission(int address)
211223
//
212224
uint8_t TwoWire::endTransmission(uint8_t sendStop)
213225
{
226+
#if !defined(I2C_OTHER_FRAME)
214227
UNUSED(sendStop);
228+
#endif
215229
int8_t ret = 4;
230+
// check transfer options and store it in the I2C handle
231+
#if defined(I2C_OTHER_FRAME)
232+
if (sendStop == 0) {
233+
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
234+
} else {
235+
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
236+
}
237+
#endif
216238

217239
if (_i2c.isMaster == 1) {
218240
// transmit buffer (blocking)

0 commit comments

Comments
 (0)