Author Topic: STM32F302 ADC Problem  (Read 1176 times)

0 Members and 1 Guest are viewing this topic.

Offline Glenn0010Topic starter

  • Regular Contributor
  • *
  • Posts: 225
  • Country: mt
STM32F302 ADC Problem
« on: November 22, 2019, 10:32:20 pm »
So in the manual it states the following:



When I run the code, it get's stuck somewhere and I can't find exactly why even while de-bugging. However I narrowed it down to the following line of code:


  while ((ADC1->ISR & ADC_ISR_ADRDY) != ADC_ISR_ADRDY){}; // wait for ADRDY
When I comment this line the code works flawlessly, however I cannot understand why, to me this line looks good. The full ADC code is listed below.
Anyone has any idea what I am doing wrong?


Code: [Select]


ADC1_COMMON->CCR = 0x0; // Resetting CKMODE
RCC->CFGR2 = RCC_CFGR2_ADC1PRES_DIV12; // ADC Prescaler /1 i.e. ADC Clock = 64Mhz
RCC->AHBENR |= RCC_AHBENR_ADC1EN; // Enable ADC1 clock
  ADC1_COMMON->CCR = 0x0; // Resetting CKMODE
GPIOB->MODER |= 0x0000000F; // GPIOB PB1 set to Analog Mode ADC1_IN12, ADC1_IN11

  // Calibration procedure
  ADC1->CR |= ADC_CR_ADVREGEN_1;
  ADC1->CR &= ~ADC_CR_ADVREGEN;
  ADC1->CR |= ADC_CR_ADVREGEN_0; // ADC Voltage regulator enabled
  for(x=0;x<5000;x++); // Delay for ADC to stabilize

  ADC1->CR &= ~ADC_CR_ADCALDIF; // calibration in Single-ended inputs Mode.
  ADC1->CR |= ADC_CR_ADCAL; // Start ADC calibration
  // Read at 1 means that a calibration in progress.
  while ( (ADC1->CR & ADC_CR_ADCAL)== ADC_CR_ADCAL){}; // wait until calibration done
calibration_value = ADC1->CALFACT; // Get Calibration Value ADC1


ADC1->ISR |= ADC_ISR_ADRDY; //clearing ADCRDY
  ADC1->CR |= ADC_CR_ADEN; // Enable ADC1
  while ((ADC1->ISR & ADC_ISR_ADRDY) != ADC_ISR_ADRDY){}; // wait for ADRDY

  ADC1->SQR1 = 0xB301; // Channel 12 first, Channel 11 second, two in sequence
ADC1->SMPR2 = 0x0; // Channel 11 and 12 Minimum conversion time
ADC1->CFGR = ADC_CFGR_DMAEN | ADC_CFGR_DMACFG | ADC_CFGR_CONT; // ADC DMA Enableed and set to circular mode and ADC in continous mode

DMA1_Channel1->CPAR = (uint32_t)(&(ADC1->DR)); // Passing the pheripheral address of the data register into DMA Channel 1
  DMA1_Channel1->CMAR = (uint32_t)ADC_Samples; // Passing the address of the arry to store the data in
DMA1_Channel1->CNDTR = 2; // " sets of data being passed
DMA1_Channel1->CCR |= DMA_CCR_CIRC | DMA_CCR_MINC | DMA_CCR_PSIZE_0 | DMA_CCR_MSIZE_0; // Channel 1 Circular Mode, Memory Increment , P size = 16bit, M size = 16 bit
DMA1_Channel1->CCR |= DMA_CCR_EN; // Enabling DMA

  ADC1->CR |= ADC_CR_ADSTART; // Start ADC1 Software Conversion
« Last Edit: November 22, 2019, 10:35:19 pm by Glenn0010 »
 

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 7521
  • Country: ca
  • Non-expert
Re: STM32F302 ADC Problem
« Reply #1 on: November 22, 2019, 11:17:52 pm »
Are you using SWD debugging? You say "it gets stuck somewhere" but if you have that line in and you pause operation, is it stuck waiting on that line or is it somewhere else?

Can you view the ADC1->ISR register in your debugger, to see if the state of the ADRDY bit is as you expect?

Code: [Select]
ADC1_COMMON->CCR = 0x0; // Resetting CKMODE

This doesn't look right as you are writing all bits of the CCR register as 0, this is *probably* ok looking at the datasheet but may or may not be what you intended?
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 

Offline Glenn0010Topic starter

  • Regular Contributor
  • *
  • Posts: 225
  • Country: mt
Re: STM32F302 ADC Problem
« Reply #2 on: November 23, 2019, 09:18:12 am »
Looks like I found the issue.

From the manual
Quote
ADEN bit cannot be set during ADCAL=1 and 4 ADC clock cycle after the ADCAL bit is
cleared by hardware(end of the calibration).

After ADC cal was = 0. I went straight to enable the ADC and wasn't waiting for the 4 clock cycles. So now I added a for loop to delay and it seems to be working fine. I guess that explains the intermittent errors I was getting.

With regards to "getting stuck somewhere", it was getting stuck in the start up file
 
The following users thanked this post: thm_w


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf