Skip to content

Commit

Permalink
Merge branch 'feature/add_esp_simplefoc_support_esp32c3' into 'master'
Browse files Browse the repository at this point in the history
esp_simplefoc: support mcu without mcpwm

See merge request ae_group/esp-iot-solution!886
  • Loading branch information
leeebo committed Nov 16, 2023
2 parents 18fefde + 4ec13dc commit 223d722
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
3 changes: 3 additions & 0 deletions components/motor/esp_simplefoc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
8 changes: 6 additions & 2 deletions components/motor/esp_simplefoc/idf_component.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
20 changes: 16 additions & 4 deletions components/motor/esp_simplefoc/port/esp/esp_hal_bldc_3pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <map>
#include "esp_hal_bldc_3pwm.h"

#ifdef CONFIG_SOC_MCPWM_SUPPORTED
#define MCPWM_TIMER_CLK_SRC MCPWM_TIMER_CLK_SRC_DEFAULT
#endif

std::vector<std::pair<DriverMode, std::tuple<int, int>>> HardwareResource = {
{DriverMode::mcpwm, {0, 0}},
{DriverMode::mcpwm, {1, 0}},
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -373,6 +379,7 @@ int BLDCDriver3PWM::init()
return 1;
}

#ifdef CONFIG_SOC_MCPWM_SUPPORTED
int BLDCDriver3PWM::init(int _mcpwm_group)
{
// PWM pins
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -461,6 +468,7 @@ int BLDCDriver3PWM::init(int _mcpwm_group)

return 1;
}
#endif

int BLDCDriver3PWM::init(std::vector<int> _ledc_channels)
{
Expand Down Expand Up @@ -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) {
Expand All @@ -559,6 +568,7 @@ int BLDCDriver3PWM::deinit()

setMcpwmGroupUnUsed(mcpwm_group);
break;
#endif
default:
break;
}
Expand All @@ -582,11 +592,13 @@ void BLDCDriver3PWM::halPwmWrite()
ESP_ERROR_CHECK(ledc_set_duty(_LEDC_MODE, static_cast<ledc_channel_t>(ledc_channels[2]), (uint32_t)((mcpwm_period * dc_c))));
ESP_ERROR_CHECK(ledc_update_duty(_LEDC_MODE, static_cast<ledc_channel_t>(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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -52,6 +54,7 @@ class BLDCDriver3PWM : public BLDCDriver {
*/
int init() override;

#ifdef CONFIG_SOC_MCPWM_SUPPORTED
/**
* @brief Motor hardware init function, using MCPWM.
*
Expand All @@ -61,6 +64,7 @@ class BLDCDriver3PWM : public BLDCDriver {
* - 1 Success
*/
int init(int _mcpwm_group);
#endif

/**
* @brief Motor hardware init function, using LEDC.
Expand Down Expand Up @@ -128,11 +132,13 @@ class BLDCDriver3PWM : public BLDCDriver {

private:
DriverMode driverMode;
int mcpwm_group;
std::vector<int> 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
};
10 changes: 3 additions & 7 deletions components/motor/esp_simplefoc/port/esp/include/esp_hal_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions examples/.build-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions examples/motor/foc_openloop_control/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion examples/motor/foc_velocity_control/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
Expand Down

0 comments on commit 223d722

Please sign in to comment.