drv_rtc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /***************************************************************************//**
  2. * @file drv_rtc.c
  3. * @brief RTC driver of RT-Thread RTOS for EFM32
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. * @author Bernard, onelife
  6. * @version 1.0
  7. *******************************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file
  10. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  11. *******************************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2009-01-05 Bernard the first version
  15. * 2010-12-27 onelife Modify for EFM32
  16. * 2011-06-16 onelife Modify init function for efm32lib v2 upgrading
  17. * 2011-12-14 onelife Move LFXO enabling routine to driver initialization
  18. * function (board.c)
  19. ******************************************************************************/
  20. /***************************************************************************//**
  21. * @addtogroup efm32
  22. * @{
  23. ******************************************************************************/
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "board.h"
  26. #include "hdl_interrupt.h"
  27. #include "drv_rtc.h"
  28. #if defined(RT_USING_RTC)
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* Private define ------------------------------------------------------------*/
  31. /* Private macro -------------------------------------------------------------*/
  32. #ifdef RT_RTC_DEBUG
  33. #define rtc_debug(format,args...) rt_kprintf(format, ##args)
  34. #else
  35. #define rtc_debug(format,args...)
  36. #endif
  37. /* Private variables ---------------------------------------------------------*/
  38. static struct rt_device rtc;
  39. static rt_uint32_t rtc_time;
  40. /* Private function prototypes -----------------------------------------------*/
  41. /* Private functions ---------------------------------------------------------*/
  42. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  43. {
  44. if (dev->rx_indicate != RT_NULL)
  45. {
  46. /* Open Interrupt */
  47. }
  48. return RT_EOK;
  49. }
  50. static rt_size_t rt_rtc_read(
  51. rt_device_t dev,
  52. rt_off_t pos,
  53. void* buffer,
  54. rt_size_t size)
  55. {
  56. return 0;
  57. }
  58. /***************************************************************************//**
  59. * @brief
  60. * Configure RTC device
  61. *
  62. * @details
  63. *
  64. * @note
  65. *
  66. * @param[in] dev
  67. * Pointer to device descriptor
  68. *
  69. * @param[in] cmd
  70. * RTC control command
  71. *
  72. * @param[in] args
  73. * Arguments
  74. *
  75. * @return
  76. * Error code
  77. ******************************************************************************/
  78. static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  79. {
  80. RT_ASSERT(dev != RT_NULL);
  81. switch (cmd)
  82. {
  83. case RT_DEVICE_CTRL_RTC_GET_TIME:
  84. *(rt_uint32_t *)args = rtc_time + RTC_CounterGet();
  85. rtc_debug("RTC: get rtc_time %x + %x\n", rtc_time, RTC_CounterGet());
  86. break;
  87. case RT_DEVICE_CTRL_RTC_SET_TIME:
  88. {
  89. rtc_time = *(rt_uint32_t *)args;
  90. rtc_debug("RTC: set rtc_time %x\n", rtc_time);
  91. /* Reset counter */
  92. RTC_CounterReset();
  93. }
  94. break;
  95. }
  96. return RT_EOK;
  97. }
  98. /***************************************************************************//**
  99. * @brief
  100. * RTC counter overflow interrupt handler
  101. *
  102. * @details
  103. *
  104. * @note
  105. ******************************************************************************/
  106. void rt_hw_rtc_isr(rt_device_t device)
  107. {
  108. if (RTC->IF & RTC_IFC_OF)
  109. {
  110. rtc_time += _RTC_CNT_MASK;
  111. }
  112. RTC->IFC = _RTC_IFC_MASK;
  113. }
  114. /***************************************************************************//**
  115. * @brief
  116. * Register RTC device
  117. *
  118. * @details
  119. *
  120. * @note
  121. *
  122. * @param[in] device
  123. * Pointer to device descriptor
  124. *
  125. * @param[in] name
  126. * Device name
  127. *
  128. * @param[in] flag
  129. * Configuration flags
  130. *
  131. * @return
  132. * Error code
  133. ******************************************************************************/
  134. rt_err_t rt_hw_rtc_register(
  135. rt_device_t device,
  136. const char *name,
  137. rt_uint32_t flag)
  138. {
  139. RT_ASSERT(device != RT_NULL);
  140. device->type = RT_Device_Class_RTC;
  141. device->rx_indicate = RT_NULL;
  142. device->tx_complete = RT_NULL;
  143. device->init = RT_NULL;
  144. device->open = rt_rtc_open;
  145. device->close = RT_NULL;
  146. device->read = rt_rtc_read;
  147. device->write = RT_NULL;
  148. device->control = rt_rtc_control;
  149. device->user_data = RT_NULL; /* no private */
  150. /* register a character device */
  151. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  152. }
  153. /***************************************************************************//**
  154. * @brief
  155. * Initialize all RTC module related hardware and register RTC device to kernel
  156. *
  157. * @details
  158. *
  159. * @note
  160. ******************************************************************************/
  161. void rt_hw_rtc_init(void)
  162. {
  163. rt_uint32_t reset;
  164. reset = RMU_ResetCauseGet();
  165. // TODO: What is the current reset mode?
  166. if (reset & RMU_RSTCAUSE_PORST || reset & RMU_RSTCAUSE_EXTRST)
  167. {
  168. RTC_Init_TypeDef rtcInit;
  169. efm32_irq_hook_init_t hook;
  170. rtcInit.enable = true;
  171. rtcInit.debugRun = false;
  172. rtcInit.comp0Top = false;
  173. rtc_time = 0UL;
  174. rt_kprintf("rtc is not configured\n");
  175. rt_kprintf("please configure with set_date and set_time\n");
  176. /* Configuring clock */
  177. CMU_ClockDivSet(cmuClock_RTC,cmuClkDiv_32768);
  178. CMU_ClockEnable(cmuClock_RTC, true);
  179. /* Initialize and enable RTC */
  180. RTC_Reset();
  181. RTC_Init(&rtcInit);
  182. hook.type = efm32_irq_type_rtc;
  183. hook.unit = 0;
  184. hook.cbFunc = rt_hw_rtc_isr;
  185. hook.userPtr = RT_NULL;
  186. efm32_irq_hook_register(&hook);
  187. /* Enabling Interrupt from RTC */
  188. RTC_IntEnable(RTC_IFC_OF);
  189. RTC_IntClear(RTC_IFC_OF);
  190. NVIC_ClearPendingIRQ(RTC_IRQn);
  191. NVIC_SetPriority(RTC_IRQn, EFM32_IRQ_PRI_DEFAULT);
  192. NVIC_EnableIRQ(RTC_IRQn);
  193. }
  194. /* register rtc device */
  195. rt_hw_rtc_register(&rtc, RT_RTC_NAME, EFM32_NO_DATA);
  196. }
  197. /***************************************************************************//**
  198. * Export to FINSH
  199. ******************************************************************************/
  200. #ifdef RT_USING_FINSH
  201. #include <finsh.h>
  202. #include <time.h>
  203. time_t time(time_t* t)
  204. {
  205. rt_device_t device;
  206. time_t time;
  207. device = rt_device_find("rtc");
  208. if (device != RT_NULL)
  209. {
  210. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  211. if (t != RT_NULL) *t = time;
  212. }
  213. return time;
  214. }
  215. void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  216. {
  217. time_t now;
  218. struct tm* ti;
  219. rt_device_t device;
  220. ti = RT_NULL;
  221. /* get current time */
  222. time(&now);
  223. ti = localtime(&now);
  224. if (ti != RT_NULL)
  225. {
  226. ti->tm_year = year - 1900;
  227. ti->tm_mon = month - 1; /* ti->tm_mon = month; */
  228. ti->tm_mday = day;
  229. }
  230. now = mktime(ti);
  231. device = rt_device_find("rtc");
  232. if (device != RT_NULL)
  233. {
  234. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  235. }
  236. }
  237. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  238. void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  239. {
  240. time_t now;
  241. struct tm* ti;
  242. rt_device_t device;
  243. ti = RT_NULL;
  244. /* get current time */
  245. time(&now);
  246. rtc_debug("RTC: Now %x\n", now);
  247. ti = localtime(&now);
  248. if (ti != RT_NULL)
  249. {
  250. ti->tm_hour = hour;
  251. ti->tm_min = minute;
  252. ti->tm_sec = second;
  253. }
  254. rtc_debug("RTC: DST %d\n", ti->tm_isdst);
  255. rtc_debug("RTC: Days in year %d\n", ti->tm_yday);
  256. rtc_debug("RTC: Day of week %d\n", ti->tm_wday);
  257. rtc_debug("RTC: Year %d\n", ti->tm_year);
  258. rtc_debug("RTC: Month %d\n", ti->tm_mon);
  259. rtc_debug("RTC: Day %d\n", ti->tm_mday);
  260. rtc_debug("RTC: Hour %d\n", ti->tm_hour);
  261. rtc_debug("RTC: Min %d\n", ti->tm_min);
  262. rtc_debug("RTC: Sec %d\n", ti->tm_sec);
  263. now = mktime(ti);
  264. rtc_debug("RTC: Now %x\n", now);
  265. device = rt_device_find("rtc");
  266. if (device != RT_NULL)
  267. {
  268. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  269. }
  270. }
  271. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  272. void list_date()
  273. {
  274. time_t now;
  275. time(&now);
  276. rt_kprintf("%s\n", ctime(&now));
  277. }
  278. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  279. #endif
  280. #endif
  281. /***************************************************************************//**
  282. * @}
  283. ******************************************************************************/