drv_rtc.c 8.1 KB

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