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