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