SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
esp32_driver_mcpwm.h
Go to the documentation of this file.
1#ifndef ESP32_DRIVER_MCPWM_H
2#define ESP32_DRIVER_MCPWM_H
3
4#include "../../hardware_api.h"
5
6#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
7
8#include "driver/mcpwm_prelude.h"
9#include "soc/mcpwm_reg.h"
10#include "soc/mcpwm_struct.h"
11#include "esp_idf_version.h"
12
13// version check - this mcpwm driver is specific for ESP-IDF 5.x and arduino-esp32 3.x
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)
16#endif
17
18#ifndef SIMPLEFOC_ESP32_HW_DEADTIME
19 #define SIMPLEFOC_ESP32_HW_DEADTIME true // TODO: Change to false when sw-deadtime & phase_state is approved ready for general use.
20#endif
21
22//!< ESP32 MCPWM driver parameters
23typedef struct ESP32MCPWMDriverParams {
24 long pwm_frequency; //!< frequency of the pwm signal
25 int group_id; //!< group of the mcpwm
26 mcpwm_timer_handle_t timers[2]; //!< timers of the mcpwm
27 mcpwm_oper_handle_t oper[3]; //!< operators of the mcpwm
28 mcpwm_cmpr_handle_t comparator[6]; //!< comparators of the mcpwm
29 mcpwm_gen_handle_t generator[6]; //!< generators of the mcpwm
30 uint32_t mcpwm_period; //!< period of the pwm signal
31 float dead_zone; //!< dead zone of the pwm signal
32} ESP32MCPWMDriverParams;
33
34
35#ifndef SIMPLEFOC_DISABLE_DEBUG
36#define SIMPLEFOC_ESP32_DEBUG(tag, str)\
37 SimpleFOCDebug::println( "ESP32-"+String(tag)+ ": "+ String(str));
38#else
39#define SIMPLEFOC_ESP32_DEBUG(tag, str)
40#endif
41
42#define SIMPLEFOC_ESP32_DRV_DEBUG(str)\
43 SIMPLEFOC_ESP32_DEBUG("DRV", str);\
44
45// macro for checking the error of the mcpwm functions
46// if the function returns an error the function will return SIMPLEFOC_DRIVER_INIT_FAILED
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; \
51 }
52
53
54// ABI bus frequency - would be better to take it from somewhere
55// but I did nto find a good exposed variable
56#define _MCPWM_FREQ 160e6f
57#define _PWM_TIMEBASE_RESOLUTION_HZ (_MCPWM_FREQ) /*!< Resolution of MCPWM */
58// pwm frequency settings
59#define _PWM_FREQUENCY 25000 // 25khz
60#define _PWM_FREQUENCY_MAX 50000 // 50kHz
61
62
63// low-level configuration API
64
65/**
66 * checking if group has pins available
67 * @param group - group of the mcpwm
68 * @param no_pins - number of pins
69 * @returns true if pins are available, false otherwise
70 */
71bool _hasAvailablePins(int group, int no_pins);
72/**
73 * function finding the last timer in the group
74 * @param group - group of the mcpwm
75 * @returns index of the last timer in the group
76 * -1 if no timer instantiated yet
77 */
78uint8_t _findLastTimer(int group);
79
80/**
81 * function finding the next timer in the group
82 * @param group - group of the mcpwm
83 * @returns index of the next timer in the group
84 * -1 if all timers are used
85 */
86uint8_t _findNextTimer(int group);
87
88
89/**
90 * function finding the best group and timer for the pwm signals
91 *
92 * @param no_pins - number of pins
93 * @param pwm_freq - frequency of the pwm signal
94 * @param group - pointer to the group
95 * @param timer - pointer to the timer
96 * @returns
97 * 1 if solution found in one group
98 * 2 if solution requires using both groups
99 * 0 if no solution possible
100 */
101int _findBestGroup(int no_pins, long pwm_freq, int* group, int* timer);
102
103
104/**
105 * function configuring the center alignement and inversion of a pwm signal
106 * @param gena - mcpwm generator handle
107 * @param cmpa - mcpwm comparator handle
108 * @param inverted - true if the signal is inverted, false otherwise
109 */
110int _configureCenterAlign(mcpwm_gen_handle_t gena, mcpwm_cmpr_handle_t cmpa, bool inverted);
111
112/**
113 * function calculating the pwm period
114 * @param pwm_frequency - frequency of the pwm signal
115 * @return uint32_t - period of the pwm signal
116 */
117uint32_t _calcPWMPeriod(long pwm_frequency);
118/**
119 * function calculating the pwm frequency
120 * @param pwm_period - period of the pwm signal
121 * @return long - frequency of the pwm signal
122 */
123long _calcPWMFreq(long pwm_period);
124
125/**
126 * function configuring the MCPWM for 6pwm
127 * @param pwm_frequency - frequency of the pwm signal
128 * @param mcpwm_group - group of the mcpwm
129 * @param timer_no - timer number
130 * @param dead_zone - dead zone of the pwm signal
131 * @param pins - array of pins
132 * @return ESP32MCPWMDriverParams* - pointer to the driver parameters if successful, -1 if failed
133 */
134void* _configure6PWMPinsMCPWM(long pwm_frequency, int mcpwm_group, int timer_no, float dead_zone, int* pins);
135/**
136 * function configuring the MCPWM for pwm generation
137 * @param pwm_frequency - frequency of the pwm signal
138 * @param mcpwm_group - group of the mcpwm
139 * @param timer_no - timer number
140 * @param no_pins - number of pins
141 * @param pins - array of pins
142 * @return ESP32MCPWMDriverParams* - pointer to the driver parameters if successful, -1 if failed
143 */
144void* _configurePinsMCPWM(long pwm_frequency, int mcpwm_group, int timer_no, int no_pins, int* pins);
145/**
146 * function setting the duty cycle to the MCPWM channel
147 * @param cmpr - mcpwm channel handle
148 * @param mcpwm_period - period of the pwm signal
149 * @param duty_cycle - duty cycle of the pwm signal
150 */
151void _setDutyCycle(mcpwm_cmpr_handle_t cmpr, uint32_t mcpwm_period, float duty_cycle);
152
153/**
154 * function setting the phase state
155 * @param generator_high - mcpwm generator handle for the high side
156 * @param generator_low - mcpwm generator handle for the low side
157 * @param phase_state - phase state
158 */
159void _forcePhaseState(mcpwm_gen_handle_t generator_high, mcpwm_gen_handle_t generator_low, PhaseState phase_state);
160
161/**
162 * function notifying the driver that low side is used with comparator
163 * @param group_id - mcpwm group id
164 */
165void _notifyLowSideUsingComparator(int group_id);
166
167#endif
168#endif
PhaseState
Definition FOCDriver.h:7
float float PhaseState * phase_state