drv_rtc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-01-05 Bernard the first version
  9. * 2010-12-27 onelife Modify for EFM32
  10. * 2011-06-16 onelife Modify init function for efm32lib v2 upgrading
  11. * 2011-12-14 onelife Move LFXO enabling routine to driver initialization function (board.c)
  12. */
  13. /***************************************************************************//**
  14. * @addtogroup efm32
  15. * @{
  16. ******************************************************************************/
  17. /* Includes ------------------------------------------------------------------*/
  18. #include <rtdevice.h>
  19. #include "board.h"
  20. #include "hdl_interrupt.h"
  21. #include "drv_rtc.h"
  22. #if defined(RT_USING_RTC)
  23. /* Private typedef -----------------------------------------------------------*/
  24. /* Private define ------------------------------------------------------------*/
  25. /* Private macro -------------------------------------------------------------*/
  26. #ifdef RT_RTC_DEBUG
  27. #define rtc_debug(format,args...) rt_kprintf(format, ##args)
  28. #else
  29. #define rtc_debug(format,args...)
  30. #endif
  31. /* Private variables ---------------------------------------------------------*/
  32. static struct rt_device rtc;
  33. static rt_uint32_t rtc_time;
  34. /* Private function prototypes -----------------------------------------------*/
  35. /* Private functions ---------------------------------------------------------*/
  36. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  37. {
  38. if (dev->rx_indicate != RT_NULL)
  39. {
  40. /* Open Interrupt */
  41. }
  42. return RT_EOK;
  43. }
  44. static rt_ssize_t rt_rtc_read(
  45. rt_device_t dev,
  46. rt_off_t pos,
  47. void* buffer,
  48. rt_size_t size)
  49. {
  50. return 0;
  51. }
  52. /***************************************************************************//**
  53. * @brief
  54. * Configure RTC device
  55. *
  56. * @details
  57. *
  58. * @note
  59. *
  60. * @param[in] dev
  61. * Pointer to device descriptor
  62. *
  63. * @param[in] cmd
  64. * RTC control command
  65. *
  66. * @param[in] args
  67. * Arguments
  68. *
  69. * @return
  70. * Error code
  71. ******************************************************************************/
  72. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  73. {
  74. RT_ASSERT(dev != RT_NULL);
  75. switch (cmd)
  76. {
  77. case RT_DEVICE_CTRL_RTC_GET_TIME:
  78. *(rt_uint32_t *)args = rtc_time + RTC_CounterGet();
  79. rtc_debug("RTC: get rtc_time %x + %x\n", rtc_time, RTC_CounterGet());
  80. break;
  81. case RT_DEVICE_CTRL_RTC_SET_TIME:
  82. {
  83. rtc_time = *(rt_uint32_t *)args;
  84. rtc_debug("RTC: set rtc_time %x\n", rtc_time);
  85. /* Reset counter */
  86. RTC_CounterReset();
  87. }
  88. break;
  89. }
  90. return RT_EOK;
  91. }
  92. /***************************************************************************//**
  93. * @brief
  94. * RTC counter overflow interrupt handler
  95. *
  96. * @details
  97. *
  98. * @note
  99. ******************************************************************************/
  100. void rt_hw_rtc_isr(rt_device_t device)
  101. {
  102. if (RTC->IF & RTC_IFC_OF)
  103. {
  104. rtc_time += _RTC_CNT_MASK;
  105. }
  106. RTC->IFC = _RTC_IFC_MASK;
  107. }
  108. /***************************************************************************//**
  109. * @brief
  110. * Initialize all RTC module related hardware and register RTC device to kernel
  111. *
  112. * @details
  113. *
  114. * @note
  115. ******************************************************************************/
  116. void rt_hw_rtc_init(void)
  117. {
  118. rt_uint32_t reset;
  119. reset = RMU_ResetCauseGet();
  120. // TODO: What is the current reset mode?
  121. if (reset & RMU_RSTCAUSE_PORST || reset & RMU_RSTCAUSE_EXTRST)
  122. {
  123. RTC_Init_TypeDef rtcInit;
  124. efm32_irq_hook_init_t hook;
  125. rtcInit.enable = true;
  126. rtcInit.debugRun = false;
  127. rtcInit.comp0Top = false;
  128. rtc_time = 0UL;
  129. rt_kprintf("rtc is not configured\n");
  130. rt_kprintf("please configure with set_date and set_time\n");
  131. /* Configuring clock */
  132. CMU_ClockDivSet(cmuClock_RTC,cmuClkDiv_32768);
  133. CMU_ClockEnable(cmuClock_RTC, true);
  134. /* Initialize and enable RTC */
  135. RTC_Reset();
  136. RTC_Init(&rtcInit);
  137. hook.type = efm32_irq_type_rtc;
  138. hook.unit = 0;
  139. hook.cbFunc = rt_hw_rtc_isr;
  140. hook.userPtr = RT_NULL;
  141. efm32_irq_hook_register(&hook);
  142. /* Enabling Interrupt from RTC */
  143. RTC_IntEnable(RTC_IFC_OF);
  144. RTC_IntClear(RTC_IFC_OF);
  145. NVIC_ClearPendingIRQ(RTC_IRQn);
  146. NVIC_SetPriority(RTC_IRQn, EFM32_IRQ_PRI_DEFAULT);
  147. NVIC_EnableIRQ(RTC_IRQn);
  148. }
  149. /* register rtc device */
  150. rtc.type = RT_Device_Class_RTC;
  151. rtc.rx_indicate = RT_NULL;
  152. rtc.tx_complete = RT_NULL;
  153. rtc.init = RT_NULL;
  154. rtc.open = rt_rtc_open;
  155. rtc.close = RT_NULL;
  156. rtc.read = rt_rtc_read;
  157. rtc.write = RT_NULL;
  158. rtc.control = rt_rtc_control;
  159. rtc.user_data = RT_NULL; /* no private */
  160. rt_device_register(&rtc, RT_RTC_NAME, RT_DEVICE_FLAG_RDWR | EFM32_NO_DATA);
  161. }
  162. #endif
  163. /***************************************************************************//**
  164. * @}
  165. ******************************************************************************/