Baremetal Nucleo-F446RE打开LED

问题描述

我正在尝试打开Nucleo-F446RE上的内部LED,但该LED总是熄灭。

计划是打开GPIOA的时钟,然后将GPIOA_5设置为输出,然后将其设置为高电平。这是文件(C文件,Linkerscript,用于编译和刷新的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

抱歉,代码太长了,但是我想当您想要帮助我时应该没什么隐藏的东西了:)

解决方法

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

将数据类型更改为void*即可。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...