STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers
crc.c
Go to the documentation of this file.
1 
25 /* Includes ------------------------------------------------------------------*/
26 #include "crc.h"
27 
28 #include "error_handler.h"
29 #include "mcu_hal.h"
30 #include "rcc.h"
31 
32 /* Private variables ---------------------------------------------------------*/
34 static CRC_HandleTypeDef crcHandle;
35 
36 /* Functions -----------------------------------------------------------------*/
37 void CrcInit(void)
38 {
39  RccEnableCrc();
40 
41  // Message: A conversion should not be performed between a pointer to
42  // object and an integer type [misra-c2012-11.4]
43  // Reason: The following CRC define is addressing the memory mapped
44  // base register of the CRC hardware peripheral.
45  // Risk: Conversion of a pointer to object into an integer may
46  // produce a value that cannot be represented in the chosen
47  // integer type resulting in undefined behavior.
48  // Prevention: Code reviews.
49  // cppcheck-suppress [misra-c2012-11.4]
50  crcHandle.Instance = CRC;
51 
52  crcHandle.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
53  crcHandle.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
54  crcHandle.Init.CRCLength = CRC_POLYLENGTH_32B;
55  crcHandle.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
56  crcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
57  crcHandle.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
58 
59  if (HAL_CRC_Init(&crcHandle) != HAL_OK)
60  {
61  ErrorHandler();
62  }
63 }
64 
65 void CrcDeInit(void)
66 {
67  (void)HAL_CRC_DeInit(&crcHandle);
68  RccDisableCrc();
69 }
70 
71 uint32_t CrcAccumulate(const uint8_t* const data, const uint32_t size)
72 {
73  // Message: A conversion shall not remove any const, volatile or _Atomic
74  // qualification from the type pointed to by a pointer
75  // [misra-c2012-11.8]
76  // A conversion shall not be performed between a pointer to
77  // object type and a pointer to a different object type
78  // [misra-c2012-11.3]
79  // Cast from 'const uint8_t *' to 'uint32_t *' increases
80  // required alignment from 1 to 4.
81  // Reason: The HAL implementation expects a non-const pointer to
82  // non-const data, however the CRC calculation does not (and
83  // should not) modify neither the pointer, nor the value.
84  // Therefore, this wrapper function expects a const pointer to
85  // const data and both the MISRA rule and the the message from
86  // clang-tidy [error: cast from 'const unsigned X *' to
87  // 'unsigned X *' drops const qualifier] is suppressed here.
88  // Additionally, the HAL implementation expects a `uint32_t*`
89  // data pointer. The CRC peripheral is configured to process
90  // bytes as input data. When using bytes, the HAL
91  // implementation casts the data pointer back to `uint8_t*`.
92  // Risk: Removing a const qualifier might circumvent the read-only
93  // status of an object and result in it being modified.
94  // Prevention: Code reviews.
95  // cppcheck-suppress [misra-c2012-11.3, misra-c2012-11.8]
96  // NOLINTNEXTLINE(clang-diagnostic-cast-qual,clang-diagnostic-cast-align)
97  return HAL_CRC_Accumulate(&crcHandle, (uint32_t* const)data, size);
98 }
99 
This file contains the CRC peripheral driver function prototypes for initialization,...
This file contains the error handling function prototypes.
void ErrorHandler(void)
This function is executed in case of error occurrence.
Definition: error_handler.c:32
static CRC_HandleTypeDef crcHandle
Definition: crc.c:34
uint32_t CrcAccumulate(const uint8_t *const data, const uint32_t size)
Calculate the CRC value over the provided buffer.
Definition: crc.c:71
void CrcDeInit(void)
De-initialize the CRC peripheral.
Definition: crc.c:65
void CrcInit(void)
Initialize the CRC peripheral.
Definition: crc.c:37
void RccDisableCrc(void)
Disable the CRC peripheral clock.
Definition: rcc.c:37
void RccEnableCrc(void)
Enable the CRC peripheral clock.
Definition: rcc.c:32
This file includes the microcontroller-specific HAL header files.
This file contains the RCC (Reset and Clock Control) driver function prototypes for enabling and disa...