/** * @brief Save multi-function pin setting and then go to power down. * @param None. * @return None. */ void Enter_PowerDown() { /* Back up original setting */ SavePinSetting(); /* Set function pin to GPIO mode */ SYS->PA_L_MFP = 0; SYS->PA_H_MFP = 0; SYS->PB_L_MFP = 0; SYS->PB_H_MFP = 0; SYS->PC_L_MFP = 0; SYS->PC_H_MFP = 0; SYS->PD_L_MFP = 0; SYS->PD_H_MFP = 0; SYS->PE_L_MFP = 0; SYS->PE_H_MFP = 0; SYS->PF_L_MFP = 0x00007700; /* Enable GPIO pull up */ PA->PUEN = 0xFFFF; PB->PUEN = 0xFFFF; PC->PUEN = 0xFFFF; PD->PUEN = 0xFFFF; PE->PUEN = 0xFFFF; PF->PUEN = 0x0033; /* exclude GPF2 and GPF3 which are HXT OUT/IN */ /* Disable LCD clock */ CLK->APBCLK &= ~CLK_APBCLK_LCD_EN; /* Disable LCD clock */ CLK->PWRCTL &= ~CLK_PWRCTL_LXT_EN_Msk; /* disable LXT - 32Khz */ CLK_PowerDown(); }
int main(void) { int i = 0; /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); /*Initial Timer0 to periodic mode with 1Hz */ TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1); /* Enable timer wake up system */ TIMER_EnableWakeup(TIMER0); /* Enable Timer0 interrupt */ TIMER_EnableInt(TIMER0); NVIC_EnableIRQ(TMR0_IRQn); /* Start Timer0 counting */ TIMER_Start(TIMER0); /* Unlock protected registers */ SYS_UnlockReg(); while(1) { CLK_PowerDown(); printf("Wake %d\n", i++); } }
/** * @brief Save multi-function pin setting and then go to power down. * @param None. * @return None. */ void Enter_PowerDown() { /* Back up original setting */ SavePinSetting(); /* Set function pin to GPIO mode */ SYS->PA_L_MFP = 0; SYS->PA_H_MFP = 0; SYS->PB_L_MFP = 0; SYS->PB_H_MFP = 0; SYS->PC_L_MFP = 0; SYS->PC_H_MFP = 0; SYS->PD_L_MFP = 0; SYS->PD_H_MFP = 0; SYS->PE_L_MFP = 0; SYS->PE_H_MFP = 0; SYS->PF_L_MFP = 0x00007700; /* Enable GPIO pull up */ PA->PUEN = 0xFFFF; PB->PUEN = 0xFFFF; PC->PUEN = 0xFFFF; PD->PUEN = 0xFFFF; PE->PUEN = 0xFFFF; PF->PUEN = 0x0033; /* exclude GPF2 and GPF3 which are HXT OUT/IN */ CLK->PWRCTL |= CLK_PWRCTL_LXT_EN_Msk; /* enable LXT - 32Khz */ CLK->PWRCTL |= CLK_PWRCTL_PD_WK_IE_Msk; /* Enable wake up interrupt source */ NVIC_EnableIRQ(PDWU_IRQn); /* Enable IRQ request for PDWU interrupt */ CLK_PowerDown(); }
/*---------------------------------------------------------------------------------------------------------*/ void PowerDownFunction(void) { /* Check if all the debug messages are finished */ UART_WAIT_TX_EMPTY(UART0); /* Enter to Power-down mode */ CLK_PowerDown(); }
/*---------------------------------------------------------------------------------------------------------*/ void PowerDownFunction(void) { /* Check if all the debug messages are finished */ UART_WAIT_TX_EMPTY(UART0); /* Enable Power-down mode wake-up interrupt */ CLK->PWRCTL |= CLK_PWRCTL_PDWKIEN_Msk; /* Enter to Power-down mode */ CLK_PowerDown(); }
//========================================================================= //----- (00005D14) -------------------------------------------------------- __myevic__ void FlushAndSleep() { #if (ENABLE_UART) UART_WAIT_TX_EMPTY( UART0 ); #endif if ( !gFlags.light_sleep ) { CLK_PowerDown(); } else { LightSleep(); } }
void Sys_Sleep() { uint8_t i; const Sys_WakeupGPIO_t *gpio; SYS_UnlockReg(); // In power down mode the only active clocks are LIRC and LXT. // All our pheriperials are clocked from HXT or PLL, so we // don't have to worry about stopping them. // Button debounce is clocked from LIRC so it will work. // Disable unwanted interrupts for(i = 0; i < 4; i++) { gpio = &Sys_wakeupGPIO[i]; if(!(Sys_wakeupSource & gpio->wakeupMask)) { GPIO_DisableInt(gpio->port, gpio->pin); } } // Enable wakeup interrupt CLK->PWRCTL |= CLK_PWRCTL_PDWKIEN_Msk; NVIC_EnableIRQ(PWRWU_IRQn); // Sleep CLK_PowerDown(); // Disable wakeup interrupt CLK->PWRCTL &= ~CLK_PWRCTL_PDWKIEN_Msk; NVIC_DisableIRQ(PWRWU_IRQn); // Re-enable previously disabled interrupts // All those interrupts are always enabled anyway, // so we just enable them all. for(i = 0; i < 4; i++) { gpio = &Sys_wakeupGPIO[i]; GPIO_EnableInt(gpio->port, gpio->pin, GPIO_INT_BOTH_EDGE); } SYS_LockReg(); }
int main(void) { /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); printf("\nThis sample code use timer to wake up system every 1 second \n"); /*Initial Timer0 to periodic mode with 1Hz */ TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1); /* Enable timer wake up system */ TIMER_EnableWakeup(TIMER0); /* Enable Timer0 interrupt */ TIMER_EnableInt(TIMER0); NVIC_EnableIRQ(TMR0_IRQn); /* Start Timer0 counting */ TIMER_Start(TIMER0); /* Unlock protected registers to let CPU enter power down mode */ SYS_UnlockReg(); while(1) { printf("Sleep 1 second\n"); // Wait 'til UART FIFO empty to get a cleaner console out while(!UART_IS_TX_EMPTY(UART0)); CLK_PowerDown(); } }
/*---------------------------------------------------------------------------------------------------------*/ int main(void) { /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, peripheral clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); printf("\n\nCPU @ %d Hz\n", SystemCoreClock); printf("+----------------------------------------+\n"); printf("| WDT Time-out Wake-up Sample Code |\n"); printf("+----------------------------------------+\n\n"); /* To check if system has been reset by WDT time-out reset or not */ if(WDT_GET_RESET_FLAG() == 1) { WDT_CLEAR_RESET_FLAG(); printf("*** System has been reset by WDT time-out event ***\n\n"); while(1); } printf("# WDT Settings:\n"); printf(" - Clock source is LIRC \n"); printf(" - Time-out interval is 2^14 * WDT clock \n"); printf(" (around 1.6384 ~ 1.7408 s) \n"); printf(" - Interrupt enable \n"); printf(" - Wake-up function enable \n"); printf(" - Reset function enable \n"); printf(" - Reset delay period is 18 * WDT clock \n"); printf("# System will generate a WDT time-out interrupt event after 1.6384 ~ 1.7408 s, \n"); printf(" and will be wake up from power-down mode also.\n"); printf(" (Use PA.0 high/low period time to check WDT time-out interval)\n"); printf("# When WDT interrupt counts large than 10, \n"); printf(" we will not reset WDT counter value and system will be reset immediately by WDT time-out reset signal.\n"); /* Use PA.0 to check time-out period time */ PA->MODE = 0xFFFFFFFD; PA0 = 1; /* Enable WDT NVIC */ NVIC_EnableIRQ(WDT_IRQn); /* Because of all bits can be written in WDT Control Register are write-protected; To program it needs to disable register protection first. */ SYS_UnlockReg(); /* Configure WDT settings and start WDT counting */ g_u32WDTINTCounts = g_u8IsWDTWakeupINT = 0; WDT_Open(WDT_TIMEOUT_2POW14, WDT_RESET_DELAY_18CLK, TRUE, TRUE); /* Enable WDT interrupt function */ WDT_EnableInt(); while(1) { /* System enter to Power-down */ /* To program PWRCTL register, it needs to disable register protection first. */ SYS_UnlockReg(); printf("\nSystem enter to power-down mode ...\n"); /* To check if all the debug messages are finished */ while(IsDebugFifoEmpty() == 0); CLK_PowerDown(); /* Check if WDT time-out interrupt and wake-up occurred or not */ while(g_u8IsWDTWakeupINT == 0); g_u8IsWDTWakeupINT = 0; PA0 ^= 1; printf("System has been waken up done. WDT interrupt counts: %d.\n\n", g_u32WDTINTCounts); } }
int32_t main (void) { uint32_t u32data; /* HCLK will be set to 42MHz in SYS_Init(void)*/ if(SYS->RegLockAddr == 1) // In end of main function, program issued CPU reset and write-protection will be disabled. SYS_LockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); //In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register. /* Init UART0 for printf */ UART0_Init(); printf("\n\nCPU @ %dHz\n", SystemCoreClock); /* This sample code will show some function about system manager controller and clock controller: 1. Read PDID 2. Get and clear reset source 3. Setting about BOD 4. Output system clock from CKO pin, and the output frequency = system clock / 4 */ printf("+----------------------------------------+\n"); printf("| Nano100 System Driver Sample Code |\n"); printf("+----------------------------------------+\n"); if (M32(FLAG_ADDR) == SIGNATURE) { printf(" CPU Reset success!\n"); M32(FLAG_ADDR) = 0; printf(" Press any key to continue ...\n"); GetChar(); } /*---------------------------------------------------------------------------------------------------------*/ /* Misc system function test */ /*---------------------------------------------------------------------------------------------------------*/ /* Read Part Device ID */ printf("Product ID 0x%x\n", SYS->PDID); /* Get reset source from last operation */ u32data = SYS_GetResetSrc(); printf("Reset Source 0x%x\n", u32data); /* Clear reset source */ SYS_ClearResetSrc(u32data); /* Unlock protected registers for Brown-Out Detector and power down settings */ SYS_UnlockReg(); /* Check if the write-protected registers are unlocked before BOD setting and CPU Reset */ if (SYS->RegLockAddr != 0) { printf("Protected Address is Unlocked\n"); } /* Enable Brown-Out Detector and Low Voltage Reset function, and set Brown-Out Detector voltage 2.5V , Enable Brown-Out Interrupt function */ SYS_EnableBOD(SYS_BODCTL_BOD25_INT_EN_Msk,SYS_BODCTL_BOD25_EN_Msk); /* Enable BOD IRQ */ NVIC_EnableIRQ(BOD_IRQn); /* Waiting for message send out */ // _UART_WAIT_TX_EMPTY(UART0); /* Enable CKO and output frequency = system clock / 4 */ CLK_EnableCKO(CLK_CLKSEL2_FRQDIV_S_HCLK,1); /* Switch HCLK clock source to Internal 11.0592MHz */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC,CLK_HCLK_CLK_DIVIDER(1)); /* Enable WDT clock */ CLK_EnableModuleClock(WDT_MODULE); /* Enable WDT and interrupt */ WDT->CTL = 0x00000050 | 0x00000004 | 0x00000008; WDT->IER |= 0x00000001; NVIC_EnableIRQ(WDT_IRQn); CLK->PWRCTL |= CLK_PWRCTL_WAKEINT_EN; /* Enable wake up interrupt source */ NVIC_EnableIRQ(PDWU_IRQn); /* Enable IRQ request for PDWU interrupt */ printf("u32PWDU_WakeFlag = %x\n",u32PWDU_WakeFlag); printf("Enter Power Down Mode >>>>>>>>>>>\n"); u32PWDU_WakeFlag = 0; /* clear software semaphore */ while(!(UART0->FSR & UART_FSR_TX_EMPTY_F_Msk)) ; /* waits for message send out */ CLK_PowerDown(); /* CPU Reset test */ printf("Waits for 5 times WDT interrupts.....\n"); while (u32WDT_Ticks <= 5); printf("<<<<<<<<<< Program resumes execution.\n"); printf("u32PWDU_WakeFlag = %x\n",u32PWDU_WakeFlag); /* Write a signature work to SRAM to check if it is reset by software */ M32(FLAG_ADDR) = SIGNATURE; printf("\n\n >>> Reset CPU <<<\n"); /* Reset CPU */ SYS_ResetCPU(); }
int main(void) { uint32_t u32Cnt; /* Init system, IP clock and multi-function I/O */ SYS_Init(); /* Init UART for printf */ UART_Init(); /* Init SPI and LCD */ LCD_Init(); LCD_EnableBackLight(); LCD_ClearScreen(); printf("CPU @ %d Hz\n", SystemCoreClock); LCD_Print(0, "Smpl_DeepSleep"); /*Initialize external interrupt*/ GPIO_Init(); /* P3.2 is used as EINT0 for deep sleep (power down) control. Press P3.2 will toggle power down/wakeup state to show how to enter power down. */ LCD_Print(1, "Press INT "); while (1) { char strClearVal[15] = "Count: "; /* Enter power when key change from low to high */ u32Cnt = 0; while (gi8Key == 1) { sprintf(g_strBuf, "Count:%d", u32Cnt++); LCD_Print(3, strClearVal); LCD_Print(3, g_strBuf); } while(gi8Key == 0) { sprintf(g_strBuf, "Count:%d", u32Cnt++); LCD_Print(3, strClearVal); LCD_Print(3, g_strBuf); } LCD_Print(2, "Deep Sleeping..."); /* Unlock protected registers */ SYS_UnlockReg(); /* Lock protected registers */ SYS_LockReg(); /* We need to disable debounce function before power down, otherwise, there would be twice interrupt when wakeup */ P3->DBEN = 0; /* enter power down */; CLK_PowerDown(); /* Re-enable debounce function if necessary */ P3->DBEN = (1 << 2); LCD_Print(2, "Working... "); printf("\nWorking... \n"); /* Make sure the key is return to high before next key action */ while (gi8Key == 0); gi8Key = 0; } }