2019-04-16 11:10 AM
Hi, I'm trying to read multiple ADC on DMA channel on the STM32L100RC, but I can't make it work. I already looked for a lot of similar example, but nothing seems to work as the STM32L100RC uses library differently than most other STM32. Here is what I've done so far, I'd be very grateful if someone could point me my mistake. P.S. I left the USART initilization out of the equation as it is working perfectly well. I managed to make it work perfectly for only one ADC. Also, from the example I've seen, I might be wrong on the line PeripheralBaseAddr, but I'm not sure for what else I replace it.
uint16_t aADCDualConvertedValue[2];
int adc0, adc1;
void UART_Initialization (void)
{...}
void ADC_Initialization (void)
{
RCC -> CR |= (0x1U << (0U));
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
ADC_InitTypeDef ADC_InitStruct;
ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStruct.ADC_NbrOfConversion = 2;
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruct.ADC_ScanConvMode = ENABLE;
ADC_Init(ADC1, &ADC_InitStruct);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_16Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_16Cycles);
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&aADCDualConvertedValue;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_Init(DMA2_Channel1, &DMA_InitStructure);
/* DMA2_Stream0 enable */
DMA_Cmd(DMA2_Channel1, ENABLE);
ADC_DMACmd(ADC1,ENABLE);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
}
uint16_t readADC(int channel)
{
uint16_t val = 0;
switch(channel)
{
case 0:
val = aADCDualConvertedValue[0];
break;
case 1:
val = aADCDualConvertedValue[1];
break;
}
return val;
}
int main(void)
{
RCC_DeInit();
GPIO_DeInit(GPIOA);
ADC_Initialization();
UART_Initialization();
RCC_MSICmd(ENABLE);
RCC_HSICmd(ENABLE);
RCC_LSICmd(ENABLE);
while(1)
{
adc0 = readADC(0);
adc1 = readADC(1);
Usart1PutNumber(adc0); //Send adc0 data
USART_SendData(USART1, terminator);
Usart1PutNumber(adc1); //Send adc1 data
}
}
}