add audio for atmega32a

planck_thk
Jack Humbert 6 years ago
parent 0236f6a8ac
commit 7443da28ae

@ -1,5 +1,5 @@
/*
Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
Copyright 2018 Jack Humbert <jack.humb@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -15,46 +15,18 @@ 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 "config_common.h"
#ifndef CONFIG_H
#define CONFIG_H
#define VENDOR_ID 0x6060
#define PRODUCT_ID 0x1001
// TODO: share these strings with usbconfig.h
// Edit usbconfig.h to change these.
#define MANUFACTURER OLKB
#define PRODUCT Planck
/* matrix size */
#define MATRIX_ROWS 4
#define MATRIX_COLS 12
#pragma once
#define MATRIX_ROW_PINS { A7, A6, A5, A4 }
#define MATRIX_COL_PINS { D7, C2, C3, C4, C5, C6, C7, A3, A2, A1, A0, B0 }
#include "config_common.h"
/* COL2ROW or ROW2COL */
#define DIODE_DIRECTION COL2ROW
// #define MATRIX_ROW_PINS { A7, A6, A5, A4 }
// #define MATRIX_COL_PINS { D7, C2, C3, C4, C5, C6, C7, A3, A2, A1, A0, B0 }
#define BACKLIGHT_LEVELS 12
// #define BACKLIGHT_BREATHING // works, but BL_TOGG might not work
// #define DIODE_DIRECTION COL2ROW
#define TAPPING_TOGGLE 3
#define NO_UART 1
/* RGB underglow */
// The RGB_DI_PIN value seems to be shared between all PS2AVRGB boards.
// The same pin is used on the JJ40, at least.
#define RGBLED_NUM 5
#define RGB_DI_PIN E2 // NOTE: for PS2AVRGB boards, underglow commands are sent via I2C to 0xB0.
#define RGBLIGHT_ANIMATIONS
/* key combination for command */
#define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)))
#undef AUDIO_VOICES
#undef C6_AUDIO
#endif
#define D5_AUDIO

@ -38,7 +38,7 @@ CONSOLE_ENABLE = no
COMMAND_ENABLE = yes
KEY_LOCK_ENABLE = yes
NKRO_ENABLE = no # Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
AUDIO_ENABLE = no
AUDIO_ENABLE = yes
# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend

@ -20,7 +20,7 @@ extern bool dip_switch[4];
void matrix_init_kb(void) {
DDRD |= (1<<5);
//DDRD |= (1<<5);
//PORTD |= (1<<5);
matrix_init_user();
}

@ -36,10 +36,10 @@
// -----------------------------------------------------------------------------
//Currently we support timers 1 and 3 used at the sime time, channels A-C,
//pins PB5, PB6, PB7, PC4, PC5, and PC6
//pins PB5, PB6, PB7, PC4, PC5, PC6, PD5, PD4
#if defined(C6_AUDIO)
#define CPIN_AUDIO
#define CPIN_SET_DIRECTION DDRC |= _BV(PORTC6);
#define TIMER_3_AUDIO
#define TIMER_3_SET_DIRECTION DDRC |= _BV(PORTC6);
#define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
#define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
#define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
@ -50,8 +50,8 @@
#define TIMER3_AUDIO_vect TIMER3_COMPA_vect
#endif
#if defined(C5_AUDIO)
#define CPIN_AUDIO
#define CPIN_SET_DIRECTION DDRC |= _BV(PORTC5);
#define TIMER_3_AUDIO
#define TIMER_3_SET_DIRECTION DDRC |= _BV(PORTC5);
#define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3B1) | (0 << COM3B0) | (1 << WGM31) | (0 << WGM30);
#define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3B)
#define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3B)
@ -62,8 +62,8 @@
#define TIMER3_AUDIO_vect TIMER3_COMPB_vect
#endif
#if defined(C4_AUDIO)
#define CPIN_AUDIO
#define CPIN_SET_DIRECTION DDRC |= _BV(PORTC4);
#define TIMER_3_AUDIO
#define TIMER_3_SET_DIRECTION DDRC |= _BV(PORTC4);
#define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3C1) | (0 << COM3C0) | (1 << WGM31) | (0 << WGM30);
#define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3C)
#define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3C)
@ -75,8 +75,8 @@
#endif
#if defined(B5_AUDIO)
#define BPIN_AUDIO
#define BPIN_SET_DIRECTION DDRB |= _BV(PORTB5);
#define TIMER_1_AUDIO
#define TIMER_1_SET_DIRECTION DDRB |= _BV(PORTB5);
#define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
#define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A)
#define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A)
@ -87,8 +87,8 @@
#define TIMER1_AUDIO_vect TIMER1_COMPA_vect
#endif
#if defined(B6_AUDIO)
#define BPIN_AUDIO
#define BPIN_SET_DIRECTION DDRB |= _BV(PORTB6);
#define TIMER_1_AUDIO
#define TIMER_1_SET_DIRECTION DDRB |= _BV(PORTB6);
#define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1B1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10);
#define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1B)
#define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1B)
@ -99,8 +99,8 @@
#define TIMER1_AUDIO_vect TIMER1_COMPB_vect
#endif
#if defined(B7_AUDIO)
#define BPIN_AUDIO
#define BPIN_SET_DIRECTION DDRB |= _BV(PORTB7);
#define TIMER_1_AUDIO
#define TIMER_1_SET_DIRECTION DDRB |= _BV(PORTB7);
#define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1C1) | (0 << COM1C0) | (1 << WGM11) | (0 << WGM10);
#define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1C)
#define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1C)
@ -110,6 +110,30 @@
#define TIMER_1_DUTY_CYCLE OCR1C
#define TIMER1_AUDIO_vect TIMER1_COMPC_vect
#endif
#if defined(D5_AUDIO)
#define TIMER_1_AUDIO
#define TIMER_1_SET_DIRECTION DDRD |= _BV(PORTD5);
#define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
#define ENABLE_AUDIO_COUNTER_1_ISR TIMSK |= _BV(OCIE1A)
#define DISABLE_AUDIO_COUNTER_1_ISR TIMSK &= ~_BV(OCIE1A)
#define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1);
#define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
#define TIMER_1_PERIOD ICR1
#define TIMER_1_DUTY_CYCLE OCR1A
#define TIMER1_AUDIO_vect TIMER1_COMPA_vect
#endif
#if defined(D4_AUDIO)
#define TIMER_1_AUDIO
#define TIMER_1_SET_DIRECTION DDRD |= _BV(PORTD4);
#define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1A1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10);
#define ENABLE_AUDIO_COUNTER_1_ISR TIMSK |= _BV(OCIE1B)
#define DISABLE_AUDIO_COUNTER_1_ISR TIMSK &= ~_BV(OCIE1B)
#define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1B1);
#define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
#define TIMER_1_PERIOD ICR1
#define TIMER_1_DUTY_CYCLE OCR1B
#define TIMER1_AUDIO_vect TIMER1_COMPB_vect
#endif
// -----------------------------------------------------------------------------
@ -185,12 +209,12 @@ void audio_init()
if (!audio_initialized) {
// Set audio ports as output
#ifdef CPIN_AUDIO
CPIN_SET_DIRECTION
#ifdef TIMER_3_AUDIO
TIMER_3_SET_DIRECTION
DISABLE_AUDIO_COUNTER_3_ISR;
#endif
#ifdef BPIN_AUDIO
BPIN_SET_DIRECTION
#ifdef TIMER_1_AUDIO
TIMER_1_SET_DIRECTION
DISABLE_AUDIO_COUNTER_1_ISR;
#endif
@ -212,18 +236,18 @@ void audio_init()
// OCR1C - PB7
// Clock Select (CS3n) = 0b010 = Clock / 8
#ifdef CPIN_AUDIO
#ifdef TIMER_3_AUDIO
INIT_AUDIO_COUNTER_3
TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
#endif
#ifdef BPIN_AUDIO
#ifdef TIMER_1_AUDIO
INIT_AUDIO_COUNTER_1
TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
#endif
#endif
audio_initialized = true;
}
@ -231,7 +255,7 @@ void audio_init()
if (audio_config.enable) {
PLAY_SONG(startup_song);
}
}
void stop_all_notes()
@ -243,12 +267,12 @@ void stop_all_notes()
}
voices = 0;
#ifdef CPIN_AUDIO
#ifdef TIMER_3_AUDIO
DISABLE_AUDIO_COUNTER_3_ISR;
DISABLE_AUDIO_COUNTER_3_OUTPUT;
#endif
#ifdef BPIN_AUDIO
#ifdef TIMER_1_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR;
DISABLE_AUDIO_COUNTER_1_OUTPUT;
#endif
@ -294,11 +318,11 @@ void stop_note(float freq)
voice_place = 0;
}
if (voices == 0) {
#ifdef CPIN_AUDIO
#ifdef TIMER_3_AUDIO
DISABLE_AUDIO_COUNTER_3_ISR;
DISABLE_AUDIO_COUNTER_3_OUTPUT;
#endif
#ifdef BPIN_AUDIO
#ifdef TIMER_1_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR;
DISABLE_AUDIO_COUNTER_1_OUTPUT;
#endif
@ -330,7 +354,7 @@ float vibrato(float average_freq) {
#endif
#ifdef CPIN_AUDIO
#ifdef TIMER_3_AUDIO
ISR(TIMER3_AUDIO_vect)
{
float freq;
@ -338,7 +362,7 @@ ISR(TIMER3_AUDIO_vect)
if (playing_note) {
if (voices > 0) {
#ifdef BPIN_AUDIO
#ifdef TIMER_1_AUDIO
float freq_alt = 0;
if (voices > 1) {
if (polyphony_rate == 0) {
@ -464,7 +488,7 @@ ISR(TIMER3_AUDIO_vect)
note_position++;
bool end_of_note = false;
if (TIMER_3_PERIOD > 0) {
if (!note_resting)
if (!note_resting)
end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF - 1));
else
end_of_note = (note_position >= (note_length));
@ -512,10 +536,10 @@ ISR(TIMER3_AUDIO_vect)
}
#endif
#ifdef BPIN_AUDIO
#ifdef TIMER_1_AUDIO
ISR(TIMER1_AUDIO_vect)
{
#if defined(BPIN_AUDIO) && !defined(CPIN_AUDIO)
#if defined(TIMER_1_AUDIO) && !defined(TIMER_3_AUDIO)
float freq = 0;
if (playing_note) {
@ -604,7 +628,7 @@ ISR(TIMER1_AUDIO_vect)
note_position++;
bool end_of_note = false;
if (TIMER_1_PERIOD > 0) {
if (!note_resting)
if (!note_resting)
end_of_note = (note_position >= (note_length / TIMER_1_PERIOD * 0xFFFF - 1));
else
end_of_note = (note_position >= (note_length));
@ -662,10 +686,10 @@ void play_note(float freq, int vol) {
}
if (audio_config.enable && voices < 8) {
#ifdef CPIN_AUDIO
#ifdef TIMER_3_AUDIO
DISABLE_AUDIO_COUNTER_3_ISR;
#endif
#ifdef BPIN_AUDIO
#ifdef TIMER_1_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR;
#endif
@ -683,12 +707,12 @@ void play_note(float freq, int vol) {
voices++;
}
#ifdef CPIN_AUDIO
#ifdef TIMER_3_AUDIO
ENABLE_AUDIO_COUNTER_3_ISR;
ENABLE_AUDIO_COUNTER_3_OUTPUT;
#endif
#ifdef BPIN_AUDIO
#ifdef CPIN_AUDIO
#ifdef TIMER_1_AUDIO
#ifdef TIMER_3_AUDIO
if (voices > 1) {
ENABLE_AUDIO_COUNTER_1_ISR;
ENABLE_AUDIO_COUNTER_1_OUTPUT;
@ -711,10 +735,10 @@ void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
if (audio_config.enable) {
#ifdef CPIN_AUDIO
#ifdef TIMER_3_AUDIO
DISABLE_AUDIO_COUNTER_3_ISR;
#endif
#ifdef BPIN_AUDIO
#ifdef TIMER_1_AUDIO
DISABLE_AUDIO_COUNTER_1_ISR;
#endif
@ -736,12 +760,12 @@ void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
note_position = 0;
#ifdef CPIN_AUDIO
#ifdef TIMER_3_AUDIO
ENABLE_AUDIO_COUNTER_3_ISR;
ENABLE_AUDIO_COUNTER_3_OUTPUT;
#endif
#ifdef BPIN_AUDIO
#ifndef CPIN_AUDIO
#ifdef TIMER_1_AUDIO
#ifndef TIMER_3_AUDIO
ENABLE_AUDIO_COUNTER_1_ISR;
ENABLE_AUDIO_COUNTER_1_OUTPUT;
#endif

Loading…
Cancel
Save