Hi,
I am looking at the code you attached but I am not really sure how your configurations are working. I have some doubts and I will give you some hints for the application.
In the main function you are following the next sequence:
1. Enable the clock gates for the Ports and ADC1.
SIM_SCGC5 = SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;
SIM_SCGC3 |= (SIM_SCGC3_ADC1_MASK );
2. Writing the values for the ADC configuration structure: single conversion, no interrupts, dma enable and prepare the data to read from channel 20 (ADC_SC1_ADCH(20))
3. Enabling DMA channel 1 with:
void Start_Dma(uint8_t Dma_Chn)
{
DMA_ERQ |= (1 << Dma_Chn); //enalbe DMA Dma_Chn channle
}
This function is enabling the DMA channel request. It is NOT start DMA transfers.
4. Configuring the ADC with the ADC configuration structure:
ADC_Config_Alt(ADC1_BASE_PTR, &Master_Adc_Config); // config ADC1
Calling this function writes to the ADCH field that will automatically start the ADC channel 20 conversion. When the ADC channel 20 conversion is finished the COCO flag will SET.
5. Configuring the DMA channel 1:
Dma_Conif(1,30, (uint32_t)(&sou),(uint32_t)(&des),1);
Here you are configuring the DMA channel 0 with the Source 30 as the trigger source. The trigger source for the ADC1 is the 41, you need to write the value 41 for the DMA trigger source request.
Also I see that the function DMA_Config is writing the next:
DMA_ERQ &= ~(1 << Dma_Chn); //close DMA channle
This instruction will disable the Channel 0 and any request will be serviced, even if the trigger source request it.
I did not test the code to check if it is writing the correct values to the registers. I think that a best sequence to configure the modules is the next (pseudo code):
int main(void)
{
/* Turn on all port clocks */
SIM_SCGC5 = SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;
SIM_SCGC3 |= (SIM_SCGC3_ADC1_MASK );
// Write the configuration values to the Master_Adc_Config structure
Dma_Conif(1,41, (uint32_t)(&sou),(uint32_t)(&des),1); //Configure DMA channel 1 with request source 41 for ADC1
Start_Dma(1); // Enabling DMA channel 1 that is already configured
ADC_Config_Alt(ADC1_BASE_PTR, &Master_Adc_Config); // config ADC1 and initiates the ADC1 channel 20 conversion
}
With this the DMA will be already configured when the ADC conversion ends and the COCO flag is SET.
Please check the points I mentioned and share your results.
I hope this information can help you.
Regards,
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. It would be nice!
-----------------------------------------------------------------------------------------------------------------------