1 /**
2   ******************************************************************************
3   * @file    lib_tmr.c
4   * @author  Application Team
5   * @version V1.1.0
6   * @date    2019-10-28
7   * @brief   Timer library.
8   ******************************************************************************
9   * @attention
10   *
11   ******************************************************************************
12   */
13 #include "lib_tmr.h"
14 
15 #define TMR_CTRL_RSTValue       (0UL)
16 #define TMR_VALUE_RSTValue      (0UL)
17 #define TMR_RELOAD_RSTValue     (0UL)
18 
19 /**
20   * @brief  Initializes the timer peripheral registers to their default reset values.
21   * @param  TMRx:
22                 TMR0 ~ TMR3
23   * @retval None
24   */
TMR_DeInit(TMR_Type * TMRx)25 void TMR_DeInit(TMR_Type *TMRx)
26 {
27   /* Check parameters */
28   assert_parameters(IS_TMR_ALL_INSTANCE(TMRx));
29 
30   /* Disable timer */
31   TMRx->CTRL &= ~TMR_CTRL_EN;
32   /* clear interrupt status */
33   TMRx->INTSTS = TMR_INTSTS_INTSTS;
34   /* write default reset values */
35   TMRx->CTRL   = TMR_CTRL_RSTValue;
36   TMRx->RELOAD = TMR_RELOAD_RSTValue;
37   TMRx->VALUE  =  TMR_VALUE_RSTValue;
38 }
39 
40 /**
41   * @brief  Initializes timer.
42   * @param  TMRx:
43                 TMR0 ~ TMR3
44             InitStruct: Timer configuration.
45                 ClockSource:
46                     TMR_CLKSRC_INTERNAL
47                     TMR_CLKSRC_EXTERNAL
48                 EXTGT:
49                     TMR_EXTGT_DISABLE
50                     TMR_EXTGT_ENABLE
51                 Period: the auto-reload value
52   * @retval None
53   */
TMR_Init(TMR_Type * TMRx,TMR_InitType * InitStruct)54 void TMR_Init(TMR_Type *TMRx, TMR_InitType *InitStruct)
55 {
56   uint32_t tmp;
57 
58   /* Check parameters */
59   assert_parameters(IS_TMR_ALL_INSTANCE(TMRx));
60   assert_parameters(IS_TMR_CLKSRC(InitStruct->ClockSource));
61   assert_parameters(IS_TMR_EXTGT(InitStruct->EXTGT));
62 
63   tmp = TMRx->CTRL;
64   tmp &= ~(TMR_CTRL_EXTCLK|TMR_CTRL_EXTEN);
65   tmp |= (InitStruct->ClockSource|InitStruct->EXTGT);
66   TMRx->CTRL = tmp;
67   TMRx->VALUE = InitStruct->Period;
68   TMRx->RELOAD = InitStruct->Period;
69 }
70 
71 /**
72   * @brief  Fills each TMR_InitType member with its default value.
73   * @param  InitStruct: pointer to an TMR_InitType structure which will be initialized.
74   * @retval None
75   */
TMR_StructInit(TMR_InitType * InitStruct)76 void TMR_StructInit(TMR_InitType *InitStruct)
77 {
78   /*--------------- Reset TMR init structure parameters values ---------------*/
79   /* Initialize the ClockSource member */
80   InitStruct->ClockSource = TMR_CLKSRC_INTERNAL;
81   /* Initialize the EXTGT member */
82   InitStruct->EXTGT = TMR_EXTGT_DISABLE;
83   /* Initialize the Period member */
84   InitStruct->Period = 0;
85 }
86 
87 /**
88   * @brief  Enables or disables timer interrupt.
89   * @param  TMRx:
90                 TMR0~TMR3
91             NewState:
92                 ENABLE
93                 DISABLE
94   * @retval None
95   */
TMR_INTConfig(TMR_Type * TMRx,uint32_t NewState)96 void TMR_INTConfig(TMR_Type *TMRx, uint32_t NewState)
97 {
98   /* Check parameters */
99   assert_parameters(IS_TMR_ALL_INSTANCE(TMRx));
100   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
101 
102   if (NewState == ENABLE)
103   {
104     TMRx->CTRL |= TMR_CTRL_INTEN;
105   }
106   else
107   {
108     TMRx->CTRL &= ~TMR_CTRL_INTEN;
109   }
110 }
111 
112 /**
113   * @brief  Gets timer interrupt status.
114   * @param  TMRx:
115                 TMR0~TMR3
116   * @retval Interrupt status.
117   */
TMR_GetINTStatus(TMR_Type * TMRx)118 uint8_t TMR_GetINTStatus(TMR_Type *TMRx)
119 {
120   /* Check parameters */
121   assert_parameters(IS_TMR_ALL_INSTANCE(TMRx));
122 
123   if (TMRx->INTSTS & TMR_INTSTS_INTSTS)
124     return 1;
125   else
126     return 0;
127 }
128 
129 /**
130   * @brief  Clears timer interrupt status bit.
131   * @param  TMRx:
132                 TMR0~TMR3
133   * @retval None.
134   */
TMR_ClearINTStatus(TMR_Type * TMRx)135 void TMR_ClearINTStatus(TMR_Type *TMRx)
136 {
137   /* Check parameters */
138   assert_parameters(IS_TMR_ALL_INSTANCE(TMRx));
139 
140   TMRx->INTSTS = TMR_INTSTS_INTSTS;
141 }
142 
143 /**
144   * @brief  Enables or disables timer.
145   * @param  TMRx:
146                 TMR0~TMR3
147             NewState:
148                 ENABLE
149                 DISABLE
150   * @retval None
151   */
TMR_Cmd(TMR_Type * TMRx,uint32_t NewState)152 void TMR_Cmd(TMR_Type *TMRx, uint32_t NewState)
153 {
154   /* Check parameters */
155   assert_parameters(IS_TMR_ALL_INSTANCE(TMRx));
156   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
157 
158   if (NewState == ENABLE)
159     TMRx->CTRL |= TMR_CTRL_EN;
160   else
161     TMRx->CTRL &= ~TMR_CTRL_EN;
162 }
163 
164 /**
165   * @brief  Gets timer current value.
166   * @param  TMRx:
167                 TMR0~TMR3
168   * @retval timer value.
169   */
TMR_GetCurrentValue(TMR_Type * TMRx)170 uint32_t TMR_GetCurrentValue(TMR_Type *TMRx)
171 {
172   /* Check parameters */
173   assert_parameters(IS_TMR_ALL_INSTANCE(TMRx));
174 
175   return (TMRx->VALUE);
176 }
177 
178 /*********************************** END OF FILE ******************************/
179