stm32 裸机点亮led

时间:2024-01-26 16:49:10

stm32不用库 裸机点亮led
startup.s 定义栈入口函数 进入main

.syntax unified
.cpu cortex-m3
.fpu softvfp
.thumb

.global vtable
.global reset_handler

.type vtable, %object
vtable:
    .word _estack
    .word reset_handler
.size vtable, .-vtable

.section .data
.word _sidata
.word _sdata
.word _edata
.word _sbss
.word _ebss

.type reset_handler, %function
reset_handler:
    ldr r0,=_sdata
    ldr r1,=_edata
    ldr r2,=_sidata
    bl flash2ram

    ldr r0,=_sbss
    ldr r1,=_ebss
    bl zerobss

  	bl main
  	loop:
    	b   loop  //死循环
.size reset_handler, .-reset_handler

main.c

void flash2ram(register unsigned int *dst, register unsigned int* dst_end, register unsigned int* src)
{
    while(dst < dst_end)
        *dst++ = *src++;
}

void zerobss(register unsigned int* start, register unsigned int* end)
{
    while(start < end)
        *start++ = 0;
}

void delay(unsigned int n)
{
    while(n--)
        ;
}

int main()
{
	//RCC_APB2ENR  IOPCEN
    *((unsigned int*)(0x40021000+0x18)) |= 0x10;   //gpioc clk
	
    //gpioc CRL General purpose output push-pull  Output mode, max speed 50 MHz.
    *((unsigned int *)(0x40011000 +0x00)) &= 0xFFFFF0FF; 
    *((unsigned int*)(0x40011000 +0x00))|= 0x00000300;
    while(1) {
    	//GPIOC_BSRR  set 
        *((unsigned int *)(0x40011000+0x10)) |= 1<<2;
        delay(1e6);
        //GPIOC_BRR  clear 
        *((unsigned int *)(0x40011000+0x14)) |= 1<<2;
        delay(1e6);
    }
}

sim.ld

_estack = 0x20005000;

MEMORY
{
    FLASH ( rx )      : ORIGIN = 0x08000000, LENGTH = 256K
    RAM ( rxw )       : ORIGIN = 0x20000000, LENGTH = 48K
}

SECTIONS
{
    .text :
    {
        . = ALIGN(4);
        *(.text)
        *(.text*)
        *(.rodata)
        . = ALIGN(4);
    } >FLASH

    _sidata = . ;
    .data : AT ( _sidata )
    {
        . = ALIGN(4);
        _sdata = .;
        *(.data)
        *(.data*)
        . = ALIGN(4);
        _edata = .;
    } >RAM

    .bss :
    {
        . = ALIGN(4);
        _sbss = .;
        *(.bss)
        *(.bss*)
        *(COMMON)
        . = ALIGN(4);
        _ebss = .;
    } >RAM
}

编译

arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb  --specs=nosys.specs -nostdlib -T sim.ld main.c startup.s -o led.elf

转binary

arm-none-eabi-objcopy -O binary led.elf led.bin

烧录

st-flash write led.bin 0x8000000