Skip to content

Commit 1eb9f02

Browse files
Merge branch 'feat/modify_bldc_control_component' into 'master'
feat: add fan support within matter-one project See merge request ae_group/esp-iot-solution!1403
2 parents 4cc44ab + 8ee1836 commit 1eb9f02

File tree

7 files changed

+78
-23
lines changed

7 files changed

+78
-23
lines changed

components/motor/esp_sensorless_bldc_control/CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# ChangeLog
22

3+
## v1.0.1 - 2025-10-22
4+
5+
### Enhancements
6+
7+
* Add fan support within matter-one project
8+
* Place alignment_comparer_get_value() and read_comparer_on_full() functions in IRAM.
9+
* Add 'is_running' flag to assist upper layer in synchronising the status of the BLDC controller.
10+
* Modify the registration method of mcpwm_timer_event_callbacks_t to utilise dynamic memory allocation.
11+
312
## v1.0.0 - 2024-9-20
413

514
* Component version maintenance.
@@ -25,4 +34,4 @@
2534

2635
### Enhancements
2736

28-
* Init version
37+
* Init version

components/motor/esp_sensorless_bldc_control/bldc_control.c

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <stdbool.h>
8+
#include <string.h>
79
#include "freertos/FreeRTOS.h"
810
#include "freertos/task.h"
911
#include "bldc_control.h"
@@ -32,6 +34,8 @@ typedef struct {
3234
void *zero_cross_handle;
3335
esp_err_t (*control_operation)(void *handle);
3436
int delayCnt; /*!< Delay count, each plus one is an interrupt function time */
37+
bool is_running; /*!< avoid duplicate start */
38+
mcpwm_timer_event_callbacks_t *cbs; /*!< dynamically allocated timer callbacks */
3539
} bldc_control_t;
3640

3741
// Table to lookup bldc control event name
@@ -107,11 +111,17 @@ esp_err_t bldc_control_init(bldc_control_handle_t *handle, bldc_control_config_t
107111
ret = bldc_zero_cross_comparer_init(&control->zero_cross_handle, &config->zero_cross_comparer_config, &control->control_param);
108112
BLDC_CHECK_GOTO(ret == ESP_OK, "bldc_zero_cross_comparer_init failed", deinit);
109113
control->zero_cross = &bldc_zero_cross_comparer_operation;
110-
mcpwm_timer_event_callbacks_t cbs = {
111-
.on_full = &read_comparer_on_full,
112-
};
113-
config->six_step_config.upper_switch_config.bldc_mcpwm.cbs = &cbs;
114+
115+
control->cbs = malloc(sizeof(mcpwm_timer_event_callbacks_t));
116+
if (!control->cbs) {
117+
ESP_LOGE(TAG, "malloc cbs failed");
118+
goto deinit;
119+
}
120+
memset(control->cbs, 0, sizeof(mcpwm_timer_event_callbacks_t));
121+
control->cbs->on_full = &read_comparer_on_full;
122+
config->six_step_config.upper_switch_config.bldc_mcpwm.cbs = control->cbs;
114123
config->six_step_config.upper_switch_config.bldc_mcpwm.timer_cb_user_data = (void *)control->zero_cross_handle;
124+
115125
break;
116126
}
117127
#if CONFIG_SOC_MCPWM_SUPPORTED
@@ -123,11 +133,17 @@ esp_err_t bldc_control_init(bldc_control_handle_t *handle, bldc_control_config_t
123133
ESP_LOGE(TAG, "error: control type must be mcpwm");
124134
goto deinit;
125135
}
126-
mcpwm_timer_event_callbacks_t cbs = {
127-
.on_full = &read_adc_on_full,
128-
};
129-
config->six_step_config.upper_switch_config.bldc_mcpwm.cbs = &cbs;
136+
137+
control->cbs = malloc(sizeof(mcpwm_timer_event_callbacks_t));
138+
if (!control->cbs) {
139+
ESP_LOGE(TAG, "malloc cbs failed");
140+
goto deinit;
141+
}
142+
memset(control->cbs, 0, sizeof(mcpwm_timer_event_callbacks_t));
143+
control->cbs->on_full = &read_adc_on_full;
144+
config->six_step_config.upper_switch_config.bldc_mcpwm.cbs = control->cbs;
130145
config->six_step_config.upper_switch_config.bldc_mcpwm.timer_cb_user_data = (void *)control->zero_cross_handle;
146+
131147
break;
132148
}
133149
#endif
@@ -155,7 +171,12 @@ esp_err_t bldc_control_init(bldc_control_handle_t *handle, bldc_control_config_t
155171
pid_ctrl_config_t pid_ctrl_config = {
156172
.init_param = PID_CTRL_PARAMETER_DEFAULT(),
157173
};
158-
pid_new_control_block(&pid_ctrl_config, &control->pid);
174+
ret = pid_new_control_block(&pid_ctrl_config, &control->pid);
175+
if (ret != ESP_OK || control->pid == NULL) {
176+
ESP_LOGE(TAG, "pid_new_control_block failed: %d", ret);
177+
ret = (ret != ESP_OK) ? ret : ESP_FAIL;
178+
goto deinit;
179+
}
159180
}
160181

161182
control->speed_mode = config->speed_mode;
@@ -164,6 +185,9 @@ esp_err_t bldc_control_init(bldc_control_handle_t *handle, bldc_control_config_t
164185

165186
return ESP_OK;
166187
deinit:
188+
if (control->cbs) {
189+
free(control->cbs);
190+
}
167191
if (control->change_phase_handle) {
168192
// todo change_phase_deinit
169193
free(control->change_phase_handle);
@@ -204,6 +228,10 @@ esp_err_t bldc_control_deinit(bldc_control_handle_t *handle)
204228
break;
205229
}
206230

231+
if (control->cbs) {
232+
free(control->cbs);
233+
control->cbs = NULL;
234+
}
207235
if (control->xSemaphore) {
208236
vSemaphoreDelete(control->xSemaphore);
209237
}
@@ -218,6 +246,9 @@ esp_err_t bldc_control_deinit(bldc_control_handle_t *handle)
218246
esp_err_t bldc_control_start(bldc_control_handle_t *handle, uint32_t expect_Speed_rpm)
219247
{
220248
bldc_control_t *control = (bldc_control_t *)handle;
249+
if (control->is_running) {
250+
return bldc_control_set_speed_rpm(handle, (int)expect_Speed_rpm);
251+
}
221252
switch (control->control_mode) {
222253
case BLDC_SIX_STEP:
223254
bldc_six_step_start(control->change_phase_handle);
@@ -245,15 +276,27 @@ esp_err_t bldc_control_start(bldc_control_handle_t *handle, uint32_t expect_Spee
245276
control->control_param.inject_count = 0;
246277
control->control_param.adc_bemf_phase = 0;
247278

248-
pid_reset_ctrl_block(control->pid);
279+
if (control->speed_mode == SPEED_CLOSED_LOOP) {
280+
if (control->pid == NULL) {
281+
ESP_LOGE(TAG, "PID not initialized in CLOSED_LOOP mode");
282+
return ESP_ERR_INVALID_STATE;
283+
}
284+
pid_reset_ctrl_block(control->pid);
285+
}
249286

250-
bldc_control_dispatch_event(BLDC_CONTROL_START, NULL, 0);
287+
// Start timer then dispatch START event if success
251288
#if INJECT_ENABLE
252289
control->control_param.status = INJECT;
253290
#else
254291
control->control_param.status = ALIGNMENT;
255292
#endif // INJECT_ENABLE
256-
bldc_gptimer_start(control->gptimer);
293+
esp_err_t err = bldc_gptimer_start(control->gptimer);
294+
if (err != ESP_OK) {
295+
ESP_LOGE(TAG, "bldc_gptimer_start failed: %d", err);
296+
return err;
297+
}
298+
control->is_running = true;
299+
bldc_control_dispatch_event(BLDC_CONTROL_START, NULL, 0);
257300

258301
return ESP_OK;
259302
}
@@ -284,6 +327,7 @@ esp_err_t bldc_control_stop(bldc_control_handle_t *handle)
284327
break;
285328
}
286329

330+
control->is_running = false;
287331
bldc_control_dispatch_event(BLDC_CONTROL_STOP, NULL, 0);
288332
return ESP_OK;
289333
}

components/motor/esp_sensorless_bldc_control/control/bldc_zero_cross_adc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef struct {
1818
uint32_t speed_cnt;
1919
} bldc_zero_cross_adc_t;
2020

21-
bool read_adc_on_full(mcpwm_timer_handle_t timer, const mcpwm_timer_event_data_t *edata, void *user_data)
21+
bool IRAM_ATTR read_adc_on_full(mcpwm_timer_handle_t timer, const mcpwm_timer_event_data_t *edata, void *user_data)
2222
{
2323
bldc_zero_cross_adc_t *zero_cross = (bldc_zero_cross_adc_t *)user_data;
2424
if (zero_cross->control_param->status == INJECT) {

components/motor/esp_sensorless_bldc_control/control/bldc_zero_cross_comparer.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -8,6 +8,7 @@
88
#include "bldc_zero_cross_comparer.h"
99
#include "bldc_common.h"
1010
#include "bldc_snls_lib.h"
11+
#include "hal/gpio_ll.h"
1112

1213
static const char *TAG = "bldc_zero_cross";
1314

@@ -19,11 +20,11 @@ typedef struct {
1920
uint16_t queue_filter_state[PHASE_MAX]; /*!< State after three-phase filtering */
2021
} bldc_zero_cross_comparer_t;
2122

22-
static void alignment_comparer_get_value(bldc_zero_cross_comparer_t *zero_cross)
23+
static void IRAM_ATTR alignment_comparer_get_value(bldc_zero_cross_comparer_t *zero_cross)
2324
{
2425
for (int i = 0; i < PHASE_MAX; i++) {
2526
zero_cross->alignment_queue_value[i] = zero_cross->alignment_queue_value[i] << 1;
26-
zero_cross->alignment_queue_value[i] |= gpio_get_level(zero_cross->alignment_pin[i]);
27+
zero_cross->alignment_queue_value[i] |= gpio_ll_get_level(&GPIO, zero_cross->alignment_pin[i]);
2728
}
2829
}
2930

@@ -48,7 +49,7 @@ static uint8_t bldc_umef_edge(uint8_t val)
4849
return 2;
4950
}
5051

51-
bool read_comparer_on_full(mcpwm_timer_handle_t timer, const mcpwm_timer_event_data_t *edata, void *user_data)
52+
bool IRAM_ATTR read_comparer_on_full(mcpwm_timer_handle_t timer, const mcpwm_timer_event_data_t *edata, void *user_data)
5253
{
5354
bldc_zero_cross_comparer_t *zero_cross = (bldc_zero_cross_comparer_t *)user_data;
5455
alignment_comparer_get_value(zero_cross);

components/motor/esp_sensorless_bldc_control/control/include/bldc_zero_cross_comparer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010
#endif
1111

1212
#include "bldc_driver.h"
13+
#include "esp_attr.h"
1314
#include "bldc_control_param.h"
1415

1516
typedef struct {

components/motor/esp_sensorless_bldc_control/idf_component.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 1.0.0
1+
version: 1.0.1
22
targets:
33
- esp32
44
- esp32s3
@@ -10,8 +10,10 @@ url: https://github.com/espressif/esp-iot-solution/tree/master/components/motor/
1010
repository: https://github.com/espressif/esp-iot-solution.git
1111
issues: https://github.com/espressif/esp-iot-solution/issues
1212
dependencies:
13-
espressif/pid_ctrl: "^0.2.0"
1413
esp_sensorless_bldc_control_lib: "^0.1.0~2"
14+
espressif/pid_ctrl:
15+
version: "^0.2.0"
16+
public: true
1517
idf:
1618
version: '>=5.0'
1719
examples:

components/motor/esp_sensorless_bldc_control/include/bldc_control.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ extern "C" {
2222
* @brief using esp_event_handler_register() to register BLDC_CONTROL_EVENT
2323
*
2424
*/
25-
/** @cond **/
2625
ESP_EVENT_DECLARE_BASE(BLDC_CONTROL_EVENT); /*!< esp event name */
27-
/** @endcond **/
2826

2927
typedef enum {
3028
BLDC_CONTROL_START = 0, /*!< BLDC control start event */

0 commit comments

Comments
 (0)