sk-lpc1788中的void SystemInit (void)函数

/*
 * Initialize the system
 *
 * @param  none
 * @return none
 *
 * @brief  Setup the microcontroller system.
 *         Initialize the System and update the SystemFrequency variable.
 */
void SystemInit (void)
{
#if (FLASH_SETUP == 1)                  /* Flash Accelerator Setup            */
  LPC_SC->FLASHCFG  = FLASHCFG_Val;
#endif
#if (CLOCK_SETUP)                       /* Clock Setup                        */
  LPC_SC->SCS       = SCS_Val; /* bit[5]=1;The main oscillator is enabled, added by fulinux. */
  if (SCS_Val & (1 << 5)) {             /* If Main Oscillator is enabled      */
    while ((LPC_SC->SCS & (1<<6)) == 0);/* Wait for Oscillator to be ready    */
  }


  LPC_SC->PCLKSEL   = PCLKSEL_Val;      /* Peripheral Clock Selection         */


  LPC_SC->EMCCLKSEL = EMCCLKSEL_Val;    /* EMC Clock Selection                */


  LPC_SC->SPIFISEL  = SPIFISEL_Val;     /* SPIFI Clock Selection              */
/* bit[9:8]=0x0; Sysclk is used as the input to the SPIFI  clock divider. added by fulinux. */


  LPC_SC->CLKSRCSEL = CLKSRCSEL_Val;    /* Select Clock Source for PLL0       */
/* bit[0]=1; Selects the main oscillator as the sysclk and PLL0 clock source. added by fulinux. */

  LPC_SC->CCLKSEL   = CCLKSEL_Val;      /* Setup Clock Divider,and select sysclk(12M) for cpu clock divider. added by fulinux. */
/* And cpu frequency is 12M at frist */

#if (PLL0_SETUP)
  LPC_SC->PLL0CFG   = PLL0CFG_Val; /* PLL0 frequency is 120M, added by fulinux. */

  LPC_SC->PLL0CON   = 0x01;             /* PLL0 Enable                        */
  LPC_SC->PLL0FEED  = 0xAA;
  LPC_SC->PLL0FEED  = 0x55;
  while (!(LPC_SC->PLL0STAT & (1<<10)));/* Wait for PLOCK0                    */
/* Can also use interrupt without waiting for. added by fulinux. */


  LPC_SC->PLL0CON   = 0x03;             /* PLL0 Enable & Connect              */
  LPC_SC->PLL0FEED  = 0xAA;
  LPC_SC->PLL0FEED  = 0x55;


  LPC_SC->CCLKSEL  |= 0x100; /* This time the cpu frequency is 120M. added by fulinux. */


  LPC_SC->SPIFISEL = 0x100 | SPIFISEL_Val; /* It seems that the LPC_SC->SPIFISEL is W only */
/* bit[9:8]=0x1; The output of the Main PLL is used as the input to the SPIFI clock divider.added by fulinux. */
#endif


#if (PLL1_SETUP)
  LPC_SC->USBCLKSEL = USBSEL_Val;       /* Setup USB Clock Divider            */
/* Sysclk is used as the input to USB clock divider. */


  LPC_SC->PLL1CFG   = PLL1CFG_Val; /* PLL1 frequency is 12M. added by fulinux.*/
  LPC_SC->PLL1CON   = 0x01;             /* PLL1 Enable                        */
  LPC_SC->PLL1FEED  = 0xAA;
  LPC_SC->PLL1FEED  = 0x55;
  while (!(LPC_SC->PLL1STAT & (1<<10)));/* Wait for PLOCK1                    */


  LPC_SC->PLL1CON   = 0x03;             /* PLL1 Enable & Connect              */
  LPC_SC->PLL1FEED  = 0xAA;
  LPC_SC->PLL1FEED  = 0x55;


  LPC_SC->USBCLKSEL |= 0x200; /* The output of the alt pll is used as the input to USB clock divider. */


//  LPC_SC->SPIFISEL = 0x200 | SPIFISEL_Val; /* It seems that the LPC_SC->SPIFISEL is W only */
#else
  LPC_SC->USBCLKSEL = USBSEL_Val;       /* Setup USB Clock Divider            */
#endif


  LPC_SC->PCONP     = PCONP_Val;        /* Power Control for Peripherals      */


  LPC_SC->CLKOUTCFG = CLKOUTCFG_Val;    /* Clock Output Configuration         */
#endif


  /* Determine clock frequency according to clock register values             */
  if (((LPC_SC->PLL0STAT >> 8)&3)==3) {/* If PLL0 enabled and connected      */
    switch (LPC_SC->CLKSRCSEL & 0x03) {
      case 0:                           /* Internal RC oscillator => PLL0     */
        SystemFrequency = (IRC_OSC * 
                          ((LPC_SC->PLL0STAT & 0x1F) + 1) /
                          (LPC_SC->CCLKSEL & 0x1F));
        PeripheralFrequency = (IRC_OSC * 
                          ((LPC_SC->PLL0STAT & 0x1F) + 1) /
                          (LPC_SC->PCLKSEL & 0x1F));
        break;
      case 1:                           /* Main oscillator => PLL0            */
        SystemFrequency = (OSC_CLK * 
                          ((LPC_SC->PLL0STAT & 0x1F) + 1) /
                          (LPC_SC->CCLKSEL & 0x1F));
        PeripheralFrequency = (OSC_CLK * 
                          ((LPC_SC->PLL0STAT & 0x1F) + 1) /
                          (LPC_SC->PCLKSEL & 0x1F));
        break;
    }
  } else {
    switch (LPC_SC->CLKSRCSEL & 0x03) {
      case 0:                           /* Internal RC oscillator => PLL0     */
        SystemFrequency = IRC_OSC / (LPC_SC->CCLKSEL & 0x1F);
        break;
      case 1:                           /* Main oscillator => PLL0            */
        SystemFrequency = OSC_CLK / (LPC_SC->CCLKSEL & 0x1F);
        break;
    }
  }
}

你可能感兴趣的:(Cortex-M3,SystemInit,sk-lpc1788,系统时钟设置)