1
0

drv_rtc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, int 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. #endif
  198. /***************************************************************************//**
  199. * @}
  200. ******************************************************************************/