简体   繁体   中英

Blinking Led in Bare-Metal C programming: STM32L476RG Nucleo Board

I tried to implement a classic blink example on an STM32L476RG Nucleo board.

According to the STM32L4x datasheet: the LD2 is connected to the GPIOA PORT 5 (PA5). The PA5 uses the AHB2 bus.

Note: I used Keil uVision 5; I created a New uVision Project with STM32L476RGTx target. In the "Manage Run-Time Environment" dialog box I selected:

  1. CMSIS >> Core (flag)
  2. Device >> Startup (flag)

Here the code:

#include "stm32l4xx.h"                  // Device header
//#include <stdint.h>

//#define MASK(x) ((uint32_t) (1<<(x))) // bitmasking 

void delayMs(int delay);

int main(void){
    // RCC->AHB2RSTR |=1; 
    // RCC->AHB2RSTR &=~1; 
    // RCC->AHB2ENR |= MASK(0); //bitwise OR. Enable GPIOA clock
     RCC->AHB2ENR |= 1;
     //GPIOA->MODER |= MASK(10);
    GPIOA->MODER |= 0x400; 

    while(1){


        //GPIOA->ODR |= MASK(4);
         GPIOA->ODR |= 0x20;
        delayMs(500);
        //GPIOA->ODR &= ~MASK(4);
        GPIOA->ODR &= ~0x20;
        delayMs(500);
    }
}

void delayMs(int delay){
int i;
    for(;delay>0; delay --){
        for (i=0; i<3195;i++);
    }
}

The Build output returns:

Build started: Project: blinknew
*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Build target 'Target 1'
compiling main.c...
linking...
Program Size: Code=520 RO-data=408 RW-data=0 ZI-data=1632  
".\Objects\blinknew.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed:  00:00:09

and when I download it, Keil uV 5 returns:

 Load "C:\\Users\\gmezz\\OneDrive\\Documenti\\Bare_Metal\\Blinknew\\Objects\\blinknew.axf" 
Erase Done.
Programming Done.
Verify OK.
Flash Load finished at 22:37:52

The LED should blink with a period of 1 s, but in reality, nothing happens.

Honestly, I don't understand what is going wrong.

Someone can help me?

GM

I may be wrong, but according to the reference manual (RM0351) section 6.2.19, you should wait 2 clock cycles after enabling the peripheral clock, before accessing its registers. Try introducing a short delay after RCC->AHB2ENR |= 1; line. In your case, I think MODER register is not getting the correct value.

I also suggest checking the actual values of registers with a debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM