/** * @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; }
/* 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 }
/* Initialize the ADC ROM Driver */ static int adcrom_init(void) { volatile int size_in_bytes; ADC_PinMuxSetup(); Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_ADC0 | SYSCON_PDRUNCFG_PD_VDDA_ENA | SYSCON_PDRUNCFG_PD_VREFP); Chip_Clock_EnablePeriphClock(SYSCON_CLOCK_ADC0); Chip_Clock_SetADCClockSource(SYSCON_ADCCLKSELSRC_MAINCLK); Chip_Clock_SetADCClockDiv(0x1); size_in_bytes = ROM_ADC_GetMemSize(); if (RAMBLOCK_H < (size_in_bytes / 4)) { return 1; } hADC = ROM_ADC_Init(start_of_ram_block0, LPC_ADC_BASE, 0); return 0; }
/* Clock and PLL initialization based on the internal oscillator */ void Chip_SetupIrcClocking(uint32_t iFreq) { PLL_CONFIG_T pllConfig; PLL_SETUP_T pllSetup; PLL_ERROR_T pllError; /* Turn on the IRC by clearing the power down bit */ Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_IRC_OSC | SYSCON_PDRUNCFG_PD_IRC); /* Select the PLL input to the IRC */ Chip_Clock_SetSystemPLLSource(SYSCON_PLLCLKSRC_IRC); /* Setup FLASH access */ setupFlashClocks(iFreq); /* Power down PLL to change the PLL divider ratio */ Chip_SYSCON_PowerDown(SYSCON_PDRUNCFG_PD_SYS_PLL); /* Setup PLL configuration */ pllConfig.desiredRate = iFreq; pllConfig.InputRate = 0; pllConfig.flags = PLL_CONFIGFLAG_FORCENOFRACT; pllError = Chip_Clock_SetupPLLData(&pllConfig, &pllSetup); if (pllError == PLL_ERROR_SUCCESS) { pllSetup.flags = PLL_SETUPFLAG_WAITLOCK | PLL_SETUPFLAG_ADGVOLT; pllError = Chip_Clock_SetupSystemPLLPrec(&pllSetup); } Chip_Clock_SetSysClockDiv(1); Chip_Clock_SetMainClockSource(SYSCON_MAINCLKSRC_PLLOUT); //Chip_Clock_SetCLKOUTSource(SYSCON_CLKOUTSRC_MAINCLK, 1); /* ASYSNC SYSCON needs to be on or all serial peripheral won't work. Be careful if PLL is used or not, ASYNC_SYSCON source needs to be selected carefully. */ Chip_SYSCON_Enable_ASYNC_Syscon(true); Chip_Clock_SetAsyncSysconClockDiv(4); Chip_Clock_SetAsyncSysconClockSource(SYSCON_ASYNC_MAINCLK);//SYSCON_ASYNC_IRC);//SYSCON_ASYNC_SYSPLLOUT }
/** * @brief main routine for bown-out example * @return Function should not exit. */ int main(void) { /* Generic Initialization */ SystemCoreClockUpdate(); Board_Init(); Board_LED_Set(0, false); Board_LED_Set(1, false); Board_LED_Set(2, false); /* If the board was reset due to a BOD event, the reset can be detected here. Turn on LED0 if reset occured due to BOD. */ if ((Chip_SYSCON_GetSystemRSTStatus() & SYSCON_RST_BOD) != 0) { Chip_SYSCON_ClearSystemRSTStatus(SYSCON_RST_BOD); Board_LED_Set(0, true); } /* Enable BOD power */ Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_BOD_RST | SYSCON_PDRUNCFG_PD_BOD_INTR); /* Set BOD detection interrupt to 3.05v and device reset to 1.5v */ Chip_PMU_SetBODLevels(PMU_BODRSTLVL_1_50V, PMU_BODINTVAL_3_05v); /* Enable BOD reset and interrupt on low power level */ Chip_PMU_EnableBODReset(); Chip_PMU_EnableBODInt(); /* Enable BOD interrupt */ NVIC_EnableIRQ(BOD_IRQn); /* Wait forever */ while (1) { __WFI(); } 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 Does not return */ int main(void) { /* Generic Initialization */ SystemCoreClockUpdate(); /* Board_Init calls Chip_GPIO_Init and enables GPIO clock if needed, Chip_GPIO_Init is not called again */ Board_Init(); Board_LED_Set(0, false); Chip_PININT_Init(LPC_PININT); /* Configure GPIO pin as input */ Chip_GPIO_SetPinDIRInput(LPC_GPIO, GPIO_PININT_PORT, GPIO_PININT_PIN); /* Configure pin as GPIO */ Chip_IOCON_PinMuxSet(LPC_IOCON, GPIO_PININT_PORT, GPIO_PININT_PIN, (IOCON_FUNC0 | IOCON_DIGITAL_EN | IOCON_GPIO_MODE)); /* Configure pin interrupt selection for the GPIO pin in Input Mux Block */ Chip_INMUX_PinIntSel(GPIO_PININT_INDEX, GPIO_PININT_PORT, GPIO_PININT_PIN); /* Configure channel interrupt as edge sensitive and falling edge interrupt */ Chip_PININT_ClearIntStatus(LPC_PININT, PININTCH(GPIO_PININT_INDEX)); Chip_PININT_SetPinModeEdge(LPC_PININT, PININTCH(GPIO_PININT_INDEX)); Chip_PININT_EnableIntLow(LPC_PININT, PININTCH(GPIO_PININT_INDEX)); /* Enable interrupt in the NVIC */ NVIC_EnableIRQ(PININT_NVIC_NAME); /* Enable wakeup for PININT0 */ Chip_SYSCON_EnableWakeup(SYSCON_STARTER_PINT0); /* save the clock source, power down the PLL */ saved_clksrc = Chip_Clock_GetMainClockSource(); /* Go to sleep mode - LED will toggle on each wakeup event */ while (1) { /* Go to sleep state - will wake up automatically on interrupt */ /* Disable PLL, if previously enabled, prior to sleep */ if (saved_clksrc == SYSCON_MAINCLKSRC_PLLOUT) { Chip_Clock_SetMainClockSource(SYSCON_MAINCLKSRC_IRC); Chip_SYSCON_PowerDown(SYSCON_PDRUNCFG_PD_SYS_PLL); } /* Lower system voltages to current lock (likely IRC) */ Chip_POWER_SetVoltage(POWER_LOW_POWER_MODE, Chip_Clock_GetMainClockRate()); /* Go to sleep leaving SRAM powered during sleep. Use lower voltage during sleep. */ Chip_POWER_EnterPowerMode(PDOWNMODE, (SYSCON_PDRUNCFG_PD_SRAM0A | SYSCON_PDRUNCFG_PD_SRAM0B)); /* On wakeup, restore PLL power if needed */ if (saved_clksrc == SYSCON_MAINCLKSRC_PLLOUT) { Chip_SYSCON_PowerUp(SYSCON_PDRUNCFG_PD_SYS_PLL); /* Wait for PLL lock */ while (!Chip_Clock_IsSystemPLLLocked()) {} Chip_POWER_SetVoltage(POWER_LOW_POWER_MODE, Chip_Clock_GetSystemPLLOutClockRate(false)); /* Use PLL for system clock */ Chip_Clock_SetMainClockSource(SYSCON_MAINCLKSRC_PLLOUT); } } return 0; }