1
1
2
2
#pragma once
3
- #if defined(ARDUINO_ARCH_RP2040)
3
+ #if defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED_RP2040)
4
4
#include " AudioPWM/PWMAudioBase.h"
5
5
#include " hardware/gpio.h"
6
6
#include " hardware/adc.h"
@@ -12,7 +12,7 @@ namespace audio_tools {
12
12
// forwrd declaratioin of callback
13
13
class PWMAudioStreamPico ;
14
14
typedef PWMAudioStreamPico PWMAudioStream;
15
- bool defaultPWMAudioOutputCallbackPico (repeating_timer* ptr);
15
+ // bool defaultPWMAudioOutputCallbackPico(repeating_timer* ptr);
16
16
17
17
/* *
18
18
* @brief Rasperry Pico Channel to pin assignments
@@ -36,7 +36,7 @@ struct PicoChannelOut {
36
36
*/
37
37
38
38
class PWMAudioStreamPico : public PWMAudioStreamBase {
39
- friend bool defaultPWMAudioOutputCallbackPico (repeating_timer* ptr);
39
+ // friend bool defaultPWMAudioOutputCallbackPico(repeating_timer* ptr);
40
40
41
41
public:
42
42
@@ -45,8 +45,9 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
45
45
}
46
46
47
47
// / Ends the output -> resets the timer and the pins
48
- void end (){
49
- LOGD (LOG_METHOD);
48
+ void end () override {
49
+ LOGD (LOG_METHOD);
50
+ is_timer_started = false ;
50
51
if (!cancel_repeating_timer (&timer)){
51
52
LOGE (" cancel_repeating_timer failed" )
52
53
}
@@ -61,10 +62,14 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
61
62
Vector<PicoChannelOut> pins;
62
63
repeating_timer_t timer;
63
64
65
+ virtual void startTimer () override {
66
+ // LOGI(LOG_METHOD);
67
+ is_timer_started = true ;
68
+ }
64
69
65
70
// setup pwm config and all pins
66
- void setupPWM (){
67
- LOGD (LOG_METHOD);
71
+ void setupPWM () override {
72
+ LOGD (LOG_METHOD);
68
73
pwm_config cfg = setupPWMConfig ();
69
74
70
75
// initialize empty pins
@@ -96,8 +101,11 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
96
101
pwm_config pico_pwm_config = pwm_get_default_config ();
97
102
int wrap_value = maxOutputValue (); // amplitude of square wave (pwm values -amplitude to amplitude) for one byte
98
103
float pwmClockDivider = static_cast <float >(clock_get_hz (clk_sys)) / (audio_config.pwm_frequency * wrap_value);
99
- LOGI (" ->clock speed is %f" , static_cast <float >(clock_get_hz (clk_sys)));
100
- LOGI (" ->divider is %f" , pwmClockDivider);
104
+ float clock_speed = static_cast <float >(clock_get_hz (clk_sys));
105
+ LOGI (" ->wrap_value = %d" , wrap_value);
106
+ LOGI (" ->max clock speed = %f" , clock_speed);
107
+ LOGI (" ->divider = %f" , pwmClockDivider);
108
+ LOGI (" ->clock speed = %f" , clock_speed/pwmClockDivider);
101
109
pwm_config_set_clkdiv (&pico_pwm_config, pwmClockDivider);
102
110
pwm_config_set_clkdiv_mode (&pico_pwm_config, PWM_DIV_FREE_RUNNING);
103
111
// pwm_config_set_phase_correct(&pico_pwm_config, false);
@@ -106,7 +114,7 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
106
114
}
107
115
108
116
// set up pwm
109
- void setupPWMPin (pwm_config &cfg, PicoChannelOut &pinInfo){
117
+ void setupPWMPin (pwm_config &cfg, PicoChannelOut &pinInfo) {
110
118
LOGD (" %s for gpio %d" ,LOG_METHOD, pinInfo.gpio );
111
119
// setup pwm pin
112
120
int gpio = pinInfo.gpio ;
@@ -119,43 +127,45 @@ class PWMAudioStreamPico : public PWMAudioStreamBase {
119
127
pwm_set_chan_level (pinInfo.slice , pinInfo.channel , 0 );
120
128
}
121
129
122
- void setupTimer (){
123
- LOGD (LOG_METHOD);
130
+ void setupTimer () override {
131
+ LOGD (LOG_METHOD);
124
132
// setup timer
125
133
uint32_t time = 1000000UL / audio_config.sample_rate ;
126
134
LOGI (" ->Timer value %ld us" , time);
127
-
128
135
if (!add_repeating_timer_us (-time, &defaultPWMAudioOutputCallbackPico, this , &timer)){
129
136
LOGE (" Error: alarm_pool_add_repeating_timer_us failed; no alarm slots available" );
137
+ } else {
138
+ LOGI (" repeating timer has started" );
130
139
}
131
140
}
132
141
133
142
// / The pico supports max 16 pwm pins
134
- virtual int maxChannels () {
143
+ virtual int maxChannels () override {
135
144
return 16 ;
136
145
};
137
146
138
147
// / Max pwm output value
139
- virtual int maxOutputValue (){
140
- return 255 ;
148
+ virtual int maxOutputValue ()override {
149
+ return std::pow (audio_config. resolution , 2 )- 1 ;
141
150
}
142
151
143
152
// / write a pwm value to the indicated channel. The values are between 0 and 255
144
- void pwmWrite (int audioChannel, int value){
153
+ void pwmWrite (int audioChannel, int value)override {
145
154
pwm_set_chan_level (pins[audioChannel].slice , pins[audioChannel].channel , value);
146
155
}
147
156
157
+ // timed output executed at the sampleRate
158
+ static bool defaultPWMAudioOutputCallbackPico (repeating_timer* ptr) {
159
+ PWMAudioStreamPico *self = (PWMAudioStreamPico*) ptr->user_data ;
160
+ // if (self!=nullptr){
161
+ self->playNextFrame ();
162
+ // }
163
+ return true ;
164
+ }
165
+
148
166
};
149
167
150
168
151
- // timed output executed at the sampleRate
152
- bool defaultPWMAudioOutputCallbackPico (repeating_timer* ptr) {
153
- PWMAudioStreamPico *self = (PWMAudioStreamPico*) ptr->user_data ;
154
- if (self!=nullptr ){
155
- self->playNextFrame ();
156
- }
157
- return true ;
158
- }
159
169
160
170
} // Namespace
161
171
0 commit comments