]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Nuvoton_Code/StdDriver/src/timer.c
Add Cortex M23 GCC and IAR ports. Add demo projects for Nuvoton NuMaker-PFM-2351.
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC / Nuvoton_Code / StdDriver / src / timer.c
diff --git a/FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Nuvoton_Code/StdDriver/src/timer.c b/FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Nuvoton_Code/StdDriver/src/timer.c
new file mode 100644 (file)
index 0000000..c088644
--- /dev/null
@@ -0,0 +1,395 @@
+/**************************************************************************//**\r
+ * @file     timer.c\r
+ * @version  V3.00\r
+ * @brief    Timer Controller(Timer) driver source file\r
+ *\r
+ * @copyright (C) 2017 Nuvoton Technology Corp. All rights reserved.\r
+*****************************************************************************/\r
+#include "NuMicro.h"\r
+\r
+\r
+/** @addtogroup Standard_Driver Standard Driver\r
+  @{\r
+*/\r
+\r
+/** @addtogroup TIMER_Driver TIMER Driver\r
+  @{\r
+*/\r
+\r
+/** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions\r
+  @{\r
+*/\r
+\r
+/**\r
+  * @brief      Open Timer with Operate Mode and Frequency\r
+  *\r
+  * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  * @param[in]  u32Mode     Operation mode. Possible options are\r
+  *                         - \ref TIMER_ONESHOT_MODE\r
+  *                         - \ref TIMER_PERIODIC_MODE\r
+  *                         - \ref TIMER_TOGGLE_MODE\r
+  *                         - \ref TIMER_CONTINUOUS_MODE\r
+  * @param[in]  u32Freq     Target working frequency\r
+  *\r
+  * @return     Real timer working frequency\r
+  *\r
+  * @details    This API is used to configure timer to operate in specified mode and frequency.\r
+  *             If timer cannot work in target frequency, a closest frequency will be chose and returned.\r
+  * @note       After calling this API, Timer is \b NOT running yet. But could start timer running be calling\r
+  *             \ref TIMER_Start macro or program registers directly.\r
+  */\r
+uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)\r
+{\r
+    uint32_t u32Clk = TIMER_GetModuleClock(timer);\r
+    uint32_t u32Cmpr = 0UL, u32Prescale = 0UL;\r
+\r
+    /* Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, prescaler = 0. */\r
+    if(u32Freq > (u32Clk / 2UL))\r
+    {\r
+        u32Cmpr = 2UL;\r
+    }\r
+    else\r
+    {\r
+        u32Cmpr = u32Clk / u32Freq;\r
+        u32Prescale = (u32Cmpr >> 24);  /* for 24 bits CMPDAT */\r
+        if (u32Prescale > 0UL)\r
+            u32Cmpr = u32Cmpr / (u32Prescale + 1UL);\r
+    }\r
+\r
+    timer->CTL = u32Mode | u32Prescale;\r
+    timer->CMP = u32Cmpr;\r
+\r
+    return(u32Clk / (u32Cmpr * (u32Prescale + 1UL)));\r
+}\r
+\r
+/**\r
+  * @brief      Stop Timer Counting\r
+  *\r
+  * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  *\r
+  * @return     None\r
+  *\r
+  * @details    This API stops timer counting and disable all timer interrupt function.\r
+  */\r
+void TIMER_Close(TIMER_T *timer)\r
+{\r
+    timer->CTL = 0UL;\r
+    timer->EXTCTL = 0UL;\r
+}\r
+\r
+/**\r
+  * @brief      Create a specify Delay Time\r
+  *\r
+  * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  * @param[in]  u32Usec     Delay period in micro seconds. Valid values are between 100~1000000 (100 micro second ~ 1 second).\r
+  *\r
+  * @return     None\r
+  *\r
+  * @details    This API is used to create a delay loop for u32usec micro seconds by using timer one-shot mode.\r
+  * @note       This API overwrites the register setting of the timer used to count the delay time.\r
+  * @note       This API use polling mode. So there is no need to enable interrupt for the timer module used to generate delay.\r
+  */\r
+void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)\r
+{\r
+    uint32_t u32Clk = TIMER_GetModuleClock(timer);\r
+    uint32_t u32Prescale = 0UL, u32Delay = (SystemCoreClock / u32Clk) + 1UL;\r
+    uint32_t u32Cmpr, u32NsecPerTick;\r
+\r
+    /* Clear current timer configuration */\r
+    timer->CTL = 0UL;\r
+    timer->EXTCTL = 0UL;\r
+\r
+    if(u32Clk <= 1000000UL)   /* min delay is 1000 us if timer clock source is <= 1 MHz */\r
+    {\r
+        if(u32Usec < 1000UL)\r
+        {\r
+            u32Usec = 1000UL;\r
+        }\r
+        if(u32Usec > 1000000UL)\r
+        {\r
+            u32Usec = 1000000UL;\r
+        }\r
+    }\r
+    else\r
+    {\r
+        if(u32Usec < 100UL)\r
+        {\r
+            u32Usec = 100UL;\r
+        }\r
+        if(u32Usec > 1000000UL)\r
+        {\r
+            u32Usec = 1000000UL;\r
+        }\r
+    }\r
+\r
+    if(u32Clk <= 1000000UL)\r
+    {\r
+        u32Prescale = 0UL;\r
+        u32NsecPerTick = 1000000000UL / u32Clk;\r
+        u32Cmpr = (u32Usec * 1000UL) / u32NsecPerTick;\r
+    }\r
+    else\r
+    {\r
+        u32Cmpr = u32Usec * (u32Clk / 1000000UL);\r
+        u32Prescale = (u32Cmpr >> 24);  /* for 24 bits CMPDAT */\r
+        if (u32Prescale > 0UL)\r
+            u32Cmpr = u32Cmpr / (u32Prescale + 1UL);\r
+    }\r
+\r
+    timer->CMP = u32Cmpr;\r
+    timer->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE | u32Prescale;\r
+\r
+    /*\r
+        When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.\r
+        And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag.\r
+    */\r
+    for(; u32Delay > 0UL; u32Delay--)\r
+    {\r
+        __NOP();\r
+    }\r
+\r
+    while(timer->CTL & TIMER_CTL_ACTSTS_Msk) {}\r
+}\r
+\r
+/**\r
+  * @brief      Enable Timer Capture Function\r
+  *\r
+  * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  * @param[in]  u32CapMode  Timer capture mode. Could be\r
+  *                         - \ref TIMER_CAPTURE_FREE_COUNTING_MODE\r
+  *                         - \ref TIMER_CAPTURE_COUNTER_RESET_MODE\r
+  * @param[in]  u32Edge     Timer capture trigger edge. Possible values are\r
+  *                         - \ref TIMER_CAPTURE_EVENT_FALLING\r
+  *                         - \ref TIMER_CAPTURE_EVENT_RISING\r
+  *                         - \ref TIMER_CAPTURE_EVENT_FALLING_RISING\r
+  *                         - \ref TIMER_CAPTURE_EVENT_RISING_FALLING\r
+  *                         - \ref TIMER_CAPTURE_EVENT_GET_LOW_PERIOD\r
+  *                         - \ref TIMER_CAPTURE_EVENT_GET_HIGH_PERIOD\r
+  *\r
+  * @return     None\r
+  *\r
+  * @details    This API is used to enable timer capture function with specify capture trigger edge \n\r
+  *             to get current counter value or reset counter value to 0.\r
+  * @note       Timer frequency should be configured separately by using \ref TIMER_Open API, or program registers directly.\r
+  */\r
+void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)\r
+{\r
+    timer->EXTCTL = (timer->EXTCTL & ~(TIMER_EXTCTL_CAPFUNCS_Msk | TIMER_EXTCTL_CAPEDGE_Msk)) |\r
+                    u32CapMode | u32Edge | TIMER_EXTCTL_CAPEN_Msk;\r
+}\r
+\r
+/**\r
+  * @brief      Disable Timer Capture Function\r
+  *\r
+  * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  *\r
+  * @return     None\r
+  *\r
+  * @details    This API is used to disable the timer capture function.\r
+  */\r
+void TIMER_DisableCapture(TIMER_T *timer)\r
+{\r
+    timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;\r
+}\r
+\r
+/**\r
+  * @brief      Enable Timer Counter Function\r
+  *\r
+  * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  * @param[in]  u32Edge     Detection edge of counter pin. Could be ether\r
+  *                         - \ref TIMER_COUNTER_EVENT_FALLING, or\r
+  *                         - \ref TIMER_COUNTER_EVENT_RISING\r
+  *\r
+  * @return     None\r
+  *\r
+  * @details    This function is used to enable the timer counter function with specify detection edge.\r
+  * @note       Timer compare value should be configured separately by using \ref TIMER_SET_CMP_VALUE macro or program registers directly.\r
+  * @note       While using event counter function, \ref TIMER_TOGGLE_MODE cannot set as timer operation mode.\r
+  */\r
+void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)\r
+{\r
+    timer->EXTCTL = (timer->EXTCTL & ~TIMER_EXTCTL_CNTPHASE_Msk) | u32Edge;\r
+    timer->CTL |= TIMER_CTL_EXTCNTEN_Msk;\r
+}\r
+\r
+/**\r
+  * @brief      Disable Timer Counter Function\r
+  *\r
+  * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  *\r
+  * @return     None\r
+  *\r
+  * @details    This API is used to disable the timer event counter function.\r
+  */\r
+void TIMER_DisableEventCounter(TIMER_T *timer)\r
+{\r
+    timer->CTL &= ~TIMER_CTL_EXTCNTEN_Msk;\r
+}\r
+\r
+/**\r
+  * @brief      Get Timer Clock Frequency\r
+  *\r
+  * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  *\r
+  * @return     Timer clock frequency\r
+  *\r
+  * @details    This API is used to get the timer clock frequency.\r
+  * @note       This API cannot return correct clock rate if timer source is from external clock input.\r
+  */\r
+uint32_t TIMER_GetModuleClock(TIMER_T *timer)\r
+{\r
+    uint32_t u32Src, u32Clk = __HIRC;\r
+    const uint32_t au32Clk[] = {__HXT, __LXT, 0UL, 0UL, 0UL, __LIRC, 0UL, __HIRC};\r
+\r
+    if(timer == TIMER0)\r
+    {\r
+        u32Src = CLK_GetModuleClockSource(TMR0_MODULE);\r
+    }\r
+    else if(timer == TIMER1)\r
+    {\r
+        u32Src = CLK_GetModuleClockSource(TMR1_MODULE);\r
+    }\r
+    else if((timer == TIMER2) || (timer == TIMER2_NS))\r
+    {\r
+        u32Src = CLK_GetModuleClockSource(TMR2_MODULE);\r
+    }\r
+    else if((timer == TIMER3) || (timer == TIMER3_NS))\r
+    {\r
+        u32Src = CLK_GetModuleClockSource(TMR3_MODULE);\r
+    }\r
+    else\r
+    {\r
+        u32Clk = 0UL;\r
+    }\r
+\r
+    if(u32Clk == 0UL)\r
+    {\r
+        ; /* Invalid timer channel */\r
+    }\r
+    else\r
+    {\r
+        if(u32Src == 2UL)\r
+        {\r
+            if((timer == TIMER0) || (timer == TIMER1))\r
+            {\r
+                u32Clk = CLK_GetPCLK0Freq();\r
+            }\r
+            else\r
+            {\r
+                u32Clk = CLK_GetPCLK1Freq();\r
+            }\r
+        }\r
+        else\r
+        {\r
+            u32Clk = au32Clk[u32Src];\r
+        }\r
+    }\r
+\r
+    return u32Clk;\r
+}\r
+\r
+/**\r
+  * @brief      Enable Timer Frequency Counter Function\r
+  *\r
+  * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  * @param[in]  u32DropCount    This parameter has no effect in this BSP\r
+  * @param[in]  u32Timeout      This parameter has no effect in this BSP\r
+  * @param[in]  u32EnableInt    Enable interrupt assertion after capture complete or not. Valid values are TRUE and FALSE\r
+  *\r
+  * @return     None\r
+  *\r
+  * @details    This function is used to calculate input event frequency. After enable\r
+  *             this function, a pair of timers, TIMER0 and TIMER1, or TIMER2 and TIMER3\r
+  *             will be configured for this function. The mode used to calculate input\r
+  *             event frequency is mentioned as "Inter Timer Trigger Mode" in Technical\r
+  *             Reference Manual.\r
+  */\r
+void TIMER_EnableFreqCounter(TIMER_T *timer,\r
+                             uint32_t u32DropCount,\r
+                             uint32_t u32Timeout,\r
+                             uint32_t u32EnableInt)\r
+{\r
+    TIMER_T *t;    /* store the timer base to configure compare value */\r
+\r
+    if(timer == TIMER0)\r
+    {\r
+        t = TIMER1;\r
+    }\r
+    else if(timer == TIMER2)\r
+    {\r
+        t = TIMER3;\r
+    }\r
+    else if(timer == TIMER2_NS)\r
+    {\r
+        t = TIMER3_NS;\r
+    }\r
+    else\r
+    {\r
+        t = 0UL ;\r
+    }\r
+\r
+    if(t != 0UL)\r
+    {\r
+        t->CMP = 0xFFFFFFUL;\r
+        t->EXTCTL = u32EnableInt ? TIMER_EXTCTL_CAPIEN_Msk : 0UL;\r
+        timer->CTL = TIMER_CTL_INTRGEN_Msk | TIMER_CTL_CNTEN_Msk;\r
+    }\r
+}\r
+\r
+/**\r
+  * @brief      Disable Timer Frequency Counter Function\r
+  *\r
+  * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  *\r
+  * @return     None\r
+  *\r
+  * @brief      This function is used to disable the Timer frequency counter function.\r
+  */\r
+void TIMER_DisableFreqCounter(TIMER_T *timer)\r
+{\r
+    timer->CTL &= ~TIMER_CTL_INTRGEN_Msk;\r
+}\r
+\r
+/**\r
+  * @brief      Select Interrupt Source to Trigger others Module\r
+  *\r
+  * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  * @param[in]  u32Src  Selects the interrupt source to trigger other modules. Could be:\r
+  *                     - \ref TIMER_TRGSRC_TIMEOUT_EVENT\r
+  *                     - \ref TIMER_TRGSRC_CAPTURE_EVENT\r
+  *\r
+  * @return     None\r
+  *\r
+  * @brief      This function is used to select the interrupt source used to trigger other modules.\r
+  */\r
+void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src)\r
+{\r
+    timer->TRGCTL = (timer->TRGCTL & ~TIMER_TRGCTL_TRGSSEL_Msk) | u32Src;\r
+}\r
+\r
+/**\r
+  * @brief      Set Modules Trigger by Timer Interrupt\r
+  *\r
+  * @param[in]  timer   The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.\r
+  * @param[in]  u32Mask The mask of modules (EPWM, EADC, DAC and PDMA) trigger by timer. Is the combination of\r
+  *                     - \ref TIMER_TRG_TO_EPWM,\r
+  *                     - \ref TIMER_TRG_TO_EADC,\r
+  *                     - \ref TIMER_TRG_TO_DAC and\r
+  *                     - \ref TIMER_TRG_TO_PDMA\r
+  *\r
+  * @return     None\r
+  *\r
+  * @details    This function is used to set EPWM, EADC, DAC and PDMA module triggered by timer interrupt event.\r
+  */\r
+void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask)\r
+{\r
+    timer->TRGCTL = (timer->TRGCTL & ~(TIMER_TRGCTL_TRGEPWM_Msk | TIMER_TRGCTL_TRGDAC_Msk | TIMER_TRGCTL_TRGEADC_Msk | TIMER_TRGCTL_TRGPDMA_Msk)) | u32Mask;\r
+}\r
+\r
+/*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */\r
+\r
+/*@}*/ /* end of group TIMER_Driver */\r
+\r
+/*@}*/ /* end of group Standard_Driver */\r
+\r
+/*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/\r