1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
* Copyright (C) 2017-2024 Alibaba Group Holding Limited
*/
/*******************************************************
* @file dw_timer.c
* @brief source file for timer csi driver
* @version V1.0
* @date 28. Apr 2020
* ******************************************************/
#include <csi_config.h>
#include <drv/timer.h>
#include <drv/irq.h>
#include "dw_timer_ll.h"
/**
\brief Timer interrupt handling function
\param[in] arg Callback function member variables
\return None
*/
void dw_timer_irq_handler(void *arg)
{
csi_timer_t *timer = (csi_timer_t *)arg;
dw_timer_regs_t *timer_base = (dw_timer_regs_t *)timer->dev.reg_base;
if (dw_timer_get_int_status(timer_base)) {
if (timer->callback) {
timer->callback(timer, timer->arg);
}
dw_timer_clear_irq(timer_base);
}
}
#if CONFIG_SUPPORT_NMI_DEMO
void timer_clear_irq()
{
dw_timer_clear_irq((dw_timer_regs_t*)DW_TIMER4_BASE);
}
#endif
/**
\brief Initialize TIMER Interface. 1. Initializes the resources needed for the TIMER interface 2.registers callback function
\param[in] timer handle timer handle to operate
\param[in] idx timer index
\return error code \ref csi_error_t
*/
csi_error_t csi_timer_init(csi_timer_t *timer, uint32_t idx)
{
CSI_PARAM_CHK(timer, CSI_ERROR);
csi_error_t ret = CSI_OK;
dw_timer_regs_t *timer_base = NULL;
if (0 == target_get(DEV_DW_TIMER_TAG, idx, &timer->dev)) {
timer_base = (dw_timer_regs_t *)HANDLE_REG_BASE(timer);
memset((void *)timer_base, 0, sizeof(dw_timer_regs_t));
} else {
ret = CSI_ERROR;
}
return ret;
}
/**
\brief De-initialize TIMER Interface. stops operation and releases the software resources used by the interface
\param[in] timer handle timer handle to operate
\return None
*/
void csi_timer_uninit(csi_timer_t *timer)
{
CSI_PARAM_CHK_NORETVAL(timer);
dw_timer_regs_t *timer_base = (dw_timer_regs_t *)HANDLE_REG_BASE(timer);
memset((void *)timer_base, 0, sizeof(dw_timer_regs_t));
}
/**
\brief Start timer
\param[in] timer handle timer handle to operate
\param[in] timeout_us the timeout for timer
\return error code \ref csi_error_t
*/
csi_error_t csi_timer_start(csi_timer_t *timer, uint32_t timeout_us)
{
CSI_PARAM_CHK(timer, CSI_ERROR);
CSI_PARAM_CHK(timeout_us, CSI_ERROR);
csi_error_t ret = CSI_OK;
dw_timer_regs_t *timer_base = (dw_timer_regs_t *)HANDLE_REG_BASE(timer);
uint32_t tmp_load = timeout_us * (soc_get_timer_freq((uint32_t)timer->dev.idx) / 1000000U);
dw_timer_set_mode_load(timer_base);
dw_timer_write_load(timer_base, tmp_load);
dw_timer_set_disable(timer_base);
dw_timer_set_enable(timer_base);
dw_timer_set_unmask(timer_base);
return ret;
}
/**
\brief Stop timer
\param[in] timer handle timer handle to operate
\return None
*/
void csi_timer_stop(csi_timer_t *timer)
{
CSI_PARAM_CHK_NORETVAL(timer);
dw_timer_regs_t *timer_base;
timer_base = (dw_timer_regs_t *)HANDLE_REG_BASE(timer);
dw_timer_set_mask(timer_base);
dw_timer_set_disable(timer_base);
}
/**
\brief Get timer remaining value
\param[in] timer handle timer handle to operate
\return the remaining value
*/
uint32_t csi_timer_get_remaining_value(csi_timer_t *timer)
{
CSI_PARAM_CHK(timer, 0U);
dw_timer_regs_t *timer_base;
timer_base = (dw_timer_regs_t *)HANDLE_REG_BASE(timer);
return dw_timer_get_current(timer_base);
}
/**
\brief Get timer load value
\param[in] timer handle timer handle to operate
\return the load value
*/
uint32_t csi_timer_get_load_value(csi_timer_t *timer)
{
CSI_PARAM_CHK(timer, 0U);
dw_timer_regs_t *timer_base = (dw_timer_regs_t *)HANDLE_REG_BASE(timer);
return (dw_timer_read_load(timer_base));
}
/**
\brief Check timer is running
\param[in] timer handle timer handle to operate
\return true->running, false->stopped
*/
bool csi_timer_is_running(csi_timer_t *timer)
{
CSI_PARAM_CHK(timer, false);
dw_timer_regs_t *timer_base = (dw_timer_regs_t *)HANDLE_REG_BASE(timer);
return (dw_timer_get_enable(timer_base) ? true : false);
}
/**
\brief Attach the callback handler to timer
\param[in] timer operate handle.
\param[in] callback callback function
\param[in] arg callback's param
\return error code \ref csi_error_t
*/
csi_error_t csi_timer_attach_callback(csi_timer_t *timer, void *callback, void *arg)
{
CSI_PARAM_CHK(timer, CSI_ERROR);
timer->callback = callback;
timer->arg = arg;
csi_irq_attach((uint32_t)timer->dev.irq_num, &dw_timer_irq_handler, &timer->dev);
csi_irq_enable((uint32_t)timer->dev.irq_num);
return CSI_OK;
}
/**
\brief Detach the callback handler
\param[in] timer operate handle.
*/
void csi_timer_detach_callback(csi_timer_t *timer)
{
CSI_PARAM_CHK_NORETVAL(timer);
timer->callback = NULL;
timer->arg = NULL;
csi_irq_disable((uint32_t)timer->dev.irq_num);
csi_irq_detach((uint32_t)timer->dev.irq_num);
}
#ifdef CONFIG_PM
csi_error_t dw_timer_pm_action(csi_dev_t *dev, csi_pm_dev_action_t action)
{
CSI_PARAM_CHK(dev, CSI_ERROR);
csi_error_t ret = CSI_OK;
csi_pm_dev_t *pm_dev = &dev->pm_dev;
switch (action) {
case PM_DEV_SUSPEND:
csi_pm_dev_save_regs(pm_dev->reten_mem, (uint32_t *)dev->reg_base, 1U);
csi_pm_dev_save_regs(pm_dev->reten_mem + 1, (uint32_t *)(dev->reg_base + 8U), 1U);
break;
case PM_DEV_RESUME:
csi_pm_dev_restore_regs(pm_dev->reten_mem, (uint32_t *)dev->reg_base, 1U);
csi_pm_dev_restore_regs(pm_dev->reten_mem + 1, (uint32_t *)(dev->reg_base + 8U), 1U);
break;
default:
ret = CSI_ERROR;
break;
}
return ret;
}
csi_error_t csi_timer_enable_pm(csi_timer_t *timer)
{
return csi_pm_dev_register(&timer->dev, dw_timer_pm_action, 8U, 0U);
}
void csi_timer_disable_pm(csi_timer_t *timer)
{
csi_pm_dev_unregister(&timer->dev);
}
#endif
|