luat_pm_air101.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "luat_base.h"
  2. #include "luat_pm.h"
  3. #include "wm_pmu.h"
  4. #include "wm_regs.h"
  5. #include "wm_timer.h"
  6. #include "wm_watchdog.h"
  7. #include "wm_ram_config.h"
  8. #define LUAT_LOG_TAG "pm"
  9. #include "luat_log.h"
  10. static int dtimer_id = 0xFF;
  11. int luat_pm_request(int mode) {
  12. dtimer_id = 0xFF;
  13. if (mode == LUAT_PM_SLEEP_MODE_LIGHT) {
  14. tls_close_peripheral_clock(TLS_PERIPHERAL_TYPE_TIMER);
  15. tls_pmu_sleep_start();
  16. tls_open_peripheral_clock(TLS_PERIPHERAL_TYPE_TIMER);
  17. return 0;
  18. }
  19. else if (mode == LUAT_PM_SLEEP_MODE_DEEP || mode == LUAT_PM_SLEEP_MODE_STANDBY) {
  20. tls_pmu_standby_start();
  21. return 0;
  22. }
  23. return -1;
  24. }
  25. static void pmu_timer0_irq(uint8_t *arg){
  26. tls_pmu_timer0_stop();
  27. dtimer_id = 0;
  28. }
  29. static void pmu_timer1_irq(uint8_t *arg){
  30. tls_pmu_timer1_stop();
  31. dtimer_id = 1;
  32. }
  33. //int luat_pm_release(int mode);
  34. int luat_pm_dtimer_start(int id, size_t timeout) {
  35. tls_pmu_clk_select(1);
  36. if (id == 0 && timeout > 0) {
  37. // 单位秒
  38. tls_pmu_timer0_stop();
  39. tls_pmu_timer0_isr_register((tls_pmu_irq_callback)pmu_timer0_irq, NULL);
  40. tls_pmu_timer0_start((timeout + 999) / 1000);
  41. return 0;
  42. }
  43. else if (id == 1 && timeout > 0) {
  44. // 单位毫妙
  45. tls_pmu_timer1_stop();
  46. tls_pmu_timer1_isr_register((tls_pmu_irq_callback)pmu_timer1_irq, NULL);
  47. tls_pmu_timer1_start(timeout);
  48. return 0;
  49. }
  50. return -1;
  51. }
  52. int luat_pm_dtimer_stop(int id) {
  53. if (id == 0) {
  54. tls_pmu_timer0_stop();
  55. return 0;
  56. }
  57. else if (id == 1) {
  58. tls_pmu_timer1_stop();
  59. return 0;
  60. }
  61. return -1;
  62. }
  63. int luat_pm_dtimer_check(int id) {
  64. return 0;
  65. }
  66. //void luat_pm_cb(int event, int arg, void* args);
  67. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  68. extern int power_bk_reg; // from wm_main.c
  69. // extern int wake_src;
  70. int luat_pm_last_state(int *lastState, int *rtcOrPad) {
  71. int reson = tls_sys_get_reboot_reason();
  72. switch (tls_sys_get_reboot_reason())
  73. {
  74. case REBOOT_REASON_POWER_ON:// 硬件复位开机
  75. *lastState = 0;
  76. *rtcOrPad = 0;
  77. break;
  78. case REBOOT_REASON_STANDBY: // 唤醒重启
  79. *lastState = 3;
  80. *rtcOrPad = 3;
  81. break;
  82. case REBOOT_REASON_EXCEPTION: // 异常重启
  83. *lastState = 0;
  84. *rtcOrPad = 1;
  85. break;
  86. case REBOOT_REASON_WDG_TIMEOUT: // 硬狗超时
  87. *lastState = 0;
  88. *rtcOrPad = 1;
  89. break;
  90. case REBOOT_REASON_ACTIVE: // 用户主动复位
  91. *lastState = 0;
  92. *rtcOrPad = 0;
  93. break;
  94. case REBOOT_REASON_SLEEP: // 不可能出现
  95. *lastState = 0;
  96. *rtcOrPad = 4;
  97. break;
  98. default:
  99. break;
  100. }
  101. return 0;
  102. }
  103. int luat_pm_get_poweron_reason(void)
  104. {
  105. int reson = tls_sys_get_reboot_reason();
  106. switch (tls_sys_get_reboot_reason())
  107. {
  108. case REBOOT_REASON_POWER_ON:// 硬件复位开机
  109. return 0;
  110. case REBOOT_REASON_STANDBY:
  111. return 2;
  112. case REBOOT_REASON_EXCEPTION:
  113. return 6;
  114. case REBOOT_REASON_WDG_TIMEOUT:
  115. return 8;
  116. case REBOOT_REASON_ACTIVE:
  117. return 3;
  118. case REBOOT_REASON_SLEEP:
  119. return 2;
  120. default:
  121. break;
  122. }
  123. return 4;
  124. }
  125. int luat_pm_force(int mode) {
  126. return luat_pm_request(mode);
  127. }
  128. int luat_pm_check(void) {
  129. return 0;
  130. }
  131. int luat_pm_dtimer_wakeup_id(int* id) {
  132. *id = dtimer_id;
  133. return 0;
  134. }
  135. int luat_pm_dtimer_list(size_t* count, size_t* list) {
  136. *count = 0;
  137. return 0;
  138. }
  139. int luat_pm_power_ctrl(int id, uint8_t onoff) {
  140. LLOGW("not support yet");
  141. return -1;
  142. }
  143. int luat_pm_wakeup_pin(int pin, int val){
  144. LLOGW("not support yet");
  145. return -1;
  146. }
  147. int luat_pm_reset(void) {
  148. extern void luat_os_reboot(int code);
  149. luat_os_reboot(0);
  150. return 0;
  151. }