cancel
Showing results for 
Search instead for 
Did you mean: 

How does CRC_Handle_16 work within the HAL library?

DOver.1
Associate

Hello,

I am currently trying to understand exactly how CRC_Handle_16 function works within the stm32f0xx_hal_crc.c file so I can implement the algorithm in a non-hardware way. The non-hardware implementation I have works fine if there is an even number of BufferLength. The problem I'm having is when I try to insert an odd number of elements. Basically, what I want to know is what exactly is happening on lines 23-24 and how would I implement this in a non-hardware way?

Any help is appreciated, thanks.

/**
  * @brief  Enter 16-bit input data to the CRC calculator.
  *         Specific data handling to optimize processing time.
  * @param  hcrc CRC handle
  * @param  pBuffer pointer to the input data buffer
  * @param  BufferLength input data buffer length
  * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  */
static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
{
  uint32_t i;  /* input data buffer index */
  __IO uint16_t *pReg;
 
  /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
   * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure
   * a correct type handling by the peripheral */
  for (i = 0U; i < (BufferLength / 2U); i++)
  {
    hcrc->Instance->DR = ((uint32_t)pBuffer[2U * i] << 16U) | (uint32_t)pBuffer[(2U * i) + 1U];
  }
  if ((BufferLength % 2U) != 0U)
  {
    pReg = (__IO uint16_t *)(__IO void *)(&hcrc->Instance->DR);                 /* Derogation MisraC2012 R.11.5 */
    *pReg = pBuffer[2U * i];
  }
 
  /* Return the CRC computed value */
  return hcrc->Instance->DR;
}

2 REPLIES 2

Type punning, to allow 16-bit write to the data register (which is otherwise defined as a pointer to 32-bit word, i.e. "plain" writes to it (assignments) result in 32-bit writes).

See disasm.

JW

>>..so I can implement the algorithm in a non-hardware way

Not sure I understand, surely doing a 16-bit sw implementation would be trivial for both odd and even counts.

The method here feeds 32-bit value pairs into the hardware until it is left with one 16-bit word to feed, and then it does an explicit 16-bit wide write to the register.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..