1  /*
2  * Copyright (C) 2017-2024 Alibaba Group Holding Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /******************************************************************************
20  * @file       drv/timer.h
21  * @brief      Header File for TIMER Driver
22  * @version    V1.0
23  * @date       9. Oct 2020
24  * @model      timer
25  ******************************************************************************/
26 
27 #ifndef _DRV_TIMER_H_
28 #define _DRV_TIMER_H_
29 
30 #include <drv/common.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 typedef struct csi_timer csi_timer_t;
37 
38 struct csi_timer {
39     csi_dev_t        dev;
40     void (*callback)(csi_timer_t *timer, void *arg);
41     void            *arg;
42     void            *priv;
43 };
44 
45 /**
46   \brief       Initialize TIMER interface. initializes the resources needed for the TIMER interface
47   \param[in]   timer    Handle to operate
48   \param[in]   idx      TIMER index
49   \return      Error code \ref csi_error_t
50 */
51 csi_error_t csi_timer_init(csi_timer_t *timer, uint32_t idx);
52 
53 /**
54   \brief       De-initialize TIMER interface. stops operation and releases the software resources used by the interface
55   \param[in]   timer    Handle to operate
56   \return      None
57 */
58 void csi_timer_uninit(csi_timer_t *timer);
59 
60 /**
61   \brief       Start TIMER
62   \param[in]   timer         Handle to operate
63   \param[in]   timeout_us    The timeout for TIMER
64   \return      Error code \ref csi_error_t
65 */
66 csi_error_t csi_timer_start(csi_timer_t *timer, uint32_t timeout_us);
67 
68 /**
69   \brief       Stop TIMER
70   \param[in]   timer    Handle to operate
71   \return      None
72 */
73 void csi_timer_stop(csi_timer_t *timer);
74 
75 /**
76   \brief       Get TIMER remaining value
77   \param[in]   timer    Handle to operate
78   \return      remaining value
79 */
80 uint32_t csi_timer_get_remaining_value(csi_timer_t *timer);
81 
82 /**
83   \brief       Get TIMER load value
84   \param[in]   timer    Handle to operate
85   \return      Load value
86 */
87 uint32_t csi_timer_get_load_value(csi_timer_t *timer);
88 
89 /**
90   \brief       Check TIMER is running
91   \param[in]   timer    Handle to operate
92   \return
93                true  - TIMER is running
94                false - TIMER is stopped
95 */
96 bool csi_timer_is_running(csi_timer_t *timer);
97 
98 /**
99   \brief       Attach the callback handler to TIMER
100   \param[in]   timer       Operate handle
101   \param[in]   callback    Callback function
102   \param[in]   arg         Callback's param
103   \return      Error code \ref csi_error_t
104 */
105 csi_error_t csi_timer_attach_callback(csi_timer_t *timer, void *callback, void *arg);
106 
107 /**
108   \brief       Detach the callback handler
109   \param[in]   timer    Operate handle
110   \return      None
111 */
112 void csi_timer_detach_callback(csi_timer_t *timer);
113 
114 /**
115   \brief       Enable TIMER power manage
116   \param[in]   timer    Handle to operate
117   \return      Error code \ref csi_error_t
118 */
119 csi_error_t csi_timer_enable_pm(csi_timer_t *timer);
120 
121 /**
122   \brief       Disable TIMER power manage
123   \param[in]   timer    Handle to operate
124   \return      None
125 */
126 void csi_timer_disable_pm(csi_timer_t *timer);
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif /* _DRV_TIMER_H_ */
133