Skip to content

Commit

Permalink
Enable SLEEP_LED on ATmega32A (qmk#8531)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zvecr authored Mar 26, 2020
1 parent c077300 commit 23e942a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
41 changes: 32 additions & 9 deletions tmk_core/common/avr/sleep_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -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___| ....
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions tmk_core/protocol/vusb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -59,6 +62,9 @@ int main(void) {
initForUsbConnectivity();

keyboard_init();
#ifdef SLEEP_LED_ENABLE
sleep_led_init();
#endif

debug("main loop\n");
while (1) {
Expand All @@ -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);
Expand Down

0 comments on commit 23e942a

Please sign in to comment.