Skip to content

Commit 68ee05e

Browse files
committed
Remove ST CRC library but keep using hardware-accelerated CRC32.
Add integrity check of the vector table. Signed-off-by: Marcos Chaparro <[email protected]>
1 parent 3c6083c commit 68ee05e

File tree

6 files changed

+57
-226
lines changed

6 files changed

+57
-226
lines changed

ChibiOS_3.0.2/ext/stdperiph_stm32f4/inc/stm32f4xx_crc.h

-83
This file was deleted.

ChibiOS_3.0.2/ext/stdperiph_stm32f4/src/stm32f4xx_crc.c

-133
This file was deleted.

ChibiOS_3.0.2/ext/stdperiph_stm32f4/stm32lib.mk

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ STM32SRC = ${CHIBIOS}/ext/stdperiph_stm32f4/src/misc.c \
88
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_syscfg.c \
99
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_tim.c \
1010
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_iwdg.c \
11-
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_crc.c \
1211
${CHIBIOS}/ext/stdperiph_stm32f4/src/stm32f4xx_wwdg.c
1312

1413
STM32INC = ${CHIBIOS}/ext/stdperiph_stm32f4/inc

crc.c

+27
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include "crc.h"
21+
#include "stm32f4xx.h"
2122

2223
// CRC Table
2324
const unsigned short crc16_tab[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084,
@@ -58,3 +59,29 @@ unsigned short crc16(unsigned char *buf, unsigned int len) {
5859
}
5960
return cksum;
6061
}
62+
63+
/**
64+
* @brief Computes the 32-bit CRC of a given buffer of data word(32-bit) using
65+
* Hardware Acceleration.
66+
* @param pBuffer: pointer to the buffer containing the data to be computed
67+
* @param BufferLength: length of the buffer to be computed
68+
* @retval 32-bit CRC
69+
*/
70+
uint32_t crc32(uint32_t *pBuffer, uint32_t BufferLength) {
71+
uint32_t index = 0;
72+
73+
for(index = 0; index < BufferLength; index++) {
74+
CRC->DR = pBuffer[index];
75+
}
76+
return (CRC->DR);
77+
}
78+
79+
/**
80+
* @brief Resets the CRC Data register (DR).
81+
* @param None
82+
* @retval None
83+
*/
84+
void crc32_reset(void) {
85+
/* Reset CRC generator */
86+
CRC->CR = CRC_CR_RESET;
87+
}

crc.h

+4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@
2020
#ifndef CRC_H_
2121
#define CRC_H_
2222

23+
#include <stdint.h>
24+
2325
/*
2426
* Functions
2527
*/
2628
unsigned short crc16(unsigned char *buf, unsigned int len);
29+
uint32_t crc32(uint32_t *buf, uint32_t len);
30+
void crc32_reset(void);
2731

2832
#endif /* CRC_H_ */

flash_helper.c

+26-9
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#include "ch.h"
2222
#include "hal.h"
2323
#include "stm32f4xx_conf.h"
24-
#include "stm32f4xx_crc.h"
2524
#include "utils.h"
2625
#include "mc_interface.h"
2726
#include "timeout.h"
2827
#include "hw.h"
28+
#include "crc.h"
2929
#include <string.h>
3030

3131
/*
@@ -56,6 +56,12 @@
5656
#define APP_CRC_WAS_CALCULATED_FLAG ((uint32_t)0xAAAAAAAA)
5757
#define APP_CRC_WAS_CALCULATED_FLAG_ADDRESS (uint32_t*)(APP_MAX_SIZE - 8)
5858

59+
#define VECTOR_TABLE_ADDRESS (uint32_t *)ADDR_FLASH_SECTOR_0
60+
#define VECTOR_TABLE_SIZE (ADDR_FLASH_SECTOR_1 - ADDR_FLASH_SECTOR_0)
61+
62+
#define APP_START_ADDRESS (uint32_t *)(ADDR_FLASH_SECTOR_1 + EEPROM_EMULATION_SIZE)
63+
#define APP_SIZE (APP_MAX_SIZE - VECTOR_TABLE_SIZE - EEPROM_EMULATION_SIZE)
64+
5965
// Private constants
6066
static const uint32_t flash_addr[FLASH_SECTORS] = {
6167
ADDR_FLASH_SECTOR_0,
@@ -214,15 +220,19 @@ uint32_t flash_helper_verify_flash_memory(void) {
214220
if( (APP_CRC_WAS_CALCULATED_FLAG_ADDRESS)[0] == APP_CRC_WAS_CALCULATED_FLAG )
215221
{
216222
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
217-
CRC_ResetDR();
223+
crc32_reset();
224+
225+
// compute vector table (sector 0)
226+
crc32((VECTOR_TABLE_ADDRESS), (VECTOR_TABLE_SIZE)/4);
227+
228+
// skip emulated EEPROM (sector 1 and 2)
218229

219-
// A CRC over the full image should return zero. Exclude the pages used for
220-
// storing user configuration data.
221-
crc= CRC_CalcBlockCRC((uint32_t*)( ADDR_FLASH_SECTOR_0 + EEPROM_EMULATION_SIZE),
222-
(APP_MAX_SIZE - EEPROM_EMULATION_SIZE)/4);
230+
// compute application code
231+
crc = crc32(APP_START_ADDRESS, (APP_SIZE)/4);
223232

224233
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, DISABLE);
225234

235+
// A CRC over the full image should return zero.
226236
return (crc == 0)? FAULT_CODE_NONE : FAULT_CODE_FLASH_CORRUPTION;
227237
}
228238
else {
@@ -239,9 +249,16 @@ uint32_t flash_helper_verify_flash_memory(void) {
239249

240250
// Compute flash crc including the new flag
241251
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
242-
CRC_ResetDR();
243-
crc= CRC_CalcBlockCRC((uint32_t*)(ADDR_FLASH_SECTOR_0 + EEPROM_EMULATION_SIZE),
244-
(APP_MAX_SIZE - EEPROM_EMULATION_SIZE - 4)/4);
252+
crc32_reset();
253+
254+
// compute vector table (sector 0)
255+
crc32(VECTOR_TABLE_ADDRESS, (VECTOR_TABLE_SIZE)/4);
256+
257+
// skip emulated EEPROM (sector 1 and 2)
258+
259+
// compute application code
260+
crc = crc32(APP_START_ADDRESS, (APP_SIZE - 4)/4);
261+
245262
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, DISABLE);
246263

247264
//Store CRC

0 commit comments

Comments
 (0)