CH32V10X 适配

This commit is contained in:
2026-05-28 15:23:19 +08:00
parent dc144a5cf4
commit e5a2334bb6
3 changed files with 379 additions and 285 deletions
+41 -65
View File
@@ -1,3 +1,4 @@
#include <os_isr.h>
#include "os_port.h"
#include "os_stack.h"
#include "os_macros.h"
@@ -5,7 +6,6 @@
#include "os_compiler.h"
#include "os_systick.h"
#include "os_sched.h"
#include "os_isr.h"
#include "cpu.h"
/* ------------------------------------------------------------------------------------------------------------------ */
@@ -20,7 +20,7 @@ uint32_t _SysTick_Config(os_tick_t ticks)
{
NVIC->CFGR = (CPU_KEY1<<16) | (1<<1/*NESTCTRL*/) | (1<<0/*HWSTKCTRL*/); /* 关闭硬件压栈和嵌套 */
NVIC->CFGR = 0xFA050003; /* 关闭硬件压栈和嵌套 */
NVIC_SetPriority(SysTick_IRQn,0xf0);
NVIC_SetPriority(Software_IRQn,0xf0);
@@ -44,6 +44,38 @@ uint32_t _SysTick_Config(os_tick_t ticks)
/* */
typedef struct {
os_uintptr_t mstatus; /* zero, 用于 mstatus 保存 */
os_uintptr_t ra; /* 返回地址 */
os_uintptr_t mepc; /* 栈指针, mepc */
os_uintptr_t x3; /* 全局指针 */
os_uintptr_t x4; /* 线程指针 */
os_uintptr_t x5; /* 临时寄存器0 */
os_uintptr_t x6; /* 临时寄存器1 */
os_uintptr_t x7; /* 临时寄存器2 */
os_uintptr_t x8; /* s0/fp */
os_uintptr_t x9; /* 保存寄存器1 */
os_uintptr_t a0; /* 函数参数/返回值 */
os_uintptr_t x11; /* 函数参数 */
os_uintptr_t x12; /* 函数参数 */
os_uintptr_t x13; /* 函数参数 */
os_uintptr_t x14; /* 函数参数 */
os_uintptr_t x15; /* 函数参数 */
os_uintptr_t x16; /* 函数参数 */
os_uintptr_t x17; /* 函数参数 */
os_uintptr_t x18; /* 保存寄存器 */
os_uintptr_t x19; /* 保存寄存器 */
os_uintptr_t x20; /* 保存寄存器 */
os_uintptr_t x21; /* 保存寄存器 */
os_uintptr_t x22; /* 保存寄存器 */
os_uintptr_t x23; /* 保存寄存器 */
os_uintptr_t x24; /* 保存寄存器 */
os_uintptr_t x25; /* 保存寄存器 */
os_uintptr_t x26; /* 保存寄存器 */
os_uintptr_t x27; /* 保存寄存器 */
os_uintptr_t x28; /* 临时寄存器 */
os_uintptr_t x29; /* 临时寄存器 */
os_uintptr_t x30; /* 临时寄存器 */
os_uintptr_t x31; /* 临时寄存器 */
#if (OS_CFG_CPU_FPU_PRESENT)
os_uintptr_t f0;
os_uintptr_t f1;
@@ -78,55 +110,23 @@ typedef struct {
os_uintptr_t f30;
os_uintptr_t f31;
#endif
os_uintptr_t mstatus; /* zero, 用于 mstatus 保存 */
os_uintptr_t ra; /* 返回地址 */
os_uintptr_t mepc; /* 栈指针, mepc */
os_uintptr_t x3; /* 全局指针 */
os_uintptr_t x4; /* 线程指针 */
os_uintptr_t x5; /* 临时寄存器0 */
os_uintptr_t x6; /* 临时寄存器1 */
os_uintptr_t x7; /* 临时寄存器2 */
os_uintptr_t x8; /* s0/fp */
os_uintptr_t x9; /* 保存寄存器1 */
os_uintptr_t a0; /* 函数参数/返回值 */
os_uintptr_t x11; /* 函数参数 */
os_uintptr_t x12; /* 函数参数 */
os_uintptr_t x13; /* 函数参数 */
os_uintptr_t x14; /* 函数参数 */
os_uintptr_t x15; /* 函数参数 */
os_uintptr_t x16; /* 函数参数 */
os_uintptr_t x17; /* 函数参数 */
os_uintptr_t x18; /* 保存寄存器 */
os_uintptr_t x19; /* 保存寄存器 */
os_uintptr_t x20; /* 保存寄存器 */
os_uintptr_t x21; /* 保存寄存器 */
os_uintptr_t x22; /* 保存寄存器 */
os_uintptr_t x23; /* 保存寄存器 */
os_uintptr_t x24; /* 保存寄存器 */
os_uintptr_t x25; /* 保存寄存器 */
os_uintptr_t x26; /* 保存寄存器 */
os_uintptr_t x27; /* 保存寄存器 */
os_uintptr_t x28; /* 临时寄存器 */
os_uintptr_t x29; /* 临时寄存器 */
os_uintptr_t x30; /* 临时寄存器 */
os_uintptr_t x31; /* 临时寄存器 */
}os_stack_frame_t;
#define STACK_FRAME_SIZE sizeof(os_stack_frame_t)
#define MSTATUS_MPIE (1<<7)
os_stack_t os_port_cpu_stack_init(os_task_entry_t entry, void* param
, void* stack_base, os_size_t stack_size, void(*exit_entry)(void)){
os_uintptr_t stack_max = (os_uintptr_t)((uint8_t *) stack_base + stack_size);
os_uintptr_t real_max = OS_ALIGN_DOWN(stack_max, OS_ALIGN_SIZE); /* 实际栈顶的位置,对齐后的位置 */
stack_size -= (stack_max - real_max); /* 实际栈大小 */
uint8_t value = 0x01;
os_stack_frame_t* p_frame = (os_stack_frame_t*)(real_max - STACK_FRAME_SIZE);
for(os_size_t i=0; i<STACK_FRAME_SIZE/sizeof(os_uintptr_t); i++){
((os_uintptr_t*)(p_frame))[i] = 0x5ac0ffee;
((os_uintptr_t*)(p_frame))[i] = (value<<24) | (value<<16) | (value << 8) | value;
value++;
}
p_frame->mstatus = MSTATUS_MPIE;
p_frame->mstatus = 0x1880;
p_frame->mepc = (os_uintptr_t)entry;
p_frame->a0 = (os_uintptr_t)param;
p_frame->ra = (os_uintptr_t)exit_entry;
@@ -139,29 +139,6 @@ os_stack_t os_port_cpu_stack_init(os_task_entry_t entry, void* param
return stack;
}
//void os_port_disable_irq(void){
// RISCV_disable_irq();
//}
//
//void os_port_enable_irq(void){
// RISCV_enable_irq();
//}
//
//os_uint_t os_port_save_disable_irq(void){
// return os_port_save_disable_irq_in_isr();
//}
//
//void os_port_restore_irq(os_uint_t level){
// os_port_restore_irq_in_isr(level);
//}
//os_uint_t os_port_save_disable_irq_in_isr(void){
// return RISCV_get_set_MSTATUS(0x1800);
//}
//void os_port_restore_irq_in_isr(os_uint_t level){
// RISCV_set_MSTATUS(level);
//}
void os_sched(void){
os_sched_in_isr();
@@ -204,22 +181,21 @@ void os_port_context_switch(void* from_sp, void* to_sp){
g_os_port_switch_from_sp = from_sp;
g_os_port_switch_to_sp = to_sp;
g_os_port_context_switch_flag = OS_TRUE;
os_critical_leave_in_isr();
if(from_sp==0){
os_critical_leave_in_isr();
os_port_context_switch_to((os_uintptr_t)to_sp);
}else{
os_critical_leave_in_isr();
os_port_send_context_switch_request();
}
}
void SysTick_Handler(void) __attribute__((interrupt()));
void SysTick_Handler(void){
void SysTick_Handler_C(void){
SysTick->CTLR=0;
SysTick->CNTL0=0;SysTick->CNTL1=0;SysTick->CNTL2=0;SysTick->CNTL3=0;
SysTick->CNTH0=0;SysTick->CNTH1=0;SysTick->CNTH2=0;SysTick->CNTH3=0;
SysTick->CTLR=0x1;
// 调用系统心跳处理
os_systick_tick();
}