mirror of
https://github.com/espressif/esp-modbus.git
synced 2026-08-03 20:24:09 +02:00
Merge branch 'bugfix/fix_atomics_incompatibility_cpp' into 'main'
fix: atomics incompatibility cpp See merge request idf/esp-modbus!169
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Workaround for atomics incompatibility issue under CPP.
|
// Workaround for atomics incompatibility issue under CPP.
|
||||||
#if defined(__cplusplus) && (ESP_IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 0, 0))
|
#if defined(__cplusplus)
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#define _Atomic(T) std::atomic<T>
|
#define _Atomic(T) std::atomic<T>
|
||||||
#define atomic_int int
|
#define atomic_int int
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
set(PROJECT_NAME "mb_serial_cpp")
|
set(PROJECT_NAME "mb_serial_cpp")
|
||||||
|
|
||||||
idf_component_register(SRCS "serial_test.cpp"
|
idf_component_register(SRCS "serial_test.cpp"
|
||||||
REQUIRES esp-modbus nvs_flash esp_event esp_timer lwip
|
REQUIRES esp-modbus esp_common nvs_flash esp_event esp_timer lwip
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS ".")
|
||||||
|
|
||||||
|
# Explicitly use C++17 so we can get std::atomic support for the workaround in mb_port_types.h
|
||||||
|
target_compile_features(${COMPONENT_LIB} PUBLIC cxx_std_17)
|
||||||
|
|
||||||
# Workaround to avoid static analysis false positives for some components.
|
# Workaround to avoid static analysis false positives for some components.
|
||||||
if(CONFIG_FMB_COMPILER_STATIC_ANALYZER_ENABLE AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
if(CONFIG_FMB_COMPILER_STATIC_ANALYZER_ENABLE AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
|
||||||
target_compile_options(${COMPONENT_LIB} PRIVATE "-fanalyzer")
|
target_compile_options(${COMPONENT_LIB} PRIVATE "-fanalyzer")
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2024-2026 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_system.h"
|
||||||
|
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
#include "mbcontroller.h"
|
#include "mbcontroller.h"
|
||||||
|
|
||||||
|
#include "esp_idf_version.h"
|
||||||
|
#include "mb_port_types.h" // atomic layout and C++ compatibility
|
||||||
|
|
||||||
#define TEST_PORT_NUM (uart_port_t)1
|
#define TEST_PORT_NUM (uart_port_t)1
|
||||||
#define TEST_SPEED 115200
|
#define TEST_SPEED 115200
|
||||||
|
|
||||||
@@ -167,8 +171,20 @@ static_assert(
|
|||||||
"CPP atomic int types are not layout compatible with int"
|
"CPP atomic int types are not layout compatible with int"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
static int check_atomic_cpp_instantiation(void)
|
||||||
|
{
|
||||||
|
// Check atomic support: mb_uid_info_t.state is _Atomic(int) in C, std::atomic<int> in C++
|
||||||
|
mb_uid_info_t addr_info = {};
|
||||||
|
addr_info.index = 1;
|
||||||
|
atomic_store(&(addr_info.state), 23);
|
||||||
|
return atomic_load(&addr_info.state);
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" void app_main(void)
|
extern "C" void app_main(void)
|
||||||
{
|
{
|
||||||
|
ESP_LOGI(TAG, "mb_port_types.h C++ atomics test passed (IDF compatible), %d, %d, %s",
|
||||||
|
check_atomic_cpp_instantiation(), (int)ESP_IDF_VERSION, esp_get_idf_version());
|
||||||
|
|
||||||
// Initialization of device peripheral and objects
|
// Initialization of device peripheral and objects
|
||||||
ESP_LOGI(TAG, "Setup master cpp....");
|
ESP_LOGI(TAG, "Setup master cpp....");
|
||||||
ESP_ERROR_CHECK(master_serial_init(&pmaster_handle));
|
ESP_ERROR_CHECK(master_serial_init(&pmaster_handle));
|
||||||
|
|||||||
Reference in New Issue
Block a user