1 /******************************************************************************
2 *Copyright(C)2017, Huada Semiconductor Co.,Ltd All rights reserved.
3 *
4 * This software is owned and published by:
5 * Huada Semiconductor Co.,Ltd("HDSC").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with HDSC
11 * components. This software is licensed by HDSC to be adapted only
12 * for use in systems utilizing HDSC components. HDSC shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. HDSC is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * Disclaimer:
19 * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
20 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
21 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
22 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
23 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
24 * WARRANTY OF NONINFRINGEMENT.
25 * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
26 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
27 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
28 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
29 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
31 * SAVINGS OR PROFITS,
32 * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
34 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
35 * FROM, THE SOFTWARE.
36 *
37 * This software may be replicated in part or whole for the licensed use,
38 * with the restriction that this Disclaimer and Copyright notice must be
39 * included with each copy of this software, whether used in part or whole,
40 * at all times.
41 */
42 
43 /** \file lpt.c
44  **
45  ** Common API of Low Power timer.
46  ** @link lptGroup Some description @endlink
47  **
48  **   - 2018-04-16   Husj    First version
49  **
50  ******************************************************************************/
51 
52 /*******************************************************************************
53  * Include files
54  ******************************************************************************/
55 #include "lpt.h"
56 /**
57  *******************************************************************************
58  ** \addtogroup LptGroup
59  ******************************************************************************/
60 //@{
61 
62 /*******************************************************************************
63  * Local pre-processor symbols/macros ('#define')
64  ******************************************************************************/
65 
66 /*******************************************************************************
67  * Global variable definitions (declared in header file with 'extern')
68  ******************************************************************************/
69 
70 /*******************************************************************************
71  * Local type definitions ('typedef')
72  ******************************************************************************/
73 
74 /*******************************************************************************
75  * Local variable definitions ('static')
76  ******************************************************************************/
77 
78 /*******************************************************************************
79  * Local function prototypes ('static')
80  ******************************************************************************/
81 static func_ptr_t pfnLpTimCallback = NULL;
82 
83 /*******************************************************************************
84  * Function implementation - global ('extern') and local ('static')
85  ******************************************************************************/
86 
87 /**
88  *****************************************************************************
89  ** \brief Low Power Timer 中断标志获取
90  **
91  **
92  **
93  **
94  ** \retval TRUE or FALSE
95  *****************************************************************************/
Lpt_GetIntFlag(void)96 boolean_t Lpt_GetIntFlag(void)
97 {
98     boolean_t bRetVal = FALSE;
99 
100     bRetVal = M0P_LPTIMER->IFR_f.TF ? TRUE : FALSE;
101 
102     return bRetVal;
103 }
104 
105 /**
106  *****************************************************************************
107  ** \brief Low Power Timer 中断标志清除
108  **
109  **
110  **
111  **
112  ** \retval Ok or Error
113  *****************************************************************************/
Lpt_ClearIntFlag(void)114 en_result_t Lpt_ClearIntFlag(void)
115 {
116     en_result_t enResult = Error;
117 
118     M0P_LPTIMER->ICLR_f.TFC = FALSE;
119     enResult = Ok;
120 
121     return enResult;
122 }
123 
124 /**
125  *****************************************************************************
126  ** \brief Low Power Timer 中断服务函数
127  **
128  **
129  ** \param [in] u8Param         == 0
130  **
131  *****************************************************************************/
LpTim_IRQHandler(uint8_t u8Param)132 void LpTim_IRQHandler(uint8_t u8Param)
133 {
134     if(NULL != pfnLpTimCallback)
135     {
136         pfnLpTimCallback();
137     }
138 }
139 
140 /**
141  *****************************************************************************
142  ** \brief Low Power Timer 中断使能
143  **
144  **
145  **
146  **
147  ** \retval Ok or Error
148  *****************************************************************************/
Lpt_EnableIrq(void)149 en_result_t Lpt_EnableIrq (void)
150 {
151     en_result_t enResult = Error;
152 
153     M0P_LPTIMER->CR_f.IE = TRUE;
154     enResult = Ok;
155 
156     return enResult;
157 }
158 
159 /**
160  *****************************************************************************
161  ** \brief Low Power Timer 中断禁止
162  **
163  **
164  **
165  **
166  ** \retval Ok or Error
167  *****************************************************************************/
Lpt_DisableIrq(void)168 en_result_t Lpt_DisableIrq(void)
169 {
170     en_result_t enResult = Error;
171 
172     M0P_LPTIMER->CR_f.IE = FALSE;
173     enResult = Ok;
174 
175     return enResult;
176 }
177 
178 /**
179  *****************************************************************************
180  ** \brief Low Power Timer 初始化配置
181  **
182  **
183  ** \param [in]  pstcConfig       初始化配置结构体指针
184  **
185  ** \retval Ok or Error
186  *****************************************************************************/
Lpt_Init(stc_lpt_config_t * pstcConfig)187 en_result_t Lpt_Init(stc_lpt_config_t* pstcConfig)
188 {
189     en_result_t enResult = Error;
190 
191     M0P_LPTIMER->CR_f.GATE_P  = pstcConfig->enGateP;
192     M0P_LPTIMER->CR_f.GATE    = pstcConfig->enGate;
193     M0P_LPTIMER->CR_f.TCK_SEL = pstcConfig->enTckSel;
194     M0P_LPTIMER->CR_f.TOG_EN  = pstcConfig->enTog;
195     M0P_LPTIMER->CR_f.CT      = pstcConfig->enCT;
196     M0P_LPTIMER->CR_f.MD      = pstcConfig->enMD;
197 
198     pfnLpTimCallback          = pstcConfig->pfnLpTimCb;
199 
200     enResult = Ok;
201 
202     return enResult;
203 }
204 
205 /**
206  *****************************************************************************
207  ** \brief Low Power Timer 启动运行
208  **
209  **
210  **
211  ** \retval Ok or Error
212  *****************************************************************************/
Lpt_Run(void)213 en_result_t Lpt_Run(void)
214 {
215     en_result_t enResult = Error;
216 
217     M0P_LPTIMER->CR_f.TR = TRUE;
218     enResult = Ok;
219 
220     return enResult;
221 }
222 
223 /**
224  *****************************************************************************
225  ** \brief Low Power Timer 停止运行
226  **
227  **
228  **
229  ** \retval Ok or Error
230  *****************************************************************************/
Lpt_Stop(void)231 en_result_t Lpt_Stop(void)
232 {
233     en_result_t enResult = Error;
234 
235     M0P_LPTIMER->CR_f.TR = FALSE;
236     enResult = Ok;
237 
238     return enResult;
239 }
240 
241 /**
242  *****************************************************************************
243  ** \brief Low Power Timer 重载值设置
244  **
245  **
246  ** \param [in]  u16Data          16bits重载值
247  **
248  ** \retval Ok or Error
249  *****************************************************************************/
Lpt_ARRSet(uint16_t u16Data)250 en_result_t Lpt_ARRSet(uint16_t u16Data)
251 {
252     en_result_t enResult = Error;
253     boolean_t bRetVal = FALSE;
254 
255     bRetVal = M0P_LPTIMER->CR_f.WT_FLAG ? TRUE : FALSE;
256     if(TRUE == bRetVal)
257     {
258         M0P_LPTIMER->ARR_f.ARR = u16Data;
259         enResult = Ok;
260     }
261     else
262     {
263         enResult = Error;
264     }
265     return enResult;
266 }
267 
268 /**
269  *****************************************************************************
270  ** \brief Low Power Timer 16位计数值获取
271  **
272  **
273  **
274  ** \retval 16bits计数值
275  *****************************************************************************/
Lpt_Cnt16Get(void)276 uint16_t Lpt_Cnt16Get(void)
277 {
278     uint16_t    u16CntData = 0;
279 
280     u16CntData = M0P_LPTIMER->CNT_f.CNT;
281 
282     return u16CntData;
283 }
284 
285 //@} // LptGroup
286 
287 /*******************************************************************************
288  * EOF (not truncated)
289  ******************************************************************************/
290