drv_wktm.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-09 CDT first version
  9. */
  10. #include <board.h>
  11. #include <drv_wktm.h>
  12. #if defined(RT_USING_PM)
  13. #if defined(BSP_USING_PM)
  14. // #define DRV_DEBUG
  15. #define LOG_TAG "drv_wktm"
  16. #include <drv_log.h>
  17. #define CMPVAL_MAX (0xFFFUL)
  18. #if defined(BSP_USING_WKTM_XTAL32)
  19. #define PWC_WKT_CLK_SRC (PWC_WKT_CLK_SRC_XTAL32)
  20. #define PWC_WKT_COUNT_FRQ (32768UL)
  21. #elif defined(BSP_USING_WKTM_64HZ)
  22. #define PWC_WKT_CLK_SRC (PWC_WKT_CLK_SRC_64HZ)
  23. #define PWC_WKT_COUNT_FRQ (64U)
  24. #else
  25. #if defined(HC32F4A0) || defined(HC32F4A8)
  26. #define PWC_WKT_CLK_SRC (PWC_WKT_CLK_SRC_RTCLRC)
  27. #elif defined(HC32F460) || defined(HC32F448) || defined(HC32F472) || defined(HC32F334)
  28. #define PWC_WKT_CLK_SRC (PWC_WKT_CLK_SRC_LRC)
  29. #endif
  30. #define PWC_WKT_COUNT_FRQ (32768UL)
  31. #endif
  32. /**
  33. * This function get timeout count value of WKTM
  34. * @param None
  35. * @return the count value
  36. */
  37. rt_uint32_t hc32_wktm_get_timeout_tick(void)
  38. {
  39. return (RT_TICK_PER_SECOND * PWC_WKT_GetCompareValue() / PWC_WKT_COUNT_FRQ);
  40. }
  41. /**
  42. * This function get the max value that WKTM can count
  43. * @param None
  44. * @return the max count
  45. */
  46. rt_uint32_t hc32_wktm_get_tick_max(void)
  47. {
  48. return (CMPVAL_MAX);
  49. }
  50. /**
  51. * This function start WKTM with reload value
  52. * @param reload The value that Comparison value of the Counter
  53. * @return RT_EOK
  54. */
  55. rt_err_t hc32_wktm_start(rt_uint32_t reload)
  56. {
  57. /* 64HZ must use XTAL32 and run RTC */
  58. #if defined(BSP_USING_WKTM_64HZ)
  59. #if defined(BSP_RTC_USING_XTAL32)
  60. if (DISABLE == RTC_GetCounterState())
  61. {
  62. /* #error "Please start the RTC!" */
  63. RT_ASSERT(0);
  64. }
  65. #else
  66. #error "Please enable XTAL32 and start the RTC!"
  67. #endif
  68. #endif
  69. if (reload > CMPVAL_MAX || !reload)
  70. {
  71. return -RT_ERROR;
  72. }
  73. PWC_WKT_SetCompareValue(reload);
  74. PWC_WKT_Cmd(ENABLE);
  75. return RT_EOK;
  76. }
  77. /**
  78. * @brief This function stop WKTM
  79. * @param None
  80. * @retval None
  81. */
  82. void hc32_wktm_stop(void)
  83. {
  84. PWC_WKT_Cmd(DISABLE);
  85. }
  86. /**
  87. * This function get the count clock of WKTM
  88. * @param None
  89. * @return the count clock frequency in Hz
  90. */
  91. rt_uint32_t hc32_wktm_get_countfreq(void)
  92. {
  93. return PWC_WKT_COUNT_FRQ;
  94. }
  95. /**
  96. * @brief This function initialize the wktm
  97. * @param None
  98. * @retval type code
  99. */
  100. int rt_hw_wktm_init(void)
  101. {
  102. rt_err_t ret = RT_EOK;
  103. /* Disable WKTM in advance */
  104. PWC_WKT_Cmd(DISABLE);
  105. /* WKTM init */
  106. PWC_WKT_Config(PWC_WKT_CLK_SRC, CMPVAL_MAX);
  107. #if defined(HC32F4A0) || defined(HC32F4A8)
  108. /* F4A0 if select RTCLRC clock need open the LRCEN by RTC->CR3 register */
  109. #if (PWC_WKT_CLK_SRC == PWC_WKT_CLK_SRC_RTCLRC)
  110. MODIFY_REG8(CM_RTC->CR3, RTC_CR3_LRCEN, 0x01U << RTC_CR3_LRCEN_POS);
  111. #endif
  112. #endif
  113. return ret;
  114. }
  115. INIT_DEVICE_EXPORT(rt_hw_wktm_init);
  116. #endif
  117. #endif /* RT_USING_PM */