HAL library 10- CRC for STM32Fxxx
STM32Fxxx devices have CRC (Cyclic Redundancy Check) module inside. This is small and useful unit when working with communications. It might become very useful to verify if your packet of data is correct.
STM32Fxxx devices uses “Ethernet” CRC with polynomial below:
X32 + X26 + X23 + X22 + X16 + X12 + X11 + X10 +X8 + X7 + X5 + X4 + X2 + X +1
It is quite ugly 😀 Well, using this module is very easy. I’ve made some functions to calculate CRC from block of data.
Library
Features
- Calculate CRC data from 8-bit data block
- Calculate CRC data from 16-bit data block
- Calculate CRC data from 32-bit data block
Dependencies
- HAL
- TM
- STM32Fxxx HAL
- defines.h
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
/** * @defgroup TM_CRC_Functions * @brief Library Functions * @{ */ /** * @brief Initializes and enables CRC peripheral * @param None * @retval None */ void TM_CRC_Init(void); /** * @brief De initializes and disable CRC peripheral * @param None * @retval None */ void TM_CRC_DeInit(void); /** * @brief Calculates 32-bit CRC value from 8-bit input array * @param *arr: Pointer to 8-bit data array for CRC calculation * @param count: Number of elements in array for calculation * @param reset: Reset CRC peripheral to 0 state before starting new calculations * - 0: CRC unit will not be reset before new calculations will happen and will use previous data to continue * - > 0: CRC unit is set to 0 before first calculation * @retval 32-bit CRC calculated number */ uint32_t TM_CRC_Calculate8(uint8_t* arr, uint32_t count, uint8_t reset); /** * @brief Calculates 32-bit CRC value from 16-bit input array * @param *arr: Pointer to 16-bit data array for CRC calculation * @param count: Number of elements in array for calculation * @param reset: Reset CRC peripheral to 0 state before starting new calculations * - 0: CRC unit will not be reset before new calculations will happen and will use previous data to continue * - > 0: CRC unit is set to 0 before first calculation * @retval 32-bit CRC calculated number */ uint32_t TM_CRC_Calculate16(uint16_t* arr, uint32_t count, uint8_t reset); /** * @brief Calculates 32-bit CRC value from 32-bit input array * @param *arr: Pointer to 32-bit data array for CRC calculation * @param count: Number of elements in array for calculation * @param reset: Reset CRC peripheral to 0 state before starting new calculations * - 0: CRC unit will not be reset before new calculations will happen and will use previous data to continue * - > 0: CRC unit is set to 0 before first calculation * @retval 32-bit CRC calculated number */ uint32_t TM_CRC_Calculate32(uint32_t* arr, uint32_t count, uint8_t reset); /** * @} */ |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
/** * Keil project example for CRC calculation * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_crc.h" /* 32-bit data array for CRC unit */ uint32_t data[16] = { 0x1234, 0x1234, 0x4562, 0x321, 0x32, 0x879, 0x987, 0x745, 0x74, 0x90, 0x98, 0x2432, 0x7453, 0xAFDE, 0xAAAA, 0xFFFF }; /* Result variable */ uint32_t result; int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Turn on all leds */ TM_DISCO_LedOn(LED_ALL); /* Initialize CRC calculation unit */ TM_CRC_Init(); /* Calculate CRC from block of 32-bits data array, 16 elements in array, reset CRC unit before start with calculation */ result = TM_CRC_Calculate32(data, 16, 1); while (1) { /* Do nothing */ } } |
Project is available on my Github, download all libraries below.
STM32 libraries based on STM32Fxxx HAL drivers.
Recent comments