Page 1 of 1

esp32s3的mcpwm的疑问,线程安全吗?怎么

Posted: Thu Apr 13, 2023 6:07 am
by QbasicJacky
1.官方驱动函数 里面调用了 heap_caps_calloc 创建各种结构体,heap_caps_calloc 函数线程安全吗?
2.mcpwm 有没有中断 例程,当pwm update 的中断 cmp 的中断

Code: Untitled.c Select all


void pwm_init(void)
{
// mcpwm timer
//定义变量 timer
mcpwm_timer_handle_t timer;
//定义操作指针 后面的 gen 和 cmp 都是绑定到操作指针上面
mcpwm_oper_handle_t opt;
//定义比较器指针
mcpwm_cmpr_handle_t cmpa;
mcpwm_cmpr_handle_t cmpb;
//定义generator 指针
mcpwm_gen_handle_t gena;
mcpwm_gen_handle_t genb;

//配置定时器计算基数和周期
mcpwm_timer_config_t timer_config = {
.group_id = 0,
.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
.resolution_hz = 1000000,
.period_ticks = 100,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP,
};
//配置操作指针绑定的分组,编写和timer的分组一致
mcpwm_operator_config_t operator_config = {
.group_id = 0,
};
//定义比较器配置,在0点更新
mcpwm_comparator_config_t comparator_config = {
.flags.update_cmp_on_tez = true,
};

//配置生成器绑定的引脚
mcpwm_generator_config_t generator_config = {};

//根据配置 初始化定时器
mcpwm_new_timer(&timer_config, &timer);
//根据配置 初始化 操作指针
mcpwm_new_operator(&operator_config, &opt);
//绑定操作指针和定时器
mcpwm_operator_connect_timer(opt,timer);

//when the cmp value to update,绑定比较指针到操作器,并且指明 0时 更新
mcpwm_new_comparator(opt, &comparator_config, &cmpa); //set the first cmp
mcpwm_new_comparator(opt, &comparator_config, &cmpb); //set the second cmp

//set the pwm pin 设定pwm的io
generator_config.gen_gpio_num=1;
mcpwm_new_generator(opt, &generator_config, &gena);
generator_config.gen_gpio_num=2;
mcpwm_new_generator(opt, &generator_config, &genb);

//set the event when empty or compare , 设定pwm定时器归零时候电平,比较后电平
mcpwm_generator_set_actions_on_timer_event(gena,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH),
MCPWM_GEN_TIMER_EVENT_ACTION_END());
mcpwm_generator_set_actions_on_compare_event(gena,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, cmpa, MCPWM_GEN_ACTION_LOW),
MCPWM_GEN_COMPARE_EVENT_ACTION_END());
mcpwm_generator_set_actions_on_timer_event(genb,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_LOW),
MCPWM_GEN_TIMER_EVENT_ACTION_END());
mcpwm_generator_set_actions_on_compare_event(genb,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, cmpb, MCPWM_GEN_ACTION_HIGH),
MCPWM_GEN_COMPARE_EVENT_ACTION_END());
//使能定时器
mcpwm_timer_enable(timer);
//启动定时器
mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP);

//设定pwm 数值
mcpwm_comparator_set_compare_value(cmpa,50);
mcpwm_comparator_set_compare_value(cmpb,20);
}

Re: esp32s3的mcpwm的疑问,线程安全吗?怎么

Posted: Mon Apr 17, 2023 9:12 am
by xien551
我用过esp32的idf5.1mcpwm,和wifi,mqtt一起用的。

程序不会崩。但是我从示波器能看到mcpwm产生的spwm的正弦波形会在其它线程工作时轻微畸变。如果我删除wifi和mqtt的功能模块,波形一直完整。

所以我猜测新版本的mcpwm线程应该是安全的,但是实时性可能会受影响。