/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V3.00
* [url=home.php?mod=space&uid=247401]@brief[/url] Use timer to wake up system from Power-down mode periodically.
*
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
/**
* @brief Timer0 IRQ
*
* @param None
*
* [url=home.php?mod=space&uid=266161]@return[/url] None
*
* [url=home.php?mod=space&uid=1543424]@Details[/url] The Timer0 default IRQ, declared in startup_M261.s.
*/
void TMR0_IRQHandler(void)
{
if(TIMER0->INTSTS & TIMER_INTSTS_TWKF_Msk)
{
printf("wk %x ", CLK->PMUSTS);
}
/* Clear wake up flag */
TIMER_ClearWakeupFlag(TIMER0);
if(TIMER0->INTSTS & TIMER_INTSTS_TIF_Msk)
{
printf("IF");
}
/* Clear interrupt flag */
TIMER_ClearIntFlag(TIMER0);
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HIRC clock */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Waiting for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Switch HCLK clock source to HIRC */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Enable LIRC */
CLK_EnableXtalRC(CLK_PWRCTL_LIRCEN_Msk);
/* Waiting for clock ready */
CLK_WaitClockReady(CLK_STATUS_LIRCSTB_Msk);
/* Enable PLL */
CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;
/* Waiting for PLL stable */
CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
/* Select HCLK clock source as PLL and HCLK source divider as 2 */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));
/* Set SysTick source to HCLK/2 */
CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
/* Enable TIMER module clock */
CLK_EnableModuleClock(TMR0_MODULE);
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_LIRC, 0);
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;
}
void UART_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART module */
SYS_ResetModule(UART0_RST);
/* Configure UART and set UART Baudrate */
UART_Open(DEBUG_PORT, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
volatile uint32_t u32Loop = 0;
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Init UART for printf */
UART_Init();
/* Lock protected registers */
SYS_LockReg();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
printf("Timer power down/wake up sample code.\n\n");
while(!UART_IS_TX_EMPTY(DEBUG_PORT));
/* Initial Timer0 to periodic mode with 1Hz, since system is fast (64MHz)
and timer is slow (10kHz), and following function calls all modified timer's
CTL register, so add extra delay between each function call and make sure the
setting take effect */
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);
CLK_SysTickDelay(50);
/* Enable timer wake up system */
TIMER_EnableWakeup(TIMER0);
CLK_SysTickDelay(50);
/* Enable Timer0 interrupt */
TIMER_EnableInt(TIMER0);
CLK_SysTickDelay(50);
NVIC_EnableIRQ(TMR0_IRQn);
/* Start Timer0 counting */
TIMER_Start(TIMER0);
CLK_SysTickDelay(50);
/* Unlock protected registers for execute CLK_PowerDown() */
SYS_UnlockReg();
while(1)
{
CLK_PowerDown();
printf("\nWake %d\n", u32Loop++);
while(!UART_IS_TX_EMPTY(DEBUG_PORT));
}
}
/*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/
|