123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- /**************************************************************************//**
- * @file etimer.h
- * @brief N9H30 series ETIMER driver header file
- *
- * @note
- * SPDX-License-Identifier: Apache-2.0
- * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
- *****************************************************************************/
- #ifndef __NU_ETIMER_H__
- #define __NU_ETIMER_H__
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- #include "N9H30.h"
- /** @addtogroup N9H30_Device_Driver N9H30 Device Driver
- @{
- */
- /** @addtogroup N9H30_ETIMER_Driver ETIMER Driver
- @{
- */
- /** @addtogroup N9H30_ETIMER_EXPORTED_CONSTANTS ETIMER Exported Constants
- @{
- */
- #define ETIMER_ONESHOT_MODE (0UL) /*!< Timer working in one shot mode */
- #define ETIMER_PERIODIC_MODE (1UL << 4) /*!< Timer working in periodic mode */
- #define ETIMER_TOGGLE_MODE (2UL << 4) /*!< Timer working in toggle mode */
- #define ETIMER_CONTINUOUS_MODE (3UL << 4) /*!< Timer working in continuous mode */
- #define ETIMER_CAPTURE_FREE_COUNTING_MODE (0UL) /*!< Free counting mode */
- #define ETIMER_CAPTURE_TRIGGER_COUNTING_MODE (1UL << 20) /*!< Trigger counting mode */
- #define ETIMER_CAPTURE_COUNTER_RESET_MODE (1UL << 17) /*!< Counter reset mode */
- #define ETIMER_CAPTURE_FALLING_EDGE (0UL) /*!< Falling edge trigger timer capture */
- #define ETIMER_CAPTURE_RISING_EDGE (1UL << 18) /*!< Rising edge trigger timer capture */
- #define ETIMER_CAPTURE_FALLING_THEN_RISING_EDGE (2UL << 18) /*!< Falling edge then rising edge trigger timer capture */
- #define ETIMER_CAPTURE_RISING_THEN_FALLING_EDGE (3UL << 18) /*!< Rising edge then falling edge trigger timer capture */
- #define ETIMER_TIMEOUT_TRIGGER (0UL) /*!< Timer timeout trigger other modules */
- #define ETIMER_CAPTURE_TRIGGER (1UL << 11) /*!< Timer capture trigger other modules */
- #define ETIMER_COUNTER_RISING_EDGE (1UL << 13) /*!< Counter increase on rising edge */
- #define ETIMER_COUNTER_FALLING_EDGE (0UL) /*!< Counter increase on falling edge */
- /*@}*/ /* end of group ETIMER_EXPORTED_CONSTANTS */
- /** @addtogroup N9H30_ETIMER_EXPORTED_FUNCTIONS ETIMER Exported Functions
- @{
- */
- /**
- * @brief This macro is used to set new Timer compared value
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @param[in] u32Value Timer compare value. Valid values are between 2 to 0xFFFFFF
- * @return None
- * \hideinitializer
- */
- #define ETIMER_SET_CMP_VALUE(timer, u32Value) \
- do{\
- if((timer) == 0) {\
- outpw(REG_ETMR0_CMPR, u32Value);\
- } else if((timer) == 1) {\
- outpw(REG_ETMR1_CMPR, u32Value);\
- } else if((timer) == 2) {\
- outpw(REG_ETMR2_CMPR, u32Value);\
- } else {\
- outpw(REG_ETMR3_CMPR, u32Value);\
- }\
- }while(0)
- /**
- * @brief This macro is used to set new Timer prescale value
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @param[in] u32Value Timer prescale value. Valid values are between 0 to 0xFF
- * @return None
- * @note Clock input is divided by (prescale + 1) before it is fed into timer
- * \hideinitializer
- */
- #define ETIMER_SET_PRESCALE_VALUE(timer, u32Value) \
- do{\
- if((timer) == 0) {\
- outpw(REG_ETMR0_PRECNT, u32Value);\
- } else if((timer) == 1) {\
- outpw(REG_ETMR1_PRECNT, u32Value);\
- } else if((timer) == 2) {\
- outpw(REG_ETMR2_PRECNT, u32Value);\
- } else {\
- outpw(REG_ETMR3_PRECNT, u32Value);\
- }\
- }while(0)
- /**
- * @brief Select Timer operating mode
- *
- * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
- * @param[in] u32OpMode Operation mode. Possible options are
- * - \ref ETIMER_ONESHOT_MODE
- * - \ref ETIMER_PERIODIC_MODE
- * - \ref ETIMER_TOGGLE_MODE
- * - \ref ETIMER_CONTINUOUS_MODE
- *
- * @return None
- * \hideinitializer
- */
- #define ETIMER_SET_OPMODE(timer, u32OpMode) \
- do{\
- if((timer) == 0) {\
- outpw(REG_ETMR0_CTL, (inpw(REG_ETMR0_CTL)&~0x30) | u32OpMode);\
- } else if((timer) == 1) {\
- outpw(REG_ETMR1_CTL, (inpw(REG_ETMR1_CTL)&~0x30) | u32OpMode);\
- } else if((timer) == 2) {\
- outpw(REG_ETMR2_CTL, (inpw(REG_ETMR2_CTL)&~0x30) | u32OpMode);\
- } else {\
- outpw(REG_ETMR3_CTL, (inpw(REG_ETMR3_CTL)&~0x30) | u32OpMode);\
- }\
- }while(0)
- /*
- * @brief This macro is used to check if specify Timer is inactive or active
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return timer is activate or inactivate
- * @retval 0 Timer 24-bit up counter is inactive
- * @retval 1 Timer 24-bit up counter is active
- * \hideinitializer
- */
- static __inline int ETIMER_Is_Active(UINT timer)
- {
- int reg;
- if (timer == 0)
- {
- reg = inpw(REG_ETMR0_CTL);
- }
- else if (timer == 1)
- {
- reg = inpw(REG_ETMR1_CTL);
- }
- else if (timer == 2)
- {
- reg = inpw(REG_ETMR2_CTL);
- }
- else
- {
- reg = inpw(REG_ETMR3_CTL);
- }
- return reg & 0x80 ? 1 : 0;
- }
- /**
- * @brief This function is used to start Timer counting
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_Start(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) | 1);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) | 1);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) | 1);
- }
- else
- {
- outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) | 1);
- }
- }
- /**
- * @brief This function is used to stop Timer counting
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_Stop(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~1);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~1);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~1);
- }
- else
- {
- outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~1);
- }
- }
- /**
- * @brief This function is used to enable the Timer wake-up function
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- * @note To wake the system from power down mode, timer clock source must be ether LXT or LIRC
- */
- static __inline void ETIMER_EnableWakeup(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) | 4);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) | 4);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) | 4);
- }
- else
- {
- outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) | 4);
- }
- }
- /**
- * @brief This function is used to disable the Timer wake-up function
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_DisableWakeup(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~4);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~4);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~4);
- }
- else
- {
- outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~4);
- }
- }
- /**
- * @brief This function is used to enable the capture pin detection de-bounce function.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_EnableCaptureDebounce(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) | 0x400000);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) | 0x400000);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) | 0x400000);
- }
- else
- {
- outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) | 0x400000);
- }
- }
- /**
- * @brief This function is used to disable the capture pin detection de-bounce function.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_DisableCaptureDebounce(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_CTL, inpw(REG_ETMR0_CTL) & ~0x400000);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_CTL, inpw(REG_ETMR1_CTL) & ~0x400000);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_CTL, inpw(REG_ETMR2_CTL) & ~0x400000);
- }
- else
- {
- outpw(REG_ETMR3_CTL, inpw(REG_ETMR3_CTL) & ~0x400000);
- }
- }
- /**
- * @brief This function is used to enable the Timer time-out interrupt function.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_EnableInt(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_IER, inpw(REG_ETMR0_IER) | 1);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_IER, inpw(REG_ETMR1_IER) | 1);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_IER, inpw(REG_ETMR2_IER) | 1);
- }
- else
- {
- outpw(REG_ETMR3_IER, inpw(REG_ETMR3_IER) | 1);
- }
- }
- /**
- * @brief This function is used to disable the Timer time-out interrupt function.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_DisableInt(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_IER, inpw(REG_ETMR0_IER) & ~1);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_IER, inpw(REG_ETMR1_IER) & ~1);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_IER, inpw(REG_ETMR2_IER) & ~1);
- }
- else
- {
- outpw(REG_ETMR3_IER, inpw(REG_ETMR3_IER) & ~1);
- }
- }
- /**
- * @brief This function is used to enable the Timer capture trigger interrupt function.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_EnableCaptureInt(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_IER, inpw(REG_ETMR0_IER) | 2);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_IER, inpw(REG_ETMR1_IER) | 2);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_IER, inpw(REG_ETMR2_IER) | 2);
- }
- else
- {
- outpw(REG_ETMR3_IER, inpw(REG_ETMR3_IER) | 2);
- }
- }
- /**
- * @brief This function is used to disable the Timer capture trigger interrupt function.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_DisableCaptureInt(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_IER, inpw(REG_ETMR0_IER) & ~2);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_IER, inpw(REG_ETMR1_IER) & ~2);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_IER, inpw(REG_ETMR2_IER) & ~2);
- }
- else
- {
- outpw(REG_ETMR3_IER, inpw(REG_ETMR3_IER) & ~2);
- }
- }
- /**
- * @brief This function indicates Timer time-out interrupt occurred or not.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return Timer time-out interrupt occurred or not
- * @retval 0 Timer time-out interrupt did not occur
- * @retval 1 Timer time-out interrupt occurred
- */
- static __inline UINT ETIMER_GetIntFlag(UINT timer)
- {
- int reg;
- if (timer == 0)
- {
- reg = inpw(REG_ETMR0_ISR);
- }
- else if (timer == 1)
- {
- reg = inpw(REG_ETMR1_ISR);
- }
- else if (timer == 2)
- {
- reg = inpw(REG_ETMR2_ISR);
- }
- else
- {
- reg = inpw(REG_ETMR3_ISR);
- }
- return reg & 1;
- }
- /**
- * @brief This function clears the Timer time-out interrupt flag.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_ClearIntFlag(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_ISR, 1);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_ISR, 1);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_ISR, 1);
- }
- else
- {
- outpw(REG_ETMR3_ISR, 1);
- }
- }
- /**
- * @brief This function indicates Timer capture interrupt occurred or not.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return Timer capture interrupt occurred or not
- * @retval 0 Timer capture interrupt did not occur
- * @retval 1 Timer capture interrupt occurred
- */
- static __inline UINT ETIMER_GetCaptureIntFlag(UINT timer)
- {
- int reg;
- if (timer == 0)
- {
- reg = inpw(REG_ETMR0_ISR);
- }
- else if (timer == 1)
- {
- reg = inpw(REG_ETMR1_ISR);
- }
- else if (timer == 2)
- {
- reg = inpw(REG_ETMR2_ISR);
- }
- else
- {
- reg = inpw(REG_ETMR3_ISR);
- }
- return (reg & 2) >> 1;
- }
- /**
- * @brief This function clears the Timer capture interrupt flag.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_ClearCaptureIntFlag(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_ISR, 2);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_ISR, 2);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_ISR, 2);
- }
- else
- {
- outpw(REG_ETMR3_ISR, 2);
- }
- }
- /**
- * @brief This function gets the Timer capture falling edge flag.
- * @param[in] timer ETIMER number. Range from 0 ~ 5
- * @return None
- */
- static __inline UINT8 ETIMER_GetCaptureFallingEdgeFlag(UINT timer)
- {
- UINT ret;
- if (timer == 0)
- {
- ret = inpw(REG_ETMR0_ISR);
- }
- else if (timer == 1)
- {
- ret = inpw(REG_ETMR1_ISR);
- }
- else if (timer == 2)
- {
- ret = inpw(REG_ETMR2_ISR);
- }
- else
- {
- ret = inpw(REG_ETMR3_ISR);
- }
- return (ret & (1 << 6)) >> 6;
- }
- /*
- * @brief This function indicates Timer has waked up system or not.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return Timer has waked up system or not
- * @retval 0 Timer did not wake up system
- * @retval 1 Timer wake up system
- */
- static __inline UINT ETIMER_GetWakeupFlag(UINT timer)
- {
- int reg;
- if (timer == 0)
- {
- reg = inpw(REG_ETMR0_ISR);
- }
- else if (timer == 1)
- {
- reg = inpw(REG_ETMR1_ISR);
- }
- else if (timer == 2)
- {
- reg = inpw(REG_ETMR2_ISR);
- }
- else
- {
- reg = inpw(REG_ETMR3_ISR);
- }
- return (reg & 0x10) >> 4;
- }
- /**
- * @brief This function clears the Timer wakeup interrupt flag.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return None
- */
- static __inline void ETIMER_ClearWakeupFlag(UINT timer)
- {
- if (timer == 0)
- {
- outpw(REG_ETMR0_ISR, 0x10);
- }
- else if (timer == 1)
- {
- outpw(REG_ETMR1_ISR, 0x10);
- }
- else if (timer == 2)
- {
- outpw(REG_ETMR2_ISR, 0x10);
- }
- else
- {
- outpw(REG_ETMR3_ISR, 0x10);
- }
- }
- /**
- * @brief This function gets the Timer compare value.
- * @param[in] timer ETIMER number. Range from 0 ~ 5
- * @return Timer compare data value
- */
- static __inline UINT ETIMER_GetCompareData(UINT timer)
- {
- if (timer == 0)
- {
- return inpw(REG_ETMR0_CMPR);
- }
- else if (timer == 1)
- {
- return inpw(REG_ETMR1_CMPR);
- }
- else if (timer == 2)
- {
- return inpw(REG_ETMR2_CMPR);
- }
- else
- {
- return inpw(REG_ETMR3_CMPR);
- }
- }
- /**
- * @brief This function gets the Timer capture data.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return Timer capture data value
- */
- static __inline UINT ETIMER_GetCaptureData(UINT timer)
- {
- if (timer == 0)
- {
- return inpw(REG_ETMR0_TCAP);
- }
- else if (timer == 1)
- {
- return inpw(REG_ETMR1_TCAP);
- }
- else if (timer == 2)
- {
- return inpw(REG_ETMR2_TCAP);
- }
- else
- {
- return inpw(REG_ETMR3_TCAP);
- }
- }
- /**
- * @brief This function reports the current timer counter value.
- * @param[in] timer ETIMER number. Range from 0 ~ 3
- * @return Timer counter value
- */
- static __inline UINT ETIMER_GetCounter(UINT timer)
- {
- if (timer == 0)
- {
- return inpw(REG_ETMR0_DR);
- }
- else if (timer == 1)
- {
- return inpw(REG_ETMR1_DR);
- }
- else if (timer == 2)
- {
- return inpw(REG_ETMR2_DR);
- }
- else
- {
- return inpw(REG_ETMR3_DR);
- }
- }
- static __inline UINT ETIMER_ClearCounter(UINT timer)
- {
- if (timer == 0)
- {
- return outpw(REG_ETMR0_DR, 0);
- }
- else if (timer == 1)
- {
- return outpw(REG_ETMR1_DR, 0);
- }
- else if (timer == 2)
- {
- return outpw(REG_ETMR2_DR, 0);
- }
- else
- {
- return outpw(REG_ETMR3_DR, 0);
- }
- }
- UINT ETIMER_Open(UINT timer, UINT u32Mode, UINT u32Freq);
- void ETIMER_Close(UINT timer);
- void ETIMER_Delay(UINT timer, UINT u32Usec);
- void ETIMER_EnableCapture(UINT timer, UINT u32CapMode, UINT u32Edge);
- void ETIMER_DisableCapture(UINT timer);
- UINT ETIMER_GetModuleClock(UINT timer);
- /*@}*/ /* end of group N9H30_ETIMER_EXPORTED_FUNCTIONS */
- /*@}*/ /* end of group N9H30_ETIMER_Driver */
- /*@}*/ /* end of group N9H30_Device_Driver */
- #ifdef __cplusplus
- }
- #endif
- #endif //__NU_ETIMER_H__
- /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/
|