1#ifndef ESP32_DRIVER_MCPWM_H
2#define ESP32_DRIVER_MCPWM_H
4#include "../../hardware_api.h"
6#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
8#include "driver/mcpwm_prelude.h"
9#include "soc/mcpwm_reg.h"
10#include "soc/mcpwm_struct.h"
11#include "esp_idf_version.h"
14#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
15#error SimpleFOC: ESP-IDF version 4 or lower detected. Please update to ESP-IDF 5.x and Arduino-esp32 3.0 (or higher)
18#ifndef SIMPLEFOC_ESP32_HW_DEADTIME
19 #define SIMPLEFOC_ESP32_HW_DEADTIME true
23typedef struct ESP32MCPWMDriverParams {
26 mcpwm_timer_handle_t timers[2];
27 mcpwm_oper_handle_t oper[3];
28 mcpwm_cmpr_handle_t comparator[6];
29 mcpwm_gen_handle_t generator[6];
30 uint32_t mcpwm_period;
32} ESP32MCPWMDriverParams;
35#ifndef SIMPLEFOC_DISABLE_DEBUG
36#define SIMPLEFOC_ESP32_DEBUG(tag, str)\
37 SimpleFOCDebug::println( "ESP32-"+String(tag)+ ": "+ String(str));
39#define SIMPLEFOC_ESP32_DEBUG(tag, str)
42#define SIMPLEFOC_ESP32_DRV_DEBUG(str)\
43 SIMPLEFOC_ESP32_DEBUG("DRV", str);\
47#define CHECK_ERR(func_call, message) \
48 if ((func_call) != ESP_OK) { \
49 SIMPLEFOC_ESP32_DRV_DEBUG("ERROR - " + String(message)); \
50 return SIMPLEFOC_DRIVER_INIT_FAILED; \
56#define _MCPWM_FREQ 160e6f
57#define _PWM_TIMEBASE_RESOLUTION_HZ (_MCPWM_FREQ)
59#define _PWM_FREQUENCY 25000
60#define _PWM_FREQUENCY_MAX 50000
71bool _hasAvailablePins(
int group,
int no_pins);
78uint8_t _findLastTimer(
int group);
86uint8_t _findNextTimer(
int group);
101int _findBestGroup(
int no_pins,
long pwm_freq,
int* group,
int* timer);
110int _configureCenterAlign(mcpwm_gen_handle_t gena, mcpwm_cmpr_handle_t cmpa,
bool inverted);
117uint32_t _calcPWMPeriod(
long pwm_frequency);
123long _calcPWMFreq(
long pwm_period);
134void* _configure6PWMPinsMCPWM(
long pwm_frequency,
int mcpwm_group,
int timer_no,
float dead_zone,
int* pins);
144void* _configurePinsMCPWM(
long pwm_frequency,
int mcpwm_group,
int timer_no,
int no_pins,
int* pins);
151void _setDutyCycle(mcpwm_cmpr_handle_t cmpr, uint32_t mcpwm_period,
float duty_cycle);
159void _forcePhaseState(mcpwm_gen_handle_t generator_high, mcpwm_gen_handle_t generator_low,
PhaseState phase_state);
165void _notifyLowSideUsingComparator(
int group_id);
float float PhaseState * phase_state