aem_os_dev.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "aem_os_dev.h"
  2. #include "aem_log.h"
  3. #ifdef CONFIG_SYS_WAKELOCK
  4. #include <sys_wakelock.h>
  5. #include <sys_manager.h>
  6. #endif
  7. #include <power_manager.h>
  8. #include "sys_manager.h"
  9. #include <soc.h>
  10. static int check_screen_lock_time(void)
  11. {
  12. int ref_cnt = 0;
  13. #ifdef CONFIG_SYS_WAKELOCK
  14. ref_cnt = sys_wakelocks_check(FULL_WAKE_LOCK);
  15. #endif
  16. return ref_cnt;
  17. }
  18. static void force_screen_lock_time_reset(void)
  19. {
  20. #ifdef CONFIG_SYS_WAKELOCK
  21. sys_wake_lock(FULL_WAKE_LOCK);
  22. sys_wake_unlock(FULL_WAKE_LOCK);
  23. #endif
  24. }
  25. static void os_sys_wake_lock(void)
  26. {
  27. #ifdef CONFIG_SYS_WAKELOCK
  28. sys_wake_lock(FULL_WAKE_LOCK);
  29. #endif
  30. }
  31. static void os_sys_wake_unlock(void)
  32. {
  33. #ifdef CONFIG_SYS_WAKELOCK
  34. int ref_cnt = check_screen_lock_time();
  35. if (ref_cnt > 0)
  36. {
  37. sys_wake_unlock(FULL_WAKE_LOCK);
  38. }
  39. #endif
  40. }
  41. static void os_sys_partial_wake_lock(void)
  42. {
  43. #ifdef CONFIG_SYS_WAKELOCK
  44. sys_wake_lock(PARTIAL_WAKE_LOCK);
  45. #endif
  46. }
  47. static void os_sys_partial_wake_unlock(void)
  48. {
  49. #ifdef CONFIG_SYS_WAKELOCK
  50. sys_wake_unlock(PARTIAL_WAKE_LOCK);
  51. #endif
  52. }
  53. static void disable_screen_lock_time(void)
  54. {
  55. int ref_cnt = check_screen_lock_time();
  56. #ifdef CONFIG_SYS_WAKELOCK
  57. if (ref_cnt == 0)
  58. {
  59. sys_wake_lock(FULL_WAKE_LOCK);
  60. }
  61. #endif
  62. }
  63. static void screen_force_off(void)
  64. {
  65. #ifndef CONFIG_SIMULATOR
  66. system_request_fast_standby();
  67. #endif
  68. }
  69. static void enable_screen_lock_time(void)
  70. {
  71. int ref_cnt = check_screen_lock_time();
  72. #ifdef CONFIG_SYS_WAKELOCK
  73. if (ref_cnt != 0)
  74. {
  75. sys_wake_unlock(FULL_WAKE_LOCK);
  76. }
  77. #endif
  78. }
  79. #ifndef CONFIG_SIMULATOR
  80. extern void system_set_autosleep_time(uint32_t timeout);
  81. #endif
  82. static void set_screen_lock_time(uint32_t s)
  83. {
  84. // disable_screen_lock_time();
  85. // enable_screen_lock_time();
  86. #ifndef CONFIG_SIMULATOR
  87. system_set_auto_sleep_time(s);
  88. #endif
  89. }
  90. static uint32_t sys_cycle;
  91. static uint32_t sys_time_ms;
  92. static uint32_t get_sys_time_ms(void)
  93. {
  94. #ifndef CONFIG_SIMULATOR
  95. uint32_t cycle = k_cycle_get_32();
  96. sys_time_ms += k_cyc_to_ms_floor32(cycle - sys_cycle);
  97. sys_cycle = cycle;
  98. #endif
  99. return sys_time_ms;
  100. }
  101. static bool is_in_isr(void)
  102. {
  103. return os_is_in_isr();
  104. }
  105. static void set_aod_mode(bool is_aod)
  106. {
  107. AEM_LOG_I("set_aod_mode %d \r\n", is_aod);
  108. #ifndef CONFIG_SIMULATOR
  109. soc_set_aod_mode(is_aod);
  110. #endif
  111. }
  112. static bool get_aod_mode(void)
  113. {
  114. #ifndef CONFIG_SIMULATOR
  115. return soc_get_aod_mode();
  116. #endif
  117. return false;
  118. }
  119. static void sys_power_reboot(int reason)
  120. {
  121. AEM_LOG_I("sys_power_reboot %d \r\n", reason);
  122. #ifndef CONFIG_SIMULATOR
  123. system_power_reboot(reason);
  124. #endif
  125. }
  126. static void sys_power_off(void)
  127. {
  128. AEM_LOG_I("sys_power_off \r\n");
  129. #ifndef CONFIG_SIMULATOR
  130. system_power_off();
  131. #endif
  132. }
  133. static uint32_t get_current_thread_id(void)
  134. {
  135. #ifndef CONFIG_SIMULATOR
  136. return (uint32_t)os_current_get();
  137. #endif
  138. return 0;
  139. }
  140. static bool is_sys_screen_on(void)
  141. {
  142. #ifndef CONFIG_SIMULATOR
  143. return system_is_screen_on();
  144. #endif
  145. return false;
  146. }
  147. static uint32_t get_hw_cycle_tick(void)
  148. {
  149. return k_cycle_get_32();
  150. }
  151. static uint32_t get_hw_cycle_tick_elaps(uint32_t prev_tick)
  152. {
  153. uint32_t curr_time = k_cycle_get_32();
  154. uint32_t elaps = k_cyc_to_us_floor32(curr_time - prev_tick) / 1000;
  155. return elaps;
  156. }
  157. static const aem_sys_os_ops_t s_sys_os_ops = {
  158. .force_screen_lock_time_reset = force_screen_lock_time_reset,
  159. .disable_screen_lock_time = disable_screen_lock_time,
  160. .enable_screen_lock_time = enable_screen_lock_time,
  161. .check_screen_lock_time = check_screen_lock_time,
  162. .screen_force_off = screen_force_off,
  163. .set_screen_lock_time = set_screen_lock_time,
  164. .sys_wake_lock = os_sys_wake_lock,
  165. .sys_wake_unlock = os_sys_wake_unlock,
  166. .sys_partial_wake_lock = os_sys_partial_wake_lock,
  167. .sys_partial_wake_unlock = os_sys_partial_wake_unlock,
  168. .get_sys_time_ms = get_sys_time_ms,
  169. .is_in_isr = is_in_isr,
  170. .set_aod_mode = set_aod_mode,
  171. .get_aod_mode = get_aod_mode,
  172. .sys_power_off = sys_power_off,
  173. .sys_power_reboot = sys_power_reboot,
  174. .get_current_thread_id = get_current_thread_id,
  175. .is_sys_screen_on = is_sys_screen_on,
  176. .get_hw_cycle_tick = get_hw_cycle_tick,
  177. .get_hw_cycle_tick_elaps = get_hw_cycle_tick_elaps,
  178. };
  179. const aem_sys_os_ops_t *aem_get_sys_os_ops(void)
  180. {
  181. return &s_sys_os_ops;
  182. }