1 | /** |
---|
2 | * @file clock.c |
---|
3 | * |
---|
4 | * @ingroup tms570 |
---|
5 | * |
---|
6 | * @brief clock functions definitions. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2014 Premysl Houdek <kom541000@gmail.com> |
---|
11 | * |
---|
12 | * Google Summer of Code 2014 at |
---|
13 | * Czech Technical University in Prague |
---|
14 | * Zikova 1903/4 |
---|
15 | * 166 36 Praha 6 |
---|
16 | * Czech Republic |
---|
17 | * |
---|
18 | * Based on LPC24xx and LPC1768 BSP |
---|
19 | * by embedded brains GmbH and others |
---|
20 | * |
---|
21 | * The license and distribution terms for this file may be |
---|
22 | * found in the file LICENSE in this distribution or at |
---|
23 | * http://www.rtems.org/license/LICENSE. |
---|
24 | */ |
---|
25 | |
---|
26 | #include <stdlib.h> |
---|
27 | |
---|
28 | #include <rtems.h> |
---|
29 | #include <bsp.h> |
---|
30 | #include <bsp/irq.h> |
---|
31 | #include <bsp/tms570-rti.h> |
---|
32 | #include <rtems/counter.h> |
---|
33 | #include <rtems/timecounter.h> |
---|
34 | |
---|
35 | static struct timecounter tms570_rti_tc; |
---|
36 | |
---|
37 | static uint32_t tms570_rti_get_timecount(struct timecounter *tc) |
---|
38 | { |
---|
39 | return TMS570_RTI.CNT[0].FRCx; |
---|
40 | } |
---|
41 | |
---|
42 | #ifndef TMS570_PREFERRED_TC_FREQUENCY |
---|
43 | /* |
---|
44 | * Define preferred main time base counter frequency |
---|
45 | * The value of 1MHz is the best matching RTEMS |
---|
46 | * timing system because then there is no need |
---|
47 | * to scale RTEMS configuration microseconds_per_tick |
---|
48 | * parameter |
---|
49 | */ |
---|
50 | #define TMS570_PREFERRED_TC_FREQUENCY 1000000 |
---|
51 | #endif /* TMS570_PREFERRED_TC_FREQUENCY */ |
---|
52 | |
---|
53 | /** |
---|
54 | * @brief Initialize the HW peripheral for clock driver |
---|
55 | * |
---|
56 | * Clock driver is implemented by RTI module |
---|
57 | * |
---|
58 | * @retval Void |
---|
59 | */ |
---|
60 | static void tms570_clock_driver_support_initialize_hardware( void ) |
---|
61 | { |
---|
62 | |
---|
63 | uint32_t microsec_per_tick; |
---|
64 | uint32_t tc_frequency; |
---|
65 | uint32_t tc_prescaler; |
---|
66 | uint32_t tc_increments_per_tick; |
---|
67 | |
---|
68 | microsec_per_tick = rtems_configuration_get_microseconds_per_tick(); |
---|
69 | tc_frequency = TMS570_PREFERRED_TC_FREQUENCY; |
---|
70 | |
---|
71 | rtems_counter_initialize_converter(BSP_PLL_OUT_CLOCK); |
---|
72 | |
---|
73 | tc_prescaler = (BSP_PLL_OUT_CLOCK + tc_frequency) / (tc_frequency * 2); |
---|
74 | |
---|
75 | /* Recompute actual most close frequency which can be realized */ |
---|
76 | tc_frequency = (BSP_PLL_OUT_CLOCK + tc_prescaler) / (tc_prescaler * 2); |
---|
77 | |
---|
78 | /* |
---|
79 | * Recompute tick period to adjust for configurable or exact |
---|
80 | * preferred time base 1 usec resolution. |
---|
81 | */ |
---|
82 | tc_increments_per_tick = ((uint64_t)microsec_per_tick * tc_frequency + |
---|
83 | 500000) / 1000000; |
---|
84 | |
---|
85 | /* Hardware specific initialize */ |
---|
86 | TMS570_RTI.GCTRL = 0; |
---|
87 | TMS570_RTI.CNT[0].CPUCx = tc_prescaler - 1; |
---|
88 | TMS570_RTI.TBCTRL = 2; |
---|
89 | TMS570_RTI.CAPCTRL = 0; |
---|
90 | TMS570_RTI.COMPCTRL = 0; |
---|
91 | /* set counter to zero */ |
---|
92 | TMS570_RTI.CNT[0].UCx = 0; |
---|
93 | TMS570_RTI.CNT[0].FRCx = 0; |
---|
94 | /* clear interrupts*/ |
---|
95 | TMS570_RTI.CLEARINTENA = 0x00070f0f; |
---|
96 | TMS570_RTI.INTFLAG = 0x0007000f; |
---|
97 | /* set timer */ |
---|
98 | TMS570_RTI.CMP[0].COMPx = TMS570_RTI.CNT[0].FRCx + tc_increments_per_tick; |
---|
99 | TMS570_RTI.COMP0CLR = TMS570_RTI.CMP[0].COMPx + tc_increments_per_tick / 2; |
---|
100 | TMS570_RTI.CMP[0].UDCPx = tc_increments_per_tick; |
---|
101 | /* enable interupt */ |
---|
102 | TMS570_RTI.SETINTENA = 0x1; |
---|
103 | /* enable timer */ |
---|
104 | TMS570_RTI.GCTRL = 1; |
---|
105 | /* set timecounter */ |
---|
106 | tms570_rti_tc.tc_get_timecount = tms570_rti_get_timecount; |
---|
107 | tms570_rti_tc.tc_counter_mask = 0xffffffff; |
---|
108 | tms570_rti_tc.tc_frequency = tc_frequency; |
---|
109 | tms570_rti_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
110 | rtems_timecounter_install(&tms570_rti_tc); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * @brief Clears interrupt source |
---|
115 | * |
---|
116 | * @retval Void |
---|
117 | */ |
---|
118 | static void tms570_clock_driver_support_at_tick( void ) |
---|
119 | { |
---|
120 | TMS570_RTI.INTFLAG = 0x00000001; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * @brief registers RTI interrupt handler |
---|
125 | * |
---|
126 | * @param[in] Clock_isr new ISR handler |
---|
127 | * @param[in] Old_ticker old ISR handler (unused and type broken) |
---|
128 | * |
---|
129 | * @retval Void |
---|
130 | */ |
---|
131 | static void tms570_clock_driver_support_install_isr( |
---|
132 | rtems_isr_entry Clock_isr |
---|
133 | ) |
---|
134 | { |
---|
135 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
136 | |
---|
137 | sc = rtems_interrupt_handler_install( |
---|
138 | TMS570_IRQ_TIMER_0, |
---|
139 | "Clock", |
---|
140 | RTEMS_INTERRUPT_UNIQUE, |
---|
141 | (rtems_interrupt_handler) Clock_isr, |
---|
142 | NULL |
---|
143 | ); |
---|
144 | if ( sc != RTEMS_SUCCESSFUL ) { |
---|
145 | rtems_fatal_error_occurred(0xdeadbeef); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * @brief disables RTI interrupt |
---|
151 | * |
---|
152 | * Called when closing clock driver |
---|
153 | * |
---|
154 | * @retval Void |
---|
155 | */ |
---|
156 | static void tms570_clock_driver_support_shutdown_hardware( void ) |
---|
157 | { |
---|
158 | /* turn off the timer interrupts */ |
---|
159 | TMS570_RTI.CLEARINTENA = 0x20000; |
---|
160 | } |
---|
161 | |
---|
162 | #define Clock_driver_support_initialize_hardware \ |
---|
163 | tms570_clock_driver_support_initialize_hardware |
---|
164 | #define Clock_driver_support_at_tick \ |
---|
165 | tms570_clock_driver_support_at_tick |
---|
166 | #define Clock_driver_support_initialize_hardware \ |
---|
167 | tms570_clock_driver_support_initialize_hardware |
---|
168 | #define Clock_driver_support_shutdown_hardware \ |
---|
169 | tms570_clock_driver_support_shutdown_hardware |
---|
170 | |
---|
171 | #define Clock_driver_support_install_isr(Clock_isr, Old_ticker ) \ |
---|
172 | tms570_clock_driver_support_install_isr( Clock_isr ) |
---|
173 | |
---|
174 | void Clock_isr(void *arg); /* to supress warning */ |
---|
175 | |
---|
176 | #include "../../../shared/clockdrv_shell.h" |
---|