RP2040 PWM stop timer before begin

This commit is contained in:
pschatzmann 2024-07-24 07:15:57 +02:00
parent d573756fd9
commit fc48d7848b
6 changed files with 18 additions and 11 deletions

View File

@ -26,7 +26,10 @@ class PWMAudioOutput : public AudioOutput {
}
}
virtual PWMConfig defaultConfig() { return pwm.defaultConfig(); }
virtual PWMConfig defaultConfig(RxTxMode mode=TX_MODE) {
if (mode!=TX_MODE) LOGE("mode not supported: using TX_MODE");
return pwm.defaultConfig();
}
PWMConfig config() { return audio_config; }

View File

@ -188,7 +188,7 @@ class DriverPWMBase {
LOGW("Could not write all data: %u -> %d", (unsigned int)size, result);
}
// activate the timer now - if not already done
startTimer();
if (!is_timer_started) startTimer();
return result * decimation();
}

View File

@ -66,13 +66,10 @@ class PWMDriverRP2040 : public DriverPWMBase {
TimerAlarmRepeating ticker;
virtual void startTimer() override {
if (!is_timer_started) {
TRACED();
long wait_time = 1000000l / audio_config.sample_rate;
ticker.setCallbackParameter(this);
ticker.begin(defaultPWMAudioOutputCallbackPico, wait_time, US);
is_timer_started = true;
}
ticker.begin(defaultPWMAudioOutputCallbackPico, audio_config.sample_rate, HZ);
is_timer_started = true;
}

View File

@ -38,6 +38,9 @@ class TimerAlarmRepeating {
bool begin(repeating_timer_callback_t callback_f, uint32_t time,
TimeUnit unit = MS) {
// stop timer if it is already active
if (is_active) end();
// start timer
is_active = p_timer->begin(callback_f, time, unit);
return is_active;
}

View File

@ -38,6 +38,10 @@ class TimerAlarmRepeatingDriverBase {
protected:
void* object = nullptr;
const char* toString(TimeUnit unit){
return TimeUnitStr[(int)unit];
}
};
} // namespace audio_tools

View File

@ -34,7 +34,7 @@ class TimerAlarmRepeatingDriverRP2040 : public TimerAlarmRepeatingDriverBase {
bool begin(const my_repeating_timer_callback_t callback_f, uint32_t time,
TimeUnit unit = MS) override {
bool result = false;
LOGI("timer time: %u %s", (unsigned int)time, unit == MS ? "ms" : "us");
LOGI("timer time: %u %s", (unsigned int)time, toString(unit));
this->instanceCallback = callback_f;
// we determine the time in microseconds