While trying to make the LPC1227 to work using a 10 MHz crystal I faced some troubles ‘cause I barely understood how to configure the clock system, so I wasn’t able to get the frequency I wished (30 MHz).
After some hours of figuring out how to solve this issue (and understand it) I think I get it.
I made a diagram that shows the several options that one may face while configuring the clock system:
For this microcontroller there are three potential sources for the system clock:
- Crystal (1 to 25 MHz)
- Internal RC (IRC, 12 MHz)
- Watchdog (7.8 KHz to 1.7 MHz)
And there are some registers that the programmer must set in order to get the desired frequency. Those values are written into constants and not directly to the CPU registers. The constants are found in the file “system_LPC122x.c” which lives in the CMSIS folder (it applies for other members of the NXP Cortex-M0 family, for example, system_LPC111x.c):
This is the frequency (in Hertz) of the external crystal. In my case 10 Mhz (and in most LPCXpresso boards, 12 MHz).
This is a binay value.
0: Don’t use the system oscillator (crystal signals, XTALIN and XTALOUT are taken directly);
1: Crystal signals go to the System Oscillator.
This is a binay value.
0: The output frequency of the IRC oscillator is either fed to the PLL or to the main clock, MAIN_CLK;
1: The PLL input is fed with the crystal signals (whether directly or through the system oscillator).
Defines the values for the M and PSEL fields (look at the user manual)
Configures who is going to be the main clock.
This is how these constants look like in my system (note that all have two values, the commented lines were the original ones):
#define SYSOSCCTRL_Val 0x00000000 //#define SYSOSCCTRL_Val 0x00000001 //#define __XTAL 12000000 #define __XTAL 10000000 //#define SYSPLLCLKSEL_Val 0x00000000 #define SYSPLLCLKSEL_Val 0x00000001 //#define SYSPLLCTRL_Val 0x00000041 #define SYSPLLCTRL_Val 0x00000042 #define MAINCLKSEL_Val 0x00000001 //#define MAINCLKSEL_Val 0x00000003
And this is a snapshot of several configurations (for a 10 MHz crystal and IRC = 12 MHz)
That’s all for today, hope you’ve enjoyed this mini-tutorial, and it is usefull for you. Any comments are welcome.
Update (5-nov): For an unknow reason (I mean, I don’t know the reason) the crystal doesn’t oscillate when SYSOSCCTRL=1. So I set this constant to zero. I used these constants for getting 45 MHz using a 15 MHz crystal:
#define SYSOSCCTRL_Val 0x00000000 #define __XTAL 15000000 #define SYSPLLCLKSEL_Val 0x00000001 #define SYSPLL_SETUP 1 #define SYSPLLCTRL_Val 0x00000022 #define MAINCLKSEL_Val 0x00000003