drv_pm.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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-06-12 CDT first version
  9. */
  10. #ifndef __DRV_PM_H__
  11. #define __DRV_PM_H__
  12. /*******************************************************************************
  13. * Include files
  14. ******************************************************************************/
  15. #include <drv_config.h>
  16. /* C binding of definitions if building with C++ compiler */
  17. #ifdef __cplusplus
  18. extern "C"
  19. {
  20. #endif
  21. /*******************************************************************************
  22. * @defgroup sleep_mode_map
  23. * @brief The mapping of rtt pm sleep mode to hc32 low power mode
  24. * --------------------------------------------------
  25. * rtt pm sleep mode | hc32 low power mode
  26. * ------------------|-------------------------------
  27. * idle | sleep
  28. * deep | stop
  29. * standby | power down mode 1 or 2, selection @ref PM_SLEEP_STANDBY_CFG
  30. * shutdown mode | power down mode 3 or 4, selection @ref PM_SLEEP_SHUTDOWN_CFG
  31. ******************************************************************************/
  32. /*******************************************************************************
  33. * Global type definitions ('typedef')
  34. ******************************************************************************/
  35. typedef void (* run_mode_init_func)(uint8_t run_mode);
  36. /**
  37. * @brief run mode config @ref PM_RUN_MODE_CFG
  38. */
  39. struct pm_run_mode_config
  40. {
  41. run_mode_init_func sys_clk_cfg;
  42. };
  43. /**
  44. * @brief sleep idle config @ref PM_SLEEP_IDLE_CFG
  45. */
  46. struct pm_sleep_mode_idle_config
  47. {
  48. uint8_t pwc_sleep_type; /*!< WFI/WFE selection and SEVONPEND configuration
  49. @ref PWC_Sleep_Type */
  50. };
  51. /**
  52. * @brief sleep deep config @ref PM_SLEEP_DEEP_CFG
  53. */
  54. struct pm_sleep_mode_deep_config
  55. {
  56. stc_pwc_stop_mode_config_t cfg;
  57. uint8_t pwc_stop_type; /*!< WFI/WFE selection and SEVONPEND configuration
  58. @ref PWC_Stop_Type */
  59. };
  60. /**
  61. * @brief sleep standby config @ref PM_SLEEP_STANDBY_CFG
  62. */
  63. struct pm_sleep_mode_standby_config
  64. {
  65. stc_pwc_pd_mode_config_t cfg; /*!< cfg.u8Mode could only be PWC_PD_MD1 or PWC_PD_MD2,
  66. @ref PWC_PDMode_Sel, @ref sleep_mode_map */
  67. };
  68. /**
  69. * @brief sleep shutdown config @ref PM_SLEEP_SHUTDOWN_CFG
  70. */
  71. struct pm_sleep_mode_shutdown_config
  72. {
  73. stc_pwc_pd_mode_config_t cfg; /*!< cfg.u8Mode could only be PWC_PD_MD3 or PWC_PD_MD4,
  74. @ref PWC_PDMode_Sel, @ref sleep_mode_map */
  75. };
  76. /*******************************************************************************
  77. * Global pre-processor symbols/macros ('#define')
  78. ******************************************************************************/
  79. #if defined(HC32F4A0) || defined(HC32F4A8)
  80. #define PM_CHECK_EFM() ((EFM_GetStatus(EFM_FLAG_RDY) == SET) && (EFM_GetStatus(EFM_FLAG_RDY1) == SET))
  81. #elif defined(HC32F460) || defined (HC32F448) || defined (HC32F472) || defined (HC32F334)
  82. #define PM_CHECK_EFM() ((EFM_GetStatus(EFM_FLAG_RDY) == SET))
  83. #endif
  84. #define PM_CHECK_XTAL() ((CM_CMU->XTALSTDCR & CLK_XTALSTD_ON) == 0)
  85. #if defined(HC32F334)
  86. #define PM_CHECK_DMA() (DMA_GetTransStatus(CM_DMA, DMA_STAT_TRANS_DMA) == RESET)
  87. #elif defined(HC32F4A0) || defined(HC32F4A8) || defined(HC32F460) || defined (HC32F448) || defined (HC32F472)
  88. #define PM_CHECK_DMA() \
  89. ( (DMA_GetTransStatus(CM_DMA1, DMA_STAT_TRANS_DMA) == RESET) && \
  90. (DMA_GetTransStatus(CM_DMA2, DMA_STAT_TRANS_DMA) == RESET))
  91. #endif
  92. #define PM_CHECK_SWDT() \
  93. ( ((CM_ICG->ICG0 & ICG_SWDT_RST_START) != ICG_SWDT_RST_START) || \
  94. ((CM_ICG->ICG0 & ICG_SWDT_LPM_CNT_STOP) == ICG_SWDT_LPM_CNT_STOP))
  95. #define PM_CHECK_PVD() \
  96. ( ((CM_PWC->PVDCR1 & (PWC_PVDCR1_PVD1IRE | PWC_PVDCR1_PVD1IRS)) != (PWC_PVDCR1_PVD1IRE | PWC_PVDCR1_PVD1IRS)) && \
  97. ((CM_PWC->PVDCR1 & (PWC_PVDCR1_PVD2IRE | PWC_PVDCR1_PVD2IRS)) != (PWC_PVDCR1_PVD2IRE | PWC_PVDCR1_PVD2IRS)))
  98. #define PM_SLEEP_SHUTDOWN_CHECK() \
  99. ( PM_CHECK_EFM() && \
  100. PM_CHECK_XTAL() && \
  101. PM_CHECK_SWDT() && \
  102. PM_CHECK_PVD())
  103. #define PM_SLEEP_DEEP_CHECK() \
  104. ( PM_CHECK_EFM() && \
  105. PM_CHECK_XTAL() && \
  106. PM_CHECK_DMA())
  107. /*
  108. * please make sure the state of the peripherals meet the requirements of entering the specified sleep mode,
  109. * otherwise system may not entering the right sleep mode or something unexpected may happen.
  110. * PM_SLEEP_CHECK is a demo of requirements and may not be comprehensive,
  111. * please refer user manual to know all the requirements in detail.
  112. */
  113. #define PM_SLEEP_CHECK(mode) \
  114. ( (mode == PM_SLEEP_MODE_STANDBY && PM_SLEEP_SHUTDOWN_CHECK()) || \
  115. ( (mode == PM_SLEEP_MODE_SHUTDOWN && PM_SLEEP_SHUTDOWN_CHECK()) || \
  116. (mode == PM_SLEEP_MODE_DEEP && PM_SLEEP_DEEP_CHECK())|| \
  117. (mode <= PM_SLEEP_MODE_IDLE)))
  118. /*******************************************************************************
  119. * Global function prototypes (definition in C source)
  120. ******************************************************************************/
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif /* __DRV_PM_H__ */
  125. /*******************************************************************************
  126. * EOF (not truncated)
  127. ******************************************************************************/