Browse Source

Add linked list operation

pull/4/head
zhibiao.xu 4 months ago
parent
commit
ed83f2b771
  1. 4
      Core/Src/main.c
  2. 1
      thead/src/context_switch.s
  3. 43
      thead/src/thread.c

4
Core/Src/main.c

@ -47,6 +47,7 @@ void SystemClock_Config(void)
void Error_Handler(void)
{
__disable_irq();
printf("Error_Handler\r\n");
while(1)
{
}
@ -91,12 +92,9 @@ int main(void)
init_task(&tcb1, prosee1, statck, (void *)&tcb1);
init_task(&tcb2, prosee2, statck2, (void *)&tcb2);
/* run the first task */
start_first_task();
__enable_irq();
while(1)
{
printf("idle\r\n");
};
}

1
thead/src/context_switch.s

@ -5,7 +5,6 @@
AREA |.text|, CODE, READONLY ;
EXPORT PendSV_Handler ; PendSV_Handler
IMPORT current_task ; C current_task
IMPORT next_task ; C next_task
IMPORT switch_to_next_task ; C switch_to_next_task
PendSV_Handler PROC

43
thead/src/thread.c

@ -8,26 +8,14 @@
next_task为下一个执行目标
***/
tcb_t *current_task;
tcb_t *next_task;
tcb_t *task_list = { 0 };
/***
***/
void switch_to_next_task(void)
{
tcb_t *temp = current_task;
current_task = next_task;
next_task = temp;
}
/***
main函数
***/
void start_first_task(void)
{
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
__DSB();
__ISB();
current_task = (current_task ? current_task : task_list)->next;
}
/***
@ -36,14 +24,8 @@ void start_first_task(void)
2.
3.
***/
tcb_t *task[2];
uint8_t tcb_cunt = 0;
static uint8_t max_tcb = 0;
void tcb_scheduler(void)
{
tcb_cunt = tcb_cunt % 2;
next_task = task[tcb_cunt];
tcb_cunt++;
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
__DSB();
__ISB();
@ -54,11 +36,9 @@ void tcb_scheduler(void)
***/
void init_task(tcb_t *tcb, void (*task_entry)(void *), uint8_t *start_stack, void *arg)
{
static tcb_t *task_first_list;
// 栈顶对齐到 8 字节(硬件要求)
static uint8_t tcb_id = 0;
task[tcb_id] = tcb;
tcb_id++;
tcb->task_id = tcb_id;
tcb->task_id = task_list ? task_list->task_id + 1 : 1;
tcb->stack_ptr = (uint32_t *)start_stack;
uint32_t *stack_top = (uint32_t *)((uint8_t *)tcb->stack_ptr + STACK_SIZE);
stack_top = (uint32_t *)((uint32_t)stack_top & ~0x07);
@ -78,10 +58,17 @@ void init_task(tcb_t *tcb, void (*task_entry)(void *), uint8_t *start_stack, voi
}
tcb->stack_ptr = stack_top;
printf("init_tch:%d\r\n", tcb_id);
if(tcb_id == 1)
printf("init_tch:%d\r\n", tcb->task_id);
if(tcb->task_id == 1)
{
printf("start_tch:%d\r\n", tcb->task_id);
task_first_list = tcb;
task_list = tcb;
}
else
{
printf("start_tch:%d\r\n", tcb_id);
next_task = tcb;
task_list->next = tcb;
tcb->next = task_first_list;
task_list = tcb;
}
}

Loading…
Cancel
Save