Skip to content

Commit a3a002f

Browse files
committed
Merge branch 'bugfix/custom_device_handle_management' into 'main'
fix(esp_board_manager): Remove dev_custom handle wrapper and simplify device management Closes AUD-6704 See merge request adf/multimedia/esp-gmf!166
2 parents 8867064 + a3c1398 commit a3a002f

File tree

18 files changed

+245
-76
lines changed

18 files changed

+245
-76
lines changed

packages/esp_board_manager/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## Unreleased
3+
## v0.4.0
44

55
### Features
66
- Add `init_skip` field to control device auto-initialization
@@ -15,6 +15,11 @@
1515
- Update README with dependencies usage guide
1616
- Add sdspi sdcard device type
1717
- Refine dependency for esp_codec_dev
18+
- Add esp_board_device_find_by_handle() API for finding device by handle
19+
- Add board information output to CMakeLists.txt generation
20+
- Update esp_codec_dev dependency to version `~1.5`
21+
- Clear the build directory to get the correct dependencies when switching boards
22+
- Update `CONFIG_IDF_TARGET` in sdkconfig according to the chip name specified in `board_info.yaml`
1823

1924
### Supported Boards
2025
- **ESP32-S3 Korvo2 V3**: Full LCD, LCD Touch, DVP Camera support

packages/esp_board_manager/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ When multiple boards with the same name exist across different paths, the ESP Bo
384384
| SPIFFS Filesystem | fs_spiffs | - | - | ✅ Supported | SPIFFS filesystem | [`dev_fs_spiffs.yaml`](devices/dev_fs_spiffs/dev_fs_spiffs.yaml) |
385385
| GPIO Control | gpio_ctrl | - | gpio | ✅ Supported | GPIO control device | [`dev_gpio_ctrl.yaml`](devices/dev_gpio_ctrl/dev_gpio_ctrl.yaml) |
386386
| LEDC Control | ledc_ctrl | - | ledc | ✅ Supported | LEDC control device | [`dev_ledc_ctrl.yaml`](devices/dev_ledc_ctrl/dev_ledc_ctrl.yaml) |
387-
| Custom Device | custom | - | any | ✅ Supported | User-defined custom device | [`dev_custom.yaml`](devices/dev_custom/dev_custom.yaml) |
387+
| [Custom Device](devices/dev_custom/README.md) | custom | - | any | ✅ Supported | User-defined custom device | [`dev_custom.yaml`](devices/dev_custom/dev_custom.yaml) |
388388
| GPIO Expander | gpio_expander | TCA9554/TCA95XX/HT8574 | i2c | ✅ Supported | GPIO expander | [`dev_gpio_expander.yaml`](devices/dev_gpio_expander/dev_gpio_expander.yaml) |
389389
| Camera Sensor | camera | - | i2c | ✅ Supported | Camera sensor | [`dev_camera.yaml`](devices/dev_camera/dev_camera.yaml) |
390390

packages/esp_board_manager/README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ ESP Board Manager 支持通过三个不同的路径位置进行板级配置,
382382
| SPIFFS 文件系统 | fs_spiffs | - | - | ✅ 支持 | SPIFFS 文件系统 | [`dev_fs_spiffs.yaml`](devices/dev_fs_spiffs/dev_fs_spiffs.yaml) |
383383
| GPIO 控制 | gpio_ctrl | - | gpio | ✅ 支持 | GPIO 控制设备 | [`dev_gpio_ctrl.yaml`](devices/dev_gpio_ctrl/dev_gpio_ctrl.yaml) |
384384
| LEDC 控制 | ledc_ctrl | - | ledc | ✅ 支持 | LEDC 控制设备 | [`dev_ledc_ctrl.yaml`](devices/dev_ledc_ctrl/dev_ledc_ctrl.yaml) |
385-
| 自定义设备 | custom | - | any | ✅ 支持 | 用户定义的自定义设备 | [`dev_custom.yaml`](devices/dev_custom/dev_custom.yaml) |
385+
| [自定义设备](devices/dev_custom/README.md | custom | - | any | ✅ 支持 | 用户定义的自定义设备 | [`dev_custom.yaml`](devices/dev_custom/dev_custom.yaml) |
386386
| GPIO 扩展芯片 | gpio_expander | TCA9554/TCA95XX/HT8574 | i2c | ✅ 支持 | GPIO 扩展芯片 | [`dev_gpio_expander.yaml`](devices/dev_gpio_expander/dev_gpio_expander.yaml) |
387387
| 摄像头 | camera | - | i2c | ✅ 支持 | 摄像头设备 | [`dev_gpio_expander.yaml`](devices/dev_camera/dev_camera.yaml) |
388388

packages/esp_board_manager/boards/esp32_s3_korvo2l/setup_device.c renamed to packages/esp_board_manager/boards/esp32_s3_korvo2l/custom_device.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* See LICENSE file for details.
66
*/
77

8-
#include <string.h>
8+
#include <stdlib.h>
99
#include "esp_board_device.h"
1010
#include "esp_log.h"
1111
#include "dev_custom.h"
@@ -14,16 +14,21 @@ static const char *TAG = "SETUP_DEVICE";
1414

1515
int my_custom_sensor_init(void *config, int cfg_size, void **device_handle)
1616
{
17-
ESP_LOGI(TAG, "Initializing my_custom_sensor device");
18-
// Allocate user device handle if needed
19-
*device_handle = NULL; // For this example, we don't need a user handle
17+
uint32_t *user_handle = (uint32_t*)malloc(sizeof(uint32_t));
18+
if (!user_handle) {
19+
ESP_LOGE(TAG, "Failed to allocate user handle");
20+
return -1;
21+
}
22+
*user_handle = 0x99887766;
23+
ESP_LOGI(TAG, "User handle allocated: %p", user_handle);
24+
*device_handle = user_handle;
2025
return 0;
2126
}
2227

2328
int my_custom_sensor_deinit(void *device_handle)
2429
{
25-
ESP_LOGI(TAG, "Deinitializing my_custom_sensor device");
26-
// Cleanup user device handle if needed
30+
ESP_LOGI(TAG, "Deinitializing my_custom_sensor device handle: %p", device_handle);
31+
free(device_handle);
2732
return 0;
2833
}
2934

packages/esp_board_manager/devices/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ function(get_device_sources sources includes)
4747
list(APPEND srcs "${CMAKE_CURRENT_LIST_DIR}/devices/dev_custom/dev_custom.c")
4848
list(APPEND incs "${CMAKE_CURRENT_LIST_DIR}/devices/dev_custom")
4949
endif()
50-
50+
5151
if(CONFIG_ESP_BOARD_DEV_CAMERA_SUPPORT)
5252
list(APPEND srcs "${CMAKE_CURRENT_LIST_DIR}/devices/dev_camera/dev_camera.c")
5353
list(APPEND incs "${CMAKE_CURRENT_LIST_DIR}/devices/dev_camera")
5454
endif()
55-
55+
5656
if(CONFIG_ESP_BOARD_DEV_FATFS_SDCARD_SPI_SUPPORT)
5757
list(APPEND srcs "${CMAKE_CURRENT_LIST_DIR}/devices/dev_fatfs_sdcard_spi/dev_fatfs_sdcard_spi.c")
5858
list(APPEND incs "${CMAKE_CURRENT_LIST_DIR}/devices/dev_fatfs_sdcard_spi")

packages/esp_board_manager/devices/dev_custom/dev_custom.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,17 @@ int dev_custom_init(void *cfg, int cfg_size, void **device_handle)
3232
ESP_LOGE(TAG, "Invalid parameters");
3333
return -1;
3434
}
35-
3635
// Cast to base config to get device name
3736
dev_custom_base_config_t *base_cfg = (dev_custom_base_config_t *)cfg;
3837
const custom_device_desc_t *desc = find_custom_device_desc(base_cfg->name);
3938
if (desc && desc->init_func) {
40-
// Allocate custom device handle
41-
custom_device_handle_t *custom_handle = malloc(sizeof(custom_device_handle_t));
42-
if (!custom_handle) {
43-
ESP_LOGE(TAG, "Failed to allocate custom device handle for '%s'", base_cfg->name);
44-
return -1;
45-
}
46-
custom_handle->device_name = base_cfg->name;
47-
custom_handle->user_handle = NULL;
48-
int ret = desc->init_func(cfg, cfg_size, &custom_handle->user_handle);
39+
void *user_handle = NULL;
40+
int ret = desc->init_func(cfg, cfg_size, &user_handle);
4941
if (ret != 0) {
5042
ESP_LOGE(TAG, "Custom device '%s' init failed with error: %d", base_cfg->name, ret);
51-
free(custom_handle);
5243
return ret;
5344
}
54-
*device_handle = custom_handle;
55-
ESP_LOGI(TAG, "Custom device '%s' initialized successfully", base_cfg->name);
45+
*device_handle = user_handle;
5646
} else {
5747
// No custom function registered, use default behavior
5848
ESP_LOGW(TAG, "No custom init function registered for device '%s', using default behavior", base_cfg->name);
@@ -67,19 +57,23 @@ int dev_custom_deinit(void *device_handle)
6757
ESP_LOGE(TAG, "Invalid device handle");
6858
return -1;
6959
}
70-
custom_device_handle_t *custom_handle = (custom_device_handle_t *)device_handle;
71-
const custom_device_desc_t *desc = find_custom_device_desc(custom_handle->device_name);
60+
// First, try to find the device handle in g_esp_board_device_handles
61+
const esp_board_device_handle_t *board_device = esp_board_device_find_by_handle(device_handle);
62+
if (board_device == NULL) {
63+
ESP_LOGE(TAG, "Device handle[%p] not found, deinit failed", device_handle);
64+
return -1;
65+
}
66+
const custom_device_desc_t *desc = find_custom_device_desc(board_device->name);
7267
if (desc && desc->deinit_func) {
73-
int ret = desc->deinit_func(custom_handle->user_handle);
68+
int ret = desc->deinit_func(device_handle);
7469
if (ret != 0) {
75-
ESP_LOGE(TAG, "Custom device '%s' deinit failed with error: %d", custom_handle->device_name, ret);
70+
ESP_LOGE(TAG, "Custom device '%s' deinit failed with error: %d", board_device->name, ret);
7671
// Continue with cleanup even if deinit failed
7772
} else {
78-
ESP_LOGI(TAG, "Custom device '%s' deinitialized successfully", custom_handle->device_name);
73+
ESP_LOGI(TAG, "Custom device '%s' deinitialized successfully", board_device->name);
7974
}
8075
} else {
81-
ESP_LOGW(TAG, "No custom deinit function found for device '%s'", custom_handle->device_name);
76+
ESP_LOGW(TAG, "No custom deinit function found for device '%s'", board_device->name);
8277
}
83-
free(custom_handle);
8478
return 0;
8579
}

packages/esp_board_manager/devices/dev_custom/dev_custom.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ typedef int (*custom_device_init_func_t)(void *config, int cfg_size, void **devi
2828
*/
2929
typedef int (*custom_device_deinit_func_t)(void *device_handle);
3030

31-
/**
32-
* @brief Custom device handle structure
33-
*
34-
* This structure should be used as the device handle to store device information
35-
* for proper deinitialization.
36-
*/
37-
typedef struct {
38-
void *user_handle; /*!< User-defined device handle */
39-
const char *device_name; /*!< Device name for lookup */
40-
} custom_device_handle_t;
41-
4231
/**
4332
* @brief Structure describing a custom device implementation
4433
*

packages/esp_board_manager/devices/dev_lcd_touch_i2c/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/esp_board_manager/gen_bmgr_config_codes.py

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,23 @@ def process_devices(self, dev_yaml_path: str, peripherals_dict, periph_name_map,
780780

781781
return device_types
782782

783+
def clear_build_directory(self, project_root: str) -> bool:
784+
"""Clear the build directory to ensure clean build when switching boards"""
785+
try:
786+
build_dir = os.path.join(project_root, 'build')
787+
788+
if os.path.exists(build_dir):
789+
self.logger.debug(f' Clearing build directory: {build_dir}')
790+
import shutil
791+
shutil.rmtree(build_dir)
792+
self.logger.info(' build directory cleared successfully')
793+
else:
794+
self.logger.debug(f'build directory does not exist: {build_dir}')
795+
return True
796+
except Exception as e:
797+
self.logger.error(f'❌ Error clearing build directory: {e}')
798+
return False
799+
783800
def clear_gen_bmgr_codes_directory(self, project_root: str) -> bool:
784801
"""Clear all files and directories in the gen_bmgr_codes directory before generating new ones"""
785802
try:
@@ -808,6 +825,28 @@ def clear_gen_bmgr_codes_directory(self, project_root: str) -> bool:
808825
self.logger.error(f'❌ Error clearing gen_bmgr_codes directory: {e}')
809826
return False
810827

828+
def get_chip_name_from_board_path(self, board_path: str) -> Optional[str]:
829+
"""Extract chip name from board_info.yaml file"""
830+
board_info_path = os.path.join(board_path, 'board_info.yaml')
831+
832+
if not os.path.exists(board_info_path):
833+
self.logger.warning(f'⚠️ board_info.yaml not found at {board_info_path}')
834+
return None
835+
836+
try:
837+
with open(board_info_path, 'r', encoding='utf-8') as f:
838+
board_yml = yaml.safe_load(f)
839+
chip = board_yml.get('chip')
840+
if chip:
841+
self.logger.debug(f' Found chip name: {chip}')
842+
return chip
843+
else:
844+
self.logger.warning(f'⚠️ No chip field found in {board_info_path}')
845+
return None
846+
except Exception as e:
847+
self.logger.error(f'❌ Error reading board_info.yaml: {e}')
848+
return None
849+
811850
def get_version_info(self):
812851
"""Get version information including component version, git commit ID and date, and generation time"""
813852
import subprocess
@@ -900,6 +939,11 @@ def run(self, args):
900939
components_dir = os.path.join(project_root, 'components')
901940
self.logger.debug(f' • Components boards: {components_dir}')
902941

942+
# Clear build directory to ensure clean build when switching boards
943+
if not self.clear_build_directory(project_root):
944+
self.logger.error('❌ Error: Failed to clear build directory!')
945+
return False
946+
903947
# Clear gen_bmgr_codes directory before generating new files
904948
if not self.clear_gen_bmgr_codes_directory(project_root):
905949
self.logger.error('❌ Error: Failed to clear gen_bmgr_codes directory!')
@@ -1076,12 +1120,25 @@ def run(self, args):
10761120
else:
10771121
# Default behavior: update sdkconfig based on board types and board selection
10781122
self.logger.debug(' Updating sdkconfig based on board types and board selection...')
1123+
1124+
# Get chip name from board_info.yaml
1125+
board_path = all_boards.get(selected_board)
1126+
if not board_path:
1127+
self.logger.error(f'❌ Board path not found for {selected_board}')
1128+
return False
1129+
1130+
chip_name = self.get_chip_name_from_board_path(board_path)
1131+
if not chip_name:
1132+
self.logger.error(f'❌ Chip name not found in board_info.yaml for {selected_board}')
1133+
return False
1134+
10791135
result = self.sdkconfig_manager.update_sdkconfig_from_board_types(
10801136
device_types=device_types,
10811137
peripheral_types=peripheral_types,
10821138
sdkconfig_path=str(Path.cwd()/'sdkconfig'),
10831139
enable=True,
1084-
board_name=selected_board
1140+
board_name=selected_board,
1141+
chip_name=chip_name
10851142
)
10861143
if result['enabled']:
10871144
self.logger.info(f"✅ Updated {len(result['enabled'])} sdkconfig features")
@@ -1102,21 +1159,22 @@ def run(self, args):
11021159
self.logger.warning(f'⚠️ Cannot write board info: board "{selected_board}" not found in all_boards')
11031160

11041161
# Setup components/gen_bmgr_codes directory and build system
1105-
if not self.setup_gen_bmgr_codes_component(project_root, board_path, device_dependencies):
1162+
if not self.setup_gen_bmgr_codes_component(project_root, board_path, device_dependencies, selected_board):
11061163
self.logger.error('❌ Error: Failed to setup components/gen_bmgr_codes!')
11071164
return False
11081165

11091166
self.logger.info(f'✅ === Board configuration generation completed successfully for board: {selected_board} ===')
11101167
return True
11111168

1112-
def setup_gen_bmgr_codes_component(self, project_root: str, board_path: str, device_dependencies: dict) -> bool:
1169+
def setup_gen_bmgr_codes_component(self, project_root: str, board_path: str, device_dependencies: dict, selected_board: str = None) -> bool:
11131170
"""
11141171
Setup components/gen_bmgr_codes directory and build system.
11151172
11161173
Args:
11171174
project_root: Path to the project root directory
11181175
board_path: Path to the selected board directory
11191176
device_dependencies: Dictionary of device dependencies
1177+
selected_board: Name of the selected board
11201178
11211179
Returns:
11221180
bool: True if setup was successful
@@ -1154,7 +1212,16 @@ def setup_gen_bmgr_codes_component(self, project_root: str, board_path: str, dev
11541212
src_dirs_str = ' '.join(['"."'] + board_src_dirs) if board_src_dirs else '"."'
11551213
include_dirs_str = ' '.join(['"."'] + board_src_dirs) if board_src_dirs else '"."'
11561214

1157-
cmakelists_content = f"""idf_component_register(
1215+
# Add board information output to CMakeLists.txt
1216+
board_info_output = ''
1217+
if selected_board:
1218+
board_info_output = f"""# Board information output
1219+
message(STATUS "Selected Board: {selected_board}")
1220+
message(STATUS "Board Path: {board_path if board_path else 'Not specified'}")
1221+
1222+
"""
1223+
1224+
cmakelists_content = f"""{board_info_output}idf_component_register(
11581225
SRC_DIRS {src_dirs_str}
11591226
INCLUDE_DIRS {include_dirs_str}
11601227
REQUIRES esp_board_manager
@@ -1238,8 +1305,8 @@ def main():
12381305
formatter_class=argparse.RawDescriptionHelpFormatter,
12391306
epilog="""
12401307
Examples:
1241-
python gen_bmgr_config_codes.py # Use sdkconfig and default boards
1242-
python gen_bmgr_config_codes.py -b echoear_core_board_v1_0 # Specify board directly
1308+
python gen_bmgr_config_codes.py # Use sdkconfig and default boards (auto-sets CONFIG_IDF_TARGET)
1309+
python gen_bmgr_config_codes.py -b echoear_core_board_v1_0 # Specify board directly (auto-sets CONFIG_IDF_TARGET)
12431310
python gen_bmgr_config_codes.py -c /custom/boards # Add customer boards directory
12441311
python gen_bmgr_config_codes.py -c /path/to/single/board # Add single board directory
12451312
python gen_bmgr_config_codes.py -b my_board -c /custom/boards # Both options

packages/esp_board_manager/gen_codes/Kconfig.in

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ config BOARD_ESP32_C5_SPOT
2626
help
2727
Use esp32_c5_spot board configuration.
2828

29+
config BOARD_ESP32_P4_FUNCTION_EV
30+
bool "esp32_p4_function_ev"
31+
help
32+
Use esp32_p4_function_ev board configuration.
33+
2934
config BOARD_ESP32_P4_FUNCTION_EV_BOARD
3035
bool "esp32_p4_function_ev_board"
3136
help
@@ -59,6 +64,7 @@ config BOARD_NAME
5964
default "echoear_core_board_v1_0" if BOARD_ECHOEAR_CORE_BOARD_V1_0
6065
default "echoear_core_board_v1_2" if BOARD_ECHOEAR_CORE_BOARD_V1_2
6166
default "esp32_c5_spot" if BOARD_ESP32_C5_SPOT
67+
default "esp32_p4_function_ev" if BOARD_ESP32_P4_FUNCTION_EV
6268
default "esp32_p4_function_ev_board" if BOARD_ESP32_P4_FUNCTION_EV_BOARD
6369
default "esp32_s3_korvo2_v3" if BOARD_ESP32_S3_KORVO2_V3
6470
default "esp32_s3_korvo2l" if BOARD_ESP32_S3_KORVO2L
@@ -122,6 +128,14 @@ config ESP_BOARD_DEV_AUDIO_CODEC_SUPPORT
122128
This option enables the audio_codec device driver.
123129
The driver is located at: devices/dev_audio_codec
124130

131+
config ESP_BOARD_DEV_CAMERA_SUPPORT
132+
bool "Device 'camera' support"
133+
default n
134+
help
135+
Enable camera device support.
136+
This option enables the camera device driver.
137+
The driver is located at: devices/dev_camera
138+
125139
config ESP_BOARD_DEV_CUSTOM_SUPPORT
126140
bool "Device 'custom' support"
127141
default n
@@ -146,6 +160,14 @@ config ESP_BOARD_DEV_FATFS_SDCARD_SUPPORT
146160
This option enables the fatfs_sdcard device driver.
147161
The driver is located at: devices/dev_fatfs_sdcard
148162

163+
config ESP_BOARD_DEV_FATFS_SDCARD_SPI_SUPPORT
164+
bool "Device 'fatfs_sdcard_spi' support"
165+
default n
166+
help
167+
Enable fatfs_sdcard_spi device support.
168+
This option enables the fatfs_sdcard_spi device driver.
169+
The driver is located at: devices/dev_fatfs_sdcard_spi
170+
149171
config ESP_BOARD_DEV_FS_SPIFFS_SUPPORT
150172
bool "Device 'fs_spiffs' support"
151173
default n

0 commit comments

Comments
 (0)