Browse Source

Add linked list operation

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

54
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)
{
}
@ -54,25 +55,25 @@ void Error_Handler(void)
void prosee1(void *data)
{
tcb_t *self = (tcb_t *)data;
tcb_t *self = (tcb_t *)data;
while(1)
{
__disable_irq();
printf("prosee_id:%d, stack_ptr:%#x\r\n", self->task_id, self->stack_ptr);
__enable_irq();
HAL_Delay(1000);
{
__disable_irq();
printf("prosee_id:%d, stack_ptr:%#x\r\n", self->task_id, self->stack_ptr);
__enable_irq();
HAL_Delay(1000);
}
}
void prosee2(void *data)
{
tcb_t *self = (tcb_t *)data;
tcb_t *self = (tcb_t *)data;
while(1)
{
__disable_irq();
printf("prosee_id:%d, stack_ptr:%#x\r\n", self->task_id, self->stack_ptr);
__enable_irq();
HAL_Delay(1000);
{
__disable_irq();
printf("prosee_id:%d, stack_ptr:%#x\r\n", self->task_id, self->stack_ptr);
__enable_irq();
HAL_Delay(1000);
}
}
@ -83,20 +84,17 @@ uint8_t statck2[STACK_SIZE];
int main(void)
{
__disable_irq();
HAL_Init();
SystemClock_Config();
MX_USART1_UART_Init();
printf("Init tasks\r\n");
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");
};
}
__disable_irq();
HAL_Init();
SystemClock_Config();
MX_USART1_UART_Init();
printf("Init tasks\r\n");
init_task(&tcb1, prosee1, statck, (void *)&tcb1);
init_task(&tcb2, prosee2, statck2, (void *)&tcb2);
__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

45
thead/src/thread.c

@ -5,29 +5,17 @@
#include "thread.h"
/***
next_task为下一个执行目标
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