1 | /* |
---|
2 | * LPC22XX/LPC21xx clock specific using the System Timer |
---|
3 | * |
---|
4 | * Set the Time0 to generate click for RTEMS |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Copyright (c) 2006 by Ray <rayx.cn@gmail.com> |
---|
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 <bsp.h> |
---|
17 | #include <bsp/irq.h> |
---|
18 | #include <lpc22xx.h> |
---|
19 | #include <rtems/bspIo.h> /* for printk */ |
---|
20 | |
---|
21 | void Clock_isr(rtems_irq_hdl_param arg); |
---|
22 | static void clock_isr_on(const rtems_irq_connect_data *unused); |
---|
23 | static void clock_isr_off(const rtems_irq_connect_data *unused); |
---|
24 | static int clock_isr_is_on(const rtems_irq_connect_data *irq); |
---|
25 | |
---|
26 | /* Replace the first value with the clock's interrupt name. */ |
---|
27 | rtems_irq_connect_data clock_isr_data = { |
---|
28 | .name = LPC22xx_INTERRUPT_TIMER0, |
---|
29 | .hdl = Clock_isr, |
---|
30 | .handle = NULL, |
---|
31 | .on = clock_isr_on, |
---|
32 | .off = clock_isr_off, |
---|
33 | .isOn = clock_isr_is_on, |
---|
34 | }; |
---|
35 | |
---|
36 | /* use the /shared/clockdrv_shell.h code template */ |
---|
37 | |
---|
38 | /** |
---|
39 | * When we get the clock interrupt |
---|
40 | * - clear the interrupt bit? |
---|
41 | * - restart the timer? |
---|
42 | */ |
---|
43 | #define Clock_driver_support_at_tick() \ |
---|
44 | do { \ |
---|
45 | if (!(T0IR & 0x01)) \ |
---|
46 | return; \ |
---|
47 | T0IR = 0x01; \ |
---|
48 | VICVectAddr = 0x00; \ |
---|
49 | } while(0) |
---|
50 | |
---|
51 | /** |
---|
52 | * Installs the clock ISR. You shouldn't need to change this. |
---|
53 | */ |
---|
54 | #define Clock_driver_support_install_isr( _new, _old ) \ |
---|
55 | do { \ |
---|
56 | (_old) = NULL; \ |
---|
57 | BSP_install_rtems_irq_handler(&clock_isr_data); \ |
---|
58 | } while(0) |
---|
59 | |
---|
60 | /** |
---|
61 | * Initialize the hardware for the clock |
---|
62 | * - Set the frequency |
---|
63 | * - enable it |
---|
64 | * - clear any pending interrupts |
---|
65 | * |
---|
66 | * Since you may want the clock always running, you can |
---|
67 | * enable interrupts here. If you do so, the clock_isr_on(), |
---|
68 | * clock_isr_off(), and clock_isr_is_on() functions can be |
---|
69 | * NOPs. |
---|
70 | * |
---|
71 | * set timer to generate interrupt every |
---|
72 | * rtems_configuration_get_microseconds_per_tick() |
---|
73 | * MR0/(LPC22xx_Fpclk/(PR0+1)) = 10/1000 = 0.01s |
---|
74 | */ |
---|
75 | #define Clock_driver_support_initialize_hardware() \ |
---|
76 | do { \ |
---|
77 | /* disable and clear timer 0, set to */ \ |
---|
78 | T0TCR &= 0; \ |
---|
79 | /* TC is incrementet on every pclk.*/ \ |
---|
80 | T0PC = 0; \ |
---|
81 | /* initialize the timer period and prescaler */ \ |
---|
82 | T0MR0 = ((LPC22xx_Fpclk/1000 * \ |
---|
83 | rtems_configuration_get_microseconds_per_tick()) / 1000); \ |
---|
84 | /* generate interrupt when T0MR0 match T0TC and Reset Timer Count*/ \ |
---|
85 | T0MCR |= 0x03; \ |
---|
86 | /* No external match */ \ |
---|
87 | T0EMR = 0; \ |
---|
88 | /* enable timer0 */ \ |
---|
89 | T0TCR = 1; \ |
---|
90 | /* enable interrupt, skyeye will check this*/ \ |
---|
91 | T0IR |= 0x01; \ |
---|
92 | } while (0) |
---|
93 | |
---|
94 | /** |
---|
95 | * Do whatever you need to shut the clock down and remove the |
---|
96 | * interrupt handler. Since this normally only gets called on |
---|
97 | * RTEMS shutdown, you may not need to do anything other than |
---|
98 | * remove the ISR. |
---|
99 | */ |
---|
100 | #define Clock_driver_support_shutdown_hardware() \ |
---|
101 | do { \ |
---|
102 | /* Disable timer */ \ |
---|
103 | T0TCR&=~0x02; \ |
---|
104 | BSP_remove_rtems_irq_handler(&clock_isr_data); \ |
---|
105 | } while (0) |
---|
106 | |
---|
107 | static uint32_t bsp_clock_nanoseconds_since_last_tick(void) |
---|
108 | { |
---|
109 | uint32_t clicks; |
---|
110 | uint32_t microseconds; |
---|
111 | |
---|
112 | clicks = T0TC; /* T0TC is the 32bit time counter 0 */ |
---|
113 | |
---|
114 | microseconds = (rtems_configuration_get_microseconds_per_tick() - clicks); |
---|
115 | return microseconds * 1000; |
---|
116 | } |
---|
117 | |
---|
118 | #define Clock_driver_nanoseconds_since_last_tick \ |
---|
119 | bsp_clock_nanoseconds_since_last_tick |
---|
120 | |
---|
121 | /** |
---|
122 | * Enables clock interrupt. |
---|
123 | * |
---|
124 | * If the interrupt is always on, this can be a NOP. |
---|
125 | */ |
---|
126 | static void clock_isr_on(const rtems_irq_connect_data *unused) |
---|
127 | { |
---|
128 | T0IR&=0x01; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Disables clock interrupts |
---|
133 | * |
---|
134 | * If the interrupt is always on, this can be a NOP. |
---|
135 | */ |
---|
136 | static void clock_isr_off(const rtems_irq_connect_data *unused) |
---|
137 | { |
---|
138 | T0IR=0x00; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * Tests to see if clock interrupt is enabled, and returns 1 if so. |
---|
143 | * If interrupt is not enabled, returns 0. |
---|
144 | * |
---|
145 | * If the interrupt is always on, this always returns 1. |
---|
146 | */ |
---|
147 | static int clock_isr_is_on(const rtems_irq_connect_data *irq) |
---|
148 | { |
---|
149 | return T0IR & 0x01; /* MR0 mask */ |
---|
150 | } |
---|
151 | |
---|
152 | /* Make sure to include this, and only at the end of the file */ |
---|
153 | #include "../../../../libbsp/shared/clockdrv_shell.h" |
---|
154 | |
---|