app_datetime.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /**************************************************************
  29. *
  30. * Module Name: RTC module
  31. * File name: app_datetime.c
  32. * Brief description:
  33. * RCT function for nrf52 platform
  34. * Author: Eagle.Lao
  35. * Data: 2017-06-12
  36. * Revision:V0.01
  37. ****************************************************************/
  38. #include <stdlib.h>
  39. #include "OSAL.h"
  40. #include <string.h>
  41. #include <time.h>
  42. #include "app_err.h"
  43. #include "app_wrist.h"
  44. #include "app_datetime.h"
  45. #include "types.h"
  46. #include "clock.h"
  47. #include "log.h"
  48. #ifndef USE_SYS_TICK
  49. #define USE_SYS_TICK FASLE //default use RTC, if system use RC32K as RTC clock source, system tick is usually recommended
  50. #endif
  51. #if(USE_SYS_TICK)
  52. #define RTC_CNT_RANGE 0x100000000
  53. #define TM_RATE (1000000/625)
  54. #else
  55. #define RTC_CNT_RANGE 0x1000000
  56. #define TM_RATE (32768)
  57. #endif
  58. #define DT_INTV_SYNC 60 //sync interval every 60s
  59. //static app_dtm_hdl_t s_evt_hdl = NULL;
  60. static datetime_cfg_t s_stm_cfg = {0};
  61. static const char* const month_str[12] = {
  62. "Jan",
  63. "Feb",
  64. "Mar",
  65. "Apr",
  66. "May",
  67. "Jun",
  68. "Jul",
  69. "Aug",
  70. "Sep",
  71. "Oct",
  72. "Nov",
  73. "Dec"
  74. };
  75. static void app_datetime_adjust_baseline(struct tm* datetm);
  76. static void print_hex (uint8_t *data, uint16 len)
  77. {
  78. uint16 i;
  79. for (i = 0; i < len - 1; i++)
  80. {
  81. LOG("%x",data[i]);
  82. LOG(" ");
  83. }
  84. LOG("%x",data[i]);
  85. }
  86. static void dt_timer_start(uint32_t intval_ms)
  87. {
  88. osal_start_timerEx(AppWrist_TaskID, TIMER_DT_EVT, intval_ms);
  89. }
  90. void timer_cnt_get(uint32_t* tick)
  91. {
  92. #if(USE_SYS_TICK)
  93. *tick = hal_systick(); // read current RTC counter
  94. #else
  95. *tick = clock_time_rtc(); // read current RTC counter
  96. #endif
  97. }
  98. static void get_default_tm(struct tm* datetm)
  99. {
  100. int i;
  101. datetime_t dtm;
  102. char dt[20];
  103. char* pstr = NULL;
  104. LOG(__DATE__);
  105. LOG(__TIME__);
  106. strcpy(dt, __DATE__);
  107. pstr = strtok(dt, " ");
  108. for(i = 0; i< 12; i++){
  109. if(strcmp(dt,month_str[i]) == 0){
  110. dtm.month = i+1;
  111. break;
  112. }
  113. }
  114. pstr = strtok(NULL, " ");
  115. dtm.day = atoi(pstr);
  116. pstr = strtok(NULL, " ");
  117. dtm.year = atoi(pstr);
  118. strcpy(dt, __TIME__);
  119. pstr = strtok(dt, ":");
  120. dtm.hour = atoi(pstr);
  121. pstr = strtok(NULL, ":");
  122. dtm.minutes = atoi(pstr);
  123. pstr = strtok(NULL, ":");
  124. dtm.seconds = atoi(pstr);
  125. DTM2TM(datetm, &dtm);
  126. }
  127. static void check_default_datetime(void)
  128. {
  129. struct tm datetm;
  130. get_default_tm(&datetm);
  131. LOG("check_default_datetime\nTime is %d-%d-%d, %d:%d:%d",
  132. datetm.tm_year+1900,
  133. datetm.tm_mon+1,
  134. datetm.tm_mday,
  135. datetm.tm_hour,
  136. datetm.tm_min,
  137. datetm.tm_sec);
  138. app_datetime_adjust_baseline(&datetm);
  139. return;
  140. }
  141. static void datetime_print(void)
  142. {
  143. datetime_t dtm;
  144. app_datetime(&dtm);
  145. LOG("Time is %d-%d-%d, %d:%d:%d\n", dtm.year, dtm.month, dtm.day, dtm.hour, dtm.minutes, dtm.seconds);
  146. }
  147. void app_datetime_sync_handler(void )
  148. {
  149. uint32_t tick= 0;
  150. timer_cnt_get(&tick);
  151. if(tick < s_stm_cfg.snapshot)
  152. s_stm_cfg.tm_base += RTC_CNT_RANGE;
  153. s_stm_cfg.snapshot = tick;
  154. dt_timer_start(DATETIME_SYNC_INTERVAL);
  155. datetime_print();
  156. }
  157. static void app_datetime_adjust_baseline(struct tm* datetm)
  158. {
  159. uint64_t tmstamp;
  160. uint32_t ticks;
  161. timer_cnt_get(&ticks);
  162. tmstamp = (uint64_t)mktime(datetm);
  163. LOG("%x",(uint32_t)tmstamp);
  164. tmstamp = tmstamp*TM_RATE;
  165. s_stm_cfg.tm_base = tmstamp - ticks;
  166. s_stm_cfg.snapshot = ticks;
  167. LOG("app_datetime_adjust_baseline:\n");
  168. print_hex((uint8_t*)&s_stm_cfg, sizeof(s_stm_cfg));
  169. LOG("\n");
  170. }
  171. int app_datetime_set(datetime_t dtm)
  172. {
  173. struct tm tm;
  174. memset(&tm,0, sizeof(tm));
  175. DTM2TM(&tm, &dtm);
  176. LOG("\napp_datetime_set:\n");
  177. LOG("Time is %d-%d-%d, %d:%d:%d\n", dtm.year, dtm.month, dtm.day, dtm.hour, dtm.minutes, dtm.seconds);
  178. app_datetime_adjust_baseline(&tm);
  179. print_hex((uint8_t*)&s_stm_cfg, sizeof(s_stm_cfg));
  180. LOG("\n");
  181. return APP_SUCCESS;
  182. }
  183. int app_datetime_diff(const datetime_t* pdtm_base, const datetime_t* pdtm_cmp)
  184. {
  185. time_t time_base, time_cmp;
  186. struct tm tm_base, tm_cmp;
  187. DTM2TM(&tm_base, pdtm_base);
  188. DTM2TM(&tm_cmp, pdtm_cmp);
  189. time_base = mktime(&tm_base);
  190. time_cmp = mktime(&tm_cmp);
  191. return (int)difftime(time_base, time_cmp);
  192. }
  193. time_t app_datetime_time_t(void)
  194. {
  195. time_t tm = (time_t)(s_stm_cfg.tm_base / TM_RATE);
  196. return tm;
  197. }
  198. int app_datetime(datetime_t* pdtm)
  199. {
  200. struct tm * ptm = NULL;
  201. uint32_t ticks;
  202. time_t tm = (time_t)(s_stm_cfg.tm_base / TM_RATE);
  203. timer_cnt_get(&ticks);
  204. if(s_stm_cfg.snapshot > ticks)
  205. tm += RTC_CNT_RANGE/TM_RATE;
  206. tm += ticks/TM_RATE;
  207. ptm = localtime(&tm);
  208. TM2DTM(pdtm, ptm)
  209. return APP_SUCCESS;
  210. }
  211. void app_datetime_init(void)//app_dtm_hdl_t evt_hdl)
  212. {
  213. //s_evt_hdl = evt_hdl;
  214. check_default_datetime();
  215. dt_timer_start(DATETIME_SYNC_INTERVAL);
  216. }