hpm_mcl_common.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_MCL_COMMON_H
  8. #define HPM_MCL_COMMON_H
  9. #include "hpm_common.h"
  10. #include "hpm_mcl_cfg.h"
  11. #include "hpm_mcl_physical.h"
  12. #include "hpm_mcl_math.h"
  13. #include "stdio.h"
  14. typedef uint32_t hpm_mcl_stat_t;
  15. enum {
  16. mcl_group_common = 0,
  17. mcl_group_motor = 1,
  18. mcl_group_encoder = 2,
  19. mcl_group_analog = 3,
  20. mcl_group_drivers = 4,
  21. };
  22. /**
  23. * @brief User-defined data with data and enable bits
  24. *
  25. */
  26. typedef struct {
  27. float value;
  28. bool enable;
  29. } mcl_user_value_t;
  30. enum {
  31. mcl_success = MAKE_STATUS(mcl_group_common, 0),
  32. mcl_fail = MAKE_STATUS(mcl_group_common, 1),
  33. mcl_invalid_argument = MAKE_STATUS(mcl_group_common, 2),
  34. mcl_invalid_pointer = MAKE_STATUS(mcl_group_common, 3),
  35. mcl_timeout = MAKE_STATUS(mcl_group_common, 4),
  36. mcl_in_development = MAKE_STATUS(mcl_group_common, 5), /**< Functions under development */
  37. mcl_running = MAKE_STATUS(mcl_group_common, 6),
  38. };
  39. /**
  40. * @brief user define code
  41. *
  42. */
  43. void mcl_user_delay_us(uint64_t tick);
  44. #define MCL_DEBUG printf
  45. #define MCL_PI HPM_PI
  46. #define MCL_2PI HPM_2_PI
  47. #define MCL_PI_DIV3 (MCL_PI / 3.0f)
  48. #define MCL_DELAY_US(x) mcl_user_delay_us(x)
  49. #define MCL_DELAY_MS(x) MCL_DELAY_US(1000*x)
  50. #define MCL_EMPTY
  51. #define MCL_ASSERT_BOOL(b, code_extend, errcode) \
  52. do { \
  53. if (!b) { \
  54. code_extend; \
  55. MCL_DEBUG("errcode:%d, file:%s, line:%d.\r\n", errcode, __FILE__, __LINE__); \
  56. return errcode; \
  57. } \
  58. } while (0)
  59. #define MCL_ASSERT_EXEC_CODE_BOOL(b, code_extend) \
  60. do { \
  61. if (!b) { \
  62. code_extend; \
  63. } \
  64. } while (0)
  65. #define MCL_ASSERT(x, return_errcode) MCL_ASSERT_BOOL(((uint32_t)(x) != 0), MCL_EMPTY, return_errcode)
  66. #define MCL_ASSERT_EXEC_CODE(x, code) MCL_ASSERT_EXEC_CODE_BOOL((x), code)
  67. #define MCL_ASSERT_EXEC_CODE_AND_RETURN(x, code, return_errcode) MCL_ASSERT_BOOL((x), code, return_errcode)
  68. #ifdef NDEBUG
  69. #define MCL_ASSERT_EXEC_CODE_OPT(x, code) ((void)0)
  70. #define MCL_ASSERT_OPT(x, return_errcode) ((void)0)
  71. #define MCL_ASSERT_EXEC_CODE_AND_RETURN_OPT(x, code, return_errcode) ((void)0)
  72. #else
  73. #define MCL_ASSERT_EXEC_CODE_OPT MCL_ASSERT_EXEC_CODE
  74. #define MCL_ASSERT_OPT MCL_ASSERT
  75. #define MCL_ASSERT_EXEC_CODE_AND_RETURN_OPT MCL_ASSERT_EXEC_CODE_AND_RETURN
  76. #endif
  77. #define MCL_FUNCTION_EXC_IF_ENABLE(b, str_f, f) \
  78. do { \
  79. if (b) { \
  80. str_f = f; \
  81. } \
  82. } while (0)
  83. #define MCL_FUNCTION_EXC_IF_ELSE_ENABLE(b, str_f, _if, _else) \
  84. do { \
  85. if (b) { \
  86. str_f = _if; \
  87. } else { \
  88. str_f = _else; \
  89. } \
  90. } while (0)
  91. #define MCL_FUNCTION_INIT_IF_EMPTY(str_function, function) MCL_FUNCTION_EXC_IF_ENABLE((str_function == NULL), str_function, function)
  92. #define MCL_FUNCTION_INIT_IF_NO_EMPTY(function, str_function) MCL_FUNCTION_EXC_IF_ENABLE((str_function != NULL), function, str_function)
  93. #define MCL_STATUS_SET_IF_TRUE(real, str, status) MCL_FUNCTION_EXC_IF_ENABLE((real), str, status)
  94. #define MCL_VALUE_SET_IF_TRUE(real, str, value) MCL_FUNCTION_EXC_IF_ENABLE((real), str, value)
  95. #define MCL_FUNCTION_SET_IF_ELSE_TRUE(real, str, _if, _else) MCL_FUNCTION_EXC_IF_ELSE_ENABLE((real), str, _if, _else)
  96. /**
  97. * @brief If used, this means that the callback function is optional
  98. *
  99. */
  100. #define _FUNC_OPTIONAL_
  101. /**
  102. * @brief Remainder of angle
  103. *
  104. */
  105. #define MCL_ANGLE_MOD_X(down, up, val) \
  106. ({ \
  107. float val_; \
  108. if ((val) > up) { \
  109. val_ = (val); \
  110. do { \
  111. val_ = (val_) - (up - down); \
  112. } while ((val_) > up); \
  113. } else if ((val) < down) { \
  114. val_ = (val); \
  115. do { \
  116. val_ = (val_) + (up - down); \
  117. } while ((val_) < down); \
  118. } else { \
  119. val_ = (val); \
  120. } \
  121. (val_); \
  122. })
  123. /**
  124. * @brief Calculate the difference in angle,
  125. * because the angle is then changed between 0-360 degrees,
  126. * there are 350 degrees to 0 degrees of the process of change,
  127. * as well as 10 degrees to 360 degrees of the process of change,
  128. * in this process, the actual angle change is 10 degrees,
  129. * but it may be calculated as 350 degrees,
  130. * so the role of the calculation is to strive for an angle value of 10 degrees,
  131. * the offset value of the maximum angle value, the default is 2pi
  132. *
  133. */
  134. #define MCL_GET_ANGLE_DELTA(val, offset) \
  135. ({ \
  136. float val_; \
  137. float temp; \
  138. val_ = 0; \
  139. temp = 0; \
  140. if ((val) > 0) { \
  141. temp = (val) - offset; \
  142. } else if ((val) < 0) { \
  143. temp = (val) + offset; \
  144. } else { \
  145. val_ = 0; \
  146. } \
  147. if (fabs(val) < fabs(temp)) { \
  148. val_ = val; \
  149. } else {\
  150. val_ = temp; \
  151. } \
  152. (val_); \
  153. })
  154. /**
  155. * @brief Data Range Limits
  156. *
  157. */
  158. #define MCL_VALUE_LIMIT(val, min, max) \
  159. do { \
  160. if ((val) > (max)) { \
  161. val = max; \
  162. } else if ((val) < (min)) { \
  163. val = min; \
  164. } \
  165. } while (0)
  166. /**
  167. * @brief Get ADC data with 12bit valid bits
  168. *
  169. */
  170. #define MCL_GET_ADC_12BIT_VALID_DATA(x) ((x & 0xffff) >> 4)
  171. typedef struct {
  172. mcl_physical_para_t physical;
  173. mcl_physical_para_q_t physical_q;
  174. } mcl_cfg_t;
  175. #endif