drv_pm.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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)
  80. #define PM_CHECK_EFM() ((EFM_GetStatus(EFM_FLAG_RDY) == SET) && (EFM_GetStatus(EFM_FLAG_RDY1) == SET))
  81. #elif defined(HC32F460)
  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. #define PM_CHECK_DMA() \
  86. ( (DMA_GetTransStatus(CM_DMA1, DMA_STAT_TRANS_DMA) == RESET) && \
  87. (DMA_GetTransStatus(CM_DMA2, DMA_STAT_TRANS_DMA) == RESET))
  88. #define PM_CHECK_SWDT() \
  89. ( ((CM_ICG->ICG0 & ICG_SWDT_RST_START) != ICG_SWDT_RST_START) || \
  90. ((CM_ICG->ICG0 & ICG_SWDT_LPM_CNT_STOP) == ICG_SWDT_LPM_CNT_STOP))
  91. #define PM_CHECK_PVD() \
  92. ( ((CM_PWC->PVDCR1 & (PWC_PVDCR1_PVD1IRE | PWC_PVDCR1_PVD1IRS)) != (PWC_PVDCR1_PVD1IRE | PWC_PVDCR1_PVD1IRS)) && \
  93. ((CM_PWC->PVDCR1 & (PWC_PVDCR1_PVD2IRE | PWC_PVDCR1_PVD2IRS)) != (PWC_PVDCR1_PVD2IRE | PWC_PVDCR1_PVD2IRS)))
  94. #define PM_SLEEP_SHUTDOWN_CHECK() \
  95. ( PM_CHECK_EFM() && \
  96. PM_CHECK_XTAL() && \
  97. PM_CHECK_SWDT() && \
  98. PM_CHECK_PVD())
  99. #define PM_SLEEP_DEEP_CHECK() \
  100. ( PM_CHECK_EFM() && \
  101. PM_CHECK_XTAL() && \
  102. PM_CHECK_DMA())
  103. /*
  104. * please make sure the state of the peripherals meet the requirements of entering the specified sleep mode,
  105. * otherwise system may not entering the right sleep mode or something unexpected may happen.
  106. * PM_SLEEP_CHECK is a demo of requirements and may not be comprehensive,
  107. * please refer user manual to know all the requirements in detail.
  108. */
  109. #define PM_SLEEP_CHECK(mode) \
  110. ( (mode == PM_SLEEP_MODE_STANDBY && PM_SLEEP_SHUTDOWN_CHECK()) || \
  111. ( (mode == PM_SLEEP_MODE_SHUTDOWN && PM_SLEEP_SHUTDOWN_CHECK()) || \
  112. (mode == PM_SLEEP_MODE_DEEP && PM_SLEEP_DEEP_CHECK())|| \
  113. (mode <= PM_SLEEP_MODE_IDLE)))
  114. /**
  115. * @defgroup PWC_Sleep_Type PWC sleep mode type.
  116. * @{
  117. */
  118. #ifndef PWC_SLEEP_WFI
  119. #define PWC_SLEEP_WFI (0x00U) /*!< Enter sleep mode by WFI, and wake-up by interrupt handle. */
  120. #endif
  121. #ifndef PWC_SLEEP_WFE_INT
  122. #define PWC_SLEEP_WFE_INT (0x01U) /*!< Enter sleep mode by WFE, and wake-up by interrupt request(SEVONPEND=1). */
  123. #endif
  124. #ifndef PWC_SLEEP_WFE_EVT
  125. #define PWC_SLEEP_WFE_EVT (0x02U) /*!< Enter sleep mode by WFE, and wake-up by event(SEVONPEND=0). */
  126. #endif
  127. /**
  128. * @}
  129. */
  130. /*******************************************************************************
  131. * Global function prototypes (definition in C source)
  132. ******************************************************************************/
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* __DRV_PM_H__ */
  137. /*******************************************************************************
  138. * EOF (not truncated)
  139. ******************************************************************************/