furi_hal_crc.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <furi_hal.h>
  2. #include <stm32wbxx_ll_crc.h>
  3. typedef enum {
  4. CRC_State_Reset,
  5. CRC_State_Ready,
  6. CRC_State_Busy,
  7. } CRC_State;
  8. typedef struct {
  9. CRC_State state;
  10. osMutexId_t mtx;
  11. } HAL_CRC_Control;
  12. static volatile HAL_CRC_Control hal_crc_control = {
  13. .state = CRC_State_Reset,
  14. .mtx = NULL,
  15. };
  16. void furi_hal_crc_init(bool synchronize) {
  17. /* initialize peripheral with default generating polynomial */
  18. LL_CRC_SetInputDataReverseMode(CRC, LL_CRC_INDATA_REVERSE_BYTE);
  19. LL_CRC_SetOutputDataReverseMode(CRC, LL_CRC_OUTDATA_REVERSE_BIT);
  20. LL_CRC_SetPolynomialCoef(CRC, LL_CRC_DEFAULT_CRC32_POLY);
  21. LL_CRC_SetPolynomialSize(CRC, LL_CRC_POLYLENGTH_32B);
  22. LL_CRC_SetInitialData(CRC, LL_CRC_DEFAULT_CRC_INITVALUE);
  23. if(synchronize) {
  24. hal_crc_control.mtx = osMutexNew(NULL);
  25. }
  26. hal_crc_control.state = CRC_State_Ready;
  27. }
  28. void furi_hal_crc_reset() {
  29. furi_check(hal_crc_control.state == CRC_State_Ready);
  30. if(hal_crc_control.mtx) {
  31. osMutexRelease(hal_crc_control.mtx);
  32. }
  33. LL_CRC_ResetCRCCalculationUnit(CRC);
  34. }
  35. static uint32_t furi_hal_crc_handle_8(uint8_t pBuffer[], uint32_t BufferLength) {
  36. uint32_t i; /* input data buffer index */
  37. hal_crc_control.state = CRC_State_Busy;
  38. /* Processing time optimization: 4 bytes are entered in a row with a single word write,
  39. * last bytes must be carefully fed to the CRC calculator to ensure a correct type
  40. * handling by the peripheral */
  41. for(i = 0U; i < (BufferLength / 4U); i++) {
  42. LL_CRC_FeedData32(
  43. CRC,
  44. ((uint32_t)pBuffer[4U * i] << 24U) | ((uint32_t)pBuffer[(4U * i) + 1U] << 16U) |
  45. ((uint32_t)pBuffer[(4U * i) + 2U] << 8U) | (uint32_t)pBuffer[(4U * i) + 3U]);
  46. }
  47. /* last bytes specific handling */
  48. if((BufferLength % 4U) != 0U) {
  49. if((BufferLength % 4U) == 1U) {
  50. LL_CRC_FeedData8(CRC, pBuffer[4U * i]);
  51. } else if((BufferLength % 4U) == 2U) {
  52. LL_CRC_FeedData16(
  53. CRC, ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U]);
  54. } else if((BufferLength % 4U) == 3U) {
  55. LL_CRC_FeedData16(
  56. CRC, ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U]);
  57. LL_CRC_FeedData8(CRC, pBuffer[(4U * i) + 2U]);
  58. }
  59. }
  60. hal_crc_control.state = CRC_State_Ready;
  61. /* Return the CRC computed value */
  62. return LL_CRC_ReadData32(CRC);
  63. }
  64. static uint32_t furi_hal_crc_accumulate(uint32_t pBuffer[], uint32_t BufferLength) {
  65. furi_check(hal_crc_control.state == CRC_State_Ready);
  66. if(hal_crc_control.mtx) {
  67. furi_check(osMutexGetOwner(hal_crc_control.mtx) != NULL);
  68. }
  69. return furi_hal_crc_handle_8((uint8_t*)pBuffer, BufferLength);
  70. }
  71. uint32_t furi_hal_crc_feed(void* data, uint16_t length) {
  72. return ~furi_hal_crc_accumulate(data, length);
  73. }
  74. bool furi_hal_crc_acquire(uint32_t timeout) {
  75. furi_assert(hal_crc_control.mtx);
  76. return osMutexAcquire(hal_crc_control.mtx, timeout) == osOK;
  77. }