[HAL][CRYP] Apply MCUAstyle fixes and update Crypt/Decrypt IT processes to avoid Computation Completed IRQ fires before the DINR pointer increment
diff --git a/Src/stm32f4xx_hal_cryp.c b/Src/stm32f4xx_hal_cryp.c
index cce0d46..c439c2c 100644
--- a/Src/stm32f4xx_hal_cryp.c
+++ b/Src/stm32f4xx_hal_cryp.c
@@ -2533,15 +2533,17 @@
/* Enable CRYP */
__HAL_CRYP_ENABLE(hcryp);
- /* Write the input block in the IN FIFO */
- hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
+ /* Increment the pointer before writing the input block in the IN FIFO to make sure that
+ when Computation Completed IRQ fires, the hcryp->CrypInCount has always a consistent value
+ and it is ready for the next operation. */
hcryp->CrypInCount++;
- hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
+ hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
hcryp->CrypInCount++;
- hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
+ hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
hcryp->CrypInCount++;
- hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
+ hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
hcryp->CrypInCount++;
+ hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
#else /* CRYP */
@@ -2780,7 +2782,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
@@ -2822,7 +2825,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
+ }
+ while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
/* Turn back to ALGOMODE of the configuration */
MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm);
@@ -2867,15 +2871,17 @@
/* Enable CRYP */
__HAL_CRYP_ENABLE(hcryp);
- /* Write the input block in the IN FIFO */
- hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
+ /* Increment the pointer before writing the input block in the IN FIFO to make sure that
+ when Computation Completed IRQ fires, the hcryp->CrypInCount has always a consistent value
+ and it is ready for the next operation. */
hcryp->CrypInCount++;
- hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
+ hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
hcryp->CrypInCount++;
- hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
+ hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
hcryp->CrypInCount++;
- hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
+ hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
hcryp->CrypInCount++;
+ hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + (hcryp->CrypInCount - 1U));
#else /* CRYP */
@@ -2961,7 +2967,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
@@ -3005,7 +3012,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
+ }
+ while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
/* Turn back to ALGOMODE of the configuration */
MODIFY_REG(hcryp->Instance->CR, CRYP_CR_ALGOMODE, hcryp->Init.Algorithm);
@@ -3943,7 +3951,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
+ }
+ while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
#else /* AES */
@@ -3980,7 +3989,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
@@ -4218,7 +4228,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
+ }
+ while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
#else /* AES */
@@ -4255,7 +4266,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
@@ -4855,7 +4867,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
+ }
+ while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
/* Select header phase */
CRYP_SET_PHASE(hcryp, CRYP_PHASE_HEADER);
@@ -5016,7 +5029,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
+ }
+ while ((hcryp->Instance->CR & CRYP_CR_CRYPEN) == CRYP_CR_CRYPEN);
#else /* AES */
@@ -5062,7 +5076,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
@@ -5107,7 +5122,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
}
@@ -5144,7 +5160,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
}
@@ -5178,7 +5195,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
}
@@ -5247,7 +5265,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
@@ -5545,16 +5564,16 @@
hcryp->Instance->DINR = *(uint32_t *)(hcryp->pCrypInBuffPtr + hcryp->CrypInCount);
hcryp->CrypInCount++;
if ((hcryp->CrypInCount == (hcryp->Size / 4U)) && ((hcryp->Size % 16U) == 0U))
- {
- /* Call Input transfer complete callback */
+ {
+ /* Call Input transfer complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
- /*Call registered Input complete callback*/
- hcryp->InCpltCallback(hcryp);
+ /*Call registered Input complete callback*/
+ hcryp->InCpltCallback(hcryp);
#else
- /*Call legacy weak Input complete callback*/
- HAL_CRYP_InCpltCallback(hcryp);
+ /*Call legacy weak Input complete callback*/
+ HAL_CRYP_InCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
- }
+ }
}
else /* Last block of payload < 128bit*/
{
@@ -5922,7 +5941,7 @@
{
headersize_in_bytes = hcryp->Init.HeaderSize;
}
-
+
if (headersize_in_bytes != 0U)
{
@@ -5966,7 +5985,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
}
}
else
@@ -6001,7 +6021,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
}
/* Last block optionally pad the data with zeros*/
for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 4U) % 4U)); loopcounter++)
@@ -6051,7 +6072,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, CRYP_FLAG_IFEM));
}
/* Wait until the complete message has been processed */
count = CRYP_TIMEOUT_GCMCCMHEADERPHASE;
@@ -6071,7 +6093,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
+ }
+ while (HAL_IS_BIT_SET(hcryp->Instance->SR, CRYP_FLAG_BUSY));
#else /* AES */
@@ -6119,7 +6142,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
@@ -6158,13 +6182,14 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
}
/* Last block optionally pad the data with zeros*/
- for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes /4U) % 4U)); loopcounter++)
+ for (loopcounter = 0U; (loopcounter < ((headersize_in_bytes / 4U) % 4U)); loopcounter++)
{
hcryp->Instance->DINR = *(uint32_t *)(hcryp->Init.Header + hcryp->CrypHeaderCount);
hcryp->CrypHeaderCount++ ;
@@ -6211,7 +6236,8 @@
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
- } while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
+ }
+ while (HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF));
/* Clear CCF flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
@@ -6329,10 +6355,10 @@
loopcounter++;
hcryp->CrypHeaderCount++;
/* Pad the data with zeros to have a complete block */
- while (loopcounter < 4U)
- {
- hcryp->Instance->DIN = 0x0U;
- loopcounter++;
+ while (loopcounter < 4U)
+ {
+ hcryp->Instance->DIN = 0x0U;
+ loopcounter++;
hcryp->CrypHeaderCount++;
}
}
@@ -6463,10 +6489,10 @@
loopcounter++;
hcryp->CrypHeaderCount++;
/* Pad the data with zeros to have a complete block */
- while (loopcounter < 4U)
- {
- hcryp->Instance->DINR = 0x0U;
- loopcounter++;
+ while (loopcounter < 4U)
+ {
+ hcryp->Instance->DINR = 0x0U;
+ loopcounter++;
hcryp->CrypHeaderCount++;
}
}