From 3133a50644ebc0a56ea32ef4e404ce64426b6b8b Mon Sep 17 00:00:00 2001
From: Tormod Volden <debian.tormod@gmail.com>
Date: Fri, 1 Apr 2022 15:42:20 +0200
Subject: [PATCH 1/4] Add gcc build for Audio Speaker demo

---
 gcc/Makefile |  79 +++++++++++++++++++
 gcc/gcc.ld   | 208 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 287 insertions(+)
 create mode 100644 gcc/Makefile
 create mode 100644 gcc/gcc.ld

diff --git a/gcc/Makefile b/gcc/Makefile
new file mode 100644
index 0000000..473f040
--- /dev/null
+++ b/gcc/Makefile
@@ -0,0 +1,79 @@
+NAME = audio
+
+CROSS = arm-none-eabi-
+
+ifndef V
+  SILENT = @echo
+else
+  SILENT = :
+endif
+CC = $(SILENT) "(cc) $@" ; $(CROSS)gcc
+LD = $(SILENT) "(ld) $@" ; $(CROSS)gcc
+AS = $(SILENT) "(as) $@" ; $(CROSS)as
+CP = $(SILENT) "(cp) $@" ; $(CROSS)objcopy
+
+CMSIS_INC = -I../../../Libraries/CMSIS/Include
+CMSIS_INC += -I../../../Libraries/CMSIS/Device/ST/STM32F10x/Include
+CMSIS = ../../../Libraries/CMSIS/Device/ST/STM32F10x/Source/Templates/gcc_ride7
+
+STLIB = ../../../Libraries/STM32F10x_StdPeriph_Driver
+USBLIB = ../../../Libraries/STM32_USB-FS-Device_Driver
+EVAL = ../../../Utilities/STM32_EVAL
+
+vpath %.s $(CMSIS)
+vpath %.c ../src
+vpath %.c $(STLIB)/src
+vpath %.c $(USBLIB)/src
+
+# main
+OBJS += startup_stm32f10x_md.o
+OBJS += stm32f10x_exti.o
+OBJS += main.o hw_config.o stm32_it.o
+OBJS += usb_desc.o usb_istr.o usb_prop.o usb_pwr.o usb_endp.o
+# from USB library
+OBJS += usb_core.o usb_init.o usb_regs.o usb_int.o usb_mem.o usb_sil.o
+# from ST lib
+OBJS += stm32f10x_gpio.o stm32f10x_spi.o stm32f10x_tim.o stm32f10x_rcc.o
+OBJS += misc.o
+# gcc files
+OBJS += system_stm32f10x.o
+
+CFLAGS += -I. -I../inc
+CFLAGS += $(CMSIS_INC)
+CFLAGS += -I$(STLIB)/inc -I$(USBLIB)/inc
+CFLAGS += -I$(EVAL) -I$(EVAL)/STM3210B_EVAL -I$(EVAL)/Common
+CFLAGS += -DSTM32F10X_MD
+CFLAGS += -DUSE_STM3210B_EVAL
+CFLAGS += -DUSE_STDPERIPH_DRIVER
+
+LDSCRIPT = gcc.ld
+
+ASFLAGS += -mcpu=cortex-m3 -mthumb
+
+LDFLAGS += -T$(LDSCRIPT) -mthumb -mcpu=cortex-m3
+CFLAGS += -mcpu=cortex-m3 -mthumb
+CFLAGS += -std=c99
+
+#LDFLAGS += --specs=nano.specs
+CFLAGS += -fno-builtin -fno-exceptions -fno-unwind-tables
+CFLAGS += -Os -fno-common -g
+
+CFLAGS += -ffunction-sections -fdata-sections
+
+# we provide _sbrk and not _sbrk_r
+# CFLAGS += -DREENTRANT_SYSCALLS_PROVIDED -DMISSING_SYSCALL_NAMES
+# LDFLAGS +=  -mfloat-abi=soft
+
+#LDFLAGS += -lm
+LDFLAGS += -Wl,--gc-sections -Wl,-Map=$(NAME).map
+LDFLAGS += -nostartfiles -nodefaultlibs -nostdlib -fno-exceptions
+# LDFLAGS += -Wl,--print-gc-sections
+
+$(NAME).bin: $(NAME).elf
+	$(CP) -O binary $^ $@
+
+$(NAME).elf : $(OBJS)
+	$(LD) -o $@ $(OBJS) $(LDFLAGS)
+
+clean:
+	rm -f $(OBJS) $(NAME).elf $(NAME).bin $(NAME).map
diff --git a/gcc/gcc.ld b/gcc/gcc.ld
new file mode 100644
index 0000000..9f6d40d
--- /dev/null
+++ b/gcc/gcc.ld
@@ -0,0 +1,208 @@
+/* Linker script to configure memory regions.
+ * 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
+ */
+MEMORY
+{
+  FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x10000 /* 64K */
+  RAM (rwx)  : ORIGIN = 0x20000000, LENGTH = 0x5000 /* 20K */
+}
+
+/* Linker script to place sections and symbol values. Should be used together
+ * with other linker script that defines memory regions FLASH and RAM.
+ * It references following symbols, which must be defined in code:
+ *   Reset_Handler : Entry of reset handler
+ *
+ * It defines following symbols, which code can use without definition:
+ *   __exidx_start
+ *   __exidx_end
+ *   __copy_table_start__
+ *   __copy_table_end__
+ *   __zero_table_start__
+ *   __zero_table_end__
+ *   __etext
+ *   __data_start__
+ *   __preinit_array_start
+ *   __preinit_array_end
+ *   __init_array_start
+ *   __init_array_end
+ *   __fini_array_start
+ *   __fini_array_end
+ *   __data_end__
+ *   __bss_start__
+ *   __bss_end__
+ *   __end__
+ *   end
+ *   __HeapLimit
+ *   __StackLimit
+ *   __StackTop
+ *   __stack
+ */
+ENTRY(Reset_Handler)
+
+SECTIONS
+{
+	.text :
+	{
+		KEEP(*(.isr_vector))
+		*(.text*)
+
+		KEEP(*(.init))
+		KEEP(*(.fini))
+
+		/* .ctors */
+		*crtbegin.o(.ctors)
+		*crtbegin?.o(.ctors)
+		*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
+		*(SORT(.ctors.*))
+		*(.ctors)
+
+		/* .dtors */
+		*crtbegin.o(.dtors)
+		*crtbegin?.o(.dtors)
+		*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
+		*(SORT(.dtors.*))
+		*(.dtors)
+
+		*(.rodata*)
+
+		KEEP(*(.eh_frame*))
+	} > FLASH
+
+	.ARM.extab :
+	{
+		*(.ARM.extab* .gnu.linkonce.armextab.*)
+	} > FLASH
+
+	__exidx_start = .;
+	.ARM.exidx :
+	{
+		*(.ARM.exidx* .gnu.linkonce.armexidx.*)
+	} > FLASH
+	__exidx_end = .;
+
+	/* To copy multiple ROM to RAM sections,
+	 * uncomment .copy.table section and,
+	 * define __STARTUP_COPY_MULTIPLE in startup_ARMCMx.S */
+	/*
+	.copy.table :
+	{
+		. = ALIGN(4);
+		__copy_table_start__ = .;
+		LONG (__etext)
+		LONG (__data_start__)
+		LONG (__data_end__ - __data_start__)
+		LONG (__etext2)
+		LONG (__data2_start__)
+		LONG (__data2_end__ - __data2_start__)
+		__copy_table_end__ = .;
+	} > FLASH
+	*/
+
+	/* To clear multiple BSS sections,
+	 * uncomment .zero.table section and,
+	 * define __STARTUP_CLEAR_BSS_MULTIPLE in startup_ARMCMx.S */
+	/*
+	.zero.table :
+	{
+		. = ALIGN(4);
+		__zero_table_start__ = .;
+		LONG (__bss_start__)
+		LONG (__bss_end__ - __bss_start__)
+		LONG (__bss2_start__)
+		LONG (__bss2_end__ - __bss2_start__)
+		__zero_table_end__ = .;
+	} > FLASH
+	*/
+
+	__etext = .;
+
+	_sidata = __etext;
+
+	.data : AT (__etext)
+	{
+		__data_start__ = .;
+		*(vtable)
+		*(.data*)
+
+		. = ALIGN(4);
+		/* preinit data */
+		PROVIDE_HIDDEN (__preinit_array_start = .);
+		KEEP(*(.preinit_array))
+		PROVIDE_HIDDEN (__preinit_array_end = .);
+
+		. = ALIGN(4);
+		/* init data */
+		PROVIDE_HIDDEN (__init_array_start = .);
+		KEEP(*(SORT(.init_array.*)))
+		KEEP(*(.init_array))
+		PROVIDE_HIDDEN (__init_array_end = .);
+
+
+		. = ALIGN(4);
+		/* finit data */
+		PROVIDE_HIDDEN (__fini_array_start = .);
+		KEEP(*(SORT(.fini_array.*)))
+		KEEP(*(.fini_array))
+		PROVIDE_HIDDEN (__fini_array_end = .);
+
+		KEEP(*(.jcr*))
+		. = ALIGN(4);
+		/* All data end */
+		__data_end__ = .;
+
+	} > RAM
+
+	_sdata = __data_start__;
+	_edata = __data_end__;
+
+	.bss :
+	{
+		. = ALIGN(4);
+		__bss_start__ = .;
+		*(.bss*)
+		*(COMMON)
+		. = ALIGN(4);
+		__bss_end__ = .;
+	} > RAM
+
+	_sbss = __bss_start__;
+	_ebss = __bss_end__;
+
+	.heap (COPY):
+	{
+		__end__ = .;
+		PROVIDE(end = .);
+		*(.heap*)
+		__HeapLimit = .;
+	} > RAM
+
+	/* .stack_dummy section doesn't contains any symbols. It is only
+	 * used for linker to calculate size of stack sections, and assign
+	 * values to stack symbols later */
+	.stack_dummy (COPY):
+	{
+		*(.stack*)
+	} > RAM
+
+	/* Set stack top to end of RAM, and stack limit move down by
+	 * size of stack_dummy section */
+	__StackTop = ORIGIN(RAM) + LENGTH(RAM);
+	__StackLimit = __StackTop - SIZEOF(.stack_dummy);
+	PROVIDE(__stack = __StackTop);
+	_estack = __StackTop;
+
+	/* Check if data + heap + stack exceeds RAM limit */
+	ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
+
+    DISCARD :
+    {
+     libc.a ( * )
+     libm.a ( * )
+     libgcc.a ( * )
+    }
+
+}
-- 
2.25.1

