roshbaby / stm8s-sdcc

STM8S Standard Peripheral Library Ported to SDCC for multiple STM8 targets
69 stars 26 forks source link

[FR] needs build instructions / examples #6

Open ildar opened 5 years ago

ildar commented 5 years ago

Just for a developer to start with. Or maybe links to a HOWTO would be good...

jackkum commented 4 years ago

@ildar I've made a Makefile

CC      = gcc -Wall -O2 -I./inc
SDCC    = sdcc -mstm8 -DSTM8103 -DSTM8S103 -I./inc
SDAR    = sdar
NAME    = lib_sdcc
SOURCES = $(wildcard src/*.c)
OBJS    = $(SOURCES:.c=.rel)

.SUFFIXES : .rel .c

all: $(NAME).lib

$(NAME).lib: $(OBJS)
    $(SDAR) -rc $@ $(OBJS)
.c.rel:
    $(SDCC) -c $< -o $@
clean:
    rm -f ${OBJS:.rel=.map} ${OBJS:.rel=.rst} ${OBJS:.rel=.cdb} ${OBJS:.rel=.asm} ${OBJS:.rel=.lst} ${OBJS:.rel=.sym} $(OBJS) $(NAME).lib

Build lib_sdcc.lib file

make

Copy files stm8s_it.c and stm8s_it.h to the project and use it in Makefile of project like that:

## Select one of these
DEVICE=stm8s103f3

## A directory for common include files
COMMONDIR = ..
SDCC_LIBS = ../stm8s-sdcc

PROGRAM = i2c_slave

SOURCES=$(wildcard *.c $(COMMONDIR)/*.c)
OBJECTS=$(SOURCES:.c=.rel)
HEADERS=$(wildcard *.h $(COMMONDIR)/*.h $(SDCC_LIBS)/inc/*.h)

CC = sdcc
PROGRAMMER = stlinkv2

DEFINES=
## Set MCU-type DEFINE
ifeq ($(DEVICE),stm8s003f3)
    DEFINES += -DSTM8S003
    # stm8_libs define
    DEFINES += -DSTM8003
endif
ifeq ($(DEVICE),stm8s103f3)
    DEFINES += -DSTM8S103
    # stm8_libs define
    DEFINES += -DSTM8103
endif

CPPFLAGS = -I$(COMMONDIR)
CFLAGS = --Werror --std-sdcc99 -mstm8 $(DEFINES)
LDFLAGS = -lstm8 -mstm8 --out-fmt-ihx 
LDLIBS = -l$(SDCC_LIBS)/lib_sdcc.lib

.PHONY: all clean

$(PROGRAM).ihx: $(OBJECTS)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

%.rel : %.c $(HEADERS)
    $(CC) -I$(SDCC_LIBS) $(LDLIBS) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

CCOMPILEDFILES=$(SOURCES:.c=.asm) $(SOURCES:.c=.lst) $(SOURCES:.c=.rel) \
               $(SOURCES:.c=.rst) $(SOURCES:.c=.sym)
clean:
    rm -f $(PROGRAM).ihx \
          $(PROGRAM).cdb \
          $(PROGRAM).lk \
          $(PROGRAM).map \
          $(CCOMPILEDFILES)

flash: $(PROGRAM).ihx
    stm8flash -c $(PROGRAMMER) -p $(DEVICE) -w $(PROGRAM).ihx
ildar commented 4 years ago

looks great, thanks a lot! Worth a PR.