diff --git a/.codespellrc b/.codespellrc index 79186c6..60bbe06 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,2 +1 @@ [codespell] -skip = ./src/touch/port/esp_lcd_touch_xpt2046.c diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index 9bc2f2a..b3f879e 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -9,7 +9,7 @@ jobs: build: strategy: matrix: - idf_ver: ["release-v5.1", "release-v5.2", "release-v5.3"] + idf_ver: ["release-v5.1", "release-v5.2", "release-v5.3"] idf_target: ["esp32", "esp32s3"] runs-on: ubuntu-20.04 container: espressif/idf:${{ matrix.idf_ver }} @@ -28,3 +28,7 @@ jobs: idf.py build rm -rf sdkconfig build managed_components dependencies.lock idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.cxx_exceptions" build + rm -rf sdkconfig build managed_components dependencies.lock + idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.cxx_exceptions;sdkconfig.ci.esp_mem" build + rm -rf sdkconfig build managed_components dependencies.lock + idf.py -DSDKCONFIG_DEFAULTS="sdkconfig.defaults;sdkconfig.ci.cxx_exceptions;sdkconfig.ci.custom_mem" build diff --git a/Kconfig b/Kconfig index 4ebe272..df18c96 100644 --- a/Kconfig +++ b/Kconfig @@ -1,111 +1,120 @@ -menu "ESP Library Utils" - config ESP_UTILS_CONFIG_FILE_SKIP - bool "Unckeck this to ignore `esp_utils_config.h`" +menu "ESP Library Utils Configurations" + config ESP_UTILS_CONF_FILE_SKIP + bool "Unckeck this to use custom `esp_utils_conf.h`" default y menu "Check functions" - config ESP_UTILS_ENABLE_CHECK - bool "Enable error check" + depends on ESP_UTILS_CONF_FILE_SKIP + config ESP_UTILS_CONF_ENABLE_CHECK + bool "Enable check functions" default y help If enabled, the driver will check the function parameters, return value, etc. Disable them will reduce the code size. - config ESP_UTILS_CHECK_WITH_ERROR_LOG - bool "Print error message on error" + config ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG + bool "Print log messages on error" default y - depends on ESP_UTILS_ENABLE_CHECK + depends on ESP_UTILS_CONF_ENABLE_CHECK help If enabled, the driver will print error message when check failed. - config ESP_UTILS_CHECK_WITH_ASSERT + config ESP_UTILS_CONF_CHECK_WITH_ASSERT bool "Assert on error" default n - depends on ESP_UTILS_ENABLE_CHECK + depends on ESP_UTILS_CONF_ENABLE_CHECK help If enabled, the driver will assert when check failed. endmenu menu "Log functions" - config ESP_UTILS_ENABLE_LOG - bool "Enable output debug log" + depends on ESP_UTILS_CONF_FILE_SKIP + config ESP_UTILS_CONF_ENABLE_LOG + bool "Enable log functions" default y help If enabled, the driver will output log for debugging. - config ESP_UTILS_LOG_BUFFER_SIZE - int "Buffer size for log messages" - depends on ESP_UTILS_ENABLE_LOG + config ESP_UTILS_CONF_LOG_BUFFER_SIZE + int "Buffer size for formatting messages" + depends on ESP_UTILS_CONF_ENABLE_LOG default 256 - choice ESP_UTILS_LOG_LEVEL - prompt "Log level for all messages" - depends on ESP_UTILS_ENABLE_LOG - default ESP_UTILS_LOG_LEVEL_INFO + choice ESP_UTILS_CONF_LOG_LEVEL + prompt "Select global log level" + depends on ESP_UTILS_CONF_ENABLE_LOG + default ESP_UTILS_CONF_LOG_LEVEL_INFO - config ESP_UTILS_LOG_LEVEL_DEBUG + config ESP_UTILS_CONF_LOG_LEVEL_DEBUG bool "Debug" help Extra information which is not necessary for normal use (values, pointers, sizes, etc) - config ESP_UTILS_LOG_LEVEL_INFO + config ESP_UTILS_CONF_LOG_LEVEL_INFO bool "Info" help Information messages which describe the normal flow of events - config ESP_UTILS_LOG_LEVEL_WARNING + config ESP_UTILS_CONF_LOG_LEVEL_WARNING bool "Warning" help Error conditions from which recovery measures have been taken - config ESP_UTILS_LOG_LEVEL_ERROR + config ESP_UTILS_CONF_LOG_LEVEL_ERROR bool "Error" help Critical errors, software module cannot recover on its own endchoice - config ESP_UTILS_LOG_GLOBAL_LEVEL + config ESP_UTILS_CONF_LOG_LEVEL int - default 0 if ESP_UTILS_LOG_LEVEL_DEBUG - default 1 if ESP_UTILS_LOG_LEVEL_INFO - default 2 if ESP_UTILS_LOG_LEVEL_WARNING - default 3 if ESP_UTILS_LOG_LEVEL_ERROR + default 0 if ESP_UTILS_CONF_LOG_LEVEL_DEBUG + default 1 if ESP_UTILS_CONF_LOG_LEVEL_INFO + default 2 if ESP_UTILS_CONF_LOG_LEVEL_WARNING + default 3 if ESP_UTILS_CONF_LOG_LEVEL_ERROR - config ESP_UTILS_ENABLE_LOG_TRACE + config ESP_UTILS_CONF_ENABLE_LOG_TRACE bool "Enable trace function" - depends on ESP_UTILS_LOG_LEVEL_DEBUG + depends on ESP_UTILS_CONF_LOG_LEVEL_DEBUG default n help If enabled, the driver will print trace log messages when enter/exit functions, useful for debugging endmenu menu "Memory functions" - choice ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_CHOICE + depends on ESP_UTILS_CONF_FILE_SKIP + choice ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_CHOICE prompt "Select general allocation type" - default ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_STDLIB + default ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_STDLIB - config ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_STDLIB + config ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_STDLIB bool "Standard library (malloc, free)" - config ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_ESP + config ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_ESP bool "ESP (heap_caps_malloc, free)" - config ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_MICROPYTHON + config ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_MICROPYTHON bool "MicroPython (mp_malloc, mp_free)" - config ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_CUSTOM - bool "Custom (`ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_MALLOC` and `ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_FREE`)" + config ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_CUSTOM + bool "Custom (`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC` and `ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_FREE`)" endchoice - config ESP_UTILS_MEM_GENERAL_ALLOC_TYPE + config ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE int - default 0 if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_STDLIB - default 1 if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_ESP - default 2 if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_MICROPYTHON - default 3 if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_CUSTOM - - config ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE + default 0 if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_STDLIB + default 1 if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_ESP + default 2 if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_MICROPYTHON + default 3 if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_CUSTOM + + config ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN + int "General esp memory alignment (bytes)" + depends on ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_ESP + default 1 + range 1 1024 + + config ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE string "General custom memory header file" - depends on ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_CUSTOM + depends on ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_CUSTOM default "stdlib.h" endmenu endmenu diff --git a/README.md b/README.md index 84f872c..1436d18 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,133 @@ # ESP Library Utils -esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including `logging`, `memory management`, and `checking`. +## Overview + +`esp-lib-utils` is a library designed to offer utility functions for other libraries, including the following features: + +- Supports a variety of utility functions, such as `logging`, `checking`, and `memory` functions. +- Supports flexible static configuration, such as setting log levels, enabling/disabling checks, using custom memory allocation functions, and more. +- Compatible with the `Arduino`, `ESP-IDF` and `MicroPython` for compilation. + +## Table of Contents + +- [ESP Library Utils](#esp-library-utils) + - [Overview](#overview) + - [Table of Contents](#table-of-contents) + - [How to Use](#how-to-use) + - [ESP-IDF Framework](#esp-idf-framework) + - [Dependencies and Versions](#dependencies-and-versions) + - [Adding to Project](#adding-to-project) + - [Configuration Instructions](#configuration-instructions) + - [Arduino IDE](#arduino-ide) + - [Dependencies and Versions](#dependencies-and-versions-1) + - [Installing the Library](#installing-the-library) + - [Configuration Instructions](#configuration-instructions-1) + - [Usage Example](#usage-example) + - [Logging Functions](#logging-functions) + - [Checking Functions](#checking-functions) + - [Memory Functions](#memory-functions) + - [FAQ](#faq) + - [Where is the directory for Arduino libraries?](#where-is-the-directory-for-arduino-libraries) + - [How to Install esp-lib-utils in Arduino IDE?](#how-to-install-esp-lib-utils-in-arduino-ide) + - [Where are the installation directory for arduino-esp32 and the SDK located?](#where-are-the-installation-directory-for-arduino-esp32-and-the-sdk-located) ## How to Use +### ESP-IDF Framework + +#### Dependencies and Versions + +| **Dependency** | **Version** | +| ----------------------------------------------- | ----------- | +| [esp-idf](https://github.com/espressif/esp-idf) | >= 5.1 | + +#### Adding to Project + +`esp-lib-utils` has been uploaded to the [Espressif Component Registry](https://components.espressif.com/), and users can add it to their project using the `idf.py add-dependency` command, for example: + +```bash +idf.py add-dependency "espressif/esp-lib-utils" +``` + +Alternatively, users can create or modify the *idf_component.yml* file in the project directory. For more details, please refer to the [Espressif Documentation - IDF Component Manager](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/tools/idf-component-manager.html). + +#### Configuration Instructions + +When developing with esp-idf, users can configure `esp-lib-utils` through the menuconfig: + +1. Run the command `idf.py menuconfig`. +2. Navigate to `Component config` > `ESP Library Utils Configurations`. + +> [!NOTE] +> To support `try-catch` when using `esp_utils::make_shared()` in C++, users need to enable the `COMPILER_CXX_EXCEPTIONS` configuration in the menuconfig. + +### Arduino IDE + +#### Dependencies and Versions + +| **Dependency** | **Version** | +| ----------------------------------------------------------- | ----------- | +| [arduino-esp32](https://github.com/espressif/arduino-esp32) | >= v3.0.0 | + +#### Installing the Library + +For installation of the `esp-lib-utils` library, refer to [How to Install esp-lib-utils in Arduino IDE](#how-to-install-esp-lib-utils-in-arduino-ide). + +#### Configuration Instructions + +Below are detailed instructions on how to configure `esp-lib-utils`. They are optional operations and are configured through the [esp_utils_conf.h](../esp_utils_conf.h) file with the following characteristics: + +1. The path sequence to search for the *esp_utils_conf.h* file is: `Current Project Directory` > `Arduino Library Directory` > `esp-lib-utils Directory`. +3. **If a project needs to use a separate configuration**, users can copy it from the root directory of `esp-lib-utils` to the project. +4. **If multiple projects need to use the same configuration**, users can place it in the [Arduino Library Directory](#where-is-the-directory-for-arduino-libraries), so that all projects can share the same configuration. The layout of the Arduino library folder should look like this: + + ``` + Arduino + |-libraries + |-esp-lib-utils + |-esp_utils_conf.h + |-other_libs + ``` + +> [!WARNING] +> * When the library version is updated, the configurations within the *esp_utils_conf.h* file might change, such as adding, deleting, or renaming. +> * To ensure the compatibility of the program, the library manages the version of the file independently and checks whether the configuration file currently used by the user are compatible with the library during compilation. +> * Detailed version information and checking rules can be found at the end of the file. + +Since `esp-lib-utils` configures its functionality through the *esp_utils_conf.h* file, users can customize behavior and default parameters by adjusting the macro definitions within this file. For example, to enable debug log output, here is a snippet of the modified `esp_utils_conf.h` file: + +```c +... +/* Set to 1 if use `ESP_UTILS_LOG*()` macros */ +#define ESP_UTILS_CONF_ENABLE_LOG (1) // 0/1 +#if ESP_UTILS_CONF_ENABLE_LOG + /** + * Global log level, logs with a level lower than this will not be compiled. Choose one of the following: + * - ESP_UTILS_LOG_LEVEL_DEBUG: Extra information which is not necessary for normal use (values, pointers, sizes, etc) + * (lowest level) + * - ESP_UTILS_LOG_LEVEL_INFO: Information messages which describe the normal flow of events + * - ESP_UTILS_LOG_LEVEL_WARNING: Error conditions from which recovery measures have been taken + * - ESP_UTILS_LOG_LEVEL_ERROR: Critical errors, software module cannot recover on its own (highest level) + * + */ + #define ESP_UTILS_CONF_LOG_LEVEL (ESP_UTILS_LOG_LEVEL_DEBUG) + ... +#endif // ESP_UTILS_CONF_ENABLE_LOG +... +``` + +> [!NOTE] +> `arduino-esp32` supports `try-catch` in C++ by default. + +## Usage Example + +### Logging Functions + ```cpp -// Define the log tag for the current library, should be declared before `esp_utils_library.h` +// Define the log tag for the current library, should be declared before `esp_lib_utils.h` #define ESP_UTILS_LOG_TAG "MyLibrary" -#include "esp_utils_library.h" +#include "esp_lib_utils.h" void test_log(void) { @@ -26,10 +145,102 @@ void test_log(void) ESP_UTILS_LOG_TRACE_EXIT(); } +``` + +### Checking Functions + +```cpp +// Define the log tag for the current library, should be declared before `esp_lib_utils.h` +#define ESP_UTILS_LOG_TAG "MyLibrary" +#include "esp_lib_utils.h" + +bool test_check_false_return(void) +{ + ESP_UTILS_CHECK_FALSE_RETURN(true, false, "Check false return failed"); + ESP_UTILS_CHECK_FALSE_RETURN(false, true, "Check false return success"); + return false; +} + +bool test_check_false_goto(void) +{ + ESP_UTILS_CHECK_FALSE_GOTO(true, err, "Check false goto failed"); + ESP_UTILS_CHECK_FALSE_GOTO(false, end, "Check false goto success"); + + ESP_UTILS_CHECK_TAG(err) + return false; + + ESP_UTILS_CHECK_TAG(end) + return true; +} + +void test_check_false_exit(void) +{ + ESP_UTILS_CHECK_FALSE_EXIT(true, "Check false exit failed"); + ESP_UTILS_CHECK_FALSE_EXIT(false, "Check false exit success"); +} + +bool test_check_error_return(void) +{ + ESP_UTILS_CHECK_ERROR_RETURN(ESP_OK, false, "Check error return failed"); + ESP_UTILS_CHECK_ERROR_RETURN(ESP_FAIL, true, "Check error return success"); + return false; +} + +bool test_check_error_goto(void) +{ + ESP_UTILS_CHECK_ERROR_GOTO(ESP_OK, err, "Check error goto failed"); + ESP_UTILS_CHECK_ERROR_GOTO(ESP_FAIL, end, "Check error goto success"); + + ESP_UTILS_CHECK_TAG(err) + return false; + + ESP_UTILS_CHECK_TAG(end) + return true; +} + +void test_check_error_exit(void) +{ + ESP_UTILS_CHECK_ERROR_EXIT(ESP_OK, "Check error exit failed"); + ESP_UTILS_CHECK_ERROR_EXIT(ESP_FAIL, "Check error exit success"); +} + +bool test_check_null_return(void) +{ + ESP_UTILS_CHECK_NULL_RETURN((void *)1, false, "Check null return failed"); + ESP_UTILS_CHECK_NULL_RETURN(NULL, true, "Check null return success"); + return false; +} + +static bool test_check_null_goto(void) +{ + ESP_UTILS_CHECK_NULL_GOTO((void *)1, err, "Check null goto failed"); + ESP_UTILS_CHECK_NULL_GOTO(NULL, end, "Check null goto success"); + + ESP_UTILS_CHECK_TAG(err) + return false; + + ESP_UTILS_CHECK_TAG(end) + return true; +} + +void test_check_null_exit(void) +{ + ESP_UTILS_CHECK_NULL_EXIT((void *)1, "Check null exit failed"); + ESP_UTILS_CHECK_NULL_EXIT(NULL, "Check null exit success"); +} + +``` + +### Memory Functions + +```cpp +// Define the log tag for the current library, should be declared before `esp_lib_utils.h` +#define ESP_UTILS_LOG_TAG "MyLibrary" +#include "esp_lib_utils.h" void test_memory(void) { - // Allocate memory in C style (`malloc/calloc` and `free` are re-defined by the library) + // Allocate memory in C style (`malloc/calloc` and `free` are re-defined in the library) int *c_ptr = (int *)malloc(sizeof(int)); ESP_UTILS_CHECK_NULL_EXIT(c_ptr, "Failed to allocate memory"); free(c_ptr); @@ -42,3 +253,24 @@ void test_memory(void) cxx_ptr = nullptr; } ``` + +## FAQ + +### Where is the directory for Arduino libraries? + +Users can find and modify the directory path for Arduino libraries by selecting `File` > `Preferences` > `Settings` > `Sketchbook location` from the menu bar in the Arduino IDE. + +### How to Install esp-lib-utils in Arduino IDE? + +- **If users want to install online**, navigate to `Sketch` > `Include Library` > `Manage Libraries...` in the Arduino IDE, then search for `esp-lib-utils` and click the `Install` button to install it. +- **If users want to install manually**, download the required version of the `.zip` file from [esp-lib-utils](https://github.com/esp-arduino-libs/esp-lib-utils), then navigate to `Sketch` > `Include Library` > `Add .ZIP Library...` in the Arduino IDE, select the downloaded `.zip` file, and click `Open` to install it. +- Users can also refer to the guides on library installation in the [Arduino IDE v1.x.x](https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries) or [Arduino IDE v2.x.x](https://docs.arduino.cc/software/ide-v2/tutorials/ide-v2-installing-a-library) documentation. + +### Where are the installation directory for arduino-esp32 and the SDK located? + +The default installation path for arduino-esp32 depends on the PC operating system: + +- Windows: `C:\Users\\AppData\Local\Arduino15\packages\esp32` +- Linux: `~/.arduino15/packages/esp32` + +The SDK for arduino-esp32 v3.x.x version is located in the `/tools/esp32-arduino-libs/idf-release_x` directory within the default installation path. diff --git a/esp_utils_config.h b/esp_utils_conf.h similarity index 52% rename from esp_utils_config.h rename to esp_utils_conf.h index 283b478..4804dc8 100644 --- a/esp_utils_config.h +++ b/esp_utils_conf.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,84 +9,85 @@ // *INDENT-OFF* //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -///////////////////////////////////////////////// Utils Configurations ///////////////////////////////////////////////// +///////////////////////////////////////////////// Check Configurations ///////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** - * Check functions related. - * * These functions are used to check the function parameters, return value, etc. Disable them will reduce the code size. * */ /* Set to 1 if use `ESP_UTILS_CHECK_*()` macros */ -#define ESP_UTILS_ENABLE_CHECK (1) // 0/1 -#if ESP_UTILS_ENABLE_CHECK +#define ESP_UTILS_CONF_ENABLE_CHECK (1) // 0/1 +#if ESP_UTILS_CONF_ENABLE_CHECK /* Set to 1 if print message when check failed */ - #define ESP_UTILS_CHECK_WITH_ERROR_LOG (1) // 0/1 + #define ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG (1) // 0/1 /* Set to 1 if assert when check failed */ - #define ESP_UTILS_CHECK_WITH_ASSERT (0) // 0/1 -#endif // ESP_UTILS_ENABLE_CHECK + #define ESP_UTILS_CONF_CHECK_WITH_ASSERT (0) // 0/1 +#endif // ESP_UTILS_CONF_ENABLE_CHECK +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////// Log Configurations ////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** - * Log functions related. - * * These functions are used to print log messages. Disable them will reduce the code size. * */ /* Set to 1 if use `ESP_UTILS_LOG*()` macros */ -#define ESP_UTILS_ENABLE_LOG (1) // 0/1 -#if ESP_UTILS_ENABLE_LOG +#define ESP_UTILS_CONF_ENABLE_LOG (1) // 0/1 +#if ESP_UTILS_CONF_ENABLE_LOG /** * Global log level, logs with a level lower than this will not be compiled. Choose one of the following: - * - ESP_UTILS_LOG_LEVEL_DEBUG: Extra information which is not necessary for normal use (values, pointers, sizes, - * etc) (lowest level) + * - ESP_UTILS_LOG_LEVEL_DEBUG: Extra information which is not necessary for normal use (values, pointers, sizes, etc) + * (lowest level) * - ESP_UTILS_LOG_LEVEL_INFO: Information messages which describe the normal flow of events * - ESP_UTILS_LOG_LEVEL_WARNING: Error conditions from which recovery measures have been taken * - ESP_UTILS_LOG_LEVEL_ERROR: Critical errors, software module cannot recover on its own (highest level) * */ - #define ESP_UTILS_LOG_GLOBAL_LEVEL ESP_UTILS_LOG_LEVEL_INFO - #if ESP_UTILS_LOG_GLOBAL_LEVEL == ESP_UTILS_LOG_LEVEL_DEBUG + #define ESP_UTILS_CONF_LOG_LEVEL (ESP_UTILS_LOG_LEVEL_INFO) + #if ESP_UTILS_CONF_LOG_LEVEL == ESP_UTILS_LOG_LEVEL_DEBUG + /* Set to 1 if print trace log messages when enter/exit functions, useful for debugging */ - #define ESP_UTILS_ENABLE_LOG_TRACE (0) - #endif // ESP_UTILS_LOG_GLOBAL_LEVEL + #define ESP_UTILS_CONF_ENABLE_LOG_TRACE (0) + + #endif // ESP_UTILS_CONF_LOG_LEVEL /* Log format buffer size */ - #define ESP_UTILS_LOG_BUFFER_SIZE (256) -#endif // ESP_UTILS_ENABLE_LOG + #define ESP_UTILS_CONF_LOG_BUFFER_SIZE (256) +#endif // ESP_UTILS_CONF_ENABLE_LOG +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////// Memory Configurations ///////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** - * Memory related - * - */ -/** - * Memory allocation type, choose one of the following: + * General Memory allocation type, choose one of the following: * - ESP_UTILS_MEM_ALLOC_TYPE_STDLIB: Use the standard library memory allocation functions (malloc, free) - * - ESP_UTILS_MEM_ALLOC_TYPE_ESP: Use the ESP-IDF memory allocation functions (heap_caps_malloc, heap_caps_free) + * - ESP_UTILS_MEM_ALLOC_TYPE_ESP: Use the ESP-IDF memory allocation functions (heap_caps_aligned_alloc, heap_caps_free) * - ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON: Use the MicroPython memory allocation functions (m_malloc, m_free) * - ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM: Use custom memory allocation functions (ESP_UTILS_MEM_ALLOC_CUSTOM_MALLOC, * ESP_UTILS_MEM_ALLOC_CUSTOM_FREE) * */ -#define ESP_UTILS_MEM_GENERAL_ALLOC_TYPE ESP_UTILS_MEM_ALLOC_TYPE_STDLIB -#if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP +#define ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE (ESP_UTILS_MEM_ALLOC_TYPE_STDLIB) +#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP - #define ESP_UTILS_MEM_GENERAL_ALLOC_ESP_CAPS (MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT) + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN (1) + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_CAPS (MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT) -#elif ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM - #define ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE stdlib.h - #define ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_MALLOC malloc - #define ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_FREE free + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE "stdlib.h" + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC malloc + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_FREE free -#endif +#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////// File Version /////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * Do not change the following versions, they are used to check if the configurations in this file are compatible with - * the current version of `esp_utils_config.h` in the library. The detailed rules are as follows: + * the current version of `esp_utils_conf.h` in the library. The detailed rules are as follows: * * 1. If the major version is not consistent, then the configurations in this file are incompatible with the library * and must be replaced with the file from the library. @@ -95,8 +96,8 @@ * 3. Even if the patch version is not consistent, it will not affect normal functionality. * */ -#define ESP_UTILS_CONFIG_FILE_VERSION_MAJOR 0 -#define ESP_UTILS_CONFIG_FILE_VERSION_MINOR 1 -#define ESP_UTILS_CONFIG_FILE_VERSION_PATCH 0 +#define ESP_UTILS_CONF_FILE_VERSION_MAJOR 0 +#define ESP_UTILS_CONF_FILE_VERSION_MINOR 1 +#define ESP_UTILS_CONF_FILE_VERSION_PATCH 0 // *INDENT-OFF* diff --git a/idf_component.yml b/idf_component.yml index 4ff3178..964f92e 100644 --- a/idf_component.yml +++ b/idf_component.yml @@ -1,5 +1,5 @@ version: "0.1.0" -description: esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory management. +description: esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory. url: https://github.com/esp-arduino-libs/esp-lib-utils repository: https://github.com/esp-arduino-libs/esp-lib-utils.git issues: https://github.com/esp-arduino-libs/esp-lib-utils/issues diff --git a/library.properties b/library.properties index 854123f..cd7e435 100644 --- a/library.properties +++ b/library.properties @@ -2,9 +2,9 @@ name=esp-lib-utils version=0.1.0 author=espressif maintainer=espressif -sentence=esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory management. +sentence=esp-lib-utils is a library designed for ESP SoCs to provide utility functions, including logging, checking, and memory. paragraph= category=Other architectures=esp32 url=https://github.com/esp-arduino-libs/esp-lib-utils -includes=esp_utils_library.h +includes=esp_lib_utils.h diff --git a/micropython.cmake b/micropython.cmake new file mode 100644 index 0000000..a350f51 --- /dev/null +++ b/micropython.cmake @@ -0,0 +1,16 @@ +# This file is to be given as "make USER_C_MODULES=..." when building Micropython port + +add_library(usermod_esp_lib_utils INTERFACE) + +# Set the source directorya and find all source files. +set(SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src) +file(GLOB_RECURSE SRCS ${SRC_DIR}/*.c) + +# Add our source files to the library. +target_sources(usermod_esp_lib_utils INTERFACE ${SRCS}) + +# Add the current directory as an include directory. +target_include_directories(usermod_esp_lib_utils INTERFACE ${SRC_DIR}) + +# Link our INTERFACE library to the usermod target. +target_link_libraries(usermod INTERFACE usermod_esp_lib_utils) diff --git a/src/check/esp_utils_check.h b/src/check/esp_utils_check.h index c3aebed..38adce6 100644 --- a/src/check/esp_utils_check.h +++ b/src/check/esp_utils_check.h @@ -4,10 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "esp_err.h" -#include "esp_utils_config_internal.h" +#include "esp_utils_conf_internal.h" #include "log/esp_utils_log.h" -#if !ESP_UTILS_ENABLE_CHECK +#if !ESP_UTILS_CONF_ENABLE_CHECK #ifndef ESP_UTILS_CHECK_TAG #define ESP_UTILS_CHECK_TAG(goto_tag) @@ -33,7 +33,7 @@ #else -#if ESP_UTILS_CHECK_WITH_ASSERT +#if ESP_UTILS_CONF_CHECK_WITH_ASSERT #ifndef ESP_UTILS_CHECK_TAG #define ESP_UTILS_CHECK_TAG(goto_tag) @@ -87,7 +87,7 @@ #define ESP_UTILS_CHECK_TAG(goto_tag) goto_tag: #endif -#if ESP_UTILS_CHECK_WITH_ERROR_LOG +#if ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG /** * @brief Check if the pointer is NULL; if NULL, log an error and return the specified value. @@ -202,8 +202,9 @@ * @param ... Additional arguments for the format string */ #define ESP_UTILS_CHECK_ERROR_GOTO(x, goto_tag, fmt, ...) do { \ - if (unlikely((x) != ESP_OK)) { \ - ESP_UTILS_LOGE(fmt, ##__VA_ARGS__); \ + esp_err_t err = (x); \ + if (unlikely((err) != ESP_OK)) { \ + ESP_UTILS_LOGE(fmt " [%s]", ##__VA_ARGS__, esp_err_to_name(err)); \ goto goto_tag; \ } \ } while(0) @@ -216,8 +217,9 @@ * @param ... Additional arguments for the format string */ #define ESP_UTILS_CHECK_ERROR_EXIT(x, fmt, ...) do { \ - if (unlikely((x) != ESP_OK)) { \ - ESP_UTILS_LOGE(fmt, ##__VA_ARGS__); \ + esp_err_t err = (x); \ + if (unlikely((err) != ESP_OK)) { \ + ESP_UTILS_LOGE(fmt " [%s]", ##__VA_ARGS__, esp_err_to_name(err)); \ return; \ } \ } while(0) @@ -262,7 +264,13 @@ #else #define ESP_UTILS_CHECK_EXCEPTION_RETURN(x, ret, fmt, ...) (x) -#define ESP_UTILS_CHECK_EXCEPTION_GOTO(x, goto_tag, fmt, ...) (x) +#define ESP_UTILS_CHECK_EXCEPTION_GOTO(x, goto_tag, fmt, ...) do {\ + x; \ + /* Aoivd unused tag warning */ \ + if (0) { \ + goto goto_tag; \ + } \ + } while (0) #define ESP_UTILS_CHECK_EXCEPTION_EXIT(x, fmt, ...) (x) #endif // CONFIG_COMPILER_CXX_EXCEPTIONS @@ -420,6 +428,6 @@ } while (0) #endif // __cplusplus -#endif // ESP_UTILS_CHECK_WITH_ERROR_LOG -#endif // ESP_UTILS_CHECK_WITH_ASSERT -#endif // ESP_UTILS_ENABLE_CHECK +#endif // ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG +#endif // ESP_UTILS_CONF_CHECK_WITH_ASSERT +#endif // ESP_UTILS_CONF_ENABLE_CHECK diff --git a/src/esp_utils_library.h b/src/esp_lib_utils.h similarity index 72% rename from src/esp_utils_library.h rename to src/esp_lib_utils.h index 690f523..2acff9a 100644 --- a/src/esp_utils_library.h +++ b/src/esp_lib_utils.h @@ -5,8 +5,12 @@ */ #pragma once +/* Base */ #include "esp_utils_types.h" #include "esp_utils_versions.h" +#include "esp_utils_conf_internal.h" + +/* Modules */ #include "check/esp_utils_check.h" #include "log/esp_utils_log.h" -#include "memory/esp_utils_memory.h" +#include "memory/esp_utils_mem.h" diff --git a/src/esp_utils_conf_internal.h b/src/esp_utils_conf_internal.h new file mode 100644 index 0000000..78bef16 --- /dev/null +++ b/src/esp_utils_conf_internal.h @@ -0,0 +1,68 @@ +/* + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +// *INDENT-OFF* + +/* Handle special Kconfig options */ +#ifndef ESP_UTILS_KCONFIG_IGNORE + #include "sdkconfig.h" + #ifdef CONFIG_ESP_UTILS_CONF_FILE_SKIP + #define ESP_UTILS_CONF_FILE_SKIP + #endif +#endif + +#ifndef ESP_UTILS_CONF_FILE_SKIP + /* If "esp_utils_conf.h" is available from here, try to use it later */ + #ifdef __has_include + #if __has_include("esp_utils_conf.h") + #ifndef ESP_UTILS_CONF_INCLUDE_SIMPLE + #define ESP_UTILS_CONF_INCLUDE_SIMPLE + #endif + #elif __has_include("../../esp_utils_conf.h") + #ifndef ESP_UTILS_CONF_INCLUDE_OUTSIDE + #define ESP_UTILS_CONF_INCLUDE_OUTSIDE + #endif + #else + #define ESP_UTILS_CONF_INCLUDE_INSIDE + #endif + #else + #define ESP_UTILS_CONF_INCLUDE_OUTSIDE + #endif +#endif + +/* Include type definitions before including configuration header file */ +#include "esp_utils_types.h" + +/* If "esp_utils_conf.h" is not skipped, include it */ +#ifndef ESP_UTILS_CONF_FILE_SKIP + #ifdef ESP_UTILS_CONF_PATH /* If there is a path defined for "esp_utils_conf.h" use it */ + #define __TO_STR_AUX(x) #x + #define __TO_STR(x) __TO_STR_AUX(x) + #include __TO_STR(ESP_UTILS_CONF_PATH) + #undef __TO_STR_AUX + #undef __TO_STR + #elif defined(ESP_UTILS_CONF_INCLUDE_SIMPLE) /* Or simply include if "esp_utils_conf.h" is available */ + #include "esp_utils_conf.h" + #elif defined(ESP_UTILS_CONF_INCLUDE_OUTSIDE) /* Or include if "../../esp_utils_conf.h" is available */ + #include "../../esp_utils_conf.h" + #elif defined(ESP_UTILS_CONF_INCLUDE_INSIDE) /* Or include the default configuration */ + #include "../esp_utils_conf.h" + #endif +#endif + +#ifndef ESP_UTILS_CONF_INCLUDE_INSIDE + /** + * There are two purposes to include the this file: + * 1. Convert configuration items starting with `CONFIG_` to the required configuration items. + * 2. Define default values for configuration items that are not defined to keep compatibility. + * + */ + #include "esp_utils_conf_kconfig.h" +#endif + +// *INDENT-OFF* diff --git a/src/esp_utils_conf_kconfig.h b/src/esp_utils_conf_kconfig.h new file mode 100644 index 0000000..cdba301 --- /dev/null +++ b/src/esp_utils_conf_kconfig.h @@ -0,0 +1,114 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +// *INDENT-OFF* + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////// Check Configurations ///////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/** + * Check functions related. + * + */ +#ifndef ESP_UTILS_CONF_ENABLE_CHECK + #ifdef CONFIG_ESP_UTILS_CONF_ENABLE_CHECK + #define ESP_UTILS_CONF_ENABLE_CHECK CONFIG_ESP_UTILS_CONF_ENABLE_CHECK + #else + #define ESP_UTILS_CONF_ENABLE_CHECK (0) + #endif +#endif +#ifndef ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG + #ifdef CONFIG_ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG + #define ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG CONFIG_ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG + #else + #define ESP_UTILS_CONF_CHECK_WITH_ERROR_LOG (0) + #endif +#endif +#ifndef ESP_UTILS_CONF_CHECK_WITH_ASSERT + #ifdef CONFIG_ESP_UTILS_CONF_CHECK_WITH_ASSERT + #define ESP_UTILS_CONF_CHECK_WITH_ASSERT CONFIG_ESP_UTILS_CONF_CHECK_WITH_ASSERT + #else + #define ESP_UTILS_CONF_CHECK_WITH_ASSERT (0) + #endif +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////// LOG Configurations ////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#ifndef ESP_UTILS_CONF_ENABLE_LOG + #ifdef CONFIG_ESP_UTILS_CONF_ENABLE_LOG + #define ESP_UTILS_CONF_ENABLE_LOG CONFIG_ESP_UTILS_CONF_ENABLE_LOG + #else + #define ESP_UTILS_CONF_ENABLE_LOG (0) + #endif +#endif + +#ifndef ESP_UTILS_CONF_LOG_BUFFER_SIZE + #ifdef CONFIG_ESP_UTILS_CONF_LOG_BUFFER_SIZE + #define ESP_UTILS_CONF_LOG_BUFFER_SIZE CONFIG_ESP_UTILS_CONF_LOG_BUFFER_SIZE + #else + #define ESP_UTILS_CONF_LOG_BUFFER_SIZE (256) + #endif +#endif + +#ifndef ESP_UTILS_CONF_LOG_LEVEL + #ifdef CONFIG_ESP_UTILS_CONF_LOG_LEVEL + #define ESP_UTILS_CONF_LOG_LEVEL CONFIG_ESP_UTILS_CONF_LOG_LEVEL + #else + #define ESP_UTILS_CONF_LOG_LEVEL ESP_UTILS_LOG_LEVEL_INFO + #endif +#endif + +#ifndef ESP_UTILS_CONF_ENABLE_LOG_TRACE + #ifdef CONFIG_ESP_UTILS_CONF_ENABLE_LOG_TRACE + #define ESP_UTILS_CONF_ENABLE_LOG_TRACE CONFIG_ESP_UTILS_CONF_ENABLE_LOG_TRACE + #else + #define ESP_UTILS_CONF_ENABLE_LOG_TRACE 0 + #endif +#endif + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////// Memory Configurations ///////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE + #ifdef CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE + #else + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE (ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE_STDLIB) + #endif +#endif + +#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP + #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN + #ifdef CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN + #else + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_ESP`" + #endif + #endif + + #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_CAPS + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_CAPS` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_ESP`" + #endif +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM + #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE + #ifdef CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE + #define ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE CONFIG_ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE + #else + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" + #endif + #endif + + #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" + #endif + + #ifndef ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_FREE + #error "`ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_FREE` must be defined when `ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM`" + #endif +#endif diff --git a/src/esp_utils_config_internal.h b/src/esp_utils_config_internal.h deleted file mode 100644 index a39cf69..0000000 --- a/src/esp_utils_config_internal.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -// *INDENT-OFF* - -/* Handle special Kconfig options */ -#ifndef ESP_UTILS_KCONFIG_IGNORE - #include "sdkconfig.h" - #ifdef CONFIG_ESP_UTILS_CONFIG_FILE_SKIP - #define ESP_UTILS_CONFIG_FILE_SKIP - #endif -#endif - -#ifndef ESP_UTILS_CONFIG_FILE_SKIP -/* If "esp_utils_config.h" is available from here, try to use it later */ -#ifdef __has_include - #if __has_include("esp_utils_config.h") - #ifndef ESP_UTILS_CONFIG_INCLUDE_SIMPLE - #define ESP_UTILS_CONFIG_INCLUDE_SIMPLE - #endif - #elif __has_include("../../esp_utils_config.h") - #ifndef ESP_UTILS_CONFIG_INCLUDE_OUTSIDE - #define ESP_UTILS_CONFIG_INCLUDE_OUTSIDE - #endif - #else - #define ESP_UTILS_CONFIG_INCLUDE_INSIDE - #endif -#endif -#else -#endif - -/* If "esp_utils_config.h" is not skipped, include it */ -#ifndef ESP_UTILS_CONFIG_FILE_SKIP - #ifdef ESP_UTILS_CONFIG_PATH /* If there is a path defined for "esp_utils_config.h" use it */ - #define __TO_STR_AUX(x) #x - #define __TO_STR(x) __TO_STR_AUX(x) - #include __TO_STR(ESP_UTILS_CONFIG_PATH) - #undef __TO_STR_AUX - #undef __TO_STR - #elif defined(ESP_UTILS_CONFIG_INCLUDE_SIMPLE) /* Or simply include if "esp_utils_config.h" is available */ - #include "esp_utils_config.h" - #elif defined(ESP_UTILS_CONFIG_INCLUDE_OUTSIDE) /* Or include if "../../esp_utils_config.h" is available */ - #include "../../esp_utils_config.h" - #elif defined(ESP_UTILS_CONFIG_INCLUDE_INSIDE) /* Or include the default configuration */ - #include "../esp_utils_config.h" - #endif -#endif - -#include "esp_utils_types.h" - -#ifndef ESP_UTILS_CONFIG_INCLUDE_INSIDE - /** - * There are two purposes to include the this file: - * 1. Convert configuration items starting with `CONFIG_` to the required configuration items. - * 2. Define default values for configuration items that are not defined to keep compatibility. - * - */ - #include "esp_utils_config_kconfig.h" -#endif - -// *INDENT-OFF* diff --git a/src/esp_utils_config_kconfig.h b/src/esp_utils_config_kconfig.h deleted file mode 100644 index f0e8238..0000000 --- a/src/esp_utils_config_kconfig.h +++ /dev/null @@ -1,109 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -// *INDENT-OFF* - -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -///////////////////////////////////////////////// Check Configurations ///////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -/** - * Check functions related. - * - */ -#ifndef ESP_UTILS_ENABLE_CHECK - #ifdef CONFIG_ESP_UTILS_ENABLE_CHECK - #define ESP_UTILS_ENABLE_CHECK CONFIG_ESP_UTILS_ENABLE_CHECK - #else - #define ESP_UTILS_ENABLE_CHECK (0) - #endif -#endif -#ifndef ESP_UTILS_CHECK_WITH_ERROR_LOG - #ifdef CONFIG_ESP_UTILS_CHECK_WITH_ERROR_LOG - #define ESP_UTILS_CHECK_WITH_ERROR_LOG CONFIG_ESP_UTILS_CHECK_WITH_ERROR_LOG - #else - #define ESP_UTILS_CHECK_WITH_ERROR_LOG (0) - #endif -#endif -#ifndef ESP_UTILS_CHECK_WITH_ASSERT - #ifdef CONFIG_ESP_UTILS_CHECK_WITH_ASSERT - #define ESP_UTILS_CHECK_WITH_ASSERT CONFIG_ESP_UTILS_CHECK_WITH_ASSERT - #else - #define ESP_UTILS_CHECK_WITH_ASSERT (0) - #endif -#endif - -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -////////////////////////////////////////////////// LOG Configurations ////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -#ifndef ESP_UTILS_ENABLE_LOG - #ifdef CONFIG_ESP_UTILS_ENABLE_LOG - #define ESP_UTILS_ENABLE_LOG CONFIG_ESP_UTILS_ENABLE_LOG - #else - #define ESP_UTILS_ENABLE_LOG (0) - #endif -#endif - -#ifndef ESP_UTILS_LOG_BUFFER_SIZE - #ifdef CONFIG_ESP_UTILS_LOG_BUFFER_SIZE - #define ESP_UTILS_LOG_BUFFER_SIZE CONFIG_ESP_UTILS_LOG_BUFFER_SIZE - #else - #define ESP_UTILS_LOG_BUFFER_SIZE (256) - #endif -#endif - -#ifndef ESP_UTILS_LOG_GLOBAL_LEVEL - #ifdef CONFIG_ESP_UTILS_LOG_GLOBAL_LEVEL - #define ESP_UTILS_LOG_GLOBAL_LEVEL CONFIG_ESP_UTILS_LOG_GLOBAL_LEVEL - #else - #define ESP_UTILS_LOG_GLOBAL_LEVEL ESP_UTILS_LOG_LEVEL_INFO - #endif -#endif - -#ifndef ESP_UTILS_ENABLE_LOG_TRACE - #ifdef CONFIG_ESP_UTILS_ENABLE_LOG_TRACE - #define ESP_UTILS_ENABLE_LOG_TRACE CONFIG_ESP_UTILS_ENABLE_LOG_TRACE - #else - #define ESP_UTILS_ENABLE_LOG_TRACE 0 - #endif -#endif - -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////// Memory Configurations ///////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -#ifndef ESP_UTILS_MEM_GENERAL_ALLOC_TYPE - #ifdef CONFIG_ESP_UTILS_MEM_GENERAL_ALLOC_TYPE - #define ESP_UTILS_MEM_GENERAL_ALLOC_TYPE CONFIG_ESP_UTILS_MEM_GENERAL_ALLOC_TYPE - #else - #define ESP_UTILS_MEM_GENERAL_ALLOC_TYPE (ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_STDLIB) - #endif -#endif - - -#if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP - #ifndef ESP_UTILS_MEM_GENERAL_ALLOC_ESP_CAPS - #ifdef CONFIG_ESP_UTILS_MEM_GENERAL_ALLOC_ESP_CAPS - #define ESP_UTILS_MEM_GENERAL_ALLOC_ESP_CAPS CONFIG_ESP_UTILS_MEM_GENERAL_ALLOC_ESP_CAPS - #else - #error "`ESP_UTILS_MEM_GENERAL_ALLOC_ESP_CAPS` must be defined when `ESP_UTILS_MEM_GENERAL_ALLOC_TYPE` is set to `ESP_UTILS_MEM_ALLOC_TYPE_ESP`" - #endif - #endif -#elif ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM - #ifndef ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE - #ifdef CONFIG_ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE - #define ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE CONFIG_ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE - #else - #define ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE "stdlib.h" - #endif - #endif - - #ifndef ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_MALLOC - #error "`ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_MALLOC` must be defined when `ESP_UTILS_MEM_GENERAL_ALLOC_TYPE` is set to `ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_CUSTOM`" - #endif - - #ifndef ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_FREE - #error "`ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_FREE` must be defined when `ESP_UTILS_MEM_GENERAL_ALLOC_TYPE` is set to `ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_CUSTOM`" - #endif -#endif diff --git a/src/esp_utils_versions.h b/src/esp_utils_versions.h index bad97ab..7cdd8a8 100644 --- a/src/esp_utils_versions.h +++ b/src/esp_utils_versions.h @@ -5,17 +5,17 @@ */ #pragma once -#include "esp_utils_config_internal.h" +#include "esp_utils_conf_internal.h" /* Library Version */ #define ESP_UTILS_VERSION_MAJOR 0 #define ESP_UTILS_VERSION_MINOR 1 #define ESP_UTILS_VERSION_PATCH 0 -/* File `esp_utils_config.h` */ -#define ESP_UTILS_CONFIG_VERSION_MAJOR 0 -#define ESP_UTILS_CONFIG_VERSION_MINOR 1 -#define ESP_UTILS_CONFIG_VERSION_PATCH 0 +/* File `esp_utils_conf.h` */ +#define ESP_UTILS_CONF_VERSION_MAJOR 0 +#define ESP_UTILS_CONF_VERSION_MINOR 1 +#define ESP_UTILS_CONF_VERSION_PATCH 0 // *INDENT-OFF* @@ -23,16 +23,16 @@ * Check if the current configuration file version is compatible with the library version * */ -/* File `esp_utils_config.h` */ -#ifndef ESP_UTILS_CONFIG_FILE_SKIP +/* File `esp_utils_conf.h` */ +#ifndef ESP_UTILS_CONF_FILE_SKIP // Check if the current configuration file version is compatible with the library version - #if ESP_UTILS_CONFIG_FILE_VERSION_MAJOR != ESP_UTILS_CONFIG_VERSION_MAJOR - #error "The file `esp_utils_config.h` version is not compatible. Please update it with the file from the library" - #elif ESP_UTILS_CONFIG_FILE_VERSION_MINOR < ESP_UTILS_CONFIG_VERSION_MINOR - #warning "The file `esp_utils_config.h` version is outdated. Some new configurations are missing" - #elif ESP_UTILS_CONFIG_FILE_VERSION_PATCH > ESP_UTILS_VERSION_PATCH - #warning "The file `esp_utils_config.h` version is newer than the library. Some new configurations are not supported" - #endif /* ESP_UTILS_CONFIG_INCLUDE_INSIDE */ -#endif /* ESP_UTILS_CONFIG_FILE_SKIP */ + #if ESP_UTILS_CONF_FILE_VERSION_MAJOR != ESP_UTILS_CONF_VERSION_MAJOR + #error "The file `esp_utils_conf.h` version is not compatible. Please update it with the file from the library" + #elif ESP_UTILS_CONF_FILE_VERSION_MINOR < ESP_UTILS_CONF_VERSION_MINOR + #warning "The file `esp_utils_conf.h` version is outdated. Some new configurations are missing" + #elif ESP_UTILS_CONF_FILE_VERSION_PATCH > ESP_UTILS_VERSION_PATCH + #warning "The file `esp_utils_conf.h` version is newer than the library. Some new configurations are not supported" + #endif /* ESP_UTILS_CONF_INCLUDE_INSIDE */ +#endif /* ESP_UTILS_CONF_FILE_SKIP */ // *INDENT-OFF* diff --git a/src/log/esp_utils_log.c b/src/log/esp_utils_log.c index 7e03502..7573367 100644 --- a/src/log/esp_utils_log.c +++ b/src/log/esp_utils_log.c @@ -4,9 +4,9 @@ * SPDX-License-Identifier: Apache-2.0 */ #include -#include "esp_utils_config_internal.h" +#include "esp_utils_conf_internal.h" -#if ESP_UTILS_ENABLE_LOG +#if ESP_UTILS_CONF_ENABLE_LOG /** * @brief Extract filename from file path * @@ -22,4 +22,4 @@ const char *esp_utils_log_extract_file_name(const char *file_path) return filename ? filename + 1 : file_path; } -#endif // ESP_UTILS_ENABLE_LOG +#endif // ESP_UTILS_CONF_ENABLE_LOG diff --git a/src/log/esp_utils_log.h b/src/log/esp_utils_log.h index 2e3308f..2d36f6c 100644 --- a/src/log/esp_utils_log.h +++ b/src/log/esp_utils_log.h @@ -5,7 +5,7 @@ */ #pragma once -#include "esp_utils_config_internal.h" +#include "esp_utils_conf_internal.h" #ifndef ESP_UTILS_LOG_TAG #define ESP_UTILS_LOG_TAG "Utils" @@ -36,13 +36,13 @@ class Log { return instance; } -#if ESP_UTILS_ENABLE_LOG +#if ESP_UTILS_CONF_ENABLE_LOG // Templates and conditional compilation: Filter logs by different levels template void print(const char *file, int line, const char *func, const char *format, ...) { // Logs below the global level will not be compiled - if constexpr (level >= ESP_UTILS_LOG_GLOBAL_LEVEL) { + if constexpr (level >= ESP_UTILS_CONF_LOG_LEVEL) { // Mutex to avoid interleaved log messages _mutex.lock(); // Use variadic arguments for formatted output @@ -52,7 +52,7 @@ class Log { va_end(args); // Output log message printf( - "[%s][%s][%s:%04d](%s): %s\n", ESP_UTILS_LOG_TAG, logLevelToString(level), + "[%c][%s][%s:%04d](%s): %s\n", logLevelToChar(level), ESP_UTILS_LOG_TAG, esp_utils_log_extract_file_name(file), line, func, _buffer ); _mutex.unlock(); @@ -62,26 +62,26 @@ class Log { // When logging is disabled, the `print` function has an empty implementation template void print(const char *, int, const char *, const char *, ...) const {} -#endif /* ESP_UTILS_ENABLE_LOG */ +#endif /* ESP_UTILS_CONF_ENABLE_LOG */ private: Log() = default; -#if ESP_UTILS_ENABLE_LOG +#if ESP_UTILS_CONF_ENABLE_LOG // Convert log level to string - static const char *logLevelToString(int level) + static constexpr char logLevelToChar(int level) { switch (level) { - case ESP_UTILS_LOG_LEVEL_DEBUG: return "DEBUG"; - case ESP_UTILS_LOG_LEVEL_INFO: return " INFO"; - case ESP_UTILS_LOG_LEVEL_WARNING: return " WARN"; - case ESP_UTILS_LOG_LEVEL_ERROR: return "ERROR"; + case ESP_UTILS_LOG_LEVEL_DEBUG: return 'D'; + case ESP_UTILS_LOG_LEVEL_INFO: return 'I'; + case ESP_UTILS_LOG_LEVEL_WARNING: return 'W'; + case ESP_UTILS_LOG_LEVEL_ERROR: return 'E'; default: break; } - return "UNKNOWN"; + return ' '; } - char _buffer[ESP_UTILS_LOG_BUFFER_SIZE]; + char _buffer[ESP_UTILS_CONF_LOG_BUFFER_SIZE]; std::mutex _mutex; #endif }; @@ -105,7 +105,7 @@ class Log { * Micros to log trace of function calls * */ -#if ESP_UTILS_ENABLE_LOG_TRACE +#if ESP_UTILS_CONF_ENABLE_LOG_TRACE #define ESP_UTILS_LOG_TRACE_ENTER_WITH_THIS() ESP_UTILS_LOGD("(@%p) Enter", this) #define ESP_UTILS_LOG_TRACE_EXIT_WITH_THIS() ESP_UTILS_LOGD("(@%p) Exit", this) #else @@ -120,13 +120,13 @@ class Log { const char *esp_utils_log_extract_file_name(const char *file_path); -#define ESP_UTILS_IMPL_LOGD(format, ...) printf("[" ESP_UTILS_LOG_TAG "][DEBUG][%s:%04d](%s): " format "\n", \ +#define ESP_UTILS_IMPL_LOGD(format, ...) printf("[D][" ESP_UTILS_LOG_TAG "][%s:%04d](%s): " format "\n", \ esp_utils_log_extract_file_name(__FILE__), __LINE__, __func__, ##__VA_ARGS__) -#define ESP_UTILS_IMPL_LOGI(format, ...) printf("[" ESP_UTILS_LOG_TAG "][ INFO][%s:%04d](%s): " format "\n", \ +#define ESP_UTILS_IMPL_LOGI(format, ...) printf("[I][" ESP_UTILS_LOG_TAG "][%s:%04d](%s): " format "\n", \ esp_utils_log_extract_file_name(__FILE__), __LINE__, __func__, ##__VA_ARGS__) -#define ESP_UTILS_IMPL_LOGW(format, ...) printf("[" ESP_UTILS_LOG_TAG "][ WARN][%s:%04d](%s): " format "\n", \ +#define ESP_UTILS_IMPL_LOGW(format, ...) printf("[W][" ESP_UTILS_LOG_TAG "][%s:%04d](%s): " format "\n", \ esp_utils_log_extract_file_name(__FILE__), __LINE__, __func__, ##__VA_ARGS__) -#define ESP_UTILS_IMPL_LOGE(format, ...) printf("[" ESP_UTILS_LOG_TAG "][ERROR][%s:%04d](%s): " format "\n", \ +#define ESP_UTILS_IMPL_LOGE(format, ...) printf("[E][" ESP_UTILS_LOG_TAG "][%s:%04d](%s): " format "\n", \ esp_utils_log_extract_file_name(__FILE__), __LINE__, __func__, ##__VA_ARGS__) #define ESP_UTILS_LOG_LEVEL(level, format, ...) do { \ @@ -138,14 +138,14 @@ const char *esp_utils_log_extract_file_name(const char *file_path); } while(0) #define ESP_UTILS_LOG_LEVEL_LOCAL(level, format, ...) do { \ - if (level >= ESP_UTILS_LOG_GLOBAL_LEVEL) ESP_UTILS_LOG_LEVEL(level, format, ##__VA_ARGS__); \ + if (level >= ESP_UTILS_CONF_LOG_LEVEL) ESP_UTILS_LOG_LEVEL(level, format, ##__VA_ARGS__); \ } while(0) /** * Macros to simplify logging calls * */ -#if ESP_UTILS_ENABLE_LOG +#if ESP_UTILS_CONF_ENABLE_LOG #define ESP_UTILS_LOGD(format, ...) ESP_UTILS_LOG_LEVEL_LOCAL(ESP_UTILS_LOG_LEVEL_DEBUG, format, ##__VA_ARGS__) #define ESP_UTILS_LOGI(format, ...) ESP_UTILS_LOG_LEVEL_LOCAL(ESP_UTILS_LOG_LEVEL_INFO, format, ##__VA_ARGS__) #define ESP_UTILS_LOGW(format, ...) ESP_UTILS_LOG_LEVEL_LOCAL(ESP_UTILS_LOG_LEVEL_WARNING, format, ##__VA_ARGS__) @@ -155,7 +155,7 @@ const char *esp_utils_log_extract_file_name(const char *file_path); #define ESP_UTILS_LOGI(...) #define ESP_UTILS_LOGW(...) #define ESP_UTILS_LOGE(...) -#endif // ESP_UTILS_ENABLE_LOG +#endif // ESP_UTILS_CONF_ENABLE_LOG #endif // __cplusplus @@ -163,7 +163,7 @@ const char *esp_utils_log_extract_file_name(const char *file_path); * Micros to log trace of function calls * */ -#if ESP_UTILS_ENABLE_LOG_TRACE +#if ESP_UTILS_CONF_ENABLE_LOG_TRACE #define ESP_UTILS_LOG_TRACE_ENTER() ESP_UTILS_LOGD("Enter") #define ESP_UTILS_LOG_TRACE_EXIT() ESP_UTILS_LOGD("Exit") #else diff --git a/src/memory/esp_utils_mem.c b/src/memory/esp_utils_mem.c new file mode 100644 index 0000000..2c8e254 --- /dev/null +++ b/src/memory/esp_utils_mem.c @@ -0,0 +1,57 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include +#include "esp_utils_conf_internal.h" +#include "check/esp_utils_check.h" +#include "log/esp_utils_log.h" +#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP +#include "esp_heap_caps.h" +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM +#include ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_INCLUDE +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON +#include +#include +#include +#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE + +void *esp_utils_mem_gen_malloc(size_t size) +{ + void *p = NULL; +#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_STDLIB + p = malloc(size); +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP + p = heap_caps_aligned_alloc(ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_ALIGN, size, ESP_UTILS_CONF_MEM_GEN_ALLOC_ESP_CAPS); +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM + p = ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_MALLOC(size); +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON +#if MICROPY_MALLOC_USES_ALLOCATED_SIZE + return gc_alloc(size, true); +#else + return m_malloc(size); +#endif // MICROPY_MALLOC_USES_ALLOCATED_SIZE +#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE + ESP_UTILS_LOGD("Malloc @%p: %d", p, (int)size); + return p; +} + +void esp_utils_mem_gen_free(void *p) +{ + ESP_UTILS_LOGD("Free @%p", p); +#if ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_STDLIB + free(p); +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP + heap_caps_free(p); +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM + ESP_UTILS_CONF_MEM_GEN_ALLOC_CUSTOM_FREE(p); +#elif ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON +#if MICROPY_MALLOC_USES_ALLOCATED_SIZE + gc_free(p); +#else + m_free(p); +#endif // MICROPY_MALLOC_USES_ALLOCATED_SIZE +#endif // ESP_UTILS_CONF_MEM_GEN_ALLOC_TYPE +} diff --git a/src/memory/esp_utils_memory.h b/src/memory/esp_utils_mem.h similarity index 69% rename from src/memory/esp_utils_memory.h rename to src/memory/esp_utils_mem.h index a7a868e..bad5905 100644 --- a/src/memory/esp_utils_memory.h +++ b/src/memory/esp_utils_mem.h @@ -6,33 +6,33 @@ #pragma once #include "sdkconfig.h" -#include "esp_utils_config_internal.h" +#include "esp_utils_conf_internal.h" #ifdef __cplusplus #include #include -extern "C" void *esp_utils_memory_malloc(size_t size); -extern "C" void esp_utils_memory_free(void *p); +extern "C" void *esp_utils_mem_gen_malloc(size_t size); +extern "C" void esp_utils_mem_gen_free(void *p); namespace esp_utils { template -struct MemoryAllocator { +struct GeneralMemoryAllocator { using value_type = T; - MemoryAllocator() = default; + GeneralMemoryAllocator() = default; template - MemoryAllocator(const MemoryAllocator &) {} + GeneralMemoryAllocator(const GeneralMemoryAllocator &) {} T *allocate(std::size_t n) { if (n == 0) { return nullptr; } - void *ptr = esp_utils_memory_malloc(n * sizeof(T)); + void *ptr = esp_utils_mem_gen_malloc(n * sizeof(T)); #if CONFIG_COMPILER_CXX_EXCEPTIONS if (ptr == nullptr) { throw std::bad_alloc(); @@ -43,7 +43,7 @@ struct MemoryAllocator { void deallocate(T *p, std::size_t n) { - esp_utils_memory_free(p); + esp_utils_mem_gen_free(p); } template @@ -66,15 +66,15 @@ struct MemoryAllocator { template std::shared_ptr make_shared(Args &&... args) { - return std::allocate_shared>(MemoryAllocator(), std::forward(args)...); + return std::allocate_shared>(GeneralMemoryAllocator(), std::forward(args)...); } } // namespace esp_utils #else -void *esp_utils_memory_malloc(size_t size); -void esp_utils_memory_free(void *p); +void *esp_utils_mem_gen_malloc(size_t size); +void esp_utils_mem_gen_free(void *p); #endif // __cplusplus @@ -85,8 +85,8 @@ void esp_utils_memory_free(void *p); #undef malloc #undef free #undef calloc -#define malloc(size) esp_utils_memory_malloc(size) -#define free(ptr) esp_utils_memory_free(ptr) +#define malloc(size) esp_utils_mem_gen_malloc(size) +#define free(ptr) esp_utils_mem_gen_free(ptr) #define calloc(n, size) \ ({ \ size_t _size = (size_t)n * size; \ diff --git a/src/memory/esp_utils_memory.c b/src/memory/esp_utils_memory.c deleted file mode 100644 index 2bdc3ec..0000000 --- a/src/memory/esp_utils_memory.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include -#include "esp_utils_config_internal.h" -#include "check/esp_utils_check.h" -#include "log/esp_utils_log.h" -#if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_ESP -#include "esp_heap_caps.h" -#elif ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_GENERAL_ALLOC_TYPE_CUSTOM -#include ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE -#endif - -void *esp_utils_memory_malloc(size_t size) -{ - void *p = NULL; -#if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON -#elif ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP - p = heap_caps_malloc(size, ESP_UTILS_MEM_GENERAL_ALLOC_ESP_CAPS); -#elif ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM - p = ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_MALLOC(size); -#else - p = malloc(size); -#endif // ESP_UTILS_MEM_GENERAL_ALLOC_TYPE - ESP_UTILS_LOGD("Malloc @%p: %d", p, (int)size); - return p; -} - -void esp_utils_memory_free(void *p) -{ - ESP_UTILS_LOGD("Free @%p", p); -#if ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_MICROPYTHON -#elif ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_ESP - heap_caps_free(p); -#elif ESP_UTILS_MEM_GENERAL_ALLOC_TYPE == ESP_UTILS_MEM_ALLOC_TYPE_CUSTOM - ESP_UTILS_MEM_GENERAL_ALLOC_CUSTOM_FREE(p); -#else - free(p); -#endif // ESP_UTILS_MEM_GENERAL_ALLOC_TYPE -} diff --git a/test_apps/CMakeLists.txt b/test_apps/CMakeLists.txt index 112dd03..dff5723 100644 --- a/test_apps/CMakeLists.txt +++ b/test_apps/CMakeLists.txt @@ -1,6 +1,6 @@ # The following lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -set(COMPONENTS main) +set(COMPONENTS main esp_heap_caps) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(lib_utils_test) diff --git a/test_apps/main/CMakeLists.txt b/test_apps/main/CMakeLists.txt index e425c85..5634a76 100644 --- a/test_apps/main/CMakeLists.txt +++ b/test_apps/main/CMakeLists.txt @@ -2,3 +2,41 @@ idf_component_register( SRCS "test_app_main.cpp" "test_on_c.c" "test_on_cpp.cpp" WHOLE_ARCHIVE ) + + idf_build_get_property(build_components BUILD_COMPONENTS) + foreach(COMPONENT ${build_components}) + if(COMPONENT MATCHES "esp-lib-utils" OR COMPONENT MATCHES "espressif__esp-lib-utils") + set(TARGET_COMPONENT ${COMPONENT}) + break() + endif() + endforeach() + + if(TARGET_COMPONENT STREQUAL "") + message(FATAL_ERROR "Component 'esp-lib-utils' not found.") + else() + idf_component_get_property(ESP_LIB_UTILS_LIB ${TARGET_COMPONENT} COMPONENT_LIB) + endif() + +if(CONFIG_ESP_UTILS_CONF_MEM_GENERAL_ALLOC_TYPE_ESP) + target_compile_options( + ${ESP_LIB_UTILS_LIB} + PUBLIC + "-DESP_UTILS_CONF_MEM_GENERAL_ALLOC_ESP_CAPS=(MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT)" + ) +elseif(CONFIG_ESP_UTILS_CONF_MEM_GENERAL_ALLOC_TYPE_CUSTOM) + target_compile_options( + ${ESP_LIB_UTILS_LIB} + PUBLIC + "-DESP_UTILS_CONF_MEM_GENERAL_ALLOC_CUSTOM_INCLUDE=\"esp_heap_caps.h\"" + ) + target_compile_options( + ${ESP_LIB_UTILS_LIB} + PUBLIC + "-DESP_UTILS_CONF_MEM_GENERAL_ALLOC_CUSTOM_MALLOC(x)=heap_caps_aligned_alloc(1, x, MALLOC_CAP_DEFAULT | MALLOC_CAP_8BIT)" + ) + target_compile_options( + ${ESP_LIB_UTILS_LIB} + PUBLIC + "-DESP_UTILS_CONF_MEM_GENERAL_ALLOC_CUSTOM_FREE(x)=heap_caps_free(x)" + ) +endif() diff --git a/test_apps/main/test_app_main.cpp b/test_apps/main/test_app_main.cpp index 31d6951..c93f289 100644 --- a/test_apps/main/test_app_main.cpp +++ b/test_apps/main/test_app_main.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/test_apps/main/test_on_c.c b/test_apps/main/test_on_c.c index 80f012e..d07fae0 100644 --- a/test_apps/main/test_on_c.c +++ b/test_apps/main/test_on_c.c @@ -5,7 +5,7 @@ */ #include "unity.h" #define ESP_UTILS_LOG_TAG "TestC" -#include "esp_utils_library.h" +#include "esp_lib_utils.h" TEST_CASE("Test log functions on C", "[utils][log][C]") { @@ -32,10 +32,10 @@ TEST_CASE("Test memory functions on C", "[utils][memory][C]") ESP_UTILS_CHECK_NULL_GOTO(bad_ptr, end, "Failed to allocate memory size: %d", MALLOC_BAD_SIZE); ESP_UTILS_LOGI("Malloced value: %d", bad_ptr[0]); -err: + ESP_UTILS_CHECK_TAG(err) TEST_ASSERT(false && "Memory allocation failed"); -end: + ESP_UTILS_CHECK_TAG(end) free(good_ptr); return; @@ -56,12 +56,12 @@ static bool test_check_false_goto(void) ESP_UTILS_CHECK_FALSE_GOTO(true, err, "Check false goto failed"); ESP_UTILS_CHECK_FALSE_GOTO(false, end, "Check false goto success"); -err: + ESP_UTILS_CHECK_TAG(err) TEST_ASSERT(false && "Check false goto failed"); return false; -end: + ESP_UTILS_CHECK_TAG(end) return true; } @@ -90,12 +90,12 @@ static bool test_check_error_goto(void) ESP_UTILS_CHECK_ERROR_GOTO(ESP_OK, err, "Check error goto failed"); ESP_UTILS_CHECK_ERROR_GOTO(ESP_FAIL, end, "Check error goto success"); -err: + ESP_UTILS_CHECK_TAG(err) TEST_ASSERT(false && "Check error goto failed"); return false; -end: + ESP_UTILS_CHECK_TAG(end) return true; } @@ -124,12 +124,12 @@ static bool test_check_null_goto(void) ESP_UTILS_CHECK_NULL_GOTO((void *)1, err, "Check null goto failed"); ESP_UTILS_CHECK_NULL_GOTO(NULL, end, "Check null goto success"); -err: + ESP_UTILS_CHECK_TAG(err) TEST_ASSERT(false && "Check null goto failed"); return false; -end: + ESP_UTILS_CHECK_TAG(end) return true; } diff --git a/test_apps/main/test_on_cpp.cpp b/test_apps/main/test_on_cpp.cpp index 27d614e..e8e0851 100644 --- a/test_apps/main/test_on_cpp.cpp +++ b/test_apps/main/test_on_cpp.cpp @@ -6,7 +6,7 @@ #include #include "unity.h" #define ESP_UTILS_LOG_TAG "TestCpp" -#include "esp_utils_library.h" +#include "esp_lib_utils.h" TEST_CASE("Test log functions on cpp", "[utils][log][CPP]") { @@ -49,10 +49,10 @@ TEST_CASE("Test memory functions on cpp", "[utils][memory][CPP]") ESP_UTILS_LOGI("Malloced value: %d", bad_ptr->buffer[0]); } -err: + ESP_UTILS_CHECK_TAG(err) TEST_ASSERT(false && "Memory allocation failed"); -end: + ESP_UTILS_CHECK_TAG(end) return; } @@ -71,12 +71,12 @@ static bool test_check_false_goto(void) ESP_UTILS_CHECK_FALSE_GOTO(true, err, "Check false goto failed"); ESP_UTILS_CHECK_FALSE_GOTO(false, end, "Check false goto success"); -err: + ESP_UTILS_CHECK_TAG(err) TEST_ASSERT(false && "Check false goto failed"); return false; -end: + ESP_UTILS_CHECK_TAG(end) return true; } @@ -105,12 +105,12 @@ static bool test_check_error_goto(void) ESP_UTILS_CHECK_ERROR_GOTO(ESP_OK, err, "Check error goto failed"); ESP_UTILS_CHECK_ERROR_GOTO(ESP_FAIL, end, "Check error goto success"); -err: + ESP_UTILS_CHECK_TAG(err) TEST_ASSERT(false && "Check error goto failed"); return false; -end: + ESP_UTILS_CHECK_TAG(end) return true; } @@ -139,12 +139,12 @@ static bool test_check_null_goto(void) ESP_UTILS_CHECK_NULL_GOTO((void *)1, err, "Check null goto failed"); ESP_UTILS_CHECK_NULL_GOTO(NULL, end, "Check null goto success"); -err: + ESP_UTILS_CHECK_TAG(err) TEST_ASSERT(false && "Check null goto failed"); return false; -end: + ESP_UTILS_CHECK_TAG(end) return true; } diff --git a/test_apps/sdkconfig.ci.custom_mem b/test_apps/sdkconfig.ci.custom_mem new file mode 100644 index 0000000..bcf1aa8 --- /dev/null +++ b/test_apps/sdkconfig.ci.custom_mem @@ -0,0 +1 @@ +CONFIG_ESP_UTILS_CONF_MEM_GENERAL_ALLOC_TYPE_CUSTOM=y diff --git a/test_apps/sdkconfig.ci.esp_mem b/test_apps/sdkconfig.ci.esp_mem new file mode 100644 index 0000000..5369f52 --- /dev/null +++ b/test_apps/sdkconfig.ci.esp_mem @@ -0,0 +1 @@ +CONFIG_ESP_UTILS_CONF_MEM_GENERAL_ALLOC_TYPE_ESP=y diff --git a/test_apps/sdkconfig.defaults b/test_apps/sdkconfig.defaults index 4e51191..0f0ddae 100644 --- a/test_apps/sdkconfig.defaults +++ b/test_apps/sdkconfig.defaults @@ -1,6 +1,5 @@ CONFIG_ESP_TASK_WDT_EN=n CONFIG_FREERTOS_HZ=1000 -CONFIG_COMPILER_CXX_EXCEPTIONS=y -CONFIG_ESP_UTILS_LOG_LEVEL_DEBUG=y -CONFIG_ESP_UTILS_ENABLE_LOG_TRACE=y +CONFIG_ESP_UTILS_CONF_LOG_LEVEL_DEBUG=y +CONFIG_ESP_UTILS_CONF_ENABLE_LOG_TRACE=y diff --git a/tools/check_file_version.py b/tools/check_file_version.py index 60cad84..d22708c 100644 --- a/tools/check_file_version.py +++ b/tools/check_file_version.py @@ -16,21 +16,21 @@ }, }, { - 'file': 'esp_utils_config.h', + 'file': 'esp_utils_conf.h', 'macro': { - 'major': 'ESP_UTILS_CONFIG_VERSION_MAJOR', - 'minor': 'ESP_UTILS_CONFIG_VERSION_MINOR', - 'patch': 'ESP_UTILS_CONFIG_VERSION_PATCH' + 'major': 'ESP_UTILS_CONF_VERSION_MAJOR', + 'minor': 'ESP_UTILS_CONF_VERSION_MINOR', + 'patch': 'ESP_UTILS_CONF_VERSION_PATCH' }, }, ] file_version_macros = [ { - 'file': 'esp_utils_config.h', + 'file': 'esp_utils_conf.h', 'macro': { - 'major': 'ESP_UTILS_CONFIG_FILE_VERSION_MAJOR', - 'minor': 'ESP_UTILS_CONFIG_FILE_VERSION_MINOR', - 'patch': 'ESP_UTILS_CONFIG_FILE_VERSION_PATCH' + 'major': 'ESP_UTILS_CONF_FILE_VERSION_MAJOR', + 'minor': 'ESP_UTILS_CONF_FILE_VERSION_MINOR', + 'patch': 'ESP_UTILS_CONF_FILE_VERSION_PATCH' }, }, ]