pm.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-06-02 Bernard the first version
  9. * 2018-08-02 Tanek split run and sleep modes, support custom mode
  10. * 2019-04-28 Zero-Free improve PM mode and device ops interface
  11. * 2020-11-23 zhangsz update pm mode select
  12. * 2020-11-27 zhangsz update pm 2.0
  13. */
  14. #ifndef __PM_H__
  15. #define __PM_H__
  16. #include <stdint.h>
  17. #include <rtthread.h>
  18. #ifndef PM_HAS_CUSTOM_CONFIG
  19. /* All modes used for rt_pm_request() and rt_pm_release() */
  20. enum
  21. {
  22. /* sleep modes */
  23. PM_SLEEP_MODE_NONE = 0,
  24. PM_SLEEP_MODE_IDLE,
  25. PM_SLEEP_MODE_LIGHT,
  26. PM_SLEEP_MODE_DEEP,
  27. PM_SLEEP_MODE_STANDBY,
  28. PM_SLEEP_MODE_SHUTDOWN,
  29. PM_SLEEP_MODE_MAX,
  30. };
  31. enum
  32. {
  33. /* run modes*/
  34. PM_RUN_MODE_HIGH_SPEED = 0,
  35. PM_RUN_MODE_NORMAL_SPEED,
  36. PM_RUN_MODE_MEDIUM_SPEED,
  37. PM_RUN_MODE_LOW_SPEED,
  38. PM_RUN_MODE_MAX,
  39. };
  40. enum
  41. {
  42. RT_PM_FREQUENCY_PENDING = 0x01,
  43. };
  44. #define RT_PM_DEFAULT_SLEEP_MODE PM_SLEEP_MODE_NONE
  45. #define RT_PM_DEFAULT_DEEPSLEEP_MODE PM_SLEEP_MODE_DEEP
  46. #define RT_PM_DEFAULT_RUN_MODE PM_RUN_MODE_NORMAL_SPEED
  47. /* The name of all modes used in the msh command "pm_dump" */
  48. #define PM_SLEEP_MODE_NAMES \
  49. { \
  50. "None Mode", \
  51. "Idle Mode", \
  52. "LightSleep Mode", \
  53. "DeepSleep Mode", \
  54. "Standby Mode", \
  55. "Shutdown Mode", \
  56. }
  57. #define PM_RUN_MODE_NAMES \
  58. { \
  59. "High Speed", \
  60. "Normal Speed", \
  61. "Medium Speed", \
  62. "Low Mode", \
  63. }
  64. /**
  65. * Modules used for
  66. * pm_module_request(PM_BOARD_ID, PM_SLEEP_MODE_IDLE)
  67. * pm_module_release(PM_BOARD_ID, PM_SLEEP_MODE_IDLE)
  68. * pm_module_release_all(PM_BOARD_ID, PM_SLEEP_MODE_IDLE)
  69. */
  70. enum pm_module_id {
  71. PM_NONE_ID = 0,
  72. PM_POWER_ID,
  73. PM_BOARD_ID,
  74. PM_BSP_ID,
  75. PM_MAIN_ID,
  76. PM_PMS_ID,
  77. PM_PMC_ID,
  78. PM_TASK_ID,
  79. PM_SPI_ID,
  80. PM_I2C_ID,
  81. PM_UART_ID,
  82. PM_CAN_ID,
  83. PM_ETH_ID,
  84. PM_SENSOR_ID,
  85. PM_LCD_ID,
  86. PM_KEY_ID,
  87. PM_TP_ID,
  88. PM_MODULE_MAX_ID, /* enum must! */
  89. };
  90. #else /* PM_HAS_CUSTOM_CONFIG */
  91. #include <pm_cfg.h>
  92. #endif /* PM_HAS_CUSTOM_CONFIG */
  93. /**
  94. * device control flag to request or release power
  95. */
  96. #define RT_PM_DEVICE_CTRL_REQUEST 0x01
  97. #define RT_PM_DEVICE_CTRL_RELEASE 0x00
  98. struct rt_pm;
  99. /**
  100. * low power mode operations
  101. */
  102. struct rt_pm_ops
  103. {
  104. void (*sleep)(struct rt_pm *pm, rt_uint8_t mode);
  105. void (*run)(struct rt_pm *pm, rt_uint8_t mode);
  106. void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout);
  107. void (*timer_stop)(struct rt_pm *pm);
  108. rt_tick_t (*timer_get_tick)(struct rt_pm *pm);
  109. };
  110. struct rt_device_pm_ops
  111. {
  112. int (*suspend)(const struct rt_device *device, rt_uint8_t mode);
  113. void (*resume)(const struct rt_device *device, rt_uint8_t mode);
  114. int (*frequency_change)(const struct rt_device *device, rt_uint8_t mode);
  115. };
  116. struct rt_device_pm
  117. {
  118. const struct rt_device *device;
  119. const struct rt_device_pm_ops *ops;
  120. };
  121. struct rt_pm_module
  122. {
  123. rt_uint8_t req_status;
  124. rt_bool_t busy_flag;
  125. rt_uint32_t timeout;
  126. rt_uint32_t start_time;
  127. };
  128. /**
  129. * power management
  130. */
  131. struct rt_pm
  132. {
  133. struct rt_device parent;
  134. /* modes */
  135. rt_uint8_t modes[PM_SLEEP_MODE_MAX];
  136. rt_uint8_t sleep_mode; /* current sleep mode */
  137. rt_uint8_t run_mode; /* current running mode */
  138. /* modules request status*/
  139. struct rt_pm_module module_status[PM_MODULE_MAX_ID];
  140. /* the list of device, which has PM feature */
  141. rt_uint8_t device_pm_number;
  142. struct rt_device_pm *device_pm;
  143. /* if the mode has timer, the corresponding bit is 1*/
  144. rt_uint8_t timer_mask;
  145. rt_uint8_t flags;
  146. const struct rt_pm_ops *ops;
  147. };
  148. enum
  149. {
  150. RT_PM_ENTER_SLEEP = 0,
  151. RT_PM_EXIT_SLEEP,
  152. };
  153. struct rt_pm_notify
  154. {
  155. void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data);
  156. void *data;
  157. };
  158. void rt_pm_request(rt_uint8_t sleep_mode);
  159. void rt_pm_release(rt_uint8_t sleep_mode);
  160. void rt_pm_release_all(rt_uint8_t sleep_mode);
  161. int rt_pm_run_enter(rt_uint8_t run_mode);
  162. void rt_pm_device_register(struct rt_device *device, const struct rt_device_pm_ops *ops);
  163. void rt_pm_device_unregister(struct rt_device *device);
  164. void rt_pm_notify_set(void (*notify)(rt_uint8_t event, rt_uint8_t mode, void *data), void *data);
  165. void rt_pm_default_set(rt_uint8_t sleep_mode);
  166. void rt_system_pm_init(const struct rt_pm_ops *ops,
  167. rt_uint8_t timer_mask,
  168. void *user_data);
  169. void rt_pm_module_request(uint8_t module_id, rt_uint8_t sleep_mode);
  170. void rt_pm_module_release(uint8_t module_id, rt_uint8_t sleep_mode);
  171. void rt_pm_module_release_all(uint8_t module_id, rt_uint8_t sleep_mode);
  172. void rt_pm_module_delay_sleep(rt_uint8_t module_id, rt_tick_t timeout);
  173. rt_uint32_t rt_pm_module_get_status(void);
  174. rt_uint8_t rt_pm_get_sleep_mode(void);
  175. #endif /* __PM_H__ */