/** * This brings up enough clocks to allow the processor to run quickly while initialising memory. * Other platform specific clock init can be done in init_platform() or init_architecture() */ WEAK void init_clocks( void ){ /** This brings up enough clocks to allow the processor to run quickly while initialising memory. * Other platform specific clock init can be done in init_platform() or init_architecture() */ //LPC54xx clock initialized in SystemInit(). #ifdef BOOTLOADER LPC_SYSCTL->SYSAHBCLKCTRL[0] |= 0x00000018; // Magicoe #if defined(__FPU_PRESENT) && __FPU_PRESENT == 1 fpuInit(); #endif #if defined(NO_BOARD_LIB) /* Chip specific SystemInit */ Chip_SystemInit(); #else /* Enable RAM 2 clock */ Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SRAM2); /* Board specific SystemInit */ Board_SystemInit(); //init pin muxing and clock. #endif LPC_SYSCTL->SYSAHBCLKCTRL[0] |= 0x00000018; // Magicoe Chip_SYSCTL_PowerUp(PDRUNCFG_PD_IRC_OSC_EN|PDRUNCFG_PD_IRC_EN); /* Configure PIN0.21 as CLKOUT with pull-up, monitor the MAINCLK on scope */ Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 21, IOCON_MODE_PULLUP | IOCON_FUNC1 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF); Chip_Clock_SetCLKOUTSource(SYSCTL_CLKOUTSRC_RTC, 1); Chip_Clock_EnableRTCOsc(); Chip_RTC_Init(LPC_RTC); Chip_RTC_Enable1KHZ(LPC_RTC); Chip_RTC_Enable(LPC_RTC); #endif }
/** * @brief main routine for CLKOUT example * @return Function should not exit. */ int main(void) { CHIP_SYSCTL_CLKOUTSRC_T clkoutClks; SystemCoreClockUpdate(); Board_Init(); Board_LED_Set(0, false); /* Enable and setup SysTick Timer at a 100Hz rate */ SysTick_Config(Chip_Clock_GetSysTickClockRate() / 100); /* Enable the power to the WDT */ Chip_SYSCTL_PowerUp(SYSCTL_POWERDOWN_WDTOSC_PD); /* Setup SCT PLL */ Chip_SYSCTL_PowerDown(SYSCTL_POWERDOWN_SCTPLL_PD); Chip_Clock_SetSCTPLLSource(SYSCTL_PLLCLKSRC_MAINOSC); Chip_Clock_SetupSCTPLL(5, 2); Chip_SYSCTL_PowerUp(SYSCTL_POWERDOWN_SCTPLL_PD); /* Wait for PLL to lock */ while (!Chip_Clock_IsSCTPLLLocked()) {} /* Enable RTC Oscillator */ Chip_Clock_EnableRTCOsc(); /* Enable SWM clocking prior to switch matrix operations */ Chip_SWM_Init(); Chip_GPIO_Init(LPC_GPIO); /* Setup pin as CLKOUT */ Chip_SWM_MovablePortPinAssign(SWM_CLK_OUT_O, CLKOUT_PORT, CLKOUT_PIN); /* Configure as a digital pin with no pullups/pulldowns */ Chip_IOCON_PinMuxSet(LPC_IOCON, CLKOUT_PORT, CLKOUT_PIN, (IOCON_MODE_INACT | IOCON_DIGMODE_EN)); /* Cycle through all clock sources for the CLKOUT pin */ while (1) { for (clkoutClks = SYSCTL_CLKOUTSRC_IRC; clkoutClks <= SYSCTL_CLKOUTSRC_RTC32K; clkoutClks++) { /* Setup CLKOUT pin for specific clock with a divider of 1 */ Chip_Clock_SetCLKOUTSource(clkoutClks, 1); /* Wait 5 seconds */ ticks100 = 0; while (ticks100 < 500) { __WFI(); } } } /* Disable CLKOUT pin by setting divider to 0 */ Chip_Clock_SetCLKOUTSource(SYSCTL_CLKOUTSRC_MAINSYSCLK, 0); return 0; }
/* Wecan Modify platform_rtc_init 2015.06.10 */ OSStatus platform_rtc_init(void) { #ifdef MICO_ENABLE_MCU_RTC /* CLKOUT = 32K_Osc */ Chip_IOCON_PinMuxSet(LPC_IOCON, platform_gpio_pins[CLKOUT].port , platform_gpio_pins[CLKOUT].pin_number, (IOCON_MODE_PULLUP | IOCON_FUNC1 | IOCON_DIGITAL_EN | IOCON_INPFILT_OFF)); Chip_Clock_SetCLKOUTSource(SYSCON_CLKOUTSRC_RTC, 1); /* Turn on the RTC 32K Oscillator */ Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_32K_OSC); /* Enable the RTC oscillator, oscillator rate can be determined by calling Chip_Clock_GetRTCOscRate() */ Chip_Clock_EnableRTCOsc(); /* Initialize RTC driver (enables RTC clocking) */ Chip_RTC_Init(LPC_RTC); /* Enable RTC as a peripheral wakeup event */ //Chip_SYSCON_EnsableWakeup(SYSCON_STARTER_RTC); /* RTC reset */ Chip_RTC_Reset(LPC_RTC); /* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you need to disable it before setting the initial RTC count. */ Chip_RTC_Disable(LPC_RTC); platform_rtc_set_time(&default_rtc_time); //Chip_RTC_SetCount(LPC_RTC, 0); /* Set a long alarm time so the interrupt won't trigger */ //Chip_RTC_SetAlarm(LPC_RTC, (Chip_RTC_GetCount(LPC_RTC) + 5)); /* Enable RTC and high resolution timer - this can be done in a single call with Chip_RTC_EnableOptions(LPC_RTC, (RTC_CTRL_RTC1KHZ_EN | RTC_CTRL_RTC_EN)); */ //Chip_RTC_Enable1KHZ(LPC_RTC); Chip_RTC_Enable(LPC_RTC); /* Clear latched RTC interrupt statuses */ Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ)); return kNoErr; #else return kUnsupportedErr; #endif }
// Set the current time given a time string in the format // 2015-02-21 05:57:00 void setTime(char *timeStr){ // Enable and configure Real-Time Clock Chip_Clock_EnableRTCOsc(); Chip_RTC_Init(LPC_RTC); // Set time in human readable form struct tm tm; time_t t = 0; tm = *localtime(&t); sscanf(timeStr, "%u-%u-%u %u:%u:%u", &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday), &(tm.tm_hour), &(tm.tm_min), &(tm.tm_sec)); tm.tm_year -= 1900; tm.tm_mon -= 1; tm.tm_isdst = 0; // No DST accounting t = mktime(&tm); Chip_RTC_Reset(LPC_RTC); // Set then clear SWRESET bit Chip_RTC_SetCount(LPC_RTC, t); // Set the time Chip_RTC_Enable(LPC_RTC); // Start counting log_string("Time Set"); }
/** * @brief Application main function * @return Does not return */ int main(void) { /* Board Initialization */ SystemCoreClockUpdate(); Board_Init(); /* Clock enables and checks */ if (CLKOUT_SEL == SYSCON_CLKOUTSRC_RTC) { /* Turn on the RTC 32K Oscillator */ Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_32K_OSC); Chip_Clock_EnableRTCOsc(); } else if (CLKOUT_SEL == SYSCON_CLKOUTSRC_WDTOSC) { /* Enable the power to the WDT Oscillator */ Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_WDT_OSC); } else if (CLKOUT_SEL == SYSCON_CLKOUTSRC_CLKIN) { /* Setup CLKIN via IOCON (pin muxing) */ Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 22, (IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_DIGITAL_EN)); if (Chip_Clock_GetExtClockInRate() == 0) { /* Can't continue, stop! */ DEBUGSTR("CLKIN selected for CLKOUT, but CLKIN rate is 0\r\n"); while (1) {} } } /* Map P0.21 as CLKOUT pin */ Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 21, (IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_DIGITAL_EN)); /* There isn't too much to this example */ Chip_Clock_SetCLKOUTSource(CLKOUT_SEL, CLKOUT_DIV); return 0; }
/** * @brief Main program body * @return int */ int main(void) { CHIP_PMU_MCUPOWER_T crntPowerSetting; /* Setup SystemCoreClock and any needed board code */ SystemCoreClockUpdate(); Board_Init(); Board_LED_Set(0, true); /* Clear any previously set deep power down and sleep flags */ Chip_PMU_ClearSleepFlags(LPC_PMU, PMU_PCON_SLEEPFLAG | PMU_PCON_DPDFLAG); /* Enable the RTC oscillator, oscillator rate can be determined by calling Chip_Clock_GetRTCOscRate() */ Chip_Clock_EnableRTCOsc(); /* Initialize RTC driver (enables RTC clocking) */ Chip_RTC_Init(LPC_RTC); /* RTC reset */ Chip_RTC_Reset(LPC_RTC); /* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you need to disable it before setting the initial RTC count. */ Chip_RTC_Disable(LPC_RTC); Chip_RTC_SetCount(LPC_RTC, 0); /* Set a long alarm time so the interrupt won't trigger */ Chip_RTC_SetAlarm(LPC_RTC, 1000); /* Enable RTC */ Chip_RTC_Enable(LPC_RTC); /* Clear latched RTC interrupt statuses */ Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ)); /* Enable RTC interrupt */ NVIC_EnableIRQ(RTC_ALARM_IRQn); /* Enable RTC alarm interrupt */ Chip_RTC_EnableWakeup(LPC_RTC, RTC_CTRL_ALARMDPD_EN); /* Output example's activity banner */ DEBUGSTR("\r\n"); DEBUGSTR("-----------------------------------------------------------------\r\n"); #ifdef RESET_POWER_CYCLE_COUNT ProcessCycleCounter(); DEBUGOUT("Power Control Example\r\n"); #else DEBUGOUT("Power Control Example Cycle Count: %d\r\n", ProcessCycleCounter()); #endif DEBUGSTR(" System will cycle through SLEEP, DEEP SLEEP, POWER\r\n"); DEBUGSTR(" DOWN, and DEEP POWER DOWN power states\r\n"); DEBUGSTR("-----------------------------------------------------------------\r\n\r\n"); /* Setup alarm, process next power state then wait for alarm to wake-up system */ crntPowerSetting = PMU_MCU_SLEEP; while (1) { /* Set alarm to wakeup in POWER_CYCLE_SEC_DELAY seconds */ Chip_RTC_SetAlarm(LPC_RTC, Chip_RTC_GetCount(LPC_RTC) + POWER_CYCLE_SEC_DELAY); /* Enter first (or next) power state */ ProcessPowerState(crntPowerSetting); /* Inc current power setting and test for overflow */ if (crntPowerSetting == PMU_MCU_DEEP_PWRDOWN) { /* Reset to lowest power setting */ crntPowerSetting = PMU_MCU_SLEEP; } else { crntPowerSetting++; } } return 0; }
/** * @brief Main program body * @return int */ int main(void) { uint32_t rtcCount; /* Starting time for this example used to set the RTC */ struct tm startTime = { 56, /* tm_sec, seconds after the minute, 0 to 59 */ 59, /* tm_min, minutes after the hour, 0 to 59 */ 23, /* tm_hour, hours since midnight, 0 to 23 */ 25, /* tm_mday, day of the month, 1 to 31 */ 9, /* tm_mon, months since January, 0 to 11 */ (2014 - TM_YEAR_BASE), /* tm_year, years since base year (1900) */ /* The following 3 fields are not used by the ConvertTimeRtc() function, while the ConvertRtcTime() fucntion will generate data in them from an RTC tick (except tm_isdst) */ 0, /* tm_wday, days since Sunday, 0 to 6 */ 0, /* tm_yday, days since January 1, 0 to 365 */ 0 /* tm_isdst, Daylight Savings Time flag */ }; /* Setup SystemCoreClock and any needed board code */ SystemCoreClockUpdate(); Board_Init(); Board_LED_Set(0, false); /* Turn on the RTC 32K Oscillator */ Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_32K_OSC); /* Enable the RTC oscillator, oscillator rate can be determined by calling Chip_Clock_GetRTCOscRate() */ Chip_Clock_EnableRTCOsc(); /* Initialize RTC driver (enables RTC clocking) */ Chip_RTC_Init(LPC_RTC); /* RTC reset */ Chip_RTC_Reset(LPC_RTC); /* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you need to disable it before setting the initial RTC count. */ Chip_RTC_Disable(LPC_RTC); /* COnvert tm structure to RTC tick count */ ConvertTimeRtc(&startTime, &rtcCount); Chip_RTC_SetCount(LPC_RTC, rtcCount); /* Set alarm to wakeup in 5s */ DEBUGOUT("Setting alarm to trigger in 5s\r\n"); rtcCount += 5; Chip_RTC_SetAlarm(LPC_RTC, rtcCount); /* Enable RTC */ Chip_RTC_Enable(LPC_RTC); /* Clear latched RTC interrupt statuses */ Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ)); /* Enable RTC interrupt */ NVIC_EnableIRQ(RTC_IRQn); /* Enable RTC alarm interrupt */ Chip_RTC_EnableWakeup(LPC_RTC, (RTC_CTRL_ALARMDPD_EN | RTC_CTRL_WAKEDPD_EN)); /* Sleep and do all the work in the RTC interrupt handler */ while (1) { /* 10 high resolution ticks that get slower each tick */ if (rtcAlarm) { /* Will not reset alarm */ DEBUGOUT("Alarm triggered, alarm reset to trigger in 5s\r\n"); rtcCount = Chip_RTC_GetCount(LPC_RTC) + 5; Chip_RTC_SetAlarm(LPC_RTC, rtcCount); rtcAlarm = false; } /* Show RTC time in UT format */ showTime(Chip_RTC_GetCount(LPC_RTC)); /* Sleep while waiting for alarm */ __WFI(); } return 0; }
/** * @brief Main program body * @return int */ int main(void) { int stateCounter = 0; /* Setup SystemCoreClock and any needed board code */ SystemCoreClockUpdate(); Board_Init(); Board_LED_Set(0, false); /* Turn on the RTC 32K Oscillator by clearing the power down bit */ if ( !Is_Chip_SYSCTL_PowerUp(PDRUNCFG_PD_32K_OSC) ) { Chip_SYSCTL_PowerUp(PDRUNCFG_PD_32K_OSC); } /* Enable the RTC oscillator, oscillator rate can be determined by calling Chip_Clock_GetRTCOscRate() */ Chip_Clock_EnableRTCOsc(); /* Initialize RTC driver (enables RTC clocking) */ Chip_RTC_Init(LPC_RTC); /* Enable RTC as a peripheral wakeup event */ Chip_SYSCTL_EnableWakeup(STARTERP0_RTC); /* RTC reset */ Chip_RTC_Reset(LPC_RTC); /* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you need to disable it before setting the initial RTC count. */ Chip_RTC_Disable(LPC_RTC); Chip_RTC_SetCount(LPC_RTC, 0); /* Set a long alarm time so the interrupt won't trigger */ Chip_RTC_SetAlarm(LPC_RTC, 1000); /* Enable RTC and high resolution timer - this can be done in a single call with Chip_RTC_EnableOptions(LPC_RTC, (RTC_CTRL_RTC1KHZ_EN | RTC_CTRL_RTC_EN)); */ Chip_RTC_Enable1KHZ(LPC_RTC); Chip_RTC_Enable(LPC_RTC); /* Clear latched RTC interrupt statuses */ Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ)); /* Enable RTC interrupt */ NVIC_EnableIRQ(RTC_IRQn); /* Enable RTC alarm interrupt */ Chip_RTC_EnableWakeup(LPC_RTC, (RTC_CTRL_ALARMDPD_EN | RTC_CTRL_WAKEDPD_EN)); /* Sleep and do all the work in the RTC interrupt handler */ while (1) { DEBUGOUT("Tick number: %d, 1KHZ int:%d, alarm int:%d\r\n", stateCounter, rtcWake, rtcAlarm); rtcWake = rtcAlarm = false; /* 10 high resolution ticks that get slower each tick */ if (stateCounter < 10) { /* Wakeup in 300, 400, 500, etc. milliSeconds */ Chip_RTC_SetWake(LPC_RTC, (300 + (stateCounter * 100))); stateCounter++; } else { DEBUGOUT("Setting alarm to wake up in 4s\r\n"); /* Set alarm to wakeup in 4 seconds */ Chip_RTC_SetAlarm(LPC_RTC, Chip_RTC_GetCount(LPC_RTC) + 4); stateCounter = 0; } // FIXME - Use ROM API's to put chip to power down modes /* LPC_PWRD_API->power_mode_configure(PMU_DEEP_POWERDOWN, PDRUNCFG_PD_32K_OSC, 1); */ __WFI(); } return 0; }
/** * @brief Main program body * @return int */ int main(void) { int stateCounter = 0; uint32_t ticks = 0; /* Setup SystemCoreClock and any needed board code */ SystemCoreClockUpdate(); Board_Init(); /* Enable the RTC oscillator, oscillator rate can be determined by calling Chip_Clock_GetRTCOscRate() */ Chip_Clock_EnableRTCOsc(); /* Initialize RTC driver (enables RTC clocking) */ Chip_RTC_Init(LPC_RTC); /* Enable RTC as a peripheral wakeup event */ Chip_SYSCTL_EnableERP1PeriphWakeup(SYSCTL_ERP1_WAKEUP_RTCALARMINT | SYSCTL_ERP1_WAKEUP_RTCWAKEINT); /* RTC reset */ Chip_RTC_Reset(LPC_RTC); /* Start RTC at a count of 0 when RTC is disabled. If the RTC is enabled, you need to disable it before setting the initial RTC count. */ Chip_RTC_Disable(LPC_RTC); Chip_RTC_SetCount(LPC_RTC, 0); /* Set a long alarm time so the interrupt won't trigger */ Chip_RTC_SetAlarm(LPC_RTC, 1000); /* Enable RTC and high resolution timer - this can be done in a single call with Chip_RTC_EnableOptions(LPC_RTC, (RTC_CTRL_RTC1KHZ_EN | RTC_CTRL_RTC_EN)); */ Chip_RTC_Enable1KHZ(LPC_RTC); Chip_RTC_Enable(LPC_RTC); /* Clear latched RTC interrupt statuses */ Chip_RTC_ClearStatus(LPC_RTC, (RTC_CTRL_OFD | RTC_CTRL_ALARM1HZ | RTC_CTRL_WAKE1KHZ)); /* Enable RTC wake and alarm interrupts */ NVIC_EnableIRQ(RTC_ALARM_IRQn); NVIC_EnableIRQ(RTC_WAKE_IRQn); /* Enable RTC alarm interrupt */ Chip_RTC_EnableWakeup(LPC_RTC, (RTC_CTRL_ALARMDPD_EN | RTC_CTRL_WAKEDPD_EN)); /* Sleep and do all the work in the RTC interrupt handler */ while (1) { ticks = 0; DEBUGOUT("Tick number: %d, 1KHZ int:%d, alarm int:%d\r\n", stateCounter, rtcWake, rtcAlarm); rtcWake = rtcAlarm = false; /* 10 high resolution ticks that get slower each tick */ if (stateCounter < 10) { /* Wakeup in 300, 400, 500, etc. milliSeconds */ Chip_RTC_SetWake(LPC_RTC, (300 + (stateCounter * 100))); stateCounter++; } else { DEBUGOUT("Setting alarm to wake up in 4s\r\n"); /* Set alarm to wakeup in 4 seconds */ Chip_RTC_SetAlarm(LPC_RTC, Chip_RTC_GetCount(LPC_RTC) + 4); stateCounter = 0; } Chip_SYSCTL_SetWakeup(~(SYSCTL_SLPWAKE_IRCOUT_PD | SYSCTL_SLPWAKE_IRC_PD | SYSCTL_SLPWAKE_FLASH_PD | SYSCTL_SLPWAKE_SYSOSC_PD | SYSCTL_SLPWAKE_SYSPLL_PD)); Chip_SYSCTL_EnableERP1PeriphWakeup(SYSCTL_ERP1_WAKEUP_RTCALARMINT | SYSCTL_ERP1_WAKEUP_RTCWAKEINT); /* Delay to allow serial transmission to complete*/ while(ticks++ < 10000) {} /* Enter MCU Deep Sleep mode */ Chip_PMU_DeepSleepState(LPC_PMU); } return 0; }