Skip to content

Commit fc48d78

Browse files
committed
RP2040 PWM stop timer before begin
1 parent d573756 commit fc48d78

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

src/AudioPWM/AudioPWM.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class PWMAudioOutput : public AudioOutput {
2626
}
2727
}
2828

29-
virtual PWMConfig defaultConfig() { return pwm.defaultConfig(); }
29+
virtual PWMConfig defaultConfig(RxTxMode mode=TX_MODE) {
30+
if (mode!=TX_MODE) LOGE("mode not supported: using TX_MODE");
31+
return pwm.defaultConfig();
32+
}
3033

3134
PWMConfig config() { return audio_config; }
3235

src/AudioPWM/PWMAudioBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class DriverPWMBase {
188188
LOGW("Could not write all data: %u -> %d", (unsigned int)size, result);
189189
}
190190
// activate the timer now - if not already done
191-
startTimer();
191+
if (!is_timer_started) startTimer();
192192
return result * decimation();
193193
}
194194

src/AudioPWM/PWMAudioRP2040.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,10 @@ class PWMDriverRP2040 : public DriverPWMBase {
6666
TimerAlarmRepeating ticker;
6767

6868
virtual void startTimer() override {
69-
if (!is_timer_started) {
70-
TRACED();
71-
long wait_time = 1000000l / audio_config.sample_rate;
72-
ticker.setCallbackParameter(this);
73-
ticker.begin(defaultPWMAudioOutputCallbackPico, wait_time, US);
74-
is_timer_started = true;
75-
}
69+
TRACED();
70+
ticker.setCallbackParameter(this);
71+
ticker.begin(defaultPWMAudioOutputCallbackPico, audio_config.sample_rate, HZ);
72+
7673
is_timer_started = true;
7774
}
7875

src/AudioTimer/AudioTimer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class TimerAlarmRepeating {
3838

3939
bool begin(repeating_timer_callback_t callback_f, uint32_t time,
4040
TimeUnit unit = MS) {
41+
// stop timer if it is already active
42+
if (is_active) end();
43+
// start timer
4144
is_active = p_timer->begin(callback_f, time, unit);
4245
return is_active;
4346
}

src/AudioTimer/AudioTimerBase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class TimerAlarmRepeatingDriverBase {
3838

3939
protected:
4040
void* object = nullptr;
41+
42+
const char* toString(TimeUnit unit){
43+
return TimeUnitStr[(int)unit];
44+
}
4145
};
4246

4347
} // namespace audio_tools

src/AudioTimer/AudioTimerRP2040.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TimerAlarmRepeatingDriverRP2040 : public TimerAlarmRepeatingDriverBase {
3434
bool begin(const my_repeating_timer_callback_t callback_f, uint32_t time,
3535
TimeUnit unit = MS) override {
3636
bool result = false;
37-
LOGI("timer time: %u %s", (unsigned int)time, unit == MS ? "ms" : "us");
37+
LOGI("timer time: %u %s", (unsigned int)time, toString(unit));
3838
this->instanceCallback = callback_f;
3939

4040
// we determine the time in microseconds
@@ -45,7 +45,7 @@ class TimerAlarmRepeatingDriverRP2040 : public TimerAlarmRepeatingDriverBase {
4545
break;
4646
case US:
4747
result = alarm_pool_add_repeating_timer_us(ap, time, &staticCallback,
48-
this, &timer);
48+
this, &timer);
4949
break;
5050
case HZ:
5151
// convert hz to time in us

0 commit comments

Comments
 (0)