sound uses timed

This commit is contained in:
Stephen Crane 2018-09-06 10:12:03 +01:00
parent ffd169fc8d
commit 1f7bf6935e
3 changed files with 10 additions and 25 deletions

View File

@ -12,17 +12,15 @@ static dac_channel_t channel;
static volatile const uint8_t *_bytes;
static volatile unsigned _size, _off;
static hw_timer_t *timer;
void IRAM_ATTR timer_callback() {
portENTER_CRITICAL_ISR(&mux);
if (_off < _size)
dac_output_voltage(channel, _bytes[_off++]);
else {
else if (_bytes) {
_bytes = 0;
dac_output_disable(channel);
timerAlarmDisable(timer);
}
portEXIT_CRITICAL_ISR(&mux);
@ -34,9 +32,7 @@ void Sound::begin(unsigned pin, unsigned freq) {
else if (pin == 26)
channel = DAC_CHANNEL_2;
timer = timerBegin(1, 80, true);
timerAttachInterrupt(timer, &timer_callback, true);
timerAlarmWrite(timer, 1000000 / freq, true);
timer_create(freq, &timer_callback);
}
const uint8_t *Sound::play(const uint8_t *bytes, unsigned size) {
@ -48,7 +44,6 @@ const uint8_t *Sound::play(const uint8_t *bytes, unsigned size) {
_size = size;
_off = 0;
dac_output_enable(channel);
timerAlarmEnable(timer);
play = bytes;
}

View File

@ -9,17 +9,16 @@
#include "timed.h"
static Timed *t;
// FIXME: disable timer when tick() returns false
#if defined(__LM4F120H5QR__)
static void (*client_handler)(void);
static void timer0isr(void) {
ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
t->tick();
client_handler();
}
void timer_create(unsigned freq, Timed *client) {
t = client;
void timer_create(unsigned freq, void (*handler)(void)) {
client_handler = handler;
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
TimerIntRegister(TIMER0_BASE, TIMER_A, timer0isr);
@ -30,13 +29,9 @@ void timer_create(unsigned freq, Timed *client) {
}
#elif defined(ESP_PLATFORM)
void IRAM_ATTR onTimer() {
t->tick();
}
void timer_create(unsigned freq, Timed *client) {
void timer_create(unsigned freq, void (*handler)(void)) {
hw_timer_t *timer = timerBegin(3, 80, true); // prescaler of 80
timerAttachInterrupt(timer, &onTimer, true);
timerAttachInterrupt(timer, handler, true);
timerAlarmWrite(timer, 1000000 / freq, true);
timerAlarmEnable(timer);
}

View File

@ -1,11 +1,6 @@
#ifndef __TIMED_H__
#define __TIMED_H__
class Timed {
public:
virtual bool tick() = 0;
};
void timer_create(unsigned freq, Timed *client);
void timer_create(unsigned freq, void (*handler)(void));
#endif