之前偶爾了解到軟件定時器,也在網上找了許多網友寫的各種版本,知道讀到嵌入式TCP/ip協議棧中的軟件定時器的寫法時,嘆為觀止,故將其修改移植到裸機stm32上使用,并向大家做一分享。
首先得感謝那些兢兢業業從事著自己的工作,并毫不吝嗇的向別人公布自己源代碼的前輩們,正因為他們慷慨無私,我們才能閱讀到各種精巧的代碼,并從中汲取養分,從而培養了許多熱愛技術,真誠無私的人。
/**@brief:1、該文件參考LWIP協議中的times.c編寫 * 2、該超時定時器按鏈表的形式進行組織,按時間長短進行排序,時間最短的永遠排在最前面 * 3、移植時應該注意的幾點: * a.周期性的調用函數。b.需要外部定時器實現時基。c.注意動態內存分配mallco的使用。 *@author: LT *@date: 2016/12/3 */#ifndef __SOFT_TIME_H#define __SOFT_TIME_H#include "typedef.h"#include <string.h>#include <stdlib.h>/* 定時器超時處理函數指針 */typedef void (* soft_timeout_handler)(void *arg);/* 超時定時器 */struct soft_timeo { struct soft_timeo *next; u32 time; soft_timeout_handler h; void *arg;};#define MEMP_soft_TIMEOUT (sizeof(struct soft_timeo))void soft_timeouts_init(u32 msecs, soft_timeout_handler handler, void *arg);void soft_timeout(u32 msecs, soft_timeout_handler handler, void *arg);void soft_untimeout(soft_timeout_handler handler, void *arg);void soft_check_timeouts(void);void soft_restart_timeouts(void);void soft_run_timeout(soft_timeout_handler handler, void *arg);u32 time_to_sec(char *RTC_set);u32 RTC_set(char* RTC_set);#endif#include "soft_time.h"#include "bsp.h"#include "malloc.h"extern volatile u32 g_soft_timer_count;static u32 timeouts_last_time; static struct soft_timeo *next_timeout;/*從字符串的左邊截取n個字符*/char * str_left(char *dst,const char *src, int n){ const char *p = src; char *q = dst; int len = strlen(src); if(n>len) n = len; while(n--) *(q++) = *(p++); *(q++)='/0'; /*有必要嗎?很有必要*/ return dst;}u32 soft_time_now(void){ return RTC_GetCounter();}/** @brief 計算預定時間距現在的時間(單位:s) * @param 預定時間轉成的字符串(例如:2016 12 03 18 30 00 錯誤) * ( 2016 12 3 18 30 0 正確) * @retval * @tips 注意strtol函數的使用 */u32 time_to_sec(char *RTC_set){ u32 set_sec;// u32 now_sec; u8 n; char *pEnd; n = accumulationdayfrom1970((int*)&set_sec, RTC_set); set_sec = set_sec * 3600 * 24 + (int)strtol(RTC_set + n,&pEnd,0) * 3600 ; set_sec = set_sec + (int)strtol(pEnd, &pEnd, 0) * 60; set_sec = set_sec + (int)strtol(pEnd, NULL, 0);/* RTC_Get_str(RTC_now); n = accumulationdayfrom1970((int*)&now_sec, RTC_now); now_sec = now_sec * 3600 * 24 + (int)strtol(RTC_now + n,&pEnd_,0) * 3600 ; now_sec = now_sec + (int)strtol(pEnd_, &pEnd_, 0) * 60; now_sec = now_sec + (int)strtol(pEnd_, NULL, 0);新聞熱點
疑難解答