2.8.3.1.2 Code
Add to the main application source file, outside of any functions:
struct
dac_module dac_instance;
struct
adc_module adc_instance;
struct
dma_resource example_resource;
Copy-paste the following setup code to your user application:COMPILER_ALIGNED(16)
DmacDescriptor example_descriptor SECTION_DMAC_DESCRIPTOR;
Add to user application initialization (typically the start of main()):void
configure_adc(
void
)
{
struct
adc_config config_adc;
adc_get_config_defaults(&config_adc);
#if !(SAML21)
#if !(SAMC21)
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
#endif
config_adc.resolution = ADC_RESOLUTION_10BIT;
#endif
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16;
config_adc.reference = ADC_REFERENCE_INTVCC1;
config_adc.positive_input = ADC_POSITIVE_INPUT_PIN4;
config_adc.freerunning =
true
;
config_adc.left_adjust =
false
;
#if (SAMC21)
adc_init(&adc_instance, ADC1, &config_adc);
#else
adc_init(&adc_instance, ADC, &config_adc);
#endif
adc_enable(&adc_instance);
}
void
configure_dac(
void
)
{
struct
dac_config config_dac;
dac_get_config_defaults(&config_dac);
#if (SAML21)
config_dac.reference = DAC_REFERENCE_INTREF;
#else
config_dac.reference = DAC_REFERENCE_AVCC;
#endif
dac_init(&dac_instance, DAC, &config_dac);
}
void
configure_dac_channel(
void
)
{
struct
dac_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);
}
void
configure_dma_resource(
struct
dma_resource *resource)
{
struct
dma_resource_config config;
dma_get_config_defaults(&config);
#if (SAMC21)
config.peripheral_trigger = ADC1_DMAC_ID_RESRDY;
#else
config.peripheral_trigger = ADC_DMAC_ID_RESRDY;
#endif
config.trigger_action = DMA_TRIGGER_ACTON_BEAT;
dma_allocate(resource, &config);
}
void
setup_transfer_descriptor(DmacDescriptor *descriptor)
{
struct
dma_descriptor_config descriptor_config;
dma_descriptor_get_config_defaults(&descriptor_config);
descriptor_config.beat_size = DMA_BEAT_SIZE_HWORD;
descriptor_config.dst_increment_enable =
false
;
descriptor_config.src_increment_enable =
false
;
descriptor_config.block_transfer_count = 1000;
descriptor_config.source_address = (uint32_t)(&adc_instance.hw->RESULT.reg);
#if (SAML21)
descriptor_config.destination_address = (uint32_t)(&dac_instance.hw->DATA[DAC_CHANNEL_0].reg);
#else
descriptor_config.destination_address = (uint32_t)(&dac_instance.hw->DATA.reg);
#endif
descriptor_config.next_descriptor_address = (uint32_t)descriptor;
dma_descriptor_create(descriptor, &descriptor_config);
}
configure_adc();
configure_dac();
configure_dac_channel();
dac_enable(&dac_instance);
configure_dma_resource(&example_resource);
setup_transfer_descriptor(&example_descriptor);
dma_add_descriptor(&example_resource, &example_descriptor);