From b18a7874698ac31ee5bcacc9db43a4e6e1c17274 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Tue, 27 Feb 2024 11:52:51 +0800 Subject: [PATCH] change(driver/twai): Update TWAI message object initialization examples This commit updates how examples and code snippets initialize the twai_message_t for transmission. Examples/snippets now explicitly demonstrate how to initialize every member/bit-field of the object. --- docs/en/api-reference/peripherals/twai.rst | 21 +++++--- .../twai_alert_and_recovery_example_main.c | 16 +++++- .../main/twai_network_example_master_main.c | 49 +++++++++++++---- .../main/twai_network_example_slave_main.c | 52 +++++++++++++++---- .../main/twai_self_test_example_main.c | 18 +++++-- 5 files changed, 122 insertions(+), 34 deletions(-) diff --git a/docs/en/api-reference/peripherals/twai.rst b/docs/en/api-reference/peripherals/twai.rst index 3a19a7f03a..d040c2f900 100644 --- a/docs/en/api-reference/peripherals/twai.rst +++ b/docs/en/api-reference/peripherals/twai.rst @@ -467,14 +467,19 @@ The following code snippet demonstrates how to transmit a message via the usage ... - //Configure message to transmit - twai_message_t message; - message.identifier = 0xAAAA; - message.extd = 1; - message.data_length_code = 4; - for (int i = 0; i < 4; i++) { - message.data[i] = 0; - } + // Configure message to transmit + twai_message_t message = { + // Message type and format settings + .extd = 1, // Standard vs extended format + .rtr = 0, // Data vs RTR frame + .ss = 0, // Whether the message is single shot (i.e., does not repeat on error) + .self = 0, // Whether the message is a self reception request (loopback) + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = 0xAAAA, + .data_length_code = 4, + .data = {0, 1, 2, 3}, + }; //Queue message for transmission if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) { diff --git a/examples/peripherals/twai/twai_alert_and_recovery/main/twai_alert_and_recovery_example_main.c b/examples/peripherals/twai/twai_alert_and_recovery/main/twai_alert_and_recovery_example_main.c index ac138733cf..7812d10448 100644 --- a/examples/peripherals/twai/twai_alert_and_recovery/main/twai_alert_and_recovery_example_main.c +++ b/examples/peripherals/twai/twai_alert_and_recovery/main/twai_alert_and_recovery_example_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ @@ -41,7 +41,19 @@ static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS(); static const twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NO_ACK); -static const twai_message_t tx_msg = {.identifier = 0, .data_length_code = 0}; + +static const twai_message_t tx_msg = { + // Message type and format settings + .extd = 0, // Standard Format message (11-bit ID) + .rtr = 0, // Send a data frame + .ss = 0, // Not single shot + .self = 0, // Not a self reception request + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = 0, + .data_length_code = 0, + .data = {0}, +}; static SemaphoreHandle_t tx_task_sem; static SemaphoreHandle_t ctrl_task_sem; diff --git a/examples/peripherals/twai/twai_network/twai_network_master/main/twai_network_example_master_main.c b/examples/peripherals/twai/twai_network/twai_network_master/main/twai_network_example_master_main.c index 820bcfb770..cc56a0b2cd 100644 --- a/examples/peripherals/twai/twai_network/twai_network_master/main/twai_network_example_master_main.c +++ b/examples/peripherals/twai/twai_network/twai_network_master/main/twai_network_example_master_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ @@ -63,15 +63,44 @@ static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS(); static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); static const twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NORMAL); -static const twai_message_t ping_message = {.identifier = ID_MASTER_PING, .data_length_code = 0, - .ss = 1, .data = {0, 0, 0, 0, 0, 0, 0, 0} - }; -static const twai_message_t start_message = {.identifier = ID_MASTER_START_CMD, .data_length_code = 0, - .data = {0, 0, 0, 0, 0, 0, 0, 0} - }; -static const twai_message_t stop_message = {.identifier = ID_MASTER_STOP_CMD, .data_length_code = 0, - .data = {0, 0, 0, 0, 0, 0, 0, 0} - }; +static const twai_message_t ping_message = { + // Message type and format settings + .extd = 0, // Standard Format message (11-bit ID) + .rtr = 0, // Send a data frame + .ss = 1, // Is single shot (won't retry on error or NACK) + .self = 0, // Not a self reception request + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = ID_MASTER_PING, + .data_length_code = 0, + .data = {0}, +}; + +static const twai_message_t start_message = { + // Message type and format settings + .extd = 0, // Standard Format message (11-bit ID) + .rtr = 0, // Send a data frame + .ss = 0, // Not single shot + .self = 0, // Not a self reception request + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = ID_MASTER_START_CMD, + .data_length_code = 0, + .data = {0}, +}; + +static const twai_message_t stop_message = { + // Message type and format settings + .extd = 0, // Standard Format message (11-bit ID) + .rtr = 0, // Send a data frame + .ss = 0, // Not single shot + .self = 0, // Not a self reception request + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = ID_MASTER_STOP_CMD, + .data_length_code = 0, + .data = {0}, +}; static QueueHandle_t tx_task_queue; static QueueHandle_t rx_task_queue; diff --git a/examples/peripherals/twai/twai_network/twai_network_slave/main/twai_network_example_slave_main.c b/examples/peripherals/twai/twai_network/twai_network_slave/main/twai_network_example_slave_main.c index 73b79e3407..63bcece707 100644 --- a/examples/peripherals/twai/twai_network/twai_network_slave/main/twai_network_example_slave_main.c +++ b/examples/peripherals/twai/twai_network/twai_network_slave/main/twai_network_example_slave_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ @@ -63,16 +63,46 @@ typedef enum { static const twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TX_GPIO_NUM, RX_GPIO_NUM, TWAI_MODE_NORMAL); static const twai_timing_config_t t_config = TWAI_TIMING_CONFIG_25KBITS(); static const twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); -static const twai_message_t ping_resp = {.identifier = ID_SLAVE_PING_RESP, .data_length_code = 0, - .data = {0, 0, 0, 0, 0, 0, 0, 0} - }; -static const twai_message_t stop_resp = {.identifier = ID_SLAVE_STOP_RESP, .data_length_code = 0, - .data = {0, 0, 0, 0, 0, 0, 0, 0} - }; -//Data bytes of data message will be initialized in the transmit task -static twai_message_t data_message = {.identifier = ID_SLAVE_DATA, .data_length_code = 4, - .data = {0, 0, 0, 0, 0, 0, 0, 0} - }; + +static const twai_message_t ping_resp = { + // Message type and format settings + .extd = 0, // Standard Format message (11-bit ID) + .rtr = 0, // Send a data frame + .ss = 0, // Not single shot + .self = 0, // Not a self reception request + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = ID_SLAVE_PING_RESP, + .data_length_code = 0, + .data = {0}, +}; + +static const twai_message_t stop_resp = { + // Message type and format settings + .extd = 0, // Standard Format message (11-bit ID) + .rtr = 0, // Send a data frame + .ss = 0, // Not single shot + .self = 0, // Not a self reception request + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = ID_SLAVE_STOP_RESP, + .data_length_code = 0, + .data = {0}, +}; + +// Data bytes of data message will be initialized in the transmit task +static twai_message_t data_message = { + // Message type and format settings + .extd = 0, // Standard Format message (11-bit ID) + .rtr = 0, // Send a data frame + .ss = 0, // Not single shot + .self = 0, // Not a self reception request + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = ID_SLAVE_DATA, + .data_length_code = 4, + .data = {1, 2, 3, 4}, +}; static QueueHandle_t tx_task_queue; static QueueHandle_t rx_task_queue; diff --git a/examples/peripherals/twai/twai_self_test/main/twai_self_test_example_main.c b/examples/peripherals/twai/twai_self_test/main/twai_self_test_example_main.c index d7f313d339..7cf7c6975e 100644 --- a/examples/peripherals/twai/twai_self_test/main/twai_self_test_example_main.c +++ b/examples/peripherals/twai/twai_self_test/main/twai_self_test_example_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ @@ -55,7 +55,19 @@ static SemaphoreHandle_t done_sem; static void twai_transmit_task(void *arg) { - twai_message_t tx_msg = {.data_length_code = 1, .identifier = MSG_ID, .self = 1}; + twai_message_t tx_msg = { + // Message type and format settings + .extd = 0, // Standard Format message (11-bit ID) + .rtr = 0, // Send a data frame + .ss = 0, // Not single shot + .self = 1, // Message is a self reception request (loopback) + .dlc_non_comp = 0, // DLC is less than 8 + // Message ID and payload + .identifier = MSG_ID, + .data_length_code = 1, + .data = {0}, + }; + for (int iter = 0; iter < NO_OF_ITERS; iter++) { xSemaphoreTake(tx_sem, portMAX_DELAY); for (int i = 0; i < NO_OF_MSGS; i++) { @@ -77,7 +89,7 @@ static void twai_receive_task(void *arg) for (int i = 0; i < NO_OF_MSGS; i++) { //Receive message and print message data ESP_ERROR_CHECK(twai_receive(&rx_message, portMAX_DELAY)); - ESP_LOGI(EXAMPLE_TAG, "Msg received - Data = %d", rx_message.data[0]); + ESP_LOGI(EXAMPLE_TAG, "Msg received\tID 0x%lx\tData = %d", rx_message.identifier, rx_message.data[0]); } //Indicate to control task all messages received for this iteration xSemaphoreGive(ctrl_sem);