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