ald_calc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_calc.c
  5. * @brief CALC module driver.
  6. *
  7. * @version V1.0
  8. * @date 04 Dec 2017
  9. * @author AE Team
  10. * @note
  11. *
  12. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  13. *
  14. *********************************************************************************
  15. */
  16. #include "ald_calc.h"
  17. /** @addtogroup ES32FXXX_ALD
  18. * @{
  19. */
  20. /** @defgroup CALC CALC
  21. * @brief CALC module driver
  22. * @{
  23. */
  24. #ifdef ALD_CALC
  25. /** @defgroup CALC_Public_Functions CALC Public Functions
  26. * @brief Accelerating calculate functions
  27. *
  28. * @verbatim
  29. ==============================================================================
  30. ##### Accelerating calculate functions #####
  31. ==============================================================================
  32. [..] This section provides functions allowing to:
  33. (+) Square root operation.
  34. (+) Division.
  35. (+) Get DZ flag.
  36. @endverbatim
  37. * @{
  38. */
  39. /**
  40. * @brief Square root operation.
  41. * @param data: The data;
  42. * @retval The value of square root.
  43. */
  44. uint32_t calc_sqrt(uint32_t data)
  45. {
  46. WRITE_REG(CALC->RDCND, data);
  47. while (READ_BIT(CALC->SQRTSR, CALC_SQRTSR_BUSY_MSK));
  48. return READ_REG(CALC->SQRTRES);
  49. }
  50. /**
  51. * @brief Calculating division.
  52. * @param dividend: The value of the dividend.
  53. * @param divisor: The value of the divisor.
  54. * @param remainder: The value of the remainder.
  55. * @retval The result of division.
  56. */
  57. uint32_t calc_div(uint32_t dividend, uint32_t divisor, uint32_t *remainder)
  58. {
  59. CLEAR_BIT(CALC->DIVCSR, CALC_DIVCSR_SIGN_MSK);
  60. SET_BIT(CALC->DIVCSR, CALC_DIVCSR_TRM_MSK);
  61. WRITE_REG(CALC->DIVDR, dividend);
  62. WRITE_REG(CALC->DIVSR, divisor);
  63. while (READ_BIT(CALC->DIVCSR, CALC_DIVCSR_BUSY_MSK));
  64. *remainder = READ_REG(CALC->DIVRR);
  65. return READ_REG(CALC->DIVQR);
  66. }
  67. /**
  68. * @brief Calculating division.
  69. * @param dividend: The value of the dividend.
  70. * @param divisor: The value of the divisor.
  71. * @param remainder: The value of the remainder.
  72. * @retval The result of division.
  73. */
  74. int32_t calc_div_sign(int32_t dividend, int32_t divisor, int32_t *remainder)
  75. {
  76. SET_BIT(CALC->DIVCSR, CALC_DIVCSR_SIGN_MSK);
  77. SET_BIT(CALC->DIVCSR, CALC_DIVCSR_TRM_MSK);
  78. WRITE_REG(CALC->DIVDR, dividend);
  79. WRITE_REG(CALC->DIVSR, divisor);
  80. while (READ_BIT(CALC->DIVCSR, CALC_DIVCSR_BUSY_MSK));
  81. *remainder = READ_REG(CALC->DIVRR);
  82. return READ_REG(CALC->DIVQR);
  83. }
  84. /**
  85. * @brief Get the flag of divisor is zero.
  86. * @retval The status, SET/RESET.
  87. */
  88. flag_status_t calc_get_dz_status(void)
  89. {
  90. if (READ_BIT(CALC->DIVCSR, CALC_DIVCSR_DZ_MSK))
  91. return SET;
  92. return RESET;
  93. }
  94. /**
  95. * @}
  96. */
  97. #endif /* ALD_CALC */
  98. /**
  99. * @}
  100. */
  101. /**
  102. * @}
  103. */