From 830315cda0fd7a6531e47198c552888781b33b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Fri, 1 Jan 2021 16:16:58 +0100 Subject: [PATCH] Use new ThingSet macros for CAN and ISO-TP fixed addressing --- lib/thingset | 2 +- src/data_nodes.cpp | 4 ++-- src/ext/can.cpp | 19 ++++++++++--------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/thingset b/lib/thingset index 4b9434d..0c39567 160000 --- a/lib/thingset +++ b/lib/thingset @@ -1 +1 @@ -Subproject commit 4b9434d562b7402e999a67732d364d40d07c26a7 +Subproject commit 0c39567a0feca1ba8036d4bd74d601cb4f02a7ba diff --git a/src/data_nodes.cpp b/src/data_nodes.cpp index 9b9ed59..050a9fb 100644 --- a/src/data_nodes.cpp +++ b/src/data_nodes.cpp @@ -53,7 +53,7 @@ bool pub_serial_enable = false; #if CONFIG_THINGSET_CAN bool pub_can_enable = false; -uint16_t ts_can_node_id = CONFIG_THINGSET_CAN_DEFAULT_NODE_ID; +uint16_t can_node_addr = CONFIG_THINGSET_CAN_DEFAULT_NODE_ID; #endif /** @@ -201,7 +201,7 @@ static DataNode data_nodes[] = { #endif #if CONFIG_THINGSET_CAN - TS_NODE_UINT16(0x59, "CanNodeId", &ts_can_node_id, + TS_NODE_UINT16(0x59, "CanNodeId", &can_node_addr, ID_CONF, TS_ANY_R | TS_ANY_W, PUB_NVM), #endif diff --git a/src/ext/can.cpp b/src/ext/can.cpp index baa31a4..d838860 100644 --- a/src/ext/can.cpp +++ b/src/ext/can.cpp @@ -29,26 +29,27 @@ LOG_MODULE_REGISTER(ext_can, CONFIG_CAN_LOG_LEVEL); #endif extern ThingSet ts; -extern uint16_t ts_can_node_id; -uint16_t ts_can_host_id = 0x00; // temporarily hard-coded +extern uint16_t can_node_addr; const struct device *can_dev; #ifdef CONFIG_ISOTP -#define RX_THREAD_STACK_SIZE 512 +#define RX_THREAD_STACK_SIZE 1024 #define RX_THREAD_PRIORITY 2 const struct isotp_fc_opts fc_opts = {.bs = 8, .stmin = 0}; struct isotp_msg_id rx_addr = { .id_type = CAN_EXTENDED_IDENTIFIER, - .use_ext_addr = 0 // Normal ISO-TP addressing (using only CAN ID) + .use_ext_addr = 0, // Normal ISO-TP addressing (using only CAN ID) + .use_fixed_addr = 1, // enable SAE J1939 compatible addressing }; struct isotp_msg_id tx_addr = { .id_type = CAN_EXTENDED_IDENTIFIER, - .use_ext_addr = 0 // Normal ISO-TP addressing (using only CAN ID) + .use_ext_addr = 0, // Normal ISO-TP addressing (using only CAN ID) + .use_fixed_addr = 1, // enable SAE J1939 compatible addressing }; struct isotp_recv_ctx recv_ctx; @@ -68,8 +69,8 @@ void can_rx_thread() static uint8_t tx_buffer[500]; // CAN node ID retrieved from EEPROM --> reset necessary after change via ThingSet serial - rx_addr.ext_id = ts_can_node_id << 8 | ts_can_host_id; - tx_addr.ext_id = ts_can_host_id << 8 | ts_can_node_id; + rx_addr.ext_id = TS_CAN_BASE_REQRESP | TS_CAN_PRIO_REQRESP | TS_CAN_TARGET_SET(can_node_addr); + tx_addr.ext_id = TS_CAN_BASE_REQRESP | TS_CAN_PRIO_REQRESP | TS_CAN_SOURCE_SET(can_node_addr); while (1) { ret = isotp_bind(&recv_ctx, can_dev, &rx_addr, &tx_addr, &fc_opts, K_FOREVER); @@ -108,7 +109,7 @@ void can_rx_thread() if (resp_len > 0) { static struct isotp_send_ctx send_ctx; int ret = isotp_send(&send_ctx, can_dev, tx_buffer, resp_len, - &tx_addr, &rx_addr, send_complette_cb, NULL); + &recv_ctx.tx_addr, &recv_ctx.rx_addr, send_complette_cb, NULL); if (ret != ISOTP_N_OK) { LOG_DBG("Error while sending data to ID %d [%d]", tx_addr.ext_id, ret); } @@ -149,7 +150,7 @@ void can_pub_thread() if (pub_can_enable) { int data_len = 0; int start_pos = 0; - while ((data_len = ts.bin_pub_can(start_pos, PUB_CAN, ts_can_node_id, can_id, can_data)) + while ((data_len = ts.bin_pub_can(start_pos, PUB_CAN, can_node_addr, can_id, can_data)) != -1) { struct zcan_frame frame = {0};