drv_wktm.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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)
  26. #define PWC_WKT_CLK_SRC (PWC_WKT_CLK_SRC_RTCLRC)
  27. #elif defined(HC32F460) || defined(HC32F448)
  28. #define PWC_WKT_CLK_SRC (PWC_WKT_CLK_SRC_LRC)
  29. #endif
  30. #define PWC_WKT_COUNT_FRQ (32768UL)
  31. #endif
  32. static rt_uint32_t cmpval = CMPVAL_MAX;
  33. /**
  34. * This function get current count value of WKTM
  35. * @param None
  36. * @return the count value
  37. */
  38. rt_uint32_t hc32_wktm_get_current_tick(void)
  39. {
  40. return (CMPVAL_MAX);
  41. }
  42. /**
  43. * This function get the max value that WKTM can count
  44. * @param None
  45. * @return the max count
  46. */
  47. rt_uint32_t hc32_wktm_get_tick_max(void)
  48. {
  49. return (CMPVAL_MAX);
  50. }
  51. /**
  52. * This function start WKTM with reload value
  53. * @param reload The value that Comparison value of the Counter
  54. * @return RT_EOK
  55. */
  56. rt_err_t hc32_wktm_start(rt_uint32_t reload)
  57. {
  58. /* 64HZ must use XTAL32 and run RTC */
  59. #if defined(BSP_USING_WKTM_64HZ)
  60. #if defined(BSP_RTC_USING_XTAL32)
  61. if (DISABLE == RTC_GetCounterState())
  62. {
  63. /* #error "Please start the RTC!" */
  64. RT_ASSERT(0);
  65. }
  66. #else
  67. #error "Please enable XTAL32 and start the RTC!"
  68. #endif
  69. #endif
  70. if (reload > CMPVAL_MAX || !reload)
  71. {
  72. return -RT_ERROR;
  73. }
  74. cmpval = reload;
  75. PWC_WKT_SetCompareValue(cmpval);
  76. PWC_WKT_Cmd(ENABLE);
  77. return RT_EOK;
  78. }
  79. /**
  80. * @brief This function stop WKTM
  81. * @param None
  82. * @retval None
  83. */
  84. void hc32_wktm_stop(void)
  85. {
  86. PWC_WKT_Cmd(DISABLE);
  87. }
  88. /**
  89. * This function get the count clock of WKTM
  90. * @param None
  91. * @return the count clock frequency in Hz
  92. */
  93. rt_uint32_t hc32_wktm_get_countfreq(void)
  94. {
  95. return PWC_WKT_COUNT_FRQ;
  96. }
  97. /**
  98. * @brief This function initialize the wktm
  99. * @param None
  100. * @retval type code
  101. */
  102. int rt_hw_wktm_init(void)
  103. {
  104. rt_err_t ret = RT_EOK;
  105. /* Disable WKTM in advance */
  106. PWC_WKT_Cmd(DISABLE);
  107. /* WKTM init */
  108. PWC_WKT_Config(PWC_WKT_CLK_SRC, CMPVAL_MAX);
  109. #if defined(HC32F4A0)
  110. /* F4A0 if select RTCLRC clock need open the LRCEN by RTC->CR3 register */
  111. #if (PWC_WKT_CLK_SRC == PWC_WKT_CLK_SRC_RTCLRC)
  112. MODIFY_REG8(CM_RTC->CR3, RTC_CR3_LRCEN, 0x01U << RTC_CR3_LRCEN_POS);
  113. #endif
  114. #endif
  115. return ret;
  116. }
  117. INIT_DEVICE_EXPORT(rt_hw_wktm_init);
  118. #endif
  119. #endif /* RT_USING_PM */