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 的中断
  1. void pwm_init(void)
  2. {
  3.     // mcpwm timer
  4.     //定义变量 timer
  5.     mcpwm_timer_handle_t timer;
  6.     //定义操作指针 后面的 gen 和 cmp 都是绑定到操作指针上面
  7.     mcpwm_oper_handle_t opt;
  8.     //定义比较器指针
  9.     mcpwm_cmpr_handle_t cmpa;
  10.     mcpwm_cmpr_handle_t cmpb;
  11.     //定义generator 指针
  12.     mcpwm_gen_handle_t  gena;
  13.     mcpwm_gen_handle_t  genb;
  14.  
  15.     //配置定时器计算基数和周期
  16.     mcpwm_timer_config_t timer_config = {
  17.         .group_id = 0,
  18.         .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
  19.         .resolution_hz = 1000000,
  20.         .period_ticks = 100,
  21.         .count_mode = MCPWM_TIMER_COUNT_MODE_UP,
  22.     };
  23.     //配置操作指针绑定的分组,编写和timer的分组一致
  24.     mcpwm_operator_config_t operator_config = {
  25.         .group_id = 0,
  26.     };
  27.     //定义比较器配置,在0点更新
  28.     mcpwm_comparator_config_t comparator_config = {
  29.         .flags.update_cmp_on_tez = true,
  30.     };
  31.  
  32.     //配置生成器绑定的引脚
  33.     mcpwm_generator_config_t generator_config = {};
  34.  
  35.     //根据配置 初始化定时器
  36.     mcpwm_new_timer(&timer_config, &timer);
  37.     //根据配置 初始化 操作指针
  38.     mcpwm_new_operator(&operator_config, &opt);
  39.     //绑定操作指针和定时器
  40.     mcpwm_operator_connect_timer(opt,timer);
  41.  
  42.     //when the cmp value to update,绑定比较指针到操作器,并且指明 0时 更新
  43.     mcpwm_new_comparator(opt, &comparator_config, &cmpa);       //set the first cmp
  44.     mcpwm_new_comparator(opt, &comparator_config, &cmpb);       //set the second cmp
  45.  
  46.     //set the pwm pin 设定pwm的io
  47.     generator_config.gen_gpio_num=1;
  48.     mcpwm_new_generator(opt, &generator_config, &gena);
  49.     generator_config.gen_gpio_num=2;
  50.     mcpwm_new_generator(opt, &generator_config, &genb);
  51.  
  52.     //set the event when empty or compare , 设定pwm定时器归零时候电平,比较后电平
  53.     mcpwm_generator_set_actions_on_timer_event(gena,
  54.             MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH),
  55.             MCPWM_GEN_TIMER_EVENT_ACTION_END());
  56.     mcpwm_generator_set_actions_on_compare_event(gena,
  57.             MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, cmpa, MCPWM_GEN_ACTION_LOW),
  58.             MCPWM_GEN_COMPARE_EVENT_ACTION_END());
  59.     mcpwm_generator_set_actions_on_timer_event(genb,
  60.             MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_LOW),
  61.             MCPWM_GEN_TIMER_EVENT_ACTION_END());
  62.     mcpwm_generator_set_actions_on_compare_event(genb,
  63.             MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, cmpb, MCPWM_GEN_ACTION_HIGH),
  64.             MCPWM_GEN_COMPARE_EVENT_ACTION_END());
  65.     //使能定时器
  66.     mcpwm_timer_enable(timer);
  67.     //启动定时器
  68.     mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP);
  69.  
  70.     //设定pwm 数值
  71.     mcpwm_comparator_set_compare_value(cmpa,50);
  72.     mcpwm_comparator_set_compare_value(cmpb,20);
  73. }

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

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

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

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