commit dc6912769121c335c6b09a6c002a4a8e0528703a Author: Xavier ASUS Date: Mon Oct 28 15:53:52 2019 +0100 First commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..23268d8 --- /dev/null +++ b/Makefile @@ -0,0 +1,67 @@ +# Toolchain definitions +CC = sdcc +LD = stm8-ld +AS = stm8-as + +MKDIR = mkdir +CP = cp + +DEFINE = -DSTM8S003 + +# Include settings +INCLUDE = $(addprefix -I, inc/) + +# Assembler flags +AS_FLAGS = + +# Compiler flags +CC_FLAGS = -mstm8 --out-fmt-elf -c --debug --opt-code-size --gas --function-sections --data-sections $(INCLUDE) + +# Path definitions +PROJECT = STM8 + +# Objects definition +# Compiled objects list +OBJ_DIR = obj +VPATH += src +OBJECTS = $(addprefix $(OBJ_DIR)/, main.o bar.o) + +# Linker flags +LD_FLAGS = -T./elf32stm8.x --print-memory-usage --gc-sections -Map $(OBJ_DIR)/map_$(PROJECT).map +LIB_DIRS = $(addprefix -L, /usr/local/share/sdcc/lib/stm8 lib) +LIBS = $(apprefix -l, stm8 stm8s) + +# Source dependencies: +DEPS = $(OBJECTS:.o=.d) +ASM_DEPS = $(OBJECTS:.o=.asm) + +# ------------------------------------ +# Instructions +# ------------------------------------ + +$(OBJ_DIR)/$(PROJECT).elf: $(OBJECTS) + $(LD) $^ -o $@ $(LD_FLAGS) $(LIBS) + +#-include $(DEPS) + +clean: + rm -rf $(OBJ_DIR)/ + +# Uncomment for standard generation + +$(OBJ_DIR)/%.d: %.c + @$(MKDIR) -p $(OBJ_DIR) + $(CC) $< $(DEFINE) $(CC_FLAGS) -MM > $@ + +$(OBJ_DIR)/%.o: %.c $(OBJ_DIR)/%.d + @$(MKDIR) -p $(OBJ_DIR) + $(CC) $< $(DEFINE) $(CC_FLAGS) -o $@ + +$(OBJ_DIR)/%.o: %.asm + @$(MKDIR) -p $(OBJ_DIR) + $(AS) $< $(AS_FLAGS) -o $@ + +# ---------------------------------------- +# Phony targets +# ---------------------------------------- +.PHONY: clean debug diff --git a/elf32stm8.x b/elf32stm8.x new file mode 100644 index 0000000..d57afbf --- /dev/null +++ b/elf32stm8.x @@ -0,0 +1,270 @@ +/* Default linker script, for normal executables */ +/* Copyright (C) 2014-2016 Free Software Foundation, Inc. + Copying and distribution of this script, with or without modification, + are permitted in any medium without royalty provided the copyright + notice and this notice are preserved. */ +ENTRY (_start) +OUTPUT_FORMAT("elf32-stm8","elf32-stm8","elf32-stm8") +OUTPUT_ARCH(stm8) +__INTERRUPTS_REGION_LENGTH__ = DEFINED(__INTERRUPTS_REGION_LENGTH__) ? __INTERRUPTS_REGION_LENGTH__ : 0x80; +__FLASH_REGION_LENGTH__ = DEFINED(__FLASH_REGION_LENGTH__) ? __FLASH_REGION_LENGTH__ : 8K; +__STACK_REGION_LENGTH__ = DEFINED(__STACK_REGION_LENGTH__) ? __STACK_REGION_LENGTH__ : 128; +__SRAM_REGION_LENGTH__ = DEFINED(__SRAM_REGION_LENGTH__) ? __SRAM_REGION_LENGTH__ : 1K; +__EEPROM_REGION_LENGTH__ = DEFINED(__EEPROM_REGION_LENGTH__) ? __EEPROM_REGION_LENGTH__ : 128; +__FUSE_REGION_LENGTH__ = DEFINED(__FUSE_REGION_LENGTH__) ? __FUSE_REGION_LENGTH__ : 11; +__LOCK_REGION_LENGTH__ = DEFINED(__LOCK_REGION_LENGTH__) ? __LOCK_REGION_LENGTH__ : 1K; +__SIGNATURE_REGION_LENGTH__ = DEFINED(__SIGNATURE_REGION_LENGTH__) ? __SIGNATURE_REGION_LENGTH__ : 1K; +__USER_SIGNATURE_REGION_LENGTH__ = DEFINED(__USER_SIGNATURE_REGION_LENGTH__) ? __USER_SIGNATURE_REGION_LENGTH__ : 1K; +MEMORY +{ + interrupts (rx) : ORIGIN = 0x8000 , LENGTH = __INTERRUPTS_REGION_LENGTH__ + RAM (rw!x) : ORIGIN = 0x2, LENGTH = __SRAM_REGION_LENGTH__ - ORIGIN(RAM) - __STACK_REGION_LENGTH__ /* Set origin to 0x2 so NULL pointers point to 0. */ + STACK (rw) : ORIGIN = 0x3FF - __STACK_REGION_LENGTH__, LENGTH = __STACK_REGION_LENGTH__ /* Set origin to 0x2 so NULL pointers point to 0. */ + eeprom (rw!x) : ORIGIN = 0x4000, LENGTH = __EEPROM_REGION_LENGTH__ + fuse (rx) : ORIGIN = 0x4800, LENGTH = __FUSE_REGION_LENGTH__ + lock (rw!x) : ORIGIN = 0x5100, LENGTH = __LOCK_REGION_LENGTH__ + ROM (rx) : ORIGIN = 0x8000 + __INTERRUPTS_REGION_LENGTH__, LENGTH = __FLASH_REGION_LENGTH__ + signature (rw!x) : ORIGIN = 0x840000, LENGTH = __SIGNATURE_REGION_LENGTH__ + user_signatures (rw!x) : ORIGIN = 0x850000, LENGTH = __USER_SIGNATURE_REGION_LENGTH__ +} +SECTIONS +{ + /* Read-only sections, merged into text segment: */ + .hash : { *(.hash) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .gnu.version : { *(.gnu.version) } + .gnu.version_d : { *(.gnu.version_d) } + .gnu.version_r : { *(.gnu.version_r) } + .rel.init : { *(.rel.init) } + .rela.init : { *(.rela.init) } + .rel.text : + { + *(.rel.text) + *(.rel.text.*) + *(.rel.gnu.linkonce.t*) + } + .rela.text : + { + *(.rela.text) + *(.rela.text.*) + *(.rela.gnu.linkonce.t*) + } + .rel.fini : { *(.rel.fini) } + .rela.fini : { *(.rela.fini) } + .rel.rodata : + { + *(.rel.rodata) + *(.rel.rodata.*) + *(.rel.gnu.linkonce.r*) + } + .rela.rodata : + { + *(.rela.rodata) + *(.rela.rodata.*) + *(.rela.gnu.linkonce.r*) + } + .rel.data : + { + *(.rel.data) + *(.rel.data.*) + *(.rel.gnu.linkonce.d*) + } + .rela.data : + { + *(.rela.data) + *(.rela.data.*) + *(.rela.gnu.linkonce.d*) + } + .rel.ctors : { *(.rel.ctors) } + .rela.ctors : { *(.rela.ctors) } + .rel.dtors : { *(.rel.dtors) } + .rela.dtors : { *(.rela.dtors) } + .rel.got : { *(.rel.got) } + .rela.got : { *(.rela.got) } + .rel.bss : { *(.rel.bss) } + .rela.bss : { *(.rela.bss) } + .rel.plt : { *(.rel.plt) } + .rela.plt : { *(.rela.plt) } + /* Internal text space or external memory. */ + + .vectors : + { + *(.vectors) + KEEP(*(.vectors)) + } > interrupts + + .text : + { + /* For data that needs to reside in the lower 64k of progmem. */ + *(.progmem.gcc*) + /* PR 13812: Placing the trampolines here gives a better chance + that they will be in range of the code that uses them. */ + . = ALIGN(2); + __trampolines_start = . ; + /* The jump trampolines for the 16-bit limited relocs will reside here. */ + *(.trampolines) + *(.trampolines*) + __trampolines_end = . ; + /* avr-libc expects these data to reside in lower 64K. */ + *libprintf_flt.a:*(.progmem.data) + *libc.a:*(.progmem.data) + *(.progmem*) + . = ALIGN(2); + /* For future tablejump instruction arrays for 3 byte pc devices. + We don't relax jump/call instructions within these sections. */ + *(.jumptables) + *(.jumptables*) + /* For code that needs to reside in the lower 128k progmem. */ + *(.lowtext) + *(.lowtext*) + __ctors_start = . ; + *(.ctors) + __ctors_end = . ; + __dtors_start = . ; + *(.dtors) + __dtors_end = . ; + KEEP(SORT(*)(.ctors)) + KEEP(SORT(*)(.dtors)) + /* From this point on, we don't bother about wether the insns are + below or above the 16 bits boundary. */ + *(.init0) /* Start here after reset. */ + KEEP (*(.init0)) + *(.init1) + KEEP (*(.init1)) + *(.init2) /* Clear __zero_reg__, set up stack pointer. */ + KEEP (*(.init2)) + *(.init3) + KEEP (*(.init3)) + *(.init4) /* Initialize data and BSS. */ + KEEP (*(.init4)) + *(.init5) + KEEP (*(.init5)) + *(.init6) /* C++ constructors. */ + KEEP (*(.init6)) + *(.init7) + KEEP (*(.init7)) + *(.init8) + KEEP (*(.init8)) + *(.init9) /* Call main(). */ + KEEP (*(.init9)) + *(.text) + . = ALIGN(2); + *(.text.*) + . = ALIGN(2); + *(.fini9) /* _exit() starts here. */ + KEEP (*(.fini9)) + *(.fini8) + KEEP (*(.fini8)) + *(.fini7) + KEEP (*(.fini7)) + *(.fini6) /* C++ destructors. */ + KEEP (*(.fini6)) + *(.fini5) + KEEP (*(.fini5)) + *(.fini4) + KEEP (*(.fini4)) + *(.fini3) + KEEP (*(.fini3)) + *(.fini2) + KEEP (*(.fini2)) + *(.fini1) + KEEP (*(.fini1)) + *(.fini0) /* Infinite loop after program termination. */ + KEEP (*(.fini0)) + _etext = . ; + } > ROM + .data : + { + PROVIDE (__data_start = .) ; + *(.data) + *(.data*) + *(.rodata) /* We need to include .rodata here if gcc is used */ + *(.rodata*) /* with -fdata-sections. */ + *(.gnu.linkonce.d*) + . = ALIGN(2); + _edata = . ; + PROVIDE (__data_end = .) ; + } > RAM AT> ROM + .bss ADDR(.data) + SIZEOF (.data) : AT (ADDR (.bss)) + { + PROVIDE (__bss_start = .) ; + *(.bss) + *(.bss*) + *(COMMON) + PROVIDE (__bss_end = .) ; + } > RAM + __data_load_start = LOADADDR(.data); + __data_load_end = __data_load_start + SIZEOF(.data); + /* Global data not cleared after reset. */ + .noinit ADDR(.bss) + SIZEOF (.bss) : AT (ADDR (.noinit)) + { + PROVIDE (__noinit_start = .) ; + *(.noinit*) + PROVIDE (__noinit_end = .) ; + _end = . ; + PROVIDE (__heap_start = .) ; + PROVIDE (___stack_end = ABSOLUTE(ORIGIN(STACK))); + } > RAM + .eeprom : + { + /* See .data above... */ + KEEP(*(.eeprom*)) + __eeprom_end = . ; + } > eeprom + .fuse : + { + KEEP(*(.fuse)) + KEEP(*(.lfuse)) + KEEP(*(.hfuse)) + KEEP(*(.efuse)) + } > fuse + .lock : + { + KEEP(*(.lock*)) + } > lock + .signature : + { + KEEP(*(.signature*)) + } > signature + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + .note.gnu.build-id : { *(.note.gnu.build-id) } + /* DWARF debug sections. + Symbols in the DWARF debugging sections are relative to the beginning + of the section so we begin them at 0. */ + /* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + /* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + /* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + /* DWARF 2 */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end ) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } + /* SGI/MIPS DWARF 2 extensions */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } + /* DWARF 3 */ + .debug_pubtypes 0 : { *(.debug_pubtypes) } + .debug_ranges 0 : { *(.debug_ranges) } + /* DWARF Extension. */ + .debug_macro 0 : { *(.debug_macro) } + .debug_addr 0 : { *(.debug_addr) } +} diff --git a/inc/bar.h b/inc/bar.h new file mode 100644 index 0000000..736b10f --- /dev/null +++ b/inc/bar.h @@ -0,0 +1,6 @@ +#ifndef BAR_H +#define BAR_H + +void bar(int x); + +#endif /* BAR_H */ diff --git a/src/bar.c b/src/bar.c new file mode 100644 index 0000000..7282ea2 --- /dev/null +++ b/src/bar.c @@ -0,0 +1,15 @@ +#include "bar.h" + +void bar(const int x) +{ + int i; + + /* Some stupid calculations here and there. */ + for (i = 0; i < 90; i++) + { + if (i - x) + { + for (;;); + } + } +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..03e5944 --- /dev/null +++ b/src/main.c @@ -0,0 +1,30 @@ +#include "bar.h" + +struct example +{ + int a; + char b; + int *c; +}; + +static void foo(struct example *const st) +{ + if (st) + { + if (st->c) + { + (*st->c)++; + } + } +} + +_Noreturn void main(void) +{ + static struct example st; + + foo(&st); + + bar(st.a); + + for (;;); +}