summaryrefslogtreecommitdiff
path: root/bsp/ck802/drivers/board_coretimer.c
blob: d3275e68d87c48f669eff5882369c223ecdfaa27 (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
/*
 * File      : board_coretimer.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-01-01     Urey      first version
 */
#include <rthw.h>
#include <rtthread.h>

#include "board.h"
#include "board_coretimer.h"

#include <stdint.h>

static inline unsigned int readreg32(volatile unsigned int *addr)
{
    return *(volatile unsigned int *)addr;
}

static inline void writereg32(unsigned int b, volatile unsigned int *addr)
{
    *(volatile unsigned int *)addr = b;
}

void CKTimerInit(uint32_t timer_id, uint32_t freq)
{
    uint32_t reg;

    writereg32(APB_DEFAULT_FREQ / freq, CORET_RVR);
    writereg32(0, CORET_CVR);

    reg = readreg32(CORET_CSR);
    reg |= CORETIM_TXCONTROL_ENABLE;
    reg |= CORETIM_TXCONTROL_INTMASK;
    writereg32(reg, CORET_CSR);

    return;
}

void CKTimerClear(uint32_t timer_id)
{
    uint32_t reg;

    reg = readreg32(CORET_CSR);
    reg |= ~CORETIM_TXCONTROL_MODE;
    writereg32(reg, CORET_CSR);
}

uint32_t CKTimer_CurrentValue(void)
{
    return readreg32(CORET_CVR);
}

void __attribute__((isr)) SysTick_Handler(void)
{
    CKTimerClear(0x1);
    /* enter interrupt */
    rt_interrupt_enter();

    rt_tick_increase();

    /* leave interrupt */
    rt_interrupt_leave();
}