summaryrefslogtreecommitdiff
path: root/bsp/essemi/es32vf2264/drivers/drv_hwtimer.c
blob: e42819af0941b3dc6c571485b7795b8b76599a80 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-3-19      wangyq       the first version
 * 2019-11-01     wangyq        update libraries
 * 2021-04-20     liuhy         the second version
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <drv_hwtimer.h>
#include <board.h>


#ifdef RT_USING_HWTIMER

struct es32f3_hwtimer_dev
{
    rt_hwtimer_t parent;
    ald_timer_handle_t *hwtimer_periph;
    IRQn_Type IRQn;
};

#ifdef BSP_USING_AD16C4T0_HWTIMER
static struct es32f3_hwtimer_dev ad16c4t0_hwtimer;

static struct rt_hwtimer_info ad16c4t0_info =
{
    ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV, /* maximum count frequency */
    (ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_AD16C4T0_HWTIMER_MODE
};

void __attribute__((interrupt)) AD16C4T_Handler(void)
{
    rt_interrupt_enter();
    ald_timer_clear_flag_status(ad16c4t0_hwtimer.hwtimer_periph, ALD_TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&ad16c4t0_hwtimer.parent);
    rt_interrupt_leave();
}

#endif

#ifdef BSP_USING_GP16C4T0_HWTIMER

static struct es32f3_hwtimer_dev gp16c4t0_hwtimer;

static struct rt_hwtimer_info gp16c4t0_info =
{
    ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV, /* maximum count frequency */
    (ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_GP16C4T0_HWTIMER_MODE
};

void __attribute__((interrupt)) GPTIMB0_Handler(void)
{
    rt_interrupt_enter();
    ald_timer_clear_flag_status(gp16c4t0_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&gp16c4t0_hwtimer.parent);
    rt_interrupt_leave();
}

#endif
#ifdef BSP_USING_GP16C4T1_HWTIMER

static struct es32f3_hwtimer_dev gp16c4t1_hwtimer;

static struct rt_hwtimer_info gp16c4t1_info =
{
    ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV, /* maximum count frequency */
    (ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_GP16C4T1_HWTIMER_MODE
};

void __attribute__((interrupt)) GPTIMB1_Handler(void)
{
    rt_interrupt_enter();
    ald_timer_clear_flag_status(gp16c4t1_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&gp16c4t1_hwtimer.parent);
    rt_interrupt_leave();
}

#endif
#ifdef BSP_USING_GP16C4T1_HWTIMER

static struct es32f3_hwtimer_dev gp16c4t1_hwtimer;

static struct rt_hwtimer_info gp16c4t1_info =
{
    ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV, /* maximum count frequency */
    (ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_GP16C4T1_HWTIMER_MODE
};

void __attribute__((interrupt)) GPTIMB2_Handler(void)
{
    rt_interrupt_enter();
    ald_timer_clear_flag_status(gp16c4t1_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&gp16c4t1_hwtimer.parent);
    rt_interrupt_leave();
}

#endif
#ifdef BSP_USING_BS16T0_HWTIMER

static struct es32f3_hwtimer_dev bs16t0_hwtimer;

static struct rt_hwtimer_info bs16t0_info =
{
    ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV, /* maximum count frequency */
    (ES_SYS_HCLK_CLK >> ES_CMU_PCLK_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_BS16T0_HWTIMER_MODE
};

void __attribute__((interrupt)) BSTIM0_Handler(void)
{
    rt_interrupt_enter();
    ald_timer_clear_flag_status(bs16t0_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&bs16t0_hwtimer.parent);
    rt_interrupt_leave();
}

#endif

static void es32f3_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{
    struct es32f3_hwtimer_dev *hwtimer = (struct es32f3_hwtimer_dev *)timer->parent.user_data;

    struct rt_hwtimer_info *hwtimer_info = (struct rt_hwtimer_info *)timer->info;

    RT_ASSERT(hwtimer != RT_NULL);

    if (1 == state)
    {
        ald_timer_base_init(hwtimer->hwtimer_periph);
        ald_timer_interrupt_config(hwtimer->hwtimer_periph, ALD_TIMER_IT_UPDATE, ENABLE);
        csi_vic_enable_sirq(hwtimer->IRQn);
    }

    hwtimer->parent.freq = ald_cmu_get_pclk_clock()/((hwtimer->hwtimer_periph->perh->PRES & 0xFFFF)+1);
    hwtimer_info->maxfreq = hwtimer->parent.freq;
    hwtimer_info->minfreq = (hwtimer->parent.freq)/0xFFFF;

}

static rt_err_t es32f3_hwtimer_start(rt_hwtimer_t *timer,
                                     rt_uint32_t cnt,
                                     rt_hwtimer_mode_t mode)
{
    struct es32f3_hwtimer_dev *hwtimer = (struct es32f3_hwtimer_dev *)timer->parent.user_data;

    RT_ASSERT(hwtimer != RT_NULL);

    WRITE_REG(hwtimer->hwtimer_periph->perh->AR, cnt);
    ald_timer_base_start(hwtimer->hwtimer_periph);

    return RT_EOK;
}

static void es32f3_hwtimer_stop(rt_hwtimer_t *timer)
{
    struct es32f3_hwtimer_dev *hwtimer = (struct es32f3_hwtimer_dev *)timer->parent.user_data;

    RT_ASSERT(hwtimer != RT_NULL);

    ald_timer_base_stop(hwtimer->hwtimer_periph);
}

static rt_uint32_t es32f3_hwtimer_count_get(rt_hwtimer_t *timer)
{
    struct es32f3_hwtimer_dev *hwtimer = (struct es32f3_hwtimer_dev *)timer->parent.user_data;
    uint32_t hwtimer_count = 0;

    RT_ASSERT(hwtimer != RT_NULL);

    hwtimer_count = READ_REG(hwtimer->hwtimer_periph->perh->COUNT);

    return hwtimer_count;
}

static rt_err_t es32f3_hwtimer_control(rt_hwtimer_t *timer,
                                       rt_uint32_t cmd,
                                       void *args)
{
    rt_err_t ret = RT_EOK;
    rt_uint32_t freq = 0;
    struct es32f3_hwtimer_dev *hwtimer = (struct es32f3_hwtimer_dev *)timer->parent.user_data;

    RT_ASSERT(hwtimer != RT_NULL);

    switch (cmd)
    {
    case HWTIMER_CTRL_FREQ_SET:
        freq = *(rt_uint32_t *)args;

        ret = -RT_ERROR;

        if(freq)
        {
            double temp,target;
            temp = (double)ald_cmu_get_pclk_clock();
            target = temp/freq;

            if(target < 0x10001)   /*最大分频 = max(PRES)+1*/
            {
                temp = target - (int)(target);

                if((temp > 0.998)&&(target < 0x10000))
                {
                    hwtimer->hwtimer_periph->perh->PRES = (uint32_t)target;
                    ret = RT_EOK;
                }
                if((temp < 0.002)&&(target >= 0x1))
                {
                    hwtimer->hwtimer_periph->perh->PRES = (uint32_t)target - 1;
                    ret = RT_EOK;
                }

            }

            if(ret == RT_EOK)     /*更新信息*/
                hwtimer->parent.freq = ald_cmu_get_pclk_clock()/((hwtimer->hwtimer_periph->perh->PRES & 0xFFFF)+1);

        }

        break;

    case HWTIMER_CTRL_STOP:
        ald_timer_base_stop(hwtimer->hwtimer_periph);
        break;

    default:
        ret = RT_EINVAL;
        break;
    }

    return ret;
}

static struct rt_hwtimer_ops es32f3_hwtimer_ops =
{
    es32f3_hwtimer_init,
    es32f3_hwtimer_start,
    es32f3_hwtimer_stop,
    es32f3_hwtimer_count_get,
    es32f3_hwtimer_control
};

int rt_hw_hwtimer_init(void)
{
    rt_err_t ret = RT_EOK;

#ifdef BSP_USING_AD16C4T0_HWTIMER
    static ald_timer_handle_t ad16c4t0_hwtimer_periph;

    ad16c4t0_hwtimer_periph.perh = AD16C4T;
    ad16c4t0_hwtimer.IRQn = AD16C4T_IRQn;

    ad16c4t0_hwtimer_periph.init.prescaler = ES_AD16C4T0_HWTIMER_PRES - 1;
    ad16c4t0_hwtimer_periph.init.mode = ( ES_AD16C4T0_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? ALD_TIMER_CNT_MODE_UP : ALD_TIMER_CNT_MODE_DOWN;
    ad16c4t0_hwtimer.hwtimer_periph = &ad16c4t0_hwtimer_periph;

    ad16c4t0_hwtimer.parent.info = &ad16c4t0_info;
    ad16c4t0_hwtimer.parent.ops = &es32f3_hwtimer_ops;
    ret = rt_device_hwtimer_register(&ad16c4t0_hwtimer.parent, ES_DEVICE_NAME_AD16C4T0_HWTIMER, &ad16c4t0_hwtimer);
#endif

#ifdef BSP_USING_GP16C4T0_HWTIMER
    static timer_handle_t gp16c4t0_hwtimer_periph;

    gp16c4t0_hwtimer_periph.perh = GP16C4T0;
    gp16c4t0_hwtimer.IRQn = GP16C4T0_IRQn;

    gp16c4t0_hwtimer_periph.init.prescaler = ES_GP16C4T0_HWTIMER_PRES - 1;
    gp16c4t0_hwtimer_periph.init.mode = ( ES_GP16C4T0_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    gp16c4t0_hwtimer.hwtimer_periph = &gp16c4t0_hwtimer_periph;

    gp16c4t0_hwtimer.parent.info = &gp16c4t0_info;
    gp16c4t0_hwtimer.parent.ops = &es32f3_hwtimer_ops;
    ret = rt_device_hwtimer_register(&gp16c4t0_hwtimer.parent, ES_DEVICE_NAME_GP16C4T0_HWTIMER, &gp16c4t0_hwtimer);
#endif

#ifdef BSP_USING_GP16C4T1_HWTIMER
    static timer_handle_t gp16c4t1_hwtimer_periph;

    gp16c4t1_hwtimer_periph.perh = GP16C4T1;
    gp16c4t1_hwtimer.IRQn = GP16C4T1_IRQn;

    gp16c4t1_hwtimer_periph.init.prescaler = ES_GP16C4T1_HWTIMER_PRES - 1;
    gp16c4t1_hwtimer_periph.init.mode = ( ES_GP16C4T1_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    gp16c4t1_hwtimer.hwtimer_periph = &gp16c4t1_hwtimer_periph;

    gp16c4t1_hwtimer.parent.info = &gp16c4t1_info;
    gp16c4t1_hwtimer.parent.ops = &es32f3_hwtimer_ops;
    ret = rt_device_hwtimer_register(&gp16c4t1_hwtimer.parent, ES_DEVICE_NAME_GP16C4T1_HWTIMER, &gp16c4t1_hwtimer);
#endif

#ifdef BSP_USING_GP16C4T2_HWTIMER
    static timer_handle_t gp16c4t2_hwtimer_periph;

    gp16c4t2_hwtimer_periph.perh = GP16C4T2;
    gp16c4t2_hwtimer.IRQn = GP16C4T2_IRQn;

    gp16c4t2_hwtimer_periph.init.prescaler = ES_GP16C4T2_HWTIMER_PRES - 1;
    gp16c4t2_hwtimer_periph.init.mode = ( ES_GP16C4T2_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    gp16c4t2_hwtimer.hwtimer_periph = &gp16c4t2_hwtimer_periph;

    gp16c4t2_hwtimer.parent.info = &gp16c4t2_info;
    gp16c4t2_hwtimer.parent.ops = &es32f3_hwtimer_ops;
    ret = rt_device_hwtimer_register(&gp16c4t2_hwtimer.parent, ES_DEVICE_NAME_GP16C4T2_HWTIMER, &gp16c4t2_hwtimer);
#endif

#ifdef BSP_USING_BS16T0_HWTIMER
    static timer_handle_t bs16t0_hwtimer_periph;

    bs16t0_hwtimer_periph.perh = BS16T0;
    bs16t0_hwtimer.IRQn = BS16T0_IRQn;

    bs16t0_hwtimer_periph.init.prescaler = ES_BS16T0_HWTIMER_PRES - 1;
    bs16t0_hwtimer_periph.init.mode = ( ES_BS16T0_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    bs16t0_hwtimer.hwtimer_periph = &bs16t0_hwtimer_periph;

    bs16t0_hwtimer.parent.info = &bs16t0_info;
    bs16t0_hwtimer.parent.ops = &es32f3_hwtimer_ops;
    ret = rt_device_hwtimer_register(&bs16t0_hwtimer.parent, ES_DEVICE_NAME_BS16T0_HWTIMER, &bs16t0_hwtimer);
#endif

    return ret;
}
INIT_BOARD_EXPORT(rt_hw_hwtimer_init);

#endif