Compare commits

..

3 Commits

Author SHA1 Message Date
Matte23
d0f42d43ae Define heap in linker and place other sections correctly 2024-11-03 19:41:58 +01:00
Matte23
d05bbdabc0 Add ARM spinlock 2024-11-03 18:13:56 +01:00
Matte23
ba22538626 Include sleep into library 2024-11-03 15:01:54 +01:00
11 changed files with 122 additions and 47 deletions

View File

@@ -1,19 +1,22 @@
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 = netduino2
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)

View File

@@ -1,6 +1,15 @@
#include "library.h"
#include "os/driver/usart.h"
#include "os/delay.h"
void puts(const char *s) {
usart_tx_write_string(s);
}
void usleep(unsigned int usec) {
delay_ms(usec);
}
void sleep(unsigned int sec) {
usleep(sec*1000);
}

View File

@@ -1 +1,3 @@
void puts(const char *s);
void usleep(unsigned int usec);
void sleep(unsigned int sec);

View File

@@ -1,15 +1,40 @@
ENTRY(_start)
MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
}
_Min_Heap_Size = 0x200 ;
_Min_Stack_Size = 0x200 ; /* Required amount of stack. Used by main(), then re-used as the interrupt stack after the kernel starts. */
_estack = ORIGIN(RAM) + LENGTH(RAM);
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;
.text : { *(.text*) } > FLASH
.rodata : { *(.rodata*) } > FLASH
.bss : { *(.bss*) } > RAM
.data : { *(.data*) } > RAM
.heap : {
. = ALIGN(8);
PROVIDE ( end = . );
PROVIDE ( _end = . );
_heap_bottom = .;
. = . + _Min_Heap_Size;
_heap_top = .;
. = . + _Min_Stack_Size;
. = ALIGN(8);
} >RAM
.stack (NOLOAD) : {
. = ORIGIN(RAM) + LENGTH(RAM);
stack_top = .;
} > RAM
__StackLimit = stack_top - _Min_Stack_Size;
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__StackLimit >= _heap_top, "region RAM overflowed with stack")
}

View File

@@ -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

View File

@@ -1,14 +1,16 @@
#include "alloc.h"
#include "stdint.h"
void* *first_mem_avail = (void*) 0x20000000;
HeapArena heap_arena;
extern void * _heap_top;
void init_allocator() {
*first_mem_avail = (void*) 0x20000000 +sizeof(void*);
heap_arena.wilderness = &_heap_top;
}
void* malloc(size_t size) {
void* block = *first_mem_avail;
*first_mem_avail += size;
void* block = heap_arena.wilderness;
heap_arena.wilderness += size;
return block;
}

View File

@@ -1,4 +1,9 @@
#include <stddef.h>
#include <stdint.h>
typedef struct {
void * wilderness;
} HeapArena;
void init_allocator();
void* malloc (size_t size);

View File

@@ -1,4 +1,4 @@
#define CLOCK_FREQUENCY 120000
#define CLOCK_FREQUENCY 168000ULL
unsigned int ms_to_ticks(unsigned int ms) {
return ms * (CLOCK_FREQUENCY/3);
@@ -6,10 +6,9 @@ unsigned int ms_to_ticks(unsigned int ms) {
void delay_routine(unsigned int delay_counter) {
asm("mov r1, %[input]\n"
"loop_2:\n"
"delay_loop:\n"
"subs r1, #1\n"
"cmp r1, #0\n"
"bne loop_2\n"
"bne delay_loop\n"
: [input] "=r" (delay_counter));
}

34
os/sync/spinlock.c Normal file
View File

@@ -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
}

16
os/sync/spinlock.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef SPINLOCK_H
#define SPINLOCK_H
#include <stdint.h>
// Spinlock
typedef struct {
volatile int lock;
} Spinlock;
void spinlock_init(Spinlock *spinlock);
void spinlock_acquire(Spinlock *spinlock);
void spinlock_release(Spinlock *spinlock);
#endif

View File

@@ -1,11 +1,13 @@
#include "library.h"
int task2(void) {
puts("Hello World from task 1\n");
puts("Hello World from task 2\n");
sleep(4);
return 0;
}
int task1(void) {
puts("Hello World from task 2\n");
puts("Hello World from task 1\n");
sleep(2);
return 0;
}