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.
This commit is contained in:
Darian Leung 2024-02-27 11:52:51 +08:00
parent 603bc0536c
commit b18a787469
No known key found for this signature in database
GPG Key ID: 8AC9127B487AA4EF
5 changed files with 122 additions and 34 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);