Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aea1c80

Browse filesBrowse files
designer2k2Dazza0
authored andcommitted
TWAI: Add receive example
This commit addes an example showing how to receive messages using the TWAI driver interface and a CAN transceiver. Specifically, the example demonstrates: - How to configure and install the TWAI drvier - How to poll for TWAI events (i.e., alerts) using twai_read_alerts() - How to handle the various events (such as TWAI_ALERT_RX_DATA) Closes espressif#7430
1 parent 3968aa9 commit aea1c80
Copy full SHA for aea1c80

File tree

Expand file treeCollapse file tree

1 file changed

+117
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+117
-0
lines changed
+117Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* ESP32 TWAI receive example.
2+
Receive messages and sends them over serial.
3+
4+
Connect a CAN bus transceiver to the RX/TX pins.
5+
For example: SN65HVD230
6+
7+
TWAI_MODE_LISTEN_ONLY is used so that the TWAI controller will not influence the bus.
8+
9+
The API gives other possible speeds and alerts:
10+
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/twai.html
11+
12+
Example output from a can bus message:
13+
-> Message received
14+
-> Message is in Standard Format
15+
-> ID: 604
16+
-> Byte: 0 = 00, 1 = 0f, 2 = 13, 3 = 02, 4 = 00, 5 = 00, 6 = 08, 7 = 00
17+
18+
Example output with alerts:
19+
-> Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.
20+
-> Bus error count: 171
21+
-> Alert: The RX queue is full causing a received frame to be lost.
22+
-> RX buffered: 4 RX missed: 46 RX overrun 0
23+
24+
created 05-11-2022 by Stephan Martin (designer2k2)
25+
*/
26+
27+
#include "driver/twai.h"
28+
29+
// Pins used to connect to CAN bus transceiver:
30+
#define RX_PIN 21
31+
#define TX_PIN 22
32+
33+
// Intervall:
34+
#define POLLING_RATE_MS 1000
35+
36+
void setup() {
37+
// Start Serial:
38+
Serial.begin(115200);
39+
40+
// Initialize configuration structures using macro initializers
41+
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_LISTEN_ONLY);
42+
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS(); //Look in the api-reference for other speed sets.
43+
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();
44+
45+
// Install TWAI driver
46+
if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) {
47+
Serial.println("Driver installed");
48+
} else {
49+
Serial.println("Failed to install driver");
50+
return;
51+
}
52+
53+
// Start TWAI driver
54+
if (twai_start() == ESP_OK) {
55+
Serial.println("Driver started");
56+
} else {
57+
Serial.println("Failed to start driver");
58+
return;
59+
}
60+
61+
// Reconfigure alerts to detect frame receive, Bus-Off error and RX queue full states
62+
uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_RX_QUEUE_FULL;
63+
if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK) {
64+
Serial.println("CAN Alerts reconfigured");
65+
} else {
66+
Serial.println("Failed to reconfigure alerts");
67+
}
68+
69+
}
70+
71+
static void handle_rx_message(twai_message_t& message) {
72+
// Process received message
73+
if (message.extd) {
74+
Serial.println("Message is in Extended Format");
75+
} else {
76+
Serial.println("Message is in Standard Format");
77+
}
78+
Serial.printf("ID: %x\nByte:", message.identifier);
79+
if (!(message.rtr)) {
80+
for (int i = 0; i < message.data_length_code; i++) {
81+
Serial.printf(" %d = %02x,", i, message.data[i]);
82+
}
83+
Serial.println("");
84+
}
85+
}
86+
87+
void loop() {
88+
// Check if alert happened
89+
uint32_t alerts_triggered;
90+
twai_read_alerts(&alerts_triggered, pdMS_TO_TICKS(POLLING_RATE_MS));
91+
twai_status_info_t twaistatus;
92+
twai_get_status_info(&twaistatus);
93+
94+
// Handle alerts
95+
if (alerts_triggered & TWAI_ALERT_ERR_PASS) {
96+
Serial.println("Alert: TWAI controller has become error passive.");
97+
}
98+
if (alerts_triggered & TWAI_ALERT_BUS_ERROR) {
99+
Serial.println("Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus.");
100+
Serial.printf("Bus error count: %d\n", twaistatus.bus_error_count);
101+
}
102+
if (alerts_triggered & TWAI_ALERT_RX_QUEUE_FULL) {
103+
Serial.println("Alert: The RX queue is full causing a received frame to be lost.");
104+
Serial.printf("RX buffered: %d\t", twaistatus.msgs_to_rx);
105+
Serial.printf("RX missed: %d\t", twaistatus.rx_missed_count);
106+
Serial.printf("RX overrun %d\n", twaistatus.rx_overrun_count);
107+
}
108+
109+
// Check if message is received
110+
if (alerts_triggered & TWAI_ALERT_RX_DATA) {
111+
// One or more messages received. Handle all.
112+
twai_message_t message;
113+
while (twai_receive(&message, 0) == ESP_OK) {
114+
handle_rx_message(message);
115+
}
116+
}
117+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.