mbed_crc_api.c 1.8 KB

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