Skip to content

Commit 104f928

Browse files
committed
Merge branch 'I2C_SEQUENTIAL_COMMUNICATION_REWORK' of git://github.com/ABOSTM/mbed-os into dev_rollup
2 parents 31b65ad + de121a3 commit 104f928

File tree

6 files changed

+73
-25
lines changed

6 files changed

+73
-25
lines changed

targets/TARGET_STM/TARGET_STM32F0/device/stm32f0xx_hal_i2c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c,
25922592
/* Prepare transfer parameters */
25932593
hi2c->pBuffPtr = pData;
25942594
hi2c->XferCount = Size;
2595-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE));
2595+
hi2c->XferOptions = XferOptions;
25962596
hi2c->XferISR = I2C_Master_ISR_IT;
25972597

25982598
/* If size > MAX_NBYTE_SIZE, use reload mode */
@@ -2658,7 +2658,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c,
26582658
/* Prepare transfer parameters */
26592659
hi2c->pBuffPtr = pData;
26602660
hi2c->XferCount = Size;
2661-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE));
2661+
hi2c->XferOptions = XferOptions;
26622662
hi2c->XferISR = I2C_Master_ISR_IT;
26632663

26642664
/* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */

targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_i2c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c,
25922592
/* Prepare transfer parameters */
25932593
hi2c->pBuffPtr = pData;
25942594
hi2c->XferCount = Size;
2595-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE)); // MBED patch
2595+
hi2c->XferOptions = XferOptions;
25962596
hi2c->XferISR = I2C_Master_ISR_IT;
25972597

25982598
/* If size > MAX_NBYTE_SIZE, use reload mode */
@@ -2666,7 +2666,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c,
26662666
/* Prepare transfer parameters */
26672667
hi2c->pBuffPtr = pData;
26682668
hi2c->XferCount = Size;
2669-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE)); // MBED patch
2669+
hi2c->XferOptions = XferOptions;
26702670
hi2c->XferISR = I2C_Master_ISR_IT;
26712671

26722672
/* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */

targets/TARGET_STM/TARGET_STM32F7/device/stm32f7xx_hal_i2c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,7 +2598,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c,
25982598
/* Prepare transfer parameters */
25992599
hi2c->pBuffPtr = pData;
26002600
hi2c->XferCount = Size;
2601-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE)); // MBED: changed
2601+
hi2c->XferOptions = XferOptions;
26022602
hi2c->XferISR = I2C_Master_ISR_IT;
26032603

26042604
/* If size > MAX_NBYTE_SIZE, use reload mode */
@@ -2672,7 +2672,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c,
26722672
/* Prepare transfer parameters */
26732673
hi2c->pBuffPtr = pData;
26742674
hi2c->XferCount = Size;
2675-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE)); // MBED: changed
2675+
hi2c->XferOptions = XferOptions;
26762676
hi2c->XferISR = I2C_Master_ISR_IT;
26772677

26782678
/* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */

targets/TARGET_STM/TARGET_STM32L0/device/stm32l0xx_hal_i2c.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c,
25922592
/* Prepare transfer parameters */
25932593
hi2c->pBuffPtr = pData;
25942594
hi2c->XferCount = Size;
2595-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE)); // MBED commit 23926a2418
2595+
hi2c->XferOptions = XferOptions;
25962596
hi2c->XferISR = I2C_Master_ISR_IT;
25972597

25982598
/* If size > MAX_NBYTE_SIZE, use reload mode */
@@ -2666,7 +2666,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c,
26662666
/* Prepare transfer parameters */
26672667
hi2c->pBuffPtr = pData;
26682668
hi2c->XferCount = Size;
2669-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE)); // MBED commit 23926a2418
2669+
hi2c->XferOptions = XferOptions;
26702670
hi2c->XferISR = I2C_Master_ISR_IT;
26712671

26722672
/* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */

targets/TARGET_STM/TARGET_STM32L4/device/stm32l4xx_hal_i2c.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,8 +3173,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16
31733173
/* Prepare transfer parameters */
31743174
hi2c->pBuffPtr = pData;
31753175
hi2c->XferCount = Size;
3176-
// Added for MBED PR #3324
3177-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE));
3176+
hi2c->XferOptions = XferOptions;
31783177
hi2c->XferISR = I2C_Master_ISR_IT;
31793178

31803179
/* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */
@@ -3420,8 +3419,7 @@ HAL_StatusTypeDef HAL_I2C_Master_Seq_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_
34203419
/* Prepare transfer parameters */
34213420
hi2c->pBuffPtr = pData;
34223421
hi2c->XferCount = Size;
3423-
// Added for MBED PR #3324
3424-
hi2c->XferOptions = (XferOptions & (~I2C_RELOAD_MODE));
3422+
hi2c->XferOptions = XferOptions;
34253423
hi2c->XferISR = I2C_Master_ISR_IT;
34263424

34273425
/* If hi2c->XferCount > MAX_NBYTE_SIZE, use reload mode */

targets/TARGET_STM/i2c_api.c

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "pinmap.h"
4141
#include "PeripheralPins.h"
4242
#include "i2c_device.h" // family specific defines
43+
#include "mbed_error.h"
4344

4445
#ifndef DEBUG_STDIO
4546
# define DEBUG_STDIO 0
@@ -597,8 +598,6 @@ int i2c_stop(i2c_t *obj)
597598
return 0;
598599
}
599600
#endif
600-
// Disable reload mode
601-
handle->Instance->CR2 &= (uint32_t)~I2C_CR2_RELOAD;
602601

603602
// Ensure the transmission is started before sending a stop
604603
if ((handle->Instance->CR2 & (uint32_t)I2C_CR2_RD_WRN) == 0) {
@@ -611,7 +610,7 @@ int i2c_stop(i2c_t *obj)
611610
}
612611

613612
// Generate the STOP condition
614-
handle->Instance->CR2 |= I2C_CR2_STOP;
613+
handle->Instance->CR2 = I2C_CR2_STOP;
615614

616615
timeout = FLAG_TIMEOUT;
617616
while (!__HAL_I2C_GET_FLAG(handle, I2C_FLAG_STOPF)) {
@@ -664,9 +663,16 @@ int i2c_byte_read(i2c_t *obj, int last)
664663
}
665664
}
666665

667-
/* Enable reload mode as we don't know how many bytes will be sent */
668-
/* and set transfer size to 1 */
669-
tmpreg |= I2C_CR2_RELOAD | (I2C_CR2_NBYTES & (1 << 16));
666+
if (last) {
667+
/* Disable Address Acknowledge */
668+
tmpreg = tmpreg & (~I2C_CR2_RELOAD);
669+
tmpreg |= I2C_CR2_NACK | (I2C_CR2_NBYTES & (1 << 16));
670+
} else {
671+
/* Enable reload mode as we don't know how many bytes will be sent */
672+
/* and set transfer size to 1 */
673+
tmpreg |= I2C_CR2_RELOAD | (I2C_CR2_NBYTES & (1 << 16));
674+
}
675+
670676
/* Set the prepared configuration */
671677
handle->Instance->CR2 = tmpreg;
672678

@@ -680,11 +686,6 @@ int i2c_byte_read(i2c_t *obj, int last)
680686
/* Then Get Byte */
681687
data = handle->Instance->RXDR;
682688

683-
if (last) {
684-
/* Disable Address Acknowledge */
685-
handle->Instance->CR2 |= I2C_CR2_NACK;
686-
}
687-
688689
return data;
689690
}
690691

@@ -760,7 +761,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
760761
I2C_HandleTypeDef *handle = &(obj_s->handle);
761762
int count = I2C_ERROR_BUS_BUSY, ret = 0;
762763
uint32_t timeout = 0;
763-
764+
#if defined(I2C_IP_VERSION_V1)
764765
// Trick to remove compiler warning "left and right operands are identical" in some cases
765766
uint32_t op1 = I2C_FIRST_AND_LAST_FRAME;
766767
uint32_t op2 = I2C_LAST_FRAME;
@@ -778,6 +779,18 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
778779
obj_s->XferOperation = I2C_NEXT_FRAME;
779780
}
780781
}
782+
#elif defined(I2C_IP_VERSION_V2)
783+
if ((obj_s->XferOperation == I2C_FIRST_FRAME) || (obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) || (obj_s->XferOperation == I2C_LAST_FRAME)) {
784+
if (stop) {
785+
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
786+
} else {
787+
obj_s->XferOperation = I2C_FIRST_FRAME;
788+
}
789+
} else {
790+
// should not happend
791+
error("I2C: abnormal case should not happend");
792+
}
793+
#endif
781794

782795
obj_s->event = 0;
783796

@@ -818,6 +831,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
818831
int count = I2C_ERROR_BUS_BUSY, ret = 0;
819832
uint32_t timeout = 0;
820833

834+
#if defined(I2C_IP_VERSION_V1)
821835
// Trick to remove compiler warning "left and right operands are identical" in some cases
822836
uint32_t op1 = I2C_FIRST_AND_LAST_FRAME;
823837
uint32_t op2 = I2C_LAST_FRAME;
@@ -835,6 +849,18 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
835849
obj_s->XferOperation = I2C_NEXT_FRAME;
836850
}
837851
}
852+
#elif defined(I2C_IP_VERSION_V2)
853+
if ((obj_s->XferOperation == I2C_FIRST_FRAME) || (obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) || (obj_s->XferOperation == I2C_LAST_FRAME)) {
854+
if (stop) {
855+
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
856+
} else {
857+
obj_s->XferOperation = I2C_FIRST_FRAME;
858+
}
859+
} else {
860+
// should not happend
861+
error("I2C: abnormal case should not happend");
862+
}
863+
#endif
838864

839865
obj_s->event = 0;
840866

@@ -874,11 +900,19 @@ void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
874900
#if DEVICE_I2C_ASYNCH
875901
/* Handle potential Tx/Rx use case */
876902
if ((obj->tx_buff.length) && (obj->rx_buff.length)) {
903+
#if defined(I2C_IP_VERSION_V1)
877904
if (obj_s->stop) {
878905
obj_s->XferOperation = I2C_LAST_FRAME;
879906
} else {
880907
obj_s->XferOperation = I2C_NEXT_FRAME;
881908
}
909+
#elif defined(I2C_IP_VERSION_V2)
910+
if (obj_s->stop) {
911+
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
912+
} else {
913+
obj_s->XferOperation = I2C_FIRST_FRAME;
914+
}
915+
#endif
882916

883917
HAL_I2C_Master_Sequential_Receive_IT(hi2c, obj_s->address, (uint8_t *)obj->rx_buff.buffer, obj->rx_buff.length, obj_s->XferOperation);
884918
} else
@@ -1143,6 +1177,7 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
11431177

11441178
/* Set operation step depending if stop sending required or not */
11451179
if ((tx_length && !rx_length) || (!tx_length && rx_length)) {
1180+
#if defined(I2C_IP_VERSION_V1)
11461181
// Trick to remove compiler warning "left and right operands are identical" in some cases
11471182
uint32_t op1 = I2C_FIRST_AND_LAST_FRAME;
11481183
uint32_t op2 = I2C_LAST_FRAME;
@@ -1160,7 +1195,18 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
11601195
obj_s->XferOperation = I2C_NEXT_FRAME;
11611196
}
11621197
}
1163-
1198+
#elif defined(I2C_IP_VERSION_V2)
1199+
if ((obj_s->XferOperation == I2C_FIRST_FRAME) || (obj_s->XferOperation == I2C_FIRST_AND_LAST_FRAME) || (obj_s->XferOperation == I2C_LAST_FRAME)) {
1200+
if (stop) {
1201+
obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME;
1202+
} else {
1203+
obj_s->XferOperation = I2C_FIRST_FRAME;
1204+
}
1205+
} else {
1206+
// should not happend
1207+
error("I2C: abnormal case should not happend");
1208+
}
1209+
#endif
11641210
if (tx_length > 0) {
11651211
HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *)tx, tx_length, obj_s->XferOperation);
11661212
}
@@ -1169,6 +1215,7 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
11691215
}
11701216
} else if (tx_length && rx_length) {
11711217
/* Two steps operation, don't modify XferOperation, keep it for next step */
1218+
#if defined(I2C_IP_VERSION_V1)
11721219
// Trick to remove compiler warning "left and right operands are identical" in some cases
11731220
uint32_t op1 = I2C_FIRST_AND_LAST_FRAME;
11741221
uint32_t op2 = I2C_LAST_FRAME;
@@ -1178,6 +1225,9 @@ void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx,
11781225
(obj_s->XferOperation == I2C_NEXT_FRAME)) {
11791226
HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *)tx, tx_length, I2C_NEXT_FRAME);
11801227
}
1228+
#elif defined(I2C_IP_VERSION_V2)
1229+
HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *)tx, tx_length, I2C_FIRST_FRAME);
1230+
#endif
11811231
}
11821232
}
11831233

0 commit comments

Comments
 (0)