From 4ec13dcf7439959b52e84327a5fd7d5514b4c44c Mon Sep 17 00:00:00 2001 From: Li Junru Date: Mon, 4 Sep 2023 17:20:01 +0800 Subject: [PATCH] esp_simplefoc: support mcu without mcpwm --- components/motor/esp_simplefoc/CHANGELOG.md | 3 +++ .../motor/esp_simplefoc/idf_component.yml | 8 ++++++-- .../port/esp/esp_hal_bldc_3pwm.cpp | 20 +++++++++++++++---- .../port/esp/include/esp_hal_bldc_3pwm.h | 12 ++++++++--- .../port/esp/include/esp_hal_serial.h | 10 +++------- examples/.build-rules.yml | 8 ++++---- .../motor/foc_openloop_control/main/main.cpp | 2 ++ .../motor/foc_velocity_control/main/main.cpp | 4 +++- 8 files changed, 46 insertions(+), 21 deletions(-) diff --git a/components/motor/esp_simplefoc/CHANGELOG.md b/components/motor/esp_simplefoc/CHANGELOG.md index 49ac02725..d9b3d4389 100644 --- a/components/motor/esp_simplefoc/CHANGELOG.md +++ b/components/motor/esp_simplefoc/CHANGELOG.md @@ -1,5 +1,8 @@ # ChangeLog +## v0.1.0 - 2023-11-14 + +* Support chip whitch without mcpwm driver ## V0.0.3 - 2023-8-16 ### Enhancements: * FOC: diff --git a/components/motor/esp_simplefoc/idf_component.yml b/components/motor/esp_simplefoc/idf_component.yml index baef34b0c..3fd1fd43d 100644 --- a/components/motor/esp_simplefoc/idf_component.yml +++ b/components/motor/esp_simplefoc/idf_component.yml @@ -1,7 +1,11 @@ -version: 0.0.2 +version: 0.1.0 targets: - esp32 + - esp32s2 - esp32s3 + - esp32c3 + - esp32c6 + - esp32h2 description: Espressif's additions to SimpleFOC url: https://github.com/espressif/esp-iot-solution/tree/master/components/motor/esp_simplefoc repository: https://github.com/espressif/esp-iot-solution.git @@ -10,7 +14,7 @@ dependencies: idf: version: '>=5.0' espressif/arduino-foc: - version: ">=2.3.0~1" + version: ">=2.3.0~3" public: true espressif/iqmath: "^1.11.0" examples: diff --git a/components/motor/esp_simplefoc/port/esp/esp_hal_bldc_3pwm.cpp b/components/motor/esp_simplefoc/port/esp/esp_hal_bldc_3pwm.cpp index 87762836c..fb2a6db7e 100644 --- a/components/motor/esp_simplefoc/port/esp/esp_hal_bldc_3pwm.cpp +++ b/components/motor/esp_simplefoc/port/esp/esp_hal_bldc_3pwm.cpp @@ -7,6 +7,10 @@ #include #include "esp_hal_bldc_3pwm.h" +#ifdef CONFIG_SOC_MCPWM_SUPPORTED +#define MCPWM_TIMER_CLK_SRC MCPWM_TIMER_CLK_SRC_DEFAULT +#endif + std::vector>> HardwareResource = { {DriverMode::mcpwm, {0, 0}}, {DriverMode::mcpwm, {1, 0}}, @@ -275,7 +279,7 @@ int BLDCDriver3PWM::init() printf("No available Driver.\n"); return 0; } - +#ifdef CONFIG_SOC_MCPWM_SUPPORTED if (ret == 1) { // mcpwm driverMode = DriverMode::mcpwm; @@ -285,7 +289,7 @@ int BLDCDriver3PWM::init() // Init mcpwm driver. mcpwm_timer_config_t timer_config = { .group_id = auto_mcpwm_group, - .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, + .clk_src = MCPWM_TIMER_CLK_SRC, .resolution_hz = _PWM_TIMEBASE_RESOLUTION_HZ, .count_mode = MCPWM_TIMER_COUNT_MODE_UP, // centeral mode .period_ticks = (uint32_t)(1 * _PWM_TIMEBASE_RESOLUTION_HZ / _PWM_FREQUENCY), // real frequency is pwm_frequency/2 @@ -336,7 +340,9 @@ int BLDCDriver3PWM::init() mcpwm_period = timer_config.period_ticks; initialized = 1; - } else if (ret == 2) { + } else +#endif + if (ret == 2) { // ledc driverMode = DriverMode::ledc; printf("Current Driver uses LEDC Channel:%d %d %d\n", auto_ledc_channels[0], auto_ledc_channels[1], auto_ledc_channels[2]); @@ -373,6 +379,7 @@ int BLDCDriver3PWM::init() return 1; } +#ifdef CONFIG_SOC_MCPWM_SUPPORTED int BLDCDriver3PWM::init(int _mcpwm_group) { // PWM pins @@ -407,7 +414,7 @@ int BLDCDriver3PWM::init(int _mcpwm_group) // Init mcpwm driver. mcpwm_timer_config_t timer_config = { .group_id = _mcpwm_group, - .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, + .clk_src = MCPWM_TIMER_CLK_SRC, .resolution_hz = _PWM_TIMEBASE_RESOLUTION_HZ, .count_mode = MCPWM_TIMER_COUNT_MODE_UP, // centeral mode .period_ticks = (uint32_t)(1 * _PWM_TIMEBASE_RESOLUTION_HZ / _PWM_FREQUENCY), // real frequency is pwm_frequency/2 @@ -461,6 +468,7 @@ int BLDCDriver3PWM::init(int _mcpwm_group) return 1; } +#endif int BLDCDriver3PWM::init(std::vector _ledc_channels) { @@ -535,6 +543,7 @@ int BLDCDriver3PWM::deinit() } setLedcChannelUnUsed(ledc_channels); break; +#ifdef CONFIG_SOC_MCPWM_SUPPORTED case DriverMode::mcpwm: for (int i = 0; i < 3; i++) { if (mcpwm_del_generator(generator[i]) != ESP_OK) { @@ -559,6 +568,7 @@ int BLDCDriver3PWM::deinit() setMcpwmGroupUnUsed(mcpwm_group); break; +#endif default: break; } @@ -582,11 +592,13 @@ void BLDCDriver3PWM::halPwmWrite() ESP_ERROR_CHECK(ledc_set_duty(_LEDC_MODE, static_cast(ledc_channels[2]), (uint32_t)((mcpwm_period * dc_c)))); ESP_ERROR_CHECK(ledc_update_duty(_LEDC_MODE, static_cast(ledc_channels[2]))); break; +#ifdef CONFIG_SOC_MCPWM_SUPPORTED case DriverMode::mcpwm: ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator[0], (uint32_t)((mcpwm_period * dc_a)))); ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator[1], (uint32_t)((mcpwm_period * dc_b)))); ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comparator[2], (uint32_t)((mcpwm_period * dc_c)))); break; +#endif default: break; } diff --git a/components/motor/esp_simplefoc/port/esp/include/esp_hal_bldc_3pwm.h b/components/motor/esp_simplefoc/port/esp/include/esp_hal_bldc_3pwm.h index fc6259c28..70e516f70 100644 --- a/components/motor/esp_simplefoc/port/esp/include/esp_hal_bldc_3pwm.h +++ b/components/motor/esp_simplefoc/port/esp/include/esp_hal_bldc_3pwm.h @@ -12,8 +12,10 @@ #include "time_utils.h" #include "defaults.h" #include "drivers/hardware_api.h" -#include "driver/mcpwm_prelude.h" #include "driver/ledc.h" +#ifdef CONFIG_SOC_MCPWM_SUPPORTED +#include "driver/mcpwm_prelude.h" +#endif #define _PWM_FREQUENCY 20000 /*!< default frequency of MCPWM */ #define _PWM_FREQUENCY_MAX 50000 /*!< Max frequency of MCPWM */ @@ -52,6 +54,7 @@ class BLDCDriver3PWM : public BLDCDriver { */ int init() override; +#ifdef CONFIG_SOC_MCPWM_SUPPORTED /** * @brief Motor hardware init function, using MCPWM. * @@ -61,6 +64,7 @@ class BLDCDriver3PWM : public BLDCDriver { * - 1 Success */ int init(int _mcpwm_group); +#endif /** * @brief Motor hardware init function, using LEDC. @@ -128,11 +132,13 @@ class BLDCDriver3PWM : public BLDCDriver { private: DriverMode driverMode; - int mcpwm_group; std::vector ledc_channels; + uint32_t mcpwm_period; +#ifdef CONFIG_SOC_MCPWM_SUPPORTED + int mcpwm_group; mcpwm_gen_handle_t generator[3] = {}; mcpwm_cmpr_handle_t comparator[3]; mcpwm_oper_handle_t oper[3]; mcpwm_timer_handle_t timer = NULL; - uint32_t mcpwm_period; +#endif }; diff --git a/components/motor/esp_simplefoc/port/esp/include/esp_hal_serial.h b/components/motor/esp_simplefoc/port/esp/include/esp_hal_serial.h index 2f7362a7f..afd4bfaf3 100644 --- a/components/motor/esp_simplefoc/port/esp/include/esp_hal_serial.h +++ b/components/motor/esp_simplefoc/port/esp/include/esp_hal_serial.h @@ -10,19 +10,15 @@ #include "driver/gpio.h" #include "driver/uart.h" #include "soc/soc_caps.h" +#include "soc/uart_channel.h" /** * ESP32-S-Devkitc default serial port: tx:1 rx:3 * ESP32-S3-Devkitc default serial port: tx:43 rx:44 */ -#if CONFIG_IDF_TARGET_ESP32 -#define TX0 1 -#define RX0 3 -#elif CONFIG_IDF_TARGET_ESP32S3 -#define TX0 43 -#define RX0 44 -#endif +#define TX0 UART_NUM_0_TXD_DIRECT_GPIO_NUM +#define RX0 UART_NUM_0_RXD_DIRECT_GPIO_NUM class HardwareSerial : public Stream { public: diff --git a/examples/.build-rules.yml b/examples/.build-rules.yml index 12714b122..b9cb597db 100644 --- a/examples/.build-rules.yml +++ b/examples/.build-rules.yml @@ -96,12 +96,12 @@ examples/lighting/lightbulb: - if: INCLUDE_DEFAULT == 1 examples/motor/foc_openloop_control: - enable: - - if: IDF_TARGET in ["esp32","esp32s3"] + disable: + - if: IDF_TARGET == "esp32c2" examples/motor/foc_velocity_control: - enable: - - if: IDF_TARGET in ["esp32","esp32s3"] + disable: + - if: IDF_TARGET == "esp32c2" examples/ota/simple_ota_example: enable: diff --git a/examples/motor/foc_openloop_control/main/main.cpp b/examples/motor/foc_openloop_control/main/main.cpp index c49432b53..b67d8a659 100644 --- a/examples/motor/foc_openloop_control/main/main.cpp +++ b/examples/motor/foc_openloop_control/main/main.cpp @@ -13,7 +13,9 @@ #include "esp_log.h" #include "esp_simplefoc.h" +#if CONFIG_SOC_MCPWM_SUPPORTED #define USING_MCPWM +#endif BLDCMotor motor = BLDCMotor(14); BLDCDriver3PWM driver = BLDCDriver3PWM(17, 16, 15); diff --git a/examples/motor/foc_velocity_control/main/main.cpp b/examples/motor/foc_velocity_control/main/main.cpp index 2d6760c93..8931eee1d 100644 --- a/examples/motor/foc_velocity_control/main/main.cpp +++ b/examples/motor/foc_velocity_control/main/main.cpp @@ -13,7 +13,9 @@ #include "esp_log.h" #include "esp_simplefoc.h" +#if CONFIG_SOC_MCPWM_SUPPORTED #define USING_MCPWM +#endif void angle_sensor_init() { @@ -26,7 +28,7 @@ float angle_sensor_get() } BLDCMotor motor = BLDCMotor(14); -BLDCDriver3PWM driver = BLDCDriver3PWM(17, 16, 15); +BLDCDriver3PWM driver = BLDCDriver3PWM(4, 5, 6); GenericSensor sensor = GenericSensor(angle_sensor_get, angle_sensor_init); float target_value = 0.0f;