From 37b8a8b8ed084e63885e57f2f53e1e93acb35b33 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 15 Jun 2011 23:31:02 +0200 Subject: [PATCH] Add loadable/ - build infrastructure for small ram executables. Also reserve 1k ram for these in the main firmware (Makefile.inc variable RAMCODE) --- Makefile | 48 ++++++++++++++++++++++++--------------- Makefile.inc | 6 +++-- loadable/.gitignore | 3 +++ loadable/Makefile | 12 ++++++++++ loadable/Makefile.sub | 47 ++++++++++++++++++++++++++++++++++++++ loadable/bin2h.pl | 41 ++++++++++++++++++++++++++++++++++ loadable/blinktest.c | 10 +++++++++ loadable/loadable.ld | 52 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 199 insertions(+), 20 deletions(-) create mode 100644 loadable/.gitignore create mode 100644 loadable/Makefile create mode 100644 loadable/Makefile.sub create mode 100755 loadable/bin2h.pl create mode 100644 loadable/blinktest.c create mode 100644 loadable/loadable.ld diff --git a/Makefile b/Makefile index 7bbacb5..c265fdd 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ INCLUDE_PATHS = -I$(ROOT_PATH) -I$(ROOT_PATH)/core include $(ROOT_PATH)/Makefile.inc +LDFLAGS+= -Wl,--gc-sections VPATH += lpc1xxx OBJS += $(TARGET)_handlers.o LPC1xxx_startup.o @@ -36,8 +37,27 @@ LD_PATH = lpc1xxx LD_SCRIPT = $(LD_PATH)/linkscript.ld LD_TEMP = $(LD_PATH)/memory.ld +### User targets: + all: $(OUTFILE).bin +protect: $(OUTFILE).bin + $(LPCFIX) -p 2 $(OUTFILE).bin + +loadables: $(OUTFILE).elf + @cd loadable && $(MAKE) + +clean: + rm -f $(OBJS) $(LD_TEMP) $(OUTFILE).elf $(OUTFILE).bin $(OUTFILE).hex + @cd core && $(MAKE) clean + @cd tools && $(MAKE) clean + @cd lcd && $(MAKE) clean + @cd modules && $(MAKE) clean + @cd filesystem && $(MAKE) clean + @cd loadable && $(MAKE) clean + +### Internal targets + %.o : %.c $(CC) $(CFLAGS) -o $@ $< @@ -56,32 +76,24 @@ filesystem/libfat.a: tools/lpcfix: cd tools && $(MAKE) -$(OUTFILE).bin: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCFIX) +$(LD_TEMP): -@echo "MEMORY" > $(LD_TEMP) -@echo "{" >> $(LD_TEMP) -@echo " flash(rx): ORIGIN = 0x00000000, LENGTH = $(FLASH)" >> $(LD_TEMP) - -@echo " sram(rwx): ORIGIN = 0x10000000+$(SRAM_USB), LENGTH = $(SRAM)-$(SRAM_USB)" >> $(LD_TEMP) + -@echo " sram(rwx): ORIGIN = 0x10000000+$(SRAM_USB), LENGTH = $(SRAM)-$(SRAM_USB)-$(RAMCODE)" >> $(LD_TEMP) -@echo "}" >> $(LD_TEMP) -@echo "INCLUDE $(LD_SCRIPT)" >> $(LD_TEMP) - $(LD) $(LDFLAGS) -T $(LD_TEMP) -o $(OUTFILE).elf $(OBJS) $(LDLIBS) + +$(OUTFILE).elf: $(OBJS) $(SYS_OBJS) $(LIBS) $(LPCFIX) $(LD_TEMP) + $(CC) $(LDFLAGS) -T $(LD_TEMP) -o $(OUTFILE).elf $(OBJS) $(LDLIBS) -@echo "" $(SIZE) $(OUTFILE).elf -@echo "" - $(OBJCOPY) $(OCFLAGS) -O binary $(OUTFILE).elf $(OUTFILE).bin + +%.bin: %.elf + $(OBJCOPY) $(OCFLAGS) -O binary $< $@ -@echo "" - $(LPCFIX) -c $(OUTFILE).bin + $(LPCFIX) -c $@ -protect: $(OUTFILE).bin - $(LPCFIX) -p 2 $(OUTFILE).bin - -clean: - rm -f $(OBJS) $(LD_TEMP) $(OUTFILE).elf $(OUTFILE).bin $(OUTFILE).hex - @cd core && $(MAKE) clean - @cd tools && $(MAKE) clean - @cd lcd && $(MAKE) clean - @cd modules && $(MAKE) clean - @cd filesystem && $(MAKE) clean - - -.PHONY: lcd/liblcd.a modules/libmodules.a filesystem/libfat.a +.PHONY: $(LD_TEMP) lcd/liblcd.a modules/libmodules.a filesystem/libfat.a diff --git a/Makefile.inc b/Makefile.inc index 82a84d4..6762cdd 100644 --- a/Makefile.inc +++ b/Makefile.inc @@ -7,6 +7,7 @@ TARGET = LPC13xx FLASH = 32K SRAM = 8K +RAMCODE=1K # For USB HID support the LPC134x reserves 384 bytes from the sram, # if you don't want to use the USB features, just use 0 here. @@ -19,7 +20,8 @@ SRAM_USB = 384 CROSS_COMPILE = arm-none-eabi- AS = $(CROSS_COMPILE)gcc CC = $(CROSS_COMPILE)gcc -LD = $(CROSS_COMPILE)gcc +LD = $(CROSS_COMPILE)ld +REALLD = $(CROSS_COMPILE)ld SIZE = $(CROSS_COMPILE)size OBJCOPY = $(CROSS_COMPILE)objcopy OBJDUMP = $(CROSS_COMPILE)objdump @@ -39,5 +41,5 @@ CPU_TYPE = cortex-$(CORTEX_TYPE) ########################################################################## CFLAGS = -std=c99 -c -g -Os $(INCLUDE_PATHS) -Wall -mthumb -ffunction-sections -fdata-sections -fmessage-length=0 -mcpu=$(CPU_TYPE) -DTARGET=$(TARGET) -fno-builtin -LDFLAGS = -nostartfiles -mthumb -mcpu=$(CPU_TYPE) -Wl,--gc-sections +LDFLAGS = -nostartfiles diff --git a/loadable/.gitignore b/loadable/.gitignore new file mode 100644 index 0000000..60ca729 --- /dev/null +++ b/loadable/.gitignore @@ -0,0 +1,3 @@ +*.elf +*.bin +*.h diff --git a/loadable/Makefile b/loadable/Makefile new file mode 100644 index 0000000..fa86f1e --- /dev/null +++ b/loadable/Makefile @@ -0,0 +1,12 @@ +# Make doesn't allow dependencies on parent directory, so we need to +# run make from one level up: + +MAKEFILE=loadable/Makefile.sub +MAKE+=--no-print-directory + +all: + @cd .. && $(MAKE) -f $(MAKEFILE) + +clean: + @cd .. && $(MAKE) -f $(MAKEFILE) clean + diff --git a/loadable/Makefile.sub b/loadable/Makefile.sub new file mode 100644 index 0000000..a4763af --- /dev/null +++ b/loadable/Makefile.sub @@ -0,0 +1,47 @@ +DIR?= loadable + +########################################################################## +# User configuration and firmware specific object files +########################################################################## +SRCS = $(wildcard $(DIR)/*.c) +OBJS = $(foreach mod,$(SRCS),$(subst .c,.o,$(mod))) +ELFS = $(foreach mod,$(SRCS),$(subst .c,.elf,$(mod))) +BINS = $(foreach mod,$(SRCS),$(subst .c,.bin,$(mod))) +HDRS = $(foreach mod,$(SRCS),$(subst .c,.h,$(mod))) + +########################################################################## +# GNU GCC compiler flags +########################################################################## +ROOT_PATH?= . + +INCLUDE_PATHS = -I$(ROOT_PATH) -I$(ROOT_PATH)/core + +include $(ROOT_PATH)/Makefile.inc + +########################################################################## +# Compiler settings, parameters and flags +########################################################################## +FIRMWARE=$(ROOT_PATH)/$(OUTFILE).elf +LDFILE=$(DIR)/loadable.ld +CFLAGS+=-mlong-calls +LDFLAGS+= -R $(FIRMWARE) + +all: $(OBJS) $(ELFS) $(BINS) $(HDRS) + +%.o : %.c + $(CC) $(CFLAGS) -o $@ $< + +%.elf: %.o $(FIRMWARE) $(LDFILE) + $(LD) $(LDFLAGS) -T $(LDFILE) -o $@ $< + $(SIZE) $@ + +%.bin: %.elf + $(OBJCOPY) $(OCFLAGS) -O binary $< $@ + +%.h: %.bin $(DIR)/bin2h.pl + $(DIR)/bin2h.pl $< + +clean: + cd $(DIR) && rm -f *.o *.elf *.bin + +.SUFFIXES: diff --git a/loadable/bin2h.pl b/loadable/bin2h.pl new file mode 100755 index 0000000..185bc54 --- /dev/null +++ b/loadable/bin2h.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl +# +# vim:set ts=4 sw=4: + +use strict; + +sub dwim{ + local $/=undef; + my $file=shift; + + open(IN,"<:bytes",$file) || die "Can't open $file: $!"; + my @bytes=unpack("C*",); + close(IN); + + $file=~s/\.[^.]+$//; + + open(OUT,">","${file}.h") || die "Can't write ${file}.h: $!"; + + $file=~s!.*/!!; + + print OUT "const uint16_t loadable_${file}_size = ", scalar @bytes, ";\n"; + print OUT "const uint8_t loadable_${file}[] = {\n"; + + my $ctr=0; + for(@bytes){ + print OUT "\t" if($ctr==0); + printf OUT "0x%02x, ",$_; + if(++$ctr==8){ + print OUT "\n"; + $ctr=0; + }; + }; + print OUT "\n" if($ctr!=0); + + print OUT "};\n"; + close(OUT); +}; + +for(@ARGV){ + dwim($_); +}; diff --git a/loadable/blinktest.c b/loadable/blinktest.c new file mode 100644 index 0000000..7530f14 --- /dev/null +++ b/loadable/blinktest.c @@ -0,0 +1,10 @@ +#include + +#include "basic/basic.h" + +void ram(void){ + for (int x=0;x<20;x++){ + gpioSetValue (RB_LED1, x%2); + delayms(50); + }; +}; diff --git a/loadable/loadable.ld b/loadable/loadable.ld new file mode 100644 index 0000000..2666465 --- /dev/null +++ b/loadable/loadable.ld @@ -0,0 +1,52 @@ +MEMORY +{ + sram(rwx): ORIGIN = 0x10001800, LENGTH = 2K +} + + +sram_top = ORIGIN(sram) + LENGTH(sram); +ENTRY(boot_entry) + +SECTIONS +{ + .text : + { + *(.text*) + *(.rodata*) + } > sram + + /* + * More information about Special Section Indexes is available in the + * free "ELF for the ARM Architecture" document from ARM Limited + * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf + * + */ + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } > sram + __exidx_start = .; + .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) } > sram + __exidx_end = .; + + _etext = .; + + .data : AT (__exidx_end) + { + _data = .; + *(vtable) + *(.data*) + _edata = .; + } > sram + + /* zero initialized data */ + .bss : + { + _bss = .; + *(.bss*) + *(COMMON) + _ebss = .; + } > sram + + end = .; + + /* For GDB compatibility we decrease the top with 16 bytes */ + stack_entry = sram_top - 16; +}