@@ -143,11 +143,14 @@ typedef struct _pyb_dac_obj_t {
143143 uint16_t pin ; // GPIO_PIN_4 or GPIO_PIN_5
144144 uint8_t bits ; // 8 or 12
145145 uint8_t state ;
146+ uint8_t outbuf_single ;
147+ uint8_t outbuf_waveform ;
146148} pyb_dac_obj_t ;
147149
148150STATIC mp_obj_t pyb_dac_init_helper (pyb_dac_obj_t * self , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
149151 static const mp_arg_t allowed_args [] = {
150152 { MP_QSTR_bits , MP_ARG_INT , {.u_int = 8 } },
153+ { MP_QSTR_buffering , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_rom_obj = MP_ROM_PTR (& mp_const_none_obj )} },
151154 };
152155
153156 // parse args
@@ -194,6 +197,19 @@ STATIC mp_obj_t pyb_dac_init_helper(pyb_dac_obj_t *self, size_t n_args, const mp
194197 mp_raise_ValueError ("unsupported bits" );
195198 }
196199
200+ // set output buffer config
201+ if (args [1 ].u_obj == mp_const_none ) {
202+ // due to legacy, default values differ for single and waveform outputs
203+ self -> outbuf_single = DAC_OUTPUTBUFFER_DISABLE ;
204+ self -> outbuf_waveform = DAC_OUTPUTBUFFER_ENABLE ;
205+ } else if (mp_obj_is_true (args [1 ].u_obj )) {
206+ self -> outbuf_single = DAC_OUTPUTBUFFER_ENABLE ;
207+ self -> outbuf_waveform = DAC_OUTPUTBUFFER_ENABLE ;
208+ } else {
209+ self -> outbuf_single = DAC_OUTPUTBUFFER_DISABLE ;
210+ self -> outbuf_waveform = DAC_OUTPUTBUFFER_DISABLE ;
211+ }
212+
197213 // reset state of DAC
198214 self -> state = DAC_STATE_RESET ;
199215
@@ -289,7 +305,7 @@ STATIC mp_obj_t pyb_dac_noise(mp_obj_t self_in, mp_obj_t freq) {
289305 // configure DAC to trigger via TIM6
290306 DAC_ChannelConfTypeDef config ;
291307 config .DAC_Trigger = DAC_TRIGGER_T6_TRGO ;
292- config .DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE ;
308+ config .DAC_OutputBuffer = self -> outbuf_waveform ;
293309 HAL_DAC_ConfigChannel (& DAC_Handle , & config , self -> dac_channel );
294310 self -> state = DAC_STATE_BUILTIN_WAVEFORM ;
295311 }
@@ -319,7 +335,7 @@ STATIC mp_obj_t pyb_dac_triangle(mp_obj_t self_in, mp_obj_t freq) {
319335 // configure DAC to trigger via TIM6
320336 DAC_ChannelConfTypeDef config ;
321337 config .DAC_Trigger = DAC_TRIGGER_T6_TRGO ;
322- config .DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE ;
338+ config .DAC_OutputBuffer = self -> outbuf_waveform ;
323339 HAL_DAC_ConfigChannel (& DAC_Handle , & config , self -> dac_channel );
324340 self -> state = DAC_STATE_BUILTIN_WAVEFORM ;
325341 }
@@ -342,7 +358,7 @@ STATIC mp_obj_t pyb_dac_write(mp_obj_t self_in, mp_obj_t val) {
342358 if (self -> state != DAC_STATE_WRITE_SINGLE ) {
343359 DAC_ChannelConfTypeDef config ;
344360 config .DAC_Trigger = DAC_TRIGGER_NONE ;
345- config .DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE ;
361+ config .DAC_OutputBuffer = self -> outbuf_single ;
346362 HAL_DAC_ConfigChannel (& DAC_Handle , & config , self -> dac_channel );
347363 self -> state = DAC_STATE_WRITE_SINGLE ;
348364 }
@@ -454,7 +470,7 @@ mp_obj_t pyb_dac_write_timed(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
454470 if (self -> state != DAC_STATE_DMA_WAVEFORM + dac_trigger ) {
455471 DAC_ChannelConfTypeDef config ;
456472 config .DAC_Trigger = dac_trigger ;
457- config .DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE ;
473+ config .DAC_OutputBuffer = self -> outbuf_waveform ;
458474 HAL_DAC_ConfigChannel (& DAC_Handle , & config , self -> dac_channel );
459475 self -> state = DAC_STATE_DMA_WAVEFORM + dac_trigger ;
460476 }
0 commit comments