diff options
Diffstat (limited to 'tmk_core/common/avr')
-rw-r--r-- | tmk_core/common/avr/bootloader.c | 365 | ||||
-rw-r--r-- | tmk_core/common/avr/sleep_led.c | 43 | ||||
-rw-r--r-- | tmk_core/common/avr/suspend.c | 171 | ||||
-rw-r--r-- | tmk_core/common/avr/suspend_avr.h | 32 | ||||
-rw-r--r-- | tmk_core/common/avr/timer.c | 61 | ||||
-rw-r--r-- | tmk_core/common/avr/timer_avr.h | 26 | ||||
-rw-r--r-- | tmk_core/common/avr/xprintf.h | 17 |
7 files changed, 361 insertions, 354 deletions
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 <avr/boot.h> #ifdef PROTOCOL_LUFA -#include <LUFA/Drivers/USB/USB.h> +# include <LUFA/Drivers/USB/USB.h> #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<<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); - - 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? - 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 + 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? + 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<<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 +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<<USB_LED_CAPS_LOCK); + led_set(1 << USB_LED_CAPS_LOCK); } // LED off if (timer.pwm.count == pgm_read_byte(&breathing_table[timer.pwm.index])) { diff --git a/tmk_core/common/avr/suspend.c b/tmk_core/common/avr/suspend.c index 2259201b5d..574000fcd8 100644 --- a/tmk_core/common/avr/suspend.c +++ b/tmk_core/common/avr/suspend.c @@ -13,37 +13,36 @@ #include "rgblight_reconfig.h" #ifdef PROTOCOL_LUFA - #include "lufa.h" +# include "lufa.h" #endif #ifdef AUDIO_ENABLE - #include "audio.h" +# include "audio.h" #endif /* AUDIO_ENABLE */ #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) - #include "rgblight.h" - extern rgblight_config_t rgblight_config; - static bool rgblight_enabled; - static bool is_suspended; +# include "rgblight.h" +extern rgblight_config_t rgblight_config; +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" \ -) - +#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 * @@ -58,23 +57,18 @@ void suspend_idle(uint8_t time) { sleep_disable(); } - // TODO: This needs some cleanup /** \brief Run keyboard level Power down * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_power_down_user (void) { } +__attribute__((weak)) void suspend_power_down_user(void) {} /** \brief Run keyboard level Power down * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_power_down_kb(void) { - suspend_power_down_user(); -} +__attribute__((weak)) void suspend_power_down_kb(void) { suspend_power_down_user(); } #ifndef NO_SUSPEND_POWER_DOWN /** \brief Power down MCU with watchdog timer @@ -98,43 +92,43 @@ 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) -#ifdef RGBLIGHT_ANIMATIONS - rgblight_timer_disable(); -#endif - if (!is_suspended) { - is_suspended = true; - rgblight_enabled = rgblight_config.enable; - rgblight_disable_noeeprom(); - } -#endif - suspend_power_down_kb(); +# 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) +# ifdef RGBLIGHT_ANIMATIONS + rgblight_timer_disable(); +# endif + if (!is_suspended) { + is_suspended = true; + rgblight_enabled = rgblight_config.enable; + rgblight_disable_noeeprom(); + } +# endif + suspend_power_down_kb(); // TODO: more power saving // See PicoPower application note @@ -158,40 +152,36 @@ static void power_down(uint8_t wdto) { * FIXME: needs doc */ void suspend_power_down(void) { - suspend_power_down_kb(); + suspend_power_down_kb(); #ifndef NO_SUSPEND_POWER_DOWN power_down(WDTO_15MS); #endif } -__attribute__ ((weak)) void matrix_power_up(void) {} -__attribute__ ((weak)) void matrix_power_down(void) {} -bool suspend_wakeup_condition(void) { +__attribute__((weak)) void matrix_power_up(void) {} +__attribute__((weak)) void matrix_power_down(void) {} +bool suspend_wakeup_condition(void) { matrix_power_up(); matrix_scan(); matrix_power_down(); for (uint8_t r = 0; r < MATRIX_ROWS; r++) { if (matrix_get_row(r)) return true; } - return false; + return false; } /** \brief run user level code immediately after wakeup * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_wakeup_init_user(void) { } +__attribute__((weak)) void suspend_wakeup_init_user(void) {} /** \brief run keyboard level code immediately after wakeup * * FIXME: needs doc */ -__attribute__ ((weak)) -void suspend_wakeup_init_kb(void) { - suspend_wakeup_init_user(); -} +__attribute__((weak)) void suspend_wakeup_init_kb(void) { suspend_wakeup_init_user(); } /** \brief run immediately after wakeup * * FIXME: needs doc @@ -202,18 +192,18 @@ void suspend_wakeup_init(void) { #ifdef BACKLIGHT_ENABLE backlight_init(); #endif - led_set(host_keyboard_leds()); + led_set(host_keyboard_leds()); #if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE) - is_suspended = false; - if (rgblight_enabled) { - #ifdef BOOTLOADER_TEENSY - wait_ms(10); - #endif - rgblight_enable_noeeprom(); - } -#ifdef RGBLIGHT_ANIMATIONS - rgblight_timer_enable(); -#endif + is_suspended = false; + if (rgblight_enabled) { +# ifdef BOOTLOADER_TEENSY + wait_ms(10); +# endif + rgblight_enable_noeeprom(); + } +# ifdef RGBLIGHT_ANIMATIONS + rgblight_timer_enable(); +# endif #endif suspend_wakeup_init_kb(); } @@ -226,8 +216,7 @@ ISR(WDT_vect) { case WDTO_15MS: timer_count += 15 + 2; // WDTO_15MS + 2(from observation) break; - default: - ; + default:; } } #endif diff --git a/tmk_core/common/avr/suspend_avr.h b/tmk_core/common/avr/suspend_avr.h index 357102da44..d73852ba11 100644 --- a/tmk_core/common/avr/suspend_avr.h +++ b/tmk_core/common/avr/suspend_avr.h @@ -7,21 +7,21 @@ #include <avr/wdt.h> #include <avr/interrupt.h> - -#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 <http://www.gnu.org/licenses/>. #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<<OCIE0A); + OCR0A = TIMER_RAW_TOP; + TIMSK0 = (1 << OCIE0A); #else // Timer0 CTC mode TCCR0 = (1 << WGM01) | prescaler; - OCR0 = TIMER_RAW_TOP; + OCR0 = TIMER_RAW_TOP; TIMSK = (1 << OCIE0); #endif } @@ -68,26 +66,18 @@ void timer_init(void) * * FIXME: needs doc */ -inline -void timer_clear(void) -{ - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - timer_count = 0; - } +inline void timer_clear(void) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer_count = 0; } } /** \brief timer read * * FIXME: needs doc */ -inline -uint16_t timer_read(void) -{ +inline uint16_t timer_read(void) { uint32_t t; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - t = timer_count; - } + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } return (t & 0xFFFF); } @@ -96,14 +86,10 @@ uint16_t timer_read(void) * * FIXME: needs doc */ -inline -uint32_t timer_read32(void) -{ +inline uint32_t timer_read32(void) { uint32_t t; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - t = timer_count; - } + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } return t; } @@ -112,14 +98,10 @@ uint32_t timer_read32(void) * * FIXME: needs doc */ -inline -uint16_t timer_elapsed(uint16_t last) -{ +inline uint16_t timer_elapsed(uint16_t last) { uint32_t t; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - t = timer_count; - } + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } return TIMER_DIFF_16((t & 0xFFFF), last); } @@ -128,25 +110,18 @@ uint16_t timer_elapsed(uint16_t last) * * FIXME: needs doc */ -inline -uint32_t timer_elapsed32(uint32_t last) -{ +inline uint32_t timer_elapsed32(uint32_t last) { uint32_t t; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - t = timer_count; - } + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } return TIMER_DIFF_32(t, last); } // excecuted once per 1ms.(excess for just timer count?) #ifndef __AVR_ATmega32A__ -#define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect +# define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect #else -#define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect +# define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect #endif -ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) -{ - timer_count++; -} +ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) { timer_count++; } diff --git a/tmk_core/common/avr/timer_avr.h b/tmk_core/common/avr/timer_avr.h index 0e85eb1017..9aea21c651 100644 --- a/tmk_core/common/avr/timer_avr.h +++ b/tmk_core/common/avr/timer_avr.h @@ -21,22 +21,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <stdint.h> #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 - |