summaryrefslogtreecommitdiff
path: root/bsp/w60x/drivers/drv_hw_timer.c
blob: d52c9c09a6eb3954536f86964aea02d2b8837aa6 (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
/*
 * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-19     fanwenl      1st version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include "wm_type_def.h"
#include "wm_timer.h"
#include "drv_hw_timer.h"

#ifdef BSP_USING_HWTIMER

struct wm_timer_Type
{
    enum tls_timer_unit unit;
    enum tls_timer_id id;
};

static void wm_timer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{
    struct tls_timer_cfg timer_cfg;
    struct wm_timer_Type *wm_timer = (struct wm_timer_Type *)timer->parent.user_data;

    timer_cfg.unit = wm_timer->unit;
    timer_cfg.timeout = 0xFFFFFFFF;
    timer_cfg.is_repeat = 0;
    timer_cfg.callback = NULL;
    timer_cfg.arg = NULL;

    if (state == 1)
    {
        tls_timer_create(&timer_cfg, wm_timer->id);
    }
    else if (state == 0)
    {
        tls_timer_destroy(wm_timer->id);
    }
}

static rt_err_t wm_timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
{
    struct wm_timer_Type *wm_timer = (struct wm_timer_Type *)timer->parent.user_data;
    uint8_t m;

    tls_timer_change(wm_timer->id, t);
    m = (opmode == HWTIMER_MODE_ONESHOT) ? 0 : 1;
    tls_timer_set_mode(wm_timer->id, m);
    tls_timer_start(wm_timer->id);

    return RT_EOK;
}

static void wm_timer_stop(rt_hwtimer_t *timer)
{
    struct wm_timer_Type *wm_timer = (struct wm_timer_Type *)timer->parent.user_data;

    tls_timer_stop(wm_timer->id);
}

static rt_uint32_t wm_timer_get(rt_hwtimer_t *timer)
{
    return 0;
}

static rt_err_t wm_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
{
    /* The frequency value is an immutable value. */
    if (cmd != HWTIMER_CTRL_FREQ_SET)
    {
        return -RT_ENOSYS;
    }
    if ( *(rt_uint32_t*)arg == 1000000)
    {
        return RT_EOK;
    }
    else
    {
        return -RT_ENOSYS;
    }
}

static const struct rt_hwtimer_info _info =
{
    1000000,            /* the maximum count frequency can be set */
    1000000,            /* the minimum count frequency can be set */
    0xFFFFFFFF,         /* the maximum counter value */
    HWTIMER_CNTMODE_DW, /* Increment or Decreasing count mode */
};

static const struct rt_hwtimer_ops _ops =
{
    wm_timer_init,
    wm_timer_start,
    wm_timer_stop,
    wm_timer_get,
    wm_timer_ctrl,
};

#ifdef USING_HW_TIMER1
static rt_hwtimer_t _timer1;
static struct wm_timer_Type wm_timer1;
#endif
#ifdef USING_HW_TIMER2
static rt_hwtimer_t _timer2;
static struct wm_timer_Type wm_timer2;
#endif
#ifdef USING_HW_TIMER3
static rt_hwtimer_t _timer3;
static struct wm_timer_Type wm_timer3;
#endif
#ifdef USING_HW_TIMER4
static rt_hwtimer_t _timer4;
static struct wm_timer_Type wm_timer4;
#endif
#ifdef USING_HW_TIMER5
static rt_hwtimer_t _timer5;
static struct wm_timer_Type wm_timer5;
#endif

int wm_hw_timer_init(void)
{
#ifdef USING_HW_TIMER1
    wm_timer1.id = TLS_TIMER_ID_1;
    wm_timer1.unit = TLS_TIMER_UNIT_US;

    _timer1.info = &_info;
    _timer1.ops = &_ops;

    rt_device_hwtimer_register(&_timer1, "timer1", &wm_timer1);
#endif
#ifdef USING_HW_TIMER2
    wm_timer2.id = TLS_TIMER_ID_2;
    wm_timer2.unit = TLS_TIMER_UNIT_US;

    _timer2.info = &_info;
    _timer2.ops = &_ops;

    rt_device_hwtimer_register(&_timer2, "timer2", &wm_timer2);
#endif
#ifdef USING_HW_TIMER3
    wm_timer3.id = TLS_TIMER_ID_3;
    wm_timer3.unit = TLS_TIMER_UNIT_US;

    _timer3.info = &_info;
    _timer3.ops = &_ops;

    rt_device_hwtimer_register(&_timer3, "timer3", &wm_timer3);
#endif
#ifdef USING_HW_TIMER4
    wm_timer4.id = TLS_TIMER_ID_4;
    wm_timer4.unit = TLS_TIMER_UNIT_US;

    _timer4.info = &_info;
    _timer4.ops = &_ops;

    rt_device_hwtimer_register(&_timer4, "timer4", &wm_timer4);
#endif
#ifdef USING_HW_TIMER5
    wm_timer5.id = TLS_TIMER_ID_5;
    wm_timer5.unit = TLS_TIMER_UNIT_US;

    _timer5.info = &_info;
    _timer5.ops = &_ops;

    rt_device_hwtimer_register(&_timer5, "timer5", &wm_timer5);
#endif
    return 0;
}
INIT_BOARD_EXPORT(wm_hw_timer_init);

void TIM1_IRQHandler(void)
{
    timer_clear_irq(1);
#ifdef USING_HW_TIMER1
    rt_device_hwtimer_isr(&_timer1);
#endif
}
void TIM2_IRQHandler(void)
{
    timer_clear_irq(2);
#ifdef USING_HW_TIMER2
    rt_device_hwtimer_isr(&_timer2);
#endif
}
void TIM3_IRQHandler(void)
{
    timer_clear_irq(3);
#ifdef USING_HW_TIMER3
    rt_device_hwtimer_isr(&_timer3);
#endif
}
void TIM4_IRQHandler(void)
{
    timer_clear_irq(4);
#ifdef USING_HW_TIMER4
    rt_device_hwtimer_isr(&_timer4);
#endif
}
void TIM5_IRQHandler(void)
{
    timer_clear_irq(5);
#ifdef USING_HW_TIMER5
    rt_device_hwtimer_isr(&_timer5);
#endif
}
#endif /* BSP_USING_HWTIMER */