Page 7 of 16
Page 165
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/mcpwm_prelude.h"
void app_main(void)
{
mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_config = {
.group_id = 0,
.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
.resolution_hz = 1000000,
.period_ticks = 20000,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP,
};
mcpwm_new_timer(&timer_config, &timer);
mcpwm_oper_handle_t oper = NULL;
mcpwm_operator_config_t operator_config = {
.group_id = 0,
};
mcpwm_new_operator(&operator_config, &oper);
mcpwm_operator_connect_timer(oper, timer);
mcpwm_gen_handle_t generator = NULL;
mcpwm_generator_config_t generator_config = {
.gen_gpio_num = 2,
};
mcpwm_new_generator(oper, &generator_config, &generator);
mcpwm_generator_set_action_on_timer_event(generator,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
MCPWM_TIMER_EVENT_FULL, MCPWM_GEN_ACTION_TOGGLE));
mcpwm_timer_enable(timer);
mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP);
}
Page 167 complete program from fragment
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/mcpwm_prelude.h"
void app_main(void)
{
mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_config = {
.group_id = 0,
.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
.resolution_hz = 1000000,
.period_ticks = 20000,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP,
};
mcpwm_new_timer(&timer_config, &timer);
mcpwm_oper_handle_t oper = NULL;
mcpwm_operator_config_t operator_config = {
.group_id = 0,
};
mcpwm_new_operator(&operator_config, &oper);
mcpwm_operator_connect_timer(oper, timer);
mcpwm_gen_handle_t generator = NULL;
mcpwm_generator_config_t generator_config = {
.gen_gpio_num = 2,
};
mcpwm_new_generator(oper, &generator_config, &generator);
mcpwm_cmpr_handle_t comparator = NULL;
mcpwm_comparator_config_t comparator_config = {
.flags.update_cmp_on_tez = true,
};
mcpwm_new_comparator(oper, &comparator_config, &comparator);
mcpwm_comparator_set_compare_value(comparator,10000);
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));
mcpwm_generator_set_action_on_compare_event(generator,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
comparator, MCPWM_GEN_ACTION_LOW));
mcpwm_timer_enable(timer);
mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP);
}
Page 169 complete program from fragment
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/mcpwm_prelude.h"
void app_main(void)
{
mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_config = {
.group_id = 0,
.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
.resolution_hz = 1000000,
.period_ticks = 20000,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP,
};
mcpwm_new_timer(&timer_config, &timer);
mcpwm_oper_handle_t oper = NULL;
mcpwm_operator_config_t operator_config = {
.group_id = 0,
};
mcpwm_new_operator(&operator_config, &oper);
mcpwm_operator_connect_timer(oper, timer);
mcpwm_generator_config_t generator_config = {
.gen_gpio_num = 2,
};
mcpwm_gen_handle_t generatorA = NULL;
mcpwm_new_generator(oper, &generator_config, &generatorA);
generator_config.gen_gpio_num = 4;
mcpwm_gen_handle_t generatorB = NULL;
mcpwm_new_generator(oper, &generator_config, &generatorB);
mcpwm_comparator_config_t comparator_config = {
.flags.update_cmp_on_tez = true,
};
mcpwm_cmpr_handle_t comparatorA = NULL;
mcpwm_new_comparator(oper, &comparator_config, &comparatorA);
mcpwm_cmpr_handle_t comparatorB = NULL;
mcpwm_new_comparator(oper, &comparator_config, &comparatorB);
mcpwm_comparator_set_compare_value(comparatorA, 10000);
mcpwm_comparator_set_compare_value(comparatorB, 5000);
mcpwm_generator_set_action_on_timer_event(generatorA,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH));
mcpwm_generator_set_action_on_compare_event(generatorA,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
comparatorA, MCPWM_GEN_ACTION_LOW));
mcpwm_generator_set_action_on_timer_event(generatorB,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH));
mcpwm_generator_set_action_on_compare_event(generatorB,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
comparatorB, MCPWM_GEN_ACTION_LOW));
mcpwm_timer_enable(timer);
mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP);
}
Page 171 complete program from fragment
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/mcpwm_prelude.h"
void app_main(void)
{
mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_config = {
.group_id = 0,
.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
.resolution_hz = 1000000,
.period_ticks = 20000,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP_DOWN,
};
mcpwm_new_timer(&timer_config, &timer);
mcpwm_oper_handle_t oper = NULL;
mcpwm_operator_config_t operator_config = {
.group_id = 0,
};
mcpwm_new_operator(&operator_config, &oper);
mcpwm_operator_connect_timer(oper, timer);
mcpwm_generator_config_t generator_config = {
.gen_gpio_num = 2,
};
mcpwm_gen_handle_t generatorA = NULL;
mcpwm_new_generator(oper, &generator_config, &generatorA);
generator_config.gen_gpio_num = 4;
mcpwm_gen_handle_t generatorB = NULL;
mcpwm_new_generator(oper, &generator_config, &generatorB);
mcpwm_comparator_config_t comparator_config = {
.flags.update_cmp_on_tez = true,
};
mcpwm_cmpr_handle_t comparatorA = NULL;
mcpwm_new_comparator(oper, &comparator_config, &comparatorA);
mcpwm_cmpr_handle_t comparatorB = NULL;
mcpwm_new_comparator(oper, &comparator_config, &comparatorB);
mcpwm_comparator_set_compare_value(comparatorA, 10000);
mcpwm_comparator_set_compare_value(comparatorB, 5000);
mcpwm_comparator_set_compare_value(comparatorA, 5000);
mcpwm_comparator_set_compare_value(comparatorB, 9000);
mcpwm_generator_set_action_on_compare_event(generatorA,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
comparatorA, MCPWM_GEN_ACTION_HIGH));
mcpwm_generator_set_action_on_compare_event(generatorA,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN,
comparatorA, MCPWM_GEN_ACTION_LOW));
mcpwm_generator_set_action_on_compare_event(generatorB,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
comparatorB, MCPWM_GEN_ACTION_HIGH));
mcpwm_generator_set_action_on_compare_event(generatorB,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN,
comparatorB, MCPWM_GEN_ACTION_LOW));
mcpwm_timer_enable(timer);
mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP);
}
Page 174 MCPWM.h
#include "driver/mcpwm_prelude.h"
mcpwm_timer_handle_t makeTimer(uint32_t res, uint32_t periodTicks)
{
mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_config = {
.group_id = 0,
.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
.resolution_hz = res,
.period_ticks = periodTicks,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP,
};
mcpwm_new_timer(&timer_config, &timer);
return timer;
}
mcpwm_oper_handle_t makeOper()
{
mcpwm_oper_handle_t oper = NULL;
mcpwm_operator_config_t operator_config = {
.group_id = 0,
};
mcpwm_new_operator(&operator_config, &oper);
return oper;
}
mcpwm_gen_handle_t makeGen(mcpwm_oper_handle_t oper, int gpio)
{
mcpwm_generator_config_t generator_config = {
.gen_gpio_num = gpio};
mcpwm_gen_handle_t generator = NULL;
mcpwm_new_generator(oper, &generator_config, &generator);
return generator;
}
mcpwm_cmpr_handle_t makeComp(mcpwm_oper_handle_t oper)
{
mcpwm_comparator_config_t comparator_config = {
.flags.update_cmp_on_tez = true,
};
mcpwm_cmpr_handle_t comparator = NULL;
mcpwm_new_comparator(oper, &comparator_config, &comparator);
return comparator;
}
void setPhaseTep(mcpwm_timer_handle_t SourceTimer, mcpwm_timer_handle_t PhasedTimer, uint32_t phaseticks)
{
mcpwm_timer_sync_src_config_t syncconfig = {
.timer_event = MCPWM_TIMER_EVENT_EMPTY,
};
mcpwm_sync_handle_t sync = NULL;
mcpwm_new_timer_sync_src(SourceTimer, &syncconfig, &sync);
mcpwm_timer_sync_phase_config_t phase = {
.count_value = phaseticks,
.direction = MCPWM_TIMER_DIRECTION_UP,
.sync_src = sync};
mcpwm_timer_set_phase_on_sync(PhasedTimer, &phase);
}
Page 176 Uses previous header
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/mcpwm_prelude.h"
#include "MCPWM.h"
void app_main(void)
{
mcpwm_timer_handle_t timer1 = makeTimer(1000000, 20000);
mcpwm_timer_handle_t timer2 = makeTimer(1000000, 20000);
mcpwm_oper_handle_t oper1 = makeOper();
mcpwm_oper_handle_t oper2 = makeOper();
mcpwm_operator_connect_timer(oper1, timer1);
mcpwm_operator_connect_timer(oper2, timer2);
mcpwm_gen_handle_t generator1 = makeGen(oper1, 2);
mcpwm_gen_handle_t generator2 = makeGen(oper2, 4);
mcpwm_cmpr_handle_t comparator1 = makeComp(oper1);
mcpwm_cmpr_handle_t comparator2 = makeComp(oper2);
mcpwm_comparator_set_compare_value(comparator1, 9000);
mcpwm_comparator_set_compare_value(comparator2, 9000);
setPhaseTep(timer1, timer2, 4000);
mcpwm_generator_set_action_on_timer_event(generator1,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH));
mcpwm_generator_set_action_on_compare_event(generator1,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
comparator1, MCPWM_GEN_ACTION_LOW));
mcpwm_generator_set_action_on_timer_event(generator2,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH));
mcpwm_generator_set_action_on_compare_event(generator2,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,
comparator2, MCPWM_GEN_ACTION_LOW));
mcpwm_timer_enable(timer1);
mcpwm_timer_start_stop(timer1, MCPWM_TIMER_START_NO_STOP);
mcpwm_timer_enable(timer2);
mcpwm_timer_start_stop(timer2, MCPWM_TIMER_START_NO_STOP);
}