Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions 06-Preemptive/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ void delay(int count)
while (count--);
}

typedef struct {
unsigned int stack[STACK_SIZE];
unsigned int * usertask;
} tcb;

/* Exception return behavior */
#define HANDLER_MSP 0xFFFFFFF1
#define THREAD_MSP 0xFFFFFFF9
Expand Down Expand Up @@ -105,19 +110,19 @@ void task2_func(void)

int main(void)
{
unsigned int user_stacks[TASK_LIMIT][STACK_SIZE];
unsigned int *usertasks[TASK_LIMIT];
tcb user_proc[TASK_LIMIT];
size_t task_count = 0;
size_t current_task;
size_t next_task;

usart_init();

print_str("OS: Starting...\n");
print_str("OS: First create task 1\n");
usertasks[0] = create_task(user_stacks[0], &task1_func);
user_proc[0].usertask = create_task(user_proc[0].stack, &task1_func);
task_count += 1;
print_str("OS: Back to OS, create task 2\n");
usertasks[1] = create_task(user_stacks[1], &task2_func);
user_proc[1].usertask = create_task(user_proc[1].stack, &task2_func);
task_count += 1;

print_str("\nOS: Start round-robin scheduler!\n");
Expand All @@ -127,13 +132,15 @@ int main(void)
*SYSTICK_VAL = 0;
*SYSTICK_CTRL = 0x07;
current_task = 0;
next_task = current_task == (task_count - 1) ? 0 : current_task + 1;

while (1) {
print_str("OS: Activate next task\n");
usertasks[current_task] = activate(usertasks[current_task]);
user_proc[current_task].usertask = activate(user_proc[current_task].usertask);
print_str("OS: Back to OS\n");

current_task = current_task == (task_count - 1) ? 0 : current_task + 1;
current_task = next_task;
next_task = current_task == (task_count - 1) ? 0 : current_task + 1;
}

return 0;
Expand Down