From 23e942ae4e66008632667f12c30bbb4f0fae31f7 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 26 Mar 2020 18:21:33 +0000 Subject: 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); -- cgit v1.2.3 33'>33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207