diff --git a/Makefile b/Makefile index cdf8f28..91b4ec7 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,14 @@ -CC = arm-none-eabi-gcc -AS = arm-none-eabi-as -LD = arm-none-eabi-ld +TOOLCHAIN = arm-none-eabi + +CC = $(TOOLCHAIN)-gcc +AS = $(TOOLCHAIN)-as +LD = $(TOOLCHAIN)-ld CPU = -mcpu=cortex-m4 THUMB = -mthumb LDFLAGS = -T linker.ld CFLAGS = $(CPU) $(THUMB) -I libc/ -I ./ +CC_SYMBOLS = -DTOOLCHAIN_GCC_ARM QEMU = qemu-system-arm BOARD = netduinoplus2 @@ -13,7 +16,7 @@ BOARD = netduinoplus2 # Output file TARGET = main.elf # Source files -SRCS = os/main.c os/process.c os/scheduler.c os/alloc.c os/delay.c os/driver/usart.c libc/library.c tasks/tasks.c +SRCS = os/main.c os/process.c os/scheduler.c os/alloc.c os/delay.c os/driver/usart.c os/sync/spinlock.c libc/library.c tasks/tasks.c # Object files OBJS = $(SRCS:.c=.o) diff --git a/os/Makefile b/os/Makefile deleted file mode 100644 index 642f902..0000000 --- a/os/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -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/sync/spinlock.c b/os/sync/spinlock.c new file mode 100644 index 0000000..31334de --- /dev/null +++ b/os/sync/spinlock.c @@ -0,0 +1,34 @@ +// sync_primitives.c +#include "spinlock.h" + +// Initialize the spinlock +void spinlock_init(Spinlock *spinlock) { + spinlock->lock = 0; // Unlocked state +} + +// Acquire the spinlock +void spinlock_acquire(Spinlock *spinlock) { + int status; + do { + // Wait until the lock is available + do { + __asm__ volatile ("ldrex %0, [%1]" + : "=&r" (status) + : "r" (&spinlock->lock) + : "memory"); + } while (status != 0); + + // Try to acquire the lock using strex + __asm__ volatile ("strex %0, %2, [%1]" + : "=&r" (status) + : "r" (&spinlock->lock), "r" (1) + : "memory"); + } while (status != 0); // Retry if strex failed +} + +// Release the spinlock +void spinlock_release(Spinlock *spinlock) { + // Use a memory barrier to ensure proper ordering + __asm__ volatile ("dmb" ::: "memory"); + spinlock->lock = 0; // Release the lock +} diff --git a/os/sync/spinlock.h b/os/sync/spinlock.h new file mode 100644 index 0000000..3829f46 --- /dev/null +++ b/os/sync/spinlock.h @@ -0,0 +1,16 @@ +#ifndef SPINLOCK_H +#define SPINLOCK_H + +#include + +// Spinlock +typedef struct { + volatile int lock; +} Spinlock; + +void spinlock_init(Spinlock *spinlock); +void spinlock_acquire(Spinlock *spinlock); +void spinlock_release(Spinlock *spinlock); + + +#endif