mirror of
https://github.com/qmk/qmk_firmware.git
synced 2024-11-22 11:29:26 +00:00
updated return-to-zero method to use pwm width of 0
This commit is contained in:
parent
9b8d4803dc
commit
0179f979e5
@ -13,7 +13,6 @@
|
|||||||
|
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "gpio.h"
|
#include "gpio.h"
|
||||||
#include "timer.h"
|
|
||||||
|
|
||||||
#if !defined(AUDIO_PIN)
|
#if !defined(AUDIO_PIN)
|
||||||
# error "Audio feature enabled, but no pin selected - see docs/feature_audio under the ARM PWM settings"
|
# error "Audio feature enabled, but no pin selected - see docs/feature_audio under the ARM PWM settings"
|
||||||
@ -32,20 +31,6 @@
|
|||||||
extern bool playing_note;
|
extern bool playing_note;
|
||||||
extern bool playing_melody;
|
extern bool playing_melody;
|
||||||
extern uint8_t note_timbre;
|
extern uint8_t note_timbre;
|
||||||
static bool channel_1_stopped = true;
|
|
||||||
|
|
||||||
void channel_1_stop(void);
|
|
||||||
void channel_1_start(void);
|
|
||||||
|
|
||||||
void audio_wait_for_pin(pin_t pin, uint8_t target_state) {
|
|
||||||
uint16_t wait_limiter_timer;
|
|
||||||
wait_limiter_timer = timer_read();
|
|
||||||
while (timer_elapsed(wait_limiter_timer) < 2) {
|
|
||||||
if (gpio_read_pin(pin) == target_state) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static PWMConfig pwmCFG = {.frequency = AUDIO_PWM_COUNTER_FREQUENCY, /* PWM clock frequency */
|
static PWMConfig pwmCFG = {.frequency = AUDIO_PWM_COUNTER_FREQUENCY, /* PWM clock frequency */
|
||||||
.period = 2,
|
.period = 2,
|
||||||
@ -56,22 +41,19 @@ static float channel_1_frequency = 0.0f;
|
|||||||
|
|
||||||
void channel_1_set_frequency(float freq) {
|
void channel_1_set_frequency(float freq) {
|
||||||
channel_1_frequency = freq;
|
channel_1_frequency = freq;
|
||||||
|
pwmcnt_t period;
|
||||||
|
pwmcnt_t width;
|
||||||
|
|
||||||
if (freq <= 0.0) {
|
if (freq <= 0.0) {
|
||||||
// a pause/rest has freq=0
|
period = 2;
|
||||||
channel_1_stop();
|
width = 0;
|
||||||
return;
|
} else {
|
||||||
|
period = (pwmCFG.frequency / freq);
|
||||||
|
width = PWM_PERCENTAGE_TO_WIDTH(&AUDIO_PWM_DRIVER, (100 - note_timbre) * 100);
|
||||||
}
|
}
|
||||||
if (channel_1_stopped) {
|
|
||||||
channel_1_start();
|
|
||||||
}
|
|
||||||
|
|
||||||
pwmcnt_t period = (pwmCFG.frequency / freq);
|
|
||||||
chSysLockFromISR();
|
chSysLockFromISR();
|
||||||
pwmChangePeriodI(&AUDIO_PWM_DRIVER, period);
|
pwmChangePeriodI(&AUDIO_PWM_DRIVER, period);
|
||||||
pwmEnableChannelI(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1,
|
pwmEnableChannelI(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1, width);
|
||||||
// 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));
|
|
||||||
chSysUnlockFromISR();
|
chSysUnlockFromISR();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,35 +64,15 @@ float channel_1_get_frequency(void) {
|
|||||||
void channel_1_start(void) {
|
void channel_1_start(void) {
|
||||||
pwmStop(&AUDIO_PWM_DRIVER);
|
pwmStop(&AUDIO_PWM_DRIVER);
|
||||||
pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
|
pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
|
||||||
channel_1_stopped = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void channel_1_stop(void) {
|
void channel_1_stop(void) {
|
||||||
// only certain timers will disable the output (set to zero) when the pwm is stopped.
|
|
||||||
// to ensure the pin is low (without having changing the pin modes), set the
|
|
||||||
// frequency to the highest valid value and wait for the pin to be zero
|
|
||||||
chSysLockFromISR();
|
|
||||||
pwmChangePeriodI(&AUDIO_PWM_DRIVER, 2);
|
|
||||||
pwmEnableChannelI(&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, 5000));
|
|
||||||
chSysUnlockFromISR();
|
|
||||||
|
|
||||||
// try stopping now if the pin is currently low
|
|
||||||
audio_wait_for_pin(AUDIO_PIN,0);
|
|
||||||
pwmStop(&AUDIO_PWM_DRIVER);
|
pwmStop(&AUDIO_PWM_DRIVER);
|
||||||
|
pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
|
||||||
uint16_t wait_limiter_timer;
|
pwmEnableChannelI(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1, 0);
|
||||||
wait_limiter_timer = timer_read();
|
pwmStop(&AUDIO_PWM_DRIVER);
|
||||||
// if it isn't actively low, it may have been stopped a little too late, so keep trying
|
|
||||||
// it should not require very many attempts, so limit to 2ms maximum wait time as a failsafe
|
|
||||||
while ((gpio_read_pin(AUDIO_PIN) == 1) && (timer_elapsed(wait_limiter_timer) < 2)) {
|
|
||||||
pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
|
|
||||||
audio_wait_for_pin(AUDIO_PIN,0);
|
|
||||||
pwmStop(&AUDIO_PWM_DRIVER);
|
|
||||||
}
|
|
||||||
channel_1_stopped = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static virtual_timer_t audio_vt;
|
static virtual_timer_t audio_vt;
|
||||||
static void audio_callback(virtual_timer_t *vtp, void *p);
|
static void audio_callback(virtual_timer_t *vtp, void *p);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user