rtc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include "rtc.h"
  2. #include "lwip/apps/sntp.h"
  3. #include "settings_api.h"
  4. #include <string.h>
  5. //TM_RTC_t calendar;
  6. /* Days in a month */
  7. uint8_t TM_RTC_Months[2][12] = {
  8. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, /* Not leap year */
  9. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} /* Leap year */
  10. };
  11. // monthly correction data sheet
  12. const uint8_t table_week[12] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
  13. // monmonth data table of common year
  14. const uint8_t mon_table[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  15. /* Internal RTC defines */
  16. #define TM_RTC_LEAP_YEAR(year) ((((year) % 4 == 0) && ((year) % 100 != 0)) || ((year) % 400 == 0))
  17. #define TM_RTC_DAYS_IN_YEAR(x) TM_RTC_LEAP_YEAR(x) ? 366 : 365
  18. #define TM_RTC_OFFSET_YEAR 1970
  19. #define TM_RTC_SECONDS_PER_DAY 86400
  20. #define TM_RTC_SECONDS_PER_HOUR 3600
  21. #define TM_RTC_SECONDS_PER_MINUTE 60
  22. #define TM_RTC_BCD2BIN(x) ((((x) >> 4) & 0x0F) * 10 + ((x) & 0x0F))
  23. #define TM_RTC_CHAR2NUM(x) ((x) - '0')
  24. #define TM_RTC_CHARISNUM(x) ((x) >= '0' && (x) <= '9')
  25. extern SemaphoreHandle_t flash_mutex;
  26. /**
  27. * @brief rtc peripheral initialization.
  28. * @param calendar
  29. * @retval 0: rtc already init
  30. 1: rtc init
  31. */
  32. uint8_t TM_RTC_Init(void)
  33. {
  34. TM_RTC_t datatime;
  35. // enable pwc and bpr clocks
  36. crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
  37. crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
  38. // enable the battery-powered domain write operations
  39. pwc_battery_powered_domain_access(TRUE);
  40. // check if rtc is initialized
  41. if (bpr_data_read(BPR_DATA4) != 0x1234)
  42. {
  43. // reset battery-powered domain register
  44. bpr_reset();
  45. // enable the lext osc
  46. crm_clock_source_enable(CRM_CLOCK_SOURCE_LEXT, TRUE);
  47. // wait lext is ready
  48. while(crm_flag_get(CRM_LEXT_STABLE_FLAG) == RESET);
  49. // select the rtc clock source
  50. crm_rtc_clock_select(CRM_RTC_CLOCK_LEXT);
  51. // enable rtc clock
  52. crm_rtc_clock_enable(TRUE);
  53. // wait for rtc registers update
  54. rtc_wait_update_finish();
  55. // wait for the register write to complete
  56. rtc_wait_config_finish();
  57. // enable the rtc second
  58. nvic_irq_enable(RTC_IRQn, 6, 0);
  59. rtc_interrupt_enable(RTC_TS_INT, TRUE);
  60. // set rtc divider: set rtc period to 1sec
  61. rtc_divider_set(32767);
  62. // wait for the register write to complete
  63. rtc_wait_config_finish();
  64. // set date and time
  65. datatime.date = 1;
  66. datatime.day = 1;
  67. datatime.month = 1;
  68. datatime.year = 0;
  69. datatime.hours = 0;
  70. datatime.minutes = 0;
  71. datatime.seconds = 0;
  72. TM_RTC_SetDateTime(&datatime);
  73. // writes data to bpr register
  74. bpr_data_write(BPR_DATA4, 0x1234);
  75. return 1;
  76. }
  77. else
  78. {
  79. // wait for rtc registers update
  80. rtc_wait_update_finish();
  81. // wait for the register write to complete
  82. rtc_wait_config_finish();
  83. // enable the rtc second
  84. nvic_irq_enable(RTC_IRQn, 6, 0);
  85. rtc_interrupt_enable(RTC_TS_INT, TRUE);
  86. return 0;
  87. }
  88. }
  89. //
  90. void TM_RTC_SetDataTimeUnix(uint32_t unixTime)
  91. {
  92. TM_RTC_t data;
  93. TM_RTC_GetDateTimeFromUnix(&data, unixTime);
  94. rtc_counter_set(unixTime);
  95. rtc_wait_config_finish();
  96. }
  97. /**
  98. * @brief set time. convert the input clock to a second.
  99. * the time basic : 1970.1.1
  100. * legitimate year: 1970 ~ 2099
  101. * @param calendar
  102. * @retval 0: set time right.
  103. * 1: set time failed.
  104. */
  105. TM_RTC_Result_t TM_RTC_SetDateTime(TM_RTC_t* data)
  106. {
  107. uint32_t seccount = 0;
  108. if (data->year > 99 ||
  109. data->month == 0 ||
  110. data->month > 12 ||
  111. data->date == 0 ||
  112. data->date > TM_RTC_Months[TM_RTC_LEAP_YEAR(2000 + data->year) ? 1 : 0][data->month - 1] ||
  113. data->hours > 23 ||
  114. data->minutes > 59 ||
  115. data->seconds > 59 ||
  116. data->day == 0 ||
  117. data->day > 7)
  118. {
  119. return TM_RTC_Result_Error;
  120. }
  121. // enable pwc and bpr clocks
  122. crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
  123. crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
  124. // enable write access to bpr domain
  125. pwc_battery_powered_domain_access(TRUE);
  126. // set the rtc counter value
  127. seccount = TM_RTC_GetUnixTimeStamp(data);
  128. rtc_counter_set(seccount);
  129. // wait for the register write to complete
  130. rtc_wait_config_finish();
  131. return TM_RTC_Result_Ok;
  132. }
  133. //
  134. TM_RTC_Result_t TM_RTC_SetDateTimeString(char* str)
  135. {
  136. TM_RTC_t tmp;
  137. uint8_t i = 0;
  138. // Get date
  139. tmp.date = 0;
  140. while (TM_RTC_CHARISNUM(*(str + i))) {
  141. tmp.date = tmp.date * 10 + TM_RTC_CHAR2NUM(*(str + i));
  142. i++;
  143. }
  144. i++;
  145. // Get month
  146. tmp.month = 0;
  147. while (TM_RTC_CHARISNUM(*(str + i))) {
  148. tmp.month = tmp.month * 10 + TM_RTC_CHAR2NUM(*(str + i));
  149. i++;
  150. }
  151. i++;
  152. // Get year
  153. tmp.year = 0;
  154. while (TM_RTC_CHARISNUM(*(str + i))) {
  155. tmp.year = tmp.year * 10 + TM_RTC_CHAR2NUM(*(str + i));
  156. i++;
  157. }
  158. i++;
  159. // Get day in a week
  160. tmp.day = 0;
  161. while (TM_RTC_CHARISNUM(*(str + i))) {
  162. tmp.day = tmp.day * 10 + TM_RTC_CHAR2NUM(*(str + i));
  163. i++;
  164. }
  165. i++;
  166. // Get hours
  167. tmp.hours = 0;
  168. while (TM_RTC_CHARISNUM(*(str + i))) {
  169. tmp.hours = tmp.hours * 10 + TM_RTC_CHAR2NUM(*(str + i));
  170. i++;
  171. }
  172. i++;
  173. // Get minutes
  174. tmp.minutes = 0;
  175. while (TM_RTC_CHARISNUM(*(str + i))) {
  176. tmp.minutes = tmp.minutes * 10 + TM_RTC_CHAR2NUM(*(str + i));
  177. i++;
  178. }
  179. i++;
  180. // Get seconds
  181. tmp.seconds = 0;
  182. while (TM_RTC_CHARISNUM(*(str + i))) {
  183. tmp.seconds = tmp.seconds * 10 + TM_RTC_CHAR2NUM(*(str + i));
  184. i++;
  185. }
  186. i++;
  187. // Return status from set date time function
  188. return TM_RTC_SetDateTime(&tmp);
  189. }
  190. //
  191. void TM_RTC_GetDateTime(TM_RTC_t* data, TM_RTC_Format_t format)
  192. {
  193. (void)format;
  194. uint32_t unix = rtc_counter_get();
  195. TM_RTC_GetDateTimeFromUnix(data, unix);
  196. }
  197. //
  198. uint32_t TM_RTC_GetUnixTimeStamp(TM_RTC_t* data)
  199. {
  200. uint32_t days = 0, seconds = 0;
  201. uint16_t i;
  202. uint16_t year = (uint16_t)(data->year + 2000);
  203. // Year is below offset year
  204. if (year < TM_RTC_OFFSET_YEAR) {
  205. return 0;
  206. }
  207. // Days in back years
  208. for (i = TM_RTC_OFFSET_YEAR; i < year; i++) {
  209. days += TM_RTC_DAYS_IN_YEAR(i);
  210. }
  211. // Days in current year
  212. for (i = 1; i < data->month; i++) {
  213. days += TM_RTC_Months[TM_RTC_LEAP_YEAR(year)][i - 1];
  214. }
  215. // Day starts with 1
  216. days += data->date - 1;
  217. seconds = days * TM_RTC_SECONDS_PER_DAY;
  218. seconds += data->hours * TM_RTC_SECONDS_PER_HOUR;
  219. seconds += data->minutes * TM_RTC_SECONDS_PER_MINUTE;
  220. seconds += data->seconds;
  221. // seconds = days * 86400;
  222. return seconds;
  223. }
  224. //
  225. void TM_RTC_GetDateTimeFromUnix(TM_RTC_t* data, uint32_t unix)
  226. {
  227. uint16_t year;
  228. // Store unix time to unix in struct
  229. data->unix = unix;
  230. // Get seconds from unix
  231. data->seconds = unix % 60;
  232. // Go to minutes
  233. unix /= 60;
  234. // Get minutes
  235. data->minutes = unix % 60;
  236. // Go to hours
  237. unix /= 60;
  238. // Get hours
  239. data->hours = unix % 24;
  240. // Go to days
  241. unix /= 24;
  242. // Get week day
  243. // Monday is day one
  244. data->day = (unix + 3) % 7 + 1;
  245. // Get year
  246. year = 1970;
  247. while (1) {
  248. if (TM_RTC_LEAP_YEAR(year)) {
  249. if (unix >= 366) {
  250. unix -= 366;
  251. } else {
  252. break;
  253. }
  254. } else if (unix >= 365) {
  255. unix -= 365;
  256. } else {
  257. break;
  258. }
  259. year++;
  260. }
  261. // Get year in xx format
  262. data->year = (uint8_t) (year - 2000);
  263. // Get month
  264. for (data->month = 0; data->month < 12; data->month++) {
  265. if (TM_RTC_LEAP_YEAR(year) && unix >= (uint32_t)TM_RTC_Months[1][data->month]) {
  266. unix -= TM_RTC_Months[1][data->month];
  267. } else if (unix >= (uint32_t)TM_RTC_Months[0][data->month]) {
  268. unix -= TM_RTC_Months[0][data->month];
  269. } else {
  270. break;
  271. }
  272. }
  273. // Get month
  274. // Month starts with 1
  275. data->month++;
  276. // Get date
  277. // Date starts with 1
  278. data->date = unix + 1;
  279. }
  280. //
  281. void TM_RTC_PrintTime(void)
  282. {
  283. TM_RTC_t data;
  284. uint32_t unix = rtc_counter_get();
  285. TM_RTC_GetDateTimeFromUnix(&data, unix);
  286. printf("%02d.%02d.%02d %02d:%02d:%02d\r\n", data.date, data.month, data.year,
  287. data.hours, data.minutes, data.seconds);
  288. }
  289. //
  290. uint32_t RTC_GetUnixTime(void)
  291. {
  292. TM_RTC_t currentTime;
  293. TM_RTC_GetDateTime(&currentTime, TM_RTC_Format_BIN);
  294. return TM_RTC_GetUnixTimeStamp(&currentTime);
  295. }
  296. //
  297. void rtc_subtim_init(void)
  298. {
  299. crm_clocks_freq_type crm_clocks_freq_struct = {0};
  300. crm_periph_clock_enable(CRM_TMR5_PERIPH_CLOCK, TRUE);
  301. crm_clocks_freq_get(&crm_clocks_freq_struct);
  302. tmr_base_init(TMR5, 9990, 24000 - 1);
  303. tmr_cnt_dir_set(TMR5, TMR_COUNT_UP);
  304. tmr_flag_clear(TMR5, TMR_OVF_FLAG);
  305. NVIC_ClearPendingIRQ(TMR5_GLOBAL_IRQn);
  306. nvic_irq_enable(TMR5_GLOBAL_IRQn, 5, 0);
  307. tmr_counter_enable(TMR5, TRUE);
  308. tmr_interrupt_enable(TMR5, TMR_OVF_INT, TRUE);
  309. }
  310. //
  311. uint64_t rtc_get_ms(void)
  312. {
  313. return ((uint64_t)RTC_GetUnixTime()*1000 + TMR5->cval/10);
  314. }
  315. //
  316. uint32_t rtc_foo(void)
  317. {
  318. return tmr_counter_value_get(TMR5);
  319. }
  320. //
  321. void TMR5_GLOBAL_IRQHandler(void)
  322. {
  323. if (tmr_flag_get(TMR5, TMR_OVF_FLAG) != RESET)
  324. {
  325. tmr_flag_clear(TMR5, TMR_OVF_FLAG);
  326. tmr_interrupt_enable(TMR5, TMR_OVF_INT, FALSE);
  327. tmr_counter_enable(TMR5, FALSE);
  328. }
  329. }
  330. //
  331. void RTC_IRQHandler(void)
  332. {
  333. if (rtc_flag_get(RTC_TS_FLAG) != RESET)
  334. {
  335. rtc_flag_clear(RTC_TS_FLAG);
  336. tmr_interrupt_enable(TMR5, TMR_OVF_INT, TRUE);
  337. tmr_counter_enable(TMR5, TRUE);
  338. TMR5->cval = 0;
  339. }
  340. }