From 93f6749e06b70ba81e15f8b77e5a18675dacddfe 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