Modified for BG96

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Sep 06 13:40:20 2018 +0100
Revision:
187:0387e8f68319
Parent:
186:707f6e361f3e
mbed-dev library. Release version 163

Who changed what in which revision?

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