-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsystick.h
120 lines (107 loc) · 3.11 KB
/
systick.h
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
#ifndef __SYSTICK_H__
#define __SYSTICK_H__
#include "memory.h"
//-----------------------------------------------------------------
// Defines
//-----------------------------------------------------------------
#define SYSTICK_CSR (0) // SYST_CSR RW -[a] SysTick Control and Status Register
#define SYSTICK_CSR_COUNTFLAG (1 << 16)
#define SYSTICK_CSR_CLKSOURCE_REF 0x0
#define SYSTICK_CSR_CLKSOURCE_CPU 0x4
#define SYSTICK_CSR_TICKINT_EN 0x2
#define SYSTICK_CSR_ENABLE 0x1
#define SYSTICK_RVR (4) // SYST_RVR RW Unknown SysTick Reload Value Register
#define SYSTICK_CVR (8) // SYST_CVR RW Unknown SysTick Current Value Register
#define SYSTICK_CALIB (12) // SYST_CALIB RO - [a] SysTick Calibration Value Register
//-----------------------------------------------------------------
// Systick: Timer device
//-----------------------------------------------------------------
class Systick: public Device
{
public:
Systick(uint32_t base_addr, int irq_num)
{
m_base_addr = base_addr;
m_irq_number = irq_num;
reset();
}
void reset(void)
{
m_irq = false;
m_reg_csr = 0;
m_reg_reload = 0;
m_reg_current = 0;
}
void write_reg(uint32_t address, uint32_t data)
{
switch (address)
{
case SYSTICK_CSR:
m_reg_csr = data;
break;
case SYSTICK_RVR:
m_reg_reload = data;
break;
case SYSTICK_CVR:
m_reg_current = data;
break;
default:
fprintf(stderr, "Systick: Bad write @ %08x\n", address);
exit (-1);
break;
}
}
uint32_t read_reg(uint32_t address)
{
uint32_t data = 0;
switch (address)
{
case SYSTICK_CSR:
data = m_reg_csr;
// Clear overflow flag
m_reg_csr &= ~SYSTICK_CSR_COUNTFLAG;
break;
case SYSTICK_RVR:
data = m_reg_reload;
break;
case SYSTICK_CVR:
data = m_reg_current;
break;
case SYSTICK_CALIB:
data = 0;
break;
default:
fprintf(stderr, "Systick: Bad read @ %08x\n", address);
exit (-1);
break;
}
return data;
}
int clock(void)
{
// Systick
if (m_reg_csr & SYSTICK_CSR_ENABLE)
{
if (m_reg_current == 0)
{
m_reg_current = m_reg_reload;
m_reg_csr |= SYSTICK_CSR_COUNTFLAG;
if (m_reg_csr & SYSTICK_CSR_TICKINT_EN)
m_irq = true;
}
else
m_reg_current -= 1;
}
bool irq = m_irq;
m_irq = false;
return irq ? m_irq_number : -1;
}
private:
uint32_t m_base_addr;
int m_irq_number;
bool m_irq;
uint32_t m_reg_csr;
uint32_t m_reg_reload;
uint32_t m_reg_current;
};
#endif