core_rtc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. static int32_t RTC_DummyCB(void *pData, void *pParam)
  23. {
  24. DBG("!");
  25. return 0;
  26. }
  27. static CBFuncEx_t prvRTCCB;
  28. static void *prvParam;
  29. static void RTC_IrqHandler(int32_t Line, void *pData)
  30. {
  31. RTC->RTC_INTCLR = 1;
  32. ISR_OnOff(RTC_IRQn, 0);
  33. prvRTCCB(pData, prvParam);
  34. }
  35. void RTC_GlobalInit(void)
  36. {
  37. int8_t Buf[4][6];
  38. LongInt Tamp;
  39. char *strMon[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  40. int Day,Year,Mon,Hour,Min,Sec,i;
  41. CmdParam CP;
  42. Date_UserDataStruct BuildDate;
  43. Time_UserDataStruct BuildTime;
  44. prvRTCCB = RTC_DummyCB;
  45. if (!RTC->RTC_REF)
  46. {
  47. memset(&CP, 0, sizeof(CP));
  48. memset(Buf, 0, sizeof(Buf));
  49. CP.param_max_len = 6;
  50. CP.param_max_num = 4;
  51. CP.param_str = (int8_t *)Buf;
  52. CmdParseParam(__DATE__, &CP, ' ');
  53. Mon = 0;
  54. for (i = 0; i < 12; i++)
  55. {
  56. if (!strcmp(strMon[i], Buf[0]))
  57. {
  58. Mon = i + 1;
  59. }
  60. }
  61. if (Buf[1][0])
  62. {
  63. Day = strtol(Buf[1], NULL, 10);
  64. Year = strtol(Buf[2], NULL, 10);
  65. }
  66. else
  67. {
  68. Day = strtol(Buf[2], NULL, 10);
  69. Year = strtol(Buf[3], NULL, 10);
  70. }
  71. CP.param_num = 0;
  72. memset(Buf, 0, sizeof(Buf));
  73. CP.param_str = (int8_t *)Buf;
  74. CmdParseParam(__TIME__, &CP, ':');
  75. Hour = strtol(Buf[0], NULL, 10);
  76. Min = strtol(Buf[1], NULL, 10);
  77. Sec = strtol(Buf[2], NULL, 10);
  78. BuildDate.Year = Year;
  79. BuildDate.Mon = Mon;
  80. BuildDate.Day = Day;
  81. BuildTime.Hour = Hour;
  82. BuildTime.Min = Min;
  83. BuildTime.Sec = Sec;
  84. RTC_SetDateTime(&BuildDate, &BuildTime, 0);
  85. }
  86. #ifdef __BUILD_OS__
  87. ISR_SetPriority(RTC_IRQn, IRQ_MAX_PRIORITY + 1);
  88. #else
  89. ISR_SetPriority(RTC_IRQn, 3);
  90. #endif
  91. ISR_SetHandler(RTC_IRQn, RTC_IrqHandler, NULL);
  92. // RTC_GetDateTime(&uBuildDate, &uBuildTime);
  93. // DBG("%04u-%02u-%02u %02u:%02u:%02u", uBuildDate.Date.Year, uBuildDate.Date.Mon,
  94. // uBuildDate.Date.Day, uBuildTime.Time.Hour, uBuildTime.Time.Min,
  95. // uBuildTime.Time.Sec);
  96. }
  97. void RTC_SetDateTime(Date_UserDataStruct *pDate, Time_UserDataStruct *pTime, uint8_t isForce)
  98. {
  99. uint64_t Tamp = UTC2Tamp(pDate, pTime);
  100. uint32_t curTime;
  101. uint32_t newTime = (uint32_t)Tamp;
  102. while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
  103. if (isForce)
  104. {
  105. RTC->RTC_CS |= RTC_CS_CLR;
  106. while (RTC->RTC_CS & RTC_CS_CLR) {;}
  107. RTC->RTC_REF = newTime;
  108. }
  109. else
  110. {
  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. if (newTime > curTime)
  116. {
  117. RTC->RTC_CS |= RTC_CS_CLR;
  118. while (RTC->RTC_CS & RTC_CS_CLR) {;}
  119. RTC->RTC_REF = newTime;
  120. }
  121. }
  122. }
  123. void RTC_GetDateTime(Date_UserDataStruct *pDate, Time_UserDataStruct *pTime)
  124. {
  125. Tamp2UTC(RTC_GetUTC(), pDate, pTime, 0);
  126. }
  127. uint64_t RTC_GetUTC(void)
  128. {
  129. uint64_t curTime;
  130. while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
  131. RTC->RTC_CS |= RTC_CS_LOCK_TIM;
  132. curTime = RTC->RTC_TIM;
  133. RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
  134. curTime += RTC->RTC_REF;
  135. return curTime;
  136. }
  137. void RTC_SetAlarm(uint32_t TimeSecond, CBFuncEx_t CB, void *pParam)
  138. {
  139. while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
  140. RTC->RTC_INTCLR = 1;
  141. RTC->RTC_CS &= ~RTC_CS_ALARM_EN;
  142. RTC->RTC_CS |= RTC_CS_LOCK_TIM;
  143. RTC->RTC_ARM = RTC->RTC_TIM + TimeSecond;
  144. RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
  145. if (CB)
  146. {
  147. prvRTCCB = CB;
  148. }
  149. else
  150. {
  151. prvRTCCB = RTC_DummyCB;
  152. }
  153. prvParam = pParam;
  154. RTC->RTC_CS |= RTC_CS_ALARM_EN;
  155. ISR_OnOff(RTC_IRQn, 1);
  156. }
  157. #ifdef __BUILD_APP__
  158. INIT_HW_EXPORT(RTC_GlobalInit, "1");
  159. #endif