hpm_pdgo_drv.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_PDGO_DRV_H
  8. #define HPM_PDGO_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_pdgo_regs.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #define DGO_GPR_WORD_COUNT (4U) /*!< DGO GPR register count */
  15. #define DGO_WAKEUP_COUNTER_TICKS_PER_SEC (32768UL) /*!< DGO Wakeup Counter frequency */
  16. #define DGO_TURNOFF_COUNTER_TICKS_PER_SEC (24000000UL) /*!< DGO Turn-off counter frequency */
  17. #define DGO_WAKEUP_TICK_IN_US (1000000UL / DGO_WAKEUP_COUNTER_TICKS_PER_SEC)
  18. #define DGO_TURNOFF_TICKS_PER_US (DGO_TURNOFF_COUNTER_TICKS_PER_SEC / 1000000UL)
  19. /**
  20. *
  21. * @brief PDGO driver APIs
  22. * @defgroup pdgo_interface DGO driver APIs
  23. * @ingroup pdgo_interfaces
  24. * @{
  25. *
  26. */
  27. /**
  28. * @brief Set DGO turn-off counter
  29. * @param [in] ptr DGO base address
  30. * @param [in] counter Turn-off counter value. Clock source is 32K
  31. */
  32. static inline void pdgo_set_turnoff_counter(PDGO_Type *ptr, uint32_t counter)
  33. {
  34. ptr->DGO_TURNOFF = counter;
  35. }
  36. /**
  37. * @brief Enable Software Wake-up feature on DGO
  38. * @param [in] ptr DGO base address
  39. */
  40. static inline void pdgo_enable_software_wakeup(PDGO_Type *ptr)
  41. {
  42. ptr->DGO_CTR1 |= PDGO_DGO_CTR1_WAKEUP_EN_MASK;
  43. }
  44. /**
  45. * @brief Disable Software Wake-up feature on DGO
  46. * @param [in] ptr DGO base address
  47. */
  48. static inline void pdgo_disable_software_wakeup(PDGO_Type *ptr)
  49. {
  50. ptr->DGO_CTR1 &= ~PDGO_DGO_CTR1_WAKEUP_EN_MASK;
  51. }
  52. /**
  53. * @brief Set DGO to one-shot wakeup mode
  54. * @param [in] ptr DGO base address
  55. */
  56. static inline void pdgo_enable_oneshot_wakeup(PDGO_Type *ptr)
  57. {
  58. ptr->DGO_CTR1 &= ~PDGO_DGO_CTR1_AOTO_SYS_WAKEUP_MASK;
  59. }
  60. /**
  61. * @brief Enable DGO register retention mode
  62. * @param [in] ptr DGO base address
  63. */
  64. static inline void pdgo_enable_retention_mode(PDGO_Type *ptr)
  65. {
  66. ptr->DGO_CTR1 |= PDGO_DGO_CTR0_RETENTION_MASK;
  67. }
  68. /**
  69. * @brief Check whether the DGO retention mode is enabled or not
  70. * @param [in] ptr DGO base address
  71. *
  72. * @retval true Retention mode is enabled
  73. * @retval false Retention mode is disabled
  74. */
  75. static inline bool pdgo_is_retention_mode_enabled(PDGO_Type *ptr)
  76. {
  77. return ((ptr->DGO_CTR1 & PDGO_DGO_CTR0_RETENTION_MASK) != 0U);
  78. }
  79. /**
  80. * @brief Disable DGO register retention mode
  81. * @param [in] ptr DGO base address
  82. */
  83. static inline void pdgo_disable_retention_mode(PDGO_Type *ptr)
  84. {
  85. ptr->DGO_CTR1 &= ~PDGO_DGO_CTR0_RETENTION_MASK;
  86. }
  87. /**
  88. * @brief Set DGO to automatic wakeup mode
  89. * @param [in] ptr DGO base address
  90. */
  91. static inline void pdgo_enable_auto_wakeup(PDGO_Type *ptr)
  92. {
  93. ptr->DGO_CTR1 |= PDGO_DGO_CTR1_AOTO_SYS_WAKEUP_MASK;
  94. }
  95. #if defined(PDGO_SUPPORT_SYS_WAKEUP_STATUS) && (PDGO_SUPPORT_SYS_WAKEUP_STATUS == 1)
  96. /**
  97. * @brief Check whether DGO is waked up by System/Software
  98. * @param [in] ptr DGO base address
  99. *
  100. * @retval true if DGO is waked up by System/Software
  101. */
  102. static inline bool pdgo_is_system_wakeup(PDGO_Type *ptr)
  103. {
  104. return ((ptr->DGO_CTR1 & PDGO_DGO_CTR1_SYS_WAKEUP_STATUS_MASK) != 0U);
  105. }
  106. #endif
  107. /**
  108. * @brief Check whether DGO is waked up by Wake-up/Reset Pin
  109. * @param [in] ptr DGO base address
  110. *
  111. * @retval true if DGO is waked up by Wakeup/Reset pin
  112. */
  113. static inline bool pdgo_is_pin_wakeup(PDGO_Type *ptr)
  114. {
  115. return ((ptr->DGO_CTR1 & PDGO_DGO_CTR1_PIN_WAKEUP_STATUS_MASK) != 0U);
  116. }
  117. /**
  118. * @brief Check whether Auto wake-up is enabled
  119. * @param [in] ptr DGO base address
  120. *
  121. * @retval true - Auto wake-up is enabled
  122. * @retval false - Auto wake-up is disabled
  123. */
  124. static inline bool pdgo_is_auto_wakeup_enabled(PDGO_Type *ptr)
  125. {
  126. return ((ptr->DGO_CTR1 & PDGO_DGO_CTR1_AOTO_SYS_WAKEUP_MASK) != 0U);
  127. }
  128. /**
  129. * @brief Enable pull-up resistor for Reset Pin
  130. * [in] ptr DGO base address
  131. */
  132. static inline void pdgo_enable_pullup_resistor_for_reset_pin(PDGO_Type *ptr)
  133. {
  134. ptr->DGO_CTR2 &= ~PDGO_DGO_CTR2_RESETN_PULLUP_DISABLE_MASK;
  135. }
  136. /**
  137. * @brief Disable pull-up resistor for Reset Pin
  138. * [in] ptr DGO base address
  139. */
  140. static inline void pdgo_disable_pullup_resistor_for_reset_pin(PDGO_Type *ptr)
  141. {
  142. ptr->DGO_CTR2 |= PDGO_DGO_CTR2_RESETN_PULLUP_DISABLE_MASK;
  143. }
  144. /**
  145. * Enable pull-down resistor for Wakeup pin
  146. * [in] ptr DGO base address
  147. */
  148. static inline void pdgo_enable_pulldown_resistor_for_wakeup_pin(PDGO_Type *ptr)
  149. {
  150. ptr->DGO_CTR2 &= ~PDGO_DGO_CTR2_WAKEUP_PULLDN_DISABLE_MASK;
  151. }
  152. /**
  153. * Disable pull-down resistor for Wakeup pin
  154. * [in] ptr DGO base address
  155. */
  156. static inline void pdgo_disable_pulldown_resistor_for_wakeup_pin(PDGO_Type *ptr)
  157. {
  158. ptr->DGO_CTR2 |= PDGO_DGO_CTR2_WAKEUP_PULLDN_DISABLE_MASK;
  159. }
  160. /**
  161. * @brief Set DGO wakeup counter
  162. * @param [in] ptr DGO base address
  163. * @param [in] wakeup_ctr Wakeup counter value. clock source is 32K
  164. */
  165. static inline void pdgo_set_wakeup_counter(PDGO_Type *ptr, uint32_t wakeup_ctr)
  166. {
  167. ptr->DGO_CTR3 = wakeup_ctr;
  168. }
  169. /**
  170. * @brief Get DGO wakeup counter value
  171. * @param [in] ptr DGO base address
  172. *
  173. * @return DGO wakeup counter value
  174. */
  175. static inline uint32_t pdgo_get_wakeup_counter(PDGO_Type *ptr)
  176. {
  177. return ptr->DGO_CTR3;
  178. }
  179. /**
  180. * @brief Write data to DGO GPR register
  181. * @param [in] ptr DGO base address
  182. * @param [in] index GPR register index
  183. * @param [in] content Data to be written to DGO GPR register
  184. */
  185. static inline void pdgo_write_gpr(PDGO_Type *ptr, uint32_t index, uint32_t content)
  186. {
  187. if (index < DGO_GPR_WORD_COUNT) {
  188. *(volatile uint32_t *) ((uint32_t) &ptr->DGO_GPR00 + index * 4) = content;
  189. }
  190. }
  191. /**
  192. * @brief Read data from DGO GPR register
  193. * @param [in] ptr DGO base address
  194. * @param [in] index GPR register index
  195. *
  196. * @return DGO GPR register value
  197. */
  198. static inline uint32_t pdgo_read_gpr(PDGO_Type *ptr, uint32_t index)
  199. {
  200. uint32_t reg_val = 0;
  201. if (index < DGO_GPR_WORD_COUNT) {
  202. reg_val = *(volatile uint32_t *) ((uint32_t) &ptr->DGO_GPR00 + index * 4);
  203. }
  204. return reg_val;
  205. }
  206. /**
  207. * @brief Convert the microsecond to DGO Wake-up counter value
  208. * @param [in] us microsecond to be converted
  209. *
  210. * @return Converted DGO Wake-up counter value
  211. */
  212. static inline uint32_t pdgo_get_wakeup_counter_from_us(uint32_t us)
  213. {
  214. return (us + DGO_WAKEUP_TICK_IN_US - 1U) / DGO_WAKEUP_TICK_IN_US;
  215. }
  216. /**
  217. * @brief Convert the DGO Wake-up counter to microseconds
  218. * @param [in] counter DGO counter
  219. *
  220. * @return Converted microseconds
  221. */
  222. static inline uint32_t pdgo_get_us_from_wakeup_counter(uint32_t counter)
  223. {
  224. return (counter * DGO_WAKEUP_TICK_IN_US);
  225. }
  226. /**
  227. * @brief Convert the microsecond to DGO Turn-off counter value
  228. * @param [in] us microsecond to be converted
  229. *
  230. * @return Converted DGO Turn-off counter value
  231. */
  232. static inline uint32_t pdgo_get_turnoff_counter_from_us(uint32_t us)
  233. {
  234. return (us * DGO_TURNOFF_TICKS_PER_US);
  235. }
  236. /**
  237. * @brief Convert the DGO Turn-off counter to microseconds
  238. * @param [in] counter DGO Turn-off counter
  239. *
  240. * @return Converted microseconds
  241. */
  242. static inline uint32_t pdgo_get_us_from_turnoff_counter(uint32_t counter)
  243. {
  244. return (counter + DGO_TURNOFF_TICKS_PER_US - 1U) / DGO_TURNOFF_TICKS_PER_US;
  245. }
  246. /**
  247. * @}
  248. */
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #endif /* HPM_DGO_DRV_H */