123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- #include "all-headers.h"
- static uint32_t gMicrocontrollerSerialNumber ;
- static uint32_t readMicrocontrollerSerialNumber (void) ;
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_MCG_C5_PRDIV = 0 ;
- #elif CPU_MHZ == 240
- static const uint32_t K_MCG_C5_PRDIV = 0 ;
- #elif CPU_MHZ == 216
- static const uint32_t K_MCG_C5_PRDIV = 0 ;
- #elif CPU_MHZ == 192
- static const uint32_t K_MCG_C5_PRDIV = 0 ;
- #elif CPU_MHZ == 180
- static const uint32_t K_MCG_C5_PRDIV = 1 ;
- #elif CPU_MHZ == 168
- static const uint32_t K_MCG_C5_PRDIV = 0 ;
- #elif CPU_MHZ == 144
- static const uint32_t K_MCG_C5_PRDIV = 0 ;
- #elif CPU_MHZ == 120
- static const uint32_t K_MCG_C5_PRDIV = 1 ;
- #elif CPU_MHZ == 96
- static const uint32_t K_MCG_C5_PRDIV = 1 ;
- #elif CPU_MHZ == 72
- static const uint32_t K_MCG_C5_PRDIV = 1 ;
- #elif CPU_MHZ == 48
- static const uint32_t K_MCG_C5_PRDIV = 1 ;
- #elif CPU_MHZ == 24
- static const uint32_t K_MCG_C5_PRDIV = 1 ;
- #else
- #error "CPU_MHZ has an invalid value"
- static const uint32_t K_MCG_C5_PRDIV = 0 ;
- #endif
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_MCG_C6_VDIV = 0 ;
- #elif CPU_MHZ == 240
- static const uint32_t K_MCG_C6_VDIV = 14 ;
- #elif CPU_MHZ == 216
- static const uint32_t K_MCG_C6_VDIV = 11 ;
- #elif CPU_MHZ == 192
- static const uint32_t K_MCG_C6_VDIV = 8 ;
- #elif CPU_MHZ == 180
- static const uint32_t K_MCG_C6_VDIV = 29 ;
- #elif CPU_MHZ == 168
- static const uint32_t K_MCG_C6_VDIV = 5 ;
- #elif CPU_MHZ == 144
- static const uint32_t K_MCG_C6_VDIV = 2 ;
- #elif CPU_MHZ == 120
- static const uint32_t K_MCG_C6_VDIV = 14 ;
- #elif CPU_MHZ == 96
- static const uint32_t K_MCG_C6_VDIV = 8 ;
- #elif CPU_MHZ == 72
- static const uint32_t K_MCG_C6_VDIV = 2 ;
- #elif CPU_MHZ == 48
- static const uint32_t K_MCG_C6_VDIV = 8 ;
- #elif CPU_MHZ == 24
- static const uint32_t K_MCG_C6_VDIV = 8 ;
- #else
- #error "CPU_MHZ has an invalid value"
- static const uint32_t K_MCG_C6_VDIV = 0 ;
- #endif
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_SIM_CLKDIV1_OUTDIV1 = 0 ;
- #elif CPU_MHZ == 48
- static const uint32_t K_SIM_CLKDIV1_OUTDIV1 = 1 ;
- #elif CPU_MHZ == 24
- static const uint32_t K_SIM_CLKDIV1_OUTDIV1 = 3 ;
- #else
- static const uint32_t K_SIM_CLKDIV1_OUTDIV1 = 0 ;
- #endif
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 0 ;
- #elif CPU_MHZ == 240
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 3 ;
- #elif CPU_MHZ == 216
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 3 ;
- #elif CPU_MHZ == 192
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 3 ;
- #elif CPU_MHZ == 180
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 2 ;
- #elif CPU_MHZ == 168
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 2 ;
- #elif CPU_MHZ == 144
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 2 ;
- #elif CPU_MHZ == 120
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 1 ;
- #elif CPU_MHZ == 96
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 1 ;
- #elif CPU_MHZ == 72
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 1 ;
- #elif CPU_MHZ == 48
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 1 ;
- #elif CPU_MHZ == 24
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 3 ;
- #else
- #error "CPU_MHZ has an invalid value"
- static const uint32_t K_SIM_CLKDIV1_OUTDIV2 = 0 ;
- #endif
- static const uint32_t BUS_MHZ = (16 / 2) * (K_MCG_C6_VDIV + 16) /(K_MCG_C5_PRDIV + 1) / (K_SIM_CLKDIV1_OUTDIV2 + 1) ;
- uint32_t busMHZ (void) {
- return BUS_MHZ ;
- }
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_SIM_CLKDIV1_OUTDIV3 = 0 ;
- #elif CPU_MHZ == 48
- static const uint32_t K_SIM_CLKDIV1_OUTDIV3 = 1 ;
- #elif CPU_MHZ == 24
- static const uint32_t K_SIM_CLKDIV1_OUTDIV3 = 3 ;
- #else
- static const uint32_t K_SIM_CLKDIV1_OUTDIV3 = 0 ;
- #endif
- static const uint32_t FLEXBUS_MHZ = (16 / 2) * (K_MCG_C6_VDIV + 16) /(K_MCG_C5_PRDIV + 1) / (K_SIM_CLKDIV1_OUTDIV3 + 1) ;
- uint32_t FlexBusMHZ (void) {
- return FLEXBUS_MHZ ;
- }
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 0 ;
- #elif CPU_MHZ == 240
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 7 ;
- #elif CPU_MHZ == 216
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 7 ;
- #elif CPU_MHZ == 192
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 6 ;
- #elif CPU_MHZ == 180
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 6 ;
- #elif CPU_MHZ == 168
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 5 ;
- #elif CPU_MHZ == 144
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 4 ;
- #elif CPU_MHZ == 120
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 4 ;
- #elif CPU_MHZ == 96
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 3 ;
- #elif CPU_MHZ == 72
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 2 ;
- #elif CPU_MHZ == 48
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 3 ;
- #elif CPU_MHZ == 24
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 3 ;
- #else
- #error "CPU_MHZ has an invalid value"
- static const uint32_t K_SIM_CLKDIV1_OUTDIV4 = 0 ;
- #endif
- static const uint32_t FLASH_KHZ = (16000 / 2) * (K_MCG_C6_VDIV + 16) / (K_MCG_C5_PRDIV + 1) / (K_SIM_CLKDIV1_OUTDIV4 + 1) ;
- uint32_t FlashKHz (void) {
- return FLASH_KHZ ;
- }
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_SIM_SOPT2_PLLFLLSEL = 0 ;
- #elif CPU_MHZ == 216
- static const uint32_t K_SIM_SOPT2_PLLFLLSEL = 3 ;
- #elif CPU_MHZ == 180
- static const uint32_t K_SIM_SOPT2_PLLFLLSEL = 3 ;
- #else
- static const uint32_t K_SIM_SOPT2_PLLFLLSEL = 1 ;
- #endif
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_SIM_CLKDIV2_USBFRAC = 0 ;
- #elif CPU_MHZ == 168
- static const uint32_t K_SIM_CLKDIV2_USBFRAC = 1 ;
- #elif CPU_MHZ == 120
- static const uint32_t K_SIM_CLKDIV2_USBFRAC = 1 ;
- #elif CPU_MHZ == 72
- static const uint32_t K_SIM_CLKDIV2_USBFRAC = 1 ;
- #else
- static const uint32_t K_SIM_CLKDIV2_USBFRAC = 0 ;
- #endif
- #ifndef CPU_MHZ
- #error "CPU_MHZ is not defined"
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 0 ;
- #elif CPU_MHZ == 240
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 4 ;
- #elif CPU_MHZ == 216
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 0 ;
- #elif CPU_MHZ == 192
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 3 ;
- #elif CPU_MHZ == 180
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 0 ;
- #elif CPU_MHZ == 168
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 6 ;
- #elif CPU_MHZ == 144
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 2 ;
- #elif CPU_MHZ == 120
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 4 ;
- #elif CPU_MHZ == 96
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 1 ;
- #elif CPU_MHZ == 72
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 2 ;
- #elif CPU_MHZ == 48
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 1 ;
- #elif CPU_MHZ == 24
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 1 ;
- #else
- #error "CPU_MHZ has an invalid value"
- static const uint32_t K_SIM_CLKDIV2_USBDIV = 0 ;
- #endif
- void startPhase1 (void) {
- WDOG_UNLOCK = 0xC520 ;
- WDOG_UNLOCK = 0xD928 ;
- WDOG_STCTRLH = 0 ;
- SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2 | SIM_SCGC3_FTM3 ;
- SIM_SCGC5 = SIM_SCGC5_PORTA | SIM_SCGC5_PORTB | SIM_SCGC5_PORTC | SIM_SCGC5_PORTD | SIM_SCGC5_PORTE ;
- SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTF ;
- LMEM_PCCCR = LMEM_PCCCR_GO | LMEM_PCCCR_INVW1 | LMEM_PCCCR_INVW0 | LMEM_PCCCR_ENWRBUF | LMEM_PCCCR_ENCACHE ;
- if ((RTC_CR & RTC_CR_OSCE) != 0) {
- RTC_SR = 0;
- RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
- }
- if (PMC_REGSC & PMC_REGSC_ACKISO) {
- PMC_REGSC |= PMC_REGSC_ACKISO;
- }
- SMC_PMPROT = SMC_PMPROT_AHSRUN | SMC_PMPROT_AVLP | SMC_PMPROT_ALLS | SMC_PMPROT_AVLLS;
- OSC_CR = OSC_CR_SC8P | OSC_CR_SC2P | OSC_CR_ERCLKEN;
- MCG_C2 = MCG_C2_RANGE (2) | MCG_C2_EREFS;
- MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(4);
- while ((MCG_S & MCG_S_OSCINIT0) == 0) {}
- while ((MCG_S & MCG_S_IREFST) != 0) {}
- while ((MCG_S & MCG_S_CLKST (3)) != MCG_S_CLKST(2)) {}
- const uint32_t serialNumber = readMicrocontrollerSerialNumber () ;
- SMC_PMCTRL = SMC_PMCTRL_RUNM (3);
- while (SMC_PMSTAT != SMC_PMPROT_AHSRUN) {}
- MCG_C5 = K_MCG_C5_PRDIV ;
- MCG_C6 = MCG_C6_PLLS | K_MCG_C6_VDIV ;
- while (!(MCG_S & MCG_S_PLLST)) {}
- while (!(MCG_S & MCG_S_LOCK0)) {}
- SIM_CLKDIV1 =
- SIM_CLKDIV1_OUTDIV1(K_SIM_CLKDIV1_OUTDIV1)
- | SIM_CLKDIV1_OUTDIV2(K_SIM_CLKDIV1_OUTDIV2)
- | SIM_CLKDIV1_OUTDIV3(K_SIM_CLKDIV1_OUTDIV3)
- | SIM_CLKDIV1_OUTDIV4(K_SIM_CLKDIV1_OUTDIV4)
- ;
- SIM_CLKDIV2 =
- SIM_CLKDIV2_USBDIV (K_SIM_CLKDIV2_USBDIV)
- | K_SIM_CLKDIV2_USBFRAC
- ;
- MCG_C1 = MCG_C1_CLKS(0) | MCG_C1_FRDIV(4);
- while ((MCG_S & MCG_S_CLKST (3)) != MCG_S_CLKST(3)) {}
- SIM_SOPT2 =
- SIM_SOPT2_USBSRC
- | (K_SIM_SOPT2_PLLFLLSEL << 16)
- | SIM_SOPT2_TRACECLKSEL
- ;
- extern uint32_t __bss_start ;
- extern const uint32_t __bss_end ;
- uint32_t * p = & __bss_start ;
- while (p != & __bss_end) {
- * p = 0 ;
- p ++ ;
- }
- extern uint32_t __data_start ;
- extern const uint32_t __data_end ;
- extern uint32_t __data_load_start ;
- uint32_t * pSrc = & __data_load_start ;
- uint32_t * pDest = & __data_start ;
- while (pDest != & __data_end) {
- * pDest = * pSrc ;
- pDest ++ ;
- pSrc ++ ;
- }
- gMicrocontrollerSerialNumber = serialNumber ;
- }
- static uint32_t readMicrocontrollerSerialNumber (void) {
- while ((FTFE_FSTAT & FTFE_FSTAT_CCIF) == 0) {}
- FTFE_FSTAT = FTFE_FSTAT_RDCOLERR | FTFE_FSTAT_ACCERR | FTFE_FSTAT_FPVIOL;
- FTFE_FCCOB_0_3 = 0x41070000 ;
- FTFE_FSTAT = FTFE_FSTAT_CCIF ;
- while ((FTFE_FSTAT & FTFE_FSTAT_CCIF) == 0) {}
- return FTFE_FCCOB_8_11 ;
- }
- uint32_t microcontrollerSerialNumber (void) {
- return gMicrocontrollerSerialNumber ;
- }
- void startPhase2 (void) {
- extern void (* __boot_routine_array_start) (void) ;
- extern void (* __boot_routine_array_end) (void) ;
- void (* * ptr) (void) = & __boot_routine_array_start ;
- while (ptr != & __boot_routine_array_end) {
- (* ptr) () ;
- ptr ++ ;
- }
- extern void (* __constructor_array_start) (void) ;
- extern void (* __constructor_array_end) (void) ;
- ptr = & __constructor_array_start ;
- while (ptr != & __constructor_array_end) {
- (* ptr) () ;
- ptr ++ ;
- }
- extern void (* __init_routine_array_start) (void) ;
- extern void (* __init_routine_array_end) (void) ;
- ptr = & __init_routine_array_start ;
- while (ptr != & __init_routine_array_end) {
- (* ptr) () ;
- ptr ++ ;
- }
- }
|