3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 /*-----------------------------------------------------------------------------
29 *----------------------------------------------------------------------------*/
31 #include "lpc43xx_rtc.h"
32 #include "lpc43xx_cgu.h"
34 static void init_RTC()
36 /* Enable GPIO register interface clock */
37 LPC_CCU1->CLK_M4_GPIO_CFG |= 1;
38 while (!(LPC_CCU1->CLK_M4_GPIO_STAT & 1)) ;
40 /* RTC Block section ------------------------------------------------------ */
44 /* Set ALARM time for second */
45 RTC_SetAlarmTime (LPC_RTC, RTC_TIMETYPE_SECOND, 30);
47 /* Set the AMR for 30s match alarm interrupt */
48 RTC_AlarmIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE);
50 /* Set the CIIR for minute counter interrupt*/
51 RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_MINUTE, ENABLE);
53 /* Enable rtc (starts increase the tick counter and second counter register) */
54 RTC_Cmd(LPC_RTC, ENABLE);
58 /*-----------------------------------------------------------------------------
60 *----------------------------------------------------------------------------*/
62 #include "lpc43xx_timer.h"
64 static void init_TIM()
66 TIM_TIMERCFG_Type TIM_ConfigStruct;
67 /* Initialize timer 0, prescale count time of 1uS */
68 TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL;
69 TIM_ConfigStruct.PrescaleValue = 204; /* 204MHz */
70 /* Set configuration for Tim_config and Tim_MatchConfig */
71 TIM_Init(LPC_TIMER2, TIM_TIMER_MODE,&TIM_ConfigStruct);
72 TIM_ResetCounter(LPC_TIMER2);
73 /* To start timer 2 */
74 TIM_Cmd(LPC_TIMER2,ENABLE);
79 return (double)LPC_TIMER2->TC/1000000.0;
83 void init_time(void) {
90 struct tm *Cyassl_MDK_gmtime(const time_t *c)
92 static struct tm date ;
94 RTC_TIME_Type RTCFullTime;
95 RTC_GetFullTime (LPC_RTC, &RTCFullTime);
97 date.tm_year = RTCFullTime.YEAR + 100 ;
98 date.tm_mon = RTCFullTime.MONTH - 1 ;
99 date.tm_mday = RTCFullTime.DOM ;
100 date.tm_hour = RTCFullTime.HOUR ;
101 date.tm_min = RTCFullTime.MIN ;
102 date.tm_sec = RTCFullTime.SEC ;
104 #if defined(DEBUG_CYASSL)
106 extern void CYASSL_MSG(char *msg) ;
108 sprintf(msg, "Debug::Cyassl_KEIL_gmtime(DATE=/%4d/%02d/%02d TIME=%02d:%02d:%02d)\n",
109 RTCFullTime.YEAR+2000, RTCFullTime.MONTH, RTCFullTime.DOM,
110 RTCFullTime.HOUR, RTCFullTime.MIN, RTCFullTime.SEC) ;
118 typedef struct func_args {
126 void time_main(void *args)
130 RTC_TIME_Type RTCFullTime;
132 if( args == NULL || ((func_args *)args)->argc == 1) {
133 RTC_GetFullTime (LPC_RTC, &RTCFullTime);
134 printf("Date: %d/%d/%d, Time: %02d:%02d:%02d\n",
135 RTCFullTime.MONTH, RTCFullTime.DOM, RTCFullTime.YEAR+2000,
136 RTCFullTime.HOUR, RTCFullTime.MIN, RTCFullTime.SEC) ;
137 } else if(((func_args *)args)->argc == 3 &&
138 ((func_args *)args)->argv[1][0] == '-' &&
139 ((func_args *)args)->argv[1][1] == 'd' ) {
141 datetime = ((func_args *)args)->argv[2];
142 sscanf(datetime, "%d/%d/%d",
143 (int *)&RTCFullTime.MONTH, (int *)&RTCFullTime.DOM, &year) ;
144 RTCFullTime.YEAR = year - 2000 ;
145 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MONTH, RTCFullTime.MONTH);
146 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_YEAR, RTCFullTime.YEAR);
147 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_DAYOFMONTH, RTCFullTime.DOM);
148 } else if(((func_args *)args)->argc == 3 &&
149 ((func_args *)args)->argv[1][0] == '-' &&
150 ((func_args *)args)->argv[1][1] == 't' ) {
151 RTC_GetFullTime (LPC_RTC, &RTCFullTime);
152 datetime = ((func_args *)args)->argv[2];
153 sscanf(datetime, "%d:%d:%d",
154 (int *)&RTCFullTime.HOUR,
155 (int *)&RTCFullTime.MIN,
156 (int *)&RTCFullTime.SEC
158 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, RTCFullTime.SEC);
159 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MINUTE, RTCFullTime.MIN);
160 RTC_SetTime (LPC_RTC, RTC_TIMETYPE_HOUR, RTCFullTime.HOUR);
161 } else printf("Invalid argument\n") ;