furi_hal_crc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. furi_check(osMutexGetOwner(hal_crc_control.mtx) == osThreadGetId());
  32. osMutexRelease(hal_crc_control.mtx);
  33. }
  34. LL_CRC_ResetCRCCalculationUnit(CRC);
  35. }
  36. static uint32_t furi_hal_crc_handle_8(uint8_t pBuffer[], uint32_t BufferLength) {
  37. uint32_t i; /* input data buffer index */
  38. hal_crc_control.state = CRC_State_Busy;
  39. /* Processing time optimization: 4 bytes are entered in a row with a single word write,
  40. * last bytes must be carefully fed to the CRC calculator to ensure a correct type
  41. * handling by the peripheral */
  42. for(i = 0U; i < (BufferLength / 4U); i++) {
  43. LL_CRC_FeedData32(
  44. CRC,
  45. ((uint32_t)pBuffer[4U * i] << 24U) | ((uint32_t)pBuffer[(4U * i) + 1U] << 16U) |
  46. ((uint32_t)pBuffer[(4U * i) + 2U] << 8U) | (uint32_t)pBuffer[(4U * i) + 3U]);
  47. }
  48. /* last bytes specific handling */
  49. if((BufferLength % 4U) != 0U) {
  50. if((BufferLength % 4U) == 1U) {
  51. LL_CRC_FeedData8(CRC, pBuffer[4U * i]);
  52. } else if((BufferLength % 4U) == 2U) {
  53. LL_CRC_FeedData16(
  54. CRC, ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U]);
  55. } else if((BufferLength % 4U) == 3U) {
  56. LL_CRC_FeedData16(
  57. CRC, ((uint16_t)(pBuffer[4U * i]) << 8U) | (uint16_t)pBuffer[(4U * i) + 1U]);
  58. LL_CRC_FeedData8(CRC, pBuffer[(4U * i) + 2U]);
  59. }
  60. }
  61. hal_crc_control.state = CRC_State_Ready;
  62. /* Return the CRC computed value */
  63. return LL_CRC_ReadData32(CRC);
  64. }
  65. static uint32_t furi_hal_crc_accumulate(uint32_t pBuffer[], uint32_t BufferLength) {
  66. furi_check(hal_crc_control.state == CRC_State_Ready);
  67. if(hal_crc_control.mtx) {
  68. furi_check(osMutexGetOwner(hal_crc_control.mtx) != NULL);
  69. }
  70. return furi_hal_crc_handle_8((uint8_t*)pBuffer, BufferLength);
  71. }
  72. uint32_t furi_hal_crc_feed(void* data, uint16_t length) {
  73. return ~furi_hal_crc_accumulate(data, length);
  74. }
  75. bool furi_hal_crc_acquire(uint32_t timeout) {
  76. furi_assert(hal_crc_control.mtx);
  77. if(osMutexAcquire(hal_crc_control.mtx, timeout) == osOK) {
  78. LL_CRC_ResetCRCCalculationUnit(CRC);
  79. return true;
  80. }
  81. return false;
  82. }