mirror of
https://github.com/espressif/esp-idf.git
synced 2024-09-20 00:36:01 -04:00
Merge branch 'change/twai_examples_message_obj_init' into 'master'
change(driver/twai): Update TWAI message object initialization examples Closes DOC-7309 See merge request espressif/esp-idf!29278
This commit is contained in:
commit
6f3ebe5a68
@ -467,14 +467,19 @@ The following code snippet demonstrates how to transmit a message via the usage
|
|||||||
|
|
||||||
...
|
...
|
||||||
|
|
||||||
//Configure message to transmit
|
// Configure message to transmit
|
||||||
twai_message_t message;
|
twai_message_t message = {
|
||||||
message.identifier = 0xAAAA;
|
// Message type and format settings
|
||||||
message.extd = 1;
|
.extd = 1, // Standard vs extended format
|
||||||
message.data_length_code = 4;
|
.rtr = 0, // Data vs RTR frame
|
||||||
for (int i = 0; i < 4; i++) {
|
.ss = 0, // Whether the message is single shot (i.e., does not repeat on error)
|
||||||
message.data[i] = 0;
|
.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
|
//Queue message for transmission
|
||||||
if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
|
if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
|
||||||
|
@ -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
|
* 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_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_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_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 tx_task_sem;
|
||||||
static SemaphoreHandle_t ctrl_task_sem;
|
static SemaphoreHandle_t ctrl_task_sem;
|
||||||
|
@ -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
|
* 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_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_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,
|
static const twai_message_t ping_message = {
|
||||||
.ss = 1, .data = {0, 0, 0, 0, 0, 0, 0, 0}
|
// Message type and format settings
|
||||||
};
|
.extd = 0, // Standard Format message (11-bit ID)
|
||||||
static const twai_message_t start_message = {.identifier = ID_MASTER_START_CMD, .data_length_code = 0,
|
.rtr = 0, // Send a data frame
|
||||||
.data = {0, 0, 0, 0, 0, 0, 0, 0}
|
.ss = 1, // Is single shot (won't retry on error or NACK)
|
||||||
};
|
.self = 0, // Not a self reception request
|
||||||
static const twai_message_t stop_message = {.identifier = ID_MASTER_STOP_CMD, .data_length_code = 0,
|
.dlc_non_comp = 0, // DLC is less than 8
|
||||||
.data = {0, 0, 0, 0, 0, 0, 0, 0}
|
// 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 tx_task_queue;
|
||||||
static QueueHandle_t rx_task_queue;
|
static QueueHandle_t rx_task_queue;
|
||||||
|
@ -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
|
* 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_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_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_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 ping_resp = {
|
||||||
};
|
// Message type and format settings
|
||||||
static const twai_message_t stop_resp = {.identifier = ID_SLAVE_STOP_RESP, .data_length_code = 0,
|
.extd = 0, // Standard Format message (11-bit ID)
|
||||||
.data = {0, 0, 0, 0, 0, 0, 0, 0}
|
.rtr = 0, // Send a data frame
|
||||||
};
|
.ss = 0, // Not single shot
|
||||||
//Data bytes of data message will be initialized in the transmit task
|
.self = 0, // Not a self reception request
|
||||||
static twai_message_t data_message = {.identifier = ID_SLAVE_DATA, .data_length_code = 4,
|
.dlc_non_comp = 0, // DLC is less than 8
|
||||||
.data = {0, 0, 0, 0, 0, 0, 0, 0}
|
// 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 tx_task_queue;
|
||||||
static QueueHandle_t rx_task_queue;
|
static QueueHandle_t rx_task_queue;
|
||||||
|
@ -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
|
* SPDX-License-Identifier: CC0-1.0
|
||||||
*/
|
*/
|
||||||
@ -55,7 +55,19 @@ static SemaphoreHandle_t done_sem;
|
|||||||
|
|
||||||
static void twai_transmit_task(void *arg)
|
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++) {
|
for (int iter = 0; iter < NO_OF_ITERS; iter++) {
|
||||||
xSemaphoreTake(tx_sem, portMAX_DELAY);
|
xSemaphoreTake(tx_sem, portMAX_DELAY);
|
||||||
for (int i = 0; i < NO_OF_MSGS; i++) {
|
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++) {
|
for (int i = 0; i < NO_OF_MSGS; i++) {
|
||||||
//Receive message and print message data
|
//Receive message and print message data
|
||||||
ESP_ERROR_CHECK(twai_receive(&rx_message, portMAX_DELAY));
|
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
|
//Indicate to control task all messages received for this iteration
|
||||||
xSemaphoreGive(ctrl_sem);
|
xSemaphoreGive(ctrl_sem);
|
||||||
|
Loading…
Reference in New Issue
Block a user