diff options
Diffstat (limited to 'tmk_core/protocol/vusb/main.c')
| -rw-r--r-- | tmk_core/protocol/vusb/main.c | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/tmk_core/protocol/vusb/main.c b/tmk_core/protocol/vusb/main.c index 2e8bb2fbb..af64f4e56 100644 --- a/tmk_core/protocol/vusb/main.c +++ b/tmk_core/protocol/vusb/main.c | |||
| @@ -53,10 +53,10 @@ static void initForUsbConnectivity(void) { | |||
| 53 | usbDeviceConnect(); | 53 | usbDeviceConnect(); |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | static void usb_remote_wakeup(void) { | 56 | static void vusb_send_remote_wakeup(void) { |
| 57 | cli(); | 57 | cli(); |
| 58 | 58 | ||
| 59 | int8_t ddr_orig = USBDDR; | 59 | uint8_t ddr_orig = USBDDR; |
| 60 | USBOUT |= (1 << USBMINUS); | 60 | USBOUT |= (1 << USBMINUS); |
| 61 | USBDDR = ddr_orig | USBMASK; | 61 | USBDDR = ddr_orig | USBMASK; |
| 62 | USBOUT ^= USBMASK; | 62 | USBOUT ^= USBMASK; |
| @@ -70,6 +70,27 @@ static void usb_remote_wakeup(void) { | |||
| 70 | sei(); | 70 | sei(); |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | bool vusb_suspended = false; | ||
| 74 | |||
| 75 | static void vusb_suspend(void) { | ||
| 76 | vusb_suspended = true; | ||
| 77 | |||
| 78 | #ifdef SLEEP_LED_ENABLE | ||
| 79 | sleep_led_enable(); | ||
| 80 | #endif | ||
| 81 | |||
| 82 | suspend_power_down(); | ||
| 83 | } | ||
| 84 | |||
| 85 | static void vusb_wakeup(void) { | ||
| 86 | vusb_suspended = false; | ||
| 87 | suspend_wakeup_init(); | ||
| 88 | |||
| 89 | #ifdef SLEEP_LED_ENABLE | ||
| 90 | sleep_led_disable(); | ||
| 91 | #endif | ||
| 92 | } | ||
| 93 | |||
| 73 | /** \brief Setup USB | 94 | /** \brief Setup USB |
| 74 | * | 95 | * |
| 75 | * FIXME: Needs doc | 96 | * FIXME: Needs doc |
| @@ -87,9 +108,8 @@ static void setup_usb(void) { | |||
| 87 | */ | 108 | */ |
| 88 | int main(void) __attribute__((weak)); | 109 | int main(void) __attribute__((weak)); |
| 89 | int main(void) { | 110 | int main(void) { |
| 90 | bool suspended = false; | ||
| 91 | #if USB_COUNT_SOF | 111 | #if USB_COUNT_SOF |
| 92 | uint16_t last_timer = timer_read(); | 112 | uint16_t sof_timer = timer_read(); |
| 93 | #endif | 113 | #endif |
| 94 | 114 | ||
| 95 | #ifdef CLKPR | 115 | #ifdef CLKPR |
| @@ -112,23 +132,24 @@ int main(void) { | |||
| 112 | while (1) { | 132 | while (1) { |
| 113 | #if USB_COUNT_SOF | 133 | #if USB_COUNT_SOF |
| 114 | if (usbSofCount != 0) { | 134 | if (usbSofCount != 0) { |
| 115 | suspended = false; | ||
| 116 | usbSofCount = 0; | 135 | usbSofCount = 0; |
| 117 | last_timer = timer_read(); | 136 | sof_timer = timer_read(); |
| 118 | # ifdef SLEEP_LED_ENABLE | 137 | if (vusb_suspended) { |
| 119 | sleep_led_disable(); | 138 | vusb_wakeup(); |
| 120 | # endif | 139 | } |
| 121 | } else { | 140 | } else { |
| 122 | // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1) | 141 | // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1) |
| 123 | if (timer_elapsed(last_timer) > 5) { | 142 | if (!vusb_suspended && timer_elapsed(sof_timer) > 5) { |
| 124 | suspended = true; | 143 | vusb_suspend(); |
| 125 | # ifdef SLEEP_LED_ENABLE | ||
| 126 | sleep_led_enable(); | ||
| 127 | # endif | ||
| 128 | } | 144 | } |
| 129 | } | 145 | } |
| 130 | #endif | 146 | #endif |
| 131 | if (!suspended) { | 147 | if (vusb_suspended) { |
| 148 | vusb_suspend(); | ||
| 149 | if (suspend_wakeup_condition()) { | ||
| 150 | vusb_send_remote_wakeup(); | ||
| 151 | } | ||
| 152 | } else { | ||
| 132 | usbPoll(); | 153 | usbPoll(); |
| 133 | 154 | ||
| 134 | // TODO: configuration process is inconsistent. it sometime fails. | 155 | // TODO: configuration process is inconsistent. it sometime fails. |
| @@ -145,6 +166,7 @@ int main(void) { | |||
| 145 | raw_hid_task(); | 166 | raw_hid_task(); |
| 146 | } | 167 | } |
| 147 | #endif | 168 | #endif |
| 169 | |||
| 148 | #ifdef CONSOLE_ENABLE | 170 | #ifdef CONSOLE_ENABLE |
| 149 | usbPoll(); | 171 | usbPoll(); |
| 150 | 172 | ||
| @@ -156,8 +178,6 @@ int main(void) { | |||
| 156 | // Run housekeeping | 178 | // Run housekeeping |
| 157 | housekeeping_task_kb(); | 179 | housekeeping_task_kb(); |
| 158 | housekeeping_task_user(); | 180 | housekeeping_task_user(); |
| 159 | } else if (suspend_wakeup_condition()) { | ||
| 160 | usb_remote_wakeup(); | ||
| 161 | } | 181 | } |
| 162 | } | 182 | } |
| 163 | } | 183 | } |
