MCPWM maximum timer frequency

riiized
Posts: 2
Joined: Tue Jun 13, 2023 1:11 pm

MCPWM maximum timer frequency

Postby riiized » Tue Jun 13, 2023 1:31 pm

Hi, ive been trying to implement the Dshot1200 protocol

(16-Bit Frames at 1200kbit/s,
≈625ns high + ≈208ns low -> 1,
≈313ns high + ≈620ns low -> 0)

using the MCPWM driver. Im wondering what frequency I can achieve.

Setting the timer configs .resolution_hz higher than 8MHz doesnt result in a faster tickrate, while only setting it higher than 80MHz leads to an error (clock divider not between 256 and 1 anymore).

The lowest stable tick duration (measured using a Logic Analyzer) was around 125ns (8MHz), sometimes 42,5ns but very unstable.


Source:

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/mcpwm_prelude.h"

static const char *TAG = "example";

#define SERVO_PULSE_GPIO 17
#define SERVO_TIMEBASE_RESOLUTION_HZ 8000000
#define SERVO_TIMEBASE_PERIOD 3

void app_main(void)
{

/*--------------------------------------------------------------------------------------------------*/
ESP_LOGI(TAG, "Create timer and operator");

mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_config = {
.group_id = 0,
.clk_src = MCPWM_TIMER_CLK_SRC_PLL160M,
.resolution_hz = SERVO_TIMEBASE_RESOLUTION_HZ,
.period_ticks = SERVO_TIMEBASE_PERIOD,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP,
};

ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer));

mcpwm_oper_handle_t oper = NULL;
mcpwm_operator_config_t operator_config = {
.group_id = 0,
};

ESP_ERROR_CHECK(mcpwm_new_operator(&operator_config, &oper));


/*--------------------------------------------------------------------------------------------------*/
ESP_LOGI(TAG, "Connect timer and operator");

ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper, timer));


/*--------------------------------------------------------------------------------------------------*/
ESP_LOGI(TAG, "Create comparator and generator from the operator");

mcpwm_cmpr_handle_t comparator = NULL;
mcpwm_comparator_config_t comparator_config = {
.flags.update_cmp_on_tep = true,
};

ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &comparator_config, &comparator));

mcpwm_gen_handle_t generator = NULL;
mcpwm_generator_config_t generator_config = {
.gen_gpio_num = SERVO_PULSE_GPIO,
};

ESP_ERROR_CHECK(mcpwm_new_generator(oper, &generator_config, &generator));


/*--------------------------------------------------------------------------------------------------*/
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator, 1));


/*--------------------------------------------------------------------------------------------------*/
ESP_LOGI(TAG, "Set generator action on timer and compare event");

ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH)));

ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comparator, MCPWM_GEN_ACTION_LOW)));


/*--------------------------------------------------------------------------------------------------*/
ESP_LOGI(TAG, "Enable and start timer");
ESP_ERROR_CHECK(mcpwm_timer_enable(timer));
ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP));
}

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: MCPWM maximum timer frequency

Postby ESP_Sprite » Wed Jun 14, 2023 2:21 am

Why not use the RMT peripheral for that? It's way more suitable for that task.

riiized
Posts: 2
Joined: Tue Jun 13, 2023 1:11 pm

Re: MCPWM maximum timer frequency

Postby riiized » Wed Jun 14, 2023 10:39 am

Okay, wrong peripheral, gotcha. But still, shouldnt the mcpwm timer frequency be higher than what I achieved?

Thanks for the help.

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: MCPWM maximum timer frequency

Postby ESP_Sprite » Thu Jun 15, 2023 1:55 am

Possibly, sorry, I don't know MCPWM that well.

ESP_morris
Posts: 290
Joined: Wed Sep 05, 2018 6:23 am

Re: MCPWM maximum timer frequency

Postby ESP_morris » Thu Jun 15, 2023 6:09 am

We have the example for DShot protocol https://github.com/espressif/esp-idf/tr ... /dshot_esc

Who is online

Users browsing this forum: Accound, Bing [Bot] and 97 guests