core_rtc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. void RTC_GlobalInit(void)
  23. {
  24. int8_t Buf[4][6];
  25. LongInt Tamp;
  26. char *strMon[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  27. int Day,Year,Mon,Hour,Min,Sec,i;
  28. CmdParam CP;
  29. Date_UserDataStruct BuildDate;
  30. Time_UserDataStruct BuildTime;
  31. if (!RTC->RTC_REF)
  32. {
  33. memset(&CP, 0, sizeof(CP));
  34. memset(Buf, 0, sizeof(Buf));
  35. CP.param_max_len = 6;
  36. CP.param_max_num = 4;
  37. CP.param_str = (int8_t *)Buf;
  38. CmdParseParam(__DATE__, &CP, ' ');
  39. Mon = 0;
  40. for (i = 0; i < 12; i++)
  41. {
  42. if (!strcmp(strMon[i], Buf[0]))
  43. {
  44. Mon = i + 1;
  45. }
  46. }
  47. if (Buf[1][0])
  48. {
  49. Day = strtol(Buf[1], NULL, 10);
  50. Year = strtol(Buf[2], NULL, 10);
  51. }
  52. else
  53. {
  54. Day = strtol(Buf[2], NULL, 10);
  55. Year = strtol(Buf[3], NULL, 10);
  56. }
  57. CP.param_num = 0;
  58. memset(Buf, 0, sizeof(Buf));
  59. CP.param_str = (int8_t *)Buf;
  60. CmdParseParam(__TIME__, &CP, ':');
  61. Hour = strtol(Buf[0], NULL, 10);
  62. Min = strtol(Buf[1], NULL, 10);
  63. Sec = strtol(Buf[2], NULL, 10);
  64. BuildDate.Year = Year;
  65. BuildDate.Mon = Mon;
  66. BuildDate.Day = Day;
  67. BuildTime.Hour = Hour;
  68. BuildTime.Min = Min;
  69. BuildTime.Sec = Sec;
  70. RTC_SetDateTime(&BuildDate, &BuildTime, 0);
  71. }
  72. // RTC_GetDateTime(&uBuildDate, &uBuildTime);
  73. // DBG("%04u-%02u-%02u %02u:%02u:%02u", uBuildDate.Date.Year, uBuildDate.Date.Mon,
  74. // uBuildDate.Date.Day, uBuildTime.Time.Hour, uBuildTime.Time.Min,
  75. // uBuildTime.Time.Sec);
  76. }
  77. void RTC_SetDateTime(Date_UserDataStruct *pDate, Time_UserDataStruct *pTime, uint8_t isForce)
  78. {
  79. uint64_t Tamp = UTC2Tamp(pDate, pTime);
  80. uint32_t curTime;
  81. uint32_t newTime = (uint32_t)Tamp;
  82. while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
  83. if (isForce)
  84. {
  85. RTC->RTC_CS |= RTC_CS_CLR;
  86. while (RTC->RTC_CS & RTC_CS_CLR) {;}
  87. RTC->RTC_REF = newTime;
  88. }
  89. else
  90. {
  91. RTC->RTC_CS |= RTC_CS_LOCK_TIM;
  92. curTime = RTC->RTC_TIM;
  93. RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
  94. curTime += RTC->RTC_REF;
  95. if (newTime > curTime)
  96. {
  97. RTC->RTC_CS |= RTC_CS_CLR;
  98. while (RTC->RTC_CS & RTC_CS_CLR) {;}
  99. RTC->RTC_REF = newTime;
  100. }
  101. }
  102. }
  103. void RTC_GetDateTime(Date_UserDataStruct *pDate, Time_UserDataStruct *pTime)
  104. {
  105. Tamp2UTC(RTC_GetUTC(), pDate, pTime, 0);
  106. }
  107. uint64_t RTC_GetUTC(void)
  108. {
  109. uint64_t curTime;
  110. while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
  111. RTC->RTC_CS |= RTC_CS_LOCK_TIM;
  112. curTime = RTC->RTC_TIM;
  113. RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
  114. curTime += RTC->RTC_REF;
  115. return curTime;
  116. }