Search code examples
cembeddedstm32nucleo

Baremetal Nucleo-F446RE Turn LED on


I am trying to turn on the Internal LED on my Nucleo-F446RE, but it the LED is always off.

The plan is to set clock for GPIOA on, then set GPIOA_5 to output and then set it to high. Here are the files (C file, Linkerscript, Bashscript for compilation and Flashing).

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

Sorry for the long code, but I think there should be nothing hidden when you want to help me :)


Solution

  • The problem is that the code is setting the 10th and 11th bit but the datatype is char*.

    Changing the Datatype to void* works.