suspend.c 2.5 KB

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