Modified for BG96
Fork of mbed-dev by
targets/TARGET_STM/mbed_crc_api.c@186:707f6e361f3e, 2018-06-22 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Jun 22 16:45:37 2018 +0100
- Revision:
- 186:707f6e361f3e
- Child:
- 187:0387e8f68319
mbed-dev library. Release version 162
Who changed what in which revision?
User | Revision | Line number | New 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 | |
Anna Bridge |
186:707f6e361f3e | 11 | bool hal_crc_is_supported(const crc_mbed_config_t* config) |
Anna Bridge |
186:707f6e361f3e | 12 | { |
Anna Bridge |
186:707f6e361f3e | 13 | if (config == NULL) { |
Anna Bridge |
186:707f6e361f3e | 14 | return false; |
Anna Bridge |
186:707f6e361f3e | 15 | } |
Anna Bridge |
186:707f6e361f3e | 16 | |
Anna Bridge |
186:707f6e361f3e | 17 | if (config->polynomial != POLY_32BIT_ANSI) { |
Anna Bridge |
186:707f6e361f3e | 18 | return false; |
Anna Bridge |
186:707f6e361f3e | 19 | } |
Anna Bridge |
186:707f6e361f3e | 20 | |
Anna Bridge |
186:707f6e361f3e | 21 | if (config->width != 32) { |
Anna Bridge |
186:707f6e361f3e | 22 | return false; |
Anna Bridge |
186:707f6e361f3e | 23 | } |
Anna Bridge |
186:707f6e361f3e | 24 | |
Anna Bridge |
186:707f6e361f3e | 25 | if ((config->final_xor != 0xFFFFFFFFU) && (config->final_xor != 0)) { |
Anna Bridge |
186:707f6e361f3e | 26 | return false; |
Anna Bridge |
186:707f6e361f3e | 27 | } |
Anna Bridge |
186:707f6e361f3e | 28 | |
Anna Bridge |
186:707f6e361f3e | 29 | return true; |
Anna Bridge |
186:707f6e361f3e | 30 | } |
Anna Bridge |
186:707f6e361f3e | 31 | |
Anna Bridge |
186:707f6e361f3e | 32 | void hal_crc_compute_partial_start(const crc_mbed_config_t* config) |
Anna Bridge |
186:707f6e361f3e | 33 | { |
Anna Bridge |
186:707f6e361f3e | 34 | MBED_ASSERT(hal_crc_is_supported(config)); |
Anna Bridge |
186:707f6e361f3e | 35 | |
Anna Bridge |
186:707f6e361f3e | 36 | __HAL_RCC_CRC_CLK_ENABLE(); |
Anna Bridge |
186:707f6e361f3e | 37 | |
Anna Bridge |
186:707f6e361f3e | 38 | final_xor = config->final_xor; |
Anna Bridge |
186:707f6e361f3e | 39 | |
Anna Bridge |
186:707f6e361f3e | 40 | current_state.Instance = CRC; |
Anna Bridge |
186:707f6e361f3e | 41 | current_state.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; |
Anna Bridge |
186:707f6e361f3e | 42 | current_state.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; |
Anna Bridge |
186:707f6e361f3e | 43 | current_state.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_DISABLE; |
Anna Bridge |
186:707f6e361f3e | 44 | current_state.Init.InitValue = config->initial_xor; |
Anna Bridge |
186:707f6e361f3e | 45 | current_state.Init.CRCLength = CRC_POLYLENGTH_32B; |
Anna Bridge |
186:707f6e361f3e | 46 | current_state.Init.InputDataInversionMode = |
Anna Bridge |
186:707f6e361f3e | 47 | config->reflect_in ? CRC_INPUTDATA_INVERSION_BYTE |
Anna Bridge |
186:707f6e361f3e | 48 | : CRC_INPUTDATA_INVERSION_NONE; |
Anna Bridge |
186:707f6e361f3e | 49 | current_state.Init.OutputDataInversionMode = |
Anna Bridge |
186:707f6e361f3e | 50 | config->reflect_out ? CRC_OUTPUTDATA_INVERSION_ENABLE |
Anna Bridge |
186:707f6e361f3e | 51 | : CRC_OUTPUTDATA_INVERSION_DISABLE; |
Anna Bridge |
186:707f6e361f3e | 52 | |
Anna Bridge |
186:707f6e361f3e | 53 | HAL_CRC_Init(¤t_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 | { |
Anna Bridge |
186:707f6e361f3e | 58 | if (data && size) { |
Anna Bridge |
186:707f6e361f3e | 59 | HAL_CRC_Accumulate(¤t_state, (uint32_t *)data, size); |
Anna Bridge |
186:707f6e361f3e | 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 | { |
Anna Bridge |
186:707f6e361f3e | 65 | const uint32_t result = current_state.Instance->DR; |
Anna Bridge |
186:707f6e361f3e | 66 | |
Anna Bridge |
186:707f6e361f3e | 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 |