zoukankan      html  css  js  c++  java
  • 嵌入式实操----基于RT1170 FreeRTOS时定器接口封装(二十二)

    本文主要是通过迁移的思维,记录本人初次使用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 内容

     /*Notes: !!!! HAL_Timer_ func  reference qcloud-iot-explorer-sdk-embedded-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"
    /*----------------------------------------------*
     * external variables                           *
     *----------------------------------------------*/
    
    /*----------------------------------------------*
     * external routine prototypes                  *
     *----------------------------------------------*/
    
    /*----------------------------------------------*
     * internal routine prototypes                  *
     *----------------------------------------------*/
    
    /*----------------------------------------------*
     * project-wide global variables                *
     *----------------------------------------------*/
    
    /*----------------------------------------------*
     * module-wide global variables                 *
     *----------------------------------------------*/
    
    /*----------------------------------------------*
     * constants                                    *
     *----------------------------------------------*/
    
    /*----------------------------------------------*
     * macros                                       *
     *----------------------------------------------*/
    
    /*----------------------------------------------*
     * routines' implementations                    *
     *----------------------------------------------*/
    uint32_t HAL_GetTimeMs(void)
    {
        return xTaskGetTickCount();
    }
    
    /*Get timestamp*/
    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>
    
    /**
    #endif  // __HAL_TIMER_FREERTOS_H__
    
     * Define timer structure, platform dependant
     */
    struct Timer {
        uint32_t end_time;
    };
    
    typedef struct Timer Timer;
    
    /**
     * @brief Check if timer expires or not
     *
     * @param timer     reference to timer
     * @return          true = expired, false = not expired yet
     */
    bool HAL_Timer_expired(Timer *timer);
    
    /**
     * @brief Set the countdown/expired value for the timer
     *
     * @param timer         reference to timer
     * @param timeout_ms    countdown/expired value (unit: millisecond)
     */
    void HAL_Timer_countdown_ms(Timer *timer, unsigned int timeout_ms);
    
    /**
     * @brief Set the countdown/expired value for the timer
     *
     * @param timer         reference to timer
     * @param timeout       countdown/expired value (unit: second)
     */
    void HAL_Timer_countdown(Timer *timer, unsigned int timeout);
    
    /**
     * @brief Check the remain time of the timer
     *
     * @param timer     reference to timer
     * @return          0 if expired, or the left time in millisecond
     */
    int HAL_Timer_remain(Timer *timer);
    
    /**
     * @brief Init the timer
     *
     * @param timer reference to timer
     */
    void HAL_Timer_init(Timer *timer);
    
    #define TIME_FORMAT_STR_LEN (20)
    /**
     * @brief Get local time in format: %y-%m-%d %H:%M:%S
     *
     * @return string of formatted time
     */
    char *HAL_Timer_current(char *time_str);
    
    /**
     * @brief Get timestamp in second
     *
     * @return   timestamp in second
     */
    long HAL_Timer_current_sec(void);
    
    #endif
    

    3. 总结

    初次看到鹅厂的定时器接口,觉得不错,仅供各位参考学习。

  • 相关阅读:
    FreeCAD二次开发-界面交互创建块工具
    vscode中使用git将自己的代码提交到码云
    js获取单页面参数(正则表达式)
    小程序的wx.request的封装
    前端实现截屏处理
    二维数组转化为json数组
    css更改滚动条样式
    盒子没有高度时填充背景图片
    页面初次渲染loading图
    layui分页的使用心得
  • 原文地址:https://www.cnblogs.com/lianghong881018/p/15169603.html
Copyright © 2011-2022 走看看