You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

suspend.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <stdbool.h>
  2. #include <avr/sleep.h>
  3. #include <avr/wdt.h>
  4. #include <avr/interrupt.h>
  5. #include "matrix.h"
  6. #include "action.h"
  7. #include "backlight.h"
  8. #include "softpwm_led.h"
  9. #include "suspend_avr.h"
  10. #include "suspend.h"
  11. #ifdef PROTOCOL_LUFA
  12. #include "lufa.h"
  13. #endif
  14. #define wdt_intr_enable(value) \
  15. __asm__ __volatile__ ( \
  16. "in __tmp_reg__,__SREG__" "\n\t" \
  17. "cli" "\n\t" \
  18. "wdr" "\n\t" \
  19. "sts %0,%1" "\n\t" \
  20. "out __SREG__,__tmp_reg__" "\n\t" \
  21. "sts %0,%2" "\n\t" \
  22. : /* no outputs */ \
  23. : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \
  24. "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \
  25. "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \
  26. _BV(WDIE) | (value & 0x07)) ) \
  27. : "r0" \
  28. )
  29. void suspend_idle(uint8_t time)
  30. {
  31. cli();
  32. set_sleep_mode(SLEEP_MODE_IDLE);
  33. sleep_enable();
  34. sei();
  35. sleep_cpu();
  36. sleep_disable();
  37. }
  38. /* Power down MCU with watchdog timer
  39. * wdto: watchdog timer timeout defined in <avr/wdt.h>
  40. * WDTO_15MS
  41. * WDTO_30MS
  42. * WDTO_60MS
  43. * WDTO_120MS
  44. * WDTO_250MS
  45. * WDTO_500MS
  46. * WDTO_1S
  47. * WDTO_2S
  48. * WDTO_4S
  49. * WDTO_8S
  50. */
  51. void suspend_power_down(uint8_t wdto)
  52. {
  53. #ifdef PROTOCOL_LUFA
  54. if (USB_DeviceState == DEVICE_STATE_Configured) return;
  55. #endif
  56. // Watchdog Interrupt Mode
  57. wdt_intr_enable(wdto);
  58. // TODO: more power saving
  59. // See PicoPower application note
  60. // - I/O port input with pullup
  61. // - prescale clock
  62. // - BOD disable
  63. // - Power Reduction Register PRR
  64. #ifdef SUSPEND_ACTION
  65. suspend_power_down_action();
  66. #endif
  67. #ifdef SOFTPWM_LED_ENABLE
  68. softpwm_led_disable();
  69. #endif
  70. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  71. sleep_enable();
  72. sei();
  73. sleep_cpu();
  74. sleep_disable();
  75. // Disable watchdog after sleep
  76. wdt_disable();
  77. }
  78. bool suspend_wakeup_condition(void)
  79. {
  80. matrix_power_up();
  81. matrix_scan();
  82. matrix_power_down();
  83. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  84. if (matrix_get_row(r)) return true;
  85. }
  86. return false;
  87. }
  88. // run immediately after wakeup
  89. void suspend_wakeup_init(void)
  90. {
  91. // clear keyboard state
  92. clear_keyboard();
  93. #ifdef SUSPEND_ACTION
  94. suspend_wakeup_init_action();
  95. #endif
  96. #ifdef BACKLIGHT_ENABLE
  97. backlight_init();
  98. #endif
  99. }
  100. #ifndef NO_SUSPEND_POWER_DOWN
  101. /* watchdog timeout */
  102. ISR(WDT_vect)
  103. {
  104. /* wakeup from MCU sleep mode */
  105. /*
  106. // blink LED
  107. static uint8_t led_state = 0;
  108. static uint8_t led_count = 0;
  109. led_count++;
  110. if ((led_count & 0x07) == 0) {
  111. led_set((led_state ^= (1<<USB_LED_CAPS_LOCK)));
  112. }
  113. */
  114. }
  115. #endif
  116. #ifdef SUSPEND_ACTION
  117. __attribute__ ((weak))
  118. void suspend_power_down_action(void)
  119. {
  120. }
  121. __attribute__ ((weak))
  122. void suspend_wakeup_init_action(void)
  123. {
  124. }
  125. #endif