drv_rtc.c 8.9 KB

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