diff options
| -rw-r--r-- | common.mk | 6 | ||||
| -rw-r--r-- | common/sleep_led.c | 84 | ||||
| -rw-r--r-- | common/sleep_led.h | 10 | ||||
| -rw-r--r-- | keyboard/gh60/Makefile.lufa | 1 | ||||
| -rw-r--r-- | protocol/lufa/lufa.c | 36 |
5 files changed, 128 insertions, 9 deletions
| @@ -47,5 +47,11 @@ ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE) | |||
| 47 | OPT_DEFS += -DMOUSE_ENABLE | 47 | OPT_DEFS += -DMOUSE_ENABLE |
| 48 | endif | 48 | endif |
| 49 | 49 | ||
| 50 | ifdef SLEEP_LED_ENABLE | ||
| 51 | SRC += $(COMMON_DIR)/sleep_led.c | ||
| 52 | OPT_DEFS += -DSLEEP_LED_ENABLE | ||
| 53 | endif | ||
| 54 | |||
| 55 | |||
| 50 | # Search Path | 56 | # Search Path |
| 51 | VPATH += $(TOP_DIR)/common | 57 | VPATH += $(TOP_DIR)/common |
diff --git a/common/sleep_led.c b/common/sleep_led.c new file mode 100644 index 000000000..edf68c352 --- /dev/null +++ b/common/sleep_led.c | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | #include <stdint.h> | ||
| 2 | #include <avr/io.h> | ||
| 3 | #include <avr/interrupt.h> | ||
| 4 | #include <avr/pgmspace.h> | ||
| 5 | #include "led.h" | ||
| 6 | #include "sleep_led.h" | ||
| 7 | |||
| 8 | /* Software PWM | ||
| 9 | * ______ ______ __ | ||
| 10 | * | ON |___OFF___| ON |___OFF___| .... | ||
| 11 | * |<-------------->|<-------------->|<- .... | ||
| 12 | * PWM period PWM period | ||
| 13 | * | ||
| 14 | * 256 interrupts/period[resolution] | ||
| 15 | * 64 periods/second[frequency] | ||
| 16 | * 256*64 interrupts/second | ||
| 17 | * F_CPU/(256*64) clocks/interrupt | ||
| 18 | */ | ||
| 19 | #define SLEEP_LED_TIMER_TOP F_CPU/(256*64) | ||
| 20 | |||
| 21 | void sleep_led_init(void) | ||
| 22 | { | ||
| 23 | /* Timer1 setup */ | ||
| 24 | /* CTC mode */ | ||
| 25 | TCCR1B |= _BV(WGM12); | ||
| 26 | /* Clock selelct: clk/1 */ | ||
| 27 | TCCR1B |= _BV(CS10); | ||
| 28 | /* Set TOP value */ | ||
| 29 | uint8_t sreg = SREG; | ||
| 30 | cli(); | ||
| 31 | OCR1AH = (SLEEP_LED_TIMER_TOP>>8)&0xff; | ||
| 32 | OCR1AL = SLEEP_LED_TIMER_TOP&0xff; | ||
| 33 | SREG = sreg; | ||
| 34 | } | ||
| 35 | |||
| 36 | void sleep_led_enable(void) | ||
| 37 | { | ||
| 38 | /* Enable Compare Match Interrupt */ | ||
| 39 | TIMSK1 |= _BV(OCIE1A); | ||
| 40 | } | ||
| 41 | |||
| 42 | void sleep_led_disable(void) | ||
| 43 | { | ||
| 44 | /* Disable Compare Match Interrupt */ | ||
| 45 | TIMSK1 &= ~_BV(OCIE1A); | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | /* Breathing Sleep LED brighness(PWM On period) table | ||
| 50 | * (32[steps] * 8[duration]) / 64[PWM periods/s] = 4 second breath cycle | ||
| 51 | */ | ||
| 52 | static const uint8_t breathing_table[32] PROGMEM = { | ||
| 53 | 0, 0, 0, 2, 9, 21, 37, 56, 78, 127, 151, 175, 197, 216, 232, 244, | ||
| 54 | 254, 244, 216, 197, 175, 151, 127, 78, 56, 37, 21, 9, 2, 0, 0, 0 | ||
| 55 | }; | ||
| 56 | |||
| 57 | ISR(TIMER1_COMPA_vect) | ||
| 58 | { | ||
| 59 | /* Software PWM | ||
| 60 | * timer:1111 1111 1111 1111 | ||
| 61 | * \----/\-/ \-------/+--- count(0-255) | ||
| 62 | * | +--------------- duration of step(8) | ||
| 63 | * +-------------------- index of step table(0-31) | ||
| 64 | */ | ||
| 65 | static union { | ||
| 66 | uint16_t row; | ||
| 67 | struct { | ||
| 68 | uint8_t count:8; | ||
| 69 | uint8_t duration:3; | ||
| 70 | uint8_t index:5; | ||
| 71 | } pwm; | ||
| 72 | } timer = { .row = 0 }; | ||
| 73 | |||
| 74 | timer.row++; | ||
| 75 | |||
| 76 | // LED on | ||
| 77 | if (timer.pwm.count == 0) { | ||
| 78 | led_set(1<<USB_LED_CAPS_LOCK); | ||
| 79 | } | ||
| 80 | // LED off | ||
| 81 | if (timer.pwm.count == pgm_read_byte(&breathing_table[timer.pwm.index])) { | ||
| 82 | led_set(0); | ||
| 83 | } | ||
| 84 | } | ||
diff --git a/common/sleep_led.h b/common/sleep_led.h new file mode 100644 index 000000000..aebdbeaa5 --- /dev/null +++ b/common/sleep_led.h | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #ifndef SLEEP_LED_H | ||
| 2 | #define SLEEP_LED_H | ||
| 3 | |||
| 4 | #define NO_SUSPEND_POWER_DOWN | ||
| 5 | |||
| 6 | void sleep_led_init(void); | ||
| 7 | void sleep_led_enable(void); | ||
| 8 | void sleep_led_disable(void); | ||
| 9 | |||
| 10 | #endif | ||
diff --git a/keyboard/gh60/Makefile.lufa b/keyboard/gh60/Makefile.lufa index a5ff609a7..8042ff3f4 100644 --- a/keyboard/gh60/Makefile.lufa +++ b/keyboard/gh60/Makefile.lufa | |||
| @@ -103,6 +103,7 @@ BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) | |||
| 103 | MOUSEKEY_ENABLE = yes # Mouse keys(+4700) | 103 | MOUSEKEY_ENABLE = yes # Mouse keys(+4700) |
| 104 | EXTRAKEY_ENABLE = yes # Audio control and System control(+450) | 104 | EXTRAKEY_ENABLE = yes # Audio control and System control(+450) |
| 105 | CONSOLE_ENABLE = yes # Console for debug(+400) | 105 | CONSOLE_ENABLE = yes # Console for debug(+400) |
| 106 | SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend | ||
| 106 | #NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA | 107 | #NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA |
| 107 | #PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support | 108 | #PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support |
| 108 | 109 | ||
diff --git a/protocol/lufa/lufa.c b/protocol/lufa/lufa.c index 556d0aeec..127dece54 100644 --- a/protocol/lufa/lufa.c +++ b/protocol/lufa/lufa.c | |||
| @@ -36,12 +36,20 @@ | |||
| 36 | this software. | 36 | this software. |
| 37 | */ | 37 | */ |
| 38 | 38 | ||
| 39 | #include <avr/sleep.h> | ||
| 40 | #include <avr/wdt.h> | ||
| 39 | #include "report.h" | 41 | #include "report.h" |
| 40 | #include "host.h" | 42 | #include "host.h" |
| 41 | #include "host_driver.h" | 43 | #include "host_driver.h" |
| 42 | #include "keyboard.h" | 44 | #include "keyboard.h" |
| 45 | #include "action.h" | ||
| 46 | #include "matrix.h" | ||
| 47 | #include "led.h" | ||
| 43 | #include "sendchar.h" | 48 | #include "sendchar.h" |
| 44 | #include "debug.h" | 49 | #include "debug.h" |
| 50 | #ifdef SLEEP_LED_ENABLE | ||
| 51 | #include "sleep_led.h" | ||
| 52 | #endif | ||
| 45 | 53 | ||
| 46 | #include "descriptor.h" | 54 | #include "descriptor.h" |
| 47 | #include "lufa.h" | 55 | #include "lufa.h" |
| @@ -140,10 +148,9 @@ static void Console_Task(void) | |||
| 140 | * 2) EVENT_USB_Device_Reset | 148 | * 2) EVENT_USB_Device_Reset |
| 141 | * 3) EVENT_USB_Device_Wake | 149 | * 3) EVENT_USB_Device_Wake |
| 142 | */ | 150 | */ |
| 143 | #include "led.h" | ||
| 144 | #include "matrix.h" | ||
| 145 | void EVENT_USB_Device_Connect(void) | 151 | void EVENT_USB_Device_Connect(void) |
| 146 | { | 152 | { |
| 153 | led_set(0x1f); // all on | ||
| 147 | } | 154 | } |
| 148 | 155 | ||
| 149 | void EVENT_USB_Device_Disconnect(void) | 156 | void EVENT_USB_Device_Disconnect(void) |
| @@ -156,17 +163,21 @@ void EVENT_USB_Device_Reset(void) | |||
| 156 | 163 | ||
| 157 | void EVENT_USB_Device_Suspend() | 164 | void EVENT_USB_Device_Suspend() |
| 158 | { | 165 | { |
| 166 | #ifdef SLEEP_LED_ENABLE | ||
| 167 | sleep_led_enable(); | ||
| 168 | #endif | ||
| 159 | } | 169 | } |
| 160 | 170 | ||
| 161 | #include "action.h" | ||
| 162 | void EVENT_USB_Device_WakeUp() | 171 | void EVENT_USB_Device_WakeUp() |
| 163 | { | 172 | { |
| 164 | // initialize | 173 | // initialize |
| 165 | matrix_init(); | 174 | matrix_init(); |
| 166 | clear_keyboard(); | 175 | clear_keyboard(); |
| 167 | 176 | ||
| 168 | // turn off LED | 177 | #ifdef SLEEP_LED_ENABLE |
| 169 | led_set(0); | 178 | sleep_led_disable(); |
| 179 | #endif | ||
| 180 | led_set(host_keyboard_leds()); | ||
| 170 | } | 181 | } |
| 171 | 182 | ||
| 172 | void EVENT_USB_Device_StartOfFrame(void) | 183 | void EVENT_USB_Device_StartOfFrame(void) |
| @@ -493,7 +504,6 @@ static void SetupHardware(void) | |||
| 493 | } | 504 | } |
| 494 | 505 | ||
| 495 | 506 | ||
| 496 | #include "matrix.h" | ||
| 497 | static bool wakeup_condition(void) | 507 | static bool wakeup_condition(void) |
| 498 | { | 508 | { |
| 499 | matrix_scan(); | 509 | matrix_scan(); |
| @@ -503,8 +513,6 @@ static bool wakeup_condition(void) | |||
| 503 | return false; | 513 | return false; |
| 504 | } | 514 | } |
| 505 | 515 | ||
| 506 | #include <avr/sleep.h> | ||
| 507 | #include <avr/wdt.h> | ||
| 508 | #define wdt_intr_enable(value) \ | 516 | #define wdt_intr_enable(value) \ |
| 509 | __asm__ __volatile__ ( \ | 517 | __asm__ __volatile__ ( \ |
| 510 | "in __tmp_reg__,__SREG__" "\n\t" \ | 518 | "in __tmp_reg__,__SREG__" "\n\t" \ |
| @@ -527,11 +535,15 @@ int main(void) | |||
| 527 | SetupHardware(); | 535 | SetupHardware(); |
| 528 | keyboard_init(); | 536 | keyboard_init(); |
| 529 | host_set_driver(&lufa_driver); | 537 | host_set_driver(&lufa_driver); |
| 538 | #ifdef SLEEP_LED_ENABLE | ||
| 539 | sleep_led_init(); | ||
| 540 | #endif | ||
| 530 | sei(); | 541 | sei(); |
| 531 | 542 | ||
| 532 | while (1) { | 543 | while (1) { |
| 533 | // while suspend | 544 | // while suspend |
| 534 | while (USB_DeviceState == DEVICE_STATE_Suspended) { | 545 | while (USB_DeviceState == DEVICE_STATE_Suspended) { |
| 546 | #ifndef NO_SUSPEND_POWER_DOWN | ||
| 535 | // Enable watchdog to wake from MCU sleep | 547 | // Enable watchdog to wake from MCU sleep |
| 536 | cli(); | 548 | cli(); |
| 537 | wdt_reset(); | 549 | wdt_reset(); |
| @@ -558,6 +570,7 @@ int main(void) | |||
| 558 | 570 | ||
| 559 | // Disable watchdog after sleep | 571 | // Disable watchdog after sleep |
| 560 | wdt_disable(); | 572 | wdt_disable(); |
| 573 | #endif | ||
| 561 | 574 | ||
| 562 | // Send request of USB Wakeup from Suspend to host | 575 | // Send request of USB Wakeup from Suspend to host |
| 563 | if (USB_Device_RemoteWakeupEnabled) { | 576 | if (USB_Device_RemoteWakeupEnabled) { |
| @@ -575,9 +588,12 @@ int main(void) | |||
| 575 | } | 588 | } |
| 576 | } | 589 | } |
| 577 | 590 | ||
| 578 | /* watchdog timeout during sleep */ | 591 | #ifndef NO_SUSPEND_POWER_DOWN |
| 592 | /* watchdog timeout */ | ||
| 579 | ISR(WDT_vect) | 593 | ISR(WDT_vect) |
| 580 | { | 594 | { |
| 595 | /* wakeup from MCU sleep mode */ | ||
| 596 | /* | ||
| 581 | // blink LED | 597 | // blink LED |
| 582 | static uint8_t led_state = 0; | 598 | static uint8_t led_state = 0; |
| 583 | static uint8_t led_count = 0; | 599 | static uint8_t led_count = 0; |
| @@ -585,4 +601,6 @@ ISR(WDT_vect) | |||
| 585 | if ((led_count & 0x07) == 0) { | 601 | if ((led_count & 0x07) == 0) { |
| 586 | led_set((led_state ^= (1<<USB_LED_CAPS_LOCK))); | 602 | led_set((led_state ^= (1<<USB_LED_CAPS_LOCK))); |
| 587 | } | 603 | } |
| 604 | */ | ||
| 588 | } | 605 | } |
| 606 | #endif | ||
