1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Please check your post formatting using the preview (below the editor text box) before posting. Highlight your code and press the {} button to mark it as code. Then check that all your indentation is correct. Then submit. \$\endgroup\$ – Transistor Apr 13 at 13:07
  • \$\begingroup\$ You need to check the pinout of the device in the datasheet. (You will not find the pin number in the init code.) \$\endgroup\$ – Stefan Wyss Apr 13 at 14:11
  • \$\begingroup\$ @StefanWyss how do I find out which channel or ADC is used so that I can check its pin number in the datasheet? \$\endgroup\$ – ajeebx Apr 13 at 14:44
  • \$\begingroup\$ You must learn how the ADC pins are configured on this particular processor, then you will be able to determine which pins are used by looking at the code. There is no magic here; you have to do some work. \$\endgroup\$ – Elliot Alderson Apr 13 at 14:45
  • \$\begingroup\$ Your code seems to use ADC1 and ADC2 (i.e. channels 1 and 2). You need to check the datasheet for pin numbers of these channels. \$\endgroup\$ – Stefan Wyss Apr 13 at 15:35
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This makes it look so simple.. excellently explained. Can we do these initializations from CubeMX? \$\endgroup\$ – ajeebx Apr 15 at 7:59
  • \$\begingroup\$ @ajeebx - I don't use CubeMX much, so I'm not particularly familiar with it, but I'd be surprised if it was not possible to init the ADCs in this way using CubeMX. \$\endgroup\$ – brhans Apr 15 at 12:15
  • \$\begingroup\$ Can you see the above code again and comment on how the DMA is used to write the ADC sampled data in 'adc_data'. It is defined in another file as an array of 4 elements. \$\endgroup\$ – ajeebx Apr 15 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.