hpm_hall_drv.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_HALL_DRV_H
  8. #define HPM_HALL_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_hall_regs.h"
  11. /**
  12. * @brief HALL driver APIs
  13. * @defgroup hall_interface HALL driver APIs
  14. * @ingroup io_interfaces
  15. * @{
  16. *
  17. */
  18. #define HALL_EVENT_WDOG_FLAG_MASK (1U << 31) /**< wdg flag */
  19. #define HALL_EVENT_PHUPT_FLAG_MASK (1U << 30) /**< phupt flag */
  20. #define HALL_EVENT_PHPRE_FLAG_MASK (1U << 29) /**< phpre flag */
  21. #define HALL_EVENT_PHDLYEN_FLAG_MASK (1U << 28) /**< phdly flag */
  22. #define HALL_EVENT_U_FLAG_MASK (1U << 23) /**< u flag */
  23. #define HALL_EVENT_V_FLAG_MASK (1U << 22) /**< v flag */
  24. #define HALL_EVENT_W_FLAG_MASK (1U << 21) /**< w flag */
  25. #define HALL_UVW_STAT_MASK (HALL_COUNT_U_USTAT_MASK | HALL_COUNT_U_VSTAT_MASK | HALL_COUNT_U_WSTAT_MASK)
  26. #define HALL_U_STAT_MASK HALL_COUNT_U_USTAT_MASK
  27. #define HALL_V_STAT_MASK HALL_COUNT_U_VSTAT_MASK
  28. #define HALL_W_STAT_MASK HALL_COUNT_U_WSTAT_MASK
  29. /**
  30. * @brief select delay start time
  31. *
  32. */
  33. typedef enum hall_count_delay_start {
  34. hall_count_delay_start_after_uvw_toggle = 0, /**< start counting delay after u,v,w toggle */
  35. hall_count_delay_start_after_pre_trigger = 1, /**< start counting delay after pre-trigger */
  36. } hall_count_delay_start_t;
  37. /**
  38. * @brief return value of motor movement direction
  39. *
  40. */
  41. typedef enum hall_rotate_direction {
  42. hall_rotate_direction_forward = 0, /**< direction forward */
  43. hall_rotate_direction_reversed = 1 /**< direction reversed */
  44. } hall_rotate_direction_t;
  45. /**
  46. * @brief counter type config
  47. *
  48. */
  49. typedef enum hall_counter_type {
  50. hall_counter_type_w = 0, /**< type w */
  51. hall_counter_type_v = 1, /**< type v */
  52. hall_counter_type_u = 2, /**< type u */
  53. hall_counter_type_timer = 3, /**< type timer */
  54. } hall_counter_type_t;
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. /**
  59. * @brief enable the watchdog
  60. *
  61. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  62. * @param[in] timeout watch dog timeout value , unit is HALL system clock
  63. */
  64. static inline void hall_wdog_enable(HALL_Type *hall_x, uint32_t timeout)
  65. {
  66. hall_x->WDGCFG = HALL_WDGCFG_WDGTO_SET(timeout)
  67. | HALL_WDGCFG_WDGEN_SET(true);
  68. }
  69. /**
  70. * @brief disable the watchdog
  71. *
  72. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  73. */
  74. static inline void hall_wdog_disable(HALL_Type *hall_x)
  75. {
  76. hall_x->WDGCFG = HALL_WDGCFG_WDGEN_SET(false);
  77. }
  78. /**
  79. * @brief delay a certain number of clock cycles after receiving a trigger event
  80. *
  81. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  82. * @param[in] delay_count delay clock cycles number
  83. * @param[in] delay_start select the trigger moment
  84. */
  85. static inline void hall_phase_config(HALL_Type *hall_x, uint32_t delay_count,
  86. hall_count_delay_start_t delay_start)
  87. {
  88. hall_x->PHCFG = HALL_PHCFG_DLYSEL_SET(delay_start)
  89. | HALL_PHCFG_DLYCNT_SET(delay_count);
  90. }
  91. /**
  92. * @brief early trigger configuration
  93. *
  94. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  95. * @param[in] counter the clock cycle number
  96. */
  97. static inline void hall_pre_uvw_transition_config(HALL_Type *hall_x, uint32_t counter)
  98. {
  99. hall_x->UVWCFG = HALL_UVWCFG_PRECNT_SET(counter);
  100. }
  101. /**
  102. * @brief enable trigger event mask
  103. *
  104. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  105. * @param[in] event_mask event mask to be checked
  106. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  107. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  108. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  109. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  110. * @arg @ref HALL_EVENT_U_FLAG_MASK
  111. * @arg @ref HALL_EVENT_V_FLAG_MASK
  112. * @arg @ref HALL_EVENT_W_FLAG_MASK
  113. */
  114. static inline void hall_trigger_output_event_enable(HALL_Type *hall_x,
  115. uint32_t event_mask)
  116. {
  117. hall_x->TRGOEN = (hall_x->TRGOEN & ~event_mask) | event_mask;
  118. }
  119. /**
  120. * @brief disable trigger event mask
  121. *
  122. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  123. * @param[in] event_mask event mask to bo checked
  124. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  125. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  126. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  127. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  128. * @arg @ref HALL_EVENT_U_FLAG_MASK
  129. * @arg @ref HALL_EVENT_V_FLAG_MASK
  130. * @arg @ref HALL_EVENT_W_FLAG_MASK
  131. */
  132. static inline void hall_trigger_output_event_disable(HALL_Type *hall_x,
  133. uint32_t event_mask)
  134. {
  135. hall_x->TRGOEN &= ~event_mask;
  136. }
  137. /**
  138. * @brief enable hardware read event
  139. *
  140. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  141. * @param[in] event_mask read registers flag
  142. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  143. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  144. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  145. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  146. * @arg @ref HALL_EVENT_U_FLAG_MASK
  147. * @arg @ref HALL_EVENT_V_FLAG_MASK
  148. * @arg @ref HALL_EVENT_W_FLAG_MASK
  149. */
  150. static inline void hall_load_read_trigger_event_enable(HALL_Type *hall_x,
  151. uint32_t event_mask)
  152. {
  153. hall_x->READEN = (hall_x->READEN & ~event_mask) | event_mask;
  154. }
  155. /**
  156. * @brief disable hardware read event
  157. *
  158. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  159. * @param[in] event_mask read registers flag
  160. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  161. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  162. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  163. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  164. * @arg @ref HALL_EVENT_U_FLAG_MASK
  165. * @arg @ref HALL_EVENT_V_FLAG_MASK
  166. * @arg @ref HALL_EVENT_W_FLAG_MASK
  167. */
  168. static inline void hall_load_read_trigger_event_disable(HALL_Type *hall_x,
  169. uint32_t event_mask)
  170. {
  171. hall_x->READEN &= ~event_mask;
  172. }
  173. /**
  174. * @brief clear status register
  175. *
  176. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  177. * @param[in] mask hall event flag
  178. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  179. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  180. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  181. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  182. * @arg @ref HALL_EVENT_U_FLAG_MASK
  183. * @arg @ref HALL_EVENT_V_FLAG_MASK
  184. * @arg @ref HALL_EVENT_W_FLAG_MASK
  185. */
  186. static inline void hall_clear_status(HALL_Type *hall_x, uint32_t mask)
  187. {
  188. hall_x->SR = mask;
  189. }
  190. /**
  191. * @brief get status register
  192. *
  193. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  194. * @retval uint32_t value:
  195. * @retval HALL_EVENT_WDOG_FLAG_MASK if watchdog counter timeout
  196. * @retval HALL_EVENT_PHUPT_FLAG_MASK if U/V/W Flip any input signal
  197. * @retval HALL_EVENT_PHPRE_FLAG_MASK if early trigger events occur
  198. * @retval HALL_EVENT_PHDLYEN_FLAG_MASK if time delay events occur
  199. * @retval HALL_EVENT_U_FLAG_MASK if U signal flip
  200. * @retval HALL_EVENT_V_FLAG_MASK if V signal flip
  201. * @retval HALL_EVENT_W_FLAG_MASK if W signal flip
  202. */
  203. static inline uint32_t hall_get_status(HALL_Type *hall_x)
  204. {
  205. return hall_x->SR;
  206. }
  207. /**
  208. * @brief enable irq
  209. *
  210. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  211. * @param[in] mask hall event flag
  212. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  213. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  214. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  215. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  216. * @arg @ref HALL_EVENT_U_FLAG_MASK
  217. * @arg @ref HALL_EVENT_V_FLAG_MASK
  218. * @arg @ref HALL_EVENT_W_FLAG_MASK
  219. */
  220. static inline void hall_irq_enable(HALL_Type *hall_x, uint32_t mask)
  221. {
  222. hall_x->IRQEN = (hall_x->IRQEN & ~mask) | mask;
  223. }
  224. /**
  225. * @brief disable irq
  226. *
  227. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  228. * @param[in] mask hall event flag
  229. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  230. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  231. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  232. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  233. * @arg @ref HALL_EVENT_U_FLAG_MASK
  234. * @arg @ref HALL_EVENT_V_FLAG_MASK
  235. * @arg @ref HALL_EVENT_W_FLAG_MASK
  236. */
  237. static inline void hall_irq_disable(HALL_Type *hall_x, uint32_t mask)
  238. {
  239. hall_x->IRQEN &= ~mask;
  240. }
  241. /**
  242. * @brief enable dma request
  243. *
  244. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  245. * @param[in] mask hall event flag
  246. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  247. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  248. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  249. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  250. * @arg @ref HALL_EVENT_U_FLAG_MASK
  251. * @arg @ref HALL_EVENT_V_FLAG_MASK
  252. * @arg @ref HALL_EVENT_W_FLAG_MASK
  253. */
  254. static inline void hall_dma_request_enable(HALL_Type *hall_x, uint32_t mask)
  255. {
  256. hall_x->DMAEN = (hall_x->DMAEN & ~mask) | mask;
  257. }
  258. /**
  259. * @brief disable dma request
  260. *
  261. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  262. * @param[in] mask hall event flag
  263. * @arg @ref HALL_EVENT_WDOG_FLAG_MASK
  264. * @arg @ref HALL_EVENT_PHUPT_FLAG_MASK
  265. * @arg @ref HALL_EVENT_PHPRE_FLAG_MASK
  266. * @arg @ref HALL_EVENT_PHDLYEN_FLAG_MASK
  267. * @arg @ref HALL_EVENT_U_FLAG_MASK
  268. * @arg @ref HALL_EVENT_V_FLAG_MASK
  269. * @arg @ref HALL_EVENT_W_FLAG_MASK
  270. */
  271. static inline void hall_dma_request_disable(HALL_Type *hall_x, uint32_t mask)
  272. {
  273. hall_x->DMAEN &= ~mask;
  274. }
  275. /**
  276. * @brief get rotate direction
  277. *
  278. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  279. * @retval hall_rotate_direction_t
  280. */
  281. static inline hall_rotate_direction_t hall_get_rotate_direction(HALL_Type *hall_x)
  282. {
  283. return (hall_rotate_direction_t)HALL_COUNT_U_DIR_GET(hall_x->COUNT[HALL_COUNT_CURRENT].U);
  284. }
  285. /**
  286. * @brief get three bits indicate UVW state
  287. *
  288. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  289. * @retval three bits UVW state
  290. */
  291. static inline uint32_t hall_get_current_uvw_stat(HALL_Type *hall_x)
  292. {
  293. return (hall_x->COUNT[HALL_COUNT_CURRENT].U & (HALL_UVW_STAT_MASK)) >> HALL_COUNT_U_WSTAT_SHIFT;
  294. }
  295. /**
  296. * @brief get current count U or V or W
  297. *
  298. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  299. * @param[in] type @ref hall_counter_type_t
  300. * @retval count value
  301. */
  302. static inline uint32_t hall_get_current_count(HALL_Type *hall_x,
  303. hall_counter_type_t type)
  304. {
  305. return *(&hall_x->COUNT[HALL_COUNT_CURRENT].W + type) & HALL_COUNT_U_UCNT_MASK;
  306. }
  307. /**
  308. * @brief get count when read event generated
  309. *
  310. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  311. * @param[in] type hall_counter_type_t
  312. * @retval count value
  313. */
  314. static inline uint32_t hall_get_count_on_read_event(HALL_Type *hall_x,
  315. hall_counter_type_t type)
  316. {
  317. return *(&hall_x->COUNT[HALL_COUNT_READ].W + type);
  318. }
  319. /**
  320. * @brief get count when snap0 event generated
  321. *
  322. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  323. * @param[in] type @ref hall_counter_type_t
  324. * @retval count value
  325. */
  326. static inline uint32_t hall_get_count_on_snap0_event(HALL_Type *hall_x,
  327. hall_counter_type_t type)
  328. {
  329. return *(&hall_x->COUNT[HALL_COUNT_SNAP0].W + type);
  330. }
  331. /**
  332. * @brief get count when snap1 event generated
  333. *
  334. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  335. * @param[in] type @ref hall_counter_type_t
  336. * @retval count value
  337. */
  338. static inline uint32_t hall_get_count_on_snap1_event(HALL_Type *hall_x,
  339. hall_counter_type_t type)
  340. {
  341. return *(&hall_x->COUNT[HALL_COUNT_SNAP1].W + type);
  342. }
  343. /**
  344. * @brief get the history count of u when u signal transition from 0 to 1
  345. *
  346. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  347. * @retval count value
  348. */
  349. static inline uint32_t hall_get_u_history0(HALL_Type *hall_x)
  350. {
  351. return hall_x->HIS[0].HIS0;
  352. }
  353. /**
  354. * @brief get the history count of u when u signal transition from 1 to 0
  355. *
  356. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  357. * @retval count value
  358. */
  359. static inline uint32_t hall_get_u_history1(HALL_Type *hall_x)
  360. {
  361. return hall_x->HIS[0].HIS1;
  362. }
  363. /**
  364. * @brief get the history count of v when v signal transition from 0 to 1
  365. *
  366. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  367. * @retval count value
  368. */
  369. static inline uint32_t hall_get_v_history0(HALL_Type *hall_x)
  370. {
  371. return hall_x->HIS[1].HIS0;
  372. }
  373. /**
  374. * @brief get the history count of v when v signal transition from 1 to 0
  375. *
  376. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  377. * @retval count value
  378. */
  379. static inline uint32_t hall_get_v_history1(HALL_Type *hall_x)
  380. {
  381. return hall_x->HIS[1].HIS1;
  382. }
  383. /**
  384. * @brief get the history count of w when w signal transition from 0 to 1
  385. *
  386. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  387. * @retval count value
  388. */
  389. static inline uint32_t hall_get_w_history0(HALL_Type *hall_x)
  390. {
  391. return hall_x->HIS[2].HIS0;
  392. }
  393. /**
  394. * @brief get the history count of w when w signal transition from 1 to 0
  395. *
  396. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  397. * @retval count value
  398. */
  399. static inline uint32_t hall_get_w_history1(HALL_Type *hall_x)
  400. {
  401. return hall_x->HIS[2].HIS1;
  402. }
  403. /**
  404. * @brief load ucnt, vcnt, wcnt and tmrcnt into their read registers.
  405. * Hardware auto-clear;
  406. *
  407. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  408. */
  409. static inline void hall_load_count_to_read_registers(HALL_Type *hall_x)
  410. {
  411. hall_x->CR |= HALL_CR_READ_MASK;
  412. }
  413. /**
  414. * @brief enable hall snap
  415. *
  416. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  417. */
  418. static inline void hall_snap_enable(HALL_Type *hall_x)
  419. {
  420. hall_x->CR |= HALL_CR_SNAPEN_SET(1);
  421. }
  422. /**
  423. * @brief disable hall snap
  424. *
  425. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  426. */
  427. static inline void hall_snap_disable(HALL_Type *hall_x)
  428. {
  429. hall_x->CR &= ~HALL_CR_SNAPEN_MASK;
  430. }
  431. /**
  432. * @brief reset all counter and related snapshots assert
  433. *
  434. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  435. */
  436. static inline void hall_counter_reset_assert(HALL_Type *hall_x)
  437. {
  438. hall_x->CR |= HALL_CR_RSTCNT_MASK;
  439. }
  440. /**
  441. * @brief reset all counter and related snapshots release
  442. *
  443. * @param[in] hall_x HALL base address HPM_HALLx(x=0..n)
  444. */
  445. static inline void hall_counter_reset_release(HALL_Type *hall_x)
  446. {
  447. hall_x->CR &= ~HALL_CR_RSTCNT_MASK;
  448. }
  449. #ifdef __cplusplus
  450. }
  451. #endif
  452. /**
  453. * @}
  454. */
  455. #endif /* HPM_HALL_DRV_H */