From fb2c7ecf23013a6e32e0e2418ece75fbdfd61274 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sat, 19 Oct 2024 19:47:36 +0200 Subject: [PATCH] feat: add support for newlib --- Makefile | 13 +++- devices/general/startup.c | 2 + devices/stm32h747/linker_script.ld | 23 ++++++ src/main.c | 24 +++++++ src/syscalls.c | 112 +++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 src/syscalls.c diff --git a/Makefile b/Makefile index f6a4588..2d9064a 100644 --- a/Makefile +++ b/Makefile @@ -23,9 +23,9 @@ CFLAGS=-I$(INCDIR) \ -I$(DEVICEDIR)/$(INCDIR) \ -Ilibs/CMSIS_6/CMSIS/Core/Include \ -Ilibs/cmsis_device_h7/Include \ - -mthumb -mcpu=$(CPU) -nostdlib -g \ + -mthumb -mcpu=$(CPU) --specs=nano.specs -g \ -DCORE_CM7 -LDFLAGS=-T$(DEVICEDIR)/linker_script.ld -nostdlib +LDFLAGS=-T$(DEVICEDIR)/linker_script.ld CC=arm-none-eabi-gcc LD=arm-none-eabi-ld @@ -33,7 +33,7 @@ OPENOCD=openocd $(BINDIR)/$(APP): $(OBJS) mkdir -p $(BINDIR) - $(LD) $^ -o $@ $(LDFLAGS) + $(CC) $^ -o $@ $(CFLAGS) $(LDFLAGS) $(OBJDIR)/src/%.o: $(SRCDIR)/%.c mkdir -p "$(OBJDIR)/src" @@ -56,3 +56,10 @@ flash: $(BINDIR)/$(APP) clean: rm -rf $(OBJDIR) $(BINDIR) + +depend: .depend +.depend: $(SRCS) + rm -f "$@" + $(CC) $(CFLAGS) -MM $^ > "$@" + +include .depend diff --git a/devices/general/startup.c b/devices/general/startup.c index f6c0937..2e3de69 100644 --- a/devices/general/startup.c +++ b/devices/general/startup.c @@ -3,6 +3,7 @@ extern uint32_t _data_size, _bss_size, _data_loadaddr, _sdata, _sbss; void main(void); +void __libc_init_array(); void reset_handler(void) { @@ -25,5 +26,6 @@ void reset_handler(void) bss[i] = 0; } + __libc_init_array(); main(); } diff --git a/devices/stm32h747/linker_script.ld b/devices/stm32h747/linker_script.ld index cda55f8..2abddc6 100644 --- a/devices/stm32h747/linker_script.ld +++ b/devices/stm32h747/linker_script.ld @@ -22,6 +22,27 @@ SECTIONS _etext = .; } >FLASH + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } >FLASH + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } >FLASH + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT(.fini_array.*))) + KEEP (*(.fini_array*)) + PROVIDE_HIDDEN (__fini_array_end = .); + } >FLASH + .data : { . = ALIGN(4); @@ -40,11 +61,13 @@ SECTIONS { . = ALIGN(4); _sbss = .; + __bss_start__ = _sbss; *(.bss) . = ALIGN(4); _ebss = .; + __bss_end__ = _ebss; } >SRAM _bss_size = _ebss - _sbss; diff --git a/src/main.c b/src/main.c index f1307c1..5d103e5 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,27 @@ #include #include #include +#include #define LED1_PIN 12 #define LED2_PIN 13 +void hard_fault_handler() { + while(1) {} +} + +void usage_fault_handler() { + while(1) {} +} + +void nmi_handler() { + while(1) {} +} + +void bus_fault_handler() { + while(1) {} +} + void led_toggle() { GPIOI->ODR ^= (1 << LED1_PIN); @@ -33,6 +50,13 @@ void exti15_10_handler(void) void main() { + uint32_t *i1 = malloc(sizeof(uint32_t)); + uint32_t *i2 = malloc(sizeof(uint32_t)); + + free(i1); + + uint32_t *i3 = malloc(sizeof(uint32_t)); + led_gpio_en(); led_input(); led_toggle(); diff --git a/src/syscalls.c b/src/syscalls.c new file mode 100644 index 0000000..1993682 --- /dev/null +++ b/src/syscalls.c @@ -0,0 +1,112 @@ +#include +#include +#include +#undef errno +extern int errno; + +void _exit(int exit_code) +{ + while (1) + { + + } +} + +int _close(int file) { + return -1; +} + +char *__env[1] = { 0 }; +char **environ = __env; + +int _execve(char *name, char **argv, char **env) { + errno = ENOMEM; + return -1; +} + +int _fork(void) { + errno = EAGAIN; + return -1; +} + + +int _fstat(int file, struct stat *st) { + st->st_mode = S_IFCHR; + return 0; +} + +int _getpid(void) { + return 1; +} + +int _isatty(int file) { + return 1; +} + +int _kill(int pid, int sig) { + errno = EINVAL; + return -1; +} + +int _link(char *old, char *new) { + errno = EMLINK; + return -1; +} + +int _lseek(int file, int ptr, int dir) { + return 0; +} + +int _open(const char *name, int flags, int mode) { + return -1; +} + +int _read(int file, char *ptr, int len) { + return 0; +} + +register char * stack_ptr asm("sp"); + +caddr_t _sbrk(int incr) { + extern char __bss_end__; /* Defined by the linker */ + static char *heap_end; + char *prev_heap_end; + + if (heap_end == 0) { + heap_end = &__bss_end__; + } + prev_heap_end = heap_end; + if (heap_end + incr > stack_ptr) { + while (1) + { + // Heap and stack collision + } + } + + heap_end += incr; + return (caddr_t) prev_heap_end; +} + +int _stat(char *file, struct stat *st) { + st->st_mode = S_IFCHR; + return 0; +} + +int _times(struct tms *buf) { + return -1; +} + +int _unlink(char *name) { + errno = ENOENT; + return -1; +} + +int _wait(int *status) { + errno = ECHILD; + return -1; +} + +int _write(int file, char *ptr, int len) { + errno = ENOENT; + return -1; +} -- 2.49.0