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
38
39
40
41
42
43
44
51
52
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
87
88
89
90
91
92
93
94
95
96
97
98
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
124
127
128
129
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
212
213
214
215
216
217
218
219
220
221
222
233
234
235
236
241
242
243
244
245
246
247
248
249
250
251
252
253
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
300
301
309
310
318
319
322
/* ... */
#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 "esp_memory_utils.h"
#include "soc/soc_caps.h"
#include "soc/mcpwm_periph.h"
#include "hal/mcpwm_ll.h"
#include "hal/gpio_hal.h"
#include "driver/mcpwm_fault.h"
#include "driver/gpio.h"
#include "mcpwm_private.h"
#include "esp_private/gpio.h"14 includes
static const char *TAG = "mcpwm";
static void mcpwm_gpio_fault_default_isr(void *args);
static esp_err_t mcpwm_del_gpio_fault(mcpwm_fault_handle_t fault);
static esp_err_t mcpwm_del_soft_fault(mcpwm_fault_handle_t fault);
static esp_err_t mcpwm_gpio_fault_register_to_group(mcpwm_gpio_fault_t *fault, int group_id)
{
mcpwm_group_t *group = mcpwm_acquire_group_handle(group_id);
ESP_RETURN_ON_FALSE(group, ESP_ERR_NO_MEM, TAG, "no mem for group (%d)", group_id);
int fault_id = -1;
portENTER_CRITICAL(&group->spinlock);
for (int i = 0; i < SOC_MCPWM_GPIO_FAULTS_PER_GROUP; i++) {
if (!group->gpio_faults[i]) {
fault_id = i;
group->gpio_faults[i] = fault;
break;
}{...}
}{...}
portEXIT_CRITICAL(&group->spinlock);
if (fault_id < 0) {
mcpwm_release_group_handle(group);
group = NULL;
}{...} else {
fault->base.group = group;
fault->fault_id = fault_id;
}{...}
ESP_RETURN_ON_FALSE(fault_id >= 0, ESP_ERR_NOT_FOUND, TAG, "no free gpio fault in group (%d)", group_id);
return ESP_OK;
}{ ... }
static void mcpwm_gpio_fault_unregister_from_group(mcpwm_gpio_fault_t *fault)
{
mcpwm_group_t *group = fault->base.group;
int fault_id = fault->fault_id;
portENTER_CRITICAL(&group->spinlock);
group->gpio_faults[fault_id] = NULL;
portEXIT_CRITICAL(&group->spinlock);
mcpwm_release_group_handle(group);
}{ ... }
static esp_err_t mcpwm_gpio_fault_destroy(mcpwm_gpio_fault_t *fault)
{
if (fault->intr) {
ESP_RETURN_ON_ERROR(esp_intr_free(fault->intr), TAG, "uninstall interrupt service failed");
}{...}
if (fault->base.group) {
mcpwm_gpio_fault_unregister_from_group(fault);
}{...}
free(fault);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_new_gpio_fault(const mcpwm_gpio_fault_config_t *config, mcpwm_fault_handle_t *ret_fault)
{
#if CONFIG_MCPWM_ENABLE_DEBUG_LOG
esp_log_level_set(TAG, ESP_LOG_DEBUG);
#endif
esp_err_t ret = ESP_OK;
mcpwm_gpio_fault_t *fault = NULL;
ESP_GOTO_ON_FALSE(config && ret_fault, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
ESP_GOTO_ON_FALSE(config->group_id < SOC_MCPWM_GROUPS && config->group_id >= 0, ESP_ERR_INVALID_ARG,
err, TAG, "invalid group ID:%d", config->group_id);
if (config->intr_priority) {
ESP_GOTO_ON_FALSE(1 << (config->intr_priority) & MCPWM_ALLOW_INTR_PRIORITY_MASK, ESP_ERR_INVALID_ARG, err,
TAG, "invalid interrupt priority:%d", config->intr_priority);
}{...}
fault = heap_caps_calloc(1, sizeof(mcpwm_gpio_fault_t), MCPWM_MEM_ALLOC_CAPS);
ESP_GOTO_ON_FALSE(fault, ESP_ERR_NO_MEM, err, TAG, "no mem for gpio fault");
ESP_GOTO_ON_ERROR(mcpwm_gpio_fault_register_to_group(fault, config->group_id), err, TAG, "register gpio fault failed");
mcpwm_group_t *group = fault->base.group;
int group_id = group->group_id;
mcpwm_hal_context_t *hal = &group->hal;
int fault_id = fault->fault_id;
ESP_GOTO_ON_ERROR(mcpwm_check_intr_priority(group, config->intr_priority), err, TAG, "set group interrupt priority failed");
gpio_func_sel(config->gpio_num, PIN_FUNC_GPIO);
gpio_input_enable(config->gpio_num);
esp_rom_gpio_connect_in_signal(config->gpio_num, mcpwm_periph_signals.groups[group_id].gpio_faults[fault_id].fault_sig, 0);
if (config->flags.pull_down) {
gpio_pulldown_en(config->gpio_num);
}{...}
if (config->flags.pull_up) {
gpio_pullup_en(config->gpio_num);
}{...}
if (config->flags.io_loop_back) {
gpio_ll_output_enable(&GPIO, config->gpio_num);
}{...}
portENTER_CRITICAL(&group->spinlock);
mcpwm_ll_fault_set_active_level(hal->dev, fault_id, config->flags.active_level);
portEXIT_CRITICAL(&group->spinlock);
mcpwm_ll_fault_enable_detection(hal->dev, fault_id, true);
fault->base.type = MCPWM_FAULT_TYPE_GPIO;
fault->gpio_num = config->gpio_num;
fault->base.del = mcpwm_del_gpio_fault;
*ret_fault = &fault->base;
ESP_LOGD(TAG, "new gpio fault (%d,%d) at %p, GPIO: %d", group_id, fault_id, fault, config->gpio_num);
return ESP_OK;
err:
if (fault) {
mcpwm_gpio_fault_destroy(fault);
}{...}
return ret;
}{ ... }
static esp_err_t mcpwm_del_gpio_fault(mcpwm_fault_handle_t fault)
{
mcpwm_gpio_fault_t *gpio_fault = __containerof(fault, mcpwm_gpio_fault_t, base);
mcpwm_group_t *group = fault->group;
mcpwm_hal_context_t *hal = &group->hal;
int group_id = group->group_id;
int fault_id = gpio_fault->fault_id;
ESP_LOGD(TAG, "del GPIO fault (%d,%d)", group->group_id, fault_id);
portENTER_CRITICAL(&group->spinlock);
mcpwm_ll_intr_enable(hal->dev, MCPWM_LL_EVENT_FAULT_MASK(fault_id), false);
mcpwm_ll_intr_clear_status(hal->dev, MCPWM_LL_EVENT_FAULT_MASK(fault_id));
portEXIT_CRITICAL(&group->spinlock);
mcpwm_ll_fault_enable_detection(hal->dev, fault_id, false);
esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ZERO_INPUT,
mcpwm_periph_signals.groups[group_id].gpio_faults[fault_id].fault_sig, 0);
ESP_RETURN_ON_ERROR(mcpwm_gpio_fault_destroy(gpio_fault), TAG, "destroy GPIO fault failed");
return ESP_OK;
}{ ... }
esp_err_t mcpwm_new_soft_fault(const mcpwm_soft_fault_config_t *config, mcpwm_fault_handle_t *ret_fault)
{
esp_err_t ret = ESP_OK;
mcpwm_soft_fault_t *soft_fault = NULL;
ESP_GOTO_ON_FALSE(config && ret_fault, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
soft_fault = heap_caps_calloc(1, sizeof(mcpwm_soft_fault_t), MCPWM_MEM_ALLOC_CAPS);
ESP_GOTO_ON_FALSE(soft_fault, ESP_ERR_NO_MEM, err, TAG, "no mem for soft fault");
soft_fault->base.type = MCPWM_FAULT_TYPE_SOFT;
soft_fault->base.del = mcpwm_del_soft_fault;
*ret_fault = &soft_fault->base;
ESP_LOGD(TAG, "new soft fault at %p", soft_fault);
return ESP_OK;
err:
free(soft_fault);
return ret;
}{ ... }
static esp_err_t mcpwm_del_soft_fault(mcpwm_fault_handle_t fault)
{
mcpwm_soft_fault_t *soft_fault = __containerof(fault, mcpwm_soft_fault_t, base);
ESP_LOGD(TAG, "del soft fault %p", soft_fault);
free(soft_fault);
return ESP_OK;
}{ ... }
esp_err_t mcpwm_soft_fault_activate(mcpwm_fault_handle_t fault)
{
ESP_RETURN_ON_FALSE(fault, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_RETURN_ON_FALSE(fault->type == MCPWM_FAULT_TYPE_SOFT, ESP_ERR_INVALID_ARG, TAG, "not a valid soft fault");
mcpwm_group_t *group = fault->group;
mcpwm_soft_fault_t *soft_fault = __containerof(fault, mcpwm_soft_fault_t, base);
mcpwm_oper_t *oper = soft_fault->oper;
ESP_RETURN_ON_FALSE(oper, ESP_ERR_INVALID_STATE, TAG, "no operator is assigned to the fault");
switch (oper->brake_mode_on_soft_fault) {
case MCPWM_OPER_BRAKE_MODE_CBC:
mcpwm_ll_brake_trigger_soft_cbc(group->hal.dev, oper->oper_id);
break;...
case MCPWM_OPER_BRAKE_MODE_OST:
mcpwm_ll_brake_trigger_soft_ost(group->hal.dev, oper->oper_id);
break;...
default:
ESP_RETURN_ON_FALSE(false, ESP_ERR_INVALID_STATE, TAG, "unknown brake mode:%d", oper->brake_mode_on_soft_fault);
break;...
}{...}
return ESP_OK;
}{ ... }
esp_err_t mcpwm_del_fault(mcpwm_fault_handle_t fault)
{
ESP_RETURN_ON_FALSE(fault, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
return fault->del(fault);
}{ ... }
esp_err_t mcpwm_fault_register_event_callbacks(mcpwm_fault_handle_t fault, const mcpwm_fault_event_callbacks_t *cbs, void *user_data)
{
ESP_RETURN_ON_FALSE(fault && cbs, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
ESP_RETURN_ON_FALSE(fault->type == MCPWM_FAULT_TYPE_GPIO, ESP_ERR_INVALID_ARG, TAG, "only gpio fault can register event callback");
mcpwm_gpio_fault_t *gpio_fault = __containerof(fault, mcpwm_gpio_fault_t, base);
mcpwm_group_t *group = fault->group;
int group_id = group->group_id;
mcpwm_hal_context_t *hal = &group->hal;
int fault_id = gpio_fault->fault_id;
#if CONFIG_MCPWM_ISR_IRAM_SAFE
if (cbs->on_fault_enter) {
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_fault_enter), ESP_ERR_INVALID_ARG, TAG, "on_fault_enter callback not in IRAM");
}{...}
if (cbs->on_fault_exit) {
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_fault_exit), ESP_ERR_INVALID_ARG, TAG, "on_fault_exit callback not in IRAM");
}{...}
if (user_data) {
ESP_RETURN_ON_FALSE(esp_ptr_internal(user_data), ESP_ERR_INVALID_ARG, TAG, "user context not in internal RAM");
}{...}
#endif/* ... */
if (!gpio_fault->intr) {
int isr_flags = MCPWM_INTR_ALLOC_FLAG & ~ESP_INTR_FLAG_INTRDISABLED;
isr_flags |= mcpwm_get_intr_priority_flag(group);
ESP_RETURN_ON_ERROR(esp_intr_alloc_intrstatus(mcpwm_periph_signals.groups[group_id].irq_id, isr_flags,
(uint32_t)mcpwm_ll_intr_get_status_reg(hal->dev), MCPWM_LL_EVENT_FAULT_MASK(fault_id),
mcpwm_gpio_fault_default_isr, gpio_fault, &gpio_fault->intr), TAG, "install interrupt service for gpio fault failed");
}{...}
portENTER_CRITICAL(&group->spinlock);
mcpwm_ll_intr_enable(hal->dev, MCPWM_LL_EVENT_FAULT_ENTER(fault_id), cbs->on_fault_enter != NULL);
mcpwm_ll_intr_enable(hal->dev, MCPWM_LL_EVENT_FAULT_EXIT(fault_id), cbs->on_fault_exit != NULL);
portEXIT_CRITICAL(&group->spinlock);
gpio_fault->on_fault_enter = cbs->on_fault_enter;
gpio_fault->on_fault_exit = cbs->on_fault_exit;
gpio_fault->user_data = user_data;
return ESP_OK;
}{ ... }
static void IRAM_ATTR mcpwm_gpio_fault_default_isr(void *args)
{
mcpwm_gpio_fault_t *fault = (mcpwm_gpio_fault_t *)args;
mcpwm_group_t *group = fault->base.group;
mcpwm_hal_context_t *hal = &group->hal;
int fault_id = fault->fault_id;
bool need_yield = false;
uint32_t status = mcpwm_ll_intr_get_status(hal->dev);
mcpwm_ll_intr_clear_status(hal->dev, status & MCPWM_LL_EVENT_FAULT_MASK(fault_id));
mcpwm_fault_event_data_t edata = {
}{...};
if (status & MCPWM_LL_EVENT_FAULT_ENTER(fault_id)) {
mcpwm_fault_event_cb_t cb = fault->on_fault_enter;
if (cb) {
if (cb(&fault->base, &edata, fault->user_data)) {
need_yield = true;
}{...}
}{...}
}{...}
if (status & MCPWM_LL_EVENT_FAULT_EXIT(fault_id)) {
mcpwm_fault_event_cb_t cb = fault->on_fault_exit;
if (cb) {
if (cb(&fault->base, &edata, fault->user_data)) {
need_yield = true;
}{...}
}{...}
}{...}
if (need_yield) {
portYIELD_FROM_ISR();
}{...}
}{ ... }