硬汉嵌入式论坛

 找回密码
 立即注册
查看: 271|回复: 2
收起左侧

[FreeRTOS] cmsis osV2 创建定时器,为啥默认是1个tick呢?

[复制链接]

14

主题

49

回帖

91

积分

初级会员

积分
91
发表于 2024-5-16 15:51:39 | 显示全部楼层 |阅读模式
osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
  const char *name;
  TimerHandle_t hTimer;
  TimerCallback_t *callb;
  UBaseType_t reload;
  int32_t mem;

  hTimer = NULL;

  if (!IS_IRQ() && (func != NULL)) {
    /* Allocate memory to store callback function and argument */
    callb = pvPortMalloc (sizeof(TimerCallback_t));

    if (callb != NULL) {
      callb->func = func;
      callb->arg  = argument;

      if (type == osTimerOnce) {
        reload = pdFALSE;
      } else {
        reload = pdTRUE;
      }

      mem  = -1;
      name = NULL;

      if (attr != NULL) {
        if (attr->name != NULL) {
          name = attr->name;
        }

        if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTimer_t))) {
          mem = 1;
        }
        else {
          if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
            mem = 0;
          }
        }
      }
      else {
        mem = 0;
      }

      if (mem == 1) {
        hTimer = xTimerCreateStatic (name, 1, reload, callb, TimerCallback, (StaticTimer_t *)attr->cb_mem);
      }
      else {
        if (mem == 0) {
          hTimer = xTimerCreate (name, 1, reload, callb, TimerCallback);
        }
      }
    }
  }

  return ((osTimerId_t)hTimer);
}






TimerHandle_t xTimerCreate(        const char * const pcTimerName,                        /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
                                                                const TickType_t xTimerPeriodInTicks,
                                                                const UBaseType_t uxAutoReload,
                                                                void * const pvTimerID,
                                                                TimerCallbackFunction_t pxCallbackFunction )


TimerHandle_t xTimerCreateStatic(        const char * const pcTimerName,                /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
                                                                                const TickType_t xTimerPeriodInTicks,
                                                                                const UBaseType_t uxAutoReload,
                                                                                void * const pvTimerID,
                                                                                TimerCallbackFunction_t pxCallbackFunction,
                                                                                StaticTimer_t *pxTimerBuffer )






发现cmsis osV2  创建定时器,为啥默认是1个tick呢,调用start时才会设置成用户想要的时间。
如果我程序初始化时候创建了一个定时器,但是暂时不想start,那不是一直在1ms调用用户的函数?这个会不会有问题呢?


osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
  TimerHandle_t hTimer = (TimerHandle_t)timer_id;
  osStatus_t stat;

  if (IS_IRQ()) {
    stat = osErrorISR;
  }
  else if (hTimer == NULL) {
    stat = osErrorParameter;
  }
  else {
    if (xTimerChangePeriod (hTimer, ticks, 0) == pdPASS) {
      stat = osOK;
    } else {
      stat = osErrorResource;
    }
  }

  return (stat);
}


回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107888
QQ
发表于 2024-5-17 09:24:05 | 显示全部楼层
确认下

The function osTimerNew creates an one-shot or periodic timer and associates it with a callback function with argument. The timer is in stopped state until it is started with osTimerStart.
回复

使用道具 举报

14

主题

49

回帖

91

积分

初级会员

积分
91
 楼主| 发表于 2024-5-17 10:57:38 | 显示全部楼层

是的,创建完成时,定时器是stop状态,freeRTOS是有一个专门的start接口的,但是cmsis 封装时候不知道怎么考虑的,没有调用start,而是创建时候先设置1tick,再使用changeperiod去start
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-6-17 12:29 , Processed in 0.274319 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表