core_rtc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_SetUTC(uint32_t Tamp, uint8_t isForce)
  98. {
  99. uint32_t curTime;
  100. uint32_t newTime = Tamp;
  101. while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
  102. if (isForce)
  103. {
  104. RTC->RTC_CS |= RTC_CS_CLR;
  105. while (RTC->RTC_CS & RTC_CS_CLR) {;}
  106. RTC->RTC_REF = newTime;
  107. }
  108. else
  109. {
  110. RTC->RTC_CS |= RTC_CS_LOCK_TIM;
  111. curTime = RTC->RTC_TIM;
  112. RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
  113. curTime += RTC->RTC_REF;
  114. if (newTime > curTime)
  115. {
  116. RTC->RTC_CS |= RTC_CS_CLR;
  117. while (RTC->RTC_CS & RTC_CS_CLR) {;}
  118. RTC->RTC_REF = newTime;
  119. }
  120. }
  121. }
  122. void RTC_SetDateTime(Date_UserDataStruct *pDate, Time_UserDataStruct *pTime, uint8_t isForce)
  123. {
  124. RTC_SetUTC((uint32_t)UTC2Tamp(pDate, pTime), isForce);
  125. }
  126. void RTC_GetDateTime(Date_UserDataStruct *pDate, Time_UserDataStruct *pTime)
  127. {
  128. Tamp2UTC(RTC_GetUTC(), pDate, pTime, 0);
  129. }
  130. uint64_t RTC_GetUTC(void)
  131. {
  132. uint64_t curTime;
  133. while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
  134. RTC->RTC_CS |= RTC_CS_LOCK_TIM;
  135. curTime = RTC->RTC_TIM;
  136. RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
  137. curTime += RTC->RTC_REF;
  138. return curTime;
  139. }
  140. void RTC_SetAlarm(uint32_t TimeSecond, CBFuncEx_t CB, void *pParam)
  141. {
  142. while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
  143. RTC->RTC_INTCLR = 1;
  144. RTC->RTC_CS &= ~RTC_CS_ALARM_EN;
  145. RTC->RTC_CS |= RTC_CS_LOCK_TIM;
  146. RTC->RTC_ARM = RTC->RTC_TIM + TimeSecond;
  147. RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
  148. if (CB)
  149. {
  150. prvRTCCB = CB;
  151. }
  152. else
  153. {
  154. prvRTCCB = RTC_DummyCB;
  155. }
  156. prvParam = pParam;
  157. RTC->RTC_CS |= RTC_CS_ALARM_EN;
  158. ISR_OnOff(RTC_IRQn, 1);
  159. }
  160. #ifdef __BUILD_APP__
  161. INIT_HW_EXPORT(RTC_GlobalInit, "1");
  162. #endif