summaryrefslogtreecommitdiff
path: root/tmk_core/common/avr
diff options
context:
space:
mode:
authorDrashna Jael're <drashna@live.com>2021-06-29 12:23:03 -0700
committerDrashna Jael're <drashna@live.com>2021-06-29 12:24:07 -0700
commitacf2c323e2927f6007b17ded577cf49fd86fec6c (patch)
tree8334dc5c71e6ab9bf33c76143eac7bb0e60159b0 /tmk_core/common/avr
parentec7a7beeed3046e9144d4c4ce0ef3b2c4f9e4341 (diff)
parentf55e39e8a2246f6f96fd5d4a84a866e2615cde7b (diff)
Merge upstream QMK Firmware at '0.12.52~1'
Diffstat (limited to 'tmk_core/common/avr')
-rw-r--r--tmk_core/common/avr/_print.h33
-rw-r--r--tmk_core/common/avr/atomic_util.h22
-rw-r--r--tmk_core/common/avr/bootloader.c2
-rw-r--r--tmk_core/common/avr/gpio.h34
-rw-r--r--tmk_core/common/avr/pin_defs.h128
-rw-r--r--tmk_core/common/avr/printf.c20
-rw-r--r--tmk_core/common/avr/printf.mk2
-rw-r--r--tmk_core/common/avr/sleep_led.c2
-rw-r--r--tmk_core/common/avr/suspend.c92
-rw-r--r--tmk_core/common/avr/suspend_avr.h25
10 files changed, 306 insertions, 54 deletions
diff --git a/tmk_core/common/avr/_print.h b/tmk_core/common/avr/_print.h
new file mode 100644
index 0000000000..5c1fdd26d8
--- /dev/null
+++ b/tmk_core/common/avr/_print.h
@@ -0,0 +1,33 @@
+/* Copyright 2012 Jun Wako <wakojun@gmail.com> */
+/* 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) 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
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 <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+/* atomic macro for AVR */
+#include <util/atomic.h>
+
+#define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
+#define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
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);
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 <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <avr/io.h>
+#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..23d948041d
--- /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 <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <avr/io.h>
+
+#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_AT90USB162__) || 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
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 <wakojun@gmail.com>
+
+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 <http://www.gnu.org/licenses/>.
+*/
+#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
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 b784a0835d..47a82a2eec 100644
--- a/tmk_core/common/avr/suspend.c
+++ b/tmk_core/common/avr/suspend.c
@@ -4,7 +4,6 @@
#include <avr/interrupt.h>
#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 <avr/wdt.h>
@@ -74,35 +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
- stop_all_notes();
-# endif /* AUDIO_ENABLE */
-# if defined(RGBLIGHT_SLEEP) && defined(RGBLIGHT_ENABLE)
- rgblight_suspend();
-# endif
-
// TODO: more power saving
// See PicoPower application note
// - I/O port input with pullup
@@ -125,10 +121,45 @@ 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
+ 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
}
@@ -163,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 <stdint.h>
-#include <stdbool.h>
-#include <avr/sleep.h>
-#include <avr/wdt.h>
-#include <avr/interrupt.h>
-
-// 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