cancel
Showing results for 
Search instead for 
Did you mean: 

Fail when flashing C program on STM32 MCU

charley lanneluc
Associate
Posted on October 28, 2017 at 17:37

Hi ladies, guys, 

I am currently working on a project, so I am getting used to work with SMT32 MCUs and more particularly with STM32F411RE. I have built a C program just to blink the led user, in order to reduce flash and RAM as much as possible than the program bed gives us, with a lot of functions, libraries, that we don't use at all. 

However, when I flash it on my board, that does not work but the firmware given by bed works so my code is wrong. But I am working on it for many hours and I need some advice and another point of view. Could you tell me what is wrong  ? 

So here it is : 

&sharpinclude 'stm32f4xx.h'

int main() {

/* HSI -> SYSCLK */

RCC->CR |= (1u << 0) ; /* bit 0 at 1 to select HSI */

RCC->CFGR = RCC->CFGR & 0xFFFFFFFC ; /* HSI == SYSCLK */

RCC->PLLCFGR = RCC->PLLCFGR & 0xFFBFFFFF ; /*  bit 22 a 0 (2MHz mini) */

RCC->PLLCFGR = RCC->PLLCFGR | 0x00000F0A ; /* 96 MHz (16*60/10) */

RCC->PLLCFGR = RCC->PLLCFGR | 0x02000000 ; /* 48 MHz (96/2) */

RCC->CR = RCC->CR & 0xFFFCFFFF ; /*  48 MHz */

RCC->CR = RCC->CR | 0x02000000 ; /* 48 MHz */

/* PUSH-PULL OUTPUT for GPIOA and PA5 for LED user */

RCC->AHB1ENR |= (1u << 0) ; /* peripheral clock enabled (bit 0 GPIOEN) */

GPIOA->MODER |= (1u << 10) ; /*  byte 10 set to 1  */

GPIOA->OTYPER &= ~(1u << 5) ; /* byte 5 put to 0 for output push pull */

/* Configuration of the timer */

RCC->APB1ENR |= (1u << 0) ; /* Enable clock TIM2 (BIT 0 register APB1ENR) */

TIM2->ARR = 6855 ;

/*Autoreaload

*/

TIM2->PSC = 10000 ;

/* Prescaler

 */

TIM2->DIER |= (1u << 0) ; ; /* Enable interrupt for tim2 */

/* Autorisation interruption */

NVIC->ISER[0] |= (1u << 28); /* Enable interrupt 28 ofr TIM2 */

NVIC->IP[28] = NVIC->IP[28] | (7 << 4); /* Priorit� (7) */

SYSCFG->EXTICR[2] &= 0xFFFFFF0F ;

EXTI->IMR |= ( 1u << 5 ) ; /* Masque interruption desactivee EXTI_IMR register */

/* Lancement du timer */

TIM2->CR1 |= (1u << 0) ; /* Counter is on */ 

while(1)

{

}

return 0 ;

}

void TIM2_IRQHandler(void){

/* LED on or off */

TIM2->SR &= ~( 1u << 0 ) ; /*  UIF puts at 0 */

GPIOA->ODR = GPIOA->ODR ^ (1u << 5);

}

Thanks a lot ! 

Best !

#incomplete-code
3 REPLIES 3
Posted on October 28, 2017 at 17:59

Try using a debugger and breaking the problem down.

For example test the XORing of PA5 in the loop, and step through check to see the line go high/low.

Review the TIM/RCC settings the debugger's peripheral view.

The RCC code seem to ignore selection and waiting phases for PLL and clock sources.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 28, 2017 at 18:22

Thank you very much Clive for your fast response. 

Indeed, I tried it, to turn it on and off again and again in my loop, but that does not work as well... 

I also wanted to run my program step by step, watching at the registers of my MCU but I got a MAC OS X, and I do not know which software I must install to do it. Step by step will permit me to detect my errors, and correct them.

Do you know how I could do that ?

Thanks ! 

Posted on October 28, 2017 at 21:00

I'm a Windows + Keil guy, so don't have much of an opinion on OSX based tools. I believe Rowley has an OSX offering, and I liked their PC tools.

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