1 | /* |
---|
2 | * Copyright (c) 2011-2012 Sebastian Huber. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <rtems.h> |
---|
16 | #include <rtems/timecounter.h> |
---|
17 | #include <rtems/score/armv7m.h> |
---|
18 | |
---|
19 | #include <bsp.h> |
---|
20 | |
---|
21 | #ifdef ARM_MULTILIB_ARCH_V7M |
---|
22 | |
---|
23 | /* This is defined in clockdrv_shell.h */ |
---|
24 | static void Clock_isr(void *arg); |
---|
25 | |
---|
26 | typedef struct { |
---|
27 | rtems_timecounter_simple base; |
---|
28 | void (*tick)(void); |
---|
29 | bool countflag; |
---|
30 | } ARMV7M_Timecounter; |
---|
31 | |
---|
32 | static ARMV7M_Timecounter _ARMV7M_TC; |
---|
33 | |
---|
34 | static uint32_t _ARMV7M_TC_systick_get(rtems_timecounter_simple *tc) |
---|
35 | { |
---|
36 | volatile ARMV7M_Systick *systick = _ARMV7M_Systick; |
---|
37 | |
---|
38 | return systick->cvr; |
---|
39 | } |
---|
40 | |
---|
41 | static bool _ARMV7M_TC_systick_is_pending(rtems_timecounter_simple *base) |
---|
42 | { |
---|
43 | ARMV7M_Timecounter *tc = (ARMV7M_Timecounter *) base; |
---|
44 | rtems_interrupt_level level; |
---|
45 | bool countflag; |
---|
46 | |
---|
47 | rtems_interrupt_disable(level); |
---|
48 | |
---|
49 | countflag = tc->countflag; |
---|
50 | if (!countflag) { |
---|
51 | volatile ARMV7M_Systick *systick = _ARMV7M_Systick; |
---|
52 | |
---|
53 | countflag = ((systick->csr & ARMV7M_SYSTICK_CSR_COUNTFLAG) != 0); |
---|
54 | tc->countflag = countflag; |
---|
55 | } |
---|
56 | |
---|
57 | rtems_interrupt_enable(level); |
---|
58 | |
---|
59 | return countflag; |
---|
60 | } |
---|
61 | |
---|
62 | static uint32_t _ARMV7M_TC_systick_get_timecount(struct timecounter *tc) |
---|
63 | { |
---|
64 | return rtems_timecounter_simple_downcounter_get( |
---|
65 | tc, |
---|
66 | _ARMV7M_TC_systick_get, |
---|
67 | _ARMV7M_TC_systick_is_pending |
---|
68 | ); |
---|
69 | } |
---|
70 | |
---|
71 | static void _ARMV7M_TC_systick_at_tick(rtems_timecounter_simple *base) |
---|
72 | { |
---|
73 | ARMV7M_Timecounter *tc = (ARMV7M_Timecounter *) base; |
---|
74 | volatile ARMV7M_Systick *systick = _ARMV7M_Systick; |
---|
75 | |
---|
76 | tc->countflag = false; |
---|
77 | |
---|
78 | /* Clear COUNTFLAG */ |
---|
79 | systick->csr; |
---|
80 | } |
---|
81 | |
---|
82 | static void _ARMV7M_TC_systick_tick(void) |
---|
83 | { |
---|
84 | rtems_timecounter_simple_downcounter_tick( |
---|
85 | &_ARMV7M_TC.base, |
---|
86 | _ARMV7M_TC_systick_get, |
---|
87 | _ARMV7M_TC_systick_at_tick |
---|
88 | ); |
---|
89 | } |
---|
90 | |
---|
91 | static uint32_t _ARMV7M_TC_dwt_get_timecount(struct timecounter *tc) |
---|
92 | { |
---|
93 | volatile ARMV7M_DWT *dwt = _ARMV7M_DWT; |
---|
94 | |
---|
95 | return dwt->cyccnt; |
---|
96 | } |
---|
97 | |
---|
98 | static void _ARMV7M_TC_dwt_tick(void) |
---|
99 | { |
---|
100 | rtems_timecounter_tick(); |
---|
101 | } |
---|
102 | |
---|
103 | static void _ARMV7M_TC_tick(void) |
---|
104 | { |
---|
105 | (*_ARMV7M_TC.tick)(); |
---|
106 | } |
---|
107 | |
---|
108 | static void _ARMV7M_Systick_handler(void) |
---|
109 | { |
---|
110 | _ARMV7M_Interrupt_service_enter(); |
---|
111 | Clock_isr(NULL); |
---|
112 | _ARMV7M_Interrupt_service_leave(); |
---|
113 | } |
---|
114 | |
---|
115 | static void _ARMV7M_Systick_handler_install(void) |
---|
116 | { |
---|
117 | _ARMV7M_Set_exception_priority_and_handler( |
---|
118 | ARMV7M_VECTOR_SYSTICK, |
---|
119 | BSP_ARMV7M_SYSTICK_PRIORITY, |
---|
120 | _ARMV7M_Systick_handler |
---|
121 | ); |
---|
122 | } |
---|
123 | |
---|
124 | static void _ARMV7M_Systick_initialize(void) |
---|
125 | { |
---|
126 | volatile ARMV7M_Systick *systick = _ARMV7M_Systick; |
---|
127 | #ifdef BSP_ARMV7M_SYSTICK_FREQUENCY |
---|
128 | uint64_t freq = BSP_ARMV7M_SYSTICK_FREQUENCY; |
---|
129 | #else |
---|
130 | uint64_t freq = ARMV7M_SYSTICK_CALIB_TENMS_GET(systick->calib) * 100ULL; |
---|
131 | #endif |
---|
132 | uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick(); |
---|
133 | uint64_t interval = (freq * us_per_tick) / 1000000ULL; |
---|
134 | bool cyccnt_enabled; |
---|
135 | |
---|
136 | systick->rvr = (uint32_t) interval; |
---|
137 | systick->cvr = 0; |
---|
138 | systick->csr = ARMV7M_SYSTICK_CSR_ENABLE |
---|
139 | | ARMV7M_SYSTICK_CSR_TICKINT |
---|
140 | | ARMV7M_SYSTICK_CSR_CLKSOURCE; |
---|
141 | |
---|
142 | cyccnt_enabled = _ARMV7M_DWT_Enable_CYCCNT(); |
---|
143 | if (cyccnt_enabled) { |
---|
144 | _ARMV7M_TC.base.tc.tc_get_timecount = _ARMV7M_TC_dwt_get_timecount; |
---|
145 | _ARMV7M_TC.base.tc.tc_counter_mask = 0xffffffff; |
---|
146 | _ARMV7M_TC.base.tc.tc_frequency = freq; |
---|
147 | _ARMV7M_TC.base.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
148 | _ARMV7M_TC.tick = _ARMV7M_TC_dwt_tick; |
---|
149 | rtems_timecounter_install(&_ARMV7M_TC.base.tc); |
---|
150 | } else { |
---|
151 | _ARMV7M_TC.tick = _ARMV7M_TC_systick_tick; |
---|
152 | rtems_timecounter_simple_install( |
---|
153 | &_ARMV7M_TC.base, |
---|
154 | freq, |
---|
155 | interval, |
---|
156 | _ARMV7M_TC_systick_get_timecount |
---|
157 | ); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | static void _ARMV7M_Systick_cleanup(void) |
---|
162 | { |
---|
163 | volatile ARMV7M_Systick *systick = _ARMV7M_Systick; |
---|
164 | |
---|
165 | systick->csr = 0; |
---|
166 | } |
---|
167 | |
---|
168 | #define Clock_driver_timecounter_tick() _ARMV7M_TC_tick() |
---|
169 | |
---|
170 | #define Clock_driver_support_initialize_hardware() \ |
---|
171 | _ARMV7M_Systick_initialize() |
---|
172 | |
---|
173 | #define Clock_driver_support_install_isr(isr, old_isr) \ |
---|
174 | do { \ |
---|
175 | _ARMV7M_Systick_handler_install(); \ |
---|
176 | old_isr = NULL; \ |
---|
177 | } while (0) |
---|
178 | |
---|
179 | #define Clock_driver_support_shutdown_hardware() \ |
---|
180 | _ARMV7M_Systick_cleanup() |
---|
181 | |
---|
182 | /* Include shared source clock driver code */ |
---|
183 | #include "../../../../shared/clockdrv_shell.h" |
---|
184 | |
---|
185 | #endif /* ARM_MULTILIB_ARCH_V7M */ |
---|