本帖最后由 海洋无限 于 2022-2-9 13:37 编辑
#技术资源# @21小跑堂 @21小管家
之前拿到板子已经上电进行了测试,下载程序这部分还真是费了点事,好在最后直接使用keil 的CMSIS DAP Debugger完成了代码的下载,至于其他下载方式我没有认真去研究,感兴趣的小伙伴可以研究下,share出来。 下面我先在板子上移植下Timer为后面实现RTOS做准备,Timer是个很简单的调度器,创建timer后可以周期调度任务,timer实现简单且占用资源极少,但使用却非常方便,工作很多年简单的项目一直使用这个,好用。修改开箱测试的LED例子,加入timer部分,实现LED闪烁,实现主要有下面几个函数
- void timer_init(struct Timer* handle, void(*timeout_cb)(void), uint32_t timeout, uint32_t repeat);
- int timer_start(struct Timer* handle);
- void timer_stop(struct Timer* handle);
- void timer_ticks(void);
- void timer_loop(void);
- void timer_task_init(void);
在滴答定时器Tick中实现timer_ticks的调度,代码如下
- void SysTick_Handler(void)
- {
- timer_ticks();
- }
在while循环中实现timer_loop调度,代码如下
- while (1)
- {
- timer_loop();
- }
timer_init函数实现如下
- <blockquote>void timer_init(struct Timer* handle, void(*timeout_cb)(void), uint32_t timeout, uint32_t repeat)
timer_start实现如下
- int timer_start(struct Timer* handle)
- {
- struct Timer* target = head_handle;
- while(target) {
- if(target == handle) return -1; //already exist.
- target = target->next;
- }
- handle->next = head_handle;
- head_handle = handle;
- return 0;
- }
timer_stop实现如下
- void timer_stop(struct Timer* handle)
- {
- struct Timer** curr;
- for(curr = &head_handle; *curr; ) {
- struct Timer* entry = *curr;
- if (entry == handle) {
- *curr = entry->next;
- } else
- curr = &entry->next;
- }
- }
timer_loop实现如下
- void timer_loop(void)
- {
- struct Timer* target;
- for(target=head_handle; target; target=target->next) {
- if(_timer_ticks >= target->timeout) {
- if(target->repeat == 0) {
- timer_stop(target);
- } else {
- target->timeout = _timer_ticks + target->repeat;
- }
- target->timeout_cb();
- }
- }
- }
最后自定义个timer_task_init函数,实现你项目中要实现的周期调度的几种定时器(类似OS中的任务,根据自己需要实现即可),这里实现500ms调度闪LED,其他的暂时没用到
- void timer_task_init(void)
- {
- // timer_init(&timer5ms, timer_5ms_callback, TIMER_NORMAL_DELAY, TIMER_5MS_PRELOAD); //5ms loop
- // timer_start(&timer5ms);
- // timer_init(&timer100ms, timer_100ms_callback, TIMER_NORMAL_DELAY, TIMER_100MS_PRELOAD); //100ms loop
- // timer_start(&timer100ms);
- timer_init(&timer500ms, timer_500ms_callback, TIMER_NORMAL_DELAY, TIMER_500MS_PRELOAD); //500ms loop
- timer_start(&timer500ms);
-
- // timer_init(&timer1s, timer_1s_callback, TIMER_NORMAL_DELAY, TIMER_1S_PRELOAD); //1s delay
- // timer_start(&timer1s);
- }
timer_500ms_callback实现如下,完成LED的周期闪烁
- void timer_500ms_callback()
- {
- if(1 == flag) {
- flag = 0;
- LedOn(GPIOB, GPIO_PIN_4);
- LedOn(GPIOB, GPIO_PIN_5);
- LedOn(GPIOA, GPIO_PIN_8);
- } else {
- flag = 1;
- LedOff(GPIOB, GPIO_PIN_4);
- LedOff(GPIOB, GPIO_PIN_5);
- LedOff(GPIOA, GPIO_PIN_8);
- }
- }
build后下载到开发板,可以看到LED周期性闪烁,符合预期的效果。
|