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