hpm_vsc_drv.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (c) 2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_VSC_DRV_H
  8. #define HPM_VSC_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_vsc_regs.h"
  11. /**
  12. * @brief VSC driver APIs
  13. * @defgroup vsc_interface VSC driver APIs
  14. * @ingroup motor_interfaces
  15. * @{
  16. */
  17. /**
  18. * @brief vsc phase mode
  19. *
  20. */
  21. typedef enum vsc_phase_mode {
  22. vsc_abc_phase = 0,
  23. vsc_ab_phase = 1,
  24. } vsc_phase_mode_t; /**< vsc_phase_mode_t */
  25. /**
  26. * @brief vsc data operation mode
  27. *
  28. */
  29. typedef enum vsc_data_opr_mode {
  30. vsc_data_opr_plus_mul_1 = 0,
  31. vsc_data_opr_plus_mul_2 = 1,
  32. vsc_data_opr_plus_div_2 = 5,
  33. vsc_data_opr_plus_div_3 = 6,
  34. vsc_data_opr_plus_div_4 = 7,
  35. vsc_data_opr_minus_mul_1 = 8,
  36. vsc_data_opr_minus_mul_2 = 9,
  37. vsc_data_opr_minus_div_2 = 13,
  38. vsc_data_opr_minus_div_3 = 14,
  39. vsc_data_opr_minus_div_4 = 15
  40. } vsc_data_opr_mode_t; /**< vsc_data_opr_mode_t */
  41. /**
  42. * @brief vsc select adc instance
  43. *
  44. */
  45. typedef enum vsc_adc_sel {
  46. vsc_sel_adc0 = 1,
  47. vsc_sel_adc1 = 2,
  48. vsc_sel_adc2 = 3
  49. } vsc_adc_sel_t; /**< vsc_adc_sel_t */
  50. /**
  51. * @brief vsc position capture mode
  52. *
  53. */
  54. typedef enum vsc_pos_cap_mode {
  55. vsc_pos_use_last_data_when_adc_sample_finish = 0,
  56. vsc_pos_use_first_data_after_adc_sample_start = 1,
  57. vsc_pos_use_last_data_before_adc_sample_start = 2
  58. } vsc_pos_cap_mode_t; /**< vsc_pos_cap_mode_t */
  59. /**
  60. * @brief vsc select adc timestamp
  61. *
  62. */
  63. typedef enum vsc_timestamp_sel {
  64. vsc_timestamp_sel_value_a = 1,
  65. vsc_timestamp_sel_value_b = 2,
  66. vsc_timestamp_sel_value_c = 3
  67. } vsc_timestamp_sel_t; /**< vsc_timestamp_sel_t */
  68. /**
  69. * @brief vsc irq mask bit
  70. *
  71. */
  72. typedef enum vsc_irq_mask {
  73. vsc_irq_convert_done = BIT0_MASK,
  74. vsc_irq_abc_over_tolerate = BIT1_MASK,
  75. vsc_irq_c_overflow = BIT2_MASK,
  76. vsc_irq_b_overflow = BIT3_MASK,
  77. vsc_irq_a_overflow = BIT4_MASK,
  78. vsc_irq_adc2_cap_not_enough = BIT5_MASK,
  79. vsc_irq_adc1_cap_not_enough = BIT6_MASK,
  80. vsc_irq_adc0_cap_not_enough = BIT7_MASK,
  81. vsc_irq_pos_timeout = BIT8_MASK,
  82. vsc_irq_adc2_timeout = BIT9_MASK,
  83. vsc_irq_adc1_timeout = BIT10_MASK,
  84. vsc_irq_adc0_timeout = BIT11_MASK,
  85. vsc_irq_convert_conflict = BIT12_MASK
  86. } vsc_irq_mask_t;
  87. /**
  88. * @brief adc config structure
  89. */
  90. typedef struct {
  91. vsc_adc_sel_t adc_sel;
  92. uint8_t adc_chn;
  93. uint32_t adc_offset;
  94. } vsc_adc_config_t;
  95. /**
  96. * @brief vsc config structure
  97. */
  98. typedef struct {
  99. vsc_data_opr_mode_t opr_0;
  100. vsc_data_opr_mode_t opr_1;
  101. vsc_data_opr_mode_t opr_2;
  102. vsc_data_opr_mode_t opr_3;
  103. } vsc_data_opr_config_t;
  104. /**
  105. * @brief vsc config structure
  106. */
  107. typedef struct {
  108. vsc_phase_mode_t phase_mode;
  109. uint8_t a_data_cnt;
  110. uint8_t b_data_cnt;
  111. uint8_t c_data_cnt;
  112. vsc_data_opr_config_t a_data_opr_config;
  113. vsc_data_opr_config_t b_data_opr_config;
  114. vsc_data_opr_config_t c_data_opr_config;
  115. vsc_adc_config_t a_adc_config;
  116. vsc_adc_config_t b_adc_config;
  117. vsc_adc_config_t c_adc_config;
  118. vsc_pos_cap_mode_t pos_cap_mode;
  119. uint16_t pole_pairs;
  120. } vsc_config_t;
  121. #ifdef __cplusplus
  122. extern "C" {
  123. #endif
  124. /**
  125. * @brief vsc enable or disable
  126. *
  127. * @param[in] vsc VSC base address
  128. * @param[in] enable true-enable, false-disable
  129. */
  130. static inline void vsc_set_enable(VSC_Type *vsc, bool enable)
  131. {
  132. if (enable) {
  133. vsc->ABC_MODE |= VSC_ABC_MODE_ENABLE_VSC_MASK;
  134. } else {
  135. vsc->ABC_MODE &= ~VSC_ABC_MODE_ENABLE_VSC_MASK;
  136. }
  137. }
  138. /**
  139. * @brief vsc config position capture mode
  140. *
  141. * @param[in] vsc VSC base address
  142. * @param[in] mode @ref vsc_pos_cap_mode_t
  143. */
  144. static inline void vsc_config_pos_capture_mode(VSC_Type *vsc, vsc_pos_cap_mode_t mode)
  145. {
  146. vsc->TIMELOCK = (vsc->TIMELOCK & ~(VSC_TIMELOCK_POSITION_CAPTURE_MODE_MASK))
  147. | VSC_TIMELOCK_POSITION_CAPTURE_MODE_SET(mode);
  148. }
  149. /**
  150. * @brief vsc config adc timestamp
  151. *
  152. * @param[in] vsc VSC base address
  153. * @param[in] select timestamp select from value a/b/c. @ref vsc_timestamp_sel_t
  154. * @param[in] num timestamp use which number index of @ref vsc_timestamp_sel_t used.
  155. */
  156. static inline void vsc_config_adc_timestamp(VSC_Type *vsc, vsc_timestamp_sel_t select, uint8_t num)
  157. {
  158. vsc->TIMELOCK = (vsc->TIMELOCK & ~(VSC_TIMELOCK_ADC_TIMESTAMP_SEL_MASK | VSC_TIMELOCK_VALUE_COUNTER_SEL_MASK))
  159. | VSC_TIMELOCK_ADC_TIMESTAMP_SEL_SET(select) | VSC_TIMELOCK_VALUE_COUNTER_SEL_SET(num);
  160. }
  161. /**
  162. * @brief vsc set irq enable or disable
  163. * @param [in] vsc CLC base address
  164. * @param [in] irq_mask irq mask, @ref vsc_irq_mask_t
  165. * @param [in] enable enable or disable
  166. * @arg true enable
  167. * @arg false disable
  168. */
  169. static inline void vsc_set_irq_enable(VSC_Type *vsc, uint32_t irq_mask, bool enable)
  170. {
  171. if (enable) {
  172. vsc->IRQ_ENABLE |= irq_mask;
  173. } else {
  174. vsc->IRQ_ENABLE &= ~irq_mask;
  175. }
  176. }
  177. /**
  178. * @brief vsc get irq status
  179. * @param [in] vsc CLC base address
  180. * @retval irq status.
  181. */
  182. static inline uint32_t vsc_get_irq_status(VSC_Type *vsc)
  183. {
  184. return vsc->IRQ_STATUS;
  185. }
  186. /**
  187. * @brief vsc clear irq status
  188. * @param [in] vsc CLC base address
  189. * @param [in] irq_mask irq mask, @ref vsc_irq_mask_t
  190. */
  191. static inline void vsc_clear_irq_status(VSC_Type *vsc, uint32_t irq_mask)
  192. {
  193. vsc->IRQ_STATUS = irq_mask;
  194. }
  195. /**
  196. * @brief vsc check irq request flag
  197. * @param [in] vsc CLC base address
  198. * @param [in] irq_mask irq mask, @ref vsc_irq_mask_t
  199. * @retval true-has irq req, false-no irq req.
  200. */
  201. static inline bool vsc_get_irq_flag(VSC_Type *vsc, uint32_t irq_mask)
  202. {
  203. return ((vsc->IRQ_STATUS & irq_mask) == irq_mask) ? true : false;
  204. }
  205. /**
  206. * @brief vsc set adc wait cycles
  207. * @param [in] vsc CLC base address
  208. * @param [in] wait_cycle adc wait cycle for exception
  209. */
  210. static inline void vsc_set_adc_wait_cycle(VSC_Type *vsc, uint32_t wait_cycle)
  211. {
  212. vsc->ADC_WAIT_CYCLE = VSC_ADC_WAIT_CYCLE_ADC_WAIT_CYCLE_SET(wait_cycle);
  213. }
  214. /**
  215. * @brief vsc set position wait cycles
  216. * @param [in] vsc CLC base address
  217. * @param [in] wait_cycle position wait cycle for exception
  218. */
  219. static inline void vsc_set_pos_wait_cycle(VSC_Type *vsc, uint32_t wait_cycle)
  220. {
  221. vsc->POS_WAIT_CYCLE = VSC_POS_WAIT_CYCLE_POS_WAIT_CYCLE_SET(wait_cycle);
  222. }
  223. /**
  224. * @brief vsc set abc phase value tolerate
  225. * @param [in] vsc CLC base address
  226. * @param [in] tolerate value a/b/c total value tolerate
  227. */
  228. static inline void vsc_set_adc_tolerate(VSC_Type *vsc, uint32_t tolerate)
  229. {
  230. vsc->ADC_PHASE_TOLERATE = VSC_ADC_PHASE_TOLERATE_ADC_PHASE_TOLERATE_SET(tolerate);
  231. }
  232. /**
  233. * @brief vsc set position pole pairs
  234. * @param [in] vsc CLC base address
  235. * @param [in] pole_pairs pole pairs number
  236. */
  237. static inline void vsc_set_pos_pole_pairs(VSC_Type *vsc, uint32_t pole_pairs)
  238. {
  239. vsc->POS_POLE = (vsc->POS_POLE & ~VSC_POS_POLE_POS_POLE_MASK) | VSC_POS_POLE_POS_POLE_SET(pole_pairs);
  240. }
  241. /**
  242. * @brief vsc set software trigger in
  243. * @param [in] vsc CLC base address
  244. */
  245. static inline void vsc_set_sw_trig_in(VSC_Type *vsc)
  246. {
  247. vsc->TRIGGER_SW = VSC_TRIGGER_SW_TRIGGER_SW_MASK;
  248. }
  249. /**
  250. * @brief vsc get d-axis value
  251. * @param [in] vsc CLC base address
  252. * @param [in] positive_seq true - positive order; false - reverse order
  253. * @retval d-axis value
  254. */
  255. static inline int32_t vsc_get_d_axis_value(VSC_Type *vsc, bool positive_seq)
  256. {
  257. if (positive_seq) {
  258. return (int32_t)vsc->ID_POSEDGE;
  259. } else {
  260. return (int32_t)vsc->ID_NEGEDGE;
  261. }
  262. }
  263. /**
  264. * @brief vsc get q-axis value
  265. * @param [in] vsc CLC base address
  266. * @param [in] positive_seq true - positive order; false - reverse order
  267. * @retval q-axis value
  268. */
  269. static inline int32_t vsc_get_q_axis_value(VSC_Type *vsc, bool positive_seq)
  270. {
  271. if (positive_seq) {
  272. return (int32_t)vsc->IQ_POSEDGE;
  273. } else {
  274. return (int32_t)vsc->IQ_NEGEDGE;
  275. }
  276. }
  277. /**
  278. * @brief vsc get alpha-axis value
  279. * @param [in] vsc CLC base address
  280. * @param [in] positive_seq true - positive order; false - reverse order
  281. * @retval alpha-axis value
  282. */
  283. static inline int32_t vsc_get_alpha_axis_value(VSC_Type *vsc, bool positive_seq)
  284. {
  285. if (positive_seq) {
  286. return (int32_t)vsc->ALPHA_POSEDGE;
  287. } else {
  288. return (int32_t)vsc->ALPHA_NEGEDGE;
  289. }
  290. }
  291. /**
  292. * @brief vsc get beta-axis value
  293. * @param [in] vsc CLC base address
  294. * @param [in] positive_seq true - positive order; false - reverse order
  295. * @retval beta-axis value
  296. */
  297. static inline int32_t vsc_get_beta_axis_value(VSC_Type *vsc, bool positive_seq)
  298. {
  299. if (positive_seq) {
  300. return (int32_t)vsc->BETA_POSEDGE;
  301. } else {
  302. if (vsc->BETA_NEGEDGE == 0x80000000) {
  303. return 0x7FFFFFFF;
  304. } else {
  305. return (int32_t)vsc->BETA_NEGEDGE;
  306. }
  307. }
  308. }
  309. /**
  310. * @brief vsc get adc timestamp value
  311. * @param [in] vsc CLC base address
  312. * @retval adc timestamp value
  313. */
  314. static inline uint32_t vsc_get_adc_timestamp(VSC_Type *vsc)
  315. {
  316. return vsc->TIMESTAMP_LOCKED;
  317. }
  318. /**
  319. * @brief vsc get default config
  320. *
  321. * @param[in] vsc VSC base address
  322. * @param[out] config vsc default config
  323. */
  324. void vsc_get_default_config(VSC_Type *vsc, vsc_config_t *config);
  325. /**
  326. * @brief vsc config initialization
  327. *
  328. * @param[in] vsc VSC base address
  329. * @param[in] config vsc config struct
  330. */
  331. void vsc_config_init(VSC_Type *vsc, vsc_config_t *config);
  332. /**
  333. * @brief vsc software inject phase a/b/c value
  334. *
  335. * @param[in] vsc VSC base address
  336. * @param[in] value_a phase a value
  337. * @param[in] value_b phase b value
  338. * @param[in] value_c phase c value
  339. */
  340. void vsc_sw_inject_abc_value(VSC_Type *vsc, int32_t value_a, int32_t value_b, int32_t value_c);
  341. /**
  342. * @brief vsc software inject position value
  343. *
  344. * @param[in] vsc VSC base address
  345. * @param[in] pos position value
  346. */
  347. void vsc_sw_inject_pos_value(VSC_Type *vsc, uint32_t pos);
  348. #ifdef __cplusplus
  349. }
  350. #endif
  351. /**
  352. * @}
  353. */
  354. #endif /* HPM_VSC_DRV_H */