summaryrefslogtreecommitdiffstats
path: root/platforms/avr
diff options
context:
space:
mode:
Diffstat (limited to 'platforms/avr')
-rw-r--r--platforms/avr/_print.h33
-rw-r--r--platforms/avr/_timer.h19
-rw-r--r--platforms/avr/_wait.h49
-rw-r--r--platforms/avr/atomic_util.h22
-rw-r--r--platforms/avr/bootloader.c293
-rw-r--r--platforms/avr/bootloader_size.c21
-rw-r--r--platforms/avr/drivers/analog.c23
-rw-r--r--platforms/avr/drivers/analog.h3
-rw-r--r--platforms/avr/drivers/audio_pwm.h17
-rw-r--r--platforms/avr/drivers/audio_pwm_hardware.c332
-rw-r--r--platforms/avr/drivers/i2c_master.c56
-rw-r--r--platforms/avr/drivers/i2c_master.h2
-rw-r--r--platforms/avr/drivers/ps2/ps2_io.c51
-rw-r--r--platforms/avr/drivers/ps2/ps2_usart.c227
-rw-r--r--platforms/avr/drivers/uart.c26
-rw-r--r--platforms/avr/drivers/uart.h8
-rw-r--r--platforms/avr/flash.mk9
-rw-r--r--platforms/avr/gpio.h49
-rw-r--r--platforms/avr/pin_defs.h128
-rw-r--r--platforms/avr/platform.c21
-rw-r--r--platforms/avr/platform.mk179
-rw-r--r--platforms/avr/platform_deps.h20
-rw-r--r--platforms/avr/printf.c20
-rw-r--r--platforms/avr/printf.mk2
-rw-r--r--platforms/avr/sleep_led.c124
-rw-r--r--platforms/avr/suspend.c152
-rw-r--r--platforms/avr/timer.c133
-rw-r--r--platforms/avr/timer_avr.h39
-rw-r--r--platforms/avr/xprintf.S498
-rw-r--r--platforms/avr/xprintf.h103
30 files changed, 2625 insertions, 34 deletions
diff --git a/platforms/avr/_print.h b/platforms/avr/_print.h
new file mode 100644
index 0000000000..5c1fdd26d8
--- /dev/null
+++ b/platforms/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/platforms/avr/_timer.h b/platforms/avr/_timer.h
new file mode 100644
index 0000000000..b81e0f68b7
--- /dev/null
+++ b/platforms/avr/_timer.h
@@ -0,0 +1,19 @@
+/* Copyright 2021 Simon Arlott
+ *
+ * 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/>.
+ */
+#pragma once
+
+// The platform is 8-bit, so prefer 16-bit timers to reduce code size
+#define FAST_TIMER_T_SIZE 16
diff --git a/platforms/avr/_wait.h b/platforms/avr/_wait.h
new file mode 100644
index 0000000000..683db6ae57
--- /dev/null
+++ b/platforms/avr/_wait.h
@@ -0,0 +1,49 @@
+/* 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 <util/delay.h>
+
+#define wait_ms(ms) \
+ do { \
+ if (__builtin_constant_p(ms)) { \
+ _delay_ms(ms); \
+ } else { \
+ for (uint16_t i = ms; i > 0; i--) { \
+ _delay_ms(1); \
+ } \
+ } \
+ } while (0)
+#define wait_us(us) \
+ do { \
+ if (__builtin_constant_p(us)) { \
+ _delay_us(us); \
+ } else { \
+ for (uint16_t i = us; i > 0; i--) { \
+ _delay_us(1); \
+ } \
+ } \
+ } while (0)
+#define wait_cpuclock(n) __builtin_avr_delay_cycles(n)
+#define CPU_CLOCK F_CPU
+
+/* The AVR series GPIOs have a one clock read delay for changes in the digital input signal.
+ * But here's more margin to make it two clocks. */
+#ifndef GPIO_INPUT_PIN_DELAY
+# define GPIO_INPUT_PIN_DELAY 2
+#endif
+
+#define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)
diff --git a/platforms/avr/atomic_util.h b/platforms/avr/atomic_util.h
new file mode 100644
index 0000000000..7c5d2e7dcc
--- /dev/null
+++ b/platforms/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/platforms/avr/bootloader.c b/platforms/avr/bootloader.c
new file mode 100644
index 0000000000..c0272903b8
--- /dev/null
+++ b/platforms/avr/bootloader.c
@@ -0,0 +1,293 @@
+#include <stdint.h>
+#include <stdbool.h>
+#include <avr/io.h>
+#include <avr/eeprom.h>
+#include <avr/interrupt.h>
+#include <avr/wdt.h>
+#include <util/delay.h>
+#include "bootloader.h"
+#include <avr/boot.h>
+
+#ifdef PROTOCOL_LUFA
+# 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.
+ * Note that 'Word'(2 bytes) size and address are used in datasheet while TMK uses 'Byte'.
+ *
+ * Size of Bootloaders in bytes:
+ * Atmel DFU loader(ATmega32U4) 4096
+ * Atmel DFU loader(AT90USB128) 8192
+ * LUFA bootloader(ATmega32U4) 4096
+ * Arduino Caterina(ATmega32U4) 4096
+ * USBaspLoader(ATmega***) 2048
+ * Teensy halfKay(ATmega32U4) 512
+ * Teensy++ halfKay(AT90USB128) 1024
+ *
+ * AVR Boot section is located at the end of Flash memory like the followings.
+ *
+ * byte Atmel/LUFA(ATMega32u4) byte Atmel(AT90SUB128)
+ * 0x0000 +---------------+ 0x00000 +---------------+
+ * | | | |
+ * | | | |
+ * | Application | | Application |
+ * | | | |
+ * = = = =
+ * | | 32KB-4KB | | 128KB-8KB
+ * 0x7000 +---------------+ 0x1E000 +---------------+
+ * | Bootloader | 4KB | Bootloader | 8KB
+ * 0x7FFF +---------------+ 0x1FFFF +---------------+
+ *
+ *
+ * byte Teensy(ATMega32u4) byte Teensy++(AT90SUB128)
+ * 0x0000 +---------------+ 0x00000 +---------------+
+ * | | | |
+ * | | | |
+ * | Application | | Application |
+ * | | | |
+ * = = = =
+ * | | 32KB-512B | | 128KB-1KB
+ * 0x7E00 +---------------+ 0x1FC00 +---------------+
+ * | Bootloader | 512B | Bootloader | 1KB
+ * 0x7FFF +---------------+ 0x1FFFF +---------------+
+ */
+#define FLASH_SIZE (FLASHEND + 1L)
+
+#if !defined(BOOTLOADER_SIZE)
+uint16_t bootloader_start;
+#endif
+
+// 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
+ */
+#define BOOTLOADER_RESET_KEY 0xB007B007
+uint32_t reset_key __attribute__((section(".noinit,\"aw\",@nobits;")));
+
+/** \brief initialize MCU status by watchdog reset
+ *
+ * FIXME: needs doc
+ */
+__attribute__((weak)) void bootloader_jump(void) {
+#if !defined(BOOTLOADER_SIZE)
+ uint8_t high_fuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
+
+ if (high_fuse & ~(FUSE_BOOTSZ0 & FUSE_BOOTSZ1)) {
+ bootloader_start = (FLASH_SIZE - 512) >> 1;
+ } else if (high_fuse & ~(FUSE_BOOTSZ1)) {
+ bootloader_start = (FLASH_SIZE - 1024) >> 1;
+ } else if (high_fuse & ~(FUSE_BOOTSZ0)) {
+ 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);
+
+ 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__) || defined(__AVR_ATmega328__) || defined(__AVR_ATtiny85__)) // 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
+}
diff --git a/platforms/avr/bootloader_size.c b/platforms/avr/bootloader_size.c
new file mode 100644
index 0000000000..a029f9321f
--- /dev/null
+++ b/platforms/avr/bootloader_size.c
@@ -0,0 +1,21 @@
+// Copyright 2017 Jack Humbert
+//
+// 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 <avr/io.h>
+#include <avr/boot.h>
+
+// clang-format off
+// this is not valid C - it's for computing the size available on the chip
+AVR_SIZE: FLASHEND + 1 - BOOTLOADER_SIZE
diff --git a/platforms/avr/drivers/analog.c b/platforms/avr/drivers/analog.c
index 8d299ffdb9..628835ccef 100644
--- a/platforms/avr/drivers/analog.c
+++ b/platforms/avr/drivers/analog.c
@@ -23,29 +23,6 @@ static uint8_t aref = ADC_REF_POWER;
void analogReference(uint8_t mode) { aref = mode & (_BV(REFS1) | _BV(REFS0)); }
-// Arduino compatible pin input
-int16_t analogRead(uint8_t pin) {
-#if defined(__AVR_ATmega32U4__)
- // clang-format off
- static const uint8_t PROGMEM pin_to_mux[] = {
- //A0 A1 A2 A3 A4 A5
- //F7 F6 F5 F4 F1 F0
- 0x07, 0x06, 0x05, 0x04, 0x01, 0x00,
- //A6 A7 A8 A9 A10 A11
- //D4 D7 B4 B5 B6 D6
- 0x20, 0x22, 0x23, 0x24, 0x25, 0x21
- };
- // clang-format on
- if (pin >= 12) return 0;
- return adc_read(pgm_read_byte(pin_to_mux + pin));
-#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
- if (pin >= 8) return 0;
- return adc_read(pin);
-#else
- return 0;
-#endif
-}
-
int16_t analogReadPin(pin_t pin) { return adc_read(pinToMux(pin)); }
uint8_t pinToMux(pin_t pin) {
diff --git a/platforms/avr/drivers/analog.h b/platforms/avr/drivers/analog.h
index 058882450d..fa2fb0d89b 100644
--- a/platforms/avr/drivers/analog.h
+++ b/platforms/avr/drivers/analog.h
@@ -22,8 +22,7 @@
#ifdef __cplusplus
extern "C" {
#endif
-void analogReference(uint8_t mode);
-int16_t analogRead(uint8_t pin);
+void analogReference(uint8_t mode);
int16_t analogReadPin(pin_t pin);
uint8_t pinToMux(pin_t pin);
diff --git a/platforms/avr/drivers/audio_pwm.h b/platforms/avr/drivers/audio_pwm.h
new file mode 100644
index 0000000000..d6eb3571da
--- /dev/null
+++ b/platforms/avr/drivers/audio_pwm.h
@@ -0,0 +1,17 @@
+/* Copyright 2020 Jack Humbert
+ * Copyright 2020 JohSchneider
+ *
+ * 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/>.
+ */
+#pragma once
diff --git a/platforms/avr/drivers/audio_pwm_hardware.c b/platforms/avr/drivers/audio_pwm_hardware.c
new file mode 100644
index 0000000000..df03a4558c
--- /dev/null
+++ b/platforms/avr/drivers/audio_pwm_hardware.c
@@ -0,0 +1,332 @@
+/* Copyright 2016 Jack Humbert
+ * Copyright 2020 JohSchneider
+ *
+ * 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/>.
+ */
+
+#if defined(__AVR__)
+# include <avr/pgmspace.h>
+# include <avr/interrupt.h>
+# include <avr/io.h>
+#endif
+
+#include "audio.h"
+
+extern bool playing_note;
+extern bool playing_melody;
+extern uint8_t note_timbre;
+
+#define CPU_PRESCALER 8
+
+/*
+ Audio Driver: PWM
+
+ drive up to two speakers through the AVR PWM hardware-peripheral, using timer1 and/or timer3 on Atmega32U4.
+
+ the primary channel_1 can be connected to either pin PC4 PC5 or PC6 (the later being used by most AVR based keyboards) with a PMW signal generated by timer3
+ and an optional secondary channel_2 on either pin PB5, PB6 or PB7, with a PWM signal from timer1
+
+ alternatively, the PWM pins on PORTB can be used as only/primary speaker
+*/
+
+#if defined(AUDIO_PIN) && (AUDIO_PIN != C4) && (AUDIO_PIN != C5) && (AUDIO_PIN != C6) && (AUDIO_PIN != B5) && (AUDIO_PIN != B6) && (AUDIO_PIN != B7) && (AUDIO_PIN != D5)
+# error "Audio feature enabled, but no suitable pin selected as AUDIO_PIN - see docs/feature_audio under the AVR settings for available options."
+#endif
+
+#if (AUDIO_PIN == C4) || (AUDIO_PIN == C5) || (AUDIO_PIN == C6)
+# define AUDIO1_PIN_SET
+# define AUDIO1_TIMSKx TIMSK3
+# define AUDIO1_TCCRxA TCCR3A
+# define AUDIO1_TCCRxB TCCR3B
+# define AUDIO1_ICRx ICR3
+# define AUDIO1_WGMx0 WGM30
+# define AUDIO1_WGMx1 WGM31
+# define AUDIO1_WGMx2 WGM32
+# define AUDIO1_WGMx3 WGM33
+# define AUDIO1_CSx0 CS30
+# define AUDIO1_CSx1 CS31
+# define AUDIO1_CSx2 CS32
+
+# if (AUDIO_PIN == C6)
+# define AUDIO1_COMxy0 COM3A0
+# define AUDIO1_COMxy1 COM3A1
+# define AUDIO1_OCIExy OCIE3A
+# define AUDIO1_OCRxy OCR3A
+# define AUDIO1_PIN C6
+# define AUDIO1_TIMERx_COMPy_vect TIMER3_COMPA_vect
+# elif (AUDIO_PIN == C5)
+# define AUDIO1_COMxy0 COM3B0
+# define AUDIO1_COMxy1 COM3B1
+# define AUDIO1_OCIExy OCIE3B
+# define AUDIO1_OCRxy OCR3B
+# define AUDIO1_PIN C5
+# define AUDIO1_TIMERx_COMPy_vect TIMER3_COMPB_vect
+# elif (AUDIO_PIN == C4)
+# define AUDIO1_COMxy0 COM3C0
+# define AUDIO1_COMxy1 COM3C1
+# define AUDIO1_OCIExy OCIE3C
+# define AUDIO1_OCRxy OCR3C
+# define AUDIO1_PIN C4
+# define AUDIO1_TIMERx_COMPy_vect TIMER3_COMPC_vect
+# endif
+#endif
+
+#if defined(AUDIO_PIN) && defined(AUDIO_PIN_ALT) && (AUDIO_PIN == AUDIO_PIN_ALT)
+# error "Audio feature: AUDIO_PIN and AUDIO_PIN_ALT on the same pin makes no sense."
+#endif
+
+#if ((AUDIO_PIN == B5) && ((AUDIO_PIN_ALT == B6) || (AUDIO_PIN_ALT == B7))) || ((AUDIO_PIN == B6) && ((AUDIO_PIN_ALT == B5) || (AUDIO_PIN_ALT == B7))) || ((AUDIO_PIN == B7) && ((AUDIO_PIN_ALT == B5) || (AUDIO_PIN_ALT == B6)))
+# error "Audio feature: PORTB as AUDIO_PIN and AUDIO_PIN_ALT at the same time is not supported."
+#endif
+
+#if defined(AUDIO_PIN_ALT) && (AUDIO_PIN_ALT != B5) && (AUDIO_PIN_ALT != B6) && (AUDIO_PIN_ALT != B7)
+# error "Audio feature: the pin selected as AUDIO_PIN_ALT is not supported."
+#endif
+
+#if (AUDIO_PIN == B5) || (AUDIO_PIN == B6) || (AUDIO_PIN == B7) || (AUDIO_PIN_ALT == B5) || (AUDIO_PIN_ALT == B6) || (AUDIO_PIN_ALT == B7) || (AUDIO_PIN == D5)
+# define AUDIO2_PIN_SET
+# define AUDIO2_TIMSKx TIMSK1
+# define AUDIO2_TCCRxA TCCR1A
+# define AUDIO2_TCCRxB TCCR1B
+# define AUDIO2_ICRx ICR1
+# define AUDIO2_WGMx0 WGM10
+# define AUDIO2_WGMx1 WGM11
+# define AUDIO2_WGMx2 WGM12
+# define AUDIO2_WGMx3 WGM13
+# define AUDIO2_CSx0 CS10
+# define AUDIO2_CSx1 CS11
+# define AUDIO2_CSx2 CS12
+
+# if (AUDIO_PIN == B5) || (AUDIO_PIN_ALT == B5)
+# define AUDIO2_COMxy0 COM1A0
+# define AUDIO2_COMxy1 COM1A1
+# define AUDIO2_OCIExy OCIE1A
+# define AUDIO2_OCRxy OCR1A
+# define AUDIO2_PIN B5
+# define AUDIO2_TIMERx_COMPy_vect TIMER1_COMPA_vect
+# elif (AUDIO_PIN == B6) || (AUDIO_PIN_ALT == B6)
+# define AUDIO2_COMxy0 COM1B0
+# define AUDIO2_COMxy1 COM1B1
+# define AUDIO2_OCIExy OCIE1B
+# define AUDIO2_OCRxy OCR1B
+# define AUDIO2_PIN B6
+# define AUDIO2_TIMERx_COMPy_vect TIMER1_COMPB_vect
+# elif (AUDIO_PIN == B7) || (AUDIO_PIN_ALT == B7)
+# define AUDIO2_COMxy0 COM1C0
+# define AUDIO2_COMxy1 COM1C1
+# define AUDIO2_OCIExy OCIE1C
+# define AUDIO2_OCRxy OCR1C
+# define AUDIO2_PIN B7
+# define AUDIO2_TIMERx_COMPy_vect TIMER1_COMPC_vect
+# elif (AUDIO_PIN == D5) && defined(__AVR_ATmega32A__)
+# pragma message "Audio support for ATmega32A is experimental and can cause crashes."
+# undef AUDIO2_TIMSKx
+# define AUDIO2_TIMSKx TIMSK
+# define AUDIO2_COMxy0 COM1A0
+# define AUDIO2_COMxy1 COM1A1
+# define AUDIO2_OCIExy OCIE1A
+# define AUDIO2_OCRxy OCR1A
+# define AUDIO2_PIN D5
+# define AUDIO2_TIMERx_COMPy_vect TIMER1_COMPA_vect
+# endif
+#endif
+
+// C6 seems to be the assumed default by many existing keyboard - but sill warn the user
+#if !defined(AUDIO1_PIN_SET) && !defined(AUDIO2_PIN_SET)
+# pragma message "Audio feature enabled, but no suitable pin selected - see docs/feature_audio under the AVR settings for available options. Don't expect to hear anything... :-)"
+// TODO: make this an error - go through the breaking-change-process and change all keyboards to the new define
+#endif
+// -----------------------------------------------------------------------------
+
+#ifdef AUDIO1_PIN_SET
+static float channel_1_frequency = 0.0f;
+void channel_1_set_frequency(float freq) {
+ if (freq == 0.0f) // a pause/rest is a valid "note" with freq=0
+ {
+ // disable the output, but keep the pwm-ISR going (with the previous
+ // frequency) so the audio-state keeps getting updated
+ // Note: setting the duty-cycle 0 is not possible on non-inverting PWM mode - see the AVR data-sheet
+ AUDIO1_TCCRxA &= ~(_BV(AUDIO1_COMxy1) | _BV(AUDIO1_COMxy0));
+ return;
+ } else {
+ AUDIO1_TCCRxA |= _BV(AUDIO1_COMxy1); // enable output, PWM mode
+ }
+
+ channel_1_frequency = freq;
+
+ // set pwm period
+ AUDIO1_ICRx = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
+ // and duty cycle
+ AUDIO1_OCRxy = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre / 100);
+}
+
+void channel_1_start(void) {
+ // enable timer-counter ISR
+ AUDIO1_TIMSKx |= _BV(AUDIO1_OCIExy);
+ // enable timer-counter output
+ AUDIO1_TCCRxA |= _BV(AUDIO1_COMxy1);
+}
+
+void channel_1_stop(void) {
+ // disable timer-counter ISR
+ AUDIO1_TIMSKx &= ~_BV(AUDIO1_OCIExy);
+ // disable timer-counter output
+ AUDIO1_TCCRxA &= ~(_BV(AUDIO1_COMxy1) | _BV(AUDIO1_COMxy0));
+}
+#endif
+
+#ifdef AUDIO2_PIN_SET
+static float channel_2_frequency = 0.0f;
+void channel_2_set_frequency(float freq) {
+ if (freq == 0.0f) {
+ AUDIO2_TCCRxA &= ~(_BV(AUDIO2_COMxy1) | _BV(AUDIO2_COMxy0));
+ return;
+ } else {
+ AUDIO2_TCCRxA |= _BV(AUDIO2_COMxy1);
+ }
+
+ channel_2_frequency = freq;
+
+ AUDIO2_ICRx = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
+ AUDIO2_OCRxy = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre / 100);
+}
+
+float channel_2_get_frequency(void) { return channel_2_frequency; }
+
+void channel_2_start(void) {
+ AUDIO2_TIMSKx |= _BV(AUDIO2_OCIExy);
+ AUDIO2_TCCRxA |= _BV(AUDIO2_COMxy1);
+}
+
+void channel_2_stop(void) {
+ AUDIO2_TIMSKx &= ~_BV(AUDIO2_OCIExy);
+ AUDIO2_TCCRxA &= ~(_BV(AUDIO2_COMxy1) | _BV(AUDIO2_COMxy0));
+}
+#endif
+
+void audio_driver_initialize() {
+#ifdef AUDIO1_PIN_SET
+ channel_1_stop();
+ setPinOutput(AUDIO1_PIN);
+#endif
+
+#ifdef AUDIO2_PIN_SET
+ channel_2_stop();
+ setPinOutput(AUDIO2_PIN);
+#endif
+