Files
2026-07-28 10:51:28 +08:00

99 lines
2.7 KiB
C

#ifndef INCLUDED_OS_SYSTICK_H
#define INCLUDED_OS_SYSTICK_H
#include <stdatomic.h>
#ifndef INCLUDED_OS_TYPES_H
#include <os_types.h>
#endif /*INCLUDED_OS_TYPES_H*/
#ifndef INCLUDED_OS_COMPILER_H
#include <os_compiler.h>
#endif /*INCLUDED_OS_COMPILER_H*/
#ifndef INCLUDED_OS_CRITICAL_H
#include <os_critical.h>
#endif /*INCLUDED_OS_CRITICAL_H*/
#ifndef INCLUDED_OS_SCHED_H
#include <os_sched.h>
#endif /*INCLUDED_OS_SCHED_H*/
#ifndef INCLUDED_OS_YIELD_LIST_H
#include <os_yield_list.h>
#endif /*INCLUDED_OS_YIELD_LIST_H*/
#ifndef INCLUDED_OS_TIMEWHEEL_H
#include <os_timewheel.h>
#endif /*INCLUDED_OS_TIMEWHEEL_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
extern volatile os_tick_t g_os_systick_ticks;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
OS_STATIC_FORCE_INLINE
void os_systick_tick(void){
os_critical_enter_in_isr();
os_bool_t need_sched_flag = OS_FALSE;
g_os_systick_ticks++;
if(g_os_sched_current_task_p && g_os_sched_current_task_p->state == kOsTaskState_Running){
if(g_os_sched_current_task_p->remain_ticks>0
&& (g_os_sched_current_task_p->remain_ticks <= g_os_sched_current_task_p->init_ticks)){
g_os_sched_current_task_p->remain_ticks--;
if(g_os_sched_current_task_p->remain_ticks==0){
// 时间窗用完了
g_os_sched_current_task_p->remain_ticks = g_os_sched_current_task_p->init_ticks; // 重置时间窗
os_yield_list_insert((os_task_t*)g_os_sched_current_task_p); // 加入yield列表
need_sched_flag = OS_TRUE;
}
}
}
os_err_t err = os_timewheel_tick();
if(err==OS_TIMEWHEEL_TICK_NEED_SCHEDULE){
need_sched_flag = OS_TRUE;
}
os_critical_leave_in_isr();
if(need_sched_flag == OS_TRUE){
os_sched_in_isr();
}
}
OS_STATIC_FORCE_INLINE
os_tick_t os_systick_get(void){
return g_os_systick_ticks;
}
OS_STATIC_FORCE_INLINE
os_tick_t os_systick_safe_get(void) {
os_critical_enter();
const os_tick_t ticks = g_os_systick_ticks;
os_critical_leave();
return ticks;
}
OS_STATIC_FORCE_INLINE
os_tick_t os_systick_safe_get_in_isr(void) {
os_critical_enter_in_isr();
const os_tick_t ticks = g_os_systick_ticks;
os_critical_leave_in_isr();
return ticks;
}
OS_STATIC_FORCE_INLINE
os_bool_t os_systick_is_timeout(os_tick_t start_tick, os_tick_t timeout_ticks) {
volatile os_tick_t end_ticks = g_os_systick_ticks;
return (end_ticks - start_tick) >= timeout_ticks;
}
#endif /*INCLUDED_OS_SYSTICK_H*/