2016-09-09 03:30 AM
I could not find a way to use the HAL (created by cubemx) to calculate a CCIT 16 bit CRC.
I did this for the L476 and this workes/** ------------------------------------------------------------------------- * @brief CRC peripheral initialization CRC_POLYNOMIAL_16B */void initialise_crc16_ccit (void){ hcrc.Instance = CRC;#ifdef STM32L476xx // The default polynomial is not used. The one to be used must be defined // in CrcHandle.Init.GeneratingPolynomial hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE; // Set the value of the generating polynomial. // The one used in that example is the 16-bit long CRC generating // polynomial X^16 + X^12 + X^5 + 1 */ hcrc.Init.GeneratingPolynomial = CRC_POLYNOMIAL_16B; // The user-defined generating polynomial yields a 16-bit long CRC hcrc.Init.CRCLength = CRC_POLYLENGTH_16B; // The default initialise value is not used hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_DISABLE; // User initialise value is used instead hcrc.Init.InitValue = CRC_INIT_VALUE; // The input data are inverted by word hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_FORMAT_BYTES; // The bit reversal is done on a full word basis. // The input stream yielded by CRC16_DATA8[] is the sequence of // bytes 0x4D, 0x3C, 0x2B, 0x1A, 0xBE, 0x71, ... // meaning that the first word written in the CRC DR register is // 0x4D3C2B1A. // Bit reversal done by the hardware on the full word leads to the actual // CRC processing of // 0x58D43CB2. // // Similarly, the second word written in the IP is 0xBE71C98A, leading to // the processing of 0x51938E7D after hardware input data reversal. // // Note that when the software writes less than a word in the IP (to minimize the // number of write accesses for a given number of bytes), the bit-reversal operation // is carried out only on the input data. // Therefore, the last written data is the last byte 0x5E, processed as 0x7A // by the hardware after bit-reversal to wrap-up the CRC computation. // // This means that the field InputDataInversionMode set to CRC_INPUTDATA_INVERSION_WORD // applied to {0x4D, 0x3C, 0x2B, 0x1A, 0xBE, 0x71, 0xC9, 0x8A, 0x5E} // yields the same result as InputDataInversionMode set to CRC_INPUTDATA_INVERSION_NONE // applied to {0x58, 0xD4, 0x3C, 0xB2, 0x51, 0x93, 0x8E, 0x7D, 0x7A}. // The output data are inverted hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLED; // Output data reversal can only be done at bit level. // The expected CRC is 0x5043 after output data reversal, meaning // that the CRC before the reversal operation is 0xC20A. */ // The input data are bytes (8-bit long data) hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;#endif#ifdef STM32L151xB//// @todo Christo we need the CRC for the L151 here#endif // De-initialize the CRC peripheral if (HAL_CRC_DeInit(&hcrc) != HAL_OK) { // Initialization Error Error_Handler(); } // Then, initialize the CRC handle if (HAL_CRC_Init(&hcrc) != HAL_OK) { // Initialization Error Error_Handler(); }}2016-09-09 08:31 AM
Hi de_jager.gerrie,
The CRC calculation unit in STM32F4 is used to get a CRC code in 32 bits data. For other STM32s like STM32L4 and STM32F3 for example, CRC code is on 8, 16, 32 bits data using a configurable generator polynomial value and size. In addition to reference manual , you can check the application note ''Using the CRC peipheral in the STM32 family'' -Hannibal-