Aidan Walton / Mbed 2 deprecated ClockControl

Dependencies:   mbed

Fork of ClockControl by Michael Wei

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ClockControl.cpp Source File

ClockControl.cpp

00001 #include "ClockControl.h"
00002 
00003 void setPLL0Frequency(unsigned char clkSrc, unsigned short M, unsigned char N)
00004 {
00005       LPC_SC->CLKSRCSEL = clkSrc;
00006       LPC_SC->PLL0CFG   = (((unsigned int)N-1) << 16) | M-1;
00007       LPC_SC->PLL0CON   = 0x01;             
00008       LPC_SC->PLL0FEED  = 0xAA;
00009       LPC_SC->PLL0FEED  = 0x55;
00010       while (!(LPC_SC->PLL0STAT & (1<<26)));
00011     
00012       LPC_SC->PLL0CON   = 0x03;
00013       LPC_SC->PLL0FEED  = 0xAA;
00014       LPC_SC->PLL0FEED  = 0x55;
00015 }
00016 
00017 void setPLL1Frequency(unsigned short M, unsigned char P)
00018 {
00019       LPC_SC->PLL1CFG   = (((unsigned int)P-1) << 16) | M-1;
00020       LPC_SC->PLL1CON   = 0x01;             
00021       LPC_SC->PLL1FEED  = 0xAA;
00022       LPC_SC->PLL1FEED  = 0x55;
00023       while (!(LPC_SC->PLL1STAT & (1<<10)));
00024     
00025       LPC_SC->PLL1CON   = 0x03;
00026       LPC_SC->PLL1FEED  = 0xAA;
00027       LPC_SC->PLL1FEED  = 0x55;
00028 }
00029 
00030 unsigned int setSystemFrequency( unsigned char USBclkDivider, unsigned char clkDivider, unsigned char clkSrc, unsigned short M, unsigned char N)
00031 {
00032     setPLL0Frequency(clkSrc, M, N);
00033     LPC_SC->CCLKCFG = clkDivider - 1;
00034     LPC_SC->USBCLKCFG = USBclkDivider - 1;
00035     // Default "PCLK_peripheral = CCLK/4". 
00036     // If System Clock is changed from default, TIMER3 changes with it. eg 96Mhz System clock TIMER3 1uS = 1uS. System Clock = 120Mhz. 1uS will actually become 0.8uS.
00037     //LPC_SC->PCLKSEL1 = 1 << 14; // Sets divider for TIMER3 to CCLK / 1.
00038     //LPC_TIM3->PR = 74;    // Sets prescaler for TIMER3 counter. (Default at 96Mhz and PCLKSEL = 0 (ie. CCLK/4) is 23) Therefore if we set PCLKSEL = CCLK/1 and scale down by (23*4) * 96Mhz/120hz = 73.6. Not perfect...but it works 
00039     SystemCoreClockUpdate();
00040     return SystemCoreClock;
00041 }
00042 
00043 void setPPL1forUSB(unsigned short M, unsigned char P) 
00044 {
00045    setPLL1Frequency(M, P);    // Drive USB subsystem from PLL1 
00046 }