RT-Thread学习笔记 --时钟节拍 rt_tick

RT-Thread学习笔记 --时钟节拍 rt_tick

时钟节拍

时钟节拍理解

任何操作系统都需要提供一个时钟节拍,以供系统处理所有和时间有关的事件。如线程的延时、线程时间片轮转调度以及定时器超时等。

RT-Thread,时钟节拍的长度根据rtconfig.h配置文件中定义:

/* RT-Thread Kernel */

#define RT_NAME_MAX 8
#define RT_ALIGN_SIZE 4
#define RT_THREAD_PRIORITY_32
#define RT_THREAD_PRIORITY_MAX 32
/*
 *频率是1000hz 周期是1/1000s = 1ms
 *修改成10000hz  周期是1/10000 = 100us
 */
#define RT_TICK_PER_SECOND 1000
#define RT_USING_OVERFLOW_CHECK
#define RT_USING_HOOK
#define RT_USING_IDLE_HOOK
#define RT_IDLE_HOOK_LIST_SIZE 4
#define IDLE_THREAD_STACK_SIZE 256
#define RT_USING_TIMER_SOFT
#define RT_TIMER_THREAD_PRIO 4
#define RT_TIMER_THREAD_STACK_SIZE 512
#define RT_DEBUG
#define RT_DEBUG_COLOR

系统滴答定时器中断处理函数(每1ms触发一次systick定时器中断)。

启动文件位置
在这里插入图片描述
中断向量表

g_pfnVectors:

  .word _estack
  .word Reset_Handler
  .word NMI_Handler
  .word HardFault_Handler
  .word MemManage_Handler
  .word BusFault_Handler
  .word UsageFault_Handler
  .word 0
  .word 0
  .word 0
  .word 0
  .word SVC_Handler
  .word DebugMon_Handler
  .word 0
  .word PendSV_Handler
  .word SysTick_Handler
  .word WWDG_IRQHandler
  .word PVD_IRQHandler
  .word TAMPER_IRQHandler
  .word RTC_IRQHandler
  .word FLASH_IRQHandler
  .word RCC_IRQHandler

#ifdef RT_USING_SMP	//多核cpu宏
#define rt_tick rt_cpu_index(0)->tick
#else
//全局变量  1ms ++
static rt_tick_t rt_tick = 0;
#endif

/**
 * This is the timer interrupt service routine.
 *
 */
void SysTick_Handler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    HAL_IncTick();
    rt_tick_increase();

    /* leave interrupt */
    rt_interrupt_leave();
}

//跳转到rt_tick_increase();

/**
 * This function will notify kernel there is one tick passed. Normally,
 * this function is invoked by clock ISR.
 */
void rt_tick_increase(void)
{
    struct rt_thread *thread;

    /* increase the global tick */
#ifdef RT_USING_SMP
    rt_cpu_self()->tick ++;
#else
    ++ rt_tick;
#endif

    /* check time slice */
    thread = rt_thread_self();

    -- thread->remaining_tick;
    if (thread->remaining_tick == 0)
    {
        /* change to initialized tick */
        thread->remaining_tick = thread->init_tick;

        thread->stat |= RT_THREAD_STAT_YIELD;

        rt_schedule();
    }

    /* check timer */
    rt_timer_check();
}


获取系统节拍

/**@{*/
//时钟节拍过去函数
/**
 * This function will return current tick from operating system startup
 *
 * @return current tick
 */
rt_tick_t rt_tick_get(void)
{
    /* return the global tick */
    return rt_tick;
}

使用方法

int main(void)
{
	 rt_tick_t tick;

    while (1)
    {
        tick = rt_tick_get();
        LOG_D("tick:%d\n",tick);
        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}

数据打印

[D/main] tick:1952281

[D/main] tick:1953282

[D/main] tick:1954283

[D/main] tick:1955284

[D/main] tick:1956285

[D/main] tick:1957286

[D/main] tick:1958287

[D/main] tick:1959288

[D/main] tick:1960289

[D/main] tick:1961290

[D/main] tick:1962291

[D/main] tick:1963292

[D/main] tick:1964293

[D/main] tick:1965294

[D/main] tick:1966295

[D/main] tick:1967296

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值