|
| 1 | +#include "crc_api.h" |
| 2 | + |
| 3 | +#include "platform/mbed_assert.h" |
| 4 | + |
| 5 | +#ifdef DEVICE_CRC |
| 6 | + |
| 7 | +#include "stm32f0xx_hal.h" |
| 8 | + |
| 9 | +static CRC_HandleTypeDef current_state; |
| 10 | +static uint32_t final_xor; |
| 11 | + |
| 12 | +bool hal_crc_is_supported(const crc_mbed_config_t* config) |
| 13 | +{ |
| 14 | + if (config == NULL) { |
| 15 | + return false; |
| 16 | + } |
| 17 | + |
| 18 | + if (config->polynomial != DEFAULT_CRC32_POLY) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + if (config->width != 32) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + if ((config->final_xor != 0xFFFFFFFFU) && (config->final_xor != 0)) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + return true; |
| 31 | +} |
| 32 | + |
| 33 | +void hal_crc_compute_partial_start(const crc_mbed_config_t* config) |
| 34 | +{ |
| 35 | + MBED_ASSERT(hal_crc_is_supported(config)); |
| 36 | + |
| 37 | + __HAL_RCC_CRC_CLK_ENABLE(); |
| 38 | + |
| 39 | + final_xor = config->final_xor; |
| 40 | + |
| 41 | + current_state.Instance = CRC; |
| 42 | + current_state.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; |
| 43 | + current_state.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; |
| 44 | + current_state.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_DISABLE; |
| 45 | + current_state.Init.InitValue = config->initial_xor; |
| 46 | + current_state.Init.CRCLength = CRC_POLYLENGTH_32B; |
| 47 | + current_state.Init.InputDataInversionMode = |
| 48 | + config->reflect_in ? CRC_INPUTDATA_INVERSION_BYTE |
| 49 | + : CRC_INPUTDATA_INVERSION_NONE; |
| 50 | + current_state.Init.OutputDataInversionMode = |
| 51 | + config->reflect_out ? CRC_OUTPUTDATA_INVERSION_ENABLE |
| 52 | + : CRC_OUTPUTDATA_INVERSION_DISABLE; |
| 53 | + |
| 54 | + HAL_CRC_Init(¤t_state); |
| 55 | +} |
| 56 | + |
| 57 | +void hal_crc_compute_partial(const uint8_t *data, const size_t size) |
| 58 | +{ |
| 59 | + HAL_CRC_Accumulate(¤t_state, (uint32_t *)data, size); |
| 60 | +} |
| 61 | + |
| 62 | +uint32_t hal_crc_get_result(void) |
| 63 | +{ |
| 64 | + const uint32_t result = current_state.Instance->DR; |
| 65 | + |
| 66 | + return (final_xor == 0xFFFFFFFFU) ? ~result : result; |
| 67 | +} |
| 68 | + |
| 69 | +#endif // DEVICE_CRC |
0 commit comments