[N32G43x] 在板子上实现Timer调度

[复制链接]
 楼主| 海洋无限 发表于 2022-2-4 17:53 | 显示全部楼层 |阅读模式
本帖最后由 海洋无限 于 2022-2-9 13:37 编辑

#技术资源# @21小跑堂 @21小管家

      之前拿到板子已经上电进行了测试,下载程序这部分还真是费了点事,好在最后直接使用keil 的CMSIS DAP Debugger完成了代码的下载,至于其他下载方式我没有认真去研究,感兴趣的小伙伴可以研究下,share出来。      下面我先在板子上移植下Timer为后面实现RTOS做准备,Timer是个很简单的调度器,创建timer后可以周期调度任务,timer实现简单且占用资源极少,但使用却非常方便,工作很多年简单的项目一直使用这个,好用。修改开箱测试的LED例子,加入timer部分,实现LED闪烁,实现主要有下面几个函数
  1. void timer_init(struct Timer* handle, void(*timeout_cb)(void), uint32_t timeout, uint32_t repeat);
  2. int  timer_start(struct Timer* handle);
  3. void timer_stop(struct Timer* handle);
  4. void timer_ticks(void);
  5. void timer_loop(void);
  6. void timer_task_init(void);
在滴答定时器Tick中实现timer_ticks的调度,代码如下
  1. void SysTick_Handler(void)
  2. {
  3.     timer_ticks();        
  4. }
在while循环中实现timer_loop调度,代码如下
  1. while (1)
  2. {
  3.     timer_loop();
  4. }
timer_init函数实现如下
  1. <blockquote>void timer_init(struct Timer* handle, void(*timeout_cb)(void), uint32_t timeout, uint32_t repeat)
timer_start实现如下
  1. int timer_start(struct Timer* handle)
  2. {
  3.         struct Timer* target = head_handle;
  4.         while(target) {
  5.                 if(target == handle) return -1;        //already exist.
  6.                 target = target->next;
  7.         }
  8.         handle->next = head_handle;
  9.         head_handle = handle;
  10.         return 0;
  11. }
timer_stop实现如下
  1. void timer_stop(struct Timer* handle)
  2. {
  3.         struct Timer** curr;
  4.         for(curr = &head_handle; *curr; ) {
  5.                 struct Timer* entry = *curr;
  6.                 if (entry == handle) {
  7.                         *curr = entry->next;
  8.                 } else
  9.                         curr = &entry->next;
  10.         }
  11. }
timer_loop实现如下
  1. void timer_loop(void)
  2. {
  3.         struct Timer* target;
  4.         for(target=head_handle; target; target=target->next) {
  5.                 if(_timer_ticks >= target->timeout) {
  6.                         if(target->repeat == 0) {
  7.                                 timer_stop(target);
  8.                         } else {
  9.                                 target->timeout = _timer_ticks + target->repeat;
  10.                         }
  11.                         target->timeout_cb();
  12.                 }
  13.         }
  14. }
最后自定义个timer_task_init函数,实现你项目中要实现的周期调度的几种定时器(类似OS中的任务,根据自己需要实现即可),这里实现500ms调度闪LED,其他的暂时没用到
  1. void timer_task_init(void)
  2. {
  3. //        timer_init(&timer5ms,     timer_5ms_callback,   TIMER_NORMAL_DELAY,   TIMER_5MS_PRELOAD); //5ms loop
  4. //        timer_start(&timer5ms);  

  5. //        timer_init(&timer100ms,   timer_100ms_callback, TIMER_NORMAL_DELAY,   TIMER_100MS_PRELOAD); //100ms loop
  6. //        timer_start(&timer100ms);

  7.         timer_init(&timer500ms,   timer_500ms_callback, TIMER_NORMAL_DELAY,   TIMER_500MS_PRELOAD); //500ms loop
  8.         timer_start(&timer500ms);
  9.         
  10. //        timer_init(&timer1s,      timer_1s_callback,    TIMER_NORMAL_DELAY,   TIMER_1S_PRELOAD); //1s delay
  11. //        timer_start(&timer1s);
  12. }
timer_500ms_callback实现如下,完成LED的周期闪烁
  1. void timer_500ms_callback()
  2. {
  3.                         if(1 == flag) {
  4.                                 flag = 0;
  5.                                 LedOn(GPIOB, GPIO_PIN_4);
  6.                                 LedOn(GPIOB, GPIO_PIN_5);
  7.                                 LedOn(GPIOA, GPIO_PIN_8);
  8.                         } else {
  9.                                 flag = 1;
  10.                                 LedOff(GPIOB, GPIO_PIN_4);
  11.                                 LedOff(GPIOB, GPIO_PIN_5);        
  12.         LedOff(GPIOA, GPIO_PIN_8);                                
  13.                         }
  14. }
build后下载到开发板,可以看到LED周期性闪烁,符合预期的效果。



caigang13 发表于 2022-2-5 15:10 来自手机 | 显示全部楼层
这个是啥OS
 楼主| 海洋无限 发表于 2022-2-5 21:33 | 显示全部楼层

只是个定时调度器,不是rtos,但是实际工作中很实用
七毛钱 发表于 2022-2-6 16:08 来自手机 | 显示全部楼层
看着挺实用的
N32MCU 发表于 2022-2-6 18:17 | 显示全部楼层
是个很实用的调度方法,大赞!!! 希望楼主提供完整的例程,方便大家用到自己的项目中。
koala889 发表于 2022-2-9 14:32 | 显示全部楼层
晒晒效果图啊~~
 楼主| 海洋无限 发表于 2022-2-10 12:24 | 显示全部楼层

就是LED闪烁,当时没拍,下次拍了传上来
yljon 发表于 2022-2-10 13:04 | 显示全部楼层
这个调度没有玩过,有时间玩一玩
iamaiqiyi 发表于 2022-2-13 09:38 | 显示全部楼层
为什么不使用RTT系统呢?     
selongli 发表于 2022-2-13 10:05 | 显示全部楼层
楼主准备是自己做ROS吗?
1988020566 发表于 2022-2-13 10:36 | 显示全部楼层
自己编写的操作系统吗?
beacherblack 发表于 2022-2-13 10:45 | 显示全部楼层
期待楼主提供一下完整的代码工程
typeof 发表于 2022-2-13 10:54 | 显示全部楼层
定时调度器是滴答时钟那个吗?
aspoke 发表于 2022-2-13 11:04 | 显示全部楼层
求个完整的文件,学习一下。     
内政奇才 发表于 2022-2-14 10:13 来自手机 | 显示全部楼层
如果有完整的工程代码就更好了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:永远忠于年轻时的梦想!

39

主题

539

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部