static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unused)); | static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unused)); | ||||
static inline void delayMicroseconds(uint32_t usec) | static inline void delayMicroseconds(uint32_t usec) | ||||
{ | { | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
uint32_t n = usec * 85; | |||||
#elif F_CPU == 240000000 | |||||
uint32_t n = usec * 80; | uint32_t n = usec * 80; | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
uint32_t n = usec * 72; | uint32_t n = usec * 72; |
#endif // end of board-specific definitions | #endif // end of board-specific definitions | ||||
#if (F_CPU == 240000000) | |||||
#if (F_CPU == 256000000) | |||||
#define F_PLL 256000000 | |||||
#ifndef F_BUS | |||||
#define F_BUS 64000000 | |||||
//#define F_BUS 128000000 // all the usual overclocking caveats apply... | |||||
#endif | |||||
#define F_MEM 32000000 | |||||
#elif (F_CPU == 240000000) | |||||
#define F_PLL 240000000 | #define F_PLL 240000000 | ||||
#ifndef F_BUS | #ifndef F_BUS | ||||
#define F_BUS 60000000 | #define F_BUS 60000000 |
SMC_PMCTRL = SMC_PMCTRL_RUNM(3); // enter HSRUN mode | SMC_PMCTRL = SMC_PMCTRL_RUNM(3); // enter HSRUN mode | ||||
while (SMC_PMSTAT != SMC_PMSTAT_HSRUN) ; // wait for HSRUN | while (SMC_PMSTAT != SMC_PMSTAT_HSRUN) ; // wait for HSRUN | ||||
#endif | #endif | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(16); | |||||
#elif F_CPU == 240000000 | |||||
MCG_C5 = MCG_C5_PRDIV0(0); | MCG_C5 = MCG_C5_PRDIV0(0); | ||||
MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(14); | MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(14); | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
#endif | #endif | ||||
#endif | #endif | ||||
// now program the clock dividers | // now program the clock dividers | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
// config divisors: 256 MHz core, 64 MHz bus, 32 MHz flash, USB = IRC48M | |||||
// TODO: gradual ramp-up for HSRUN mode | |||||
#if F_BUS == 64000000 | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(7); | |||||
#elif F_BUS == 128000000 | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(7); | |||||
#else | |||||
#error "This F_CPU & F_BUS combination is not supported" | |||||
#endif | |||||
SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(0); | |||||
#elif F_CPU == 240000000 | |||||
// config divisors: 240 MHz core, 60 MHz bus, 30 MHz flash, USB = 240 / 5 | // config divisors: 240 MHz core, 60 MHz bus, 30 MHz flash, USB = 240 / 5 | ||||
// TODO: gradual ramp-up for HSRUN mode | // TODO: gradual ramp-up for HSRUN mode | ||||
#if F_BUS == 60000000 | #if F_BUS == 60000000 | ||||
#elif F_BUS == 72000000 | #elif F_BUS == 72000000 | ||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(7); | SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(7); | ||||
#elif F_BUS == 108000000 | #elif F_BUS == 108000000 | ||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(7); | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(7); | |||||
#else | #else | ||||
#error "This F_CPU & F_BUS combination is not supported" | #error "This F_CPU & F_BUS combination is not supported" | ||||
#endif | #endif | ||||
// now we're in PEE mode | // now we're in PEE mode | ||||
// USB uses PLL clock, trace is CPU clock, CLKOUT=OSCERCLK0 | // USB uses PLL clock, trace is CPU clock, CLKOUT=OSCERCLK0 | ||||
#if defined(KINETISK) | #if defined(KINETISK) | ||||
#if F_CPU == 216000000 || F_CPU == 180000000 | |||||
#if F_CPU == 256000000 || F_CPU == 216000000 || F_CPU == 180000000 | |||||
SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_IRC48SEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6); | SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_IRC48SEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6); | ||||
#else | #else | ||||
SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6); | SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6); | ||||
startup_late_hook(); | startup_late_hook(); | ||||
main(); | main(); | ||||
while (1) ; | while (1) ; | ||||
} | } | ||||
// the peripheral speed (F_BUS). Serial1 & Serial2 baud | // the peripheral speed (F_BUS). Serial1 & Serial2 baud | ||||
// rates will be impacted, but most other peripherals | // rates will be impacted, but most other peripherals | ||||
// will continue functioning at the same speed. | // will continue functioning at the same speed. | ||||
#if F_CPU == 240000000 && F_BUS == 60000000 | |||||
#if F_CPU == 256000000 && F_BUS == 64000000 | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // TODO: TEST | |||||
#elif F_CPU == 256000000 && F_BUS == 128000000 | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // TODO: TEST | |||||
#elif F_CPU == 240000000 && F_BUS == 60000000 | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok | SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok | ||||
#elif F_CPU == 240000000 && F_BUS == 80000000 | #elif F_CPU == 240000000 && F_BUS == 80000000 | ||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok | SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok | ||||
SMC_PMCTRL = SMC_PMCTRL_RUNM(3); | SMC_PMCTRL = SMC_PMCTRL_RUNM(3); | ||||
while (SMC_PMSTAT != SMC_PMSTAT_HSRUN) {;} // wait | while (SMC_PMSTAT != SMC_PMSTAT_HSRUN) {;} // wait | ||||
// Then configure clock for full speed | // Then configure clock for full speed | ||||
#if F_CPU == 240000000 && F_BUS == 60000000 | |||||
#if F_CPU == 256000000 && F_BUS == 64000000 | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7); | |||||
#elif F_CPU == 256000000 && F_BUS == 128000000 | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7); | |||||
#elif F_CPU == 240000000 && F_BUS == 60000000 | |||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7); | SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7); | ||||
#elif F_CPU == 240000000 && F_BUS == 80000000 | #elif F_CPU == 240000000 && F_BUS == 80000000 | ||||
SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7); | SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7); |
} | } | ||||
// TODO: verify these result in correct timeouts... | // TODO: verify these result in correct timeouts... | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
#define PULSEIN_LOOPS_PER_USEC 34 | |||||
#elif F_CPU == 240000000 | |||||
#define PULSEIN_LOOPS_PER_USEC 33 | #define PULSEIN_LOOPS_PER_USEC 33 | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
#define PULSEIN_LOOPS_PER_USEC 31 | #define PULSEIN_LOOPS_PER_USEC 31 |
#ifdef HAS_KINETIS_MPU | #ifdef HAS_KINETIS_MPU | ||||
MPU_RGDAAC0 |= 0x03000000; | MPU_RGDAAC0 |= 0x03000000; | ||||
#endif | #endif | ||||
#if F_CPU == 180000000 || F_CPU == 216000000 | |||||
#if F_CPU == 180000000 || F_CPU == 216000000 || F_CPU == 256000000 | |||||
// if using IRC48M, turn on the USB clock recovery hardware | // if using IRC48M, turn on the USB clock recovery hardware | ||||
USB0_CLK_RECOVER_IRC_EN = USB_CLK_RECOVER_IRC_EN_IRC_EN | USB_CLK_RECOVER_IRC_EN_REG_EN; | USB0_CLK_RECOVER_IRC_EN = USB_CLK_RECOVER_IRC_EN_IRC_EN | USB_CLK_RECOVER_IRC_EN_REG_EN; | ||||
USB0_CLK_RECOVER_CTRL = USB_CLK_RECOVER_CTRL_CLOCK_RECOVER_EN | | USB0_CLK_RECOVER_CTRL = USB_CLK_RECOVER_CTRL_CLOCK_RECOVER_EN | |
// When the PC isn't listening, how long do we wait before discarding data? | // When the PC isn't listening, how long do we wait before discarding data? | ||||
#define TX_TIMEOUT_MSEC 30 | #define TX_TIMEOUT_MSEC 30 | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706) | |||||
#elif F_CPU == 240000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) |
// When the PC isn't listening, how long do we wait before discarding data? | // When the PC isn't listening, how long do we wait before discarding data? | ||||
#define TX_TIMEOUT_MSEC 50 | #define TX_TIMEOUT_MSEC 50 | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706) | |||||
#elif F_CPU == 240000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) |
// When the PC isn't listening, how long do we wait before discarding data? | // When the PC isn't listening, how long do we wait before discarding data? | ||||
#define TX_TIMEOUT_MSEC 40 | #define TX_TIMEOUT_MSEC 40 | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706) | |||||
#elif F_CPU == 240000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) |
// When the PC isn't listening, how long do we wait before discarding data? | // When the PC isn't listening, how long do we wait before discarding data? | ||||
#define TX_TIMEOUT_MSEC 30 | #define TX_TIMEOUT_MSEC 30 | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706) | |||||
#elif F_CPU == 240000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) |
// too short, we risk losing data during the stalls that are common with ordinary desktop | // too short, we risk losing data during the stalls that are common with ordinary desktop | ||||
// software. If it's too long, we stall the user's program when no software is running. | // software. If it's too long, we stall the user's program when no software is running. | ||||
#define TX_TIMEOUT_MSEC 30 | #define TX_TIMEOUT_MSEC 30 | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706) | |||||
#elif F_CPU == 240000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) |
// too short, we risk losing data during the stalls that are common with ordinary desktop | // too short, we risk losing data during the stalls that are common with ordinary desktop | ||||
// software. If it's too long, we stall the user's program when no software is running. | // software. If it's too long, we stall the user's program when no software is running. | ||||
#define TX_TIMEOUT_MSEC 70 | #define TX_TIMEOUT_MSEC 70 | ||||
#if F_CPU == 240000000 | |||||
#if F_CPU == 256000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706) | |||||
#elif F_CPU == 240000000 | |||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600) | ||||
#elif F_CPU == 216000000 | #elif F_CPU == 216000000 | ||||
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) | #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440) |