Electronic – Finding ADC pins from Init code

cmicrocontrollerstm32stm32cubemxstm32l

I am trying to figure out the GPIO Pins that are used for ADC input looking at the following ADC Initialization code for STM32L476RG.

      // DMA for ADC1
  RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN; // DMA1
  // Wait a bit
  nop(); nop(); nop(); nop(); nop(); nop();
  DMA1_Channel1->CPAR = (uint32_t)&(ADC123_COMMON->CDR);
  DMA1_Channel1->CMAR = (uint32_t)&adc_data;
  DMA1_Channel1->CNDTR = 2;
  DMA1_Channel1->CCR = DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1 | DMA_CCR_MINC |
                       DMA_CCR_CIRC | DMA_CCR_TCIE | DMA_CCR_EN;

  // ADC123
  RCC->AHB2ENR |= RCC_AHB2ENR_ADCEN;
  // Connect to system clock
  RCC->CCIPR |= RCC_CCIPR_ADCSEL_0 | RCC_CCIPR_ADCSEL_1;
  // Divide clock (/8)
  ADC123_COMMON->CCR |= ADC_CCR_PRESC_2;
  // Dual mode
  ADC123_COMMON->CCR |= ADC_CCR_DUAL_1 | ADC_CCR_DUAL_2;
  // MDMA
  ADC123_COMMON->CCR |= ADC_CCR_MDMA_1;

  // ADC1+2+3
  // Disable DEEPPWD, enable ADVREGEN
  ADC1->CR = ADC_CR_ADVREGEN;
  ADC2->CR = ADC_CR_ADVREGEN;

  // Wait a bit
  int n; for(n=0;n<100000;n++) nop();

  // Calibrate
  ADC1->CR |= ADC_CR_ADCAL;
  ADC2->CR |= ADC_CR_ADCAL;
  while(ADC1->CR & ADC_CR_ADCAL);
  while(ADC2->CR & ADC_CR_ADCAL);
  // Wait a bit
  for(n=0;n<100000;n++) nop();

  // Enable procedure
  ADC1->ISR |= ADC_ISR_ADRDY;
  ADC1->CR |= ADC_CR_ADEN;
  ADC2->ISR |= ADC_ISR_ADRDY;
  ADC2->CR |= ADC_CR_ADEN;
  while(!(ADC1->ISR & ADC_ISR_ADRDY));
  while(!(ADC2->ISR & ADC_ISR_ADRDY));

  // Sequence
  ADC1->SQR1 = (1<<6) | (3<<12) | 1;
  ADC2->SQR1 = (2<<6) | (4<<12) | 1;

  // Oversampling (16x)
  ADC1->CFGR2 = (3<<2) | 1;
  ADC2->CFGR2 = (3<<2) | 1;

I expect 3 input channels but cannot find out which are those.

Best Answer

According to the datasheet, Your device has 3 separate ADC modules with 16 input channels (not 2 or 3 channels as suggested in the comments to your question).

From a snippet of your initialization code:

  ADC1->SQR1 = (1<<6) | (3<<12) | 1;
  ADC2->SQR1 = (2<<6) | (4<<12) | 1;

it appears that ADC1 & ADC2 are being used, and each of them is being configured to sample 2 different channels - so 4 in total.
ADC1 is sampling channel 1 & channel 3.
ADC2 is sampling channel 2 & channel 4.
See page 602 of the reference manual.

To work out the physical pins these channels refer to, we go back to the datasheet - The section of Table 16 on pages 71 & 72 tells us that channel 1 for all 3 ADC modules (ADC123_IN1) is on PC0/pin-8 of your STM32L476RG 64-pin package.
Similarly, channel 2 is PC1/pin-9 and channels 3 & 4 are PC2/pin-10 & PC3/pin-11.