1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup lpc_clock |
---|
5 | * |
---|
6 | * @brief Clock driver configuration. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009-2011 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Obere Lagerstr. 30 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.com/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <bsp/lpc-clock-config.h> |
---|
24 | #include <bsp/lpc-timer.h> |
---|
25 | |
---|
26 | /* This is defined in ../../../shared/clockdrv_shell.h */ |
---|
27 | rtems_isr Clock_isr(rtems_vector_number vector); |
---|
28 | |
---|
29 | static volatile lpc_timer *const lpc_clock = |
---|
30 | (volatile lpc_timer *) LPC_CLOCK_TIMER_BASE; |
---|
31 | |
---|
32 | static void lpc_clock_at_tick(void) |
---|
33 | { |
---|
34 | lpc_clock->ir = LPC_TIMER_IR_MR0; |
---|
35 | } |
---|
36 | |
---|
37 | static void lpc_clock_handler_install(void) |
---|
38 | { |
---|
39 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
40 | |
---|
41 | sc = rtems_interrupt_handler_install( |
---|
42 | LPC_CLOCK_INTERRUPT, |
---|
43 | "Clock", |
---|
44 | RTEMS_INTERRUPT_UNIQUE, |
---|
45 | (rtems_interrupt_handler) Clock_isr, |
---|
46 | NULL |
---|
47 | ); |
---|
48 | if (sc != RTEMS_SUCCESSFUL) { |
---|
49 | rtems_fatal_error_occurred(0xdeadbeef); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | static void lpc_clock_initialize(void) |
---|
54 | { |
---|
55 | uint64_t interval = ((uint64_t) LPC_CLOCK_REFERENCE |
---|
56 | * (uint64_t) rtems_configuration_get_microseconds_per_tick()) / 1000000; |
---|
57 | |
---|
58 | /* Enable module */ |
---|
59 | LPC_CLOCK_MODULE_ENABLE(); |
---|
60 | |
---|
61 | /* Reset timer */ |
---|
62 | lpc_clock->tcr = LPC_TIMER_TCR_RST; |
---|
63 | |
---|
64 | /* Clear interrupt flags */ |
---|
65 | lpc_clock->ir = LPC_TIMER_IR_ALL; |
---|
66 | |
---|
67 | /* Set timer mode */ |
---|
68 | lpc_clock->ccr = 0; |
---|
69 | |
---|
70 | /* Timer is incremented every PERIPH_CLK tick */ |
---|
71 | lpc_clock->pr = 0; |
---|
72 | |
---|
73 | /* Set match registers */ |
---|
74 | lpc_clock->mr0 = (uint32_t) interval; |
---|
75 | |
---|
76 | /* Generate interrupt and reset counter on match with MR0 */ |
---|
77 | lpc_clock->mcr = LPC_TIMER_MCR_MR0_INTR | LPC_TIMER_MCR_MR0_RST; |
---|
78 | |
---|
79 | /* No external match */ |
---|
80 | lpc_clock->emr = 0x0; |
---|
81 | |
---|
82 | /* Enable timer */ |
---|
83 | lpc_clock->tcr = LPC_TIMER_TCR_EN; |
---|
84 | } |
---|
85 | |
---|
86 | static void lpc_clock_cleanup(void) |
---|
87 | { |
---|
88 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
89 | |
---|
90 | /* Disable timer */ |
---|
91 | lpc_clock->tcr = 0x0; |
---|
92 | |
---|
93 | /* Remove interrupt handler */ |
---|
94 | sc = rtems_interrupt_handler_remove( |
---|
95 | LPC_CLOCK_INTERRUPT, |
---|
96 | (rtems_interrupt_handler) Clock_isr, |
---|
97 | NULL |
---|
98 | ); |
---|
99 | if (sc != RTEMS_SUCCESSFUL) { |
---|
100 | rtems_fatal_error_occurred(0xdeadbeef); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | static uint32_t lpc_clock_nanoseconds_since_last_tick(void) |
---|
105 | { |
---|
106 | uint64_t k = (1000000000ULL << 32) / LPC_CLOCK_REFERENCE; |
---|
107 | uint64_t c = lpc_clock->tc; |
---|
108 | |
---|
109 | if ((lpc_clock->ir & LPC_TIMER_IR_MR0) != 0) { |
---|
110 | c = lpc_clock->tc + lpc_clock->mr0; |
---|
111 | } |
---|
112 | |
---|
113 | return (uint32_t) ((c * k) >> 32); |
---|
114 | } |
---|
115 | |
---|
116 | #define Clock_driver_support_at_tick() lpc_clock_at_tick() |
---|
117 | #define Clock_driver_support_initialize_hardware() lpc_clock_initialize() |
---|
118 | #define Clock_driver_support_install_isr(isr, old_isr) \ |
---|
119 | do { \ |
---|
120 | lpc_clock_handler_install(); \ |
---|
121 | old_isr = NULL; \ |
---|
122 | } while (0) |
---|
123 | |
---|
124 | #define Clock_driver_support_shutdown_hardware() lpc_clock_cleanup() |
---|
125 | #define Clock_driver_nanoseconds_since_last_tick \ |
---|
126 | lpc_clock_nanoseconds_since_last_tick |
---|
127 | |
---|
128 | /* Include shared source clock driver code */ |
---|
129 | #include "../../../../shared/clockdrv_shell.h" |
---|