suspend.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* TODO */
  2. #include "ch.h"
  3. #include "hal.h"
  4. #include "matrix.h"
  5. #include "action.h"
  6. #include "action_util.h"
  7. #include "mousekey.h"
  8. #include "host.h"
  9. #include "suspend.h"
  10. #include "wait.h"
  11. #ifdef BACKLIGHT_ENABLE
  12. # include "backlight.h"
  13. #endif
  14. /** \brief suspend idle
  15. *
  16. * FIXME: needs doc
  17. */
  18. void suspend_idle(uint8_t time) {
  19. // TODO: this is not used anywhere - what units is 'time' in?
  20. wait_ms(time);
  21. }
  22. /** \brief Run keyboard level Power down
  23. *
  24. * FIXME: needs doc
  25. */
  26. __attribute__((weak)) void suspend_power_down_user(void) {}
  27. /** \brief Run keyboard level Power down
  28. *
  29. * FIXME: needs doc
  30. */
  31. __attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); }
  32. /** \brief suspend power down
  33. *
  34. * FIXME: needs doc
  35. */
  36. void suspend_power_down(void) {
  37. // TODO: figure out what to power down and how
  38. // shouldn't power down TPM/FTM if we want a breathing LED
  39. // also shouldn't power down USB
  40. suspend_power_down_kb();
  41. // on AVR, this enables the watchdog for 15ms (max), and goes to
  42. // SLEEP_MODE_PWR_DOWN
  43. wait_ms(17);
  44. }
  45. /** \brief suspend wakeup condition
  46. *
  47. * FIXME: needs doc
  48. */
  49. __attribute__((weak)) void matrix_power_up(void) {}
  50. __attribute__((weak)) void matrix_power_down(void) {}
  51. bool suspend_wakeup_condition(void) {
  52. matrix_power_up();
  53. matrix_scan();
  54. matrix_power_down();
  55. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  56. if (matrix_get_row(r)) return true;
  57. }
  58. return false;
  59. }
  60. /** \brief run user level code immediately after wakeup
  61. *
  62. * FIXME: needs doc
  63. */
  64. __attribute__((weak)) void suspend_wakeup_init_user(void) {}
  65. /** \brief run keyboard level code immediately after wakeup
  66. *
  67. * FIXME: needs doc
  68. */
  69. __attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); }
  70. /** \brief suspend wakeup condition
  71. *
  72. * run immediately after wakeup
  73. * FIXME: needs doc
  74. */
  75. void suspend_wakeup_init(void) {
  76. // clear keyboard state
  77. // need to do it manually, because we're running from ISR
  78. // and clear_keyboard() calls print
  79. // so only clear the variables in memory
  80. // the reports will be sent from main.c afterwards
  81. // or if the PC asks for GET_REPORT
  82. clear_mods();
  83. clear_weak_mods();
  84. clear_keys();
  85. #ifdef MOUSEKEY_ENABLE
  86. mousekey_clear();
  87. #endif /* MOUSEKEY_ENABLE */
  88. #ifdef EXTRAKEY_ENABLE
  89. host_system_send(0);
  90. host_consumer_send(0);
  91. #endif /* EXTRAKEY_ENABLE */
  92. #ifdef BACKLIGHT_ENABLE
  93. backlight_init();
  94. #endif /* BACKLIGHT_ENABLE */
  95. suspend_wakeup_init_kb();
  96. }