Compare commits
2 Commits
6d0a60881c
...
d05bbdabc0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d05bbdabc0
|
||
|
|
ba22538626
|
13
Makefile
13
Makefile
@@ -1,19 +1,22 @@
|
|||||||
CC = arm-none-eabi-gcc
|
TOOLCHAIN = arm-none-eabi
|
||||||
AS = arm-none-eabi-as
|
|
||||||
LD = arm-none-eabi-ld
|
CC = $(TOOLCHAIN)-gcc
|
||||||
|
AS = $(TOOLCHAIN)-as
|
||||||
|
LD = $(TOOLCHAIN)-ld
|
||||||
CPU = -mcpu=cortex-m4
|
CPU = -mcpu=cortex-m4
|
||||||
THUMB = -mthumb
|
THUMB = -mthumb
|
||||||
|
|
||||||
LDFLAGS = -T linker.ld
|
LDFLAGS = -T linker.ld
|
||||||
CFLAGS = $(CPU) $(THUMB) -I libc/ -I ./
|
CFLAGS = $(CPU) $(THUMB) -I libc/ -I ./
|
||||||
|
CC_SYMBOLS = -DTOOLCHAIN_GCC_ARM
|
||||||
|
|
||||||
QEMU = qemu-system-arm
|
QEMU = qemu-system-arm
|
||||||
BOARD = netduino2
|
BOARD = netduinoplus2
|
||||||
|
|
||||||
# Output file
|
# Output file
|
||||||
TARGET = main.elf
|
TARGET = main.elf
|
||||||
# Source files
|
# 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
|
# Object files
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
#include "library.h"
|
#include "library.h"
|
||||||
#include "os/driver/usart.h"
|
#include "os/driver/usart.h"
|
||||||
|
#include "os/delay.h"
|
||||||
|
|
||||||
void puts(const char *s) {
|
void puts(const char *s) {
|
||||||
usart_tx_write_string(s);
|
usart_tx_write_string(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void usleep(unsigned int usec) {
|
||||||
|
delay_ms(usec);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sleep(unsigned int sec) {
|
||||||
|
usleep(sec*1000);
|
||||||
|
}
|
||||||
@@ -1 +1,3 @@
|
|||||||
void puts(const char *s);
|
void puts(const char *s);
|
||||||
|
void usleep(unsigned int usec);
|
||||||
|
void sleep(unsigned int sec);
|
||||||
22
os/Makefile
22
os/Makefile
@@ -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
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#define CLOCK_FREQUENCY 120000
|
#define CLOCK_FREQUENCY 168000ULL
|
||||||
|
|
||||||
unsigned int ms_to_ticks(unsigned int ms) {
|
unsigned int ms_to_ticks(unsigned int ms) {
|
||||||
return ms * (CLOCK_FREQUENCY/3);
|
return ms * (CLOCK_FREQUENCY/3);
|
||||||
@@ -6,10 +6,9 @@ unsigned int ms_to_ticks(unsigned int ms) {
|
|||||||
|
|
||||||
void delay_routine(unsigned int delay_counter) {
|
void delay_routine(unsigned int delay_counter) {
|
||||||
asm("mov r1, %[input]\n"
|
asm("mov r1, %[input]\n"
|
||||||
"loop_2:\n"
|
"delay_loop:\n"
|
||||||
"subs r1, #1\n"
|
"subs r1, #1\n"
|
||||||
"cmp r1, #0\n"
|
"bne delay_loop\n"
|
||||||
"bne loop_2\n"
|
|
||||||
: [input] "=r" (delay_counter));
|
: [input] "=r" (delay_counter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
34
os/sync/spinlock.c
Normal file
34
os/sync/spinlock.c
Normal 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
16
os/sync/spinlock.h
Normal 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
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
#include "library.h"
|
#include "library.h"
|
||||||
|
|
||||||
int task2(void) {
|
int task2(void) {
|
||||||
puts("Hello World from task 1\n");
|
puts("Hello World from task 2\n");
|
||||||
|
sleep(4);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int task1(void) {
|
int task1(void) {
|
||||||
puts("Hello World from task 2\n");
|
puts("Hello World from task 1\n");
|
||||||
|
sleep(2);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user