2021-12-22

STM32F103C8 bare metal with linker scripts GCC

I am trying to write bare metal code in assembly and compile + link it using GCC toolchain. As I know the proper steps is to follow the following steps:

  1. After restart - MCU has to check vector table and execute reset handler, where I initialize stack pointer.
  2. Execute main code. In order to complete this task I also need to have respective linker script. When i am trying to execute linker it throws syntax error. Please advice:
  3. What has to be corrected in linker script
  4. Correct vtable and handler execution sequence.

Code:

stack_size      =       0x400
stack_start     =       0x20000000+stack_size
gpiob_base      =       0x40010C00
rcc_base        =       0x40021000
rcc_apb2enr     =       rcc_base+0x18
gpio_crl        =       gpiob_base
gpiob_odr       =       gpiob_base+0x0C

                .syntax unified
                .cpu cortex-m3
                .thumb
                .global main
                .global vtable
main:
                LDR R0, =rcc_apb2enr
                LDR R1, [R0]
                LDR R2, =0x8 // Activate 3rd bit in registry
                ORR R1, R2
                STR R1, [R0]

                // Configure GPIO_CRL
                LDR R0, =gpio_crl
                LDR R1, [R0]
                LDR R2, =0xFFFFFF00
                AND R1,R1,R2
                ORR R1, R1, #0x20
                STR R1, [R0] // Reset register

                //Configure GPIOB_ODR
                LDR R0, =gpiob_odr
                LDR R1, [R0]
                ORR R1, #0x2
                STR R1, [R0]
                B .
vtable:
        .word   stack_start
        .word   reset_handler

reset_handler:
                B main

Linker script:

/* - STM32F103C8T6 - Medium Density device
 * - RAM: 20K, Flash:64K CPU: 72MHz
*/
ENTRY(reset_handler);
MEMORY {
        RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
        FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K
}

SECTIONS
{
        /* Section that stores program instructions (code) */
        .text : {
                . = ALIGN(4);
                KEEP(*(.vtable))
                *(.text)
                *(.text*)
                *(.rodata)
                *(.rodata*)
                . = ALIGN(4);
        } > FLASH

        _data_flash = .;

        // Section that store initialized data - variables
        .data : AT(_data_flash){
                . = ALIGN(4);
                _data_begin = .;
                *(.data)
                *(.data*)
                . = ALIGN(4);
                _data_end = .;
        }  > RAM

        /* Section that stores uninitialized data */
        .bss :{
                _bss_begin = .;
                _bss_start_ = _bss_begin;
                *(.bss)
                *(.bss*)
                *(COMMON)
                . = ALIGN(4);
                _bss_end = .;
                _bss_end_ = _bss_end;
        } > RAM

        /* Here we define stack */
        _stack_size = 1024;
        _stack_end = ORIGIN(RAM)+LENGTH(RAM);
        _stack_begin = _stack_end - _stack_size;
        . = _stack_begin;
    ._stack :{
                . = . + _stack_size;
        } > RAM
}



._stack :{
                . = . + _stack_size;
        } > RAM
}

Disassembly of program:

pi@mylab:~/assembly $ arm-none-eabi-objdump --disassemble bp.o

bp.o:     file format elf32-littlearm


Disassembly of section .text:

00000000 <main>:
   0:   480d            ldr     r0, [pc, #52]   ; (38 <reset_handler+0x4>)
   2:   6801            ldr     r1, [r0, #0]
   4:   f04f 0208       mov.w   r2, #8
   8:   ea41 0102       orr.w   r1, r1, r2
   c:   6001            str     r1, [r0, #0]
   e:   480b            ldr     r0, [pc, #44]   ; (3c <reset_handler+0x8>)
  10:   6801            ldr     r1, [r0, #0]
  12:   f06f 02ff       mvn.w   r2, #255        ; 0xff
  16:   ea01 0102       and.w   r1, r1, r2
  1a:   f041 0120       orr.w   r1, r1, #32
  1e:   6001            str     r1, [r0, #0]
  20:   4807            ldr     r0, [pc, #28]   ; (40 <reset_handler+0xc>)
  22:   6801            ldr     r1, [r0, #0]
  24:   f041 0102       orr.w   r1, r1, #2
  28:   6001            str     r1, [r0, #0]
  2a:   e7fe            b.n     2a <main+0x2a>

0000002c <isr_vector>:
  2c:   20000400        .word   0x20000400
  30:   00000034        .word   0x00000034

00000034 <reset_handler>:
  34:   f7ff bffe       b.w     0 <main>
  38:   40021018        .word   0x40021018
  3c:   40010c00        .word   0x40010c00
  40:   40010c0c        .word   0x40010c0c


from Recent Questions - Stack Overflow https://ift.tt/3J6Xu5P
https://ift.tt/eA8V8J

No comments:

Post a Comment