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