14 # error "Wrong implementation included for this platform"
17 #include <hardware/dma.h>
23 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
34 #include <hardware/adc.h>
38 #define getADCReading() rp2040_adc_result
39 #define channelNumToIndex(channel) channel
131 #define LOOP_YIELD tight_loop_contents();
134 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED)
135 #include <hardware/pwm.h>
137 # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
140 # if (MOZZI_AUDIO_CHANNELS > 1
)
147 #include <pico/time.h>
169 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC)
181 # if (MOZZI_AUDIO_BITS == 8
)
182 # if (MOZZI_AUDIO_CHANNELS > 1
)
188 # elif (MOZZI_AUDIO_BITS == 16
)
189 # if (MOZZI_AUDIO_CHANNELS > 1
)
195 # elif (MOZZI_AUDIO_BITS == 24
)
196 # if (MOZZI_AUDIO_CHANNELS > 1
)
202 # elif (MOZZI_AUDIO_BITS == 32
)
203 # if (MOZZI_AUDIO_CHANNELS > 1
)
209 # error Invalid number of MOZZI_AUDIO_BITS configured
216 static void startAudio() {
217 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
220 analogWrite(MOZZI_AUDIO_PIN_1, MOZZI_AUDIO_BIAS);
223 pwm_config c = pwm_get_default_config();
224 pwm_config_set_clkdiv(&c, 1);
225 pwm_config_set_wrap(&c, 1l << MOZZI_AUDIO_BITS);
226 pwm_init(pwm_gpio_to_slice_num(MOZZI_AUDIO_PIN_1), &c,
true);
227 gpio_set_function(MOZZI_AUDIO_PIN_1, GPIO_FUNC_PWM);
228 gpio_set_drive_strength(MOZZI_AUDIO_PIN_1, GPIO_DRIVE_STRENGTH_12MA);
229 # if (MOZZI_AUDIO_CHANNELS > 1
)
230 # if ((MOZZI_AUDIO_PIN_1 / 2
) != (MOZZI_AUDIO_PIN_1 / 2
))
231 # error Audio channel pins for stereo or HIFI must be on the same PWM slice (which is the case for the pairs (0
,1
), (2
,3
), (4
,5
), etc. Adjust MOZZI_AUDIO_PIN_1/2
.
233 gpio_set_function(MOZZI_AUDIO_PIN_2, GPIO_FUNC_PWM);
234 gpio_set_drive_strength(MOZZI_AUDIO_PIN_2, GPIO_DRIVE_STRENGTH_12MA);
238 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED)
239 for (audio_update_alarm_num = 0; audio_update_alarm_num < 4; ++audio_update_alarm_num) {
240 if (!hardware_alarm_is_claimed(audio_update_alarm_num)) {
241 hardware_alarm_claim(audio_update_alarm_num);
242 hardware_alarm_set_callback(audio_update_alarm_num, audioOutputCallback);
246 micros_per_update = 1000000l / MOZZI_AUDIO_RATE;
248 next_audio_update = make_timeout_time_us(micros_per_update);
250 }
while (hardware_alarm_set_target(audio_update_alarm_num, next_audio_update));
252 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC)
253 i2s.setBCLK(MOZZI_I2S_PIN_BCK);
254 i2s.setDATA(MOZZI_I2S_PIN_DATA);
255 i2s.setBitsPerSample(MOZZI_AUDIO_BITS);
257 # if (MOZZI_AUDIO_BITS > 16
)
258 i2s.setBuffers(MOZZI_RP2040_BUFFERS, (size_t) (MOZZI_RP2040_BUFFER_SIZE/MOZZI_RP2040_BUFFERS), 0);
260 i2s.setBuffers(MOZZI_RP2040_BUFFERS, (size_t) (MOZZI_RP2040_BUFFER_SIZE/MOZZI_RP2040_BUFFERS/2), 0);
262 # if MOZZI_IS(MOZZI_I2S_FORMAT, MOZZI_I2S_FORMAT_LSBJ)
265 i2s.begin(MOZZI_AUDIO_RATE);
270 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED)
271 hardware_alarm_set_callback(audio_update_alarm_num, NULL);
272 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC)
281 #warning Automatic random seeding is not implemented on this platform
287 #undef MOZZI_RP2040_BUFFERS
288 #undef MOZZI_RP2040_BUFFER_SIZE