void Reset_Handler(void) { // Copy the data sections from flash to SRAM. unsigned int LoadAddr, ExeAddr, SectionLen; unsigned int *SectionTableAddr; // Load base address of Global Section Table SectionTableAddr = &__data_section_table; // Copy the data sections from flash to SRAM. while (SectionTableAddr < &__data_section_table_end) { LoadAddr = *SectionTableAddr++; ExeAddr = *SectionTableAddr++; SectionLen = *SectionTableAddr++; data_init(LoadAddr, ExeAddr, SectionLen); } // At this point, SectionTableAddr = &__bss_section_table; // Zero fill the bss segment while (SectionTableAddr < &__bss_section_table_end) { ExeAddr = *SectionTableAddr++; SectionLen = *SectionTableAddr++; bss_init(ExeAddr, SectionLen); } #if STARTUP_DELAY volatile unsigned int i; for(i=0; i<STARTUP_DELAY; i++); #endif // Set clock mode, DEFAULT_CLOCK is defined in config.h, and the default behaviour // is to set the clock to 72MHz from the external crystal. Using defines here to // reduce code space #if DEFAULT_CLOCK == XTAL ClockModeXTAL(); #elif DEFAULT_CLOCK == IRC72 ClockModeIRC72(); #elif DEFAULT_CLOCK == IRC12 ClockModeIRC12(); #endif LPC_SYSCON->SYSAHBCLKDIV = 1; LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 6) | (1 << 16); // Set all pins to digital inputs (except P0[0] which is the reset button) #if PORT_STARTUP_INIT Port0Init(ALL & ~PIN0); Port1Init(ALL); #endif // Initialise and start the system tick timer if allowed by the SYSTICK_EN // definition in config.h, if the system tick timer is running, then the Delay() // function will use it, otherwise Delay() will use a fixed loop which is not // accurate when there are interrupts running, as any interrupt would stop the // loop and cuase the delay to be longer than expected #if SYSTICK_EN && SYSTICK_STARTUP SysTickInit(); #endif // Run the user-supplied setup() function if it exists if(setup) { setup(); } // Run the user-supplied main() function if it exists if(main) { main(); } // Loop the user-supplied setup() function if it exists if(loop) { while(1) loop(); } // Do nothing (except handle interrupts) while(1); }
// *** This is the startup code void Reset_Handler(void) { unsigned long *pSrc, *pDest; unsigned int i; // Initialise RAM pSrc = &_etext; for(pDest=&__data; pDest<&__edata; ) { *pDest++ = *pSrc++; } for(pDest=&__bss; pDest<&__ebss; ) { *pDest++ = 0; } // Initialise the uninitialised variable described in section 19.2.1 of UM10365 *((uint32_t *)(0x10000054)) = 0x0; #if STARTUP_DELAY for(i=0; i<STARTUP_DELAY; i++); #endif // Set clock mode, DEFAULT_CLOCK is defined in config.h, and the default behaviour // is to set the clock to 72MHz from the external crystal. Using defines here to // reduce code space #if DEFAULT_CLOCK == XTAL LPC_SYSCON->PDRUNCFG &= ~(1 << 5); // Power up system oscillator for(i = 0; i < 100; i++); // Brief delay LPC_SYSCON->SYSOSCCTRL = 0; // System oscillator setup - not bypass, 1-20MHz range LPC_SYSCON->SYSPLLCLKSEL = 0x01; // Select system oscillator as PLL source LPC_SYSCON->SYSPLLCLKUEN = 0x00; // Update clock source LPC_SYSCON->SYSPLLCLKUEN = 0x01; while(!(LPC_SYSCON->SYSPLLCLKUEN & 0x01)); // Wait for update LPC_SYSCON->MAINCLKSEL = 0x01; // Select sys osc as main clock source LPC_SYSCON->MAINCLKUEN = 0x00; // Update clock source LPC_SYSCON->MAINCLKUEN = 0x01; while (!(LPC_SYSCON->MAINCLKUEN & 0x01)); // Wait for clock update LPC_SYSCON->SYSPLLCTRL = 0x25; // Select PLL divider to 6 (12MHz - 72MHz) LPC_SYSCON->PDRUNCFG &= ~(1 << 7); // Power up PLL while(!(LPC_SYSCON->SYSPLLSTAT & 0x01)); // Wait for PLL lock LPC_SYSCON->MAINCLKSEL = 0x03; // Select PLL as main clock source LPC_SYSCON->MAINCLKUEN = 0x00; // Update clock source LPC_SYSCON->MAINCLKUEN = 0x01; while (!(LPC_SYSCON->MAINCLKUEN & 0x01)); // Wait for clock update #elif DEFAULT_CLOCK == IRC72 LPC_SYSCON->PDRUNCFG &= ~((1<<0) | (1<<1)); // Power up IRC oscillator for(i = 0; i < 100; i++); // Brief delay LPC_SYSCON->MAINCLKSEL = 0x00; // Select IRC as main clock source LPC_SYSCON->MAINCLKUEN = 0x00; // Update clock source LPC_SYSCON->MAINCLKUEN = 0x01; while (!(LPC_SYSCON->MAINCLKUEN & 0x01)); // Wait for clock update LPC_SYSCON->SYSPLLCLKSEL = 0x00; // Select IRC as PLL source LPC_SYSCON->SYSPLLCLKUEN = 0x00; // Update clock source LPC_SYSCON->SYSPLLCLKUEN = 0x01; while(!(LPC_SYSCON->SYSPLLCLKUEN & 0x01)); // Wait for update LPC_SYSCON->SYSPLLCTRL = 0x25; // Select PLL divider to 6 (12MHz - 72MHz) LPC_SYSCON->PDRUNCFG &= ~(1<<7); // Power up PLL while(!(LPC_SYSCON->SYSPLLSTAT & 0x01)); // Wait for PLL lock LPC_SYSCON->MAINCLKSEL = 0x03; // Select PLL as main clock source LPC_SYSCON->MAINCLKUEN = 0x00; // Update clock source LPC_SYSCON->MAINCLKUEN = 0x01; while (!(LPC_SYSCON->MAINCLKUEN & 0x01)); // Wait for clock update LPC_SYSCON->PDRUNCFG |= (1<<5); // Power down system oscillator #elif DEFAULT_CLOCK == IRC12 LPC_SYSCON->PDRUNCFG &= ~((1<<0) | (1<<1)); // Power up IRC oscillator for(i = 0; i < 100; i++); // Brief delay LPC_SYSCON->MAINCLKSEL = 0x00; // Select IRC as main clock source LPC_SYSCON->MAINCLKUEN = 0x00; // Update clock source LPC_SYSCON->MAINCLKUEN = 0x01; while (!(LPC_SYSCON->MAINCLKUEN & 0x01)); // Wait for clock update LPC_SYSCON->PDRUNCFG |= ((1<<5) | (1<<7)); // Power down system oscillator and IRC #endif // Set all pins to digital inputs (except P0[0] which is the reset button) #if PORT_STARTUP_INIT Port0Init(ALL & ~PIN0); Port1Init(ALL); Port2Init(ALL); Port3Init(ALL); #endif // Initialise and start the system tick timer if allowed by the SYSTICK_EN // definition in config.h, if the system tick timer is running, then the Delay() // function will use it, otherwise Delay() will use a fixed loop which is not // accurate when there are interrupts running, as any interrupt would stop the // loop and cuase the delay to be longer than expected #if SYSTICK_EN && SYSTICK_STARTUP SysTickInit(); #endif // Run the user-supplied setup() function if it exists if(setup) { setup(); } // Run the user-supplied main() function if it exists if(main) { main(); } // Loop the user-supplied setup() function if it exists if(loop) { while(1) loop(); } // Do nothing (except handle interrupts) while(1); }