Skip to content

Commit ea040a4

Browse files
committed
stmhal: In HAL I2C driver, move DMA setup to after sending I2C address.
Previous to this patch the DMA was setup and then the I2C address sent. If the I2C address sending failed (eg no I2C device on the bus) then the DMA was left in an inconsistent state. This patch moves the DMA setup to after a successful sending of the I2C address(es). See issue #1765.
1 parent 3cfb15c commit ea040a4

File tree

1 file changed

+64
-52
lines changed

1 file changed

+64
-52
lines changed

stmhal/hal/f4/src/stm32f4xx_hal_i2c.c

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,19 +1262,6 @@ HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t
12621262
hi2c->State = HAL_I2C_STATE_BUSY_TX;
12631263
hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
12641264

1265-
hi2c->pBuffPtr = pData;
1266-
hi2c->XferSize = Size;
1267-
hi2c->XferCount = Size;
1268-
1269-
/* Set the I2C DMA transfert complete callback */
1270-
hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
1271-
1272-
/* Set the DMA error callback */
1273-
hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1274-
1275-
/* Enable the DMA Stream */
1276-
HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
1277-
12781265
/* Send Slave Address */
12791266
if(I2C_MasterRequestWrite(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
12801267
{
@@ -1292,6 +1279,22 @@ HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t
12921279
}
12931280
}
12941281

1282+
/* dpgeorge: DMA initialisation is moved to after sending the address */
1283+
{
1284+
hi2c->pBuffPtr = pData;
1285+
hi2c->XferSize = Size;
1286+
hi2c->XferCount = Size;
1287+
1288+
/* Set the I2C DMA transfert complete callback */
1289+
hi2c->hdmatx->XferCpltCallback = I2C_DMAMasterTransmitCplt;
1290+
1291+
/* Set the DMA error callback */
1292+
hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
1293+
1294+
/* Enable the DMA Stream */
1295+
HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
1296+
}
1297+
12951298
/* Enable DMA Request */
12961299
hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
12971300

@@ -1338,19 +1341,6 @@ HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t D
13381341
hi2c->State = HAL_I2C_STATE_BUSY_RX;
13391342
hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
13401343

1341-
hi2c->pBuffPtr = pData;
1342-
hi2c->XferSize = Size;
1343-
hi2c->XferCount = Size;
1344-
1345-
/* Set the I2C DMA transfert complete callback */
1346-
hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
1347-
1348-
/* Set the DMA error callback */
1349-
hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1350-
1351-
/* Enable the DMA Stream */
1352-
HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
1353-
13541344
/* Send Slave Address */
13551345
if(I2C_MasterRequestRead(hi2c, DevAddress, I2C_TIMEOUT_FLAG) != HAL_OK)
13561346
{
@@ -1368,6 +1358,22 @@ HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t D
13681358
}
13691359
}
13701360

1361+
/* dpgeorge: DMA initialisation is moved to after sending the address */
1362+
{
1363+
hi2c->pBuffPtr = pData;
1364+
hi2c->XferSize = Size;
1365+
hi2c->XferCount = Size;
1366+
1367+
/* Set the I2C DMA transfert complete callback */
1368+
hi2c->hdmarx->XferCpltCallback = I2C_DMAMasterReceiveCplt;
1369+
1370+
/* Set the DMA error callback */
1371+
hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
1372+
1373+
/* Enable the DMA Stream */
1374+
HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
1375+
}
1376+
13711377
if(Size == 1)
13721378
{
13731379
/* Disable Acknowledge */
@@ -2050,19 +2056,6 @@ HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAdd
20502056
hi2c->State = HAL_I2C_STATE_MEM_BUSY_TX;
20512057
hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
20522058

2053-
hi2c->pBuffPtr = pData;
2054-
hi2c->XferSize = Size;
2055-
hi2c->XferCount = Size;
2056-
2057-
/* Set the I2C DMA transfert complete callback */
2058-
hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
2059-
2060-
/* Set the DMA error callback */
2061-
hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2062-
2063-
/* Enable the DMA Stream */
2064-
HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
2065-
20662059
/* Send Slave Address and Memory Address */
20672060
if(I2C_RequestMemoryWrite(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
20682061
{
@@ -2080,6 +2073,22 @@ HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAdd
20802073
}
20812074
}
20822075

2076+
/* dpgeorge: DMA initialisation is moved to after sending the addresses */
2077+
{
2078+
hi2c->pBuffPtr = pData;
2079+
hi2c->XferSize = Size;
2080+
hi2c->XferCount = Size;
2081+
2082+
/* Set the I2C DMA transfert complete callback */
2083+
hi2c->hdmatx->XferCpltCallback = I2C_DMAMemTransmitCplt;
2084+
2085+
/* Set the DMA error callback */
2086+
hi2c->hdmatx->XferErrorCallback = I2C_DMAError;
2087+
2088+
/* Enable the DMA Stream */
2089+
HAL_DMA_Start_IT(hi2c->hdmatx, (uint32_t)pData, (uint32_t)&hi2c->Instance->DR, Size);
2090+
}
2091+
20832092
/* Enable DMA Request */
20842093
hi2c->Instance->CR2 |= I2C_CR2_DMAEN;
20852094

@@ -2128,19 +2137,6 @@ HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddr
21282137
hi2c->State = HAL_I2C_STATE_MEM_BUSY_RX;
21292138
hi2c->ErrorCode = HAL_I2C_ERROR_NONE;
21302139

2131-
hi2c->pBuffPtr = pData;
2132-
hi2c->XferSize = Size;
2133-
hi2c->XferCount = Size;
2134-
2135-
/* Set the I2C DMA transfert complete callback */
2136-
hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
2137-
2138-
/* Set the DMA error callback */
2139-
hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2140-
2141-
/* Enable the DMA Stream */
2142-
HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
2143-
21442140
/* Send Slave Address and Memory Address */
21452141
if(I2C_RequestMemoryRead(hi2c, DevAddress, MemAddress, MemAddSize, I2C_TIMEOUT_FLAG) != HAL_OK)
21462142
{
@@ -2158,6 +2154,22 @@ HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddr
21582154
}
21592155
}
21602156

2157+
/* dpgeorge: DMA initialisation is moved to after sending the addresses */
2158+
{
2159+
hi2c->pBuffPtr = pData;
2160+
hi2c->XferSize = Size;
2161+
hi2c->XferCount = Size;
2162+
2163+
/* Set the I2C DMA transfert complete callback */
2164+
hi2c->hdmarx->XferCpltCallback = I2C_DMAMemReceiveCplt;
2165+
2166+
/* Set the DMA error callback */
2167+
hi2c->hdmarx->XferErrorCallback = I2C_DMAError;
2168+
2169+
/* Enable the DMA Stream */
2170+
HAL_DMA_Start_IT(hi2c->hdmarx, (uint32_t)&hi2c->Instance->DR, (uint32_t)pData, Size);
2171+
}
2172+
21612173
if(Size == 1)
21622174
{
21632175
/* Disable Acknowledge */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy