From 6587c8498ffe2a50bc9c8eb6d490e9d874678bfc Mon Sep 17 00:00:00 2001 From: Matte23 Date: Sat, 2 Nov 2024 19:07:52 +0100 Subject: [PATCH] Add base code --- .gdbinit | 2 ++ .gitignore | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 44 ++++++++++++++++++++++++++++++++++++++++ libc/library.c | 10 +++++++++ libc/library.h | 1 + linker.ld | 15 ++++++++++++++ os/Makefile | 22 ++++++++++++++++++++ os/alloc.c | 14 +++++++++++++ os/alloc.h | 4 ++++ os/main.c | 17 ++++++++++++++++ 10 files changed, 184 insertions(+) create mode 100644 .gdbinit create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 libc/library.c create mode 100644 libc/library.h create mode 100644 linker.ld create mode 100644 os/Makefile create mode 100644 os/alloc.c create mode 100644 os/alloc.h create mode 100644 os/main.c diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..ea0b84e --- /dev/null +++ b/.gdbinit @@ -0,0 +1,2 @@ +target remote :1234 +b main diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d6796a --- /dev/null +++ b/.gitignore @@ -0,0 +1,55 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +# GDB +.gdb_history \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..36b9686 --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +CC = arm-none-eabi-gcc +AS = arm-none-eabi-as +LD = arm-none-eabi-ld +CPU = -mcpu=cortex-m4 +THUMB = -mthumb + +LDFLAGS = -T linker.ld +CFLAGS = $(CPU) $(THUMB) + +QEMU = qemu-system-arm +BOARD = netduino2 + +# Output file +TARGET = main.elf +# Source files +SRCS = os/main.c os/process.c os/scheduler.c os/alloc.c tasks/library.c tasks/tasks.c +# Object files +OBJS = $(SRCS:.c=.o) + +debug: CFLAGS += -g +debug: all + +run: debug + $(QEMU) -M $(BOARD) -nographic -kernel $(TARGET) + +gdb: debug + $(QEMU) -M $(BOARD) -nographic -kernel $(TARGET) -s -S + +# Default target to build the ELF file +all: $(TARGET) + +$(TARGET): $(OBJS) startup.o + $(LD) $(LDFLAGS) startup.o $(OBJS) -o $(TARGET) + +startup.o: startup.s + $(AS) $(CPU) -g startup.s -o startup.o + +# Rule to compile source files into object files +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f *.elf *.o *.i + rm -f os/*.o tasks/*.o diff --git a/libc/library.c b/libc/library.c new file mode 100644 index 0000000..c37f1ee --- /dev/null +++ b/libc/library.c @@ -0,0 +1,10 @@ +#include "library.h" + +volatile unsigned int *const USART1_PTR = (unsigned int *)0x40011004; + +void puts(const char *s) { + while(*s != '\0') { /* Loop until end of string */ + *USART1_PTR= (unsigned int)(*s); /* Transmit char */ + s++; /* Next char */ + } +} diff --git a/libc/library.h b/libc/library.h new file mode 100644 index 0000000..3aa8f9b --- /dev/null +++ b/libc/library.h @@ -0,0 +1 @@ +void puts(const char *s); diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..8b92698 --- /dev/null +++ b/linker.ld @@ -0,0 +1,15 @@ +ENTRY(_start) + +SECTIONS +{ + /* text section (code)*/ + .text : { *(.text*) } + /* data section, initialized variables */ + .data : { *(.data) } + ram_start = 0x20000000; + /* bss section, uninitialized variables */ + .bss : { *(.bss*) } + /* stack section */ + /* The stack is placed at the end of the RAM */ + stack_top = 0x2001ffff; +} diff --git a/os/Makefile b/os/Makefile new file mode 100644 index 0000000..642f902 --- /dev/null +++ b/os/Makefile @@ -0,0 +1,22 @@ +CC = arm-none-eabi-gcc +AS = arm-none-eabi-as +LD = arm-none-eabi-ld +CPU = -mcpu=cortex-m4 +THUMB = -mthumb + +LDFLAGS = -T linker.ld +CFLAGS = -c $(CPU) $(THUMB) -g + +all: main.o process.o scheduler.o + +main.o: main.c + $(CC) $(CFLAGS) main.c -o main.o + +scheduler.o: scheduler.c + $(CC) $(CFLAGS) scheduler.c -o scheduler.o + +process.o: process.c + $(CC) $(CFLAGS) process.c -o process.o + +clean: + rm -f *.elf *.o *.i diff --git a/os/alloc.c b/os/alloc.c new file mode 100644 index 0000000..c2af1b5 --- /dev/null +++ b/os/alloc.c @@ -0,0 +1,14 @@ +#include "alloc.h" +#include "stdint.h" + +void* *first_mem_avail = (void*) 0x20000000; + +void init_allocator() { + *first_mem_avail = (void*) 0x20000000 +sizeof(void*); +} + +void* malloc(size_t size) { + void* block = *first_mem_avail; + *first_mem_avail += size; + return block; +} diff --git a/os/alloc.h b/os/alloc.h new file mode 100644 index 0000000..bfe4861 --- /dev/null +++ b/os/alloc.h @@ -0,0 +1,4 @@ +#include + +void init_allocator(); +void* malloc (size_t size); diff --git a/os/main.c b/os/main.c new file mode 100644 index 0000000..7d19c5e --- /dev/null +++ b/os/main.c @@ -0,0 +1,17 @@ +#include "process.h" +#include "scheduler.h" +#include "alloc.h" + +extern int task1(void); +extern int task2(void); + +int main(void) { + init_allocator(); + ProcessTable *ptable; + create_process_table(&ptable); + + create_process(ptable, task1); + create_process(ptable, task2); + + run(ptable); +}