pm.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  11. #ifndef __PM_H__
  12. #define __PM_H__
  13. #include <rtthread.h>
  14. #ifndef PM_HAS_CUSTOM_CONFIG
  15. /* All modes used for rt_pm_request() adn rt_pm_release() */
  16. enum
  17. {
  18. /* run modes */
  19. PM_RUN_MODE_NORMAL = 0,
  20. /* sleep modes */
  21. PM_SLEEP_MODE_SLEEP,
  22. PM_SLEEP_MODE_TIMER,
  23. PM_SLEEP_MODE_SHUTDOWN,
  24. };
  25. /* The name of all modes used in the msh command "pm_dump" */
  26. #define PM_MODE_NAMES \
  27. { \
  28. "Running Mode", \
  29. \
  30. "Sleep Mode", \
  31. "Timer Mode", \
  32. "Shutdown Mode", \
  33. }
  34. /* run mode count : 1 */
  35. #define PM_RUN_MODE_COUNT 1
  36. /* sleep mode count : 3 */
  37. #define PM_SLEEP_MODE_COUNT 3
  38. /* support redefining default run mode */
  39. #ifndef PM_RUN_MODE_DEFAULT
  40. #define PM_RUN_MODE_DEFAULT 0
  41. #endif
  42. /* support redefining default sleep mode */
  43. #ifndef PM_SLEEP_MODE_DEFAULT
  44. #define PM_SLEEP_MODE_DEFAULT (PM_SLEEP_MODE_START)
  45. #endif
  46. /* support redefining the minimum tick into sleep mode */
  47. #ifndef PM_MIN_ENTER_SLEEP_TICK
  48. #define PM_MIN_ENTER_SLEEP_TICK (1)
  49. #endif
  50. #else /* PM_HAS_CUSTOM_CONFIG */
  51. #include <pm_cfg.h>
  52. #ifndef PM_RUN_MODE_COUNT
  53. #error "You must defined PM_RUN_MODE_COUNT on pm_cfg.h"
  54. #endif
  55. #ifndef PM_SLEEP_MODE_COUNT
  56. #error "You must defined PM_SLEEP_MODE_COUNT on pm_cfg.h"
  57. #endif
  58. #ifndef PM_MODE_DEFAULT
  59. #error "You must defined PM_MODE_DEFAULT on pm_cfg.h"
  60. #endif
  61. #ifndef PM_MODE_NAMES
  62. #error "You must defined PM_MODE_NAMES on pm_cfg.h"
  63. #endif
  64. #ifndef PM_RUN_MODE_DEFAULT
  65. #error "You must defined PM_RUN_MODE_DEFAULT on pm_cfg.h"
  66. #endif
  67. /* The default sleep mode(PM_SLEEP_MODE_DEFAULT) are not required.
  68. * If the default mode is defined, it is requested once in rt_system_pm_init()
  69. */
  70. #endif /* PM_HAS_CUSTOM_CONFIG */
  71. /* run mode must start at 0 */
  72. #define PM_RUN_MODE_START 0
  73. /* the values of the run modes and sleep mode must be consecutive */
  74. #define PM_SLEEP_MODE_START PM_RUN_MODE_COUNT
  75. /* all mode count */
  76. #define PM_MODE_COUNT (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT)
  77. /* The last mode, will be request in rt_system_pm_init() */
  78. #define PM_MODE_MAX (PM_RUN_MODE_COUNT + PM_SLEEP_MODE_COUNT - 1)
  79. #if PM_MODE_COUNT > 32
  80. #error "The number of modes cannot exceed 32"
  81. #endif
  82. /**
  83. * device control flag to request or release power
  84. */
  85. #define RT_PM_DEVICE_CTRL_REQUEST 0x01
  86. #define RT_PM_DEVICE_CTRL_RELEASE 0x02
  87. struct rt_pm;
  88. /**
  89. * low power mode operations
  90. */
  91. struct rt_pm_ops
  92. {
  93. void (*enter)(struct rt_pm *pm);
  94. void (*exit)(struct rt_pm *pm);
  95. #if PM_RUN_MODE_COUNT > 1
  96. void (*frequency_change)(struct rt_pm *pm, rt_uint32_t frequency);
  97. #endif
  98. void (*timer_start)(struct rt_pm *pm, rt_uint32_t timeout);
  99. void (*timer_stop)(struct rt_pm *pm);
  100. rt_tick_t (*timer_get_tick)(struct rt_pm *pm);
  101. };
  102. struct rt_device_pm_ops
  103. {
  104. #if PM_RUN_MODE_COUNT > 1
  105. void (*frequency_change)(const struct rt_device* device);
  106. #endif
  107. void (*suspend)(const struct rt_device* device);
  108. void (*resume) (const struct rt_device* device);
  109. };
  110. struct rt_device_pm
  111. {
  112. const struct rt_device* device;
  113. const struct rt_device_pm_ops* ops;
  114. };
  115. /**
  116. * power management
  117. */
  118. struct rt_pm
  119. {
  120. struct rt_device parent;
  121. /* modes */
  122. rt_uint8_t modes[PM_MODE_COUNT];
  123. rt_uint8_t current_mode; /* current pm mode */
  124. rt_uint8_t exit_count;
  125. /* the list of device, which has PM feature */
  126. rt_uint8_t device_pm_number;
  127. struct rt_device_pm* device_pm;
  128. struct rt_semaphore device_lock;
  129. /* if the mode has timer, the corresponding bit is 1*/
  130. rt_uint32_t timer_mask;
  131. const struct rt_pm_ops *ops;
  132. };
  133. void rt_pm_enter(void);
  134. void rt_pm_exit(void);
  135. void rt_pm_request(rt_ubase_t mode);
  136. void rt_pm_release(rt_ubase_t mode);
  137. void rt_pm_register_device(struct rt_device* device, const struct rt_device_pm_ops* ops);
  138. void rt_pm_unregister_device(struct rt_device* device);
  139. void rt_system_pm_init(const struct rt_pm_ops *ops,
  140. rt_uint8_t timer_mask,
  141. void *user_data);
  142. #endif /* __PM_H__ */