@@ -110,13 +110,14 @@ void common_hal_audioio_audioout_construct(audioio_audioout_obj_t* self,
110
110
_pm_enable_bus_clock (PM_BUS_APBC , DAC );
111
111
#endif
112
112
113
- // SAMD21: This clock should be <= 12 MHz, per datasheet section 47.6.3.
114
- // SAMD51: This clock should be <= 350kHz, per datasheet table 37-6.
113
+ // SAMD51: This clock should be <= 12 MHz, per datasheet section 47.6.3.
114
+ // SAMD21: This clock is 48mhz despite the datasheet saying it must only be <= 350kHz, per
115
+ // datasheet table 37-6. It's incorrect because the max output rate is 350ksps and is only
116
+ // achieved when the GCLK is more than 8mhz.
115
117
_gclk_enable_channel (DAC_GCLK_ID , CONF_GCLK_DAC_SRC );
116
118
117
-
118
- DAC -> CTRLA .bit .SWRST = 1 ;
119
- while (DAC -> CTRLA .bit .SWRST == 1 ) {}
119
+ DAC -> CTRLA .bit .SWRST = 1 ;
120
+ while (DAC -> CTRLA .bit .SWRST == 1 ) {}
120
121
121
122
bool channel0_enabled = true;
122
123
#ifdef SAMD51
@@ -127,9 +128,11 @@ void common_hal_audioio_audioout_construct(audioio_audioout_obj_t* self,
127
128
if (channel0_enabled ) {
128
129
#ifdef SAMD21
129
130
DAC -> EVCTRL .reg |= DAC_EVCTRL_STARTEI ;
131
+ // We disable the voltage pump because we always run at 3.3v.
130
132
DAC -> CTRLB .reg = DAC_CTRLB_REFSEL_AVCC |
131
133
DAC_CTRLB_LEFTADJ |
132
- DAC_CTRLB_EOEN ;
134
+ DAC_CTRLB_EOEN |
135
+ DAC_CTRLB_VPD ;
133
136
#endif
134
137
#ifdef SAMD51
135
138
DAC -> EVCTRL .reg |= DAC_EVCTRL_STARTEI0 ;
@@ -283,6 +286,16 @@ void common_hal_audioio_audioout_play(audioio_audioout_obj_t* self,
283
286
common_hal_audioio_audioout_stop (self );
284
287
}
285
288
audio_dma_result result = AUDIO_DMA_OK ;
289
+ uint32_t sample_rate = audiosample_sample_rate (sample );
290
+ #ifdef SAMD21
291
+ uint32_t max_sample_rate = 350000 ;
292
+ #endif
293
+ #ifdef SAMD51
294
+ uint32_t max_sample_rate = 1000000 ;
295
+ #endif
296
+ if (sample_rate > max_sample_rate ) {
297
+ mp_raise_ValueError_varg ("Sample rate too high. It must be less than %d" , max_sample_rate );
298
+ }
286
299
#ifdef SAMD21
287
300
result = audio_dma_setup_playback (& self -> left_dma , sample , loop , true, 0 ,
288
301
false /* output unsigned */ ,
0 commit comments