Skip to content

Commit 138382b

Browse files
committed
add cpp test-app, fix atomcs issue (+workaround v5.0)
1 parent a2a1654 commit 138382b

File tree

13 files changed

+192
-21
lines changed

13 files changed

+192
-21
lines changed

modbus/mb_objects/include/mb_config.h

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#pragma once
7-
7+
#include <inttypes.h>
88
#include "sdkconfig.h" // for KConfig options
99

10-
#ifdef __cplusplus
11-
extern "C" {
12-
#endif
13-
1410
#if __has_include("esp_idf_version.h")
1511
#include "esp_idf_version.h"
1612
#endif
1713

18-
#include <inttypes.h>
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
1917

2018
/* ----------------------- Defines ------------------------------------------*/
2119
/*! \defgroup modbus_cfg Modbus Configuration
@@ -48,8 +46,8 @@ extern "C" {
4846

4947
#ifdef ESP_IDF_VERSION
5048

51-
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0))
52-
// Features supported from 4.4
49+
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0))
50+
// Features supported from v5.0
5351
#define MB_TIMER_SUPPORTS_ISR_DISPATCH_METHOD 1
5452
#endif
5553

modbus/mb_objects/include/mb_port_types.h

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@
55
*/
66
#pragma once
77

8-
#include "stdatomic.h"
98
#include "mb_config.h"
109
#include "mb_types.h"
1110

12-
#define MB_ATTR_WEAK __attribute__ ((weak))
11+
#if __has_include("esp_idf_version.h")
12+
#include "esp_idf_version.h"
13+
#define IS_OLD_IDF_VER (IDF_VERSION <= ESP_IDF_VERSION_VAL(5, 0, 0))
14+
#endif
1315

14-
#ifdef __cplusplus
16+
// Workaround for atomics incompatibility issue under CPP.
17+
#if defined(__cplusplus) && IS_OLD_IDF_VER
18+
#include <atomic>
19+
#define _Atomic(T) std::atomic<T>
20+
#else
21+
#include <stdatomic.h>
22+
#endif
23+
24+
25+
26+
#if defined(__cplusplus)
1527
extern "C" {
1628
#endif
1729

30+
#define MB_ATTR_WEAK __attribute__ ((weak))
31+
1832
typedef enum _mb_comm_mode mb_mode_type_t;
1933

2034
#if (CONFIG_FMB_COMM_MODE_ASCII_EN || CONFIG_FMB_COMM_MODE_RTU_EN)
@@ -98,7 +112,7 @@ typedef struct _uid_info {
98112
uint16_t uid; /*!< node unit ID (UID) field for MBAP frame */
99113
uint16_t port; /*!< node port number */
100114
mb_comm_mode_t proto; /*!< protocol type */
101-
atomic_int state; /*!< node state */
115+
_Atomic(int) state; /*!< node state */
102116
void *inst; /*!< pointer to linked instance */
103117
} mb_uid_info_t;
104118

modbus/mb_ports/common/mb_transaction.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ typedef struct transaction_item {
1919
int msg_id;
2020
void *pnode;
2121
transaction_tick_t tick;
22-
_Atomic pending_state_t state;
22+
_Atomic(int) state;
2323
STAILQ_ENTRY(transaction_item) next;
2424
} transaction_item_t;
2525

modbus/mb_ports/common/port_event.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ static const char *TAG = "mb_port.event";
1818

1919
struct mb_port_event_t
2020
{
21-
_Atomic mb_err_event_t curr_err_type;
21+
_Atomic(int) curr_err_type;
2222
SemaphoreHandle_t resource_hdl;
2323
EventGroupHandle_t event_group_hdl;
2424
QueueHandle_t event_hdl;
25-
_Atomic uint64_t curr_trans_id;
25+
_Atomic(uint64_t) curr_trans_id;
2626
};
2727

2828
mb_err_enum_t mb_port_event_create(mb_port_base_t *inst)

modbus/mb_ports/common/port_other.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "port_common.h"
1212

1313
/* ----------------------- Variables ----------------------------------------*/
14-
static _Atomic uint32_t inst_counter = 0;
14+
static _Atomic(uint32_t) inst_counter = 0;
1515

1616
/* ----------------------- Start implementation -----------------------------*/
1717
int lock_obj(_lock_t *plock)

modbus/mb_ports/common/port_timer.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ struct mb_port_timer_t
2929
//spinlock_t spin_lock;
3030
esp_timer_handle_t timer_handle;
3131
uint16_t t35_ticks;
32-
_Atomic uint32_t response_time_ms;
33-
_Atomic bool timer_state;
34-
_Atomic uint16_t timer_mode;
32+
_Atomic(uint32_t) response_time_ms;
33+
_Atomic(bool) timer_state;
34+
_Atomic(uint16_t) timer_mode;
3535
};
3636

3737
/* ----------------------- Static variables ---------------------------------*/

modbus/mb_ports/serial/port_serial.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ typedef struct
3737
uint64_t send_time_stamp;
3838
uint64_t recv_time_stamp;
3939
uint32_t flags;
40-
bool enabled;
40+
_Atomic(bool) enabled;
4141
QueueHandle_t uart_queue; // A queue to handle UART event.
4242
TaskHandle_t task_handle; // UART task to handle UART event.
4343
SemaphoreHandle_t bus_sema_handle; // Rx blocking semaphore handle
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(mb_serial_cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "serial_test.cpp"
2+
INCLUDE_DIRS ".")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies:
2+
idf: ">=5.0"
3+
espressif/esp-modbus:
4+
version: "^2.0.0"
5+
override_path: "../../../../"
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include "esp_log.h"
2+
3+
#include "sdkconfig.h"
4+
#include "mbcontroller.h"
5+
6+
#define TEST_PORT_NUM (uart_port_t)1
7+
#define TEST_SPEED 115200
8+
9+
#define TAG "CPP_TEST"
10+
#define MB_SLAVE_SHORT_ADDRESS 1
11+
12+
enum {
13+
MB_DEVICE_ADDR1 = 1
14+
};
15+
16+
// Enumeration of all supported CIDs for device (used in parameter definition table)
17+
enum {
18+
CID_DEV_REG0 = 0
19+
};
20+
21+
#define STR(fieldname) ((const char*)( fieldname ))
22+
#define OPTS(min_val, max_val, step_val) { .opt1 = min_val, .opt2 = max_val, .opt3 = step_val }
23+
24+
static void *pmaster_handle = NULL;
25+
static void *pslave_handle = NULL;
26+
27+
// Example Data (Object) Dictionary for Modbus parameters
28+
const mb_parameter_descriptor_t dummy_dict[] = {
29+
// CID, Name, Units, Modbus addr, register type, Modbus Reg Start Addr, Modbus Reg read length,
30+
// Instance offset (NA), Instance type, Instance length (bytes), Options (NA), Permissions
31+
{ CID_DEV_REG0, STR("MB_hold_reg-0"), STR("Data"), MB_DEVICE_ADDR1, MB_PARAM_HOLDING, 0, 1,
32+
0, PARAM_TYPE_U16, PARAM_SIZE_U16, OPTS( 0,0,0 ), PAR_PERMS_READ_WRITE_TRIGGER },
33+
};
34+
35+
// Calculate number of parameters in the table
36+
const uint16_t num_device_parameters = (sizeof(dummy_dict)/sizeof(dummy_dict[0]));
37+
38+
// Modbus serial master initialization
39+
static esp_err_t master_serial_init(void **pinst)
40+
{
41+
mb_communication_info_t comm;
42+
comm.ser_opts.port = (uart_port_t)TEST_PORT_NUM;
43+
comm.ser_opts.mode = (mb_comm_mode_t)MB_RTU;
44+
comm.ser_opts.baudrate = TEST_SPEED;
45+
comm.ser_opts.parity = MB_PARITY_NONE;
46+
comm.ser_opts.uid = 0;
47+
comm.ser_opts.response_tout_ms = 100;
48+
comm.ser_opts.data_bits = UART_DATA_8_BITS;
49+
comm.ser_opts.stop_bits = UART_STOP_BITS_1;
50+
// Initialize Modbus controller
51+
esp_err_t err = mbc_master_create_serial(&comm, pinst);
52+
MB_RETURN_ON_FALSE((pinst != NULL), ESP_ERR_INVALID_STATE, TAG,
53+
"mbc master initialization fail.");
54+
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
55+
"mbc master initialization fail, returns(0x%x).", (int)err);
56+
err = mbc_master_set_descriptor(*pinst, &dummy_dict[0], num_device_parameters);
57+
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
58+
"mbc master set descriptor fail, returns(0x%x).", (int)err);
59+
err = mbc_master_start(*pinst);
60+
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
61+
"mbc master start fail, returned (0x%x).", (int)err);
62+
const mb_parameter_descriptor_t *pdescriptor = NULL;
63+
err = mbc_master_get_cid_info(*pinst, CID_DEV_REG0, &pdescriptor);
64+
MB_RETURN_ON_FALSE(((err != ESP_ERR_NOT_FOUND) && (pdescriptor != NULL)), ESP_ERR_INVALID_STATE, TAG,
65+
"mbc master get descriptor fail, returned (0x%x).", (int)err);
66+
uint16_t regs[] = {0x1111, 0x2222};
67+
uint8_t type = 0;
68+
err = mbc_master_get_parameter(*pinst, pdescriptor->cid, (uint8_t *)&regs[0], &type);
69+
MB_RETURN_ON_FALSE((err != ESP_ERR_INVALID_STATE), ESP_ERR_INVALID_STATE, TAG,
70+
"mbc master get parameter fail, returned (0x%x).", (int)err);
71+
ESP_LOGI(TAG, "Modbus master stack initialized...");
72+
return ESP_OK;
73+
}
74+
75+
// Modbus serial slave initialization
76+
static esp_err_t slave_serial_init(void **pinst)
77+
{
78+
mb_register_area_descriptor_t reg_area;
79+
mb_communication_info_t comm;
80+
comm.ser_opts.port = (uart_port_t)TEST_PORT_NUM;
81+
comm.ser_opts.mode = (mb_comm_mode_t)MB_RTU;
82+
comm.ser_opts.baudrate = TEST_SPEED;
83+
comm.ser_opts.parity = MB_PARITY_NONE;
84+
comm.ser_opts.uid = MB_SLAVE_SHORT_ADDRESS;
85+
comm.ser_opts.response_tout_ms = 100;
86+
comm.ser_opts.data_bits = UART_DATA_8_BITS;
87+
comm.ser_opts.stop_bits = UART_STOP_BITS_1;
88+
// Initialize Modbus controller
89+
esp_err_t err = mbc_slave_create_serial(&comm, pinst);
90+
MB_RETURN_ON_FALSE((pinst != NULL), ESP_ERR_INVALID_STATE, TAG,
91+
"mbc slave initialization fail.");
92+
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
93+
"mbc slave initialization fail, returns(0x%x).", (int)err);
94+
uint16_t holding_regs[] = {0x1111, 0x2222, 0x3333, 0x4444};
95+
reg_area.type = MB_PARAM_HOLDING;
96+
reg_area.start_offset = 0;
97+
reg_area.address = (void*)&holding_regs[0];
98+
reg_area.size = sizeof(holding_regs);
99+
reg_area.access = MB_ACCESS_RW;
100+
ESP_ERROR_CHECK(mbc_slave_set_descriptor(*pinst, reg_area));
101+
err = mbc_slave_start(*pinst);
102+
MB_RETURN_ON_FALSE((err == ESP_OK), ESP_ERR_INVALID_STATE, TAG,
103+
"mbc slave start fail, returned (0x%x).", (int)err);
104+
ESP_LOGI(TAG, "Modbus slave stack initialized...");
105+
return err;
106+
}
107+
108+
// Intentionally verify that atomic values are layout compatible with original types
109+
// Note: the `__is_layout_compatible() and ` is not supported in old versions
110+
static_assert(
111+
sizeof(std::atomic_int) == sizeof(int) &&
112+
sizeof(std::atomic<int>) == sizeof(int) &&
113+
sizeof(_Atomic(int)) == sizeof(int) &&
114+
sizeof(_Atomic(uint32_t)) == sizeof(uint32_t) &&
115+
sizeof(_Atomic(uint16_t)) == sizeof(uint16_t) &&
116+
sizeof(_Atomic(uint64_t)) == sizeof(uint64_t)
117+
);
118+
119+
extern "C" void app_main(void)
120+
{
121+
// Initialization of device peripheral and objects
122+
ESP_LOGI(TAG, "Setup master cpp....");
123+
ESP_ERROR_CHECK(master_serial_init(&pmaster_handle));
124+
ESP_ERROR_CHECK(mbc_master_delete(pmaster_handle));
125+
ESP_LOGI(TAG, "Master test passed successfully.");
126+
ESP_LOGI(TAG, "Setup slave cpp....");
127+
ESP_ERROR_CHECK(slave_serial_init(&pslave_handle));
128+
ESP_ERROR_CHECK(mbc_slave_delete(pslave_handle));
129+
ESP_LOGI(TAG, "Slave test passed successfully.");
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
2+
# SPDX-License-Identifier: Apache-2.0
3+
import pytest
4+
from pytest_embedded import Dut
5+
6+
@pytest.mark.esp32
7+
@pytest.mark.generic
8+
def test_cpp_mb_serial_master_slave(dut: Dut) -> None:
9+
dut.expect('Setup master cpp....')
10+
dut.expect('Modbus master stack initialized...', timeout=5)
11+
dut.expect('Master test passed successfully.', timeout=5)
12+
dut.expect('Setup slave cpp....')
13+
dut.expect('Modbus slave stack initialized...', timeout=5)
14+
dut.expect('Slave test passed successfully.', timeout=5)
15+
dut.expect('Returned from app_main()')

test_apps/test_common/mb_utest_lib/port_adapter.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef struct _mb_adapter_port_entry
4646
uint16_t recv_length;
4747
uint64_t send_time_stamp;
4848
uint64_t recv_time_stamp;
49-
_Atomic uint64_t test_timeout_us;
49+
_Atomic(uint64_t) test_timeout_us;
5050
uint32_t flags;
5151
mb_uid_info_t addr_info;
5252
QueueHandle_t rx_queue;

0 commit comments

Comments
 (0)