time-LCP43xx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* time.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL 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. * wolfSSL 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-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. /*-----------------------------------------------------------------------------
  25. * initialize RTC
  26. *----------------------------------------------------------------------------*/
  27. #include <stdio.h>
  28. #include "lpc43xx_rtc.h"
  29. #include "lpc43xx_cgu.h"
  30. static void init_RTC()
  31. {
  32. /* Enable GPIO register interface clock */
  33. LPC_CCU1->CLK_M4_GPIO_CFG |= 1;
  34. while (!(LPC_CCU1->CLK_M4_GPIO_STAT & 1)) ;
  35. /* RTC Block section ------------------------------------------------------ */
  36. /* Init RTC module */
  37. RTC_Init(LPC_RTC);
  38. /* Set ALARM time for second */
  39. RTC_SetAlarmTime (LPC_RTC, RTC_TIMETYPE_SECOND, 30);
  40. /* Set the AMR for 30s match alarm interrupt */
  41. RTC_AlarmIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE);
  42. /* Set the CIIR for minute counter interrupt*/
  43. RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_MINUTE, ENABLE);
  44. /* Enable rtc (starts increase the tick counter and second counter register) */
  45. RTC_Cmd(LPC_RTC, ENABLE);
  46. }
  47. /*-----------------------------------------------------------------------------
  48. * initialize TIM
  49. *----------------------------------------------------------------------------*/
  50. #include "lpc43xx_timer.h"
  51. static void init_TIM()
  52. {
  53. TIM_TIMERCFG_Type TIM_ConfigStruct;
  54. /* Initialize timer 0, prescale count time of 1uS */
  55. TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_TICKVAL;
  56. TIM_ConfigStruct.PrescaleValue = 204; /* 204MHz */
  57. /* Set configuration for Tim_config and Tim_MatchConfig */
  58. TIM_Init(LPC_TIMER2, TIM_TIMER_MODE,&TIM_ConfigStruct);
  59. TIM_ResetCounter(LPC_TIMER2);
  60. /* To start timer 2 */
  61. TIM_Cmd(LPC_TIMER2,ENABLE);
  62. }
  63. double current_time()
  64. {
  65. return (double)LPC_TIMER2->TC/1000000.0;
  66. }
  67. void init_time(void) {
  68. init_RTC() ;
  69. init_TIM() ;
  70. }
  71. #include <time.h>
  72. struct tm *Cyassl_MDK_gmtime(const time_t *c)
  73. {
  74. static struct tm date ;
  75. RTC_TIME_Type RTCFullTime;
  76. RTC_GetFullTime (LPC_RTC, &RTCFullTime);
  77. date.tm_year = RTCFullTime.YEAR + 100 ;
  78. date.tm_mon = RTCFullTime.MONTH - 1 ;
  79. date.tm_mday = RTCFullTime.DOM ;
  80. date.tm_hour = RTCFullTime.HOUR ;
  81. date.tm_min = RTCFullTime.MIN ;
  82. date.tm_sec = RTCFullTime.SEC ;
  83. #if defined(DEBUG_CYASSL)
  84. {
  85. extern void CYASSL_MSG(char *msg) ;
  86. char msg[100] ;
  87. sprintf(msg, "Debug::Cyassl_KEIL_gmtime(DATE=/%4d/%02d/%02d TIME=%02d:%02d:%02d)\n",
  88. RTCFullTime.YEAR+2000, RTCFullTime.MONTH, RTCFullTime.DOM,
  89. RTCFullTime.HOUR, RTCFullTime.MIN, RTCFullTime.SEC) ;
  90. CYASSL_MSG(msg) ;
  91. }
  92. #endif
  93. return(&date) ;
  94. }
  95. typedef struct func_args {
  96. int argc;
  97. char** argv;
  98. int return_code;
  99. } func_args;
  100. #include <stdio.h>
  101. void time_main(void *args)
  102. {
  103. char * datetime ;
  104. int year ;
  105. RTC_TIME_Type RTCFullTime;
  106. if( args == NULL || ((func_args *)args)->argc == 1) {
  107. RTC_GetFullTime (LPC_RTC, &RTCFullTime);
  108. printf("Date: %d/%d/%d, Time: %02d:%02d:%02d\n",
  109. RTCFullTime.MONTH, RTCFullTime.DOM, RTCFullTime.YEAR+2000,
  110. RTCFullTime.HOUR, RTCFullTime.MIN, RTCFullTime.SEC) ;
  111. } else if(((func_args *)args)->argc == 3 &&
  112. ((func_args *)args)->argv[1][0] == '-' &&
  113. ((func_args *)args)->argv[1][1] == 'd' ) {
  114. datetime = ((func_args *)args)->argv[2];
  115. sscanf(datetime, "%d/%d/%d",
  116. (int *)&RTCFullTime.MONTH, (int *)&RTCFullTime.DOM, &year) ;
  117. RTCFullTime.YEAR = year - 2000 ;
  118. RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MONTH, RTCFullTime.MONTH);
  119. RTC_SetTime (LPC_RTC, RTC_TIMETYPE_YEAR, RTCFullTime.YEAR);
  120. RTC_SetTime (LPC_RTC, RTC_TIMETYPE_DAYOFMONTH, RTCFullTime.DOM);
  121. } else if(((func_args *)args)->argc == 3 &&
  122. ((func_args *)args)->argv[1][0] == '-' &&
  123. ((func_args *)args)->argv[1][1] == 't' ) {
  124. RTC_GetFullTime (LPC_RTC, &RTCFullTime);
  125. datetime = ((func_args *)args)->argv[2];
  126. sscanf(datetime, "%d:%d:%d",
  127. (int *)&RTCFullTime.HOUR,
  128. (int *)&RTCFullTime.MIN,
  129. (int *)&RTCFullTime.SEC
  130. ) ;
  131. RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, RTCFullTime.SEC);
  132. RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MINUTE, RTCFullTime.MIN);
  133. RTC_SetTime (LPC_RTC, RTC_TIMETYPE_HOUR, RTCFullTime.HOUR);
  134. } else printf("Invalid argument\n") ;
  135. }