From 746058306b6c3b037fc560a13bb78d088c734401 Mon Sep 17 00:00:00 2001 From: Danny Nguyen Date: Wed, 20 Mar 2019 17:45:45 -0400 Subject: Make sure NOBITS is set on reset_key Patch from https://github.com/qmk/qmk_firmware/issues/3657#issuecomment-415147411 Long story short, in avr-gcc pre-8.2, reset_key was assigned to a memory area that was in a normal range, but when 8.2 came out, that memory got moved to an out of range area, causing errors like 0x800293 out of range. Apparently, this was fixed up in avr-gcc, but we haven't seen a release with the fix yet (we expected it in 8.3, but that didn't happen for some reason). What this commit does is move the reset_key back to the original memory location it was in before. --- tmk_core/common/avr/bootloader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index c4b913280e..3cdcd2e426 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -70,7 +70,7 @@ * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html */ #define BOOTLOADER_RESET_KEY 0xB007B007 -uint32_t reset_key __attribute__ ((section (".noinit"))); +uint32_t reset_key __attribute__ ((section (".noinit,\"aw\",@nobits;"))); /** \brief initialize MCU status by watchdog reset * -- cgit v1.2.3 From 3538955778c253e68779605cc67c27e15d195729 Mon Sep 17 00:00:00 2001 From: yiancar Date: Tue, 16 Jul 2019 03:11:59 +0100 Subject: Usbasploader bootloader option addition (#6304) * Added USBasp bootloader option for USBasploader * author comment * ifdef fix :) * Add usbasp target * Update docs/flashing.md Co-Authored-By: fauxpark * Update docs/flashing.md Co-Authored-By: fauxpark * Update docs/flashing.md Co-Authored-By: fauxpark --- tmk_core/common/avr/bootloader.c | 77 ++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 34 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index 3cdcd2e426..29036f7c5a 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -65,6 +65,13 @@ #define BOOT_SIZE_1024 0b010 #define BOOT_SIZE_2048 0b000 +//compatibility between ATMega8 and ATMega88 +#if !defined (MCUCSR) + #if defined (MCUSR) + #define MCUCSR MCUSR + #endif +#endif + /** \brief Entering the Bootloader via Software * * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html @@ -149,6 +156,39 @@ void bootloader_jump(void) { while(1) {} // wait for watchdog timer to trigger + #elif defined(BOOTLOADER_USBASP) + // Taken with permission of Stephan Baerwolf from https://github.com/tinyusbboard/API/blob/master/apipage.c + wdt_enable(WDTO_15MS); + wdt_reset(); + asm volatile ( + "cli \n\t" + "ldi r29 , %[ramendhi] \n\t" + "ldi r28 , %[ramendlo] \n\t" + #if (FLASHEND>131071) + "ldi r18 , %[bootaddrhi] \n\t" + "st Y+, r18 \n\t" + #endif + "ldi r18 , %[bootaddrme] \n\t" + "st Y+, r18 \n\t" + "ldi r18 , %[bootaddrlo] \n\t" + "st Y+, r18 \n\t" + "out %[mcucsrio], __zero_reg__ \n\t" + "bootloader_startup_loop%=: \n\t" + "rjmp bootloader_startup_loop%= \n\t" + : + : [mcucsrio] "I" (_SFR_IO_ADDR(MCUCSR)), + #if (FLASHEND>131071) + [ramendhi] "M" (((RAMEND - 2) >> 8) & 0xff), + [ramendlo] "M" (((RAMEND - 2) >> 0) & 0xff), + [bootaddrhi] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >>16) & 0xff), + #else + [ramendhi] "M" (((RAMEND - 1) >> 8) & 0xff), + [ramendlo] "M" (((RAMEND - 1) >> 0) & 0xff), + #endif + [bootaddrme] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), + [bootaddrlo] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff) + ); + #else // Assume remaining boards are DFU, even if the flag isn't set #if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? @@ -172,24 +212,19 @@ void bootloader_jump(void) { } -#ifdef __AVR_ATmega32A__ - // MCUSR is actually called MCUCSR in ATmega32A - #define MCUSR MCUCSR -#endif - /* this runs before main() */ void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3"))); void bootloader_jump_after_watchdog_reset(void) { #ifndef BOOTLOADER_HALFKAY - if ((MCUSR & (1< Date: Fri, 12 Jul 2019 11:22:39 -0700 Subject: Have clang ignore the code in bootloader_size.c --- tmk_core/common/avr/bootloader_size.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader_size.c b/tmk_core/common/avr/bootloader_size.c index 0d8d534f84..a029f9321f 100644 --- a/tmk_core/common/avr/bootloader_size.c +++ b/tmk_core/common/avr/bootloader_size.c @@ -16,5 +16,6 @@ #include #include +// clang-format off // this is not valid C - it's for computing the size available on the chip -AVR_SIZE: FLASHEND + 1 - BOOTLOADER_SIZE \ No newline at end of file +AVR_SIZE: FLASHEND + 1 - BOOTLOADER_SIZE -- cgit v1.2.3 From b624f32f944acdc59dcb130674c09090c5c404cb Mon Sep 17 00:00:00 2001 From: skullY Date: Fri, 30 Aug 2019 11:19:03 -0700 Subject: clang-format changes --- tmk_core/common/avr/bootloader.c | 365 ++++++++++++++++++++++---------------- tmk_core/common/avr/sleep_led.c | 43 ++--- tmk_core/common/avr/suspend.c | 171 +++++++++--------- tmk_core/common/avr/suspend_avr.h | 32 ++-- tmk_core/common/avr/timer.c | 61 ++----- tmk_core/common/avr/timer_avr.h | 26 +-- tmk_core/common/avr/xprintf.h | 17 +- 7 files changed, 361 insertions(+), 354 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index 29036f7c5a..5f9ecc5101 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -9,10 +9,9 @@ #include #ifdef PROTOCOL_LUFA -#include +# include #endif - /** \brief Bootloader Size in *bytes* * * AVR Boot section size are defined by setting BOOTSZ fuse in fact. Consult with your MCU datasheet. @@ -57,19 +56,19 @@ #define FLASH_SIZE (FLASHEND + 1L) #if !defined(BOOTLOADER_SIZE) - uint16_t bootloader_start; +uint16_t bootloader_start; #endif -#define BOOT_SIZE_256 0b110 -#define BOOT_SIZE_512 0b100 +#define BOOT_SIZE_256 0b110 +#define BOOT_SIZE_512 0b100 #define BOOT_SIZE_1024 0b010 #define BOOT_SIZE_2048 0b000 -//compatibility between ATMega8 and ATMega88 -#if !defined (MCUCSR) - #if defined (MCUSR) - #define MCUCSR MCUSR - #endif +// compatibility between ATMega8 and ATMega88 +#if !defined(MCUCSR) +# if defined(MCUSR) +# define MCUCSR MCUSR +# endif #endif /** \brief Entering the Bootloader via Software @@ -77,163 +76,223 @@ * http://www.fourwalledcubicle.com/files/LUFA/Doc/120730/html/_page__software_bootloader_start.html */ #define BOOTLOADER_RESET_KEY 0xB007B007 -uint32_t reset_key __attribute__ ((section (".noinit,\"aw\",@nobits;"))); +uint32_t reset_key __attribute__((section(".noinit,\"aw\",@nobits;"))); /** \brief initialize MCU status by watchdog reset * * FIXME: needs doc */ void bootloader_jump(void) { +#if !defined(BOOTLOADER_SIZE) + uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); - #if !defined(BOOTLOADER_SIZE) - uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); - - if (high_fuse & BOOT_SIZE_256) { - bootloader_start = (FLASH_SIZE - 512) >> 1; - } else if (high_fuse & BOOT_SIZE_512) { - bootloader_start = (FLASH_SIZE - 1024) >> 1; - } else if (high_fuse & BOOT_SIZE_1024) { - bootloader_start = (FLASH_SIZE - 2048) >> 1; - } else { - bootloader_start = (FLASH_SIZE - 4096) >> 1; - } - #endif + if (high_fuse & BOOT_SIZE_256) { + bootloader_start = (FLASH_SIZE - 512) >> 1; + } else if (high_fuse & BOOT_SIZE_512) { + bootloader_start = (FLASH_SIZE - 1024) >> 1; + } else if (high_fuse & BOOT_SIZE_1024) { + bootloader_start = (FLASH_SIZE - 2048) >> 1; + } else { + bootloader_start = (FLASH_SIZE - 4096) >> 1; + } +#endif // Something like this might work, but it compiled larger than the block above // bootloader_start = FLASH_SIZE - (256 << (~high_fuse & 0b110 >> 1)); +#if defined(BOOTLOADER_HALFKAY) + // http://www.pjrc.com/teensy/jump_to_bootloader.html + cli(); + // disable watchdog, if enabled (it's not) + // disable all peripherals + // a shutdown call might make sense here + UDCON = 1; + USBCON = (1 << FRZCLK); // disable USB + UCSR1B = 0; + _delay_ms(5); +# if defined(__AVR_AT90USB162__) // Teensy 1.0 + EIMSK = 0; + PCICR = 0; + SPCR = 0; + ACSR = 0; + EECR = 0; + TIMSK0 = 0; + TIMSK1 = 0; + UCSR1B = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + PORTB = 0; + PORTC = 0; + PORTD = 0; + asm volatile("jmp 0x3E00"); +# elif defined(__AVR_ATmega32U4__) // Teensy 2.0 + EIMSK = 0; + PCICR = 0; + SPCR = 0; + ACSR = 0; + EECR = 0; + ADCSRA = 0; + TIMSK0 = 0; + TIMSK1 = 0; + TIMSK3 = 0; + TIMSK4 = 0; + UCSR1B = 0; + TWCR = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + DDRE = 0; + DDRF = 0; + TWCR = 0; + PORTB = 0; + PORTC = 0; + PORTD = 0; + PORTE = 0; + PORTF = 0; + asm volatile("jmp 0x7E00"); +# elif defined(__AVR_AT90USB646__) // Teensy++ 1.0 + EIMSK = 0; + PCICR = 0; + SPCR = 0; + ACSR = 0; + EECR = 0; + ADCSRA = 0; + TIMSK0 = 0; + TIMSK1 = 0; + TIMSK2 = 0; + TIMSK3 = 0; + UCSR1B = 0; + TWCR = 0; + DDRA = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + DDRE = 0; + DDRF = 0; + PORTA = 0; + PORTB = 0; + PORTC = 0; + PORTD = 0; + PORTE = 0; + PORTF = 0; + asm volatile("jmp 0xFC00"); +# elif defined(__AVR_AT90USB1286__) // Teensy++ 2.0 + EIMSK = 0; + PCICR = 0; + SPCR = 0; + ACSR = 0; + EECR = 0; + ADCSRA = 0; + TIMSK0 = 0; + TIMSK1 = 0; + TIMSK2 = 0; + TIMSK3 = 0; + UCSR1B = 0; + TWCR = 0; + DDRA = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + DDRE = 0; + DDRF = 0; + PORTA = 0; + PORTB = 0; + PORTC = 0; + PORTD = 0; + PORTE = 0; + PORTF = 0; + asm volatile("jmp 0x1FC00"); +# endif + +#elif defined(BOOTLOADER_CATERINA) + // this block may be optional + // TODO: figure it out + + uint16_t *const bootKeyPtr = (uint16_t *)0x0800; + + // Value used by Caterina bootloader use to determine whether to run the + // sketch or the bootloader programmer. + uint16_t bootKey = 0x7777; + + *bootKeyPtr = bootKey; + + // setup watchdog timeout + wdt_enable(WDTO_60MS); - #if defined(BOOTLOADER_HALFKAY) - // http://www.pjrc.com/teensy/jump_to_bootloader.html - cli(); - // disable watchdog, if enabled (it's not) - // disable all peripherals - // a shutdown call might make sense here - UDCON = 1; - USBCON = (1<131071) - "ldi r18 , %[bootaddrhi] \n\t" - "st Y+, r18 \n\t" - #endif - "ldi r18 , %[bootaddrme] \n\t" - "st Y+, r18 \n\t" - "ldi r18 , %[bootaddrlo] \n\t" - "st Y+, r18 \n\t" - "out %[mcucsrio], __zero_reg__ \n\t" - "bootloader_startup_loop%=: \n\t" - "rjmp bootloader_startup_loop%= \n\t" - : - : [mcucsrio] "I" (_SFR_IO_ADDR(MCUCSR)), - #if (FLASHEND>131071) - [ramendhi] "M" (((RAMEND - 2) >> 8) & 0xff), - [ramendlo] "M" (((RAMEND - 2) >> 0) & 0xff), - [bootaddrhi] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >>16) & 0xff), - #else - [ramendhi] "M" (((RAMEND - 1) >> 8) & 0xff), - [ramendlo] "M" (((RAMEND - 1) >> 0) & 0xff), - #endif - [bootaddrme] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), - [bootaddrlo] "M" ((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff) - ); - - #else // Assume remaining boards are DFU, even if the flag isn't set - - #if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? - UDCON = 1; - USBCON = (1< 131071) + "ldi r18 , %[bootaddrhi] \n\t" + "st Y+, r18 \n\t" +# endif + "ldi r18 , %[bootaddrme] \n\t" + "st Y+, r18 \n\t" + "ldi r18 , %[bootaddrlo] \n\t" + "st Y+, r18 \n\t" + "out %[mcucsrio], __zero_reg__ \n\t" + "bootloader_startup_loop%=: \n\t" + "rjmp bootloader_startup_loop%= \n\t" + : + : [ mcucsrio ] "I"(_SFR_IO_ADDR(MCUCSR)), +# if (FLASHEND > 131071) + [ ramendhi ] "M"(((RAMEND - 2) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 2) >> 0) & 0xff), [ bootaddrhi ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff), +# else + [ ramendhi ] "M"(((RAMEND - 1) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 1) >> 0) & 0xff), +# endif + [ bootaddrme ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [ bootaddrlo ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)); + +#else // Assume remaining boards are DFU, even if the flag isn't set + +# if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? + UDCON = 1; + USBCON = (1 << FRZCLK); // disable USB + UCSR1B = 0; + _delay_ms(5); // 5 seems to work fine +# endif + +# ifdef BOOTLOADER_BOOTLOADHID + // force bootloadHID to stay in bootloader mode, so that it waits + // for a new firmware to be flashed + eeprom_write_byte((uint8_t *)1, 0x00); +# endif + + // watchdog reset + reset_key = BOOTLOADER_RESET_KEY; + wdt_enable(WDTO_250MS); + for (;;) + ; +#endif } /* this runs before main() */ -void bootloader_jump_after_watchdog_reset(void) __attribute__ ((used, naked, section (".init3"))); -void bootloader_jump_after_watchdog_reset(void) -{ - #ifndef BOOTLOADER_HALFKAY - if ((MCUCSR & (1<> 1))(); - #else - asm("ijmp" :: "z" (bootloader_start)); - #endif - } - #endif +void bootloader_jump_after_watchdog_reset(void) __attribute__((used, naked, section(".init3"))); +void bootloader_jump_after_watchdog_reset(void) { +#ifndef BOOTLOADER_HALFKAY + if ((MCUCSR & (1 << WDRF)) && reset_key == BOOTLOADER_RESET_KEY) { + reset_key = 0; + + // My custom USBasploader requires this to come up. + MCUCSR = 0; + + // Seems like Teensy halfkay loader requires clearing WDRF and disabling watchdog. + MCUCSR &= ~(1 << WDRF); + wdt_disable(); + +// This is compled into 'icall', address should be in word unit, not byte. +# ifdef BOOTLOADER_SIZE + ((void (*)(void))((FLASH_SIZE - BOOTLOADER_SIZE) >> 1))(); +# else + asm("ijmp" ::"z"(bootloader_start)); +# endif + } +#endif } diff --git a/tmk_core/common/avr/sleep_led.c b/tmk_core/common/avr/sleep_led.c index 0cb774c81f..61fa70dc3e 100644 --- a/tmk_core/common/avr/sleep_led.c +++ b/tmk_core/common/avr/sleep_led.c @@ -16,14 +16,13 @@ * 256*64 interrupts/second * F_CPU/(256*64) clocks/interrupt */ -#define SLEEP_LED_TIMER_TOP F_CPU/(256*64) +#define SLEEP_LED_TIMER_TOP F_CPU / (256 * 64) /** \brief Sleep LED initialization * * FIXME: needs doc */ -void sleep_led_init(void) -{ +void sleep_led_init(void) { /* Timer1 setup */ /* CTC mode */ TCCR1B |= _BV(WGM12); @@ -32,17 +31,16 @@ void sleep_led_init(void) /* Set TOP value */ uint8_t sreg = SREG; cli(); - OCR1AH = (SLEEP_LED_TIMER_TOP>>8)&0xff; - OCR1AL = SLEEP_LED_TIMER_TOP&0xff; - SREG = sreg; + OCR1AH = (SLEEP_LED_TIMER_TOP >> 8) & 0xff; + OCR1AL = SLEEP_LED_TIMER_TOP & 0xff; + SREG = sreg; } /** \brief Sleep LED enable * * FIXME: needs doc */ -void sleep_led_enable(void) -{ +void sleep_led_enable(void) { /* Enable Compare Match Interrupt */ TIMSK1 |= _BV(OCIE1A); } @@ -51,8 +49,7 @@ void sleep_led_enable(void) * * FIXME: needs doc */ -void sleep_led_disable(void) -{ +void sleep_led_disable(void) { /* Disable Compare Match Interrupt */ TIMSK1 &= ~_BV(OCIE1A); } @@ -61,13 +58,11 @@ void sleep_led_disable(void) * * FIXME: needs doc */ -void sleep_led_toggle(void) -{ +void sleep_led_toggle(void) { /* Disable Compare Match Interrupt */ TIMSK1 ^= _BV(OCIE1A); } - /** \brief Breathing Sleep LED brighness(PWM On period) table * * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle @@ -75,15 +70,9 @@ void sleep_led_toggle(void) * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i } */ -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 -}; +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(TIMER1_COMPA_vect) { /* Software PWM * timer:1111 1111 1111 1111 * \_____/\/ \_______/____ count(0-255) @@ -93,17 +82,17 @@ ISR(TIMER1_COMPA_vect) static union { uint16_t row; struct { - uint8_t count:8; - uint8_t duration:2; - uint8_t index:6; + uint8_t count : 8; + uint8_t duration : 2; + uint8_t index : 6; } pwm; - } timer = { .row = 0 }; + } timer = {.row = 0}; timer.row++; - + // LED on if (timer.pwm.count == 0) { - led_set(1< #include - -#define wdt_intr_enable(value) \ -__asm__ __volatile__ ( \ - "in __tmp_reg__,__SREG__" "\n\t" \ - "cli" "\n\t" \ - "wdr" "\n\t" \ - "sts %0,%1" "\n\t" \ - "out __SREG__,__tmp_reg__" "\n\t" \ - "sts %0,%2" "\n\t" \ - : /* no outputs */ \ - : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ - "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ - "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ - _BV(WDIE) | (value & 0x07)) ) \ - : "r0" \ -) +#define wdt_intr_enable(value) \ + __asm__ __volatile__("in __tmp_reg__,__SREG__" \ + "\n\t" \ + "cli" \ + "\n\t" \ + "wdr" \ + "\n\t" \ + "sts %0,%1" \ + "\n\t" \ + "out __SREG__,__tmp_reg__" \ + "\n\t" \ + "sts %0,%2" \ + "\n\t" \ + : /* no outputs */ \ + : "M"(_SFR_MEM_ADDR(_WD_CONTROL_REG)), "r"(_BV(_WD_CHANGE_BIT) | _BV(WDE)), "r"((uint8_t)((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDIE) | (value & 0x07))) \ + : "r0") #endif diff --git a/tmk_core/common/avr/timer.c b/tmk_core/common/avr/timer.c index b7d4f060ef..63ec549dff 100644 --- a/tmk_core/common/avr/timer.c +++ b/tmk_core/common/avr/timer.c @@ -22,7 +22,6 @@ along with this program. If not, see . #include "timer_avr.h" #include "timer.h" - // counter resolution 1ms // NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }} volatile uint32_t timer_count; @@ -31,8 +30,7 @@ volatile uint32_t timer_count; * * FIXME: needs doc */ -void timer_init(void) -{ +void timer_init(void) { #if TIMER_PRESCALER == 1 uint8_t prescaler = 0x01; #elif TIMER_PRESCALER == 8 @@ -44,7 +42,7 @@ void timer_init(void) #elif TIMER_PRESCALER == 1024 uint8_t prescaler = 0x05; #else -# error "Timer prescaler value is NOT vaild." +# error "Timer prescaler value is NOT vaild." #endif #ifndef __AVR_ATmega32A__ @@ -53,13 +51,13 @@ void timer_init(void) TCCR0B = prescaler; - OCR0A = TIMER_RAW_TOP; - TIMSK0 = (1<. #include #ifndef TIMER_PRESCALER -# if F_CPU > 16000000 -# define TIMER_PRESCALER 256 -# elif F_CPU > 2000000 -# define TIMER_PRESCALER 64 -# elif F_CPU > 250000 -# define TIMER_PRESCALER 8 -# else -# define TIMER_PRESCALER 1 -# endif +# if F_CPU > 16000000 +# define TIMER_PRESCALER 256 +# elif F_CPU > 2000000 +# define TIMER_PRESCALER 64 +# elif F_CPU > 250000 +# define TIMER_PRESCALER 8 +# else +# define TIMER_PRESCALER 1 +# endif #endif -#define TIMER_RAW_FREQ (F_CPU/TIMER_PRESCALER) -#define TIMER_RAW TCNT0 -#define TIMER_RAW_TOP (TIMER_RAW_FREQ/1000) +#define TIMER_RAW_FREQ (F_CPU / TIMER_PRESCALER) +#define TIMER_RAW TCNT0 +#define TIMER_RAW_TOP (TIMER_RAW_FREQ / 1000) #if (TIMER_RAW_TOP > 255) -# error "Timer0 can't count 1ms at this clock freq. Use larger prescaler." +# error "Timer0 can't count 1ms at this clock freq. Use larger prescaler." #endif #endif diff --git a/tmk_core/common/avr/xprintf.h b/tmk_core/common/avr/xprintf.h index 08d9f93a0c..70e0f8e48a 100644 --- a/tmk_core/common/avr/xprintf.h +++ b/tmk_core/common/avr/xprintf.h @@ -13,7 +13,7 @@ extern "C" { #endif extern void (*xfunc_out)(uint8_t); -#define xdev_out(func) xfunc_out = (void(*)(uint8_t))(func) +#define xdev_out(func) xfunc_out = (void (*)(uint8_t))(func) /* This is a pointer to user defined output function. It must be initialized before using this modle. @@ -25,13 +25,11 @@ void xputc(char chr); All outputs from this module are output via this function. */ - /*-----------------------------------------------------------------------------*/ void xputs(const char *string_p); /* The string placed in the ROM is forwarded to xputc() directly. -*/ - + */ /*-----------------------------------------------------------------------------*/ void xitoa(long value, char radix, char width); @@ -49,13 +47,12 @@ void xitoa(long value, char radix, char width); 0x55 2 -8 "01010101" */ - /*-----------------------------------------------------------------------------*/ -#define xprintf(format, ...) __xprintf(PSTR(format), ##__VA_ARGS__) -#define xsprintf(str, format, ...) __xsprintf(str, PSTR(format), ##__VA_ARGS__) -#define xfprintf(func, format, ...) __xfprintf(func, PSTR(format), ##__VA_ARGS__) +#define xprintf(format, ...) __xprintf(PSTR(format), ##__VA_ARGS__) +#define xsprintf(str, format, ...) __xsprintf(str, PSTR(format), ##__VA_ARGS__) +#define xfprintf(func, format, ...) __xfprintf(func, PSTR(format), ##__VA_ARGS__) -void __xprintf(const char *format_p, ...); /* Send formatted string to the registered device */ +void __xprintf(const char *format_p, ...); /* Send formatted string to the registered device */ // void __xsprintf(char*, const char *format_p, ...); /* Put formatted string to the memory */ // void __xfprintf(void(*func)(uint8_t), const char *format_p, ...); /* Send formatted string to the specified device */ @@ -84,7 +81,6 @@ void __xprintf(const char *format_p, ...); /* Send formatted string to the regis */ - /*-----------------------------------------------------------------------------*/ char xatoi(char **str, long *ret); @@ -108,4 +104,3 @@ char xatoi(char **str, long *ret); #endif #endif - -- cgit v1.2.3 From 91bd2117df4cc4d2be6c840103614f2245e54bd1 Mon Sep 17 00:00:00 2001 From: fauxpark Date: Sun, 8 Sep 2019 01:12:46 +1000 Subject: Banish some more magic numbers (#6662) --- tmk_core/common/avr/bootloader.c | 11 +++-------- tmk_core/common/avr/timer.c | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 19 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index 5f9ecc5101..ca9746f327 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -59,11 +59,6 @@ uint16_t bootloader_start; #endif -#define BOOT_SIZE_256 0b110 -#define BOOT_SIZE_512 0b100 -#define BOOT_SIZE_1024 0b010 -#define BOOT_SIZE_2048 0b000 - // compatibility between ATMega8 and ATMega88 #if !defined(MCUCSR) # if defined(MCUSR) @@ -86,11 +81,11 @@ void bootloader_jump(void) { #if !defined(BOOTLOADER_SIZE) uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); - if (high_fuse & BOOT_SIZE_256) { + if (high_fuse & ~(FUSE_BOOTSZ0 & FUSE_BOOTSZ1)) { bootloader_start = (FLASH_SIZE - 512) >> 1; - } else if (high_fuse & BOOT_SIZE_512) { + } else if (high_fuse & ~(FUSE_BOOTSZ1)) { bootloader_start = (FLASH_SIZE - 1024) >> 1; - } else if (high_fuse & BOOT_SIZE_1024) { + } else if (high_fuse & ~(FUSE_BOOTSZ0)) { bootloader_start = (FLASH_SIZE - 2048) >> 1; } else { bootloader_start = (FLASH_SIZE - 4096) >> 1; diff --git a/tmk_core/common/avr/timer.c b/tmk_core/common/avr/timer.c index 63ec549dff..88fa1dfa65 100644 --- a/tmk_core/common/avr/timer.c +++ b/tmk_core/common/avr/timer.c @@ -32,33 +32,32 @@ volatile uint32_t timer_count; */ void timer_init(void) { #if TIMER_PRESCALER == 1 - uint8_t prescaler = 0x01; + uint8_t prescaler = _BV(CS00); #elif TIMER_PRESCALER == 8 - uint8_t prescaler = 0x02; + uint8_t prescaler = _BV(CS01); #elif TIMER_PRESCALER == 64 - uint8_t prescaler = 0x03; + uint8_t prescaler = _BV(CS00) | _BV(CS01); #elif TIMER_PRESCALER == 256 - uint8_t prescaler = 0x04; + uint8_t prescaler = _BV(CS02); #elif TIMER_PRESCALER == 1024 - uint8_t prescaler = 0x05; + uint8_t prescaler = _BV(CS00) | _BV(CS02); #else -# error "Timer prescaler value is NOT vaild." +# error "Timer prescaler value is not valid" #endif #ifndef __AVR_ATmega32A__ // Timer0 CTC mode - TCCR0A = 0x02; - + TCCR0A = _BV(WGM01); TCCR0B = prescaler; OCR0A = TIMER_RAW_TOP; - TIMSK0 = (1 << OCIE0A); + TIMSK0 = _BV(OCIE0A); #else // Timer0 CTC mode - TCCR0 = (1 << WGM01) | prescaler; + TCCR0 = _BV(WGM01) | prescaler; OCR0 = TIMER_RAW_TOP; - TIMSK = (1 << OCIE0); + TIMSK = _BV(OCIE0); #endif } -- cgit v1.2.3 From abfd6ed9613013d3c9f15da8b575a902d9bcf274 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 17 Oct 2019 17:48:58 +0100 Subject: Move tmk_core/common/backlight to quantum/backlight (#6710) * Move tmk_core/common/backlight to quantum/backlight * Add guards to backlight inclusion * Add guards to backlight inclusion * Update backlight guards on clueboard/60 * Use full paths to avoid vpath issues --- tmk_core/common/avr/suspend.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index 574000fcd8..c59c196880 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -4,7 +4,6 @@ #include #include "matrix.h" #include "action.h" -#include "backlight.h" #include "suspend_avr.h" #include "suspend.h" #include "timer.h" @@ -16,6 +15,10 @@ # include "lufa.h" #endif +#ifdef BACKLIGHT_ENABLE +# include "backlight.h" +#endif + #ifdef AUDIO_ENABLE # include "audio.h" #endif /* AUDIO_ENABLE */ -- cgit v1.2.3 From dbbab409812ea7c579760e2973399245c21ff843 Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Tue, 10 Mar 2020 17:46:03 +0900 Subject: Refactor rgblight_reconfig.h (#7773) * Moved contents of rgblight_reconfig.h to rgblight_post_config.h. In #3582, rgblight_reconfig.h had to be newly created. Now, the build system of qmk_firmware has a post_cofig feature, so that what was done in rgblight_reconfig.h can now be realized in rgblight_post_config.h. **This commit does not change the build result.** Testing script ```shell # build on master git checkout master echo master > /tmp/master_md5.txt # RGBLIGHT_ENABLE = no make HELIX=verbose helix/rev2:default:clean make HELIX=verbose helix/rev2:default md5 helix_rev2_default.hex >> /tmp/master_md5.txt # RGBLIGHT_ENABLE = yes, with animations make HELIX=verbose helix/rev2/back:default:clean make HELIX=verbose helix/rev2/back:default md5 helix_rev2_back_default.hex >> /tmp/master_md5.txt # RGBLIGHT_ENABLE = yes, without animations make HELIX=verbose,no_ani helix/rev2/back:default:clean make HELIX=verbose,no_ani helix/rev2/back:default md5 helix_rev2_back_default.hex >> /tmp/master_md5.txt # build on refactor_rgblight_reconfig.h git checkout refactor_rgblight_reconfig.h echo refactor_rgblight_reconfig.h > /tmp/branch_md5.txt # RGBLIGHT_ENABLE = no make HELIX=verbose helix/rev2:default:clean make HELIX=verbose helix/rev2:default md5 helix_rev2_default.hex >> /tmp/branch_md5.txt # RGBLIGHT_ENABLE = yes, with animations make HELIX=verbose helix/rev2/back:default:clean make HELIX=verbose helix/rev2/back:default md5 helix_rev2_back_default.hex >> /tmp/branch_md5.txt # RGBLIGHT_ENABLE = yes, without animations make HELIX=verbose,no_ani helix/rev2/back:default:clean make HELIX=verbose,no_ani helix/rev2/back:default md5 helix_rev2_back_default.hex >> /tmp/branch_md5.txt diff -u /tmp/master_md5.txt /tmp/branch_md5.txt ``` Test result: ``` --- /tmp/master_md5.txt 2020-01-03 15:42:22.000000000 +0900 +++ /tmp/branch_md5.txt 2020-01-03 15:42:42.000000000 +0900 @@ -1,4 +1,4 @@ -master +refactor_rgblight_reconfig.h MD5 (helix_rev2_default.hex) = f360032edd522448366d471d8f4f8181 MD5 (helix_rev2_back_default.hex) = 0c663acc6cccc44476b3b969ad22a48f MD5 (helix_rev2_back_default.hex) = e66b1195ff6d38e6e22c975b8ae42fd3 ``` * Expressions that are too long are difficult to read, so wrap them. * Edit the expression again * remove `defined(RGBLIGHT_ANIMATIONS)` in `tmk_core/common/*/suspend.c`, `tmk_core/protocol/*/main.c` move contents of rgblight_reconfig.h to rgblight.h. The following changes were made to rgblight.h. ```diff +#ifdef RGBLIGHT_USE_TIMER void rgblight_task(void); void rgblight_timer_init(void); void rgblight_timer_enable(void); void rgblight_timer_disable(void); void rgblight_timer_toggle(void); +#else +#define rgblight_task() +#define rgblight_timer_init() +#define rgblight_timer_enable() +#define rgblight_timer_disable() +#define rgblight_timer_toggle() +#endif ``` The following changes were made to tmk_core/common/avr/suspend.c, tmk_core/common/chibios/suspend.c, tmk_core/protocol/chibios/main.c, tmk_core/protocol/lufa/lufa.c, tmk_core/protocol/vusb/main.c. ```diff -# ifdef RGBLIGHT_ANIMATIONS rgblight_timer_enable(); -# endif ``` ```diff -#if defined(RGBLIGHT_ANIMATIONS) && defined(RGBLIGHT_ENABLE) +#if defined(RGBLIGHT_ENABLE) rgblight_task(); #endif ``` * remove 'defined(RGBLIGHT_ANIMATIONS)' in tmk_core/common/keyboard.c Co-authored-by: Joel Challis --- tmk_core/common/avr/suspend.c | 22 ---------------------- tmk_core/common/avr/suspend_avr.h | 33 +++++++++++++++++---------------- 2 files changed, 17 insertions(+), 38 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index c59c196880..11ac146eb8 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -9,7 +9,6 @@ #include "timer.h" #include "led.h" #include "host.h" -#include "rgblight_reconfig.h" #ifdef PROTOCOL_LUFA # include "lufa.h" @@ -30,23 +29,6 @@ static bool rgblight_enabled; static bool is_suspended; #endif -#define wdt_intr_enable(value) \ - __asm__ __volatile__("in __tmp_reg__,__SREG__" \ - "\n\t" \ - "cli" \ - "\n\t" \ - "wdr" \ - "\n\t" \ - "sts %0,%1" \ - "\n\t" \ - "out __SREG__,__tmp_reg__" \ - "\n\t" \ - "sts %0,%2" \ - "\n\t" \ - : /* no outputs */ \ - : "M"(_SFR_MEM_ADDR(_WD_CONTROL_REG)), "r"(_BV(_WD_CHANGE_BIT) | _BV(WDE)), "r"((uint8_t)((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDIE) | (value & 0x07))) \ - : "r0") - /** \brief Suspend idle * * FIXME: needs doc @@ -122,9 +104,7 @@ static void power_down(uint8_t wdto) { // stop_all_notes(); # endif /* AUDIO_ENABLE */ # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) -# ifdef RGBLIGHT_ANIMATIONS rgblight_timer_disable(); -# endif if (!is_suspended) { is_suspended = true; rgblight_enabled = rgblight_config.enable; @@ -204,9 +184,7 @@ void suspend_wakeup_init(void) { # endif rgblight_enable_noeeprom(); } -# ifdef RGBLIGHT_ANIMATIONS rgblight_timer_enable(); -# endif #endif suspend_wakeup_init_kb(); } diff --git a/tmk_core/common/avr/suspend_avr.h b/tmk_core/common/avr/suspend_avr.h index d73852ba11..e4cc0be505 100644 --- a/tmk_core/common/avr/suspend_avr.h +++ b/tmk_core/common/avr/suspend_avr.h @@ -7,21 +7,22 @@ #include #include -#define wdt_intr_enable(value) \ - __asm__ __volatile__("in __tmp_reg__,__SREG__" \ - "\n\t" \ - "cli" \ - "\n\t" \ - "wdr" \ - "\n\t" \ - "sts %0,%1" \ - "\n\t" \ - "out __SREG__,__tmp_reg__" \ - "\n\t" \ - "sts %0,%2" \ - "\n\t" \ - : /* no outputs */ \ - : "M"(_SFR_MEM_ADDR(_WD_CONTROL_REG)), "r"(_BV(_WD_CHANGE_BIT) | _BV(WDE)), "r"((uint8_t)((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDIE) | (value & 0x07))) \ - : "r0") +// clang-format off +#define wdt_intr_enable(value) \ +__asm__ __volatile__ ( \ + "in __tmp_reg__,__SREG__" "\n\t" \ + "cli" "\n\t" \ + "wdr" "\n\t" \ + "sts %0,%1" "\n\t" \ + "out __SREG__,__tmp_reg__" "\n\t" \ + "sts %0,%2" "\n\t" \ + : /* no outputs */ \ + : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ + "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ + "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ + _BV(WDIE) | (value & 0x07)) ) \ + : "r0" \ +) +// clang-format on #endif -- cgit v1.2.3 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 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/sleep_led.c b/tmk_core/common/avr/sleep_led.c index 61fa70dc3e..63dcc2afd9 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) -- cgit v1.2.3 From 9c4bf0ac4aed4ab315f27bfa5d2f54b0dd931138 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 2 Apr 2020 22:23:57 +0100 Subject: Initial support for ATtiny85 (#8632) * Initial support for ATtiny85 * Update mcu selection --- tmk_core/common/avr/bootloader.c | 2 +- tmk_core/common/avr/timer.c | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index ca9746f327..32f69ae8b4 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -247,7 +247,7 @@ void bootloader_jump(void) { #else // Assume remaining boards are DFU, even if the flag isn't set -# if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? +# if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny85__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? UDCON = 1; USBCON = (1 << FRZCLK); // disable USB UCSR1B = 0; diff --git a/tmk_core/common/avr/timer.c b/tmk_core/common/avr/timer.c index 88fa1dfa65..c2e6c6e081 100644 --- a/tmk_core/common/avr/timer.c +++ b/tmk_core/common/avr/timer.c @@ -45,19 +45,26 @@ void timer_init(void) { # error "Timer prescaler value is not valid" #endif -#ifndef __AVR_ATmega32A__ +#if defined(__AVR_ATmega32A__) + // Timer0 CTC mode + TCCR0 = _BV(WGM01) | prescaler; + + OCR0 = TIMER_RAW_TOP; + TIMSK = _BV(OCIE0); +#elif defined(__AVR_ATtiny85__) // Timer0 CTC mode TCCR0A = _BV(WGM01); TCCR0B = prescaler; - OCR0A = TIMER_RAW_TOP; - TIMSK0 = _BV(OCIE0A); + OCR0A = TIMER_RAW_TOP; + TIMSK = _BV(OCIE0A); #else // Timer0 CTC mode - TCCR0 = _BV(WGM01) | prescaler; + TCCR0A = _BV(WGM01); + TCCR0B = prescaler; - OCR0 = TIMER_RAW_TOP; - TIMSK = _BV(OCIE0); + OCR0A = TIMER_RAW_TOP; + TIMSK0 = _BV(OCIE0A); #endif } -- cgit v1.2.3 From 59188bf29d10cdf0500811a68f611ab6a724494f Mon Sep 17 00:00:00 2001 From: QMK Bot Date: Thu, 2 Apr 2020 21:59:37 +0000 Subject: format code according to conventions [skip ci] --- tmk_core/common/avr/bootloader.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index 32f69ae8b4..7e5d2b0579 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -237,13 +237,13 @@ void bootloader_jump(void) { "bootloader_startup_loop%=: \n\t" "rjmp bootloader_startup_loop%= \n\t" : - : [ mcucsrio ] "I"(_SFR_IO_ADDR(MCUCSR)), + : [mcucsrio] "I"(_SFR_IO_ADDR(MCUCSR)), # if (FLASHEND > 131071) - [ ramendhi ] "M"(((RAMEND - 2) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 2) >> 0) & 0xff), [ bootaddrhi ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff), + [ramendhi] "M"(((RAMEND - 2) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 2) >> 0) & 0xff), [bootaddrhi] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff), # else - [ ramendhi ] "M"(((RAMEND - 1) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 1) >> 0) & 0xff), + [ramendhi] "M"(((RAMEND - 1) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 1) >> 0) & 0xff), # endif - [ bootaddrme ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [ bootaddrlo ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)); + [bootaddrme] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [bootaddrlo] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)); #else // Assume remaining boards are DFU, even if the flag isn't set -- cgit v1.2.3 From 7aa4cc9603b8cdb0ea3ede753eff7d07a86a18b0 Mon Sep 17 00:00:00 2001 From: itsnoteasy Date: Wed, 10 Jun 2020 22:23:11 +0100 Subject: adds support for the atmega328 (#9043) Co-authored-by: Ryan --- tmk_core/common/avr/bootloader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index 7e5d2b0579..a1db55da93 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -247,7 +247,7 @@ void bootloader_jump(void) { #else // Assume remaining boards are DFU, even if the flag isn't set -# if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATtiny85__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? +# if !(defined(__AVR_ATmega32A__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) || defined(__AVR_ATtiny85__)) // no USB - maybe BOOTLOADER_BOOTLOADHID instead though? UDCON = 1; USBCON = (1 << FRZCLK); // disable USB UCSR1B = 0; -- cgit v1.2.3 From 1d1d5da43f86d9dded47c66afec94991d623f114 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 26 Dec 2020 15:56:11 +1100 Subject: Change include guards in tmk_core/ and drivers/ to pragma once (#11240) --- tmk_core/common/avr/suspend_avr.h | 5 +---- tmk_core/common/avr/timer_avr.h | 5 +---- tmk_core/common/avr/xprintf.h | 5 +---- 3 files changed, 3 insertions(+), 12 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/suspend_avr.h b/tmk_core/common/avr/suspend_avr.h index e4cc0be505..6df048f3bb 100644 --- a/tmk_core/common/avr/suspend_avr.h +++ b/tmk_core/common/avr/suspend_avr.h @@ -1,5 +1,4 @@ -#ifndef SUSPEND_AVR_H -#define SUSPEND_AVR_H +#pragma once #include #include @@ -24,5 +23,3 @@ __asm__ __volatile__ ( \ : "r0" \ ) // clang-format on - -#endif diff --git a/tmk_core/common/avr/timer_avr.h b/tmk_core/common/avr/timer_avr.h index 9aea21c651..c1b726bd01 100644 --- a/tmk_core/common/avr/timer_avr.h +++ b/tmk_core/common/avr/timer_avr.h @@ -15,8 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifndef TIMER_AVR_H -#define TIMER_AVR_H 1 +#pragma once #include @@ -38,5 +37,3 @@ along with this program. If not, see . #if (TIMER_RAW_TOP > 255) # error "Timer0 can't count 1ms at this clock freq. Use larger prescaler." #endif - -#endif diff --git a/tmk_core/common/avr/xprintf.h b/tmk_core/common/avr/xprintf.h index 70e0f8e48a..80834f1714 100644 --- a/tmk_core/common/avr/xprintf.h +++ b/tmk_core/common/avr/xprintf.h @@ -2,8 +2,7 @@ Extended itoa, puts and printf (C)ChaN, 2011 -----------------------------------------------------------------------------*/ -#ifndef XPRINTF_H -#define XPRINTF_H +#pragma once #include #include @@ -102,5 +101,3 @@ char xatoi(char **str, long *ret); #ifdef __cplusplus } #endif - -#endif -- cgit v1.2.3 From 1b7b72c0e96856d2b9f73f705787af3426662bcf Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 28 Dec 2020 04:36:32 +1100 Subject: Ensure single newline at EOF for core files (#11310) --- tmk_core/common/avr/xprintf.S | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/xprintf.S b/tmk_core/common/avr/xprintf.S index 06434b98d9..c5a414c35c 100644 --- a/tmk_core/common/avr/xprintf.S +++ b/tmk_core/common/avr/xprintf.S @@ -450,7 +450,7 @@ xatoi: brcs 70f ;/ cpi r22, 10 ;if(r22 >= 10) { brcs 53f ; r22 -= 7; - subi r22, 7 ; if(r22 < 10) + subi r22, 7 ; if(r22 < 10) cpi r22, 10 ; brcs 70f ;} 53: cp r22, r25 ;if(r22 >= r25) error; @@ -496,5 +496,3 @@ xatoi: ret .endfunc #endif - - -- cgit v1.2.3 From 411b92e206746faa0b2264a155d5c9a64cf38a37 Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 30 Dec 2020 20:58:57 +1100 Subject: Remove useless wait in AVR suspend code (#11352) --- tmk_core/common/avr/suspend.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index 11ac146eb8..aa0d42680a 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -179,9 +179,6 @@ void suspend_wakeup_init(void) { #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) is_suspended = false; if (rgblight_enabled) { -# ifdef BOOTLOADER_TEENSY - wait_ms(10); -# endif rgblight_enable_noeeprom(); } rgblight_timer_enable(); -- cgit v1.2.3 From e7db582e356a961ba608ef441f201d879ec8d740 Mon Sep 17 00:00:00 2001 From: Dasky <32983009+daskygit@users.noreply.github.com> Date: Tue, 5 Jan 2021 05:53:37 +0000 Subject: Set avr's bootloader_jump function to be weak (#11418) Co-authored-by: Dasky --- tmk_core/common/avr/bootloader.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index a1db55da93..c0272903b8 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -77,7 +77,7 @@ uint32_t reset_key __attribute__((section(".noinit,\"aw\",@nobits;"))); * * FIXME: needs doc */ -void bootloader_jump(void) { +__attribute__((weak)) void bootloader_jump(void) { #if !defined(BOOTLOADER_SIZE) uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); -- cgit v1.2.3 From 4d96b85b6f39e89bdcc74853958f91153f27bfc1 Mon Sep 17 00:00:00 2001 From: Joshua Diamond Date: Mon, 11 Jan 2021 03:07:09 -0500 Subject: Lighting Layers should be disabled when suspended (#11442) * Lighting Layers should be disabled when suspended * bugfixes --- tmk_core/common/avr/suspend.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index aa0d42680a..86c3df040a 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -24,9 +24,6 @@ #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) # include "rgblight.h" -extern rgblight_config_t rgblight_config; -static bool rgblight_enabled; -static bool is_suspended; #endif /** \brief Suspend idle @@ -104,12 +101,7 @@ static void power_down(uint8_t wdto) { // stop_all_notes(); # endif /* AUDIO_ENABLE */ # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) - rgblight_timer_disable(); - if (!is_suspended) { - is_suspended = true; - rgblight_enabled = rgblight_config.enable; - rgblight_disable_noeeprom(); - } + rgblight_suspend(); # endif suspend_power_down_kb(); @@ -177,11 +169,7 @@ void suspend_wakeup_init(void) { #endif led_set(host_keyboard_leds()); #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) - is_suspended = false; - if (rgblight_enabled) { - rgblight_enable_noeeprom(); - } - rgblight_timer_enable(); + rgblight_wakeup(); #endif suspend_wakeup_init_kb(); } -- cgit v1.2.3 From 31c57aab35e6fd49c4c8336f449419afe7630e93 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Mon, 18 Jan 2021 05:12:15 +1100 Subject: `qmk cformat` --- tmk_core/common/avr/bootloader.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index c0272903b8..4e3a27022d 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -237,13 +237,13 @@ __attribute__((weak)) void bootloader_jump(void) { "bootloader_startup_loop%=: \n\t" "rjmp bootloader_startup_loop%= \n\t" : - : [mcucsrio] "I"(_SFR_IO_ADDR(MCUCSR)), + : [ mcucsrio ] "I"(_SFR_IO_ADDR(MCUCSR)), # if (FLASHEND > 131071) - [ramendhi] "M"(((RAMEND - 2) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 2) >> 0) & 0xff), [bootaddrhi] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff), + [ ramendhi ] "M"(((RAMEND - 2) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 2) >> 0) & 0xff), [ bootaddrhi ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff), # else - [ramendhi] "M"(((RAMEND - 1) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 1) >> 0) & 0xff), + [ ramendhi ] "M"(((RAMEND - 1) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 1) >> 0) & 0xff), # endif - [bootaddrme] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [bootaddrlo] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)); + [ bootaddrme ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [ bootaddrlo ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)); #else // Assume remaining boards are DFU, even if the flag isn't set -- cgit v1.2.3 From ae4ee7553abfaa2149fcea04c3cbee20f3b8c7a5 Mon Sep 17 00:00:00 2001 From: Joshua Diamond Date: Sun, 31 Jan 2021 17:25:55 -0500 Subject: Stop sounds when suspended (#11553) * fix stopping audio on suspend vs. startup sound * trim firmware size * fix stuck audio on startup (ARM) --- tmk_core/common/avr/suspend.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index 86c3df040a..9db0e0064a 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -97,8 +97,7 @@ static void power_down(uint8_t wdto) { led_set(leds_off); # ifdef AUDIO_ENABLE - // This sometimes disables the start-up noise, so it's been disabled - // stop_all_notes(); + stop_all_notes(); # endif /* AUDIO_ENABLE */ # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) rgblight_suspend(); @@ -157,6 +156,7 @@ __attribute__((weak)) void suspend_wakeup_init_user(void) {} * FIXME: needs doc */ __attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); } + /** \brief run immediately after wakeup * * FIXME: needs doc -- cgit v1.2.3 From 9a4618b05b9f1093908c2153c719c5eb5d4a79ee Mon Sep 17 00:00:00 2001 From: Joshua Diamond Date: Mon, 1 Feb 2021 19:12:41 -0500 Subject: Address wake from sleep instability (#11450) * resolve race condition between suspend and wake in LUFA * avoid multiple calls to suspend_power_down() / suspend_wakeup_init() * Remove duplicate suspend_power_down_kb() call * pause on wakeup to wait for USB state to settle * need the repeated suspend_power_down() (that's where the sleep is) * more efficient implementation * fine tune the pause after sending wakeup * speculative chibios version of pause-after-wake * make wakeup delay configurable, and adjust value * better location for wakeup delay --- tmk_core/common/avr/suspend.c | 1 - 1 file changed, 1 deletion(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index 9db0e0064a..b784a0835d 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -102,7 +102,6 @@ static void power_down(uint8_t wdto) { # if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) rgblight_suspend(); # endif - suspend_power_down_kb(); // TODO: more power saving // See PicoPower application note -- cgit v1.2.3 From de8caf708c1a9a80527a04be620ed3969262e50b Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sun, 14 Feb 2021 00:51:06 +0000 Subject: Split gpio and atomic to platform (#11792) --- tmk_core/common/avr/atomic_util.h | 22 +++++++ tmk_core/common/avr/gpio.h | 34 ++++++++++ tmk_core/common/avr/pin_defs.h | 128 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 tmk_core/common/avr/atomic_util.h create mode 100644 tmk_core/common/avr/gpio.h create mode 100644 tmk_core/common/avr/pin_defs.h (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/atomic_util.h b/tmk_core/common/avr/atomic_util.h new file mode 100644 index 0000000000..7c5d2e7dcc --- /dev/null +++ b/tmk_core/common/avr/atomic_util.h @@ -0,0 +1,22 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +/* atomic macro for AVR */ +#include + +#define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE) +#define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON) diff --git a/tmk_core/common/avr/gpio.h b/tmk_core/common/avr/gpio.h new file mode 100644 index 0000000000..231556c29c --- /dev/null +++ b/tmk_core/common/avr/gpio.h @@ -0,0 +1,34 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include "pin_defs.h" + +typedef uint8_t pin_t; + +#define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF)) +#define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF)) +#define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low") +#define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin)&0xF)) + +#define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin)&0xF)) +#define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF)) +#define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin)) + +#define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF))) + +#define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF)) diff --git a/tmk_core/common/avr/pin_defs.h b/tmk_core/common/avr/pin_defs.h new file mode 100644 index 0000000000..dbfed21f48 --- /dev/null +++ b/tmk_core/common/avr/pin_defs.h @@ -0,0 +1,128 @@ +/* Copyright 2021 QMK + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include + +#define PORT_SHIFTER 4 // this may be 4 for all AVR chips + +// If you want to add more to this list, reference the PINx definitions in these header +// files: https://github.com/vancegroup-mirrors/avr-libc/tree/master/avr-libc/include/avr + +#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) +# define ADDRESS_BASE 0x00 +# define PINB_ADDRESS 0x3 +# define PINC_ADDRESS 0x6 +# define PIND_ADDRESS 0x9 +# define PINE_ADDRESS 0xC +# define PINF_ADDRESS 0xF +#elif defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) +# define ADDRESS_BASE 0x00 +# define PINB_ADDRESS 0x3 +# define PINC_ADDRESS 0x6 +# define PIND_ADDRESS 0x9 +#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) +# define ADDRESS_BASE 0x00 +# define PINA_ADDRESS 0x0 +# define PINB_ADDRESS 0x3 +# define PINC_ADDRESS 0x6 +# define PIND_ADDRESS 0x9 +# define PINE_ADDRESS 0xC +# define PINF_ADDRESS 0xF +#elif defined(__AVR_ATmega32A__) +# define ADDRESS_BASE 0x10 +# define PIND_ADDRESS 0x0 +# define PINC_ADDRESS 0x3 +# define PINB_ADDRESS 0x6 +# define PINA_ADDRESS 0x9 +#elif defined(__AVR_ATtiny85__) +# define ADDRESS_BASE 0x10 +# define PINB_ADDRESS 0x6 +#else +# error "Pins are not defined" +#endif + +#define PINDEF(port, pin) ((PIN##port##_ADDRESS << PORT_SHIFTER) | pin) + +#define _PIN_ADDRESS(p, offset) _SFR_IO8(ADDRESS_BASE + ((p) >> PORT_SHIFTER) + (offset)) +// Port X Input Pins Address +#define PINx_ADDRESS(p) _PIN_ADDRESS(p, 0) +// Port X Data Direction Register, 0:input 1:output +#define DDRx_ADDRESS(p) _PIN_ADDRESS(p, 1) +// Port X Data Register +#define PORTx_ADDRESS(p) _PIN_ADDRESS(p, 2) + +/* I/O pins */ +#ifdef PORTA +# define A0 PINDEF(A, 0) +# define A1 PINDEF(A, 1) +# define A2 PINDEF(A, 2) +# define A3 PINDEF(A, 3) +# define A4 PINDEF(A, 4) +# define A5 PINDEF(A, 5) +# define A6 PINDEF(A, 6) +# define A7 PINDEF(A, 7) +#endif +#ifdef PORTB +# define B0 PINDEF(B, 0) +# define B1 PINDEF(B, 1) +# define B2 PINDEF(B, 2) +# define B3 PINDEF(B, 3) +# define B4 PINDEF(B, 4) +# define B5 PINDEF(B, 5) +# define B6 PINDEF(B, 6) +# define B7 PINDEF(B, 7) +#endif +#ifdef PORTC +# define C0 PINDEF(C, 0) +# define C1 PINDEF(C, 1) +# define C2 PINDEF(C, 2) +# define C3 PINDEF(C, 3) +# define C4 PINDEF(C, 4) +# define C5 PINDEF(C, 5) +# define C6 PINDEF(C, 6) +# define C7 PINDEF(C, 7) +#endif +#ifdef PORTD +# define D0 PINDEF(D, 0) +# define D1 PINDEF(D, 1) +# define D2 PINDEF(D, 2) +# define D3 PINDEF(D, 3) +# define D4 PINDEF(D, 4) +# define D5 PINDEF(D, 5) +# define D6 PINDEF(D, 6) +# define D7 PINDEF(D, 7) +#endif +#ifdef PORTE +# define E0 PINDEF(E, 0) +# define E1 PINDEF(E, 1) +# define E2 PINDEF(E, 2) +# define E3 PINDEF(E, 3) +# define E4 PINDEF(E, 4) +# define E5 PINDEF(E, 5) +# define E6 PINDEF(E, 6) +# define E7 PINDEF(E, 7) +#endif +#ifdef PORTF +# define F0 PINDEF(F, 0) +# define F1 PINDEF(F, 1) +# define F2 PINDEF(F, 2) +# define F3 PINDEF(F, 3) +# define F4 PINDEF(F, 4) +# define F5 PINDEF(F, 5) +# define F6 PINDEF(F, 6) +# define F7 PINDEF(F, 7) +#endif \ No newline at end of file -- cgit v1.2.3 From 1f2fe2eab99548f4bde2a79b03e3303cc9b73214 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sun, 14 Feb 2021 01:44:22 +0000 Subject: Refactor platform logic within print.h (#11863) * Remove GCC check from debug * Remove platform logic from common.mk * Refactor platform logic within print.h * restore debug.c format * headers * Rename function pointer type * review comments * Update tmk_core/common/printf.c Co-authored-by: Nick Brassel * Format Co-authored-by: Nick Brassel --- tmk_core/common/avr/_print.h | 33 +++++++++++++++++++++++++++++++++ tmk_core/common/avr/printf.c | 20 ++++++++++++++++++++ tmk_core/common/avr/printf.mk | 2 ++ 3 files changed, 55 insertions(+) create mode 100644 tmk_core/common/avr/_print.h create mode 100644 tmk_core/common/avr/printf.c create mode 100644 tmk_core/common/avr/printf.mk (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/_print.h b/tmk_core/common/avr/_print.h new file mode 100644 index 0000000000..f9b79bdf85 --- /dev/null +++ b/tmk_core/common/avr/_print.h @@ -0,0 +1,33 @@ +/* Copyright 2012 Jun Wako */ +/* Very basic print functions, intended to be used with usb_debug_only.c + * http://www.pjrc.com/teensy/ + * Copyright (c) 2008 PJRC.COM, LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#pragma once + +#include "avr/xprintf.h" + +// Create user & normal print defines +#define print(s) xputs(PSTR(s)) +#define println(s) xputs(PSTR(s "\r\n")) +#define uprint(s) print(s) +#define uprintln(s) println(s) +#define uprintf(fmt, ...) xprintf(fmt, ##__VA_ARGS__) \ No newline at end of file diff --git a/tmk_core/common/avr/printf.c b/tmk_core/common/avr/printf.c new file mode 100644 index 0000000000..9ad7a38693 --- /dev/null +++ b/tmk_core/common/avr/printf.c @@ -0,0 +1,20 @@ +/* +Copyright 2011 Jun Wako + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#include "xprintf.h" +#include "sendchar.h" + +void print_set_sendchar(sendchar_func_t func) { xdev_out(func); } diff --git a/tmk_core/common/avr/printf.mk b/tmk_core/common/avr/printf.mk new file mode 100644 index 0000000000..060ad88c57 --- /dev/null +++ b/tmk_core/common/avr/printf.mk @@ -0,0 +1,2 @@ +TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/xprintf.S +TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c -- cgit v1.2.3 From c27a778281824423a324d04276d291f06b49b1ae Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 15 Feb 2021 06:55:43 +1100 Subject: Format code according to conventions (#11905) Co-authored-by: QMK Bot --- tmk_core/common/avr/bootloader.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/bootloader.c b/tmk_core/common/avr/bootloader.c index 4e3a27022d..c0272903b8 100644 --- a/tmk_core/common/avr/bootloader.c +++ b/tmk_core/common/avr/bootloader.c @@ -237,13 +237,13 @@ __attribute__((weak)) void bootloader_jump(void) { "bootloader_startup_loop%=: \n\t" "rjmp bootloader_startup_loop%= \n\t" : - : [ mcucsrio ] "I"(_SFR_IO_ADDR(MCUCSR)), + : [mcucsrio] "I"(_SFR_IO_ADDR(MCUCSR)), # if (FLASHEND > 131071) - [ ramendhi ] "M"(((RAMEND - 2) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 2) >> 0) & 0xff), [ bootaddrhi ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff), + [ramendhi] "M"(((RAMEND - 2) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 2) >> 0) & 0xff), [bootaddrhi] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 16) & 0xff), # else - [ ramendhi ] "M"(((RAMEND - 1) >> 8) & 0xff), [ ramendlo ] "M"(((RAMEND - 1) >> 0) & 0xff), + [ramendhi] "M"(((RAMEND - 1) >> 8) & 0xff), [ramendlo] "M"(((RAMEND - 1) >> 0) & 0xff), # endif - [ bootaddrme ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [ bootaddrlo ] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)); + [bootaddrme] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 8) & 0xff), [bootaddrlo] "M"((((FLASH_SIZE - BOOTLOADER_SIZE) >> 1) >> 0) & 0xff)); #else // Assume remaining boards are DFU, even if the flag isn't set -- cgit v1.2.3 From 39694d5eb0b7e48e06f9544600041fbbedfff956 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 25 Feb 2021 15:54:25 +1100 Subject: V-USB suspend refactor (#11891) --- tmk_core/common/avr/sleep_led.c | 2 +- tmk_core/common/avr/suspend.c | 95 +++++++++++++++++++++++++++------------ tmk_core/common/avr/suspend_avr.h | 25 ----------- 3 files changed, 67 insertions(+), 55 deletions(-) delete mode 100644 tmk_core/common/avr/suspend_avr.h (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/sleep_led.c b/tmk_core/common/avr/sleep_led.c index 63dcc2afd9..9a3b52abe5 100644 --- a/tmk_core/common/avr/sleep_led.c +++ b/tmk_core/common/avr/sleep_led.c @@ -90,7 +90,7 @@ void sleep_led_toggle(void) { * * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle * - * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63 + * https://www.wolframalpha.com/input/?i=sin%28x%2F64*pi%29**8+*+255%2C+x%3D0+to+63 * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i } */ 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}; diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index 86c3df040a..cb505ab329 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -4,7 +4,6 @@ #include #include "matrix.h" #include "action.h" -#include "suspend_avr.h" #include "suspend.h" #include "timer.h" #include "led.h" @@ -13,6 +12,9 @@ #ifdef PROTOCOL_LUFA # include "lufa.h" #endif +#ifdef PROTOCOL_VUSB +# include "vusb.h" +#endif #ifdef BACKLIGHT_ENABLE # include "backlight.h" @@ -52,7 +54,25 @@ __attribute__((weak)) void suspend_power_down_user(void) {} */ __attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); } -#ifndef NO_SUSPEND_POWER_DOWN +#if !defined(NO_SUSPEND_POWER_DOWN) && defined(WDT_vect) + +// clang-format off +#define wdt_intr_enable(value) \ +__asm__ __volatile__ ( \ + "in __tmp_reg__,__SREG__" "\n\t" \ + "cli" "\n\t" \ + "wdr" "\n\t" \ + "sts %0,%1" "\n\t" \ + "out __SREG__,__tmp_reg__" "\n\t" \ + "sts %0,%2" "\n\t" \ + : /* no outputs */ \ + : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ + "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ + "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | _BV(WDIE) | (value & 0x07))) \ + : "r0" \ +) +// clang-format on + /** \brief Power down MCU with watchdog timer * * wdto: watchdog timer timeout defined in @@ -74,37 +94,11 @@ static uint8_t wdt_timeout = 0; * FIXME: needs doc */ static void power_down(uint8_t wdto) { -# ifdef PROTOCOL_LUFA - if (USB_DeviceState == DEVICE_STATE_Configured) return; -# endif wdt_timeout = wdto; // Watchdog Interrupt Mode wdt_intr_enable(wdto); -# ifdef BACKLIGHT_ENABLE - backlight_set(0); -# endif - - // Turn off LED indicators - uint8_t leds_off = 0; -# if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE) - if (is_backlight_enabled()) { - // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off - leds_off |= (1 << USB_LED_CAPS_LOCK); - } -# endif - led_set(leds_off); - -# ifdef AUDIO_ENABLE - // This sometimes disables the start-up noise, so it's been disabled - // stop_all_notes(); -# endif /* AUDIO_ENABLE */ -# if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) - rgblight_suspend(); -# endif - suspend_power_down_kb(); - // TODO: more power saving // See PicoPower application note // - I/O port input with pullup @@ -127,10 +121,46 @@ static void power_down(uint8_t wdto) { * FIXME: needs doc */ void suspend_power_down(void) { +#ifdef PROTOCOL_LUFA + if (USB_DeviceState == DEVICE_STATE_Configured) return; +#endif +#ifdef PROTOCOL_VUSB + if (!vusb_suspended) return; +#endif + suspend_power_down_kb(); #ifndef NO_SUSPEND_POWER_DOWN + // Turn off backlight +# ifdef BACKLIGHT_ENABLE + backlight_set(0); +# endif + + // Turn off LED indicators + uint8_t leds_off = 0; +# if defined(BACKLIGHT_CAPS_LOCK) && defined(BACKLIGHT_ENABLE) + if (is_backlight_enabled()) { + // Don't try to turn off Caps Lock indicator as it is backlight and backlight is already off + leds_off |= (1 << USB_LED_CAPS_LOCK); + } +# endif + led_set(leds_off); + + // Turn off audio +# ifdef AUDIO_ENABLE + // This sometimes disables the start-up noise, so it's been disabled + // stop_all_notes(); +# endif + + // Turn off underglow +# if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) + rgblight_suspend(); +# endif + + // Enter sleep state if possible (ie, the MCU has a watchdog timeout interrupt) +# if defined(WDT_vect) power_down(WDTO_15MS); +# endif #endif } @@ -164,17 +194,24 @@ __attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_us void suspend_wakeup_init(void) { // clear keyboard state clear_keyboard(); + + // Turn on backlight #ifdef BACKLIGHT_ENABLE backlight_init(); #endif + + // Restore LED indicators led_set(host_keyboard_leds()); + + // Wake up underglow #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) rgblight_wakeup(); #endif + suspend_wakeup_init_kb(); } -#ifndef NO_SUSPEND_POWER_DOWN +#if !defined(NO_SUSPEND_POWER_DOWN) && defined(WDT_vect) /* watchdog timeout */ ISR(WDT_vect) { // compensate timer for sleep diff --git a/tmk_core/common/avr/suspend_avr.h b/tmk_core/common/avr/suspend_avr.h deleted file mode 100644 index 6df048f3bb..0000000000 --- a/tmk_core/common/avr/suspend_avr.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -// clang-format off -#define wdt_intr_enable(value) \ -__asm__ __volatile__ ( \ - "in __tmp_reg__,__SREG__" "\n\t" \ - "cli" "\n\t" \ - "wdr" "\n\t" \ - "sts %0,%1" "\n\t" \ - "out __SREG__,__tmp_reg__" "\n\t" \ - "sts %0,%2" "\n\t" \ - : /* no outputs */ \ - : "M" (_SFR_MEM_ADDR(_WD_CONTROL_REG)), \ - "r" (_BV(_WD_CHANGE_BIT) | _BV(WDE)), \ - "r" ((uint8_t) ((value & 0x08 ? _WD_PS3_MASK : 0x00) | \ - _BV(WDIE) | (value & 0x07)) ) \ - : "r0" \ -) -// clang-format on -- cgit v1.2.3 From 1bb6d8de6fa5063b21bc34549e1cf552e60d90ba Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 25 Mar 2021 11:33:53 +0000 Subject: Fix USER_PRINT on avr/atsam (#12269) * Fix USER_PRINT on avr/atsam * Update tmk_core/common/arm_atsam/_print.h Co-authored-by: Ryan Co-authored-by: Ryan --- tmk_core/common/avr/_print.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tmk_core/common/avr') diff --git a/tmk_core/common/avr/_print.h b/tmk_core/common/avr/_print.h index f9b79bdf85..5c1fdd26d8 100644 --- a/tmk_core/common/avr/_print.h +++ b/tmk_core/common/avr/_print.h @@ -28,6 +28,6 @@ // Create user & normal print defines #define print(s) xputs(PSTR(s)) #define println(s) xputs(PSTR(s "\r\n")) -#define uprint(s) print(s) -#define uprintln(s) println(s) -#define uprintf(fmt, ...) xprintf(fmt, ##__VA_ARGS__) \ No newline at end of file +#define uprint(s) xputs(PSTR(s)) +#define uprintln(s) xputs(PSTR(s "\r\n")) +#define uprintf(fmt, ...) __xprintf(PSTR(fmt), ##__VA_ARGS__) \ No newline at end of file -- cgit v1.2.3