1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup lpc24xx_clocks |
---|
5 | * |
---|
6 | * @brief System clocks. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2008 |
---|
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 found in the file |
---|
18 | * LICENSE in this distribution or at http://www.rtems.com/license/LICENSE. |
---|
19 | */ |
---|
20 | |
---|
21 | #include <bsp.h> |
---|
22 | #include <bsp/utility.h> |
---|
23 | #include <bsp/lpc24xx.h> |
---|
24 | #include <bsp/system-clocks.h> |
---|
25 | |
---|
26 | /** |
---|
27 | * @brief Internal RC oscillator frequency in [Hz]. |
---|
28 | */ |
---|
29 | #define LPC24XX_OSCILLATOR_INTERNAL 4000000U |
---|
30 | |
---|
31 | #ifndef LPC24XX_OSCILLATOR_MAIN |
---|
32 | #error "unknown main oscillator frequency" |
---|
33 | #endif |
---|
34 | |
---|
35 | #ifndef LPC24XX_OSCILLATOR_RTC |
---|
36 | #error "unknown RTC oscillator frequency" |
---|
37 | #endif |
---|
38 | |
---|
39 | void lpc24xx_timer_initialize(void) |
---|
40 | { |
---|
41 | /* Reset timer */ |
---|
42 | T1TCR = TCR_RST; |
---|
43 | |
---|
44 | /* Set timer mode */ |
---|
45 | T1CTCR = 0; |
---|
46 | |
---|
47 | /* Set prescaler to zero */ |
---|
48 | T1PR = 0; |
---|
49 | |
---|
50 | /* Reset all interrupt flags */ |
---|
51 | T1IR = 0xff; |
---|
52 | |
---|
53 | /* Do not stop on a match */ |
---|
54 | T1MCR = 0; |
---|
55 | |
---|
56 | /* No captures */ |
---|
57 | T1CCR = 0; |
---|
58 | |
---|
59 | /* Start timer */ |
---|
60 | T1TCR = TCR_EN; |
---|
61 | } |
---|
62 | |
---|
63 | void lpc24xx_micro_seconds_delay(unsigned us) |
---|
64 | { |
---|
65 | unsigned start = lpc24xx_timer(); |
---|
66 | unsigned delay = us * (lpc24xx_cclk() / 1000000); |
---|
67 | unsigned elapsed = 0; |
---|
68 | |
---|
69 | do { |
---|
70 | elapsed = lpc24xx_timer() - start; |
---|
71 | } while (elapsed < delay); |
---|
72 | } |
---|
73 | |
---|
74 | unsigned lpc24xx_pllclk(void) |
---|
75 | { |
---|
76 | unsigned clksrc = GET_CLKSRCSEL_CLKSRC(CLKSRCSEL); |
---|
77 | unsigned pllinclk = 0; |
---|
78 | unsigned pllclk = 0; |
---|
79 | |
---|
80 | /* Get PLL input frequency */ |
---|
81 | switch (clksrc) { |
---|
82 | case 0: |
---|
83 | pllinclk = LPC24XX_OSCILLATOR_INTERNAL; |
---|
84 | break; |
---|
85 | case 1: |
---|
86 | pllinclk = LPC24XX_OSCILLATOR_MAIN; |
---|
87 | break; |
---|
88 | case 2: |
---|
89 | pllinclk = LPC24XX_OSCILLATOR_RTC; |
---|
90 | break; |
---|
91 | default: |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | /* Get PLL output frequency */ |
---|
96 | if ((PLLSTAT & PLLSTAT_PLLC) != 0) { |
---|
97 | uint32_t pllcfg = PLLCFG; |
---|
98 | unsigned n = GET_PLLCFG_NSEL(pllcfg) + 1; |
---|
99 | unsigned m = GET_PLLCFG_MSEL(pllcfg) + 1; |
---|
100 | |
---|
101 | pllclk = (pllinclk / n) * 2 * m; |
---|
102 | } else { |
---|
103 | pllclk = pllinclk; |
---|
104 | } |
---|
105 | |
---|
106 | return pllclk; |
---|
107 | } |
---|
108 | |
---|
109 | unsigned lpc24xx_cclk(void) |
---|
110 | { |
---|
111 | /* Get PLL output frequency */ |
---|
112 | unsigned pllclk = lpc24xx_pllclk(); |
---|
113 | |
---|
114 | /* Get CPU frequency */ |
---|
115 | unsigned cclk = pllclk / (GET_CCLKCFG_CCLKSEL(CCLKCFG) + 1); |
---|
116 | |
---|
117 | return cclk; |
---|
118 | } |
---|