Browse Source

Merge pull request 'format code' (#3) from feature/mutil_thread into master

Reviewed-on: #3
master
xzb 4 months ago
parent
commit
f64dcc208b
  1. 4
      Core/Src/main.c
  2. 74
      thead/src/thread.c

4
Core/Src/main.c

@ -58,7 +58,7 @@ void prosee1(void *data)
while(1)
{
__disable_irq();
printf("prosee_%d, stack_ptr:%#x\r\n", self->task_id, self->stack_ptr);
printf("prosee_id:%d, stack_ptr:%#x\r\n", self->task_id, self->stack_ptr);
__enable_irq();
HAL_Delay(1000);
}
@ -70,7 +70,7 @@ void prosee2(void *data)
while(1)
{
__disable_irq();
printf("prosee_%d, stack_ptr:%#x\r\n", self->task_id, self->stack_ptr);
printf("prosee_id:%d, stack_ptr:%#x\r\n", self->task_id, self->stack_ptr);
__enable_irq();
HAL_Delay(1000);
}

74
thead/src/thread.c

@ -4,7 +4,6 @@
#include "usart.h"
#include "thread.h"
/***
next_task为下一个执行目标
***/
@ -14,7 +13,7 @@ tcb_t *next_task;
/***
***/
void switch_to_next_task(void)
void switch_to_next_task(void)
{
tcb_t *temp = current_task;
current_task = next_task;
@ -24,10 +23,10 @@ void switch_to_next_task(void)
/***
main函数
***/
void start_first_task(void)
void start_first_task(void)
{
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
__DSB();
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
__DSB();
__ISB();
}
@ -40,16 +39,13 @@ void start_first_task(void)
tcb_t *task[2];
uint8_t tcb_cunt = 0;
static uint8_t max_tcb = 0;
void tcb_scheduler(void)
void tcb_scheduler(void)
{
if(tcb_cunt >= 1)
{
tcb_cunt = -1;
}
tcb_cunt++;
next_task = task[tcb_cunt];
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
__DSB();
tcb_cunt = tcb_cunt % 2;
next_task = task[tcb_cunt];
tcb_cunt++;
SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
__DSB();
__ISB();
}
@ -59,35 +55,33 @@ void tcb_scheduler(void)
void init_task(tcb_t *tcb, void (*task_entry)(void *), uint8_t *start_stack, void *arg)
{
// 栈顶对齐到 8 字节(硬件要求)
static uint8_t tcb_id = 0;
task[tcb_id] = tcb;
tcb_id++;
tcb->task_id = tcb_id;
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);
static uint8_t tcb_id = 0;
task[tcb_id] = tcb;
tcb_id++;
tcb->task_id = tcb_id;
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);
*(--stack_top) = 0x01000000; // xPSR
*(--stack_top) = (uint32_t)task_entry; // PC
*(--stack_top) = 0xDEADBEEF; // LR
*(--stack_top) = 0x0000000C; // R12
*(--stack_top) = 0x00000003; // R3
*(--stack_top) = 0x00000002; // R2
*(--stack_top) = 0x00000000; // R1
*(--stack_top) = (uint32_t)arg; // R0 (参数)
*(--stack_top) = 0x01000000; // xPSR
*(--stack_top) = (uint32_t)task_entry; // PC
*(--stack_top) = 0xDEADBEEF; // LR
*(--stack_top) = 0x0000000C; // R12
*(--stack_top) = 0x00000003; // R3
*(--stack_top) = 0x00000002; // R2
*(--stack_top) = 0x00000000; // R1
*(--stack_top) = (uint32_t)arg; // R0 (参数)
for (int i = 0; i < 8; i++)
{
*(--stack_top) = 0x12345678; // R4-R11
for(int i = 0; i < 8; i++)
{
*(--stack_top) = 0x12345678; // R4-R11
}
tcb->stack_ptr = stack_top;
printf("init_tch:%d\r\n", tcb_id);
if(tcb_id == 1)
{
printf("start_tch:%d\r\n", tcb_id);
next_task = tcb;
}
printf("init_tch:%d\r\n", tcb_id);
if(tcb_id == 1)
{
printf("start_tch:%d\r\n", tcb_id);
next_task = tcb;
}
}

Loading…
Cancel
Save