drv_rtc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. if (reset & RMU_RSTCAUSE_PORST || reset & RMU_RSTCAUSE_EXTRST) //TODO
  157. {
  158. RTC_Init_TypeDef rtcInit;
  159. efm32_irq_hook_init_t hook;
  160. rtcInit.enable = true;
  161. rtcInit.debugRun = false;
  162. rtcInit.comp0Top = false;
  163. rtc_time = 0UL;
  164. rt_kprintf("rtc is not configured\n");
  165. rt_kprintf("please configure with set_date and set_time\n");
  166. /* Configuring clocks in the Clock Management Unit (CMU) */
  167. startLfxoForRtc();
  168. /* Initialize and enable RTC */
  169. RTC_Init(&rtcInit);
  170. hook.type = efm32_irq_type_rtc;
  171. hook.unit = 0;
  172. hook.cbFunc = rt_hw_rtc_isr;
  173. hook.userPtr = RT_NULL;
  174. efm32_irq_hook_register(&hook);
  175. /* Enabling Interrupt from RTC */
  176. RTC_IntEnable(RTC_IFC_OF);
  177. RTC_IntClear(RTC_IFC_OF);
  178. NVIC_ClearPendingIRQ(RTC_IRQn);
  179. NVIC_SetPriority(RTC_IRQn, EFM32_IRQ_PRI_DEFAULT);
  180. NVIC_EnableIRQ(RTC_IRQn);
  181. }
  182. /* register rtc device */
  183. rt_hw_rtc_register(&rtc, RT_RTC_NAME, EFM32_NO_DATA);
  184. }
  185. /******************************************************************//**
  186. * @brief
  187. * Start LFXO for RTC
  188. *
  189. * @details
  190. * Starts the LFXO and routes it to the RTC. RTC clock is prescaled to save energy.
  191. *
  192. * @note
  193. *********************************************************************/
  194. static void startLfxoForRtc(void)
  195. {
  196. /* Starting LFXO and waiting until it is stable */
  197. CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
  198. /* Routing the LFXO clock to the RTC */
  199. CMU_ClockDivSet(cmuClock_RTC,cmuClkDiv_32768);
  200. CMU_ClockSelectSet(cmuClock_LFA,cmuSelect_LFXO);
  201. CMU_ClockEnable(cmuClock_RTC, true);
  202. }
  203. /*********************************************************************
  204. * Export to FINSH
  205. *********************************************************************/
  206. #ifdef RT_USING_FINSH
  207. #include <finsh.h>
  208. #include <time.h>
  209. time_t time(time_t* t)
  210. {
  211. rt_device_t device;
  212. time_t time;
  213. device = rt_device_find("rtc");
  214. if (device != RT_NULL)
  215. {
  216. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  217. if (t != RT_NULL) *t = time;
  218. }
  219. return time;
  220. }
  221. void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  222. {
  223. time_t now;
  224. struct tm* ti;
  225. rt_device_t device;
  226. ti = RT_NULL;
  227. /* get current time */
  228. time(&now);
  229. ti = localtime(&now);
  230. if (ti != RT_NULL)
  231. {
  232. ti->tm_year = year - 1900;
  233. ti->tm_mon = month - 1; /* ti->tm_mon = month; */
  234. ti->tm_mday = day;
  235. }
  236. now = mktime(ti);
  237. device = rt_device_find("rtc");
  238. if (device != RT_NULL)
  239. {
  240. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  241. }
  242. }
  243. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  244. void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  245. {
  246. time_t now;
  247. struct tm* ti;
  248. rt_device_t device;
  249. ti = RT_NULL;
  250. /* get current time */
  251. time(&now);
  252. ti = localtime(&now);
  253. if (ti != RT_NULL)
  254. {
  255. ti->tm_hour = hour;
  256. ti->tm_min = minute;
  257. ti->tm_sec = second;
  258. }
  259. now = mktime(ti);
  260. device = rt_device_find("rtc");
  261. if (device != RT_NULL)
  262. {
  263. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  264. }
  265. }
  266. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  267. void list_date()
  268. {
  269. time_t now;
  270. time(&now);
  271. rt_kprintf("%s\n", ctime(&now));
  272. }
  273. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  274. #endif
  275. /******************************************************************//**
  276. * @}
  277. *********************************************************************/