summaryrefslogtreecommitdiff
path: root/bsp/essemi/es32f0654/drivers/drv_hwtimer.c
blob: 09bf3c433a4b68388e0f47c702675c15b955831e (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * 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 es32f0_hwtimer_dev
{
    rt_hwtimer_t parent;
    timer_handle_t *hwtimer_periph;
    IRQn_Type IRQn;
};

#ifdef BSP_USING_AD16C4T0_HWTIMER
static struct es32f0_hwtimer_dev ad16c4t0_hwtimer;

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

void AD16C4T0_BRK_UP_TRIG_COM_Handler(void)
{
    ald_timer_clear_flag_status(ad16c4t0_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&ad16c4t0_hwtimer.parent);
}

#endif

#ifdef BSP_USING_GP16C4T0_HWTIMER

static struct es32f0_hwtimer_dev gp16c4t0_hwtimer;

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

void GP16C4T0_LCD_Handler(void)
{
    ald_timer_clear_flag_status(gp16c4t0_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&gp16c4t0_hwtimer.parent);
}

#endif

#ifdef BSP_USING_GP16C2T0_HWTIMER

static struct es32f0_hwtimer_dev gp16c2t0_hwtimer;

static struct rt_hwtimer_info gp16c2t0_info =
{
    ES_SYS_CLK >> ES_CMU_PCLK_1_DIV, /* maximum count frequency */
    (ES_SYS_CLK >> ES_CMU_PCLK_1_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_GP16C2T0_HWTIMER_MODE
};

void GP16C2T0_Handler(void)
{
    ald_timer_clear_flag_status(gp16c2t0_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&gp16c2t0_hwtimer.parent);
}

#endif

#ifdef BSP_USING_GP16C2T1_HWTIMER

static struct es32f0_hwtimer_dev gp16c2t1_hwtimer;

static struct rt_hwtimer_info gp16c2t1_info =
{
    ES_SYS_CLK >> ES_CMU_PCLK_1_DIV, /* maximum count frequency */
    (ES_SYS_CLK >> ES_CMU_PCLK_1_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_GP16C2T1_HWTIMER_MODE
};

void GP16C2T1_Handler(void)
{
    ald_timer_clear_flag_status(gp16c2t1_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&gp16c2t1_hwtimer.parent);
}

#endif

#ifdef BSP_USING_BS16T0_HWTIMER

static struct es32f0_hwtimer_dev bs16t0_hwtimer;

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

void BS16T0_Handler(void)
{
    ald_timer_clear_flag_status(bs16t0_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&bs16t0_hwtimer.parent);
}

#endif

#ifdef BSP_USING_BS16T1_HWTIMER

static struct es32f0_hwtimer_dev bs16t1_hwtimer;

static struct rt_hwtimer_info bs16t1_info =
{
    ES_SYS_CLK >> ES_CMU_PCLK_1_DIV, /* maximum count frequency */
    (ES_SYS_CLK >> ES_CMU_PCLK_1_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_BS16T1_HWTIMER_MODE
};

void BS16T1_UART2_Handler(void)
{
    ald_timer_clear_flag_status(bs16t1_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&bs16t1_hwtimer.parent);
}

#endif

#ifdef BSP_USING_BS16T2_HWTIMER

static struct es32f0_hwtimer_dev bs16t2_hwtimer;

static struct rt_hwtimer_info bs16t2_info =
{
    ES_SYS_CLK >> ES_CMU_PCLK_1_DIV, /* maximum count frequency */
    (ES_SYS_CLK >> ES_CMU_PCLK_1_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_BS16T2_HWTIMER_MODE
};

void BS16T2_UART3_Handler(void)
{
    ald_timer_clear_flag_status(bs16t2_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&bs16t2_hwtimer.parent);
}

#endif

#ifdef BSP_USING_BS16T3_HWTIMER

static struct es32f0_hwtimer_dev bs16t3_hwtimer;

static struct rt_hwtimer_info bs16t3_info =
{
    ES_SYS_CLK >> ES_CMU_PCLK_1_DIV, /* maximum count frequency */
    (ES_SYS_CLK >> ES_CMU_PCLK_1_DIV)/(1U<<16),        /* minimum count frequency */
    0xFFFF,    /* counter maximum value */
    ES_BS16T3_HWTIMER_MODE
};

void BS16T3_DAC0_Handler(void)
{
    ald_timer_clear_flag_status(bs16t3_hwtimer.hwtimer_periph, TIMER_FLAG_UPDATE);
    rt_device_hwtimer_isr(&bs16t3_hwtimer.parent);
}

#endif

static void es32f0_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{
    struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_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, TIMER_IT_UPDATE, ENABLE);
        NVIC_EnableIRQ(hwtimer->IRQn);
    }

    hwtimer->parent.freq = ald_cmu_get_pclk1_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 es32f0_hwtimer_start(rt_hwtimer_t *timer,
                                     rt_uint32_t cnt,
                                     rt_hwtimer_mode_t mode)
{
    struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_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 es32f0_hwtimer_stop(rt_hwtimer_t *timer)
{
    struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;

    RT_ASSERT(hwtimer != RT_NULL);

    ald_timer_base_stop(hwtimer->hwtimer_periph);
}

static rt_uint32_t es32f0_hwtimer_count_get(rt_hwtimer_t *timer)
{
    struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_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 es32f0_hwtimer_control(rt_hwtimer_t *timer,
                                       rt_uint32_t cmd,
                                       void *args)
{
    rt_err_t ret = RT_EOK;
    rt_uint32_t freq = 0;
    struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_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_pclk1_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_pclk1_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 es32f0_hwtimer_ops =
{
    es32f0_hwtimer_init,
    es32f0_hwtimer_start,
    es32f0_hwtimer_stop,
    es32f0_hwtimer_count_get,
    es32f0_hwtimer_control
};

int rt_hw_hwtimer_init(void)
{
    rt_err_t ret = RT_EOK;

#ifdef BSP_USING_AD16C4T0_HWTIMER
    static timer_handle_t ad16c4t0_hwtimer_periph;

    ad16c4t0_hwtimer_periph.perh = AD16C4T0;
    ad16c4t0_hwtimer.IRQn = AD16C4T0_BRK_UP_TRIG_COM_IRQn;

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

    ad16c4t0_hwtimer.parent.info = &ad16c4t0_info;
    ad16c4t0_hwtimer.parent.ops = &es32f0_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_LCD_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 = &es32f0_hwtimer_ops;
    ret = rt_device_hwtimer_register(&gp16c4t0_hwtimer.parent, ES_DEVICE_NAME_GP16C4T0_HWTIMER, &gp16c4t0_hwtimer);
#endif

#ifdef BSP_USING_GP16C2T0_HWTIMER
    static timer_handle_t gp16c2t0_hwtimer_periph;

    gp16c2t0_hwtimer_periph.perh = GP16C2T0;
    gp16c2t0_hwtimer.IRQn = GP16C2T0_IRQn;

    gp16c2t0_hwtimer_periph.init.prescaler = ES_GP16C2T0_HWTIMER_PRES - 1;
    gp16c2t0_hwtimer_periph.init.mode = ( ES_GP16C2T0_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    gp16c2t0_hwtimer.hwtimer_periph = &gp16c2t0_hwtimer_periph;

    gp16c2t0_hwtimer.parent.info = &gp16c2t0_info;
    gp16c2t0_hwtimer.parent.ops = &es32f0_hwtimer_ops;
    ret = rt_device_hwtimer_register(&gp16c2t0_hwtimer.parent, ES_DEVICE_NAME_GP16C2T0_HWTIMER, &gp16c2t0_hwtimer);
#endif

#ifdef BSP_USING_GP16C2T1_HWTIMER
    static timer_handle_t gp16c2t1_hwtimer_periph;

    gp16c2t1_hwtimer_periph.perh = GP16C2T1;
    gp16c2t1_hwtimer.IRQn = GP16C2T1_IRQn;

    gp16c2t1_hwtimer_periph.init.prescaler = ES_GP16C2T1_HWTIMER_PRES - 1;
    gp16c2t1_hwtimer_periph.init.mode = ( ES_GP16C2T1_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    gp16c2t1_hwtimer.hwtimer_periph = &gp16c2t1_hwtimer_periph;

    gp16c2t1_hwtimer.parent.info = &gp16c2t1_info;
    gp16c2t1_hwtimer.parent.ops = &es32f0_hwtimer_ops;
    ret = rt_device_hwtimer_register(&gp16c2t1_hwtimer.parent, ES_DEVICE_NAME_GP16C2T1_HWTIMER, &gp16c2t1_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 = &es32f0_hwtimer_ops;
    ret = rt_device_hwtimer_register(&bs16t0_hwtimer.parent, ES_DEVICE_NAME_BS16T0_HWTIMER, &bs16t0_hwtimer);
#endif

#ifdef BSP_USING_BS16T1_HWTIMER
    static timer_handle_t bs16t1_hwtimer_periph;

    bs16t1_hwtimer_periph.perh = BS16T1;
    bs16t1_hwtimer.IRQn = BS16T1_UART2_IRQn;

    bs16t1_hwtimer_periph.init.prescaler = ES_BS16T1_HWTIMER_PRES - 1;
    bs16t1_hwtimer_periph.init.mode = ( ES_BS16T1_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    bs16t1_hwtimer.hwtimer_periph = &bs16t1_hwtimer_periph;

    bs16t1_hwtimer.parent.info = &bs16t1_info;
    bs16t1_hwtimer.parent.ops = &es32f0_hwtimer_ops;
    ret = rt_device_hwtimer_register(&bs16t1_hwtimer.parent, ES_DEVICE_NAME_BS16T1_HWTIMER, &bs16t1_hwtimer);
#endif

#ifdef BSP_USING_BS16T2_HWTIMER
    static timer_handle_t bs16t2_hwtimer_periph;

    bs16t2_hwtimer_periph.perh = BS16T2;
    bs16t2_hwtimer.IRQn = BS16T2_UART3_IRQn;

    bs16t2_hwtimer_periph.init.prescaler = ES_BS16T2_HWTIMER_PRES - 1;
    bs16t2_hwtimer_periph.init.mode = ( ES_BS16T2_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    bs16t2_hwtimer.hwtimer_periph = &bs16t2_hwtimer_periph;

    bs16t2_hwtimer.parent.info = &bs16t2_info;
    bs16t2_hwtimer.parent.ops = &es32f0_hwtimer_ops;
    ret = rt_device_hwtimer_register(&bs16t2_hwtimer.parent, ES_DEVICE_NAME_BS16T2_HWTIMER, &bs16t2_hwtimer);
#endif

#ifdef BSP_USING_BS16T3_HWTIMER
    static timer_handle_t bs16t3_hwtimer_periph;

    bs16t3_hwtimer_periph.perh = BS16T3;
    bs16t3_hwtimer.IRQn = BS16T3_DAC0_IRQn;

    bs16t3_hwtimer_periph.init.prescaler = ES_BS16T3_HWTIMER_PRES - 1;
    bs16t3_hwtimer_periph.init.mode = ( ES_BS16T3_HWTIMER_MODE == HWTIMER_CNTMODE_UP )? TIMER_CNT_MODE_UP : TIMER_CNT_MODE_DOWN;
    bs16t3_hwtimer.hwtimer_periph = &bs16t3_hwtimer_periph;

    bs16t3_hwtimer.parent.info = &bs16t3_info;
    bs16t3_hwtimer.parent.ops = &es32f0_hwtimer_ops;
    ret = rt_device_hwtimer_register(&bs16t3_hwtimer.parent, ES_DEVICE_NAME_BS16T3_HWTIMER, &bs16t3_hwtimer);
#endif


    return ret;
}
INIT_BOARD_EXPORT(rt_hw_hwtimer_init);

#endif