查看: 297|回复: 1

[原创] S32DS SDK使用ftm_pwm的FTM_DRV_Init函数的一个注意事项

[复制链接]

该用户从未签到

2

主题

2

帖子

0

新手上路

Rank: 1

积分
45
最后登录
2023-12-5
发表于 2023-12-5 09:34:17 | 显示全部楼层 |阅读模式
使用sdk中的ftm_pwm时,我们肯定需要先初始化ftm,即调用FTM_DRV_Init函数。这个函数需传入三个参数,其中state为输出驱动的状态的结构体。
如果不出意外的话,我们后面调用pwm相关的其他函数并不需要用到这个输出的结构体,所以我们很容易把他忽略。
但是,这个state结构体参数,在我们使用pwm的周期内,必须保证其保持在内存中(定义为静态变量).否则,在调用其他pwm函数时,可能会出错。

  1. <div>/*!</div><div> * @brief Initializes the FTM driver.</div><div> *</div><div> * @param[in] instance The FTM peripheral instance number.</div><div> * @param[in] info The FTM user configuration structure, see #ftm_user_config_t.</div><div> * @param[out] state The FTM state structure of the driver.</div><div> * @return operation status</div><div> *        - STATUS_SUCCESS : Completed successfully.</div><div> *        - STATUS_ERROR : Error occurred.</div><div> */</div><div>status_t FTM_DRV_Init(uint32_t instance,</div><div>                      const ftm_user_config_t * info,</div><div>                      ftm_state_t * state);</div>
复制代码




我就是因为在开始时,只在初始化函数调用位置定义了一个局部变量,然后程序执行其他任务后,再执行pwm占空比调整函数时,出现无法调整问题。最初是以为其他功能和pwm有冲突,找了好久问题。
最后发现,sdk源码中,其有个ftmStatePtr集合,是直接保存了我们传入的结构体的指针。
  1. status_t FTM_DRV_Init(uint32_t instance,
  2.                       const ftm_user_config_t * info,
  3.                       ftm_state_t * state)
  4. {
  5. ...
复制代码




当调用FTM_DRV_UpdatePwmChannel函数更新占空比时,会从ftmStatePtr集合中取出我们前面传入的指针,获取ftm的状态。如果此时指针指向的位置已经被覆盖,那状态就会出错。
  1. status_t FTM_DRV_UpdatePwmChannel(uint32_t instance,
  2.                                   uint8_t channel,
  3.                                   ftm_pwm_update_option_t typeOfUpdate,
  4.                                   uint16_t firstEdge,
  5.                                   uint16_t secondEdge,
  6.                                   bool softwareTrigger)
  7. {
  8. ...
  9. <font color="#ff0000">    ftm_state_t * state = ftmStatePtr[instance];</font>
  10.     status_t retStatus = STATUS_SUCCESS;

  11.     /* Get the newest period in the MOD register */
  12.     ftmPeriod = FTM_DRV_GetMod(ftmBase);
  13.     /* Check the mode operation in FTM module */
  14. <font color="#ff0000">    if (state->ftmMode == FTM_MODE_CEN_ALIGNED_PWM)
  15.     {
  16.         ftmPeriod = (uint16_t)(ftmPeriod << 1U);
  17.     }
  18.     else if (state->ftmMode == FTM_MODE_EDGE_ALIGNED_PWM)
  19.     {
  20.         ftmPeriod = (uint16_t)(ftmPeriod + 1U);
  21.     }
  22.     else
  23.     {
  24.         retStatus = STATUS_ERROR;
  25.     }</font>
复制代码




回复

使用道具 举报

该用户从未签到

0

主题

1

帖子

0

注册会员

Rank: 2

积分
55
最后登录
2024-4-22
发表于 2023-12-13 21:19:06 | 显示全部楼层
为什么我把这个state结构体设为static变量还是不能更新占空比呢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /4 下一条

Archiver|手机版|小黑屋|恩智浦技术社区

GMT+8, 2024-4-28 04:50 , Processed in 0.112123 second(s), 19 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

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