1
6
7
8
9
10
11
12
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
44
45
46
47
48
49
50
51
52
61
62
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
96
97
98
99
100
101
102
103
104
105
108
111
112
113
114
115
116
117
118
119
120
123
124
125
126
127
128
129
130
131
132
133
134
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
352
357
371
372
373
374
375
376
377
378
379
380
388
389
390
400
401
404
407
408
411
412
413
414
415
/* ... */
#include <stdlib.h>
#include <stdarg.h>
#include <sys/cdefs.h>
#include "sdkconfig.h"
#if CONFIG_MCPWM_ENABLE_DEBUG_LOG
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG/* ... */
#endif
#include "freertos/FreeRTOS.h"
#include "esp_attr.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_log.h"
#include "soc/soc_caps.h"
#include "soc/mcpwm_periph.h"
#include "hal/mcpwm_ll.h"
#include "hal/gpio_hal.h"
#include "driver/gpio.h"
#include "driver/mcpwm_gen.h"
#include "mcpwm_private.h"
#include "esp_private/esp_gpio_reserve.h"
#include "esp_private/gpio.h"14 includes
static const char *TAG = "mcpwm";
static esp_err_t mcpwm_generator_register_to_operator(mcpwm_gen_t *gen, mcpwm_oper_t *oper)
{
int gen_id = -1;
portENTER_CRITICAL(&oper->spinlock);
for (int i = 0; i < SOC_MCPWM_GENERATORS_PER_OPERATOR; i++) {
if (!oper->generators[i]) {
oper->generators[i] = gen;
gen_id = i;
break;
}{...}
}{...}
portEXIT_CRITICAL(&oper->spinlock);
ESP_RETURN_ON_FALSE(gen_id >= 0, ESP_ERR_NOT_FOUND, TAG, "no free generator in operator (%d,%d)", oper->group->group_id, oper->oper_id);
gen->gen_id = gen_id;
gen->oper = oper;
return ESP_OK;
}{ ... }
static void mcpwm_generator_unregister_from_operator(mcpwm_gen_t *gen)
{
mcpwm_oper_t *oper = gen->oper;
int gen_id = gen->gen_id;
portENTER_CRITICAL(&oper->spinlock);
oper->generators[gen_id] = NULL;
portEXIT_CRITICAL(&oper->spinlock);
}{ ... }
static esp_err_t mcpwm_generator_destroy(mcpwm_gen_t *gen)
{
if (gen->oper) {
mcpwm_generator_unregister_from_operator(gen);
}{...}
free(gen);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_new_generator(mcpwm_oper_handle_t oper, const mcpwm_generator_config_t *config, mcpwm_gen_handle_t *ret_gen)
{
esp_err_t ret = ESP_OK;
mcpwm_gen_t *gen = NULL;
ESP_GOTO_ON_FALSE(oper && config && ret_gen, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
gen = heap_caps_calloc(1, sizeof(mcpwm_gen_t), MCPWM_MEM_ALLOC_CAPS);
ESP_GOTO_ON_FALSE(gen, ESP_ERR_NO_MEM, err, TAG, "no mem for generator");
ESP_GOTO_ON_ERROR(mcpwm_generator_register_to_operator(gen, oper), err, TAG, "register generator failed");
mcpwm_group_t *group = oper->group;
mcpwm_hal_context_t *hal = &group->hal;
int oper_id = oper->oper_id;
int gen_id = gen->gen_id;
mcpwm_hal_generator_reset(hal, oper_id, gen_id);
gen->gen_gpio_num = -1;
uint64_t old_gpio_rsv_mask = esp_gpio_reserve(BIT64(config->gen_gpio_num));
if (old_gpio_rsv_mask & BIT64(config->gen_gpio_num)) {
ESP_LOGW(TAG, "GPIO %d is not usable, maybe conflict with others", config->gen_gpio_num);
}{...}
gpio_func_sel(config->gen_gpio_num, PIN_FUNC_GPIO);
esp_rom_gpio_connect_out_signal(config->gen_gpio_num,
mcpwm_periph_signals.groups[group->group_id].operators[oper_id].generators[gen_id].pwm_sig,
config->flags.invert_pwm, 0);
if (config->flags.io_loop_back) {
gpio_ll_input_enable(&GPIO, config->gen_gpio_num);
}{...}
if (config->flags.io_od_mode) {
gpio_ll_od_enable(&GPIO, config->gen_gpio_num);
}{...}
gen->gen_gpio_num = config->gen_gpio_num;
gen->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
*ret_gen = gen;
ESP_LOGD(TAG, "new generator (%d,%d,%d) at %p, GPIO %d", group->group_id, oper_id, gen_id, gen, gen->gen_gpio_num);
return ESP_OK;
err:
if (gen) {
mcpwm_generator_destroy(gen);
}{...}
return ret;
}{ ... }
esp_err_t mcpwm_del_generator(mcpwm_gen_handle_t gen)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
ESP_LOGD(TAG, "del generator (%d,%d,%d)", group->group_id, oper->oper_id, gen->gen_id);
if (gen->gen_gpio_num >= 0) {
gpio_output_disable(gen->gen_gpio_num);
esp_gpio_revoke(BIT64(gen->gen_gpio_num));
}{...}
ESP_RETURN_ON_ERROR(mcpwm_generator_destroy(gen), TAG, "destroy generator failed");
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_force_level(mcpwm_gen_handle_t gen, int level, bool hold_on)
{
ESP_RETURN_ON_FALSE(gen && level <= 1, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
mcpwm_hal_context_t *hal = &group->hal;
int oper_id = oper->oper_id;
int gen_id = gen->gen_id;
if (level < 0) {
if (hold_on) {
mcpwm_ll_gen_disable_continue_force_action(hal->dev, oper_id, gen_id);
}{...} else {
mcpwm_ll_gen_disable_noncontinue_force_action(hal->dev, oper_id, gen_id);
}{...}
}{...} else {
if (hold_on) {
mcpwm_ll_gen_set_continue_force_level(hal->dev, oper_id, gen_id, level);
}{...} else {
mcpwm_ll_gen_set_noncontinue_force_level(hal->dev, oper_id, gen_id, level);
mcpwm_ll_gen_trigger_noncontinue_force_action(hal->dev, oper_id, gen_id);
}{...}
}{...}
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_action_on_timer_event(mcpwm_gen_handle_t gen, mcpwm_gen_timer_event_action_t ev_act)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_RETURN_ON_FALSE(ev_act.event != MCPWM_TIMER_EVENT_INVALID, ESP_ERR_INVALID_ARG, TAG, "invalid event");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
mcpwm_timer_t *timer = oper->timer;
ESP_RETURN_ON_FALSE(timer, ESP_ERR_INVALID_STATE, TAG, "no timer is connected to the operator");
bool invalid_utep = (timer->count_mode == MCPWM_TIMER_COUNT_MODE_UP_DOWN) &&
(ev_act.direction == MCPWM_TIMER_DIRECTION_UP) &&
(ev_act.event == MCPWM_TIMER_EVENT_FULL);
bool invalid_dtez = (timer->count_mode == MCPWM_TIMER_COUNT_MODE_UP_DOWN) &&
(ev_act.direction == MCPWM_TIMER_DIRECTION_DOWN) &&
(ev_act.event == MCPWM_TIMER_EVENT_EMPTY);
if (invalid_utep || invalid_dtez) {
ESP_RETURN_ON_FALSE(false, ESP_ERR_INVALID_ARG, TAG, "UTEP and DTEZ can't be reached under MCPWM_TIMER_COUNT_MODE_UP_DOWN mode");
}{...}
mcpwm_ll_generator_set_action_on_timer_event(group->hal.dev, oper->oper_id, gen->gen_id,
ev_act.direction, ev_act.event, ev_act.action);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_actions_on_timer_event(mcpwm_gen_handle_t gen, mcpwm_gen_timer_event_action_t ev_act, ...)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
mcpwm_timer_t *timer = oper->timer;
ESP_RETURN_ON_FALSE(timer, ESP_ERR_INVALID_STATE, TAG, "no timer is connected to the operator");
mcpwm_gen_timer_event_action_t ev_act_itor = ev_act;
bool invalid_utep = false;
bool invalid_dtez = false;
va_list it;
va_start(it, ev_act);
while (ev_act_itor.event != MCPWM_TIMER_EVENT_INVALID) {
invalid_utep = (timer->count_mode == MCPWM_TIMER_COUNT_MODE_UP_DOWN) &&
(ev_act_itor.direction == MCPWM_TIMER_DIRECTION_UP) &&
(ev_act_itor.event == MCPWM_TIMER_EVENT_FULL);
invalid_dtez = (timer->count_mode == MCPWM_TIMER_COUNT_MODE_UP_DOWN) &&
(ev_act_itor.direction == MCPWM_TIMER_DIRECTION_DOWN) &&
(ev_act_itor.event == MCPWM_TIMER_EVENT_EMPTY);
if (invalid_utep || invalid_dtez) {
va_end(it);
ESP_RETURN_ON_FALSE(false, ESP_ERR_INVALID_ARG, TAG, "UTEP and DTEZ can't be reached under MCPWM_TIMER_COUNT_MODE_UP_DOWN mode");
}{...}
mcpwm_ll_generator_set_action_on_timer_event(group->hal.dev, oper->oper_id, gen->gen_id,
ev_act_itor.direction, ev_act_itor.event, ev_act_itor.action);
ev_act_itor = va_arg(it, mcpwm_gen_timer_event_action_t);
}{...}
va_end(it);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_action_on_compare_event(mcpwm_gen_handle_t gen, mcpwm_gen_compare_event_action_t ev_act)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_RETURN_ON_FALSE(ev_act.comparator, ESP_ERR_INVALID_ARG, TAG, "invalid comparator");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
mcpwm_ll_generator_set_action_on_compare_event(group->hal.dev, oper->oper_id, gen->gen_id,
ev_act.direction, ev_act.comparator->cmpr_id, ev_act.action);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_actions_on_compare_event(mcpwm_gen_handle_t gen, mcpwm_gen_compare_event_action_t ev_act, ...)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
mcpwm_gen_compare_event_action_t ev_act_itor = ev_act;
va_list it;
va_start(it, ev_act);
while (ev_act_itor.comparator) {
mcpwm_ll_generator_set_action_on_compare_event(group->hal.dev, oper->oper_id, gen->gen_id,
ev_act_itor.direction, ev_act_itor.comparator->cmpr_id, ev_act_itor.action);
ev_act_itor = va_arg(it, mcpwm_gen_compare_event_action_t);
}{...}
va_end(it);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_action_on_brake_event(mcpwm_gen_handle_t gen, mcpwm_gen_brake_event_action_t ev_act)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_RETURN_ON_FALSE(ev_act.brake_mode != MCPWM_OPER_BRAKE_MODE_INVALID, ESP_ERR_INVALID_ARG, TAG, "invalid brake mode");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
mcpwm_ll_generator_set_action_on_brake_event(group->hal.dev, oper->oper_id, gen->gen_id,
ev_act.direction, ev_act.brake_mode, ev_act.action);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_actions_on_brake_event(mcpwm_gen_handle_t gen, mcpwm_gen_brake_event_action_t ev_act, ...)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
mcpwm_gen_brake_event_action_t ev_act_itor = ev_act;
va_list it;
va_start(it, ev_act);
while (ev_act_itor.brake_mode != MCPWM_OPER_BRAKE_MODE_INVALID) {
mcpwm_ll_generator_set_action_on_brake_event(group->hal.dev, oper->oper_id, gen->gen_id,
ev_act_itor.direction, ev_act_itor.brake_mode, ev_act_itor.action);
ev_act_itor = va_arg(it, mcpwm_gen_brake_event_action_t);
}{...}
va_end(it);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_action_on_fault_event(mcpwm_gen_handle_t gen, mcpwm_gen_fault_event_action_t ev_act)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
mcpwm_fault_t *fault = ev_act.fault;
ESP_RETURN_ON_FALSE(fault->type == MCPWM_FAULT_TYPE_GPIO, ESP_ERR_NOT_SUPPORTED, TAG, "not supported fault type");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
int trigger_id = -1;
portENTER_CRITICAL(&oper->spinlock);
for (int i = 0; i < SOC_MCPWM_TRIGGERS_PER_OPERATOR; i++) {
if (oper->triggers[i] == MCPWM_TRIGGER_NO_ASSIGN) {
trigger_id = i;
oper->triggers[i] = MCPWM_TRIGGER_GPIO_FAULT;
break;
}{...}
}{...}
portEXIT_CRITICAL(&oper->spinlock);
ESP_RETURN_ON_FALSE(trigger_id >= 0, ESP_ERR_NOT_FOUND, TAG, "no free trigger in operator (%d,%d)", group->group_id, oper->oper_id);
mcpwm_gpio_fault_t *gpio_fault = __containerof(fault, mcpwm_gpio_fault_t, base);
mcpwm_ll_operator_set_trigger_from_gpio_fault(group->hal.dev, oper->oper_id, trigger_id, gpio_fault->fault_id);
mcpwm_ll_generator_set_action_on_trigger_event(group->hal.dev, oper->oper_id, gen->gen_id,
ev_act.direction, trigger_id, ev_act.action);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_action_on_sync_event(mcpwm_gen_handle_t gen, mcpwm_gen_sync_event_action_t ev_act)
{
ESP_RETURN_ON_FALSE(gen, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
mcpwm_oper_t *oper = gen->oper;
mcpwm_group_t *group = oper->group;
int trigger_id = -1;
int trigger_sync_used = 0;
portENTER_CRITICAL(&oper->spinlock);
for (int i = 0; i < SOC_MCPWM_TRIGGERS_PER_OPERATOR; i++) {
if (oper->triggers[i] == MCPWM_TRIGGER_SYNC_EVENT) {
trigger_sync_used = 1;
break;
}{...}
if (oper->triggers[i] == MCPWM_TRIGGER_NO_ASSIGN) {
trigger_id = i;
oper->triggers[i] = MCPWM_TRIGGER_SYNC_EVENT;
break;
}{...}
}{...}
portEXIT_CRITICAL(&oper->spinlock);
ESP_RETURN_ON_FALSE(!trigger_sync_used, ESP_ERR_INVALID_STATE, TAG, "only one sync supported");
ESP_RETURN_ON_FALSE(trigger_id >= 0, ESP_ERR_NOT_FOUND, TAG, "no free trigger in operator (%d,%d)", group->group_id, oper->oper_id);
mcpwm_ll_operator_set_trigger_from_sync(group->hal.dev, oper->oper_id, trigger_id);
mcpwm_ll_generator_set_action_on_trigger_event(group->hal.dev, oper->oper_id, gen->gen_id,
ev_act.direction, trigger_id, ev_act.action);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_generator_set_dead_time(mcpwm_gen_handle_t in_generator, mcpwm_gen_handle_t out_generator, const mcpwm_dead_time_config_t *config)
{
ESP_RETURN_ON_FALSE(in_generator && out_generator && config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_RETURN_ON_FALSE(in_generator->oper == out_generator->oper, ESP_ERR_INVALID_ARG, TAG, "in/out generator are not derived from the same operator");
ESP_RETURN_ON_FALSE(config->negedge_delay_ticks < MCPWM_LL_MAX_DEAD_DELAY && config->posedge_delay_ticks < MCPWM_LL_MAX_DEAD_DELAY,
ESP_ERR_INVALID_ARG, TAG, "delay time out of range");
mcpwm_oper_t *oper = in_generator->oper;
mcpwm_group_t *group = oper->group;
mcpwm_hal_context_t *hal = &group->hal;
int oper_id = oper->oper_id;
bool delay_module_conflict = false;
portENTER_CRITICAL(&oper->spinlock);
if (config->posedge_delay_ticks) {
if (oper->posedge_delay_owner && oper->posedge_delay_owner != in_generator) {
delay_module_conflict = true;
}{...}
}{...}
if (config->negedge_delay_ticks) {
if (oper->negedge_delay_owner && oper->negedge_delay_owner != in_generator) {
delay_module_conflict = true;
}{...}
}{...}
if (!delay_module_conflict) {
if (config->posedge_delay_ticks) {
oper->posedge_delay_owner = in_generator;
}{...} else if (oper->posedge_delay_owner == in_generator) {
oper->posedge_delay_owner = NULL;
}{...}
if (config->negedge_delay_ticks) {
oper->negedge_delay_owner = in_generator;
}{...} else if (oper->negedge_delay_owner == in_generator) {
oper->negedge_delay_owner = NULL;
}{...}
}{...}
portEXIT_CRITICAL(&oper->spinlock);
ESP_RETURN_ON_FALSE(!delay_module_conflict, ESP_ERR_INVALID_STATE, TAG, "delay module is in use by other generator");
bool bypass = (config->negedge_delay_ticks == 0) && (config->posedge_delay_ticks == 0);
bool delay_on_both_edge = config->posedge_delay_ticks && config->negedge_delay_ticks;
int out_path_id = -1;
if (bypass) {
out_path_id = in_generator->gen_id;
}{...} else if (config->negedge_delay_ticks) {
out_path_id = 1;
}{...} else {
out_path_id = 0;
}{...}
bool swap_path = out_path_id != out_generator->gen_id;
mcpwm_ll_deadtime_bypass_path(hal->dev, oper_id, out_path_id, bypass);
if (!bypass) {
if (config->posedge_delay_ticks) {
mcpwm_ll_deadtime_red_select_generator(hal->dev, oper_id, in_generator->gen_id);
}{...} else {
mcpwm_ll_deadtime_fed_select_generator(hal->dev, oper_id, in_generator->gen_id);
}{...}
mcpwm_ll_deadtime_enable_deb(hal->dev, oper_id, delay_on_both_edge);
mcpwm_ll_deadtime_invert_outpath(hal->dev, oper_id, out_path_id, config->flags.invert_output);
mcpwm_ll_deadtime_swap_out_path(hal->dev, oper_id, out_generator->gen_id, swap_path);
}{...}
if (config->posedge_delay_ticks) {
mcpwm_ll_deadtime_set_rising_delay(hal->dev, oper_id, config->posedge_delay_ticks);
}{...}
if (config->negedge_delay_ticks) {
mcpwm_ll_deadtime_set_falling_delay(hal->dev, oper_id, config->negedge_delay_ticks);
}{...}
if (delay_on_both_edge && in_generator->gen_id == 0 && oper->generators[1]) {
ESP_LOGW(TAG, "generator B will not function correctly. To set deadtime on both edges for one generator while bypassing the deadtime for the other, please set the deadtime for generator B only.");
}{...}
ESP_LOGD(TAG, "operator (%d,%d) dead time (R:%"PRIu32",F:%"PRIu32"), topology code:%"PRIx32, group->group_id, oper_id,
config->posedge_delay_ticks, config->negedge_delay_ticks, mcpwm_ll_deadtime_get_switch_topology(hal->dev, oper_id));
return ESP_OK;
}{ ... }