-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
mcpwm.c
733 lines (693 loc) · 37.3 KB
/
mcpwm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
206
207
208
209
210
211
212
213
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include "esp_log.h"
#include "esp_err.h"
#include "esp_intr.h"
#include "esp_intr_alloc.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/xtensa_api.h"
#include "freertos/task.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
#include "soc/io_mux_reg.h"
#include "soc/gpio_sig_map.h"
#include "driver/mcpwm.h"
#include "driver/periph_ctrl.h"
static mcpwm_dev_t *MCPWM[2] = {&MCPWM0, &MCPWM1};
static const char *MCPWM_TAG = "MCPWM";
static portMUX_TYPE mcpwm_spinlock = portMUX_INITIALIZER_UNLOCKED;
#define MCPWM_CHECK(a, str, ret_val) if (!(a)) { \
ESP_LOGE(MCPWM_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
return (ret_val); \
}
#define MCPWM_UNIT_NUM_ERROR "MCPWM UNIT NUM ERROR"
#define MCPWM_TIMER_ERROR "MCPWM TIMER NUM ERROR"
#define MCPWM_PARAM_ADDR_ERROR "MCPWM PARAM ADDR ERROR"
#define MCPWM_DUTY_TYPE_ERROR "MCPWM DUTY TYPE ERROR"
#define MCPWM_GPIO_ERROR "MCPWM GPIO NUM ERROR"
#define MCPWM_OP_ERROR "MCPWM OPERATOR ERROR"
#define MCPWM_DB_ERROR "MCPWM DEADTIME TYPE ERROR"
#define MCPWM_BASE_CLK (2 * APB_CLK_FREQ) //2*APB_CLK_FREQ 160Mhz
#define MCPWM_CLK_PRESCL 15 //MCPWM clock prescale
#define TIMER_CLK_PRESCALE 9 //MCPWM timer prescales
#define MCPWM_CLK (MCPWM_BASE_CLK/(MCPWM_CLK_PRESCL +1))
#define MCPWM_PIN_IGNORE (-1)
#define OFFSET_FOR_GPIO_IDX_1 6
#define OFFSET_FOR_GPIO_IDX_2 75
esp_err_t mcpwm_gpio_init(mcpwm_unit_t mcpwm_num, mcpwm_io_signals_t io_signal, int gpio_num)
{
if (gpio_num == MCPWM_PIN_IGNORE) {
//IGNORE
return ESP_OK;
}
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK((GPIO_IS_VALID_GPIO(gpio_num)), MCPWM_GPIO_ERROR, ESP_ERR_INVALID_ARG);
periph_module_enable(PERIPH_PWM0_MODULE + mcpwm_num);
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
bool mcpwm_gpio_sig = (io_signal <= MCPWM2B);
if (mcpwm_num == MCPWM_UNIT_0) {
if (mcpwm_gpio_sig) {
MCPWM_CHECK((GPIO_IS_VALID_OUTPUT_GPIO(gpio_num)), MCPWM_GPIO_ERROR, ESP_ERR_INVALID_ARG);
gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
gpio_matrix_out(gpio_num, PWM0_OUT0A_IDX + io_signal, 0, 0);
} else {
gpio_set_direction(gpio_num, GPIO_MODE_INPUT);
gpio_matrix_in(gpio_num, PWM0_SYNC0_IN_IDX + io_signal - OFFSET_FOR_GPIO_IDX_1, 0);
}
} else { //MCPWM_UNIT_1
if (mcpwm_gpio_sig) {
MCPWM_CHECK((GPIO_IS_VALID_OUTPUT_GPIO(gpio_num)), MCPWM_GPIO_ERROR, ESP_ERR_INVALID_ARG);
gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
gpio_matrix_out(gpio_num, PWM1_OUT0A_IDX + io_signal, 0, 0);
} else if (io_signal >= MCPWM_SYNC_0 && io_signal < MCPWM_FAULT_2) {
gpio_set_direction(gpio_num, GPIO_MODE_INPUT);
gpio_matrix_in(gpio_num, PWM1_SYNC0_IN_IDX + io_signal - OFFSET_FOR_GPIO_IDX_1, 0);
} else {
gpio_set_direction(gpio_num, GPIO_MODE_INPUT);
gpio_matrix_in(gpio_num, PWM1_SYNC0_IN_IDX + io_signal - OFFSET_FOR_GPIO_IDX_2, 0);
}
}
return ESP_OK;
}
esp_err_t mcpwm_set_pin(mcpwm_unit_t mcpwm_num, const mcpwm_pin_config_t *mcpwm_pin)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
mcpwm_gpio_init(mcpwm_num, MCPWM0A, mcpwm_pin->mcpwm0a_out_num); //MCPWM0A
mcpwm_gpio_init(mcpwm_num, MCPWM0B, mcpwm_pin->mcpwm0b_out_num); //MCPWM0B
mcpwm_gpio_init(mcpwm_num, MCPWM1A, mcpwm_pin->mcpwm1a_out_num); //MCPWM1A
mcpwm_gpio_init(mcpwm_num, MCPWM1B, mcpwm_pin->mcpwm1b_out_num); //MCPWM1B
mcpwm_gpio_init(mcpwm_num, MCPWM2A, mcpwm_pin->mcpwm2a_out_num); //MCPWM2A
mcpwm_gpio_init(mcpwm_num, MCPWM2B, mcpwm_pin->mcpwm2b_out_num); //MCPWM2B
mcpwm_gpio_init(mcpwm_num, MCPWM_SYNC_0, mcpwm_pin->mcpwm_sync0_in_num); //SYNC0
mcpwm_gpio_init(mcpwm_num, MCPWM_SYNC_1, mcpwm_pin->mcpwm_sync1_in_num); //SYNC1
mcpwm_gpio_init(mcpwm_num, MCPWM_SYNC_2, mcpwm_pin->mcpwm_sync2_in_num); //SYNC2
mcpwm_gpio_init(mcpwm_num, MCPWM_FAULT_0, mcpwm_pin->mcpwm_fault0_in_num); //FAULT0
mcpwm_gpio_init(mcpwm_num, MCPWM_FAULT_0, mcpwm_pin->mcpwm_fault1_in_num); //FAULT1
mcpwm_gpio_init(mcpwm_num, MCPWM_FAULT_0, mcpwm_pin->mcpwm_fault2_in_num); //FAULT2
mcpwm_gpio_init(mcpwm_num, MCPWM_CAP_0, mcpwm_pin->mcpwm_cap0_in_num); //CAP0
mcpwm_gpio_init(mcpwm_num, MCPWM_CAP_1, mcpwm_pin->mcpwm_cap1_in_num); //CAP1
mcpwm_gpio_init(mcpwm_num, MCPWM_CAP_2, mcpwm_pin->mcpwm_cap2_in_num); //CAP2
return ESP_OK;
}
esp_err_t mcpwm_start(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->timer[timer_num].mode.start = 2;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_stop(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->timer[timer_num].mode.start = 0;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_set_frequency(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, uint32_t frequency)
{
uint32_t mcpwm_num_of_pulse;
uint32_t previous_period;
uint32_t set_duty_a, set_duty_b;
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
mcpwm_num_of_pulse = MCPWM_CLK / (frequency * (TIMER_CLK_PRESCALE + 1));
previous_period = MCPWM[mcpwm_num]->timer[timer_num].period.period;
MCPWM[mcpwm_num]->timer[timer_num].period.prescale = TIMER_CLK_PRESCALE;
MCPWM[mcpwm_num]->timer[timer_num].period.period = mcpwm_num_of_pulse;
MCPWM[mcpwm_num]->timer[timer_num].period.upmethod = 0;
set_duty_a = (((MCPWM[mcpwm_num]->channel[timer_num].cmpr_value[0].cmpr_val) * mcpwm_num_of_pulse) / previous_period);
set_duty_b = (((MCPWM[mcpwm_num]->channel[timer_num].cmpr_value[1].cmpr_val) * mcpwm_num_of_pulse) / previous_period);
MCPWM[mcpwm_num]->channel[timer_num].cmpr_value[0].cmpr_val = set_duty_a;
MCPWM[mcpwm_num]->channel[timer_num].cmpr_value[1].cmpr_val = set_duty_b;
MCPWM[mcpwm_num]->channel[timer_num].cmpr_cfg.a_upmethod = 0;
MCPWM[mcpwm_num]->channel[timer_num].cmpr_cfg.b_upmethod = 0;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_set_duty(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_operator_t op_num, float duty)
{
uint32_t set_duty;
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(op_num < MCPWM_OPR_MAX, MCPWM_OP_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
set_duty = (MCPWM[mcpwm_num]->timer[timer_num].period.period) * (duty) / 100;
MCPWM[mcpwm_num]->channel[timer_num].cmpr_value[op_num].cmpr_val = set_duty;
MCPWM[mcpwm_num]->channel[timer_num].cmpr_cfg.a_upmethod = BIT(0);
MCPWM[mcpwm_num]->channel[timer_num].cmpr_cfg.b_upmethod = BIT(0);
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_set_duty_in_us(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_operator_t op_num, uint32_t duty)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(op_num < MCPWM_OPR_MAX, MCPWM_OP_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].cmpr_value[op_num].cmpr_val = duty;
MCPWM[mcpwm_num]->channel[timer_num].cmpr_cfg.a_upmethod = BIT(0);
MCPWM[mcpwm_num]->channel[timer_num].cmpr_cfg.b_upmethod = BIT(0);
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_set_duty_type(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_operator_t op_num,
mcpwm_duty_type_t duty_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(op_num < MCPWM_OPR_MAX, MCPWM_OP_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(duty_num < MCPWM_DUTY_MODE_MAX, MCPWM_DUTY_TYPE_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
if (op_num == MCPWM_OPR_A) {
if (MCPWM[mcpwm_num]->timer[timer_num].mode.mode == MCPWM_UP_COUNTER) {
if (duty_num == MCPWM_DUTY_MODE_1) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utea = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utep = 0;
} else { //MCPWM_DUTY_MODE_0
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utea = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utep = 0;
}
} else if (MCPWM[mcpwm_num]->timer[timer_num].mode.mode == MCPWM_DOWN_COUNTER) {
if (duty_num == MCPWM_DUTY_MODE_1) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtep = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtea = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtez = 0;
} else { //MCPWM_DUTY_MODE_0
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtep = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtea = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtez = 0;
}
} else { //Timer count up-down
if (duty_num == MCPWM_DUTY_MODE_1) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utea = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtea = 1;
} else { //MCPWM_DUTY_MODE_0
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utea = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtea = 2;
}
}
}
if (op_num == MCPWM_OPR_B) {
if (MCPWM[mcpwm_num]->timer[timer_num].mode.mode == MCPWM_UP_COUNTER) {
if (duty_num == MCPWM_DUTY_MODE_1) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].uteb = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utep = 0;
} else { //MCPWM_DUTY_MODE_0
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].uteb = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utep = 0;
}
} else if (MCPWM[mcpwm_num]->timer[timer_num].mode.mode == MCPWM_DOWN_COUNTER) {
if (duty_num == MCPWM_DUTY_MODE_1) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtep = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dteb = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtez = 0;
} else { //MCPWM_DUTY_MODE_0
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtep = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dteb = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtez = 0;
}
} else { //Timer count up-down
if (duty_num == MCPWM_DUTY_MODE_1) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].uteb = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dteb = 1;
} else { //MCPWM_DUTY_MODE_0
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].uteb = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dteb = 2;
}
}
}
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_init(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, const mcpwm_config_t *mcpwm_conf)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
periph_module_enable(PERIPH_PWM0_MODULE + mcpwm_num);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->clk_cfg.prescale = MCPWM_CLK_PRESCL;
mcpwm_set_frequency(mcpwm_num, timer_num, mcpwm_conf->frequency);
MCPWM[mcpwm_num]->timer[timer_num].mode.mode = mcpwm_conf ->counter_mode;
mcpwm_set_duty(mcpwm_num, timer_num, 0, mcpwm_conf->cmpr_a);
mcpwm_set_duty(mcpwm_num, timer_num, 1, mcpwm_conf->cmpr_b);
mcpwm_set_duty_type(mcpwm_num, timer_num, 0, mcpwm_conf->duty_mode);
mcpwm_set_duty_type(mcpwm_num, timer_num, 1, mcpwm_conf->duty_mode);
mcpwm_start(mcpwm_num, timer_num);
MCPWM[mcpwm_num]->timer_sel.operator0_sel = 0;
MCPWM[mcpwm_num]->timer_sel.operator1_sel = 1;
MCPWM[mcpwm_num]->timer_sel.operator2_sel = 2;
MCPWM[mcpwm_num]->update_cfg.global_up_en = 1;
MCPWM[mcpwm_num]->update_cfg.global_force_up = 1;
MCPWM[mcpwm_num]->update_cfg.global_force_up = 0;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
uint32_t mcpwm_get_frequency(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num)
{
uint32_t frequency;
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
frequency = MCPWM_CLK / ((MCPWM[mcpwm_num]->timer[timer_num].period.period) * (TIMER_CLK_PRESCALE + 1));
return frequency;
}
float mcpwm_get_duty(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_operator_t op_num)
{
float duty;
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(op_num < MCPWM_OPR_MAX, MCPWM_OP_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
duty = 100.0 * (MCPWM[mcpwm_num]->channel[timer_num].cmpr_value[op_num].cmpr_val) / (MCPWM[mcpwm_num]->timer[timer_num].period.period);
portEXIT_CRITICAL(&mcpwm_spinlock);
return duty;
}
esp_err_t mcpwm_set_signal_high(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_operator_t op_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(op_num < MCPWM_OPR_MAX, MCPWM_OP_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
if (op_num == MCPWM_OPR_A) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utea = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utep = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtez = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtea = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtep = 2;
} else { //MCPWM_OPR_B
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].uteb = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utep = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtez = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dteb = 2;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtep = 2;
}
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_set_signal_low(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_operator_t op_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(op_num < MCPWM_OPR_MAX, MCPWM_OP_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
if (op_num == 0) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utea = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utep = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtez = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtea = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtep = 1;
} if (op_num == 1) {
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utez = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].uteb = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].utep = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtez = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dteb = 1;
MCPWM[mcpwm_num]->channel[timer_num].generator[op_num].dtep = 1;
}
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_carrier_enable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].carrier_cfg.en = 1;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_carrier_disable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].carrier_cfg.en = 0;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_carrier_set_period(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, uint8_t carrier_period)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].carrier_cfg.prescale = carrier_period;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_carrier_set_duty_cycle(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, uint8_t carrier_duty)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].carrier_cfg.duty = carrier_duty;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_carrier_oneshot_mode_enable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, uint8_t pulse_width)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].carrier_cfg.oshtwth = pulse_width;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_carrier_oneshot_mode_disable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].carrier_cfg.oshtwth = 0;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_carrier_output_invert(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num,
mcpwm_carrier_out_ivt_t carrier_ivt_mode)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].carrier_cfg.out_invert = carrier_ivt_mode;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_carrier_init(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, const mcpwm_carrier_config_t *carrier_conf)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
mcpwm_carrier_enable(mcpwm_num, timer_num);
mcpwm_carrier_set_period(mcpwm_num, timer_num, carrier_conf->carrier_period);
mcpwm_carrier_set_duty_cycle(mcpwm_num, timer_num, carrier_conf->carrier_duty);
if (carrier_conf->carrier_os_mode == MCPWM_ONESHOT_MODE_EN) {
mcpwm_carrier_oneshot_mode_enable(mcpwm_num, timer_num, carrier_conf->pulse_width_in_os);
} else {
mcpwm_carrier_oneshot_mode_disable(mcpwm_num, timer_num);
}
mcpwm_carrier_output_invert(mcpwm_num, timer_num, carrier_conf->carrier_ivt_mode);
MCPWM[mcpwm_num]->channel[timer_num].carrier_cfg.in_invert = 0;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_deadtime_enable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_deadtime_type_t dt_mode,
uint32_t red, uint32_t fed)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(dt_mode < MCPWM_DEADTIME_TYPE_MAX, MCPWM_DB_ERROR, ESP_ERR_INVALID_ARG );
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_upmethod = BIT(0);
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_upmethod = BIT(0);
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.clk_sel = 0;
MCPWM[mcpwm_num]->channel[timer_num].db_red_cfg.red = red;
MCPWM[mcpwm_num]->channel[timer_num].db_fed_cfg.fed = fed;
switch (dt_mode) {
case MCPWM_BYPASS_RED:
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 0; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outbypass = 1; //S1
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_outinvert = 0; //S2
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 0; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 0; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_insel = 1; //S5
break;
case MCPWM_BYPASS_FED:
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 1; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outbypass = 0; //S1
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_outinvert = 0; //S2
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 0; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 0; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_insel = 0; //S5
break;
case MCPWM_ACTIVE_HIGH_MODE:
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 0; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outbypass = 0; //S1
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_outinvert = 0; //S2
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 0; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 0; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_insel = 1; //S5
break;
case MCPWM_ACTIVE_LOW_MODE:
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 0; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outbypass = 0; //S1
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_outinvert = 1; //S2
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 1; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 0; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_insel = 1; //S5
break;
case MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE:
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 0; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outbypass = 0; //S1
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_outinvert = 0; //S2
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 1; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 0; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_insel = 1; //S5
break;
case MCPWM_ACTIVE_LOW_COMPLIMENT_MODE:
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 0; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outbypass = 0; //S1
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_outinvert = 1; //S2
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 0; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 1; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_insel = 0; //S5
break;
case MCPWM_ACTIVE_RED_FED_FROM_PWMXA:
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 0; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 0; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 1; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outswap = 1; //S6
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outswap = 0; //S7
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.deb_mode = 1; //S8
break;
case MCPWM_ACTIVE_RED_FED_FROM_PWMXB:
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 0; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 0; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 0; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outswap = 1; //S6
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outswap = 0; //S7
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.deb_mode = 1; //S8
break;
default :
break;
}
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_deadtime_disable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outbypass = 1; //S0
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outbypass = 1; //S1
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_outinvert = 0; //S2
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_outinvert = 0; //S3
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.red_insel = 0; //S4
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.fed_insel = 0; //S5
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.a_outswap = 0; //S6
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.b_outswap = 0; //S7
MCPWM[mcpwm_num]->channel[timer_num].db_cfg.deb_mode = 0; //S8
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_fault_init(mcpwm_unit_t mcpwm_num, mcpwm_fault_input_level_t intput_level, mcpwm_fault_signal_t fault_sig)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
switch (fault_sig) {
case MCPWM_SELECT_F0:
MCPWM[mcpwm_num]->fault_detect.f0_en = 1;
MCPWM[mcpwm_num]->fault_detect.f0_pole = intput_level;
break;
case MCPWM_SELECT_F1:
MCPWM[mcpwm_num]->fault_detect.f1_en = 1;
MCPWM[mcpwm_num]->fault_detect.f1_pole = intput_level;
break;
case MCPWM_SELECT_F2:
MCPWM[mcpwm_num]->fault_detect.f2_en = 1;
MCPWM[mcpwm_num]->fault_detect.f2_pole = intput_level;
break;
default :
break;
}
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_fault_deinit(mcpwm_unit_t mcpwm_num, mcpwm_fault_signal_t fault_sig)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
if (fault_sig == MCPWM_SELECT_F0) {
MCPWM[mcpwm_num]->fault_detect.f0_en = 0;
} else if (fault_sig == MCPWM_SELECT_F1) {
MCPWM[mcpwm_num]->fault_detect.f1_en = 0;
} else { //MCPWM_SELECT_F2
MCPWM[mcpwm_num]->fault_detect.f2_en = 0;
}
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_fault_set_cyc_mode(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_fault_signal_t fault_sig,
mcpwm_action_on_pwmxa_t action_on_pwmxa, mcpwm_action_on_pwmxb_t action_on_pwmxb)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg1.cbcpulse = BIT(0);
if (fault_sig == MCPWM_SELECT_F0) {
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f0_cbc = 1;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f0_ost = 0;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_cbc_d = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_cbc_u = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_cbc_d = action_on_pwmxb;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_cbc_u = action_on_pwmxb;
} else if (fault_sig == MCPWM_SELECT_F1) {
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f1_cbc = 1;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f1_ost = 0;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_cbc_d = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_cbc_u = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_cbc_d = action_on_pwmxb;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_cbc_u = action_on_pwmxb;
} else { //MCPWM_SELECT_F2
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f2_cbc = 1;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f2_ost = 0;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_cbc_d = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_cbc_u = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_cbc_d = action_on_pwmxb;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_cbc_u = action_on_pwmxb;
}
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_fault_set_oneshot_mode(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_fault_signal_t fault_sig,
mcpwm_action_on_pwmxa_t action_on_pwmxa, mcpwm_action_on_pwmxb_t action_on_pwmxb)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
if (fault_sig == MCPWM_SELECT_F0) {
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f0_ost = 1;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f0_cbc = 0;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_ost_d = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_ost_u = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_ost_d = action_on_pwmxb;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_ost_u = action_on_pwmxb;
} else if (fault_sig == MCPWM_SELECT_F1) {
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f1_ost = 1;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f1_cbc = 0;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_ost_d = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_ost_u = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_ost_d = action_on_pwmxb;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_ost_u = action_on_pwmxb;
} else { //MCPWM_SELECT_F2
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f2_ost = 1;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.f2_cbc = 0;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_ost_d = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.a_ost_u = action_on_pwmxa;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_ost_d = action_on_pwmxb;
MCPWM[mcpwm_num]->channel[timer_num].tz_cfg0.b_ost_u = action_on_pwmxb;
}
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_capture_enable(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t cap_sig, mcpwm_capture_on_edge_t cap_edge,
uint32_t num_of_pulse)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->cap_timer_cfg.timer_en = 1;
MCPWM[mcpwm_num]->cap_cfg_ch[cap_sig].en = 1;
MCPWM[mcpwm_num]->cap_cfg_ch[cap_sig].mode = (1 << cap_edge);
MCPWM[mcpwm_num]->cap_cfg_ch[cap_sig].prescale = num_of_pulse;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_capture_disable(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t cap_sig)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->cap_cfg_ch[cap_sig].en = 0;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
uint32_t mcpwm_capture_signal_get_value(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t cap_sig)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
return MCPWM[mcpwm_num]->cap_val_ch[cap_sig];
}
uint32_t mcpwm_capture_signal_get_edge(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t cap_sig)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
if (cap_sig == MCPWM_SELECT_CAP0) {
return ( MCPWM[mcpwm_num]->cap_status.cap0_edge + 1);
} else if (cap_sig == MCPWM_SELECT_CAP1) {
return (MCPWM[mcpwm_num]->cap_status.cap1_edge + 1);
} else { //MCPWM_SELECT_CAP2
return (MCPWM[mcpwm_num]->cap_status.cap2_edge + 1);
}
return 0;
}
esp_err_t mcpwm_sync_enable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_sync_signal_t sync_sig,
uint32_t phase_val)
{
uint32_t set_phase;
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
set_phase = (MCPWM[mcpwm_num]->timer[timer_num].period.period) * (phase_val) / 1000;
MCPWM[mcpwm_num]->timer[timer_num].sync.timer_phase = set_phase;
if (timer_num == MCPWM_TIMER_0) {
MCPWM[mcpwm_num]->timer_synci_cfg.t0_in_sel = sync_sig;
} else if (timer_num == MCPWM_TIMER_1) {
MCPWM[mcpwm_num]->timer_synci_cfg.t1_in_sel = sync_sig;
} else { //MCPWM_TIMER_2
MCPWM[mcpwm_num]->timer_synci_cfg.t2_in_sel = sync_sig;
}
MCPWM[mcpwm_num]->timer[timer_num].sync.out_sel = 0;
MCPWM[mcpwm_num]->timer[timer_num].sync.in_en = 1;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_sync_disable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num)
{
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(timer_num < MCPWM_TIMER_MAX, MCPWM_TIMER_ERROR, ESP_ERR_INVALID_ARG);
portENTER_CRITICAL(&mcpwm_spinlock);
MCPWM[mcpwm_num]->timer[timer_num].sync.in_en = 0;
portEXIT_CRITICAL(&mcpwm_spinlock);
return ESP_OK;
}
esp_err_t mcpwm_isr_register(mcpwm_unit_t mcpwm_num, void (*fn)(void *), void *arg, int intr_alloc_flags, intr_handle_t *handle)
{
esp_err_t ret;
MCPWM_CHECK(mcpwm_num < MCPWM_UNIT_MAX, MCPWM_UNIT_NUM_ERROR, ESP_ERR_INVALID_ARG);
MCPWM_CHECK(fn != NULL, MCPWM_PARAM_ADDR_ERROR, ESP_ERR_INVALID_ARG);
ret = esp_intr_alloc((ETS_PWM0_INTR_SOURCE + mcpwm_num), intr_alloc_flags, fn, arg, handle);
return ret;
}