esp-idf/examples/peripherals/twai/twai_self_test
..
main
CMakeLists.txt
Makefile
README.md
example_test.py

README.md

TWAI Self Test Example

(See the README.md file in the upper level 'examples' directory for more information about examples.)

The TWAI Self Test Example demonstrates how a node can transmit TWAI messages to itself using the TWAI driver's "No Acknowledgement" mode and Self Reception Requests. The Self Test Example can be run as a simple test to determine whether a target (ESP32, ESP32-S2, ESP32-S3 or ESP32-C3) is properly connected to a working external transceiver.

How to use example

Hardware Required

This example requires only a single target (e.g., an ESP32 or ESP32-S2). The target must be connected to an external transceiver (e.g., a SN65HVD23X transceiver). This connection usually consists of a TX and an RX signal.

Note: If you don't have an external transceiver, this example can still be run by simply connecting the TX GPIO and RX GPIO with a jumper.

Configure the project

  • Set the target of the build (where {IDF_TARGET} stands for the target chip such as eszp32 or esp32s2).
  • Then run menuconfig to configure the example.
idf.py set-target {IDF_TARGET}
idf.py menuconfig
  • Under Example Configuration, configure the pin assignments using the options TX GPIO Number and RX GPIO Number according to how the target was connected to the transceiver. By default, TX GPIO Number and RX GPIO Number are set to the following values:
    • On the ESP32, TX GPIO Number and RX GPIO Number default to 21 and 22 respectively
    • On the ESP32-S2, TX GPIO Number and RX GPIO Number default to 20 and 21 respectively
    • On the ESP32-S3, TX GPIO Number and RX GPIO Number default to 4 and 5 respectively
    • On the ESP32-C3, TX GPIO Number and RX GPIO Number default to 2 and 3 respectively

Build and Flash

Build the project and flash it to the board, then run monitor tool to view serial output:

idf.py -p PORT flash monitor

(Replace PORT with the name of the serial port to use.)

(To exit the serial monitor, type Ctrl-].)

See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.

Example Output

I (345) TWAI Self Test: Driver installed
I (345) TWAI Self Test: Driver started
I (355) TWAI Self Test: Msg received - Data = 0
...
I (1335) TWAI Self Test: Msg received - Data = 99
I (1335) TWAI Self Test: Driver stopped
I (1435) TWAI Self Test: Driver started
I (1435) TWAI Self Test: Msg received - Data = 0
...
I (2425) TWAI Self Test: Msg received - Data = 99
I (2425) TWAI Self Test: Driver stopped
I (2525) TWAI Self Test: Driver started
I (2525) TWAI Self Test: Msg received - Data = 0
...
I (3515) TWAI Self Test: Msg received - Data = 99
I (3515) TWAI Self Test: Driver stopped
I (3615) TWAI Self Test: Driver uninstalled

Troubleshooting

I (345) TWAI Self Test: Driver installed
I (345) TWAI Self Test: Driver started

If the TWAI driver is installed and started but no messages are received, check that the target is correctly connected to the external transceiver, and that the external transceiver is operating properly (i.e., properly powered and not in sleep mode).

Example Breakdown

The TWAI Self Test Example will do multiple iterations of the following steps:

  1. Install the TWAI driver
  2. Start the TWAI driver
  3. Simultaneously transmit and receive multiple messages using the self reception request.
  4. Stop the TWAI driver
  5. Repeat steps 2 to 4 for multiple iterations
  6. Uninstall the TWAI driver