From cccc8852a8ff6678e5cbfa8240c9280318526efd Mon Sep 17 00:00:00 2001 From: Will Fong Date: Fri, 24 Apr 2015 16:06:06 +0800 Subject: [PATCH 1/2] Improve indentation and add variable 'next_task' --- 06-Preemptive/os.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/06-Preemptive/os.c b/06-Preemptive/os.c index b5e4cc8..cc47b4e 100644 --- a/06-Preemptive/os.c +++ b/06-Preemptive/os.c @@ -17,8 +17,8 @@ void usart_init(void) { - *(RCC_APB2ENR) |= (uint32_t) (0x00000001 | 0x00000004); - *(RCC_APB1ENR) |= (uint32_t) (0x00020000); + *(RCC_APB2ENR) |= (uint32_t)(0x00000001 | 0x00000004); + *(RCC_APB1ENR) |= (uint32_t)(0x00020000); /* USART2 Configuration, Rx->PA3, Tx->PA2 */ *(GPIOA_CRL) = 0x00004B00; @@ -109,6 +109,7 @@ int main(void) unsigned int *usertasks[TASK_LIMIT]; size_t task_count = 0; size_t current_task; + size_t next_task; usart_init(); @@ -127,13 +128,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]); 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; From 4c94fe232acb738c3ccf02ba274c09461339b5a2 Mon Sep 17 00:00:00 2001 From: Will Fong Date: Fri, 24 Apr 2015 16:11:35 +0800 Subject: [PATCH 2/2] Add a simple task_control_block --- 06-Preemptive/os.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/06-Preemptive/os.c b/06-Preemptive/os.c index cc47b4e..04b63dc 100644 --- a/06-Preemptive/os.c +++ b/06-Preemptive/os.c @@ -48,6 +48,11 @@ void delay(int count) while (count--); } +struct task_c_b { + unsigned int stack[STACK_SIZE]; + unsigned int * usertask; +} typedef tcb; + /* Exception return behavior */ #define HANDLER_MSP 0xFFFFFFF1 #define THREAD_MSP 0xFFFFFFF9 @@ -105,8 +110,7 @@ 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; @@ -115,10 +119,10 @@ int main(void) 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"); @@ -132,7 +136,7 @@ int main(void) 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 = next_task;