/**************************************************************************//**
* @file main.c
* @version V2.00
* $Revision: 4 $
* $Date: 14/04/28 5:51p $
* @brief NUC230_240 Series RTC Driver Sample Code
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NUC230_240.h"
#define PLLCON_SETTING CLK_PLLCON_50MHz_HXT
#define PLL_CLOCK 50000000
void RTC_IRQHandler(void)
{
/* To check if RTC Tick interrupt occurred */
if(RTC_GET_TICK_INT_FLAG() == 1) {
/* Clear RTC tick interrupt flag */
RTC_CLEAR_TICK_INT_FLAG();
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Global Interface Variables Declarations */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable IRC22M clock */
CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk;
/* Waiting for IRC22M clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);
/* Switch HCLK clock source to HIRC */
CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_HIRC;
/* Set PLL to Power-down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
CLK->PLLCON |= CLK_PLLCON_PD_Msk;
/* Enable external 12 MHz XTAL, 32 kHz XTAL */
CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_XTL32K_EN_Msk;
/* Enable PLL and Set PLL frequency */
CLK->PLLCON = PLLCON_SETTING;
/* Waiting for clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_XTL32K_STB_Msk);
/* Switch HCLK clock source to PLL, STCLK to HCLK/2 */
CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_HCLK_DIV2 | CLK_CLKSEL0_HCLK_S_PLL;
/* Enable peripheral clock */
CLK->APBCLK = CLK_APBCLK_UART0_EN_Msk | CLK_APBCLK_RTC_EN_Msk;
/* Peripheral clock source */
CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL;
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
SystemCoreClockUpdate();
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PB multi-function pins for UART0 RXD, TXD */
SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
}
void UART0_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset IP */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
S_RTC_TIME_DATA_T sWriteRTC, sReadRTC;
uint32_t u32Sec,u32Min;
/* 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();
/* Open RTC and start counting */
sWriteRTC.u32Year = 2014;
sWriteRTC.u32Month = 2;
sWriteRTC.u32Day = 6;
sWriteRTC.u32DayOfWeek = RTC_THURSDAY;
sWriteRTC.u32Hour = 15;
sWriteRTC.u32Minute = 30;
sWriteRTC.u32Second = 30;
sWriteRTC.u32TimeScale = RTC_CLOCK_24;
RTC_Open(&sWriteRTC);
/* Enable RTC tick interrupt, one RTC tick is 1/4 second */
// NVIC_EnableIRQ(RTC_IRQn);
// RTC_EnableInt(RTC_RIER_TIER_Msk);
// RTC_SetTickPeriod(RTC_TICK_1_4_SEC);
// Enable spare register
RTC_EnableSpareRegister();
while((RTC->SPRCTL&RTC_SPRCTL_SPRRDY_Msk)==0);
RTC->SPR[0]=1;
while((RTC->SPRCTL&RTC_SPRCTL_SPRRDY_Msk)==0);
RTC->SPR[1]=2;
while((RTC->SPRCTL&RTC_SPRCTL_SPRRDY_Msk)==0);
u32Sec=RTC->SPR[0];
while((RTC->SPRCTL&RTC_SPRCTL_SPRRDY_Msk)==0);
u32Min=RTC->SPR[1];
if((u32Sec==1)&&(u32Min==2))
printf("SpareRegister Access Successful\n");
else
printf("SpareRegister Access Fail\n");
while(1);
}
/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
|