From 23e942ae4e66008632667f12c30bbb4f0fae31f7 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 26 Mar 2020 18:21:33 +0000 Subject: [PATCH] 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 --- tmk_core/common/avr/sleep_led.c | 41 +++++++++++++++++++++++++-------- 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 @@ #include "led.h" #include "sleep_led.h" +#ifndef SLEEP_LED_TIMER +# define SLEEP_LED_TIMER 1 +#endif + +#if SLEEP_LED_TIMER == 1 +# define TCCRxB TCCR1B +# define TIMERx_COMPA_vect TIMER1_COMPA_vect +# if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register +# define TIMSKx TIMSK +# else +# define TIMSKx TIMSK1 +# endif +# define OCIExA OCIE1A +# define OCRxx OCR1A +#elif SLEEP_LED_TIMER == 3 +# define TCCRxB TCCR3B +# define TIMERx_COMPA_vect TIMER3_COMPA_vect +# define TIMSKx TIMSK3 +# define OCIExA OCIE3A +# define OCRxx OCR3A +#else +error("Invalid SLEEP_LED_TIMER config") +#endif + /* Software PWM * ______ ______ __ * | ON |___OFF___| ON |___OFF___| .... @@ -25,15 +49,14 @@ void sleep_led_init(void) { /* Timer1 setup */ /* CTC mode */ - TCCR1B |= _BV(WGM12); + TCCRxB |= _BV(WGM12); /* Clock selelct: clk/1 */ - TCCR1B |= _BV(CS10); + TCCRxB |= _BV(CS10); /* Set TOP value */ uint8_t sreg = SREG; cli(); - OCR1AH = (SLEEP_LED_TIMER_TOP >> 8) & 0xff; - OCR1AL = SLEEP_LED_TIMER_TOP & 0xff; - SREG = sreg; + OCRxx = SLEEP_LED_TIMER_TOP; + SREG = sreg; } /** \brief Sleep LED enable @@ -42,7 +65,7 @@ void sleep_led_init(void) { */ void sleep_led_enable(void) { /* Enable Compare Match Interrupt */ - TIMSK1 |= _BV(OCIE1A); + TIMSKx |= _BV(OCIExA); } /** \brief Sleep LED disable @@ -51,7 +74,7 @@ void sleep_led_enable(void) { */ void sleep_led_disable(void) { /* Disable Compare Match Interrupt */ - TIMSK1 &= ~_BV(OCIE1A); + TIMSKx &= ~_BV(OCIExA); } /** \brief Sleep LED toggle @@ -60,7 +83,7 @@ void sleep_led_disable(void) { */ void sleep_led_toggle(void) { /* Disable Compare Match Interrupt */ - TIMSK1 ^= _BV(OCIE1A); + TIMSKx ^= _BV(OCIExA); } /** \brief Breathing Sleep LED brighness(PWM On period) table @@ -72,7 +95,7 @@ void sleep_led_toggle(void) { */ 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}; -ISR(TIMER1_COMPA_vect) { +ISR(TIMERx_COMPA_vect) { /* Software PWM * timer:1111 1111 1111 1111 * \_____/\/ \_______/____ 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 @@ #include "timer.h" #include "uart.h" #include "debug.h" +#ifdef SLEEP_LED_ENABLE +# include "sleep_led.h" +#endif #define UART_BAUD_RATE 115200 @@ -59,6 +62,9 @@ int main(void) { initForUsbConnectivity(); keyboard_init(); +#ifdef SLEEP_LED_ENABLE + sleep_led_init(); +#endif debug("main loop\n"); while (1) { @@ -67,10 +73,16 @@ int main(void) { suspended = false; usbSofCount = 0; last_timer = timer_read(); +# ifdef SLEEP_LED_ENABLE + sleep_led_disable(); +# endif } else { // Suspend when no SOF in 3ms-10ms(7.1.7.4 Suspending of USB1.1) if (timer_elapsed(last_timer) > 5) { suspended = true; +# ifdef SLEEP_LED_ENABLE + sleep_led_enable(); +# endif /* uart_putchar('S'); _delay_ms(1);