Skip to content

Commit 677594d

Browse files
committed
feat: error codes as a list
1 parent bedb2cd commit 677594d

File tree

8 files changed

+42
-19
lines changed

8 files changed

+42
-19
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ target_include_directories(${ESP_STUB_LIB}
3939
INTERFACE include
4040
PRIVATE include/esp-stub-lib
4141
)
42-
# Public within the library
42+
# Accessible within stub-lib (including subdirectories target/<chip> and target/common), but not from top-level client code
4343
include_directories(include)
4444
include_directories(include/esp-stub-lib)
45+
include_directories(include/private)
4546

4647
# STUB_COMPILE_DEFS is optional definitions coming from the parent CMakeLists.txt
4748
add_compile_definitions(${STUB_COMPILE_DEFS})

example/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ file(GLOB SRC_FILES "${SRC_DIR}/*.c" "${SRC_DIR}/*.S")
2727

2828
# Target configuration
2929
add_executable(${PROJECT_NAME} ${SRC_FILES})
30+
add_dependencies(${PROJECT_NAME} stub_lib_err_header)
3031
target_include_directories(${PROJECT_NAME}
3132
PRIVATE include
33+
PRIVATE .
3234
)
3335
foreach(script ${LINKER_SCRIPTS})
3436
target_link_options(${PROJECT_NAME} PRIVATE -T${script})
@@ -54,3 +56,10 @@ add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
5456
get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
5557
add_subdirectory(${PROJECT_ROOT} esp-stub-lib)
5658
target_link_libraries(${PROJECT_NAME} PRIVATE esp-stub-lib)
59+
60+
add_custom_target(stub_lib_err_header
61+
COMMAND ${CMAKE_COMMAND} -E copy
62+
${PROJECT_ROOT}/include/private/stub_lib_err.h
63+
${CMAKE_CURRENT_LIST_DIR}/stub_lib_err.h
64+
COMMENT "Preparing stub_lib_err.h before building the stub"
65+
)

example/stub_main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
#include <esp-stub-lib/log.h>
1313
#include <esp-stub-lib/flash.h>
14-
#include <esp-stub-lib/err.h>
14+
15+
#include "stub_lib_err.h"
1516

1617
#include "stub_main.h"
1718

include/esp-stub-lib/err.h

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

include/esp-stub-lib/flash.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#pragma once
88

99
#include <stdint.h>
10-
#include "err.h"
10+
11+
#include <stub_lib_err.h>
1112

1213
typedef struct stub_lib_flash_info {
1314
uint32_t id;

include/private/stub_lib_err.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#pragma once
8+
9+
// Limit error code range for stub-lib
10+
#define STUB_LIB_ERR_END 0x100000
11+
12+
#define STUB_LIB_ERROR_CODE_LIST \
13+
X(STUB_LIB_OK, 0, "Operation esp-stub-lib success") \
14+
X(STUB_LIB_FAIL, (STUB_LIB_ERR_END - 1), "Generic esp-stub-lib failure") \
15+
\
16+
X(STUB_LIB_ERR_FLASH_INIT_UNKNOWN_FLASH_ID, 0x1000, "Unknown flash size, unknown flash id") \
17+
18+
// Only numerical values, used in esp-stub-lib sources
19+
20+
#define X(code_, value_, info_) code_ = value_,
21+
typedef enum stub_lib_err_codes {
22+
STUB_LIB_ERROR_CODE_LIST
23+
} stub_lib_err_t;
24+
#undef X

src/flash.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
#include <flash.h>
8-
#include <err.h>
98
#include <log.h>
9+
#include <stub_lib_err.h>
1010
#include <target/flash.h>
1111
#include <private/rom_flash_config.h>
1212

@@ -16,8 +16,7 @@ stub_lib_err_t stub_lib_flash_init(void **state)
1616
uint32_t flash_id = stub_target_flash_get_flash_id();
1717
uint32_t flash_size = stub_target_flash_id_to_flash_size(flash_id);
1818
if (flash_size == 0) {
19-
STUB_LOGE("Invalid flash size: 0\n");
20-
return STUB_LIB_FAIL;
19+
return STUB_LIB_ERR_FLASH_INIT_UNKNOWN_FLASH_ID;
2120
}
2221
STUB_LOG_TRACEF("Flash size: %d MB\n", flash_size / (1024 * 1024));
2322

src/target/common/src/flash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ uint32_t stub_target_flash_id_to_flash_size(uint32_t flash_id)
4545
return 32 * 1024 * 1024;
4646
}
4747

48-
STUB_LOGE("Unknown flash_id: 0x%x", flash_id);
48+
STUB_LOGE("Unknown flash_id: 0x%x\n", flash_id);
4949
return 0;
5050
}

0 commit comments

Comments
 (0)