Compare commits
5 Commits
137ec0fd74
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d0f42d43ae
|
||
|
|
d05bbdabc0
|
||
|
|
ba22538626
|
||
|
|
6d0a60881c
|
||
|
|
c265a35cc6
|
15
Makefile
15
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)
|
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 tasks/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,10 +1,15 @@
|
|||||||
#include "library.h"
|
#include "library.h"
|
||||||
|
#include "os/driver/usart.h"
|
||||||
volatile unsigned int *const USART1_PTR = (unsigned int *)0x40011004;
|
#include "os/delay.h"
|
||||||
|
|
||||||
void puts(const char *s) {
|
void puts(const char *s) {
|
||||||
while(*s != '\0') { /* Loop until end of string */
|
usart_tx_write_string(s);
|
||||||
*USART1_PTR= (unsigned int)(*s); /* Transmit char */
|
|
||||||
s++; /* Next char */
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
45
linker.ld
45
linker.ld
@@ -1,15 +1,40 @@
|
|||||||
ENTRY(_start)
|
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
|
SECTIONS
|
||||||
{
|
{
|
||||||
/* text section (code)*/
|
.text : { *(.text*) } > FLASH
|
||||||
.text : { *(.text*) }
|
.rodata : { *(.rodata*) } > FLASH
|
||||||
/* data section, initialized variables */
|
.bss : { *(.bss*) } > RAM
|
||||||
.data : { *(.data) }
|
.data : { *(.data*) } > RAM
|
||||||
ram_start = 0x20000000;
|
.heap : {
|
||||||
/* bss section, uninitialized variables */
|
. = ALIGN(8);
|
||||||
.bss : { *(.bss*) }
|
PROVIDE ( end = . );
|
||||||
/* stack section */
|
PROVIDE ( _end = . );
|
||||||
/* The stack is placed at the end of the RAM */
|
_heap_bottom = .;
|
||||||
stack_top = 0x2001ffff;
|
. = . + _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")
|
||||||
}
|
}
|
||||||
|
|||||||
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
|
|
||||||
10
os/alloc.c
10
os/alloc.c
@@ -1,14 +1,16 @@
|
|||||||
#include "alloc.h"
|
#include "alloc.h"
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
|
|
||||||
void* *first_mem_avail = (void*) 0x20000000;
|
HeapArena heap_arena;
|
||||||
|
|
||||||
|
extern void * _heap_top;
|
||||||
|
|
||||||
void init_allocator() {
|
void init_allocator() {
|
||||||
*first_mem_avail = (void*) 0x20000000 +sizeof(void*);
|
heap_arena.wilderness = &_heap_top;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* malloc(size_t size) {
|
void* malloc(size_t size) {
|
||||||
void* block = *first_mem_avail;
|
void* block = heap_arena.wilderness;
|
||||||
*first_mem_avail += size;
|
heap_arena.wilderness += size;
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void * wilderness;
|
||||||
|
} HeapArena;
|
||||||
|
|
||||||
void init_allocator();
|
void init_allocator();
|
||||||
void* malloc (size_t size);
|
void* malloc (size_t size);
|
||||||
|
|||||||
17
os/delay.c
Normal file
17
os/delay.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#define CLOCK_FREQUENCY 168000ULL
|
||||||
|
|
||||||
|
unsigned int ms_to_ticks(unsigned int ms) {
|
||||||
|
return ms * (CLOCK_FREQUENCY/3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void delay_routine(unsigned int delay_counter) {
|
||||||
|
asm("mov r1, %[input]\n"
|
||||||
|
"delay_loop:\n"
|
||||||
|
"subs r1, #1\n"
|
||||||
|
"bne delay_loop\n"
|
||||||
|
: [input] "=r" (delay_counter));
|
||||||
|
}
|
||||||
|
|
||||||
|
void delay_ms(unsigned int seconds) {
|
||||||
|
delay_routine(ms_to_ticks(seconds));
|
||||||
|
}
|
||||||
2
os/delay.h
Normal file
2
os/delay.h
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
void delay_routine(unsigned int delay_counter);
|
||||||
|
void delay_ms(unsigned int ms);
|
||||||
57
os/driver/usart.c
Normal file
57
os/driver/usart.c
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#include "usart.h"
|
||||||
|
#include "os/delay.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#define USART_SR_TXE (1 << 7)
|
||||||
|
|
||||||
|
typedef struct uart_s {
|
||||||
|
uint32_t USART_SR;
|
||||||
|
uint32_t USART_DR;
|
||||||
|
uint32_t USART_BRR;
|
||||||
|
uint32_t USART_CR1;
|
||||||
|
uint32_t USART_CR2;
|
||||||
|
uint32_t USART_CR3;
|
||||||
|
uint32_t USART_GTPR;
|
||||||
|
} uart_c;
|
||||||
|
|
||||||
|
volatile uart_c *const USART1 = (uart_c *)0x40011000;
|
||||||
|
|
||||||
|
static uint32_t usart_tx_ready(void) {
|
||||||
|
return (USART1->USART_SR & USART_SR_TXE) >> 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t usart_tx_write(const uint8_t *data_bytes, uint32_t n_bytes) {
|
||||||
|
if (data_bytes == NULL) return USART_TX_ERROR;
|
||||||
|
|
||||||
|
for (int i = 0; i < n_bytes; i++) {
|
||||||
|
unsigned int timeout = TIMEOUT;
|
||||||
|
while (!usart_tx_ready()) {
|
||||||
|
delay_ms(1);
|
||||||
|
timeout--;
|
||||||
|
if (timeout == 0) return USART_TX_BUSY;
|
||||||
|
}
|
||||||
|
|
||||||
|
USART1->USART_DR = *data_bytes;
|
||||||
|
|
||||||
|
data_bytes++;
|
||||||
|
}
|
||||||
|
return USART_TX_COMPLETE;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t usart_tx_write_string(const uint8_t *data_bytes) {
|
||||||
|
if (data_bytes == NULL) return USART_TX_ERROR;
|
||||||
|
|
||||||
|
while (*data_bytes != '\0') {
|
||||||
|
unsigned int timeout = TIMEOUT;
|
||||||
|
while (!usart_tx_ready()) {
|
||||||
|
delay_ms(1);
|
||||||
|
timeout--;
|
||||||
|
if (timeout == 0) return USART_TX_BUSY;
|
||||||
|
}
|
||||||
|
|
||||||
|
USART1->USART_DR = *data_bytes;
|
||||||
|
|
||||||
|
data_bytes++;
|
||||||
|
}
|
||||||
|
return USART_TX_COMPLETE;
|
||||||
|
}
|
||||||
12
os/driver/usart.h
Normal file
12
os/driver/usart.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#define TIMEOUT 1000
|
||||||
|
|
||||||
|
enum UartErrorCode {
|
||||||
|
USART_TX_ERROR,
|
||||||
|
USART_TX_BUSY,
|
||||||
|
USART_TX_COMPLETE
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t usart_tx_ready(void);
|
||||||
|
uint32_t usart_tx_write(const uint8_t *data_bytes, uint32_t n_bytes);
|
||||||
|
uint32_t usart_tx_write_string(const uint8_t *data_bytes);
|
||||||
@@ -1,15 +1,23 @@
|
|||||||
#include "process.h"
|
#include "process.h"
|
||||||
#include "alloc.h"
|
#include "alloc.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
void create_process_table(ProcessTable **table) {
|
void create_process_table(ProcessTable **table) {
|
||||||
*table = malloc(sizeof(ProcessTable));
|
*table = malloc(sizeof(ProcessTable));
|
||||||
(*table)->entries = 0;
|
(*table)->entries = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ProcessStack* create_stack(size_t size) {
|
||||||
|
ProcessStack* stack = malloc(sizeof(ProcessStack) + size*sizeof(uint32_t));
|
||||||
|
stack->stack_size = size;
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
int create_process(ProcessTable *table, void *entrypoint) {
|
int create_process(ProcessTable *table, void *entrypoint) {
|
||||||
if (table->entries>=MAX_PROCESS) return 1;
|
if (table->entries>=MAX_PROCESS) return 1;
|
||||||
|
|
||||||
Process *pentry = &table->process_list[table->entries++];
|
Process *pentry = &table->process_list[table->entries++];
|
||||||
pentry->entrypoint = entrypoint;
|
pentry->entrypoint = entrypoint;
|
||||||
|
pentry->stack = create_stack(STACK_SIZE);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
10
os/process.h
10
os/process.h
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stddef.h>
|
||||||
#ifndef PROCESS_H
|
#ifndef PROCESS_H
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@@ -7,9 +8,14 @@
|
|||||||
#define STACK_SIZE 32
|
#define STACK_SIZE 32
|
||||||
#define STACK_START(stack) (&stack[STACK_SIZE-1])
|
#define STACK_START(stack) (&stack[STACK_SIZE-1])
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t stack_size;
|
||||||
|
uint32_t stack[];
|
||||||
|
} ProcessStack;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int (*entrypoint)();
|
int (*entrypoint)();
|
||||||
uint32_t stack[STACK_SIZE];
|
ProcessStack* stack;
|
||||||
} Process;
|
} Process;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -19,5 +25,7 @@ typedef struct {
|
|||||||
|
|
||||||
void create_process_table(ProcessTable **table);
|
void create_process_table(ProcessTable **table);
|
||||||
|
|
||||||
|
ProcessStack* create_stack(size_t size);
|
||||||
|
|
||||||
int create_process(ProcessTable *table, void *entrypoint);
|
int create_process(ProcessTable *table, void *entrypoint);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
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