首页 > 解决方案 > Baremetal Nucleo-F446RE 打开 LED

问题描述

我正在尝试打开 Nucleo-F446RE 上的内部 LED,但 LED 始终处于关闭状态。

计划是将 GPIOA 的时钟设置为 on,然后将 GPIOA_5 设置为输出,然后将其设置为高电平。这是文件(C 文件、链接脚本、用于编译和闪烁的 Bashscript)。

Blink.c:

// Create references to symbols defined in the linker script 
extern unsigned int _data_start;    
extern unsigned int _data_end;
extern unsigned int _data_load;
extern unsigned int _bss_start;
extern unsigned int _bss_end;

void startup();         // Function prototype (forward declaration) for startup function
int main();         // Function prototype for main function

// Below we create an array of pointers which would form our vector table
// We use __attribute__ ((section(".vectors"))) to tell the compiler that we want the
// array to be placed in a memory section that we call ".vectors"
unsigned int * vectors[2] __attribute__ ((section(".vectors"))) = 
{
    (unsigned int *)    0x20020000,     // Address of top of stack. 20kB = 1024 x 20 = 20480 bytes = 0x5000 
    (unsigned int *)    startup         // Address of the reset handler which is also our startup function
};

// The startup function, address was provided in the vector table   
void startup()
{
    volatile unsigned int *src, *dest;

    // Copy data section values from load time memory address (LMA) to their address in SRAM 
    for (src = &_data_load, dest = &_data_start; dest < &_data_end; src++, dest++) 
        *dest = *src;
    
    // Initialize all uninitialized variables (bss section) to 0
    for (dest = &_bss_start; dest < &_bss_end; dest++)
        *dest = 0;

    // Calling the main function
    main();
    
    while(1);   // Normally main() should never return, but just incase we loop infinitely
}

// LED2 on PA5

#define GPIOA 0x40020000
#define RCC   0x40023800

#define GPIOA_MODER *((volatile char*) GPIOA + 0x0)
#define GPIOA_BSRR  *((volatile char*) GPIOA + 0x18)
#define RCC_AHB1ENR *((volatile char*) RCC + 0x30)


int main(){

    RCC_AHB1ENR |= (1 << 0);

    for(int i = 0; i < 10; i++){ // wait for a few cycles
        asm("nop");
    }

    GPIOA_MODER |= (1 << 10); // set PA5 to output
    GPIOA_MODER &= ~(1 << 11);

    GPIOA_BSRR = (1 << 5); // set pin high

    while(1){}
}

linker.ld:

    MEMORY
    {
        rom (rx) : ORIGIN = 0x08000000, LENGTH = 512K
        ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
    }
    
    SECTIONS
    {
        .text :                 /* Define output file TEXT section */
        {
            *(.vectors)             /* Vector table */
            *(.text)                /* Program code */
            . = ALIGN(4);       /* Make sure data that follows are aligned to 4 byte boundary */
            *(.rodata)      /* Read only, section set asside for constants */
        } >rom
        
        
        .data :             /* Define output file DATA section */
        {
            _data_start = .;    /* Get the memory address (VMA) for start of section .data */
            *(.data)        /* Initialized static and global variable values */
            . = ALIGN(4); 
            _data_end = .;      /* Get the memory address (VMA) for end of section .data */
        } >ram AT >rom          /* After AT we specify the load-time location */
        
        _data_load = LOADADDR(.data);   /* Get the load memory address (LMA) for section .data */
        
        
        .bss :              /* Define output file BSS section */
        {
            _bss_start = .;     /* Get memory address of start of bss section */
            *(.bss)         /* Uninitialized static and global variables */
            *(COMMON)       /* Uninitialized variables are placed in COMMON section for object files */
            . = ALIGN(4);   
            _bss_end = .;       /* Get memory address of end of bss section */
        } >ram
}

compile_flash.sh:

arm-none-eabi-gcc -O0 -Wall -c -g -mcpu=cortex-m4 -mthumb blink.c -o blink.o
arm-none-eabi-ld -o blink.elf -T linker.ld blink.o
arm-none-eabi-objcopy blink.elf blink.bin -O binary

arm-none-eabi-nm --numeric-sort blink.elf

st-flash --reset write blink.bin 0x08000000

很抱歉代码很长,但我认为当你想帮助我时应该没有什么隐藏的:)

标签: cembeddedstm32nucleo

解决方案


问题是代码设置了第 10 位和第 11 位,但数据类型是char*.

将数据类型更改为void*有效。


推荐阅读