.global _start
.global main
_start:
bl main
#include "inc/main.h"
#include "inc/fun.h"
int main()
{
int a = 0;
int b = 2;
fun(a,b);
return 1;
}
-
fun文件
#include "fun.h"
void fun(int a,int b)
{
int c= a + b;
}
-
Makefile
TARGET := demo.bin
BUILD := demo
ENV ?= RAM
COBJS += start.o
COBJS += main.o
COBJS += fun.o
CROSS_COMPILE := arm-none-eabi-
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld
OBJCOPY := $(CROSS_COMPILE)objcopy
CFLAGS += -Wall
CFLAGS += -I./inc
LDFLAGS += -T ld/mem.ld
LDFLAGS += -T ld/sections.ld
#ifeq ($(ENV),RAM)
#LDFLAGS += -Ttext=0x20000000
#else
#LDFLAGS += -Ttext=0x0
#endif
all:$(TARGET)
ifeq ($(ENV),RAM)
$(TARGET):$(BUILD)
$(OBJCOPY) -O binary $^ $@
else
$(TARGET):$(BUILD)
$(OBJCOPY) -O binary $^ $@
endif
$(BUILD):$(COBJS)
$(LD) $(LDFLAGS) -o $@ $^
%.o:%.c
$(CC) $(CFLAGS) -c -o $@ $^
%.o:%.S
$(CC) $(CFLAGS) -c -o $@ $^
clean:
rm -f $(TARGET) $(BUILD) *.o
-
链接脚本mem.ld
/*
* Memory Spaces Definitions.
*
* Need modifying for a specific board.
* FLASH.ORIGIN: starting address of flash
* FLASH.LENGTH: length of flash
* RAM.ORIGIN: starting address of RAM bank 0
* RAM.LENGTH: length of RAM bank 0
*
* The values below can be addressed in further linker scripts
* using functions like 'ORIGIN(RAM)' or 'LENGTH(RAM)'.
*/
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 8K
SRAM_DUMMY (xrw) : ORIGIN = 0x1FFE0000, LENGTH = 128K
DRAM_DUMMY (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
/* SRAM=SRAM_DUMMY+DRAM_DUMMY */
SRAM (xrw) : ORIGIN = 0x1FFE0000, LENGTH = 384K
DRAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0K
SDRAM (xrw) : ORIGIN = 0x30000000, LENGTH = 2M
/* SDRAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8M change the size of SDRAM from 2MB to 8MB*/
}
-
链接脚本sections.ld
/*
* Default linker script for Cortex-M (it includes specifics for STM32F[34]xx).
*
* To make use of the multi-region initialisations, define
* OS_INCLUDE_STARTUP_INIT_MULTIPLE_RAM_SECTIONS for the _startup.c file.
*/
__sram_start = ORIGIN(SRAM);
__sram_end = ORIGIN(SRAM) + LENGTH(SRAM);
__sdram_start = ORIGIN(SDRAM);
__sdram_end = ORIGIN(SDRAM) + LENGTH(SDRAM);
/*__l2_heap_size = __sdram_end - __l2_heap_start;*/
/*
* The '__stack' definition is required by crt0, do not remove it.
*/
__stack = ORIGIN(SRAM) + LENGTH(SRAM) - 16;
_estack = __stack; /* STM specific definition */
_Minimum_Stack_Size = 2048 ;
_Minimum_text_sec_Size = LENGTH(SRAM_DUMMY);
ASSERT(ORIGIN(SRAM_DUMMY) == ORIGIN(SRAM), "SRAM START ADDR ERROR")
ASSERT((LENGTH(SRAM_DUMMY)+LENGTH(DRAM_DUMMY)) == LENGTH(SRAM), "SRAM LENGTH ERROR");
ASSERT((ORIGIN(SRAM_DUMMY)+LENGTH(SRAM_DUMMY)) == ORIGIN(DRAM_DUMMY), "SRAM_DUMMY LENGTH ERROR")
__Main_Stack_Size = _Minimum_Stack_Size ;
/*
* Default heap definitions.
* The heap start immediately after the last statically allocated
* .sbss/.noinit section, and extends up to the main stack limit.
PROVIDE ( _Heap_Begin = _end_noinit ) ;
PROVIDE ( _Heap_Limit = __sram_end ) ;
ASSERT (_Heap_Begin < _Heap_Limit, "no heap memory available");
*/
/* support for cm_backtrace*/
_sstack = ORIGIN(DRAM); /*stack start address, we use static stack in bss section.*/
_estack = __stack; /*stack end address, NOTICE: _estack was defined up*/
_stext = __sram_start; /*text start address*/
/*
* The entry point is informative, for debuggers and simulators,
* since the Cortex-M vector points to it anyway.
*/
ENTRY(_start)
/* Sections Definitions */
SECTIONS
{
.l1_text : ALIGN(4)
{
FILL(0xFF)
start.o
main.o
} >SRAM
.rodata : ALIGN(4)
{
*(.rodata)
} >SRAM
.data : ALIGN(4)
{
*(.data)
} >SRAM
.bss : ALIGN(4)
{
*(.bss)
} >SRAM
.l2_text : ALIGN(4)
{
fun.o
} >SDRAM AT>SRAM
}