#define SAMPLE_BUFFER_LENGTH 32
uint32_t adcInputBuffer[SAMPLE_BUFFER_LENGTH];
uint32_t holdingInputBuffer[SAMPLE_BUFFER_LENGTH];
void hw_adc_setup(void)
{
SC->PCONP |= PCONP_PCAD | PCONP_PCGPDMA;
SC->PCLKSEL0 |= 1<<24;
PINCON->PINSEL1 &= (uint32_t) ~(0x3f << 14);
PINCON->PINSEL1 |= 0x15 << 14;
ADC->ADCR = (uint32_t) ((1 << 21) | (1 << 8) | 0x01);
/* Enable DMA */
GPDMA->DMACConfig = 0x01;
/* Clear errors etc */
GPDMA->DMACIntTCClear = 0xff;
GPDMA->DMACIntErrClr = 0xff;
/* Set up DMA channel */
GPDMACH0->DMACCConfig = 0;
GPDMACH0->DMACCControl =
(SAMPLE_BUFFER_LENGTH << 0) | /* Transfer size */
(0x00 << 12) | /* Source burst size */
(0x00 << 15) | /* Destination burst size */
(0x02 << 18) | /* 32 bit word source transfer width */
(0x02 << 21) | /* 32 bit word destination transfer width */
(0x01 << 27) | /* Destination increment */
(0x01UL << 31); /* Terminal count interrupt enable bit */
GPDMACH0->DMACCLLI = (uint32_t) NULL;
GPDMACH0->DMACCDestAddr = (uint32_t) adcInputBuffer;
GPDMACH0->DMACCSrcAddr = ADC->ADDR0;
/* Fixme: Don't know if this is needed but it was in the example... */
while((GPDMA->DMACConfig & 0x01) == 0);
GPDMACH0->DMACCConfig =
(4 << 1) | /* Source peripheral ADC */
(2 << 11) | /* Transfer type; P->M */
(1 << 14) | /* Interrupt error mask */
(1 << 15); /* Terminal count interrupt mask */
ADC->ADINTEN = 1<<8;
NVIC_EnableIRQ(DMA_IRQn);
NVIC_SetPriority(DMA_IRQn, 5);
}
void hw_adc_trig(void)
{
/* Set buffer address */
GPDMACH0->DMACCDestAddr = (uint32_t) adcInputBuffer;
/* Enable DMA-channel*/
GPDMACH0->DMACCConfig |= 0x01;
/* Burst mode */
ADC->ADCR |= 1 << 16;
hw_gpio_set_testpin_1(true);
}
void DMA_IRQHandler (void)
{
uint32_t IntTCStat = GPDMA->DMACRawIntTCStat;
uint32_t IntErrStat = GPDMA->DMACRawIntErrStat;
memcpy(holdingInputBuffer, adcInputBuffer, SAMPLE_BUFFER_LENGTH * 4);
GPDMA->DMACIntErrClr = IntTCStat;
GPDMA->DMACIntErrClr = IntErrStat;
hw_gpio_set_testpin_1(false);
/* Turn off burst mode */
ADC->ADCR &= ~(1UL << 16);
/* Disable DMA-channel*/
GPDMACH0->DMACCConfig &= ~0x01UL;
}
|