summaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/action.c5
-rw-r--r--quantum/audio/audio.h13
-rw-r--r--quantum/audio/driver_avr_pwm.h17
-rw-r--r--quantum/audio/driver_avr_pwm_hardware.c332
-rw-r--r--quantum/audio/driver_chibios_dac.h126
-rw-r--r--quantum/audio/driver_chibios_dac_additive.c335
-rw-r--r--quantum/audio/driver_chibios_dac_basic.c245
-rw-r--r--quantum/audio/driver_chibios_pwm.h40
-rw-r--r--quantum/audio/driver_chibios_pwm_hardware.c144
-rw-r--r--quantum/audio/driver_chibios_pwm_software.c164
-rw-r--r--quantum/audio/song_list.h8
-rw-r--r--quantum/backlight/backlight_chibios.c10
-rw-r--r--quantum/eeconfig.c11
-rw-r--r--quantum/keyboard.c13
-rw-r--r--quantum/matrix.c28
-rw-r--r--quantum/mcu_selection.mk34
-rw-r--r--quantum/process_keycode/process_haptic.c1
-rw-r--r--quantum/process_keycode/process_programmable_button.c31
-rw-r--r--quantum/process_keycode/process_programmable_button.h23
-rw-r--r--quantum/process_keycode/process_unicode_common.c47
-rw-r--r--quantum/programmable_button.c37
-rw-r--r--quantum/programmable_button.h30
-rw-r--r--quantum/quantum.c99
-rw-r--r--quantum/quantum.h4
-rw-r--r--quantum/quantum_keycodes.h70
-rw-r--r--quantum/usb_device_state.c51
-rw-r--r--quantum/usb_device_state.h39
27 files changed, 493 insertions, 1464 deletions
diff --git a/quantum/action.c b/quantum/action.c
index be135f18f2..95f39d23d4 100644
--- a/quantum/action.c
+++ b/quantum/action.c
@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "keycode.h"
#include "keyboard.h"
#include "mousekey.h"
+#include "programmable_button.h"
#include "command.h"
#include "led.h"
#include "action_layer.h"
@@ -988,6 +989,10 @@ void clear_keyboard_but_mods_and_keys() {
mousekey_clear();
mousekey_send();
#endif
+#ifdef PROGRAMMABLE_BUTTON_ENABLE
+ programmable_button_clear();
+ programmable_button_send();
+#endif
}
/** \brief Utilities for actions. (FIXME: Needs better description)
diff --git a/quantum/audio/audio.h b/quantum/audio/audio.h
index 56b9158a1a..290d461f5a 100644
--- a/quantum/audio/audio.h
+++ b/quantum/audio/audio.h
@@ -26,17 +26,12 @@
#if defined(__AVR__)
# include <avr/io.h>
-# if defined(AUDIO_DRIVER_PWM)
-# include "driver_avr_pwm.h"
-# endif
#endif
-#if defined(PROTOCOL_CHIBIOS)
-# if defined(AUDIO_DRIVER_PWM)
-# include "driver_chibios_pwm.h"
-# elif defined(AUDIO_DRIVER_DAC)
-# include "driver_chibios_dac.h"
-# endif
+#if defined(AUDIO_DRIVER_PWM)
+# include "audio_pwm.h"
+#elif defined(AUDIO_DRIVER_DAC)
+# include "audio_dac.h"
#endif
typedef union {
diff --git a/quantum/audio/driver_avr_pwm.h b/quantum/audio/driver_avr_pwm.h
deleted file mode 100644
index d6eb3571da..0000000000
--- a/quantum/audio/driver_avr_pwm.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/* 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/quantum/audio/driver_avr_pwm_hardware.c b/quantum/audio/driver_avr_pwm_hardware.c
deleted file mode 100644
index df03a4558c..0000000000
--- a/quantum/audio/driver_avr_pwm_hardware.c
+++ /dev/null
@@ -1,332 +0,0 @@
-/* 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
-
- // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers TCCR3A/TCCR3B, TCCR1A/TCCR1B
- // Compare Output Mode (COM3An and COM1An) = 0b00 = Normal port operation
- // OC3A -- PC6
- // OC3B -- PC5
- // OC3C -- PC4
- // OC1A -- PB5
- // OC1B -- PB6
- // OC1C -- PB7
-
- // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14. Period = ICR3, Duty Cycle OCR3A)
- // OCR3A - PC6
- // OCR3B - PC5
- // OCR3C - PC4
- // OCR1A - PB5
- // OCR1B - PB6
- // OCR1C - PB7
-
- // Clock Select (CS3n) = 0b010 = Clock / 8
-#ifdef AUDIO1_PIN_SET
- // initialize timer-counter
- AUDIO1_TCCRxA = (0 << AUDIO1_COMxy1) | (0 << AUDIO1_COMxy0) | (1 << AUDIO1_WGMx1) | (0 << AUDIO1_WGMx0);
- AUDIO1_TCCRxB = (1 << AUDIO1_WGMx3) | (1 << AUDIO1_WGMx2) | (0 << AUDIO1_CSx2) | (1 << AUDIO1_CSx1) | (0 << AUDIO1_CSx0);
-#endif
-
-#ifdef AUDIO2_PIN_SET
- AUDIO2_TCCRxA = (0 << AUDIO2_COMxy1) | (0 << AUDIO2_COMxy0) | (1 << AUDIO2_WGMx1) | (0 << AUDIO2_WGMx0);
- AUDIO2_TCCRxB = (1 << AUDIO2_WGMx3) | (1 << AUDIO2_WGMx2) | (0 << AUDIO2_CSx2) | (1 << AUDIO2_CSx1) | (0 << AUDIO2_CSx0);
-#endif
-}
-
-void audio_driver_stop() {
-#ifdef AUDIO1_PIN_SET
- channel_1_stop();
-#endif
-
-#ifdef AUDIO2_PIN_SET
- channel_2_stop();
-#endif
-}
-
-void audio_driver_start(void) {
-#ifdef AUDIO1_PIN_SET
- channel_1_start();
- if (playing_note) {
- channel_1_set_frequency(audio_get_processed_frequency(0));
- }
-#endif
-
-#if !defined(AUDIO1_PIN_SET) && defined(AUDIO2_PIN_SET)
- channel_2_start();
- if (playing_note) {
- channel_2_set_frequency(audio_get_processed_frequency(0));
- }
-#endif
-}
-
-static volatile uint32_t isr_counter = 0;
-#ifdef AUDIO1_PIN_SET
-ISR(AUDIO1_TIMERx_COMPy_vect) {
- isr_counter++;
- if (isr_counter < channel_1_frequency / (CPU_PRESCALER * 8)) return;
-
- isr_counter = 0;
- bool state_changed = audio_update_state();
-
- if (!playing_note && !playing_melody) {
- channel_1_stop();
-# ifdef AUDIO2_PIN_SET
- channel_2_stop();
-# endif
- return;
- }
-
- if (state_changed) {
- channel_1_set_frequency(audio_get_processed_frequency(0));
-# ifdef AUDIO2_PIN_SET
- if (audio_get_number_of_active_tones() > 1) {
- channel_2_set_frequency(audio_get_processed_frequency(1));
- } else {
- channel_2_stop();
- }
-# endif
- }
-}
-#endif
-
-#if !defined(AUDIO1_PIN_SET) && defined(AUDIO2_PIN_SET)
-ISR(AUDIO2_TIMERx_COMPy_vect) {
- isr_counter++;
- if (isr_counter < channel_2_frequency / (CPU_PRESCALER * 8)) return;
-
- isr_counter = 0;
- bool state_changed = audio_update_state();
-
- if (!playing_note && !playing_melody) {
- channel_2_stop();
- return;
- }
-
- if (state_changed) {
- channel_2_set_frequency(audio_get_processed_frequency(0));
- }
-}
-#endif
diff --git a/quantum/audio/driver_chibios_dac.h b/quantum/audio/driver_chibios_dac.h
deleted file mode 100644
index 07cd622ead..0000000000
--- a/quantum/audio/driver_chibios_dac.h
+++ /dev/null
@@ -1,126 +0,0 @@
-/* Copyright 2019 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
-
-#ifndef A4
-# define A4 PAL_LINE(GPIOA, 4)
-#endif
-#ifndef A5
-# define A5 PAL_LINE(GPIOA, 5)
-#endif
-
-/**
- * Size of the dac_buffer arrays. All must be the same size.
- */
-#define AUDIO_DAC_BUFFER_SIZE 256U
-
-/**
- * Highest value allowed sample value.
-
- * since the DAC is limited to 12 bit, the absolute max is 0xfff = 4095U;
- * lower values adjust the peak-voltage aka volume down.
- * adjusting this value has only an effect on a sample-buffer whose values are
- * are NOT pregenerated - see square-wave
- */
-#ifndef AUDIO_DAC_SAMPLE_MAX
-# define AUDIO_DAC_SAMPLE_MAX 4095U
-#endif
-
-#if !defined(AUDIO_DAC_SAMPLE_RATE) && !defined(AUDIO_MAX_SIMULTANEOUS_TONES) && !defined(AUDIO_DAC_QUALITY_VERY_LOW) && !defined(AUDIO_DAC_QUALITY_LOW) && !defined(AUDIO_DAC_QUALITY_HIGH) && !defined(AUDIO_DAC_QUALITY_VERY_HIGH)
-# define AUDIO_DAC_QUALITY_SANE_MINIMUM
-#endif
-
-/**
- * These presets allow you to quickly switch between quality settings for
- * the DAC. The sample rate and maximum number of simultaneous tones roughly
- * has an inverse relationship - slightly higher sample rates may be possible.
- *
- * NOTE: a high sample-rate results in a higher cpu-load, which might lead to
- * (audible) discontinuities and/or starve other processes of cpu-time
- * (like RGB-led back-lighting, ...)
- */
-#ifdef AUDIO_DAC_QUALITY_VERY_LOW
-# define AUDIO_DAC_SAMPLE_RATE 11025U
-# define AUDIO_MAX_SIMULTANEOUS_TONES 8
-#endif
-
-#ifdef AUDIO_DAC_QUALITY_LOW
-# define AUDIO_DAC_SAMPLE_RATE 22050U
-# define AUDIO_MAX_SIMULTANEOUS_TONES 4
-#endif
-
-#ifdef AUDIO_DAC_QUALITY_HIGH
-# define AUDIO_DAC_SAMPLE_RATE 44100U
-# define AUDIO_MAX_SIMULTANEOUS_TONES 2
-#endif
-
-#ifdef AUDIO_DAC_QUALITY_VERY_HIGH
-# define AUDIO_DAC_SAMPLE_RATE 88200U
-# define AUDIO_MAX_SIMULTANEOUS_TONES 1
-#endif
-
-#ifdef AUDIO_DAC_QUALITY_SANE_MINIMUM
-/* a sane-minimum config: with a trade-off between cpu-load and tone-range
- *
- * the (currently) highest defined note is NOTE_B8 with 7902Hz; if we now
- * aim for an even even multiple of the buffer-size, we end up with:
- * ( roundUptoPow2(highest note / AUDIO_DAC_BUFFER_SIZE) * nyquist-rate * AUDIO_DAC_BUFFER_SIZE)
- * 7902/256 = 30.867 * 2 * 256 ~= 16384
- * which works out (but the 'scope shows some sampling artifacts with lower harmonics :-P)
- */
-# define AUDIO_DAC_SAMPLE_RATE 16384U
-# define AUDIO_MAX_SIMULTANEOUS_TONES 8
-#endif
-
-/**
- * Effective bit-rate of the DAC. 44.1khz is the standard for most audio - any
- * lower will sacrifice perceptible audio quality. Any higher will limit the
- * number of simultaneous tones. In most situations, a tenth (1/10) of the
- * sample rate is where notes become unbearable.
- */
-#ifndef AUDIO_DAC_SAMPLE_RATE
-# define AUDIO_DAC_SAMPLE_RATE 44100U
-#endif
-
-/**
- * The number of tones that can be played simultaneously. If too high a value
- * is used here, the keyboard will freeze and glitch-out when that many tones
- * are being played.
- */
-#ifndef AUDIO_MAX_SIMULTANEOUS_TONES
-# define AUDIO_MAX_SIMULTANEOUS_TONES 2
-#endif
-
-/**
- * The default value of the DAC when not playing anything. Certain hardware
- * setups may require a high (AUDIO_DAC_SAMPLE_MAX) or low (0) value here.
- * Since multiple added sine waves tend to oscillate around the midpoint,
- * and possibly never/rarely reach either 0 of MAX, 1/2 MAX can be a
- * reasonable default value.
- */
-#ifndef AUDIO_DAC_OFF_VALUE
-# define AUDIO_DAC_OFF_VALUE AUDIO_DAC_SAMPLE_MAX / 2
-#endif
-
-#if AUDIO_DAC_OFF_VALUE > AUDIO_DAC_SAMPLE_MAX
-# error "AUDIO_DAC: OFF_VALUE may not be larger than SAMPLE_MAX"
-#endif
-
-/**
- *user overridable sample generation/processing
- */
-uint16_t dac_value_generate(void);
diff --git a/quantum/audio/driver_chibios_dac_additive.c b/quantum/audio/driver_chibios_dac_additive.c
deleted file mode 100644
index db304adb87..0000000000
--- a/quantum/audio/driver_chibios_dac_additive.c
+++ /dev/null
@@ -1,335 +0,0 @@
-/* Copyright 2016-2019 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/>.
- */
-
-#include "audio.h"
-#include <ch.h>
-#include <hal.h>
-
-/*
- Audio Driver: DAC
-
- which utilizes the dac unit many STM32 are equipped with, to output a modulated waveform from samples stored in the dac_buffer_* array who are passed to the hardware through DMA
-
- it is also possible to have a custom sample-LUT by implementing/overriding 'dac_value_generate'
-
- this driver allows for multiple simultaneous tones to be played through one single channel by doing additive wave-synthesis
-*/
-
-#if !defined(AUDIO_PIN)
-# error "Audio feature enabled, but no suitable pin selected as AUDIO_PIN - see docs/feature_audio under 'ARM (DAC additive)' for available options."
-#endif
-#if defined(AUDIO_PIN_ALT) && !defined(AUDIO_PIN_ALT_AS_NEGATIVE)
-# pragma message "Audio feature: AUDIO_PIN_ALT set, but not AUDIO_PIN_ALT_AS_NEGATIVE - pin will be left unused; audio might still work though."
-#endif
-
-#if !defined(AUDIO_PIN_ALT)
-// no ALT pin defined is valid, but the c-ifs below need some value set
-# define AUDIO_PIN_ALT PAL_NOLINE
-#endif
-
-#if !defined(AUDIO_DAC_SAMPLE_WAVEFORM_SINE) && !defined(AUDIO_DAC_SAMPLE_WAVEFORM_TRIANGLE) && !defined(AUDIO_DAC_SAMPLE_WAVEFORM_SQUARE) && !defined(AUDIO_DAC_SAMPLE_WAVEFORM_TRAPEZOID)
-# define AUDIO_DAC_SAMPLE_WAVEFORM_SINE
-#endif
-
-#ifdef AUDIO_DAC_SAMPLE_WAVEFORM_SINE
-/* one full sine wave over [0,2*pi], but shifted up one amplitude and left pi/4; for the samples to start at 0
- */
-static const dacsample_t dac_buffer_sine[AUDIO_DAC_BUFFER_SIZE] = {
- // 256 values, max 4095
- 0x0, 0x1, 0x2, 0x6, 0xa, 0xf, 0x16, 0x1e, 0x27, 0x32, 0x3d, 0x4a, 0x58, 0x67, 0x78, 0x89, 0x9c, 0xb0, 0xc5, 0xdb, 0xf2, 0x10a, 0x123, 0x13e, 0x159, 0x175, 0x193, 0x1b1, 0x1d1, 0x1f1, 0x212, 0x235, 0x258, 0x27c, 0x2a0, 0x2c6, 0x2ed, 0x314, 0x33c, 0x365, 0x38e, 0x3b8, 0x3e3, 0x40e, 0x43a, 0x467, 0x494, 0x4c2, 0x4f0, 0x51f, 0x54e, 0x57d, 0x5ad, 0x5dd, 0x60e, 0x63f, 0x670, 0x6a1, 0x6d3, 0x705, 0x737, 0x769, 0x79b, 0x7cd, 0x800, 0x832, 0x864, 0x896, 0x8c8, 0x8fa, 0x92c, 0x95e, 0x98f, 0x9c0, 0x9f1, 0xa22, 0xa52, 0xa82, 0xab1, 0xae0, 0xb0f, 0xb3d, 0xb6b, 0xb98, 0xbc5, 0xbf1, 0xc1c, 0xc47, 0xc71, 0xc9a, 0xcc3, 0xceb, 0xd12, 0xd39, 0xd5f, 0xd83, 0xda7, 0xdca, 0xded, 0xe0e, 0xe2e, 0xe4e, 0xe6c, 0xe8a, 0xea6, 0xec1, 0xedc, 0xef5, 0xf0d, 0xf24, 0xf3a, 0xf4f, 0xf63, 0xf76, 0xf87, 0xf98, 0xfa7, 0xfb5, 0xfc2, 0xfcd, 0xfd8, 0xfe1, 0xfe9, 0xff0, 0xff5, 0xff9, 0xffd, 0xffe,
- 0xfff, 0xffe, 0xffd, 0xff9, 0xff5, 0xff0, 0xfe9, 0xfe1, 0xfd8, 0xfcd, 0xfc2, 0xfb5, 0xfa7, 0xf98, 0xf87, 0xf76, 0xf63, 0xf4f, 0xf3a, 0xf24, 0xf0d, 0xef5, 0xedc, 0xec1, 0xea6, 0xe8a, 0xe6c, 0xe4e, 0xe2e, 0xe0e, 0xded, 0xdca, 0xda7, 0xd83, 0xd5f, 0xd39, 0xd12, 0xceb, 0xcc3, 0xc9a, 0xc71, 0xc47, 0xc1c, 0xbf1, 0xbc5, 0xb98, 0xb6b, 0xb3d, 0xb0f, 0xae0, 0xab1, 0xa82, 0xa52, 0xa22, 0x9f1, 0x9c0, 0x98f, 0x95e, 0x92c, 0x8fa, 0x8c8, 0x896, 0x864, 0x832, 0x800, 0x7cd, 0x79b, 0x769, 0x737, 0x705, 0x6d3, 0x6a1, 0x670, 0x63f, 0x60e, 0x5dd, 0x5ad, 0x57d, 0x54e, 0x51f, 0x4f0, 0x4c2, 0x494, 0x467, 0x43a, 0x40e, 0x3e3, 0x3b8, 0x38e, 0x365, 0x33c, 0x314, 0x2ed, 0x2c6, 0x2a0, 0x27c, 0x258, 0x235, 0x212, 0x1f1, 0x1d1, 0x1b1, 0x193, 0x175, 0x159, 0x13e, 0x123, 0x10a, 0xf2, 0xdb, 0xc5, 0xb0, 0x9c, 0x89, 0x78, 0x67, 0x58, 0x4a, 0x3d, 0x32, 0x27, 0x1e, 0x16, 0xf, 0xa, 0x6, 0x2, 0x1};
-#endif // AUDIO_DAC_SAMPLE_WAVEFORM_SINE
-#ifdef AUDIO_DAC_SAMPLE_WAVEFORM_TRIANGLE
-static const dacsample_t dac_buffer_triangle[AUDIO_DAC_BUFFER_SIZE] = {
- // 256 values, max 4095
- 0x0, 0x20, 0x40, 0x60, 0x80, 0xa0, 0xc0, 0xe0, 0x100, 0x120, 0x140, 0x160, 0x180, 0x1a0, 0x1c0, 0x1e0, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0x400, 0x420, 0x440, 0x460, 0x480, 0x4a0, 0x4c0, 0x4e0, 0x500, 0x520, 0x540, 0x560, 0x580, 0x5a0, 0x5c0, 0x5e0, 0x600, 0x620, 0x640, 0x660, 0x680, 0x6a0, 0x6c0, 0x6e0, 0x700, 0x720, 0x740, 0x760, 0x780, 0x7a0, 0x7c0, 0x7e0, 0x800, 0x81f, 0x83f, 0x85f, 0x87f, 0x89f, 0x8bf, 0x8df, 0x8ff, 0x91f, 0x93f, 0x95f, 0x97f, 0x99f, 0x9bf, 0x9df, 0x9ff, 0xa1f, 0xa3f, 0xa5f, 0xa7f, 0xa9f, 0xabf, 0xadf, 0xaff, 0xb1f, 0xb3f, 0xb5f, 0xb7f, 0xb9f, 0xbbf, 0xbdf, 0xbff, 0xc1f, 0xc3f, 0xc5f, 0xc7f, 0xc9f, 0xcbf, 0xcdf, 0xcff, 0xd1f, 0xd3f, 0xd5f, 0xd7f, 0xd9f, 0xdbf, 0xddf, 0xdff, 0xe1f, 0xe3f, 0xe5f, 0xe7f, 0xe9f, 0xebf, 0xedf, 0xeff, 0xf1f, 0xf3f, 0xf5f, 0xf7f, 0xf9f, 0xfbf, 0xfdf,
- 0xfff, 0xfdf, 0xfbf, 0xf9f, 0xf7f, 0xf5f, 0xf3f, 0xf1f, 0xeff, 0xedf, 0xebf, 0xe9f, 0xe7f, 0xe5f, 0xe3f, 0xe1f, 0xdff, 0xddf, 0xdbf, 0xd9f, 0xd7f, 0xd5f, 0xd3f, 0xd1f, 0xcff, 0xcdf, 0xcbf, 0xc9f, 0xc7f, 0xc5f, 0xc3f, 0xc1f, 0xbff, 0xbdf, 0xbbf, 0xb9f, 0xb7f, 0xb5f, 0xb3f, 0xb1f, 0xaff, 0xadf, 0xabf, 0xa9f, 0xa7f, 0xa5f, 0xa3f, 0xa1f, 0x9ff, 0x9df, 0x9bf, 0x99f, 0x97f, 0x95f, 0x93f, 0x91f, 0x8ff, 0x8df, 0x8bf, 0x89f, 0x87f, 0x85f, 0x83f, 0x81f, 0x800, 0x7e0, 0x7c0, 0x7a0, 0x780, 0x760, 0x740, 0x720, 0x700, 0x6e0, 0x6c0, 0x6a0, 0x680, 0x660, 0x640, 0x620, 0x600, 0x5e0, 0x5c0, 0x5a0, 0x580, 0x560, 0x540, 0x520, 0x500, 0x4e0, 0x4c0, 0x4a0, 0x480, 0x460, 0x440, 0x420, 0x400, 0x3e0, 0x3c0, 0x3a0, 0x380, 0x360, 0x340, 0x320, 0x300, 0x2e0, 0x2c0, 0x2a0, 0x280, 0x260, 0x240, 0x220, 0x200, 0x1e0, 0x1c0, 0x1a0, 0x180, 0x160, 0x140, 0x120, 0x100, 0xe0, 0xc0, 0xa0, 0x80, 0x60, 0x40, 0x20};
-#endif // AUDIO_DAC_SAMPLE_WAVEFORM_TRIANGLE
-#ifdef AUDIO_DAC_SAMPLE_WAVEFORM_SQUARE
-static const dacsample_t dac_buffer_square[AUDIO_DAC_BUFFER_SIZE] = {
- [0 ... AUDIO_DAC_BUFFER_SIZE / 2 - 1] = 0, // first and
- [AUDIO_DAC_BUFFER_SIZE / 2 ... AUDIO_DAC_BUFFER_SIZE - 1] = AUDIO_DAC_SAMPLE_MAX, // second half
-};
-#endif // AUDIO_DAC_SAMPLE_WAVEFORM_SQUARE
-/*
-// four steps: 0, 1/3, 2/3 and 1
-static const dacsample_t dac_buffer_staircase[AUDIO_DAC_BUFFER_SIZE] = {
- [0 ... AUDIO_DAC_BUFFER_SIZE/3 -1 ] = 0,
- [AUDIO_DAC_BUFFER_SIZE / 4 ... AUDIO_DAC_BUFFER_SIZE / 2 -1 ] = AUDIO_DAC_SAMPLE_MAX / 3,
- [AUDIO_DAC_BUFFER_SIZE / 2 ... 3 * AUDIO_DAC_BUFFER_SIZE / 4 -1 ] = 2 * AUDIO_DAC_SAMPLE_MAX / 3,
- [3 * AUDIO_DAC_BUFFER_SIZE / 4 ... AUDIO_DAC_BUFFER_SIZE -1 ] = AUDIO_DAC_SAMPLE_MAX,
-}
-*/
-#ifdef AUDIO_DAC_SAMPLE_WAVEFORM_TRAPEZOID
-static const dacsample_t dac_buffer_trapezoid[AUDIO_DAC_BUFFER_SIZE] = {0x0, 0x1f, 0x7f, 0xdf, 0x13f, 0x19f, 0x1ff, 0x25f, 0x2bf, 0x31f, 0x37f, 0x3df, 0x43f, 0x49f, 0x4ff, 0x55f, 0x5bf, 0x61f, 0x67f, 0x6df, 0x73f, 0x79f, 0x7ff, 0x85f, 0x8bf, 0x91f, 0x97f, 0x9df, 0xa3f, 0xa9f, 0xaff, 0xb5f, 0xbbf, 0xc1f, 0xc7f, 0xcdf, 0xd3f, 0xd9f, 0xdff, 0xe5f, 0xebf, 0xf1f, 0xf7f, 0xfdf, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff, 0xfff,
- 0xfff, 0xfdf, 0xf7f, 0xf1f, 0xebf, 0xe5f, 0xdff, 0xd9f, 0xd3f, 0xcdf, 0xc7f, 0xc1f, 0xbbf, 0xb5f, 0xaff, 0xa9f, 0xa3f, 0x9df, 0x97f, 0x91f, 0x8bf, 0x85f, 0x7ff, 0x79f, 0x73f, 0x6df, 0x67f, 0x61f, 0x5bf, 0x55f, 0x4ff, 0x49f, 0x43f, 0x3df, 0x37f, 0x31f, 0x2bf, 0x25f, 0x1ff, 0x19f, 0x13f, 0xdf, 0x7f, 0x1f, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
-#endif // AUDIO_DAC_SAMPLE_WAVEFORM_TRAPEZOID
-
-static dacsample_t dac_buffer_empty[AUDIO_DAC_BUFFER_SIZE] = {AUDIO_DAC_OFF_VALUE};
-
-/* keep track of the sample position for for each frequency */
-static float dac_if[AUDIO_MAX_SIMULTANEOUS_TONES] = {0.0};
-
-static float active_tones_snapshot[AUDIO_MAX_SIMULTANEOUS_TONES] = {0, 0};
-static uint8_t active_tones_snapshot_length = 0;
-
-typedef enum {
- OUTPUT_SHOULD_START,
- OUTPUT_RUN_NORMALLY,
- // path 1: wait for zero, then change/update active tones
- OUTPUT_TONES_CHANGED,
- OUTPUT_REACHED_ZERO_BEFORE_TONE_CHANGE,
- // path 2: hardware should stop, wait for zero then turn output off = stop the timer
- OUTPUT_SHOULD_STOP,
- OUTPUT_REACHED_ZERO_BEFORE_OFF,
- OUTPUT_OFF,
- OUTPUT_OFF_1,
- OUTPUT_OFF_2, // trailing off: giving the DAC two more conversion cycles until the AUDIO_DAC_OFF_VALUE reaches the output, then turn the timer off, which leaves the output at that level
- number_of_output_states
-} output_states_t;
-output_states_t state = OUTPUT_OFF_2;
-
-/**
- * Generation of the waveform being passed to the callback. Declared weak so users
- * can override it with their own wave-forms/noises.
- */
-__attribute__((weak)) uint16_t dac_value_generate(void) {
- // DAC is running/asking for values but snapshot length is zero -> must be playing a pause
- if (active_tones_snapshot_length == 0) {
- return AUDIO_DAC_OFF_VALUE;
- }
-
- /* doing additive wave synthesis over all currently playing tones = adding up
- * sine-wave-samples for each frequency, scaled by the number of active tones
- */
- uint16_t value = 0;
- float frequency = 0.0f;
-
- for (uint8_t i = 0; i < active_tones_snapshot_length; i++) {
- /* Note: a user implementation does not have to rely on the active_tones_snapshot, but
- * could directly query the active frequencies through audio_get_processed_frequency */
- frequency = active_tones_snapshot[i];
-
- dac_if[i] = dac_if[i] + ((frequency * AUDIO_DAC_BUFFER_SIZE) / AUDIO_DAC_SAMPLE_RATE) * 2 / 3;
- /*Note: the 2/3 are necessary to get the correct frequencies on the
- * DAC output (as measured with an oscilloscope), since the gpt
- * timer runs with 3*AUDIO_DAC_SAMPLE_RATE; and the DAC callback
- * is called twice per conversion.*/
-
- dac_if[i] = fmod(dac_if[i], AUDIO_DAC_BUFFER_SIZE);
-
- // Wavetable generation/lookup
- uint16_t dac_i = (uint16_t)dac_if[i];
-
-#if defined(AUDIO_DAC_SAMPLE_WAVEFORM_SINE)
- value += dac_buffer_sine[dac_i] / active_tones_snapshot_length;
-#elif defined(AUDIO_DAC_SAMPLE_WAVEFORM_TRIANGLE)
- value += dac_buffer_triangle[dac_i] / active_tones_snapshot_length;
-#elif defined(AUDIO_DAC_SAMPLE_WAVEFORM_TRAPEZOID)
- value += dac_buffer_trapezoid[dac_i] / active_tones_snapshot_length;
-#elif defined(AUDIO_DAC_SAMPLE_WAVEFORM_SQUARE)
- value += dac_buffer_square[dac_i] / active_tones_snapshot_length;
-#endif
- /*
- // SINE
- value += dac_buffer_sine[dac_i] / active_tones_snapshot_length / 3;
- // TRIANGLE
- value += dac_buffer_triangle[dac_i] / active_tones_snapshot_length / 3;
- // SQUARE
- value += dac_buffer_square[dac_i] / active_tones_snapshot_length / 3;
- //NOTE: combination of these three wave-forms is more exemplary - and doesn't sound particularly good :-P
- */
-
- // STAIRS (mostly usefully as test-pattern)
- // value_avg = dac_buffer_staircase[dac_i] / active_tones_snapshot_length;
- }
-
- return value;
-}
-
-/**
- * DAC streaming callback. Does all of the main computing for playing songs.
- *
- * Note: chibios calls this CB twice: during the 'half buffer event', and the 'full buffer event'.
- */
-static void dac_end(DACDriver *dacp) {
- dacsample_t *sample_p = (dacp)->samples;
-
- // work on the other half of the buffer
- if (dacIsBufferComplete(dacp)) {
- sample_p += AUDIO_DAC_BUFFER_SIZE / 2; // 'half_index'
- }
-
- for (uint8_t s = 0; s < AUDIO_DAC_BUFFER_SIZE / 2; s++) {
- if (OUTPUT_OFF <= state) {
- sample_p[s] = AUDIO_DAC_OFF_VALUE;
- continue;
- } else {
- sample_p[s] = dac_value_generate();
- }
-
- /* zero crossing (or approach, whereas zero == DAC_OFF_VALUE, which can be configured to anything from 0 to DAC_SAMPLE_MAX)
- * ============================*=*========================== AUDIO_DAC_SAMPLE_MAX
- * * *
- * * *
- * ---------------------------------------------------------
- * * * } AUDIO_DAC_SAMPLE_MAX/100
- * --------------------------------------------------------- AUDIO_DAC_OFF_VALUE
- * * * } AUDIO_DAC_SAMPLE_MAX/100
- * ---------------------------------------------------------
- * *
- * * *
- * * *
- * =====*=*================================================= 0x0
- */
- if (((sample_p[s] + (AUDIO_DAC_SAMPLE_MAX / 100)) > AUDIO_DAC_OFF_VALUE) && // value approaches from below
- (sample_p[s] < (AUDIO_DAC_OFF_VALUE + (AUDIO_DAC_SAMPLE_MAX / 100))) // or above
- ) {
- if ((OUTPUT_SHOULD_START == state) && (active_tones_snapshot_length > 0)) {
- state = OUTPUT_RUN_NORMALLY;
- } else if (OUTPUT_TONES_CHANGED == state) {
- state = OUTPUT_REACHED_ZERO_BEFORE_TONE_CHANGE;
- } else if (OUTPUT_SHOULD_STOP == state) {
- state = OUTPUT_REACHED_ZERO_BEFORE_OFF;
- }
- }
-
- // still 'ramping up', reset the output to OFF_VALUE until the generated values reach that value, to do a smooth handover
- if (OUTPUT_SHOULD_START == state) {
- sample_p[s] = AUDIO_DAC_OFF_VALUE;
- }
-
- if ((OUTPUT_SHOULD_START == state) || (OUTPUT_REACHED_ZERO_BEFORE_OFF == state) || (OUTPUT_REACHED_ZERO_BEFORE_TONE_CHANGE == state)) {
- uint8_t active_tones = MIN(AUDIO_MAX_SIMULTANEOUS_TONES, audio_get_number_of_active_tones());
- active_tones_snapshot_length = 0;
- // update the snapshot - once, and only on occasion that something changed;
- // -> saves cpu cycles (?)
- for (uint8_t i = 0; i < active_tones; i++) {
- float freq = audio_get_processed_frequency(i);
- if (freq > 0) { // disregard 'rest' notes, with valid frequency 0.0f; which would only lower the resulting waveform volume during the additive synthesis step
- active_tones_snapshot[active_tones_snapshot_length++] = freq;
- }
- }
-
- if ((0 == active_tones_snapshot_length) && (OUTPUT_REACHED_ZERO_BEFORE_OFF == state)) {
- state = OUTPUT_OFF;
- }
- if (OUTPUT_REACHED_ZERO_BEFORE_TONE_CHANGE == state) {
- state = OUTPUT_RUN_NORMALLY;
- }
- }
- }
-
- // update audio internal state (note position, current_note, ...)
- if (audio_update_state()) {
- if (OUTPUT_SHOULD_STOP != state) {
- state = OUTPUT_TONES_CHANGED;
- }
- }
-
- if (OUTPUT_OFF <= state) {
- if (OUTPUT_OFF_2 == state) {
- // stopping timer6 = stopping the DAC at whatever value it is currently pushing to the output = AUDIO_DAC_OFF_VALUE
- gptStopTimer(&GPTD6);
- } else {
- state++;
- }
- }
-}
-
-static void dac_error(DACDriver *dacp, dacerror_t err) {
- (void)dacp;
- (void)err;
-
- chSysHalt("DAC failure. halp");
-}
-
-static const GPTConfig gpt6cfg1 = {.frequency = AUDIO_DAC_SAMPLE_RATE * 3,
- .callback = NULL,
- .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
- .dier = 0U};
-
-static const DACConfig dac_conf = {.init = AUDIO_DAC_OFF_VALUE, .datamode = DAC_DHRM_12BIT_RIGHT};
-
-/**
- * @note The DAC_TRG(0) here selects the Timer 6 TRGO event, which is triggered
- * on the rising edge after 3 APB1 clock cycles, causing our gpt6cfg1.frequency
- * to be a third of what we expect.
- *
- * Here are all the values for DAC_TRG (TSEL in the ref manual)
- * TIM15_TRGO 0b011
- * TIM2_TRGO 0b100
- * TIM3_TRGO 0b001
- * TIM6_TRGO 0b000
- * TIM7_TRGO 0b010
- * EXTI9 0b110
- * SWTRIG 0b111
- */
-static const DACConversionGroup dac_conv_cfg = {.num_channels = 1U, .end_cb = dac_end, .error_cb = dac_error, .trigger = DAC_TRG(0b000)};
-
-void audio_driver_initialize() {
- if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
- palSetLineMode(A4, PAL_MODE_INPUT_ANALOG);
- dacStart(&DACD1, &dac_conf);
- }
- if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
- palSetLineMode(A5, PAL_MODE_INPUT_ANALOG);
- dacStart(&DACD2, &dac_conf);
- }
-
- /* enable the output buffer, to directly drive external loads with no additional circuitry
- *
- * see: AN4566 Application note: Extending the DAC performance of STM32 microcontrollers
- * Note: Buffer-Off bit -> has to be set 0 to enable the output buffer
- * Note: enabling the output buffer imparts an additional dc-offset of a couple mV
- *
- * this is done here, reaching directly into the stm32 registers since chibios has not implemented BOFF handling yet
- * (see: chibios/os/hal/ports/STM32/todo.txt '- BOFF handling in DACv1.'
- */
- DACD1.params->dac->CR &= ~DAC_CR_BOFF1;
- DACD2.params->dac->CR &= ~DAC_CR_BOFF2;
-
- if (AUDIO_PIN == A4) {
- dacStartConversion(&DACD1, &dac_conv_cfg, dac_buffer_empty, AUDIO_DAC_BUFFER_SIZE);
- } else if (AUDIO_PIN == A5) {
- dacStartConversion(&DACD2, &dac_conv_cfg, dac_buffer_empty, AUDIO_DAC_BUFFER_SIZE);
- }
-
- // no inverted/out-of-phase waveform (yet?), only pulling AUDIO_PIN_ALT to AUDIO_DAC_OFF_VALUE
-#if defined(AUDIO_PIN_ALT_AS_NEGATIVE)
- if (AUDIO_PIN_ALT == A4) {
- dacPutChannelX(&DACD1, 0, AUDIO_DAC_OFF_VALUE);
- } else if (AUDIO_PIN_ALT == A5) {
- dacPutChannelX(&DACD2, 0, AUDIO_DAC_OFF_VALUE);
- }
-#endif
-
- gptStart(&GPTD6, &gpt6cfg1);
-}
-
-void audio_driver_stop(void) { state = OUTPUT_SHOULD_STOP; }
-
-void audio_driver_start(void) {
- gptStartContinuous(&GPTD6, 2U);
-
- for (uint8_t i = 0; i < AUDIO_MAX_SIMULTANEOUS_TONES; i++) {
- dac_if[i] = 0.0f;
- active_tones_snapshot[i] = 0.0f;
- }
- active_tones_snapshot_length = 0;
- state = OUTPUT_SHOULD_START;
-}
diff --git a/quantum/audio/driver_chibios_dac_basic.c b/quantum/audio/driver_chibios_dac_basic.c
deleted file mode 100644
index fac6513506..0000000000
--- a/quantum/audio/driver_chibios_dac_basic.c
+++ /dev/null
@@ -1,245 +0,0 @@
-/* Copyright 2016-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/>.
- */
-
-#include "audio.h"
-#include "ch.h"
-#include "hal.h"
-
-/*
- Audio Driver: DAC
-
- which utilizes both channels of the DAC unit many STM32 are equipped with to output a modulated square-wave, from precomputed samples stored in a buffer, which is passed to the hardware through DMA
-
- this driver can either be used to drive to separate speakers, wired to A4+Gnd and A5+Gnd, which allows two tones to be played simultaneously
- OR
- one speaker wired to A4+A5 with the AUDIO_PIN_ALT_AS_NEGATIVE define set - see docs/feature_audio
-
-*/
-
-#if !defined(AUDIO_PIN)
-# pragma message "Audio feature enabled, but no suitable pin selected as AUDIO_PIN - see docs/feature_audio under 'ARM (DAC basic)' for available options."
-// TODO: make this an 'error' instead; go through a breaking change, and add AUDIO_PIN A5 to all keyboards currently using AUDIO on STM32 based boards? - for now: set the define here
-# define AUDIO_PIN A5
-#endif
-// check configuration for ONE speaker, connected to both DAC pins
-#if defined(AUDIO_PIN_ALT_AS_NEGATIVE) && !defined(AUDIO_PIN_ALT)
-# error "Audio feature: AUDIO_PIN_ALT_AS_NEGATIVE set, but no pin configured as AUDIO_PIN_ALT"
-#endif
-
-#ifndef AUDIO_PIN_ALT
-// no ALT pin defined is valid, but the c-ifs below need some value set
-# define AUDIO_PIN_ALT -1
-#endif
-
-#if !defined(AUDIO_STATE_TIMER)
-# define AUDIO_STATE_TIMER GPTD8
-#endif
-
-// square-wave
-static const dacsample_t dac_buffer_1[AUDIO_DAC_BUFFER_SIZE] = {
- // First half is max, second half is 0
- [0 ... AUDIO_DAC_BUFFER_SIZE / 2 - 1] = AUDIO_DAC_SAMPLE_MAX,
- [AUDIO_DAC_BUFFER_SIZE / 2 ... AUDIO_DAC_BUFFER_SIZE - 1] = 0,
-};
-
-// square-wave
-static const dacsample_t dac_buffer_2[AUDIO_DAC_BUFFER_SIZE] = {
- // opposite of dac_buffer above
- [0 ... AUDIO_DAC_BUFFER_SIZE / 2 - 1] = 0,
- [AUDIO_DAC_BUFFER_SIZE / 2 ... AUDIO_DAC_BUFFER_SIZE - 1] = AUDIO_DAC_SAMPLE_MAX,
-};
-
-GPTConfig gpt6cfg1 = {.frequency = AUDIO_DAC_SAMPLE_RATE,
- .callback = NULL,
- .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
- .dier = 0U};
-GPTConfig gpt7cfg1 = {.frequency = AUDIO_DAC_SAMPLE_RATE,
- .callback = NULL,
- .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
- .dier = 0U};
-
-static void gpt_audio_state_cb(GPTDriver *gptp);
-GPTConfig gptStateUpdateCfg = {.frequency = 10,
- .callback = gpt_audio_state_cb,
- .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
- .dier = 0U};
-
-static const DACConfig dac_conf_ch1 = {.init = AUDIO_DAC_OFF_VALUE, .datamode = DAC_DHRM_12BIT_RIGHT};
-static const DACConfig dac_conf_ch2 = {.init = AUDIO_DAC_OFF_VALUE, .datamode = DAC_DHRM_12BIT_RIGHT};
-
-/**
- * @note The DAC_TRG(0) here selects the Timer 6 TRGO event, which is triggered
- * on the rising edge after 3 APB1 clock cycles, causing our gpt6cfg1.frequency
- * to be a third of what we expect.
- *
- * Here are all the values for DAC_TRG (TSEL in the ref manual)
- * TIM15_TRGO 0b011
- * TIM2_TRGO 0b100
- * TIM3_TRGO 0b001
- * TIM6_TRGO 0b000
- * TIM7_TRGO 0b010
- * EXTI9 0b110
- * SWTRIG 0b111
- */
-static const DACConversionGroup dac_conv_grp_ch1 = {.num_channels = 1U, .trigger = DAC_TRG(0b000)};
-static const DACConversionGroup dac_conv_grp_ch2 = {.num_channels = 1U, .trigger = DAC_TRG(0b010)};
-
-void channel_1_start(void) {
- gptStart(&GPTD6, &gpt6cfg1);
- gptStartContinuous(&GPTD6, 2U);
- palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
-}
-
-void channel_1_stop(void) {
- gptStopTimer(&GPTD6);
- palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL);
- palSetPad(GPIOA, 4);
-}
-
-static float channel_1_frequency = 0.0f;
-void channel_1_set_frequency(float freq) {
- channel_1_frequency = freq;
-
- channel_1_stop();
- if (freq <= 0.0) // a pause/rest has freq=0
- return;
-
- gpt6cfg1.frequency = 2 * freq * AUDIO_DAC_BUFFER_SIZE;
- channel_1_start();
-}
-float channel_1_get_frequency(void) { return channel_1_frequency; }
-
-void channel_2_start(void) {
- gptStart(&GPTD7, &gpt7cfg1);
- gptStartContinuous(&GPTD7, 2U);
- palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
-}
-
-void channel_2_stop(void) {
- gptStopTimer(&GPTD7);
- palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL);
- palSetPad(GPIOA, 5);
-}
-
-static float channel_2_frequency = 0.0f;
-void channel_2_set_frequency(float freq) {
- channel_2_frequency = freq;
-
- channel_2_stop();
- if (freq <= 0.0) // a pause/rest has freq=0
- return;
-
- gpt7cfg1.frequency = 2 * freq * AUDIO_DAC_BUFFER_SIZE;
- channel_2_start();
-}
-float channel_2_get_frequency(void) { return channel_2_frequency; }
-
-static void gpt_audio_state_cb(GPTDriver *gptp) {
- if (audio_update_state()) {
-#if defined(AUDIO_PIN_ALT_AS_NEGATIVE)
- // one piezo/speaker connected to both audio pins, the generated square-waves are inverted
- channel_1_set_frequency(audio_get_processed_frequency(0));
- channel_2_set_frequency(audio_get_processed_frequency(0));
-
-#else // two separate audio outputs/speakers
- // primary speaker on A4, optional secondary on A5
- if (AUDIO_PIN == A4) {
- channel_1_set_frequency(audio_get_processed_frequency(0));
- if (AUDIO_PIN_ALT == A5) {
- if (audio_get_number_of_active_tones() > 1) {
- channel_2_set_frequency(audio_get_processed_frequency(1));
- } else {
- channel_2_stop();
- }
- }
- }
-
- // primary speaker on A5, optional secondary on A4
- if (AUDIO_PIN == A5) {
- channel_2_set_frequency(audio_get_processed_frequency(0));
- if (AUDIO_PIN_ALT == A4) {
- if (audio_get_number_of_active_tones() > 1) {
- channel_1_set_frequency(audio_get_processed_frequency(1));
- } else {
- channel_1_stop();
- }
- }
- }
-#endif
- }
-}
-
-void audio_driver_initialize() {
- if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
- palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
- dacStart(&DACD1, &dac_conf_ch1);
-
- // initial setup of the dac-triggering timer is still required, even
- // though it gets reconfigured and restarted later on
- gptStart(&GPTD6, &gpt6cfg1);
- }
-
- if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
- palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
- dacStart(&DACD2, &dac_conf_ch2);
-
- gptStart(&GPTD7, &gpt7cfg1);
- }
-
- /* enable the output buffer, to directly drive external loads with no additional circuitry
- *
- * see: AN4566 Application note: Extending the DAC performance of STM32 microcontrollers
- * Note: Buffer-Off bit -> has to be set 0 to enable the output buffer
- * Note: enabling the output buffer imparts an additional dc-offset of a couple mV
- *
- * this is done here, reaching directly into the stm32 registers since chibios has not implemented BOFF handling yet
- * (see: chibios/os/hal/ports/STM32/todo.txt '- BOFF handling in DACv1.'
- */
- DACD1.params->dac->CR &= ~DAC_CR_BOFF1;
- DACD2.params->dac->CR &= ~DAC_CR_BOFF2;
-
- // start state-updater
- gptStart(&AUDIO_STATE_TIMER, &gptStateUpdateCfg);
-}
-
-void audio_driver_stop(void) {
- if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
- gptStopTimer(&GPTD6);
-
- // stop the ongoing conversion and put the output in a known state
- dacStopConversion(&DACD1);
- dacPutChannelX(&DACD1, 0, AUDIO_DAC_OFF_VALUE);
- }
-
- if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
- gptStopTimer(&GPTD7);
-
- dacStopConversion(&DACD2);
- dacPutChannelX(&DACD2, 0, AUDIO_DAC_OFF_VALUE);
- }
- gptStopTimer(&AUDIO_STATE_TIMER);
-}
-
-void audio_driver_start(void) {
- if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
- dacStartConversion(&DACD1, &dac_conv_grp_ch1, (dacsample_t *)dac_buffer_1, AUDIO_DAC_BUFFER_SIZE);
- }
- if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
- dacStartConversion(&DACD2, &dac_conv_grp_ch2, (dacsample_t *)dac_buffer_2, AUDIO_DAC_BUFFER_SIZE);
- }
- gptStartContinuous(&AUDIO_STATE_TIMER, 2U);
-}
diff --git a/quantum/audio/driver_chibios_pwm.h b/quantum/audio/driver_chibios_pwm.h
deleted file mode 100644
index 86cab916e1..0000000000
--- a/quantum/audio/driver_chibios_pwm.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/* 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
-
-#if !defined(AUDIO_PWM_DRIVER)
-// NOTE: Timer2 seems to be used otherwise in QMK, otherwise we could default to A5 (= TIM2_CH1, with PWMD2 and alternate-function(1))
-# define AUDIO_PWM_DRIVER PWMD1
-#endif
-
-#if !defined(AUDIO_PWM_CHANNEL)
-// NOTE: sticking to the STM data-sheet numbering: TIMxCH1 to TIMxCH4
-// default: STM32F303CC PA8+TIM1_CH1 -> 1
-# define AUDIO_PWM_CHANNEL 1
-#endif
-
-#if !defined(AUDIO_PWM_PAL_MODE)
-// pin-alternate function: see the data-sheet for which pin needs what AF to connect to TIMx_CHy
-// default: STM32F303CC PA8+TIM1_CH1 -> 6
-# define AUDIO_PWM_PAL_MODE 6
-#endif
-
-#if !defined(AUDIO_STATE_TIMER)
-// timer used to trigger updates in the audio-system, configured/enabled in chibios mcuconf.
-// Tim6 is the default for "larger" STMs, smaller ones might not have this one (enabled) and need to switch to a different one (e.g.: STM32F103 has only Tim1-Tim4)
-# define AUDIO_STATE_TIMER GPTD6
-#endif
diff --git a/quantum/audio/driver_chibios_pwm_hardware.c b/quantum/audio/driver_chibios_pwm_hardware.c
deleted file mode 100644
index 3c7d89b290..0000000000
--- a/quantum/audio/driver_chibios_pwm_hardware.c
+++ /dev/null
@@ -1,144 +0,0 @@
-/* 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/>.
- */
-
-/*
-Audio Driver: PWM
-
-the duty-cycle is always kept at 50%, and the pwm-period is adjusted to match the frequency of a note to be played back.
-
-this driver uses the chibios-PWM system to produce a square-wave on specific output pins that are connected to the PWM hardware.
-The hardware directly toggles the pin via its alternate function. see your MCUs data-sheet for which pin can be driven by what timer - looking for TIMx_CHy and the corresponding alternate function.
-
- */
-
-#include "audio.h"
-#include "ch.h"
-#include "hal.h"
-
-#if !defined(AUDIO_PIN)
-# error "Audio feature enabled, but no pin selected - see docs/feature_audio under the ARM PWM settings"
-#endif
-
-extern bool playing_note;
-extern bool playing_melody;
-extern uint8_t note_timbre;
-
-static PWMConfig pwmCFG = {
- .frequency = 100000, /* PWM clock frequency */
- // CHIBIOS-BUG? can't set the initial period to <2, or the pwm (hard or software) takes ~130ms with .frequency=500000 for a pwmChangePeriod to take effect; with no output=silence in the meantime
- .period = 2, /* initial PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
- .callback = NULL, /* no callback, the hardware directly toggles the pin */
- .channels =
- {
-#if AUDIO_PWM_CHANNEL == 4
- {PWM_OUTPUT_DISABLED, NULL}, /* channel 0 -> TIMx_CH1 */
- {PWM_OUTPUT_DISABLED, NULL}, /* channel 1 -> TIMx_CH2 */
- {PWM_OUTPUT_DISABLED, NULL}, /* channel 2 -> TIMx_CH3 */
- {PWM_OUTPUT_ACTIVE_HIGH, NULL} /* channel 3 -> TIMx_CH4 */
-#elif AUDIO_PWM_CHANNEL == 3
- {PWM_OUTPUT_DISABLED, NULL},
- {PWM_OUTPUT_DISABLED, NULL},
- {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* TIMx_CH3 */
- {PWM_OUTPUT_DISABLED, NULL}
-#elif AUDIO_PWM_CHANNEL == 2
- {PWM_OUTPUT_DISABLED, NULL},
- {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* TIMx_CH2 */
- {PWM_OUTPUT_DISABLED, NULL},
- {PWM_OUTPUT_DISABLED, NULL}
-#else /*fallback to CH1 */
- {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* TIMx_CH1 */
- {PWM_OUTPUT_DISABLED, NULL},
- {PWM_OUTPUT_DISABLED, NULL},
- {PWM_OUTPUT_DISABLED, NULL}
-#endif
- },
-};
-
-static float channel_1_frequency = 0.0f;
-void channel_1_set_frequency(float freq) {
- channel_1_frequency = freq;
-
- if (freq <= 0.0) // a pause/rest has freq=0
- return;
-
- pwmcnt_t period = (pwmCFG.frequency / freq);
- pwmChangePeriod(&AUDIO_PWM_DRIVER, period);
- pwmEnableChannel(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1,
- // adjust the duty-cycle so that the output is for 'note_timbre' duration HIGH
- PWM_PERCENTAGE_TO_WIDTH(&AUDIO_PWM_DRIVER, (100 - note_timbre) * 100));
-}
-
-float channel_1_get_frequency(void) { return channel_1_frequency; }
-
-void channel_1_start(void) {
- pwmStop(&AUDIO_PWM_DRIVER);
- pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
-}
-
-void channel_1_stop(void) { pwmStop(&AUDIO_PWM_DRIVER); }
-
-static void gpt_callback(GPTDriver *gptp);
-GPTConfig gptCFG = {
- /* a whole note is one beat, which is - per definition in musical_notes.h - set to 64
- the longest note is BREAVE_DOT=128+64=192, the shortest SIXTEENTH=4
- the tempo (which might vary!) is in bpm (beats per minute)
- therefore: if the timer ticks away at .frequency = (60*64)Hz,
- and the .interval counts from 64 downwards - audio_update_state is
- called just often enough to not miss any notes
- */
- .frequency = 60 * 64,
- .callback = gpt_callback,
-};
-
-void audio_driver_initialize(void) {
- pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
-
- // connect the AUDIO_PIN to the PWM hardware
-#if defined(USE_GPIOV1) // STM32F103C8
- palSetLineMode(AUDIO_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
-#else // GPIOv2 (or GPIOv3 for f4xx, which is the same/compatible at this command)
- palSetLineMode(AUDIO_PIN, PAL_STM32_MODE_ALTERNATE | PAL_STM32_ALTERNATE(AUDIO_PWM_PAL_MODE));
-#endif
-
- gptStart(&AUDIO_STATE_TIMER, &gptCFG);
-}
-
-void audio_driver_start(void) {
- channel_1_stop();
- channel_1_start();
-
- if (playing_note || playing_melody) {
- gptStartContinuous(&AUDIO_STATE_TIMER, 64);
- }
-}
-
-void audio_driver_stop(void) {
- channel_1_stop();
- gptStopTimer(&AUDIO_STATE_TIMER);
-}
-
-/* a regular timer task, that checks the note to be currently played
- * and updates the pwm to output that frequency
- */
-static void gpt_callback(GPTDriver *gptp) {
- float freq; // TODO: freq_alt
-
- if (audio_update_state()) {
- freq = audio_get_processed_frequency(0); // freq_alt would be index=1
- channel_1_set_frequency(freq);
- }
-}
diff --git a/quantum/audio/driver_chibios_pwm_software.c b/quantum/audio/driver_chibios_pwm_software.c
deleted file mode 100644
index 15c3e98b6a..0000000000
--- a/quantum/audio/driver_chibios_pwm_software.c
+++ /dev/null
@@ -1,164 +0,0 @@
-/* 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/>.
- */
-
-/*
-Audio Driver: PWM
-
-the duty-cycle is always kept at 50%, and the pwm-period is adjusted to match the frequency of a note to be played back.
-
-this driver uses the chibios-PWM system to produce a square-wave on any given output pin in software
-- a pwm callback is used to set/clear the configured pin.
-
- */
-#include "audio.h"
-#include "ch.h"
-#include "hal.h"
-
-#if !defined(AUDIO_PIN)
-# error "Audio feature enabled, but no pin selected - see docs/feature_audio under the ARM PWM settings"
-#endif
-extern bool playing_note;
-extern bool playing_melody;
-extern uint8_t note_timbre;
-
-static void pwm_audio_period_callback(PWMDriver *pwmp);
-static void pwm_audio_channel_interrupt_callback(PWMDriver *pwmp);
-
-static PWMConfig pwmCFG = {
- .frequency = 100000, /* PWM clock frequency */
- // CHIBIOS-BUG? can't set the initial period to <2, or the pwm (hard or software) takes ~130ms with .frequency=500000 for a pwmChangePeriod to take effect; with no output=silence in the meantime
- .period = 2, /* initial PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
- .callback = pwm_audio_period_callback,
- .channels =
- {
- // software-PWM just needs another callback on any channel
- {PWM_OUTPUT_ACTIVE_HIGH, pwm_audio_channel_interrupt_callback}, /* channel 0 -> TIMx_CH1 */
- {PWM_OUTPUT_DISABLED, NULL}, /* channel 1 -> TIMx_CH2 */
- {PWM_OUTPUT_DISABLED, NULL}, /* channel 2 -> TIMx_CH3 */
- {PWM_OUTPUT_DISABLED, NULL} /* channel 3 -> TIMx_CH4 */
- },
-};
-
-static float channel_1_frequency = 0.0f;
-void channel_1_set_frequency(float freq) {
- channel_1_frequency = freq;
-
- if (freq <= 0.0) // a pause/rest has freq=0
- return;
-
- pwmcnt_t period = (pwmCFG.frequency / freq);
- pwmChangePeriod(&AUDIO_PWM_DRIVER, period);
-
- pwmEnableChannel(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1,
- // adjust the duty-cycle so that the output is for 'note_timbre' duration HIGH
- PWM_PERCENTAGE_TO_WIDTH(&AUDIO_PWM_DRIVER, (100 - note_timbre) * 100));
-}
-
-float channel_1_get_frequency(void) { return channel_1_frequency; }
-
-void channel_1_start(void) {
- pwmStop(&AUDIO_PWM_DRIVER);
- pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
-
- pwmEnablePeriodicNotification(&AUDIO_PWM_DRIVER);
- pwmEnableChannelNotification(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1);
-}
-
-void channel_1_stop(void) {
- pwmStop(&AUDIO_PWM_DRIVER);
-
- palClearLine(AUDIO_PIN); // leave the line low, after last note was played
-
-#if defined(AUDIO_PIN_ALT) && defined(AUDIO_PIN_ALT_AS_NEGATIVE)
- palClearLine(AUDIO_PIN_ALT); // leave the line low, after last note was played
-#endif
-}
-
-// generate a PWM signal on any pin, not necessarily the one connected to the timer
-static void pwm_audio_period_callback(PWMDriver *pwmp) {
- (void)pwmp;
- palClearLine(AUDIO_PIN);
-
-#if defined(AUDIO_PIN_ALT) && defined(AUDIO_PIN_ALT_AS_NEGATIVE)
- palSetLine(AUDIO_PIN_ALT);
-#endif
-}
-static void pwm_audio_channel_interrupt_callback(PWMDriver *pwmp) {
- (void)pwmp;
- if (channel_1_frequency > 0) {
- palSetLine(AUDIO_PIN); // generate a PWM signal on any pin, not necessarily the one connected to the timer
-#if defined(AUDIO_PIN_ALT) && defined(AUDIO_PIN_ALT_AS_NEGATIVE)
- palClearLine(AUDIO_PIN_ALT);
-#endif
- }
-}
-
-static void gpt_callback(GPTDriver *gptp);
-GPTConfig gptCFG = {
- /* a whole note is one beat, which is - per definition in musical_notes.h - set to 64
- the longest note is BREAVE_DOT=128+64=192, the shortest SIXTEENTH=4
- the tempo (which might vary!) is in bpm (beats per minute)
- therefore: if the timer ticks away at .frequency = (60*64)Hz,
- and the .interval counts from 64 downwards - audio_update_state is
- called just often enough to not miss anything
- */
- .frequency = 60 * 64,
- .callback = gpt_callback,
-};
-
-void audio_driver_initialize(void) {
- pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
-
- palSetLineMode(AUDIO_PIN, PAL_MODE_OUTPUT_PUSHPULL);
- palClearLine(AUDIO_PIN);
-
-#if defined(AUDIO_PIN_ALT) && defined(AUDIO_PIN_ALT_AS_NEGATIVE)
- palSetLineMode(AUDIO_PIN_ALT, PAL_MODE_OUTPUT_PUSHPULL);
- palClearLine(AUDIO_PIN_ALT);
-#endif
-
- pwmEnablePeriodicNotification(&AUDIO_PWM_DRIVER); // enable pwm callbacks
- pwmEnableChannelNotification(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1);
-
- gptStart(&AUDIO_STATE_TIMER, &gptCFG);
-}
-
-void audio_driver_start(void) {
- channel_1_stop();
- channel_1_start();
-
- if (playing_note || playing_melody) {
- gptStartContinuous(&AUDIO_STATE_TIMER, 64);
- }
-}
-
-void audio_driver_stop(void) {
- channel_1_stop();
- gptStopTimer(&AUDIO_STATE_TIMER);
-}
-
-/* a regular timer task, that checks the note to be currently played
- * and updates the pwm to output that frequency
- */
-static void gpt_callback(GPTDriver *gptp) {
- float freq; // TODO: freq_alt
-
- if (audio_update_state()) {
- freq = audio_get_processed_frequency(0); // freq_alt would be index=1
- channel_1_set_frequency(freq);
- }
-}
diff --git a/quantum/audio/song_list.h b/quantum/audio/song_list.h
index b54b397e1c..8e80a016aa 100644
--- a/quantum/audio/song_list.h
+++ b/quantum/audio/song_list.h
@@ -20,11 +20,9 @@
#include "musical_notes.h"
-#if __GNUC__ > 5 // don't use for older gcc compilers since check isn't supported.
-# if __has_include("user_song_list.h")
-# include "user_song_list.h"
-# endif // if file exists
-#endif // __GNUC__
+#if __has_include("user_song_list.h")
+# include "user_song_list.h"
+#endif // if file exists
#define NO_SOUND
diff --git a/quantum/backlight/backlight_chibios.c b/quantum/backlight/backlight_chibios.c
index 4d5a69e14e..7c6edd10d6 100644
--- a/quantum/backlight/backlight_chibios.c
+++ b/quantum/backlight/backlight_chibios.c
@@ -8,9 +8,13 @@
# define BACKLIGHT_LIMIT_VAL 255
#endif
-// GPIOV2 && GPIOV3
#ifndef BACKLIGHT_PAL_MODE
-# define BACKLIGHT_PAL_MODE 2
+# if defined(USE_GPIOV1)
+# define BACKLIGHT_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
+# else
+// GPIOV2 && GPIOV3
+# define BACKLIGHT_PAL_MODE 5
+# endif
#endif
// GENERIC
@@ -70,7 +74,7 @@ static uint32_t rescale_limit_val(uint32_t val) {
void backlight_init_ports(void) {
#ifdef USE_GPIOV1
- palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
+ palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), BACKLIGHT_PAL_MODE);
#else
palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), PAL_MODE_ALTERNATE(BACKLIGHT_PAL_MODE));
#endif
diff --git a/quantum/eeconfig.c b/quantum/eeconfig.c
index 92f0ac4439..4c2ad2490c 100644
--- a/quantum/eeconfig.c
+++ b/quantum/eeconfig.c
@@ -4,11 +4,6 @@
#include "eeconfig.h"
#include "action_layer.h"
-#ifdef STM32_EEPROM_ENABLE
-# include <hal.h>
-# include "eeprom_stm32.h"
-#endif
-
#if defined(EEPROM_DRIVER)
# include "eeprom_driver.h"
#endif
@@ -43,9 +38,6 @@ __attribute__((weak)) void eeconfig_init_kb(void) {
* FIXME: needs doc
*/
void eeconfig_init_quantum(void) {
-#ifdef STM32_EEPROM_ENABLE
- EEPROM_Erase();
-#endif
#if defined(EEPROM_DRIVER)
eeprom_driver_erase();
#endif
@@ -111,9 +103,6 @@ void eeconfig_enable(void) { eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_N
* FIXME: needs doc
*/
void eeconfig_disable(void) {
-#ifdef STM32_EEPROM_ENABLE
- EEPROM_Erase();
-#endif
#if defined(EEPROM_DRIVER)
eeprom_driver_erase();
#endif
diff --git a/quantum/keyboard.c b/quantum/keyboard.c
index b98fc64e45..6054faa03b 100644
--- a/quantum/keyboard.c
+++ b/quantum/keyboard.c
@@ -76,6 +76,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifdef JOYSTICK_ENABLE
# include "process_joystick.h"
#endif
+#ifdef PROGRAMMABLE_BUTTON_ENABLE
+# include "programmable_button.h"
+#endif
#ifdef HD44780_ENABLE
# include "hd44780.h"
#endif
@@ -97,9 +100,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifdef DIP_SWITCH_ENABLE
# include "dip_switch.h"
#endif
-#ifdef STM32_EEPROM_ENABLE
-# include "eeprom_stm32.h"
-#endif
#ifdef EEPROM_DRIVER
# include "eeprom_driver.h"
#endif
@@ -246,9 +246,6 @@ void keyboard_setup(void) {
disable_jtag();
#endif
print_set_sendchar(sendchar);
-#ifdef STM32_EEPROM_ENABLE
- EEPROM_Init();
-#endif
#ifdef EEPROM_DRIVER
eeprom_driver_init();
#endif
@@ -548,6 +545,10 @@ MATRIX_LOOP_END:
digitizer_task();
#endif
+#ifdef PROGRAMMABLE_BUTTON_ENABLE
+ programmable_button_send();
+#endif
+
// update LED
if (led_status != host_keyboard_leds()) {
led_status = host_keyboard_leds();
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 33586c431b..4fbcc2419c 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -288,10 +288,8 @@ void matrix_init(void) {
matrix_init_pins();
// initialize matrix state: all keys off
- for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
- raw_matrix[i] = 0;
- matrix[i] = 0;
- }
+ memset(matrix, 0, sizeof(matrix));
+ memset(raw_matrix, 0, sizeof(raw_matrix));
debounce_init(ROWS_PER_HAND);
@@ -312,24 +310,22 @@ __attribute__((weak)) bool transport_master_if_connected(matrix_row_t master_mat
bool matrix_post_scan(void) {
bool changed = false;
if (is_keyboard_master()) {
+ static bool last_connected = false;
matrix_row_t slave_matrix[ROWS_PER_HAND] = {0};
if (transport_master_if_connected(matrix + thisHand, slave_matrix)) {
- for (int i = 0; i < ROWS_PER_HAND; ++i) {
- if (matrix[thatHand + i] != slave_matrix[i]) {
- matrix[thatHand + i] = slave_matrix[i];
- changed = true;
- }
- }
- } else {
- // reset other half if disconnected
- for (int i = 0; i < ROWS_PER_HAND; ++i) {
- matrix[thatHand + i] = 0;
- slave_matrix[i] = 0;
- }
+ changed = memcmp(matrix + thatHand, slave_matrix, sizeof(slave_matrix)) != 0;
+ last_connected = true;
+ } else if (last_connected) {
+ // reset other half when disconnected
+ memset(slave_matrix, 0, sizeof(slave_matrix));
changed = true;
+
+ last_connected = false;
}
+ if (changed) memcpy(matrix + thatHand, slave_matrix, sizeof(slave_matrix));
+
matrix_scan_quantum();
} else {
transport_slave(matrix + thatHand, matrix + thisHand);
diff --git a/quantum/mcu_selection.mk b/quantum/mcu_selection.mk
index f7eaeec8ac..92e3a7c923 100644
--- a/quantum/mcu_selection.mk
+++ b/quantum/mcu_selection.mk
@@ -81,7 +81,7 @@ ifneq ($(findstring MK20DX256, $(MCU)),)
BOARD ?= PJRC_TEENSY_3_1
endif
-ifneq ($(findstring MK66F18, $(MCU)),)
+ifneq ($(findstring MK66FX1M0, $(MCU)),)
# Cortex version
MCU = cortex-m4
@@ -273,6 +273,38 @@ ifneq ($(findstring STM32F401, $(MCU)),)
UF2_FAMILY ?= STM32F4
endif
+ifneq ($(findstring STM32F405, $(MCU)),)
+ # Cortex version
+ MCU = cortex-m4
+
+ # ARM version, CORTEX-M0/M1 are 6, CORTEX-M3/M4/M7 are 7
+ ARMV = 7
+
+ ## chip/board settings
+ # - the next two should match the directories in
+ # <chibios>/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)
+ MCU_FAMILY = STM32
+ MCU_SERIES = STM32F4xx
+
+ # Linker script to use
+ # - it should exist either in <chibios>/os/common/ports/ARMCMx/compilers/GCC/ld/
+ # or <keyboard_dir>/ld/
+ MCU_LDSCRIPT ?= STM32F405xG
+
+ # Startup code to use
+ # - it should exist in <chibios>/os/common/startup/ARMCMx/compilers/GCC/mk/
+ MCU_STARTUP ?= stm32f4xx
+
+ # Board: it should exist either in <chibios>/os/hal/boards/,
+ # <keyboard_dir>/boards/, or drivers/boards/
+ BOARD ?= GENERIC_STM32_F405XG
+
+ USE_FPU ?= yes
+
+ # UF2 settings
+ UF2_FAMILY ?= STM32F4
+endif
+
ifneq ($(findstring STM32F407, $(MCU)),)
# Cortex version
MCU = cortex-m4
diff --git a/quantum/process_keycode/process_haptic.c b/quantum/process_keycode/process_haptic.c
index 64d455d009..1b9c2f24fa 100644
--- a/quantum/process_keycode/process_haptic.c
+++ b/quantum/process_keycode/process_haptic.c
@@ -32,6 +32,7 @@ __attribute__((weak)) bool get_haptic_enabled_key(uint16_t keycode, keyrecord_t
break;
case KC_LCTRL ... KC_RGUI:
case QK_MOMENTARY ... QK_MOMENTARY_MAX:
+ case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
#endif
#ifdef NO_HAPTIC_FN
case KC_FN0 ... KC_FN31:
diff --git a/quantum/process_keycode/process_programmable_button.c b/quantum/process_keycode/process_programmable_button.c
new file mode 100644
index 0000000000..c6e77faacc
--- /dev/null
+++ b/quantum/process_keycode/process_programmable_button.c
@@ -0,0 +1,31 @@
+/*
+Copyright 2021 Thomas Weißschuh <thomas@t-8ch.de>
+
+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 "process_programmable_button.h"
+#include "programmable_button.h"
+
+bool process_programmable_button(uint16_t keycode, keyrecord_t *record) {
+ if (keycode >= PROGRAMMABLE_BUTTON_MIN && keycode <= PROGRAMMABLE_BUTTON_MAX) {
+ uint8_t button = keycode - PROGRAMMABLE_BUTTON_MIN + 1;
+ if (record->event.pressed) {
+ programmable_button_on(button);
+ } else {
+ programmable_button_off(button);
+ }
+ }
+ return true;
+}
diff --git a/quantum/process_keycode/process_programmable_button.h b/quantum/process_keycode/process_programmable_button.h
new file mode 100644
index 0000000000..47c6ce5614
--- /dev/null
+++ b/quantum/process_keycode/process_programmable_button.h
@@ -0,0 +1,23 @@
+/*
+Copyright 2021 Thomas Weißschuh <thomas@t-8ch.de>
+
+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
+
+#include <stdint.h>
+#include "quantum.h"
+
+bool process_programmable_button(uint16_t keycode, keyrecord_t *record);
diff --git a/quantum/process_keycode/process_unicode_common.c b/quantum/process_keycode/process_unicode_common.c
index 46fcaaa86b..7853c22c5d 100644
--- a/quantum/process_keycode/process_unicode_common.c
+++ b/quantum/process_keycode/process_unicode_common.c
@@ -22,6 +22,7 @@
unicode_config_t unicode_config;
uint8_t unicode_saved_mods;
bool unicode_saved_caps_lock;
+bool unicode_saved_num_lock;
#if UNICODE_SELECTED_MODES != -1
static uint8_t selected[] = {UNICODE_SELECTED_MODES};
@@ -79,13 +80,14 @@ void persist_unicode_input_mode(void) { eeprom_update_byte(EECONFIG_UNICODEMODE,
__attribute__((weak)) void unicode_input_start(void) {
unicode_saved_caps_lock = host_keyboard_led_state().caps_lock;
+ unicode_saved_num_lock = host_keyboard_led_state().num_lock;
// Note the order matters here!
// Need to do this before we mess around with the mods, or else
// UNICODE_KEY_LNX (which is usually Ctrl-Shift-U) might not work
// correctly in the shifted case.
if (unicode_config.input_mode == UC_LNX && unicode_saved_caps_lock) {
- tap_code(KC_CAPS);
+ tap_code(KC_CAPSLOCK);
}
unicode_saved_mods = get_mods(); // Save current mods
@@ -99,8 +101,12 @@ __attribute__((weak)) void unicode_input_start(void) {
tap_code16(UNICODE_KEY_LNX);
break;
case UC_WIN:
+ // For increased reliability, use numpad keys for inputting digits
+ if (!unicode_saved_num_lock) {
+ tap_code(KC_NUMLOCK);
+ }
register_code(KC_LALT);
- tap_code(KC_PPLS);
+ tap_code(KC_KP_PLUS);
break;
case UC_WINC:
tap_code(UNICODE_KEY_WINC);
@@ -117,13 +123,16 @@ __attribute__((weak)) void unicode_input_finish(void) {
unregister_code(UNICODE_KEY_MAC);
break;
case UC_LNX:
- tap_code(KC_SPC);
+ tap_code(KC_SPACE);
if (unicode_saved_caps_lock) {
- tap_code(KC_CAPS);
+ tap_code(KC_CAPSLOCK);
}
break;
case UC_WIN:
unregister_code(KC_LALT);
+ if (!unicode_saved_num_lock) {
+ tap_code(KC_NUMLOCK);
+ }
break;
case UC_WINC:
tap_code(KC_ENTER);
@@ -139,26 +148,44 @@ __attribute__((weak)) void unicode_input_cancel(void) {
unregister_code(UNICODE_KEY_MAC);
break;
case UC_LNX:
- tap_code(KC_ESC);
+ tap_code(KC_ESCAPE);
if (unicode_saved_caps_lock) {
- tap_code(KC_CAPS);
+ tap_code(KC_CAPSLOCK);
}
break;
case UC_WINC:
- tap_code(KC_ESC);
+ tap_code(KC_ESCAPE);
break;
case UC_WIN:
unregister_code(KC_LALT);
+ if (!unicode_saved_num_lock) {
+ tap_code(KC_NUMLOCK);
+ }
break;
}
set_mods(unicode_saved_mods); // Reregister previously set mods
}
+// clang-format off
+
+static void send_nibble_wrapper(uint8_t digit) {
+ if (unicode_config.input_mode == UC_WIN) {
+ uint8_t kc = digit < 10
+ ? KC_KP_1 + (10 + digit - 1) % 10
+ : KC_A + (digit - 10);
+ tap_code(kc);
+ return;
+ }
+ send_nibble(digit);
+}
+
+// clang-format on
+
void register_hex(uint16_t hex) {
for (int i = 3; i >= 0; i--) {
uint8_t digit = ((hex >> (i * 4)) & 0xF);
- send_nibble(digit);
+ send_nibble_wrapper(digit);
}
}
@@ -171,10 +198,10 @@ void register_hex32(uint32_t hex) {
uint8_t digit = ((hex >> (i * 4)) & 0xF);
if (digit == 0) {
if (!onzerostart) {
- send_nibble(digit);
+ send_nibble_wrapper(digit);
}
} else {
- send_nibble(digit);
+ send_nibble_wrapper(digit);
onzerostart = false;
}
}
diff --git a/quantum/programmable_button.c b/quantum/programmable_button.c
new file mode 100644
index 0000000000..be828fd17c
--- /dev/null
+++ b/quantum/programmable_button.c
@@ -0,0 +1,37 @@
+/*
+Copyright 2021 Thomas Weißschuh <thomas@t-8ch.de>
+
+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 "programmable_button.h"
+#include "host.h"
+
+#define REPORT_BIT(index) (((uint32_t)1) << (index - 1))
+
+static uint32_t programmable_button_report = 0;
+
+void programmable_button_clear(void) { programmable_button_report = 0; }
+
+void programmable_button_send(void) { host_programmable_button_send(programmable_button_report); }
+
+void programmable_button_on(uint8_t index) { programmable_button_report |= REPORT_BIT(index); }
+
+void programmable_button_off(uint8_t index) { programmable_button_report &= ~REPORT_BIT(index); }
+
+bool programmable_button_is_on(uint8_t index) { return !!(programmable_button_report & REPORT_BIT(index)); };
+
+uint32_t programmable_button_get_report(void) { return programmable_button_report; };
+
+void programmable_button_set_report(uint32_t report) { programmable_button_report = report; }
diff --git a/quantum/programmable_button.h b/quantum/programmable_button.h
new file mode 100644
index 0000000000..e89b8b9fd6
--- /dev/null
+++ b/quantum/programmable_button.h
@@ -0,0 +1,30 @@
+/*
+Copyright 2021 Thomas Weißschuh <thomas@t-8ch.de>
+
+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
+
+#include <stdint.h>
+#include <stdbool.h>
+#include "report.h"
+
+void programmable_button_clear(void);
+void programmable_button_send(void);
+void programmable_button_on(uint8_t index);
+void programmable_button_off(uint8_t index);
+bool programmable_button_is_on(uint8_t index);
+uint32_t programmable_button_get_report(void);
+void programmable_button_set_report(uint32_t report);
diff --git a/quantum/quantum.c b/quantum/quantum.c
index e60378afe4..326c8370b1 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -296,6 +296,9 @@ bool process_record_quantum(keyrecord_t *record) {
#ifdef JOYSTICK_ENABLE
process_joystick(keycode, record) &&
#endif
+#ifdef PROGRAMMABLE_BUTTON_ENABLE
+ process_programmable_button(keycode, record) &&
+#endif
true)) {
return false;
}
@@ -480,3 +483,99 @@ void api_send_unicode(uint32_t unicode) {
__attribute__((weak)) void startup_user() {}
__attribute__((weak)) void shutdown_user() {}
+
+/** \brief Run keyboard level Power down
+ *
+ * FIXME: needs doc
+ */
+__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(); }
+
+void suspend_power_down_quantum(void) {
+#ifndef NO_SUSPEND_POWER_DOWN
+// Turn off backlight
+# ifdef BACKLIGHT_ENABLE
+ backlight_set(0);
+# endif
+
+# ifdef LED_MATRIX_ENABLE
+ led_matrix_task();
+# endif
+# ifdef RGB_MATRIX_ENABLE
+ rgb_matrix_task();
+# 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
+
+# if defined(LED_MATRIX_ENABLE)
+ led_matrix_set_suspend_state(true);
+# endif
+# if defined(RGB_MATRIX_ENABLE)
+ rgb_matrix_set_suspend_state(true);
+# endif
+
+# ifdef OLED_ENABLE
+ oled_off();
+# endif
+# ifdef ST7565_ENABLE
+ st7565_off();
+# endif
+#endif
+}
+
+/** \brief run user level code immediately after wakeup
+ *
+ * FIXME: needs doc
+ */
+__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_quantum(void) {
+// 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
+
+#if defined(LED_MATRIX_ENABLE)
+ led_matrix_set_suspend_state(false);
+#endif
+#if defined(RGB_MATRIX_ENABLE)
+ rgb_matrix_set_suspend_state(false);
+#endif
+ suspend_wakeup_init_kb();
+}
diff --git a/quantum/quantum.h b/quantum/quantum.h
index 86b717e445..5cbe84d0c9 100644
--- a/quantum/quantum.h
+++ b/quantum/quantum.h
@@ -147,6 +147,10 @@ extern layer_state_t layer_state;
# include "process_joystick.h"
#endif
+#ifdef PROGRAMMABLE_BUTTON_ENABLE
+# include "process_programmable_button.h"
+#endif
+
#ifdef GRAVE_ESC_ENABLE
# include "process_grave_esc.h"
#endif
diff --git a/quantum/quantum_keycodes.h b/quantum/quantum_keycodes.h
index ef4b0f457b..2ea81dd4c8 100644
--- a/quantum/quantum_keycodes.h
+++ b/quantum/quantum_keycodes.h
@@ -524,6 +524,40 @@ enum quantum_keycodes {
// Additional magic key
MAGIC_TOGGLE_GUI,
+ // Programmable Button
+ PROGRAMMABLE_BUTTON_1,
+ PROGRAMMABLE_BUTTON_2,
+ PROGRAMMABLE_BUTTON_3,
+ PROGRAMMABLE_BUTTON_4,
+ PROGRAMMABLE_BUTTON_5,
+ PROGRAMMABLE_BUTTON_6,
+ PROGRAMMABLE_BUTTON_7,
+ PROGRAMMABLE_BUTTON_8,
+ PROGRAMMABLE_BUTTON_9,
+ PROGRAMMABLE_BUTTON_10,
+ PROGRAMMABLE_BUTTON_11,
+ PROGRAMMABLE_BUTTON_12,
+ PROGRAMMABLE_BUTTON_13,
+ PROGRAMMABLE_BUTTON_14,
+ PROGRAMMABLE_BUTTON_15,
+ PROGRAMMABLE_BUTTON_16,
+ PROGRAMMABLE_BUTTON_17,
+ PROGRAMMABLE_BUTTON_18,
+ PROGRAMMABLE_BUTTON_19,
+ PROGRAMMABLE_BUTTON_20,
+ PROGRAMMABLE_BUTTON_21,
+ PROGRAMMABLE_BUTTON_22,
+ PROGRAMMABLE_BUTTON_23,
+ PROGRAMMABLE_BUTTON_24,
+ PROGRAMMABLE_BUTTON_25,
+ PROGRAMMABLE_BUTTON_26,
+ PROGRAMMABLE_BUTTON_27,
+ PROGRAMMABLE_BUTTON_28,
+ PROGRAMMABLE_BUTTON_29,
+ PROGRAMMABLE_BUTTON_30,
+ PROGRAMMABLE_BUTTON_31,
+ PROGRAMMABLE_BUTTON_32,
+
// Start of custom keycode range for keyboards and keymaps - always leave at the end
SAFE_RANGE
};
@@ -854,3 +888,39 @@ enum quantum_keycodes {
#define OS_TOGG ONESHOT_TOGGLE
#define OS_ON ONESHOT_ENABLE
#define OS_OFF ONESHOT_DISABLE
+
+// Programmable Button aliases
+#define PB_1 PROGRAMMABLE_BUTTON_1
+#define PB_2 PROGRAMMABLE_BUTTON_2
+#define PB_3 PROGRAMMABLE_BUTTON_3
+#define PB_4 PROGRAMMABLE_BUTTON_4
+#define PB_5 PROGRAMMABLE_BUTTON_5
+#define PB_6 PROGRAMMABLE_BUTTON_6
+#define PB_7 PROGRAMMABLE_BUTTON_7
+#define PB_8 PROGRAMMABLE_BUTTON_8
+#define PB_9 PROGRAMMABLE_BUTTON_9
+#define PB_10 PROGRAMMABLE_BUTTON_10
+#define PB_11 PROGRAMMABLE_BUTTON_11
+#define PB_12 PROGRAMMABLE_BUTTON_12
+#define PB_13 PROGRAMMABLE_BUTTON_13
+#define PB_14 PROGRAMMABLE_BUTTON_14
+#define PB_15 PROGRAMMABLE_BUTTON_15
+#define PB_16 PROGRAMMABLE_BUTTON_16
+#define PB_17 PROGRAMMABLE_BUTTON_17
+#define PB_18 PROGRAMMABLE_BUTTON_18
+#define PB_19 PROGRAMMABLE_BUTTON_19
+#define PB_20 PROGRAMMABLE_BUTTON_20
+#define PB_21 PROGRAMMABLE_BUTTON_21
+#define PB_22 PROGRAMMABLE_BUTTON_22
+#define PB_23 PROGRAMMABLE_BUTTON_23
+#define PB_24 PROGRAMMABLE_BUTTON_24
+#define PB_25 PROGRAMMABLE_BUTTON_25
+#define PB_26 PROGRAMMABLE_BUTTON_26
+#define PB_27 PROGRAMMABLE_BUTTON_27
+#define PB_28 PROGRAMMABLE_BUTTON_28
+#define PB_29 PROGRAMMABLE_BUTTON_29
+#define PB_30 PROGRAMMABLE_BUTTON_30
+#define PB_31 PROGRAMMABLE_BUTTON_31
+#define PB_32 PROGRAMMABLE_BUTTON_32
+#define PROGRAMMABLE_BUTTON_MIN PROGRAMMABLE_BUTTON_1
+#define PROGRAMMABLE_BUTTON_MAX PROGRAMMABLE_BUTTON_32
diff --git a/quantum/usb_device_state.c b/quantum/usb_device_state.c
new file mode 100644
index 0000000000..5ccd309ec2
--- /dev/null
+++ b/quantum/usb_device_state.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2021 Andrei Purdea <andrei@purdea.ro>
+ *
+ * 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 "usb_device_state.h"
+
+enum usb_device_state usb_device_state = USB_DEVICE_STATE_NO_INIT;
+
+__attribute__((weak)) void notify_usb_device_state_change_kb(enum usb_device_state usb_device_state) { notify_usb_device_state_change_user(usb_device_state); }
+
+__attribute__((weak)) void notify_usb_device_state_change_user(enum usb_device_state usb_device_state) {}
+
+static void notify_usb_device_state_change(enum usb_device_state usb_device_state) { notify_usb_device_state_change_kb(usb_device_state); }
+
+void usb_device_state_set_configuration(bool isConfigured, uint8_t configurationNumber) {
+ usb_device_state = isConfigured ? USB_DEVICE_STATE_CONFIGURED : USB_DEVICE_STATE_INIT;
+ notify_usb_device_state_change(usb_device_state);
+}
+
+void usb_device_state_set_suspend(bool isConfigured, uint8_t configurationNumber) {
+ usb_device_state = USB_DEVICE_STATE_SUSPEND;
+ notify_usb_device_state_change(usb_device_state);
+}
+
+void usb_device_state_set_resume(bool isConfigured, uint8_t configurationNumber) {
+ usb_device_state = isConfigured ? USB_DEVICE_STATE_CONFIGURED : USB_DEVICE_STATE_INIT;
+ notify_usb_device_state_change(usb_device_state);
+}
+
+void usb_device_state_set_reset(void) {
+ usb_device_state = USB_DEVICE_STATE_INIT;
+ notify_usb_device_state_change(usb_device_state);
+}
+
+void usb_device_state_init(void) {
+ usb_device_state = USB_DEVICE_STATE_INIT;
+ notify_usb_device_state_change(usb_device_state);
+}
diff --git a/quantum/usb_device_state.h b/quantum/usb_device_state.h
new file mode 100644
index 0000000000..c229311d46
--- /dev/null
+++ b/quantum/usb_device_state.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2021 Andrei Purdea <andrei@purdea.ro>
+ *
+ * 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
+
+#include <stdbool.h>
+#include <stdint.h>
+
+void usb_device_state_set_configuration(bool isConfigured, uint8_t configurationNumber);
+void usb_device_state_set_suspend(bool isConfigured, uint8_t configurationNumber);
+void usb_device_state_set_resume(bool isConfigured, uint8_t configurationNumber);
+void usb_device_state_set_reset(void);
+void usb_device_state_init(void);
+
+enum usb_device_state {
+ USB_DEVICE_STATE_NO_INIT = 0, // We're in this state before calling usb_device_state_init()
+ USB_DEVICE_STATE_INIT = 1, // Can consume up to 100mA
+ USB_DEVICE_STATE_CONFIGURED = 2, // Can consume up to what is specified in configuration descriptor, typically 500mA
+ USB_DEVICE_STATE_SUSPEND = 3 // Can consume only suspend current
+};
+
+extern enum usb_device_state usb_device_state;
+
+void notify_usb_device_state_change_kb(enum usb_device_state usb_device_state);
+void notify_usb_device_state_change_user(enum usb_device_state usb_device_state);