本文主要是通过迁移的思维,记录本人初次使用NXP MCUXpresso SDK API进行BSP开发
本文主要是描述基于FreeRTOS系统下,定时器的接口封装代码实现。 hal_timer_freertos.c,hal_timer_freertos.h。 来源于qcloud-iot-explorer-sdk-embedded-c 定时器代码接口
1. hal_timer_freertos.c 内容
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fsl_common.h"
#include "fsl_debug_console.h"
#include "hal_timer_freertos.h"
uint32_t HAL_GetTimeMs(void)
{
return xTaskGetTickCount();
}
long HAL_Timer_current_sec(void)
{
return HAL_GetTimeMs() / 1000;
}
char *HAL_Timer_current(char *time_str)
{
long time_sec;
time_sec = HAL_Timer_current_sec();
memset(time_str, 0, TIME_FORMAT_STR_LEN);
snprintf(time_str, TIME_FORMAT_STR_LEN, "%ld", time_sec);
return time_str;
}
bool HAL_Timer_expired(Timer *timer)
{
uint32_t now_ts;
now_ts = HAL_GetTimeMs();
return (now_ts > timer->end_time) ? true : false;
}
void HAL_Timer_countdown_ms(Timer *timer, unsigned int timeout_ms)
{
timer->end_time = HAL_GetTimeMs();
timer->end_time += timeout_ms;
}
void HAL_Timer_countdown(Timer *timer, unsigned int timeout)
{
timer->end_time = HAL_GetTimeMs();
timer->end_time += timeout * 1000;
}
int HAL_Timer_remain(Timer *timer)
{
return (int)(timer->end_time - HAL_GetTimeMs());
}
void HAL_Timer_init(Timer *timer)
{
timer->end_time = 0;
}
2. hal_timer_freertos.h 内容
#ifndef __HAL_TIMER_FREERTOS_H__
#define __HAL_TIMER_FREERTOS_H__
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Timer {
uint32_t end_time;
};
typedef struct Timer Timer;
bool HAL_Timer_expired(Timer *timer);
void HAL_Timer_countdown_ms(Timer *timer, unsigned int timeout_ms);
void HAL_Timer_countdown(Timer *timer, unsigned int timeout);
int HAL_Timer_remain(Timer *timer);
void HAL_Timer_init(Timer *timer);
#define TIME_FORMAT_STR_LEN (20)
char *HAL_Timer_current(char *time_str);
long HAL_Timer_current_sec(void);
#endif
3. 总结
初次看到鹅厂的定时器接口,觉得不错,仅供各位参考学习。
|