cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 ADC without STD-Lib

markusp
Associate II
Posted on August 28, 2013 at 09:34

Hi,

I tried to program an ADC without using STD-Lib. But I have some problems with my initialization, I think. The compiler has no problems with my program, so there are no syntax errors, but the program get stuck at that line: while((ADC3->ISR & ADC_ISR_EOC)==0);// wait until conversion is complete I think I have a problem with my initialization, but I don't it exactly. I hope somebody can help me 😉 best regards Markus

GPIO_CLKconfig();
clock_config();
//-----ADC_GPIO-----------------
GPIOB->MODER |= GPIO_MODER_MODER1_1;
GPIOB->MODER |= GPIO_MODER_MODER1_0; // Analoge MODE
/* Analog mode 11
* Alternate 10
* GPIO 01
* Input 00
*/
GPIOB -> OSPEEDR |= GPIO_OSPEEDER_OSPEEDR1_1| GPIO_OSPEEDER_OSPEEDR1_0;// 50 MHZ GPIO
//--------------------------------
//----LED_GPIO---------------------
GPIOE->MODER |=GPIO_MODER_MODER8_0; // GPIO MODE
GPIOE->MODER &=~GPIO_MODER_MODER8_1;
/* Analog mode 11
* Alternate 10
* GPIO 01
* Input 00
*/
GPIOE -> OSPEEDR |= GPIO_OSPEEDER_OSPEEDR8_1| GPIO_OSPEEDER_OSPEEDR8_0;// 50 MHZ GPIO
GPIOE->MODER |=GPIO_MODER_MODER9_0;
GPIOE -> OSPEEDR |= GPIO_OSPEEDER_OSPEEDR9_1| GPIO_OSPEEDER_OSPEEDR9_0;
//----------------------------------
// ADC init
RCC -> AHBENR |= RCC_AHBENR_ADC34EN; //CLockenable for ADC 3 und 4
ADC3 -> CR &= ~ ADC_CR_ADEN; //disable AD
ADC3->IER |= ADC_IER_EOC | ADC_IER_RDY; //enable EOC-Interrupt and ADC-Ready-Interrupt
/*calibration*/
ADC3 -> CR &=~ADC_CR_ADCALDIF; //clear register
ADC3 -> CR |=(1<<
28
);// voltage regulator 01
ADC3 -> CR |= ADC_CR_ADCAL; //starting calibration
while((ADC3->CR & ADC_CR_ADCAL)==0); //wait until calibration is ready
/*end of calibration*/
ADC3 -> CR |= ADC_CR_ADEN; //enable AD
while((ADC3 -> ISR & ADC_ISR_ADRD)==1);//wait until ADC is ready
/*conversion and channel selection*/
ADC3-> SQR1 = 0;
ADC3-> SQR1 |=ADC_SQR1_SQ1_0; //channel 1
ADC3-> CFGR &= ~ADC_CFGR_RES;
ADC3-> CFGR |= ADC_CFGR_RES_0; // 10 bit resolution
/*
* 00 => 12 bit
* 01 => 10 bit
* 10 => 8 bit
* 11 => 6 bit
*/
while(1)
{
ADC3-> SMPR1 |= ADC_SMPR1_SMP1_2; //sampling time to 5 clock cycles
//SMPR1....for channel 1
ADC3-> CFGR &= ~ADC_CFGR_CONT; //clear register
ADC3-> CR |= ADC_CR_ADSTART; // ADC start
while((ADC3->ISR & ADC_ISR_EOC)==0);// wait until conversion is complete
GPIOE-> BSRR = GPIO_Pin_9; //set LED3 to high
value=ADC_DR_RDATA; //get value of the ADC
ADC3-> CR |= ADC_CR_ADSTP; // stop conversion
if(value>=512)
{
GPIOE-> BRR = GPIO_Pin_8; // low
}
else
{
GPIOE-> BSRR = GPIO_Pin_8; //high
}
}
return 0;
}
these are the two librarys which I wrote:
void GPIO_CLKconfig()
{
RCC -> AHBENR |= RCC_AHBENR_GPIOAEN | //enable clock for GPIOA
RCC_AHBENR_GPIOBEN | //enable clock for GPIOB
RCC_AHBENR_GPIOCEN | //enable clock for GPIOC
RCC_AHBENR_GPIODEN | //enable clock for GPIOD
RCC_AHBENR_GPIOEEN | //enable clock for GPIOE
RCC_AHBENR_GPIOFEN ; //enable clock for GPIOF
}
void GPIO_PinConfig(GPIO_TypeDef* port,uint16_t pin,uint8_t mode)
{
port -> MODER &= ~(1<<(pin*2));
port -> PUPDR &= ~(1<<(pin*2));
port -> OSPEEDR &= ~(1<<(pin*2));
port -> OTYPER &= ~(1<<
pin
);
port -> MODER |= (uint32_t)((mode&0x03)<<(pin*2));
port -> PUPDR |= (uint32_t)(((mode&0x03)>>2)<<(pin*2));
port -> OSPEEDR |= (uint32_t)(((mode&0x03)>>4)<<(pin*2));
port -> OTYPER |= (uint32_t)(((mode&0x03)>>6)<<
pin
);
}
The second library:
void clock_config(void)
{
/*
* ================================================================================
* Initializing of PLL for using a higher internal clock-speed (64MHz)
* ================================================================================
*/
/* Enable Prefetch Buffer and set Flash Latency */
/*These bits represent the ratio of the SYSCLK (system clock) period to the Flash
access time. */
/*Siehe RM0316 Seite 62 */
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY_1;
/*configure prescaler */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1 | //HCLK = SYSCLK
(uint32_t)RCC_CFGR_PPRE1_DIV2 | // prescaler APB1 = 2
(uint32_t)RCC_CFGR_PPRE2_DIV1; // prescaler APB2 =2
/* PLL configuration */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL));//clear register
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | //PLL gets HSI/2
RCC_CFGR_PLLMULL16); //PLL-factor is 16 => 64MHz
RCC->CR |= RCC_CR_PLLON; /* Enable PLL */
while((RCC->CR & RCC_CR_PLLRDY) == 0);/* Wait till PLL is ready */
/* Select PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));//clear register
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL);
}

#adc #stm32f3 #stdperiph
8 REPLIES 8
crt2
Associate II
Posted on August 28, 2013 at 10:18

Before we start reading the code of many custom defined macros (which can be defined right or wrong), I would like to ask you for the reason. You do know that you can change STD-lib to your needs, or whatever you'd like?

Imagine every guy here would have its own library - nobody cant know what you have defined somewhere and what you don't.
markusp
Associate II
Posted on August 28, 2013 at 11:53

thanks for your quick answer.

I don't want to use the std-Lib because I wanna know exactly what every codeline does. When I use the std-Lib I don't have that feeling.

And the two own libraries are not the problem, because I tested them earlier. My main question is the code of the ADC, so can somebody please check it? Maybe I forgot something to initialize.

regards

frankmeyer9
Associate II
Posted on August 28, 2013 at 12:49

Look at ST's ADC samples provided in the firmware package, they show you the required sequence of initialization. Compare it with your code.

I don't want to use the std-Lib because I wanna know exactly what every codeline does. When I use the std-Lib I don't have that feeling.

 

IMHO a rather nonprofessional stance.

Roll-your-own usually implies left-on-your-own.

markusp
Associate II
Posted on August 28, 2013 at 14:36

I tried it and it didn't help.

Is it really a professional way to use that library? Because I think it makes many things you dont't want. Also I'm working on a project, where I have to know what every codeline does and I do not need operations working in the background.

Has anybody else another idea what I can do?

sincerely 

 

frankmeyer9
Associate II
Posted on August 28, 2013 at 15:14

Is it really a professional way to use that library?

 

As professional developer, one has to work rather often with foreign code. Project managers rarely grant one the time to redesign/rewrite poorly written/documented code.

ST's lib is the attempt to provide a consistent interface to the peripherals of the Cortex M devices. You might appreciate it once you port code between different variants, or develop code for more than one variant.

For my projects, I tried it the other way. Starting from a FW_lib example provided by ST, I removed all unnecessary code, and 'inlined' other where it seemed appropriate.

And additionally, you can watch the related peripheral registers in the debugger (especially GPIO and ADC). The settings, and the sequence, should basically match those of ST's examples.

ran2
Associate III
Posted on August 28, 2013 at 15:29

Following this problem, could you please clarify what do you mean by''can change STD-lib to your needs, or whatever you'd like? ''

This is really an issue while developers are considering whether they should use the ST-lib or simply the CMSIS. Is there any official document saying what can I do with the ST-lib?

Thanks in advance.

Posted on August 28, 2013 at 16:27

Is there any official document saying what can I do with the ST-lib?

There's the release notes

License

The enclosed firmware and all the related documentation are not covered by a License Agreement, if you need such License you can contact your local STMicroelectronics office.

THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

Basically, here it is, you own it, and any problems/issues that arise from it's use are yours alone.

Disclaimer : I am not a lawyer, this is not legal advice, I do not work for, or represent ST.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on August 28, 2013 at 16:33

Also I'm working on a project, where I have to know what every codeline does

Exactly, and this requires a significantly high knowledge threshold on your part, rather than mine.

The library is designed to decimate the amount of time to develop working code, and permit it to be portable and easier to read by others.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..