PWM

PWM通道初始化顺序

发布于 2022-05-24 11:28:02

SDK: wm-sdk-w806
电路板:合宙AIR103 demo板

SDK中默认的app,包括有初始化PWM功能,考虑更换到板子对应的LED灯PB24/PB25/PB26,对应PWM2/PWM3/PWM4。
测试时发现PWM初始化有顺序要求,需要按照PWM4>PWM3>PWM2顺序执行【初始化+启动】方可,否则会导致PWM2不工作。

# PWM2不工作
PWM_Init(&pwm[0], PWM_CHANNEL_2);
HAL_PWM_Start(&pwm[0]);
PWM_Init(&pwm[1], PWM_CHANNEL_3);
HAL_PWM_Start(&pwm[1]);
PWM_Init(&pwm[2], PWM_CHANNEL_4);
HAL_PWM_Start(&pwm[2]);

# PWM2/3/4工作正常
PWM_Init(&pwm[0], PWM_CHANNEL_4);
HAL_PWM_Start(&pwm[0]);
PWM_Init(&pwm[1], PWM_CHANNEL_3);
HAL_PWM_Start(&pwm[1]);
PWM_Init(&pwm[2], PWM_CHANNEL_2);
HAL_PWM_Start(&pwm[2]);

# PWM2/3/4工作正常
PWM_Init(&pwm[0], PWM_CHANNEL_2);
PWM_Init(&pwm[1], PWM_CHANNEL_3);
PWM_Init(&pwm[2], PWM_CHANNEL_4);
HAL_PWM_Start(&pwm[0]);
HAL_PWM_Start(&pwm[1]);
HAL_PWM_Start(&pwm[2]);

参考原例程中,也是按照PWM2>PWM1>PWM0顺序初始化的。
是否其中有特殊的缘由?

for (i = 2; i >= 0; i--)
{
    PWM_Init(&pwm[i], PWM_CHANNEL_0 + i);
    HAL_PWM_Start(&pwm[i]);
}

查看更多

关注者
0
被浏览
81
2 个回答
kicer
kicer 2022-05-25
This guy hasn't written anything yet

是设置的独立通道。

排查发现是SDK中__PWM_CountType_Config函数的问题。
CR寄存器中CNTTYPEx移位错误,导致CNTEN被覆盖,patch如下:

diff --git a/platform/drivers/wm_pwm.c b/platform/drivers/wm_pwm.c
index 7674828d3c020f718b0f71b67eb3ce29fd3f8e92..a88d3e77b1241dccd118d9ce3d64d6ab5f157c72 100644
--- a/platform/drivers/wm_pwm.c
+++ b/platform/drivers/wm_pwm.c
@@ -70,7 +70,7 @@ HAL_StatusTypeDef __PWM_CountType_Config(PWM_HandleTypeDef *hpwm, uint32_t Chann
     }
     else
     {
-        MODIFY_REG(hpwm->Instance->CR, (0x0FF << (PWM_CR_CNTTYPE0_Pos + Channel * 2)), 
+        MODIFY_REG(hpwm->Instance->CR, (0x03 << (PWM_CR_CNTTYPE0_Pos + Channel * 2)), 
                     (CounterMode << (PWM_CR_CNTTYPE0_Pos + Channel * 2)));
     }
Mrzhao
Mrzhao 认证专家 2022-05-24
一个嵌入式爱好者。CSDN主页:https://blog.csdn.net/qq_37280428?spm=1011.2415.3001.5343

是否设置:hpwm.Init.OutMode = PWM_OUT_MODE_INDEPENDENT; //独立通道输出

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览