]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/IDE/MDK-ARM/LPC43xx/time-LCP43xx.c
Demo application related:
[freertos] / FreeRTOS-Plus / Source / CyaSSL / IDE / MDK-ARM / LPC43xx / time-LCP43xx.c
1 /* time.c
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
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.
11  *
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.
16  *
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
20  */
21  
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26
27 /*-----------------------------------------------------------------------------
28  *        initialize RTC 
29  *----------------------------------------------------------------------------*/
30 #include <stdio.h>
31 #include "lpc43xx_rtc.h"
32 #include "lpc43xx_cgu.h"
33
34 static void init_RTC() 
35 {       
36                 /* Enable GPIO register interface clock                                     */
37                 LPC_CCU1->CLK_M4_GPIO_CFG     |= 1;
38                 while (!(LPC_CCU1->CLK_M4_GPIO_STAT   & 1)) ;
39         
40     /* RTC Block section ------------------------------------------------------ */
41     /* Init RTC module */
42     RTC_Init(LPC_RTC);
43
44     /* Set ALARM time for second */
45     RTC_SetAlarmTime (LPC_RTC, RTC_TIMETYPE_SECOND, 30);
46
47     /* Set the AMR for 30s match alarm interrupt */
48     RTC_AlarmIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE);
49
50     /* Set the CIIR for minute counter interrupt*/
51     RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_MINUTE, ENABLE);
52
53     /* Enable rtc (starts increase the tick counter and second counter register) */
54     RTC_Cmd(LPC_RTC, ENABLE);
55                                 
56 }
57
58 /*-----------------------------------------------------------------------------
59  *        initialize TIM
60  *----------------------------------------------------------------------------*/
61  
62 #include "lpc43xx_timer.h"
63
64 static void init_TIM()
65 {
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);
75 }
76
77 double current_time() 
78 {
79     return (double)LPC_TIMER2->TC/1000000.0; 
80 }
81
82
83 void init_time(void) {
84           init_RTC() ;
85     init_TIM() ;
86 }
87
88 #include <time.h>
89
90 struct tm *Cyassl_MDK_gmtime(const time_t *c) 
91
92     static struct tm date ; 
93
94         RTC_TIME_Type RTCFullTime;
95           RTC_GetFullTime (LPC_RTC, &RTCFullTime);
96
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 ;
103
104     #if defined(DEBUG_CYASSL) 
105     {
106                           extern void CYASSL_MSG(char *msg) ;
107         char msg[100] ;
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) ; 
111         CYASSL_MSG(msg) ;   
112     }
113     #endif
114     
115     return(&date) ;
116 }
117
118 typedef struct func_args {
119     int    argc;
120     char** argv;
121     int    return_code;
122 } func_args;
123
124 #include <stdio.h>
125
126 void time_main(void *args) 
127 {
128     char * datetime ;
129           int year ;
130         RTC_TIME_Type RTCFullTime;
131
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' ) {
140
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
157         ) ;
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") ; 
162 }
163
164
165
166