Library 47- CRC module on STM32F4
STM32F4xx 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.
STM32F4 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
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx CRC
- TM
- defines.h
- 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 57 |
/** * Initialize CRC peripheral * * No return */ extern void TM_CRC_Init(void); /** * Calculate 32-bit CRC value from 8-bit input array * * Parameters: * - uint8_t* arr: * Pointer to 8-bit data array for calculation * - uint16_t count: * Number of elements in array for calculation * - uint8_t reset: * 0: CRC unit will not be reset before new calculations will happen and will use * previous data to continue * 1: CRC unit is set to 0 before first calculation * * 32-bit CRC number is returned */ extern uint32_t TM_CRC_Calculate8(uint8_t* arr, uint16_t count, uint8_t reset); /** * Calculate 32-bit CRC value from 16-bit input array * * Parameters: * - uint16_t* arr: * Pointer to 16-bit data array for calculation * - uint16_t count: * Number of elements in array for calculation * - uint8_t reset: * 0: CRC unit will not be reset before new calculations will happen and will use * previous data to continue * 1: CRC unit is set to 0 before first calculation * * 32-bit CRC number is returned */ extern uint32_t TM_CRC_Calculate16(uint16_t* arr, uint16_t count, uint8_t reset); /** * Calculate 32-bit CRC value from 8-bit input array * * Parameters: * - uint32_t* arr: * Pointer to 32-bit data array for calculation * - uint16_t count: * Number of elements in array for calculation * - uint8_t reset: * 0: CRC unit will not be reset before new calculations will happen and will use * previous data to continue * 1: CRC unit is set to 0 before first calculation * * 32-bit CRC number is returned */ extern uint32_t TM_CRC_Calculate32(uint32_t* arr, uint16_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 56 57 58 |
/** * Keil project example for CRC * * 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 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" #include "tm_stm32f4_crc.h" #include "tm_stm32f4_swo.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 }; int main(void) { uint32_t result; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize leds on board */ 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 before start with calculation */ result = TM_CRC_Calculate32(data, 16, 1); while (1) { /* Toggle leds */ TM_DISCO_LedToggle(LED_ALL); /* Some delay */ Delayms(500); } } |
Project available on Github, download library below.
Recent comments