diff options
| author | Joel Challis <git@zvecr.com> | 2020-03-26 18:21:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-26 18:21:33 +0000 |
| commit | 23e942ae4e66008632667f12c30bbb4f0fae31f7 (patch) | |
| tree | a5d3fc389ee9444635abeaba2126d500e595676e | |
| parent | c077300e19a9c788b3d30bc65c1198fc88de3d5b (diff) | |
| download | qmk_firmware-23e942ae4e66008632667f12c30bbb4f0fae31f7.tar.gz qmk_firmware-23e942ae4e66008632667f12c30bbb4f0fae31f7.zip | |
Enable SLEEP_LED on ATmega32A (#8531)
* Port over some AVR backlight logic to SLEEP_LED
* Port over some AVR backlight logic to SLEEP_LED - add timer 3
* Port over some AVR backlight logic to SLEEP_LED - clang format
* Enable SLEEP_LED within vusb protocol
| -rw-r--r-- | tmk_core/common/avr/sleep_led.c | 41 | ||||
| -rw-r--r-- | tmk_core/protocol/vusb/main.c | 12 |
2 files changed, 44 insertions, 9 deletions
diff --git a/tmk_core/common/avr/sleep_led.c b/tmk_core/common/avr/sleep_led.c index 61fa70dc3..63dcc2afd 100644 --- a/tmk_core/common/avr/sleep_led.c +++ b/tmk_core/common/avr/sleep_led.c | |||
| @@ -5,6 +5,30 @@ | |||
| 5 | #include "led.h" | 5 | #include "led.h" |
| 6 | #include "sleep_led.h" | 6 | #include "sleep_led.h" |
| 7 | 7 | ||
| 8 | #ifndef SLEEP_LED_TIMER | ||
| 9 | # define SLEEP_LED_TIMER 1 | ||
| 10 | #endif | ||
| 11 | |||
| 12 | #if SLEEP_LED_TIMER == 1 | ||
| 13 | # define TCCRxB TCCR1B | ||
| 14 | # define TIMERx_COMPA_vect TIMER1_COMPA_vect | ||
| 15 | # if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register | ||
| 16 | # define TIMSKx TIMSK | ||
| 17 | # else | ||
| 18 | # define TIMSKx TIMSK1 | ||
| 19 | # endif | ||
| 20 | # define OCIExA OCIE1A | ||
| 21 | # define OCRxx OCR1A | ||
| 22 | #elif SLEEP_LED_TIMER == 3 | ||
| 23 | # define TCCRxB TCCR3B | ||
| 24 | # define TIMERx_COMPA_vect TIMER3_COMPA_vect | ||
| 25 | # define TIMSKx TIMSK3 | ||
| 26 | # define OCIExA OCIE3A | ||
| 27 | # define OCRxx OCR3A | ||
| 28 | #else | ||
| 29 | error("Invalid SLEEP_LED_TIMER config") | ||
| 30 | #endif | ||
| 31 | |||
| 8 | /* Software PWM | 32 | /* Software PWM |
| 9 | * ______ ______ __ | 33 | * ______ ______ __ |
| 10 | * | ON |___OFF___| ON |___OFF___| .... | 34 | * | ON |___OFF___| ON |___OFF___| .... |
| @@ -25,15 +49,14 @@ | |||
| 25 | void sleep_led_init(void) { | 49 | void sleep_led_init(void) { |
| 26 | /* Timer1 setup */ | 50 | /* Timer1 setup */ |
| 27 | /* CTC mode */ | 51 | /* CTC mode */ |
| 28 | TCCR1B |= _BV(WGM12); | 52 | TCCRxB |= _BV(WGM12); |
| 29 | /* Clock selelct: clk/1 */ | 53 | /* Clock selelct: clk/1 */ |
| 30 | TCCR1B |= _BV(CS10); | 54 | TCCRxB |= _BV(CS10); |
| 31 | /* Set TOP value */ | 55 | /* Set TOP value */ |
| 32 | uint8_t sreg = SREG; | 56 | uint8_t sreg = SREG; |
| 33 | cli(); | 57 | cli(); |
| 34 | OCR1AH = (SLEEP_LED_TIMER_TOP >> 8) & 0xff; | 58 | OCRxx = SLEEP_LED_TIMER_TOP; |
| 35 | OCR1AL = SLEEP_LED_TIMER_TOP & 0xff; | 59 | SREG = sreg; |
| 36 | SREG = sreg; | ||
| 37 | } | 60 | } |
| 38 | 61 | ||
| 39 | /** \brief Sleep LED enable | 62 | /** \brief Sleep LED enable |
| @@ -42,7 +65,7 @@ void sleep_led_init(void) { | |||
| 42 | */ | 65 | */ |
| 43 | void sleep_led_enable(void) { | 66 | void sleep_led_enable(void) { |
| 44 | /* Enable Compare Match Interrupt */ | 67 | /* Enable Compare Match Interrupt */ |
| 45 | TIMSK1 |= _BV(OCIE1A); | 68 | TIMSKx |= _BV(OCIExA); |
| 46 | } | 69 | } |
| 47 | 70 | ||
| 48 | /** \brief Sleep LED disable | 71 | /** \brief Sleep LED disable |
| @@ -51,7 +74,7 @@ void sleep_led_enable(void) { | |||
| 51 | */ | 74 | */ |
| 52 | void sleep_led_disable(void) { | 75 | void sleep_led_disable(void) { |
| 53 | /* Disable Compare Match Interrupt */ | 76 | /* Disable Compare Match Interrupt */ |
| 54 | TIMSK1 &= ~_BV(OCIE1A); | 77 | TIMSKx &= ~_BV(OCIExA); |
| 55 | } | 78 | } |
| 56 | 79 | ||
| 57 | /** \brief Sleep LED toggle | 80 | /** \brief Sleep LED toggle |
| @@ -60,7 +83,7 @@ void sleep_led_disable(void) { | |||
| 60 | */ | 83 | */ |
| 61 | void sleep_led_toggle(void) { | 84 | void sleep_led_toggle(void) { |
| 62 | /* Disable Compare Match Interrupt */ | 85 | /* Disable Compare Match Interrupt */ |
| 63 | TIMSK1 ^= _BV(OCIE1A); | 86 | TIMSKx ^= _BV(OCIExA); |
| 64 | } | 87 | } |
| 65 | 88 | ||
| 66 | /** \brief Breathing Sleep LED brighness(PWM On period) table | 89 | /** \brief Breathing Sleep LED brighness(PWM On period) table |
| @@ -72,7 +95,7 @@ void sleep_led_toggle(void) { | |||
| 72 | */ | 95 | */ |
| 73 | static const uint8_t breathing_table[64] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | 96 | static const uint8_t breathing_table[64] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 74 | 97 | ||
| 75 | ISR(TIMER1_COMPA_vect) { | 98 | ISR(TIMERx_COMPA_vect) { |
| 76 | /* Software PWM | 99 | /* Software PWM |
| 77 | * timer:1111 1111 1111 1111 | 100 | * timer:1111 1111 1111 1111 |
| 78 | * \_____/\/ \_______/____ count(0-255) | 101 | * \_____/\/ \_______/____ count(0-255) |
diff --git a/tmk_core/protocol/vusb/main.c b/tmk_core/protocol/vusb/main.c index 068c4d8f3..219989876 100644 --- a/tmk_core/protocol/vusb/main.c +++ b/tmk_core/protocol/vusb/main.c | |||
| @@ -20,6 +20,9 @@ | |||
| 20 | #include "timer.h" | 20 | #include "timer.h" |
| 21 | #include "uart.h" | 21 | #include "uart.h" |
| 22 | #include "debug.h" | 22 | #include "debug.h" |
| 23 | #ifdef SLEEP_LED_ENABLE | ||
| 24 | # include "sleep_led.h" | ||
| 25 | #endif | ||
| 23 | 26 | ||
| 24 | #define UART_BAUD_RATE 115200 | 27 | #define UART_BAUD_RATE 115200 |
| 25 | 28 | ||
| @@ -59,6 +62,9 @@ int main(void) { | |||
| 59 | initForUsbConnectivity(); | 62 | initForUsbConnectivity(); |
| 60 | 63 | ||
| 61 | keyboard_init(); | 64 | keyboard_init(); |
| 65 | #ifdef SLEEP_LED_ENABLE | ||
| 66 | sleep_led_init(); | ||
| 67 | #endif | ||
| 62 | 68 | ||
| 63 | debug("main loop\n"); | 69 | debug("main loop\n"); |
| 64 | while (1) { | 70 | while (1) { |
| @@ -67,10 +73,16 @@ int main(void) { | |||
| 67 | suspended = false; | 73 | suspended = false; |
| 68 | usbSofCount = 0; | 74 | usbSofCount = 0; |
| 69 | last_timer = timer_read(); | 75 | last_timer = timer_read(); |
| 76 | # ifdef SLEEP_LED_ENABLE | ||
| 77 | sleep_led_disable(); | ||
| 78 | # endif | ||
| 70 | } else { | 79 | } else { |
| 71 | // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1) | 80 | // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1) |
| 72 | if (timer_elapsed(last_timer) > 5) { | 81 | if (timer_elapsed(last_timer) > 5) { |
| 73 | suspended = true; | 82 | suspended = true; |
| 83 | # ifdef SLEEP_LED_ENABLE | ||
| 84 | sleep_led_enable(); | ||
| 85 | # endif | ||
| 74 | /* | 86 | /* |
| 75 | uart_putchar('S'); | 87 | uart_putchar('S'); |
| 76 | _delay_ms(1); | 88 | _delay_ms(1); |
