一、从我以前的贴子有给大家分享过软件定时器,刚好无聊再优化一下。二、
首先介绍一个unsigned int这个类型,他是一个无符号16bit(32bit)类型,为什么是16bit又说32bit呢?因为在不同的cpu系统中,他可以是2个字节,或者是4个字节。所以在使用的时候,我们要注意一个问题:
例如:
- unsigned int var = 0x1234;
- var = var << 16 >> 16;
复制代码 如果对于32bit来说,var = 0x1234, 然而对于16bit来说,var = 0, 所以,在实际应用中,我们更倾向于使用以下类型
- typedef signed char int8_t;
- typedef unsigned char uint8_t;
- typedef signed short int16_t;
- typedef unsigned short uint16_t;
- typedef signed long int32_t;
- typedef unsigned long uint32_t;
复制代码 这是第一点,另外还有一点,就是无符号变量的一个回环特性。
对一个unsigned 类型的变量进行++操作, 当变量溢出后会返回0。利用这个特性,我们可以在一些特殊应用程序设计中利用他的回环特性而不需要在后面增加一溢出判断,以减少代码量。我一直沿用很久的软件定时器,就是用了这一回环特性。
软件定时器,从始至终都是围绕一个tick变量操作,理所当然,这个tick将会在一个硬件定时器上进行++操作来确定tick的时基间隔。
所以,软件定时器里有一个全局变量
在提供api方面,应该提供以下操作:
1、初始化软件定时器
2、创建一个软件定时器
3、让一个软件定时器开始工作
4、让一个软件定时器停止工作
5、删除一个软件定时器
在这里,我们使用一个内部堆的概念来把可以提供的软件定时器个数集中在一起:
- struct ifs_stmr_t {
- ifs_uint32_t timer_count;
- ifs_uint32_t time_out;
- void (*time_out_callback)(void *param);
- void *param;
- ifs_uint8_t timer_flag;
- struct option {
- ifs_uint8_t is_active : 1;
- ifs_uint8_t is_alive : 1;
- ifs_uint8_t nc : 6;
- }opt;
- } timer_list[SOFT_TIMERS_MAX];
复制代码 下面是每一个函数的具体代码:
- void ifs_stmr_init(void)
- {
- ifs_uint32_t i;
- for (i = 0; i < SOFT_TIMERS_MAX; i++) {
- timer_list[i].timer_count = 0;
- timer_list[i].timer_flag = 0;
- timer_list[i].time_out = 0;
- timer_list[i].time_out_callback = NULL;
- timer_list[i].param = NULL;
- timer_list[i].opt.is_active = 0;
- timer_list[i].opt.is_alive = 0;
- }
- stmr_ticks = 0;
- }
复制代码- ifs_stmr_handle ifs_stmr_registered(ifs_uint32_t period,
- timer_out_callback *callback,
- void *callback_param,
- ifs_uint8_t flag)
- {
- int i;
-
- if (callback == NULL) return -1;
-
- for (i = 0; i < SOFT_TIMERS_MAX; i++) {
- if (timer_list[i].opt.is_alive) continue;
- timer_list[i].opt.is_alive = 1;
- timer_list[i].opt.is_active = 0;
- timer_list[i].time_out = period;
- timer_list[i].time_out_callback = callback;
- timer_list[i].param = callback_param;
- timer_list[i].timer_flag = flag;
- return i;
- }
-
- return -1;
- }
复制代码
- int ifs_stmr_delete(ifs_stmr_handle handle)
- {
- int i = (int)handle;
-
- if (i >= SOFT_TIMERS_MAX || i < 0) return -1;
- timer_list[i].opt.is_alive = 0;
- timer_list[i].opt.is_active = 0;
- timer_list[i].timer_count = 0;
- timer_list[i].time_out = 0;
- timer_list[i].time_out_callback = NULL;
- timer_list[i].param = NULL;
- timer_list[i].timer_flag = 0;
- return 0;
- }
复制代码
- int ifs_stmr_start(ifs_stmr_handle handle)
- {
- int i = (int)handle;
-
- if (i >= SOFT_TIMERS_MAX || i < 0) return -1;
- if (!timer_list[i].opt.is_alive) return -1;
-
- if (timer_list[i].opt.is_active != 1) {
- timer_list[i].timer_count = stmr_ticks;
- timer_list[i].opt.is_active = 1;
- }
-
- return 0;
- }
复制代码
- int ifs_stmr_stop(ifs_stmr_handle handle)
- {
- int i = (int)handle;
-
- if (i >= SOFT_TIMERS_MAX || i < 0) return -1;
- if (!timer_list[i].opt.is_alive) return -1;
-
- if (timer_list[i].opt.is_active != 0)
- timer_list[i].opt.is_active = 0;
-
- return 0;
- }
复制代码
- void ifs_stmr_interrupt(void)
- {
- stmr_ticks ++;
- }
- /*
- * !brief soft timer task
- */
- int ifs_stmr_task(void)
- {
- ifs_uint8_t i, ret = 0;
-
- for (i = 0; i < SOFT_TIMERS_MAX; i ++) {
- if ((timer_list[i].opt.is_alive == 0) ||
- (timer_list[i].opt.is_active == 0)) {
- continue;
-
- }
-
- if ((stmr_ticks - timer_list[i].timer_count) >= timer_list[i].time_out) {
- TMR_CALLBACK(timer_list[i]);
- if (timer_list[i].timer_flag == IFS_STMR_FLAG_PERIOD) {
- timer_list[i].timer_count = stmr_ticks;
- } else if (timer_list[i].timer_flag == IFS_STMR_FLAG_ONESHOT) { //one shot
- timer_list[i].opt.is_active = 0;
- } else {
- timer_list[i].opt.is_alive = 0;
- timer_list[i].opt.is_active = 0;
- }
- }
- ret++;
- }
- return ret;
- }
复制代码
|
https://www.stmcu.org.cn/module/forum/thread-614550-1-1.html
还有一些定义没有给出。
这么小得你都看出了,看来时有认真看得呀,哈哈哈哈
定义全给出了,有啥问题可以说出来一起讨论
嗯嗯,有啥好意见尽管提
IFS_STMR_FLAG_PERIOD
IFS_STMR_FLAG_ONESHOT
这几个没有定义