/****************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V3.00
* $Revision: 3 $
* $Date: 15/08/07 1:31p $
* [url=home.php?mod=space&uid=247401]@brief[/url] Show how to wake up system form Power-down mode by UART interrupt.
*
* @note
* Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "M051Series.h"
#define PLL_CLOCK 50000000
/*---------------------------------------------------------------------------------------------------------*/
/* Define functions prototype */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void);
void UART_CTSWakeUpTest(void);
/*---------------------------------------------------------------------------------------------------------*/
/* ISR to handle UART Channel 1 interrupt event */
/*---------------------------------------------------------------------------------------------------------*/
void UART1_IRQHandler(void)
{
if(UART_GET_INT_FLAG(UART1, UART_ISR_MODEM_INT_Msk)) /* UART Modem Status interrupt flag */
{
printf("UART Modem Status interrupt happen.");
UART_ClearIntFlag(UART1, UART_ISR_MODEM_INT_Msk); /* Clear UART Modem Status interrupt flag */
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function for System Entry to Power Down Mode */
/*---------------------------------------------------------------------------------------------------------*/
void PowerDownFunction(void)
{
/* Check if all the debug messages are finished */
UART_WAIT_TX_EMPTY(UART0);
/* Enter to Power-down mode */
CLK_PowerDown();
}
/*---------------------------------------------------------------------------------------------------------*/
/* UART nCTS Wake-up Function */
/*---------------------------------------------------------------------------------------------------------*/
void UART_CTSWakeUpTest(void)
{
printf("+----------------------------------------------------------+\n");
printf("| Power-Down and Wake-up by UART interrupt Sample Code |\n");
printf("+----------------------------------------------------------+\n\n");
/* Clear Modem Status interrupt before entering Power-down mode */
UART_ClearIntFlag(UART1, UART_ISR_MODEM_INT_Msk);
/* Enable UART Wake-up function and Modem Status interrupt */
UART_EnableInt(UART1, UART_IER_WAKE_EN_Msk|UART_IER_MODEM_IEN_Msk);
printf("System enter to Power-down mode.\n");
printf("Toggle nCTS of UART1 to wake-up system.\n\n");
/* Unlock protected registers before entering Power-down mode */
SYS_UnlockReg();
/* Enter to Power-down mode */
PowerDownFunction();
/* Lock protected registers after entering Power-down mode */
SYS_LockReg();
/* Disable UART Wake-up function and Modem Status interrupt */
UART_DisableInt(UART1, UART_IER_WAKE_EN_Msk|UART_IER_MODEM_IEN_Msk);
printf("\nSystem waken-up done.\n");
printf("\nUART Sample Program End.\n");
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable Internal RC 22.1184MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
/* Waiting for Internal RC clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
/* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
/* Enable external XTAL 12MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
/* Waiting for external XTAL clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
/* Set core clock as PLL_CLOCK from PLL */
CLK_SetCoreClock(PLL_CLOCK);
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_EnableModuleClock(UART1_MODULE);
/* Select UART module clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set P3 multi-function pins for UART0 RXD and TXD */
SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);
/* Set P1 multi-function pins for UART1 RXD and TXD */
SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
SYS->P1_MFP |= (SYS_MFP_P12_RXD1 | SYS_MFP_P13_TXD1);
/* Set P0 multi-function pins for UART1 CTS */
SYS->P0_MFP = SYS->P0_MFP & (~SYS_MFP_P00_Msk) | SYS_MFP_P00_CTS1;
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART0 */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
void UART1_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART1 */
SYS_ResetModule(UART1_RST);
/* Configure UART1 and set UART1 Baudrate */
UART_Open(UART1, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
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();
/* Init UART0 for testing */
UART1_Init();
/*---------------------------------------------------------------------------------------------------------*/
/* SAMPLE CODE */
/*---------------------------------------------------------------------------------------------------------*/
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
printf("\n\nUART Sample Program.\n");
/* UART wake-up sample function */
UART_CTSWakeUpTest();
while(1);
}
|