AnsweredAssumed Answered

Fail when flashing C program on STM32 MCU

Question asked by charley lanneluc on Oct 28, 2017
Latest reply on Oct 28, 2017 by Clive One

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 : 

 

#include "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 !

Outcomes