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
41 inline void adc_run_once () {
42 hw_set_bits(&adc_hw->cs, ADC_CS_START_ONCE_BITS);
45 uint8_t adcPinToChannelNum(uint8_t pin) {
46 if (pin >= 26) pin -= 26;
51 void adcStartConversion(uint8_t channel) {
52 adc_select_input(channel);
57 void startSecondADCReadOnCurrentChannel() {
62 void setupFastAnalogRead(int8_t speed) {
63 if (speed == FAST_ADC) {
65 }
else if (speed == FASTER_ADC) {
72 void rp2040_adc_queue_handler();
74 static uint16_t rp2040_adc_result = 0;
75 int rp2040_adc_dma_chan;
76 void setupMozziADC(int8_t speed) {
77 for (
int i = 0; i < (int) NUM_ANALOG_INPUTS; ++i) {
90 uint dma_chan = dma_claim_unused_channel(
true);
91 rp2040_adc_dma_chan = dma_chan;
92 static dma_channel_config cfg = dma_channel_get_default_config(dma_chan);
95 channel_config_set_transfer_data_size(&cfg, DMA_SIZE_16);
96 channel_config_set_read_increment(&cfg,
false);
97 channel_config_set_write_increment(&cfg,
false);
100 channel_config_set_dreq(&cfg, DREQ_ADC);
102 dma_channel_configure(dma_chan, &cfg,
110 dma_channel_set_irq0_enabled(dma_chan,
true);
111 irq_add_shared_handler(DMA_IRQ_0, rp2040_adc_queue_handler, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY);
112 irq_set_enabled(DMA_IRQ_0,
true);
113 dma_channel_start(dma_chan);
116 void rp2040_adc_queue_handler() {
117 if (!dma_channel_get_irq0_status(rp2040_adc_dma_chan))
return;
118 dma_channel_acknowledge_irq0(rp2040_adc_dma_chan);
121 dma_channel_set_trans_count(rp2040_adc_dma_chan, 1,
true);
131 #define LOOP_YIELD tight_loop_contents();
134 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
135 #include <hardware/pwm.h>
139 #include <pico/time.h>
146 absolute_time_t next_audio_update;
147 uint64_t micros_per_update, next_audio_update_shifted;
148 const uint64_t micros_per_update_shifted = (1000000l << 8) /
MOZZI_AUDIO_RATE;
149 uint audio_update_alarm_num;
151 void audioOutputCallback(uint) {
153 defaultAudioOutput();
154 next_audio_update_shifted += micros_per_update_shifted;
155 next_audio_update = delayed_by_us(nil_time, next_audio_update_shifted>>8);
157 }
while (hardware_alarm_set_target(audio_update_alarm_num, next_audio_update));
160 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
170 # if (MOZZI_AUDIO_CHANNELS > 1)
172 inline bool canBufferAudioOutput() {
173 return (pwm.availableForWrite()>1);
177 inline bool canBufferAudioOutput() {
178 return (pwm.availableForWrite());
185 # if (MOZZI_AUDIO_CHANNELS > 1)
190 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC)
196 inline bool canBufferAudioOutput() {
197 return (i2s.availableForWrite());
202 # if (MOZZI_AUDIO_BITS == 8)
203 # if (MOZZI_AUDIO_CHANNELS > 1)
204 i2s.write8(f.l(), f.r());
206 i2s.write8(f.l(), 0);
209 # elif (MOZZI_AUDIO_BITS == 16)
210 # if (MOZZI_AUDIO_CHANNELS > 1)
211 i2s.write16(f.l(), f.r());
213 i2s.write16(f.l(), 0);
216 # elif (MOZZI_AUDIO_BITS == 24)
217 # if (MOZZI_AUDIO_CHANNELS > 1)
218 i2s.write24(f.l(), f.r());
220 i2s.write24(f.l(), 0);
223 # elif (MOZZI_AUDIO_BITS == 32)
224 # if (MOZZI_AUDIO_CHANNELS > 1)
225 i2s.write32(f.l(), f.r());
227 i2s.write32(f.l(), 0);
230 # error Invalid number of MOZZI_AUDIO_BITS configured
237 static void startAudio() {
238 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
241 # if (MOZZI_AUDIO_CHANNELS > 1)
242 # if ((MOZZI_AUDIO_PIN_1 / 2) != (MOZZI_AUDIO_PIN_1 / 2))
243 # 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 .
245 gpio_set_drive_strength(MOZZI_AUDIO_PIN_2, GPIO_DRIVE_STRENGTH_12MA);
247 pwm.setBuffers(MOZZI_RP2040_BUFFERS, (
size_t) (MOZZI_RP2040_BUFFER_SIZE/MOZZI_RP2040_BUFFERS));
252 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
253 for (audio_update_alarm_num = 0; audio_update_alarm_num < 4; ++audio_update_alarm_num) {
254 if (!hardware_alarm_is_claimed(audio_update_alarm_num)) {
255 hardware_alarm_claim(audio_update_alarm_num);
256 hardware_alarm_set_callback(audio_update_alarm_num, audioOutputCallback);
262 next_audio_update = make_timeout_time_us(micros_per_update);
263 next_audio_update_shifted = to_us_since_boot(next_audio_update) << 8;
265 }
while (hardware_alarm_set_target(audio_update_alarm_num, next_audio_update));
267 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC)
268 i2s.setBCLK(MOZZI_I2S_PIN_BCK);
269 i2s.setDATA(MOZZI_I2S_PIN_DATA);
272 # if (MOZZI_AUDIO_BITS > 16)
273 i2s.setBuffers(MOZZI_RP2040_BUFFERS, (
size_t) (MOZZI_RP2040_BUFFER_SIZE/MOZZI_RP2040_BUFFERS), 0);
275 i2s.setBuffers(MOZZI_RP2040_BUFFERS, (
size_t) (MOZZI_RP2040_BUFFER_SIZE/MOZZI_RP2040_BUFFERS/2), 0);
277 # if MOZZI_IS(MOZZI_I2S_FORMAT, MOZZI_I2S_FORMAT_LSBJ)
285 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
286 hardware_alarm_set_callback(audio_update_alarm_num, NULL);
287 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC)
289 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
297 void MozziRandPrivate::autoSeed() {
298 #warning Automatic random seeding is not implemented on this platform
304 #undef MOZZI_RP2040_BUFFERS
305 #undef MOZZI_RP2040_BUFFER_SIZE
#define MOZZI_AUDIO_BITS
Output resolution of audio samples.
#define MOZZI_AUDIO_PIN_1
Only for MOZZI_AUDIO_MODE s MOZZI_OUTPUT_PWM and MOZZI_OUTPUT_2PIN_PWM: The IO pin to use as (first) ...
#define MOZZI_AUDIO_RATE
Defines the audio rate, i.e.
void stopMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
This struct encapsulates one frame of mono audio output.