|
| 1 | +/* ESP32 TWAI transmit example. |
| 2 | + This transmits a message every second. |
| 3 | +
|
| 4 | + Connect a CAN bus transceiver to the RX/TX pins. |
| 5 | + For example: SN65HVD230 |
| 6 | +
|
| 7 | + The API gives other possible speeds and alerts: |
| 8 | + https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/twai.html |
| 9 | +
|
| 10 | + created 27-06-2023 by Stephan Martin (designer2k2) |
| 11 | +*/ |
| 12 | + |
| 13 | +#include "driver/twai.h" |
| 14 | + |
| 15 | +// Pins used to connect to CAN bus transceiver: |
| 16 | +#define RX_PIN 21 |
| 17 | +#define TX_PIN 22 |
| 18 | + |
| 19 | +// Intervall: |
| 20 | +#define TRANSMIT_RATE_MS 1000 |
| 21 | + |
| 22 | +#define POLLING_RATE_MS 1000 |
| 23 | + |
| 24 | +static bool driver_installed = false; |
| 25 | + |
| 26 | +unsigned long previousMillis = 0; // will store last time a message was send |
| 27 | + |
| 28 | + |
| 29 | +void setup() { |
| 30 | + // Start Serial: |
| 31 | + Serial.begin(115200); |
| 32 | + |
| 33 | + // Initialize configuration structures using macro initializers |
| 34 | + twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL); |
| 35 | + twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS(); //Look in the api-reference for other speed sets. |
| 36 | + twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); |
| 37 | + |
| 38 | + // Install TWAI driver |
| 39 | + if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) { |
| 40 | + Serial.println("Driver installed"); |
| 41 | + } else { |
| 42 | + Serial.println("Failed to install driver"); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // Start TWAI driver |
| 47 | + if (twai_start() == ESP_OK) { |
| 48 | + Serial.println("Driver started"); |
| 49 | + } else { |
| 50 | + Serial.println("Failed to start driver"); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Reconfigure alerts to detect TX alerts and Bus-Off errors |
| 55 | + uint32_t alerts_to_enable = TWAI_ALERT_TX_IDLE | TWAI_ALERT_TX_SUCCESS | TWAI_ALERT_TX_FAILED | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR; |
| 56 | + if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK) { |
| 57 | + Serial.println("CAN Alerts reconfigured"); |
| 58 | + } else { |
| 59 | + Serial.println("Failed to reconfigure alerts"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // TWAI driver is now successfully installed and started |
| 64 | + driver_installed = true; |
| 65 | +} |
| 66 | + |
| 67 | +static void send_message() { |
| 68 | + // Send message |
| 69 | + |
| 70 | + // Configure message to transmit |
| 71 | + twai_message_t message; |
| 72 | + message.identifier = 0x0F6; |
| 73 | + message.data_length_code = 4; |
| 74 | + for (int i = 0; i < 4; i++) { |
| 75 | + message.data[i] = 0; |
| 76 | + } |
| 77 | + |
| 78 | + // Queue message for transmission |
| 79 | + if (twai_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) { |
| 80 | + printf("Message queued for transmission\n"); |
| 81 | + } else { |
| 82 | + printf("Failed to queue message for transmission\n"); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void loop() { |
| 87 | + if (!driver_installed) { |
| 88 | + // Driver not installed |
| 89 | + delay(1000); |
| 90 | + return; |
| 91 | + } |
| 92 | + // Check if alert happened |
| 93 | + uint32_t alerts_triggered; |
| 94 | + twai_read_alerts(&alerts_triggered, pdMS_TO_TICKS(POLLING_RATE_MS)); |
| 95 | + twai_status_info_t twaistatus; |
| 96 | + twai_get_status_info(&twaistatus); |
| 97 | + |
| 98 | + // Handle alerts |
| 99 | + if (alerts_triggered & TWAI_ALERT_ERR_PASS) { |
| 100 | + Serial.println("Alert: TWAI controller has become error passive."); |
| 101 | + } |
| 102 | + if (alerts_triggered & TWAI_ALERT_BUS_ERROR) { |
| 103 | + Serial.println("Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus."); |
| 104 | + Serial.printf("Bus error count: %d\n", twaistatus.bus_error_count); |
| 105 | + } |
| 106 | + if (alerts_triggered & TWAI_ALERT_TX_FAILED) { |
| 107 | + Serial.println("Alert: The Transmission failed."); |
| 108 | + Serial.printf("TX buffered: %d\t", twaistatus.msgs_to_tx); |
| 109 | + Serial.printf("TX error: %d\t", twaistatus.tx_error_counter); |
| 110 | + Serial.printf("TX failed: %d\n", twaistatus.tx_failed_count); |
| 111 | + } |
| 112 | + if (alerts_triggered & TWAI_ALERT_TX_SUCCESS) { |
| 113 | + Serial.println("Alert: The Transmission was successful."); |
| 114 | + Serial.printf("TX buffered: %d\t", twaistatus.msgs_to_tx); |
| 115 | + } |
| 116 | + |
| 117 | + // Send message |
| 118 | + unsigned long currentMillis = millis(); |
| 119 | + if (currentMillis - previousMillis >= TRANSMIT_RATE_MS) { |
| 120 | + previousMillis = currentMillis; |
| 121 | + send_message(); |
| 122 | + } |
| 123 | +} |
0 commit comments