hal_utils.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "hal/hal_utils.h"
  7. #include "hal/assert.h"
  8. /**
  9. * @brief helper function, calculate the Greatest Common Divisor
  10. * @note gcd(a, b) = gcd(b, a % b)
  11. * @param a bigger value
  12. * @param b smaller value
  13. * @return result of gcd(a, b)
  14. */
  15. __attribute__((always_inline))
  16. static inline uint32_t _gcd(uint32_t a, uint32_t b)
  17. {
  18. uint32_t c = a % b;
  19. while (c != 0) {
  20. a = b;
  21. b = c;
  22. c = a % b;
  23. }
  24. return b;
  25. }
  26. __attribute__((always_inline))
  27. static inline uint32_t _sub_abs(uint32_t a, uint32_t b)
  28. {
  29. return a > b ? a - b : b - a;
  30. }
  31. uint32_t hal_utils_calc_clk_div_frac_fast(const hal_utils_clk_info_t *clk_info, hal_utils_clk_div_t *clk_div)
  32. {
  33. HAL_ASSERT(clk_info->max_fract > 2);
  34. uint32_t div_denom = 2;
  35. uint32_t div_numer = 0;
  36. uint32_t div_integ = clk_info->src_freq_hz / clk_info->exp_freq_hz;
  37. uint32_t freq_error = clk_info->src_freq_hz % clk_info->exp_freq_hz;
  38. // fractional divider
  39. if (freq_error) {
  40. // Carry bit if the decimal is greater than 1.0 - 1.0 / ((max_fract - 1) * 2)
  41. if (freq_error < clk_info->exp_freq_hz - clk_info->exp_freq_hz / (clk_info->max_fract - 1) * 2) {
  42. // Calculate the Greatest Common Divisor, time complexity O(log n)
  43. uint32_t gcd = _gcd(clk_info->exp_freq_hz, freq_error);
  44. // divide by the Greatest Common Divisor to get the accurate fraction before normalization
  45. div_denom = clk_info->exp_freq_hz / gcd;
  46. div_numer = freq_error / gcd;
  47. // normalize div_denom and div_numer
  48. uint32_t d = div_denom / clk_info->max_fract + 1;
  49. // divide by the normalization coefficient to get the denominator and numerator within range of clk_info->max_fract
  50. div_denom /= d;
  51. div_numer /= d;
  52. } else {
  53. div_integ++;
  54. }
  55. }
  56. // If the expect frequency is too high or too low to satisfy the integral division range, failed and return 0
  57. if (div_integ < clk_info->min_integ || div_integ >= clk_info->max_integ || div_integ == 0) {
  58. return 0;
  59. }
  60. // Assign result
  61. clk_div->integer = div_integ;
  62. clk_div->denominator = div_denom;
  63. clk_div->numerator = div_numer;
  64. // Return the actual frequency
  65. if (div_numer) {
  66. uint32_t temp = div_integ * div_denom + div_numer;
  67. return (uint32_t)(((uint64_t)clk_info->src_freq_hz * div_denom + temp / 2) / temp);
  68. }
  69. return clk_info->src_freq_hz / div_integ;
  70. }
  71. uint32_t hal_utils_calc_clk_div_frac_accurate(const hal_utils_clk_info_t *clk_info, hal_utils_clk_div_t *clk_div)
  72. {
  73. HAL_ASSERT(clk_info->max_fract > 2);
  74. uint32_t div_denom = 2;
  75. uint32_t div_numer = 0;
  76. uint32_t div_integ = clk_info->src_freq_hz / clk_info->exp_freq_hz;
  77. uint32_t freq_error = clk_info->src_freq_hz % clk_info->exp_freq_hz;
  78. if (freq_error) {
  79. // Carry bit if the decimal is greater than 1.0 - 1.0 / ((max_fract - 1) * 2)
  80. if (freq_error < clk_info->exp_freq_hz - clk_info->exp_freq_hz / (clk_info->max_fract - 1) * 2) {
  81. // Search the closest fraction, time complexity O(n)
  82. for (uint32_t sub = 0, a = 2, b = 0, min = UINT32_MAX; min && a < clk_info->max_fract; a++) {
  83. b = (a * freq_error + clk_info->exp_freq_hz / 2) / clk_info->exp_freq_hz;
  84. sub = _sub_abs(clk_info->exp_freq_hz * b, freq_error * a);
  85. if (sub < min) {
  86. div_denom = a;
  87. div_numer = b;
  88. min = sub;
  89. }
  90. }
  91. } else {
  92. div_integ++;
  93. }
  94. }
  95. // If the expect frequency is too high or too low to satisfy the integral division range, failed and return 0
  96. if (div_integ < clk_info->min_integ || div_integ >= clk_info->max_integ || div_integ == 0) {
  97. return 0;
  98. }
  99. // Assign result
  100. clk_div->integer = div_integ;
  101. clk_div->denominator = div_denom;
  102. clk_div->numerator = div_numer;
  103. // Return the actual frequency
  104. if (div_numer) {
  105. uint32_t temp = div_integ * div_denom + div_numer;
  106. return (uint32_t)(((uint64_t)clk_info->src_freq_hz * div_denom + temp / 2) / temp);
  107. }
  108. return clk_info->src_freq_hz / div_integ;
  109. }
  110. uint32_t hal_utils_calc_clk_div_integer(const hal_utils_clk_info_t *clk_info, uint32_t *int_div)
  111. {
  112. uint32_t div_integ = clk_info->src_freq_hz / clk_info->exp_freq_hz;
  113. uint32_t freq_error = clk_info->src_freq_hz % clk_info->exp_freq_hz;
  114. /* If there is error and always round up,
  115. Or, do the normal rounding and error >= (src/n + src/(n+1)) / 2,
  116. then carry the bit */
  117. if ((freq_error && clk_info->round_opt == HAL_DIV_ROUND_UP) || (clk_info->round_opt == HAL_DIV_ROUND &&
  118. (freq_error >= clk_info->src_freq_hz / (2 * div_integ * (div_integ + 1))))) {
  119. div_integ++;
  120. }
  121. // Assign result
  122. *int_div = div_integ;
  123. // Return the actual frequency
  124. return clk_info->src_freq_hz / div_integ;
  125. }