Skip to content

Commit 2cbdf19

Browse files
author
Adrian Negreanu
committed
lpc43xx_cgu: remove
Signed-off-by: Adrian Negreanu <adrian.negreanu@nxp.com>
1 parent 10103f1 commit 2cbdf19

File tree

4 files changed

+116
-1418
lines changed

4 files changed

+116
-1418
lines changed

source/hic_hal/nxp/lpc4322/board_LPC43xx.c

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,112 @@
2020
*/
2121

2222
#include "sdk.h"
23-
#include "lpc43xx_cgu.h"
23+
#include "LPC43xx.h"
24+
25+
#define XTAL_OSC_HZ 12000000
26+
27+
28+
static void wait_usec(uint32_t clock_hz, volatile uint32_t us)
29+
{
30+
us *= (clock_hz / 1000000) / 3;
31+
32+
while (us--);
33+
}
34+
35+
36+
static void clock_setup_base_m4(uint32_t clock_hz)
37+
{
38+
uint32_t msel;
39+
uint32_t nsel;
40+
uint32_t tmp;
41+
42+
// 0. Select IRC as BASE_M4_CLK source
43+
tmp = LPC_CGU->BASE_M4_CLK & ~(0x0F << 24); // clear clk_sel.
44+
LPC_CGU->BASE_M4_CLK = tmp
45+
| (0x01 << 11) // AUTOBLOCK En
46+
| (0x01 << 24) // CLK_SEL = IRC
47+
;
48+
49+
// 1. Enable the crystal oscillator @12 MHz.
50+
LPC_CGU->XTAL_OSC_CTRL &= ~(1 << 2); // HF: 0=XTAL running at freq < 15 MHz.
51+
LPC_CGU->XTAL_OSC_CTRL &= ~1; // Power up XTAL.
52+
53+
// 2. Wait 250us
54+
wait_usec(XTAL_OSC_HZ, 250);
55+
56+
// 3. Reconfigure PLL1 as follows:
57+
// - Select the M and N divider values
58+
// to produce the final desired
59+
// PLL1 output frequency.
60+
// Ex: 120MHz => M=10,N=1 => msel=9,nsel=0.
61+
msel = clock_hz / XTAL_OSC_HZ - 1;
62+
nsel = 0;
63+
tmp = LPC_CGU->PLL1_CTRL & ~((0xFF << 16) | (0x03 << 12));
64+
LPC_CGU->PLL1_CTRL = tmp | (msel << 16) | (nsel << 12);
65+
66+
// - Select the crystal oscillator as
67+
// clock source for PLL1.
68+
tmp = LPC_CGU->PLL1_CTRL & ~(0x0F << 24); // clear CLK_SEL.
69+
LPC_CGU->PLL1_CTRL = tmp
70+
| (0x01 << 11) // AUTOBLOCK: 0=Disabled, 1=Enabled.
71+
| (0x06 << 24) // CLK_SEL = XTAL.
72+
;
73+
74+
// 4. Wait for the PLL1 to lock
75+
while ((LPC_CGU->PLL1_STAT & 1) == 0x0)
76+
{}
77+
78+
// 5. Set PLL1 P-divider to divide by 2 (DIRECT=0 and PSEL=0)
79+
LPC_CGU->PLL1_CTRL &= ~((0x03 << 8) | (1 << 7));
80+
81+
// 6. Select PLL1 as BASE_M4_CLK source.
82+
// BASE_M4_CLK is now operating in the
83+
// mid frequency range (90 MHz - 110 Mhz).
84+
tmp = LPC_CGU->BASE_M4_CLK & ~(0x0F << 24); // clear CLK_SEL.
85+
LPC_CGU->BASE_M4_CLK = tmp
86+
| (0x01 << 11) // AUTOBLOCK: 0=Disabled, 1=Enabled.
87+
| (0x09 << 24) // CLK_SEL = PLL1.
88+
;
89+
// 7. Wait 20us
90+
tmp = (XTAL_OSC_HZ * (msel + 1)) / ((nsel + 1) * 2);
91+
wait_usec(tmp, 20);
92+
93+
// 8. Set PLL P-divider to direct output mode (DIRECT=1)
94+
LPC_CGU->PLL1_CTRL |= 1 << 7;
95+
96+
// BASE_M4_CLK is now operating in the
97+
// high frequency range.
98+
SystemCoreClock = (XTAL_OSC_HZ * (msel + 1)) / (nsel + 1);
99+
}
100+
101+
static void clock_setup_pll0usb()
102+
{
103+
LPC_CGU->PLL0USB_CTRL |= 1; // Power down PLL0USB.
104+
105+
// The USB core require output clock to be running @480 MHz.
106+
// Setup PLL0USB to generate 480MHz from 12 MHz XTAL.
107+
LPC_CGU->PLL0USB_NP_DIV = (514 << 12) // N
108+
| (98 << 0); // P
109+
LPC_CGU->PLL0USB_MDIV = (0x7FFA << 0) // MDEC
110+
| (0x0B << 17) // SELP
111+
| (0x10 << 22) // SELI
112+
| (0x00 << 28) // SELR
113+
;
114+
LPC_CGU->PLL0USB_CTRL = (0 << 0) // PD: 0=Power Up.
115+
| (0x01 << 2) // DIRECTI: 1=bypass pre-divider.
116+
| (0x01 << 3) // DIRECTO: 1=bypass post-divider.
117+
| (0x01 << 4) // CLKEN
118+
| (0x01 << 11) // AUTOBLOCK: 0=Disabled, 1=Enabled.
119+
| (0x06 << 24) // CLK_SEL = XTAL_OSC.
120+
;
121+
122+
LPC_CREG->CREG0 &= ~(1 << 5); // Enable USB0 PHY power.
123+
}
124+
24125

25126
void sdk_init(void)
26127
{
27-
/* Set core clock to 120MHz */
28-
CGU_Init(120000000);
29-
/* Set up USB0 clock */
30-
/* Disable PLL first */
31-
CGU_EnableEntity(CGU_CLKSRC_PLL0, DISABLE);
32-
33-
/* the usb core require output clock = 480MHz */
34-
if (CGU_SetPLL0() != CGU_ERROR_SUCCESS) {
35-
while (1);
36-
}
37-
38-
CGU_EntityConnect(CGU_CLKSRC_XTAL_OSC, CGU_CLKSRC_PLL0);
39-
/* Enable PLL after all setting is done */
40-
CGU_EnableEntity(CGU_CLKSRC_PLL0, ENABLE);
41-
/* Turn on the USB0PHY */
42-
LPC_CREG->CREG0 &= ~(1 << 5);
128+
clock_setup_base_m4(120000000);
129+
130+
clock_setup_pll0usb();
43131
}

0 commit comments

Comments
 (0)