6.8.2.1.2 Code
Add to the main application source file, outside of any functions:
#define DATA_LENGTH (16)
structdac_module dac_instance;
structrtc_module rtc_instance;
structevents_resource event_dac;
staticvolatilebooltransfer_is_done =false;
Callback function:staticuint16_t dac_data[DATA_LENGTH];
Copy-paste the following setup code to your user application:voiddac_callback(uint8_t channel){UNUSED(channel);transfer_is_done =true;}
voidconfigure_rtc_count(void){structrtc_count_events rtc_event;structrtc_count_config config_rtc_count;rtc_count_get_config_defaults(&config_rtc_count);config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;config_rtc_count.mode = RTC_COUNT_MODE_16BIT;#ifdef FEATURE_RTC_CONTINUOUSLY_UPDATEDconfig_rtc_count.continuously_update =true;#endifrtc_count_init(&rtc_instance, RTC, &config_rtc_count);rtc_event.generate_event_on_overflow =true;rtc_count_enable_events(&rtc_instance, &rtc_event);rtc_count_enable(&rtc_instance);}
voidconfigure_dac(void){structdac_config config_dac;dac_get_config_defaults(&config_dac);#if (SAML21)dac_instance.start_on_event[DAC_CHANNEL_0] =true;#elsedac_instance.start_on_event =true;#endifdac_init(&dac_instance, DAC, &config_dac);structdac_events events =#if (SAML21){ .on_event_chan0_start_conversion =true};#else{ .on_event_start_conversion =true};#endifdac_enable_events(&dac_instance, &events);}
Define a data length variables and add to user application (typically the start of main()):voidconfigure_dac_channel(void){structdac_chan_config config_dac_chan;dac_chan_get_config_defaults(&config_dac_chan);dac_chan_set_config(&dac_instance, DAC_CHANNEL_0,&config_dac_chan);dac_chan_enable(&dac_instance, DAC_CHANNEL_0);}
uint32_t i;
Add to user application initialization (typically the start of main()): configure_rtc_count();rtc_count_set_period(&rtc_instance, 1);configure_dac();configure_dac_channel();dac_enable(&dac_instance);configure_event_resource();dac_register_callback(&dac_instance, DAC_CHANNEL_0,dac_callback,DAC_CALLBACK_TRANSFER_COMPLETE);dac_chan_enable_callback(&dac_instance, DAC_CHANNEL_0,DAC_CALLBACK_TRANSFER_COMPLETE);for(i = 0;i < DATA_LENGTH;i++) {dac_data[i] = 0xfff * i;}
