1 /**************************************************************************//**
2 * @file lptmr_pwm.h
3 * @version V1.01
4 * @brief M2L31 series LPTMR PWM Controller(LPTMR PWM) driver header file
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
8 *****************************************************************************/
9
10 #ifndef __LPTMR_PWM_H__
11 #define __LPTMR_PWM_H__
12
13 #ifdef __cplusplus
14 extern "C"
15 {
16 #endif
17
18
19 /** @addtogroup Standard_Driver Standard Driver
20 @{
21 */
22
23 /** @addtogroup LPTMR_PWM_Driver LPTMR PWM Driver
24 @{
25 */
26
27 /** @addtogroup LPTMR_PWM_EXPORTED_CONSTANTS LPTMR PWM Exported Constants
28 @{
29 */
30 /*---------------------------------------------------------------------------------------------------------*/
31 /* LPTPWM Output Channel Constant Definitions */
32 /*---------------------------------------------------------------------------------------------------------*/
33 #define LPTPWM_CH0 (BIT0) /*!< Indicate PWMx_CH0 \hideinitializer */
34
35 /*---------------------------------------------------------------------------------------------------------*/
36 /* LPTPWM Output Channel Selection Definitions */
37 /*---------------------------------------------------------------------------------------------------------*/
38 #define LPTPWM_TOUT_PIN_FROM_TX (0UL << LPTMR_PWMPOCTL_POSEL_Pos) /*!< Indicate PWMx output to Tx pins \hideinitializer */
39 #define LPTPWM_TOUT_PIN_FROM_TX_EXT (1UL << LPTMR_PWMPOCTL_POSEL_Pos) /*!< Indicate PWMx output to Tx_ext pins \hideinitializer */
40
41 /*---------------------------------------------------------------------------------------------------------*/
42 /* LPTPWM Counter Mode Constant Definitions */
43 /*---------------------------------------------------------------------------------------------------------*/
44 #define LPTPWM_AUTO_RELOAD_MODE (0UL) /*!< Auto-reload mode \hideinitializer */
45 #define LPTPWM_ONE_SHOT_MODE (1UL) /*!< One-shot mode \hideinitializer */
46
47 /*---------------------------------------------------------------------------------------------------------*/
48 /* LPTPWM Trigger LPPDMA Source Select Constant Definitions */
49 /*---------------------------------------------------------------------------------------------------------*/
50 #define LPTPWM_TRIGGER_AT_PERIOD_POINT (0UL << LPTMR_PWMTRGCTL_TRGSEL_Pos) /*!< Timer PWM trigger LPPDMA while counter period point event occurred \hideinitializer */
51 #define LPTPWM_TRIGGER_AT_COMPARE_POINT (1UL << LPTMR_PWMTRGCTL_TRGSEL_Pos) /*!< Timer PWM trigger LPPDMA while counter compare point event occurred \hideinitializer */
52 #define LPTPWM_TRIGGER_AT_PERIOD_OR_COMPARE_POINT (2UL << LPTMR_PWMTRGCTL_TRGSEL_Pos) /*!< Timer PWM trigger LPPDMA while counter period or compare point event occurred \hideinitializer */
53
54 /*---------------------------------------------------------------------------------------------------------*/
55 /* Interrupt Flag Accumulator Constant Definitions */
56 /*---------------------------------------------------------------------------------------------------------*/
57 #define LPTPWM_IFA_PERIOD_POINT (1U) /*!< Low Power Timer PWM counter equal to period \hideinitializer */
58 #define LPTPWM_IFA_COMPARE_UP_COUNT_POINT (2U) /*!< Low Power Timer PWM counter up count to comparator value \hideinitializer */
59
60 /*@}*/ /* end of group LPTMR_PWM_EXPORTED_CONSTANTS */
61
62
63 /** @addtogroup LPTMR_PWM_EXPORTED_FUNCTIONS LPTMR PWM Exported Functions
64 @{
65 */
66
67 /**
68 * @brief Enable LPTPWM Counter Mode
69 *
70 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
71 *
72 * @return None
73 *
74 * @details This macro is used to enable specified LPTMR channel as PWM counter mode, then lptmr counter mode is invalid.
75 * @note All registers about time counter function will be cleared to 0 and lptmr clock source will be changed to PCLKx automatically after executing this macro.
76 * \hideinitializer
77 */
78 #define LPTPWM_ENABLE_PWM_MODE(lptmr) ((lptmr)->CTL |= LPTMR_CTL_FUNCSEL_Msk)
79
80 /**
81 * @brief Disable LPTPWM Counter Mode
82 *
83 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
84 *
85 * @return None
86 *
87 * @details This macro is used to disable specified LPTMR channel as PWM counter mode, then lptmr counter mode is available.
88 * @note All registers about LPTPWM counter function will be cleared to 0 after executing this macro.
89 * \hideinitializer
90 */
91 #define LPTPWM_DISABLE_PWM_MODE(lptmr) ((lptmr)->CTL &= ~LPTMR_CTL_FUNCSEL_Msk)
92
93 /**
94 * @brief Start LPTPWM Counter
95 *
96 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
97 *
98 * @return None
99 *
100 * @details This macro is used to enable LPTPWM generator and start counter counting.
101 * \hideinitializer
102 */
103 #define LPTPWM_START_COUNTER(lptmr) ((lptmr)->PWMCTL |= LPTMR_PWMCTL_CNTEN_Msk)
104
105 /**
106 * @brief Stop LPTPWM Counter
107 *
108 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
109 *
110 * @return None
111 *
112 * @details This macro is used to stop LPTPWM counter after current period is completed.
113 * \hideinitializer
114 */
115 #define LPTPWM_STOP_COUNTER(lptmr) ((lptmr)->PWMPERIOD = 0x0UL)
116
117 /**
118 * @brief Set Counter Clock Prescaler
119 *
120 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
121 *
122 * @param[in] u32Prescaler Clock prescaler of specified channel. Valid values are between 0x0~0xFF.
123 *
124 * @return None
125 *
126 * @details This macro is used to set the prescaler of specified LPTMR PWM.
127 * @note If prescaler is 0, then there is no scaling in counter clock source.
128 * \hideinitializer
129 */
130 #define LPTPWM_SET_PRESCALER(lptmr, u32Prescaler) ((lptmr)->PWMCLKPSC = (u32Prescaler))
131
132 /**
133 * @brief Get Counter Clock Prescaler
134 *
135 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
136 *
137 * @return Target prescaler setting, CLKPSC (LPTMRx_PWMCLKPSC[7:0])
138 *
139 * @details Get the prescaler setting, the target counter clock divider is (CLKPSC + 1).
140 * \hideinitializer
141 */
142 #define LPTPWM_GET_PRESCALER(lptmr) ((lptmr)->PWMCLKPSC)
143
144 /**
145 * @brief Set Counter Period
146 *
147 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
148 *
149 * @param[in] u32Period Period of specified channel. Valid values are between 0x0~0xFFFF.
150 *
151 * @return None
152 *
153 * @details This macro is used to set the period of specified LPTMR PWM.
154 * \hideinitializer
155 */
156 #define LPTPWM_SET_PERIOD(lptmr, u32Period) ((lptmr)->PWMPERIOD = (u32Period))
157
158 /**
159 * @brief Get Counter Period
160 *
161 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
162 *
163 * @return Target period setting, PERIOD (LPTMRx_PWMPERIOD[15:0])
164 *
165 * @details This macro is used to get the period of specified LPTMR PWM.
166 * \hideinitializer
167 */
168 #define LPTPWM_GET_PERIOD(lptmr) ((lptmr)->PWMPERIOD)
169
170 /**
171 * @brief Set Comparator Value
172 *
173 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
174 *
175 * @param[in] u32Cmp Comparator of specified channel. Valid values are between 0x0~0xFFFF.
176 *
177 * @return None
178 *
179 * @details This macro is used to set the comparator value of specified LPTMR PWM.
180 * \hideinitializer
181 */
182 #define LPTPWM_SET_CMPDAT(lptmr, u32Cmp) ((lptmr)->PWMCMPDAT = (u32Cmp))
183
184 /**
185 * @brief Get Comparator Value
186 *
187 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
188 *
189 * @return Target comparator setting, CMPDAT (LPTMRx_PWMCMPDAT[15:0])
190 *
191 * @details This macro is used to get the comparator value of specified LPTMR PWM.
192 * \hideinitializer
193 */
194 #define LPTPWM_GET_CMPDAT(lptmr) ((lptmr)->PWMCMPDAT)
195
196 /**
197 * @brief Clear Counter
198 *
199 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
200 *
201 * @return None
202 *
203 * @details This macro is used to clear counter of specified LPTMR PWM.
204 * \hideinitializer
205 */
206 #define LPTPWM_CLEAR_COUNTER(lptmr) ((lptmr)->PWMCNTCLR = LPTMR_PWMCNTCLR_CNTCLR_Msk)
207
208 /**
209 * @brief Enable Output Function
210 *
211 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
212 *
213 * @param[in] u32Channel Enable specified channel output function. Valid values are:
214 * - \ref LPTPWM_CH0
215 *
216 * @return None
217 *
218 * @details This macro is used to enable output function of specified output pins.
219 * @note If the corresponding bit in u32ChMask parameter is 0, then output function will be disabled in this channel.
220 * \hideinitializer
221 */
222 #define LPTPWM_ENABLE_OUTPUT(lptmr, u32Channel) ((lptmr)->PWMPOCTL = (u32Channel))
223
224 /**
225 * @brief Select Toggle-output Pin
226 *
227 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
228 * @param[in] u32ToutSel Toggle-output pin selection, valid values are:
229 * - \ref LPTPWM_TOUT_PIN_FROM_TX
230 * - \ref LPTPWM_TOUT_PIN_FROM_TX_EXT
231 *
232 * @return None
233 *
234 * @details This macro is used to select LPTPWM toggle-output pin is output on Tx or Tx_EXT pin.
235 */
236 #define LPTPWM_SELECT_TOUT_PIN(lptmr, u32ToutSel) ((lptmr)->PWMPOCTL = ((lptmr)->PWMPOCTL & ~LPTMR_PWMPOCTL_POSEL_Msk) | (u32ToutSel))
237
238 /**
239 * @brief Set Output Inverse
240 *
241 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
242 *
243 * @param[in] u32Channel Set specified channel output is inversed or not. Valid values are:
244 * - \ref LPTPWM_CH0
245 *
246 * @return None
247 *
248 * @details This macro is used to enable output inverse of specified output pins.
249 * @note If u32ChMask parameter is 0, then output inverse function will be disabled.
250 * \hideinitializer
251 */
252 #define LPTPWM_SET_OUTPUT_INVERSE(lptmr, u32Channel) ((lptmr)->PWMPOLCTL = (u32Channel))
253
254 /**
255 * @brief Enable Period Event Interrupt
256 *
257 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
258 *
259 * @return None
260 *
261 * @details This macro is used to enable the period event interrupt function.
262 * \hideinitializer
263 */
264 #define LPTPWM_ENABLE_PERIOD_INT(lptmr) ((lptmr)->PWMINTEN0 |= LPTMR_PWMINTEN0_PIEN_Msk)
265
266 /**
267 * @brief Disable Period Event Interrupt
268 *
269 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
270 *
271 * @return None
272 *
273 * @details This macro is used to disable the period event interrupt function.
274 * \hideinitializer
275 */
276 #define LPTPWM_DISABLE_PERIOD_INT(lptmr) ((lptmr)->PWMINTEN0 &= ~LPTMR_PWMINTEN0_PIEN_Msk)
277
278 /**
279 * @brief Get Period Event Interrupt Flag
280 *
281 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
282 *
283 * @retval 0 Period event interrupt did not occur
284 * @retval 1 Period event interrupt occurred
285 *
286 * @details This macro indicates period event occurred or not.
287 * \hideinitializer
288 */
289 #define LPTPWM_GET_PERIOD_INT_FLAG(lptmr) (((lptmr)->PWMINTSTS0 & LPTMR_PWMINTSTS0_PIF_Msk)? 1 : 0)
290
291 /**
292 * @brief Clear Period Event Interrupt Flag
293 *
294 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
295 *
296 * @return None
297 *
298 * @details This macro clears period event interrupt flag.
299 * \hideinitializer
300 */
301 #define LPTPWM_CLEAR_PERIOD_INT_FLAG(lptmr) ((lptmr)->PWMINTSTS0 = LPTMR_PWMINTSTS0_PIF_Msk)
302
303 /**
304 * @brief Enable Compare Up Event Interrupt
305 *
306 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
307 *
308 * @return None
309 *
310 * @details This macro is used to enable the compare up event interrupt function.
311 * \hideinitializer
312 */
313 #define LPTPWM_ENABLE_CMP_UP_INT(lptmr) ((lptmr)->PWMINTEN0 |= LPTMR_PWMINTEN0_CMPUIEN_Msk)
314
315 /**
316 * @brief Disable Compare Up Event Interrupt
317 *
318 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
319 *
320 * @return None
321 *
322 * @details This macro is used to disable the compare up event interrupt function.
323 * \hideinitializer
324 */
325 #define LPTPWM_DISABLE_CMP_UP_INT(lptmr) ((lptmr)->PWMINTEN0 &= ~LPTMR_PWMINTEN0_CMPUIEN_Msk)
326
327 /**
328 * @brief Get Compare Up Event Interrupt Flag
329 *
330 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
331 *
332 * @retval 0 Compare up event interrupt did not occur
333 * @retval 1 Compare up event interrupt occurred
334 *
335 * @details This macro indicates compare up event occurred or not.
336 * \hideinitializer
337 */
338 #define LPTPWM_GET_CMP_UP_INT_FLAG(lptmr) (((lptmr)->PWMINTSTS0 & LPTMR_PWMINTSTS0_CMPUIF_Msk)? 1 : 0)
339
340 /**
341 * @brief Clear Compare Up Event Interrupt Flag
342 *
343 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
344 *
345 * @return None
346 *
347 * @details This macro clears compare up event interrupt flag.
348 * \hideinitializer
349 */
350 #define LPTPWM_CLEAR_CMP_UP_INT_FLAG(lptmr) ((lptmr)->PWMINTSTS0 = LPTMR_PWMINTSTS0_CMPUIF_Msk)
351
352 /**
353 * @brief Get Counter Reach Maximum Count Status
354 *
355 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
356 *
357 * @retval 0 LPTMR PWM counter never counts to maximum value
358 * @retval 1 LPTMR PWM counter counts to maximum value, 0xFFFF
359 *
360 * @details This macro indicates LPTMR PWM counter has count to 0xFFFF or not.
361 * \hideinitializer
362 */
363 #define LPTPWM_GET_REACH_MAX_CNT_STATUS(lptmr) (((lptmr)->PWMSTATUS & LPTMR_PWMSTATUS_CNTMAXF_Msk)? 1 : 0)
364
365 /**
366 * @brief Clear Counter Reach Maximum Count Status
367 *
368 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
369 *
370 * @return None
371 *
372 * @details This macro clears reach maximum count status.
373 * \hideinitializer
374 */
375 #define LPTPWM_CLEAR_REACH_MAX_CNT_STATUS(lptmr) ((lptmr)->PWMSTATUS = LPTMR_PWMSTATUS_CNTMAXF_Msk)
376
377 uint32_t LPTPWM_ConfigOutputFreqAndDuty(LPTMR_T *lptmr, uint32_t u32Frequency, uint32_t u32DutyCycle);
378 void LPTPWM_EnableCounter(LPTMR_T *lptmr);
379 void LPTPWM_DisableCounter(LPTMR_T *lptmr);
380 void LPTPWM_EnableTrigger(LPTMR_T *lptmr, uint32_t u32TargetMask, uint32_t u32Condition);
381 void LPTPWM_DisableTrigger(LPTMR_T *lptmr, uint32_t u32TargetMask);
382
383
384 /**
385 * @brief Enable Trigger LPPDMA
386 *
387 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
388 * @param[in] u32Condition The condition to trigger LPPDMA. It could be one of following conditions:
389 * - \ref LPTPWM_TRIGGER_AT_PERIOD_POINT
390 * - \ref LPTPWM_TRIGGER_AT_COMPARE_POINT
391 * - \ref LPTPWM_TRIGGER_AT_PERIOD_OR_COMPARE_POINT
392 * @return None
393 *
394 * @details This function is used to enable specified counter compare event to trigger LPPDMA.
395 */
396 #define LPTPWM_EnableTriggerLPPDMA(lptmr, u32Condition) LPTPWM_EnableTrigger(lptmr, LPTMR_PWMTRGCTL_PWMTRGLPPDMA_Msk, u32Condition)
397
398 /**
399 * @brief Disable Trigger LPPDMA
400 *
401 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
402 *
403 * @return None
404 *
405 * @details This function is used to disable counter compare event to trigger LPPDMA.
406 */
407 #define LPTPWM_DisableTriggerLPPDMA(lptmr) LPTPWM_DisableTrigger(lptmr, LPTMR_PWMTRGCTL_PWMTRGLPPDMA_Msk)
408
409 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
410 __STATIC_INLINE void LPTPWM_EnableWakeup(LPTMR_T *lptmr);
411 __STATIC_INLINE void LPTPWM_DisableWakeup(LPTMR_T *lptmr);
412 __STATIC_INLINE uint32_t LPTPWM_GetWakeupFlag(LPTMR_T *lptmr);
413 __STATIC_INLINE void LPTPWM_ClearWakeupFlag(LPTMR_T *lptmr);
414
415 /**
416 * @brief Enable LPTPWM Interrupt Wake-up Function
417 *
418 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
419 *
420 * @return None
421 *
422 * @details This function is used to enable the LPTPWM interrupt Wake-up function and interrupt source could be period interrupt, \n
423 * or compare interrupt.
424 * @note To wake the system from Power-down mode, PCLKx source must be ether LXT or LIRC.
425 * \hideinitializer
426 */
LPTPWM_EnableWakeup(LPTMR_T * lptmr)427 __STATIC_INLINE void LPTPWM_EnableWakeup(LPTMR_T *lptmr)
428 {
429 lptmr->PWMCTL |= LPTMR_PWMCTL_PWMINTWKEN_Msk;
430 }
431
432 /**
433 * @brief Disable LPTPWM Interrupt Wake-up Function
434 *
435 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
436 *
437 * @return None
438 *
439 * @details This function is used to disable the LPTPWM interrupt Wake-up function.
440 * \hideinitializer
441 */
LPTPWM_DisableWakeup(LPTMR_T * lptmr)442 __STATIC_INLINE void LPTPWM_DisableWakeup(LPTMR_T *lptmr)
443 {
444 lptmr->PWMCTL &= ~LPTMR_PWMCTL_PWMINTWKEN_Msk;
445 }
446
447 /**
448 * @brief Get LPTPWM Wake-up Flag
449 *
450 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
451 *
452 * @retval 0 LPTPWM does not cause CPU wake-up
453 * @retval 1 LPTPWM interrupt event cause CPU wake-up
454 *
455 * @details This function indicates LPTPWM interrupt event has waked up system or not.
456 * \hideinitializer
457 */
LPTPWM_GetWakeupFlag(LPTMR_T * lptmr)458 __STATIC_INLINE uint32_t LPTPWM_GetWakeupFlag(LPTMR_T *lptmr)
459 {
460 return ((lptmr->PWMSTATUS & LPTMR_PWMSTATUS_PWMINTWKF_Msk) ? 1 : 0);
461 }
462
463 /**
464 * @brief Clear LPTPWM Wake-up Flag
465 *
466 * @param[in] lptmr The pointer of the specified LPTMR module. It could be LPTMR0, LPTMR1.
467 *
468 * @return None
469 *
470 * @details This function clears LPTPWM Wake-up flag.
471 * \hideinitializer
472 */
LPTPWM_ClearWakeupFlag(LPTMR_T * lptmr)473 __STATIC_INLINE void LPTPWM_ClearWakeupFlag(LPTMR_T *lptmr)
474 {
475 lptmr->PWMSTATUS = LPTMR_PWMSTATUS_PWMINTWKF_Msk;
476 }
477
478 void LPTPWM_EnableAcc(LPTMR_T *lptmr, uint32_t u32IntFlagCnt, uint32_t u32IntAccSrc);
479 void LPTPWM_DisableAcc(LPTMR_T *lptmr);
480 void LPTPWM_EnableAccInt(LPTMR_T *lptmr);
481 void LPTPWM_DisableAccInt(LPTMR_T *lptmr);
482 void LPTPWM_ClearAccInt(LPTMR_T *lptmr);
483 uint32_t LPTPWM_GetAccInt(LPTMR_T *lptmr);
484 void LPTPWM_EnableAccLPPDMA(LPTMR_T *lptmr);
485 void LPTPWM_DisableAccPDMA(LPTMR_T *lptmr);
486 void LPTPWM_EnableAccStopMode(LPTMR_T *lptmr);
487 void LPTPWM_DisableAccStopMode(LPTMR_T *lptmr);
488
489 /*@}*/ /* end of group LPTMR_PWM_EXPORTED_FUNCTIONS */
490
491 /*@}*/ /* end of group LPTMR_PWM_Driver */
492
493 /*@}*/ /* end of group Standard_Driver */
494
495 #ifdef __cplusplus
496 }
497 #endif
498
499 #endif /* __LPTMR_PWM_H__ */
500
501 /*** (C) COPYRIGHT 2020 Nuvoton Technology Corp. ***/
502