Hi,
I am new here and have been trying to make the demo example of TWAIreceive.ino which is the demo of the WAVESHARE ESP32S3 LCD With touch 7" ( This board).
The code below work perfectly on Arduino IDE but after import to PIO it seems to work only on TWAI_MODE_LISTENONLY but does not work on NORMAL MODE.
I attached here the code that I use
07_TWAIreceive.ino
#include "waveshare_twai_port.h"
// Function to handle received messages
static void handle_rx_message(twai_message_t &message)
{
// Process received message
if (message.extd)
{
Serial.println("Message is in Extended Format"); // Print if the message is in extended format
}
else
{
Serial.println("Message is in Standard Format"); // Print if the message is in standard format
}
Serial.printf("ID: %x\nByte:", message.identifier); // Print message ID
if (!(message.rtr))
{ // Check if it is not a remote transmission request
for (int i = 0; i < message.data_length_code; i++)
{
Serial.printf(" %d = %02x,", i, message.data[i]); // Print each byte of the message data
}
Serial.println(""); // Print a new line
}
}
// Function to initialize the TWAI driver
bool waveshare_twai_init()
{
// Initialize configuration structures using macro initializers
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS(); // set 500Kbps
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); // Accept all messages
// Install TWAI driver
if (twai_driver_install(&g_config, &t_config, &f_config) != ESP_OK)
{
Serial.println("Failed to install driver"); // Print error message
return false; // Return false if driver installation fails
}
Serial.println("Driver installed"); // Print success message
// Start TWAI driver
if (twai_start() != ESP_OK)
{
Serial.println("Failed to start driver"); // Print error message
return false; // Return false if starting the driver fails
}
Serial.println("Driver started"); // Print success message
// Reconfigure alerts to detect frame receive, Bus-Off error, and RX queue full states
uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_RX_QUEUE_FULL;
if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK)
{
Serial.println("CAN Alerts reconfigured"); // Print success message
}
else
{
Serial.println("Failed to reconfigure alerts"); // Print error message
return false; // Return false if alert reconfiguration fails
}
// TWAI driver is now successfully installed and started
return true; // Return true on success
}
// Function to receive messages via TWAI
void waveshare_twai_receive()
{
// Check if alert happened
uint32_t alerts_triggered;
twai_read_alerts(&alerts_triggered, pdMS_TO_TICKS(POLLING_RATE_MS)); // Read triggered alerts
twai_status_info_t twaistatus; // Create status info structure
twai_get_status_info(&twaistatus); // Get status information
// Handle alerts
if (alerts_triggered & TWAI_ALERT_ERR_PASS)
{
Serial.println("Alert: TWAI controller has become error passive."); // Print passive error alert
}
if (alerts_triggered & TWAI_ALERT_BUS_ERROR)
{
Serial.println("Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus."); // Print bus error alert
Serial.printf("Bus error count: %d\n", twaistatus.bus_error_count); // Print bus error count
}
if (alerts_triggered & TWAI_ALERT_RX_QUEUE_FULL)
{
Serial.println("Alert: The RX queue is full causing a received frame to be lost."); // Print RX queue full alert
Serial.printf("RX buffered: %d\t", twaistatus.msgs_to_rx); // Print buffered RX messages
Serial.printf("RX missed: %d\t", twaistatus.rx_missed_count); // Print missed RX count
Serial.printf("RX overrun %d\n", twaistatus.rx_overrun_count); // Print RX overrun count
}
// Check if message is received
if (alerts_triggered & TWAI_ALERT_RX_DATA)
{
// One or more messages received. Handle all.
twai_message_t message;
while (twai_receive(&message, 0) == ESP_OK)
{ // Receive messages
handle_rx_message(message); // Handle each received message
}
}
}
#include "waveshare_twai_port.h"
// Function to handle received messages
static void handle_rx_message(twai_message_t &message)
{
// Process received message
if (message.extd)
{
Serial.println("Message is in Extended Format"); // Print if the message is in extended format
}
else
{
Serial.println("Message is in Standard Format"); // Print if the message is in standard format
}
Serial.printf("ID: %x\nByte:", message.identifier); // Print message ID
if (!(message.rtr))
{ // Check if it is not a remote transmission request
for (int i = 0; i < message.data_length_code; i++)
{
Serial.printf(" %d = %02x,", i, message.data[i]); // Print each byte of the message data
}
Serial.println(""); // Print a new line
}
}
// Function to initialize the TWAI driver
bool waveshare_twai_init()
{
// Initialize configuration structures using macro initializers
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT((gpio_num_t)TX_PIN, (gpio_num_t)RX_PIN, TWAI_MODE_NORMAL);
twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS(); // set 500Kbps
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); // Accept all messages
// Install TWAI driver
if (twai_driver_install(&g_config, &t_config, &f_config) != ESP_OK)
{
Serial.println("Failed to install driver"); // Print error message
return false; // Return false if driver installation fails
}
Serial.println("Driver installed"); // Print success message
// Start TWAI driver
if (twai_start() != ESP_OK)
{
Serial.println("Failed to start driver"); // Print error message
return false; // Return false if starting the driver fails
}
Serial.println("Driver started"); // Print success message
// Reconfigure alerts to detect frame receive, Bus-Off error, and RX queue full states
uint32_t alerts_to_enable = TWAI_ALERT_RX_DATA | TWAI_ALERT_ERR_PASS | TWAI_ALERT_BUS_ERROR | TWAI_ALERT_RX_QUEUE_FULL;
if (twai_reconfigure_alerts(alerts_to_enable, NULL) == ESP_OK)
{
Serial.println("CAN Alerts reconfigured"); // Print success message
}
else
{
Serial.println("Failed to reconfigure alerts"); // Print error message
return false; // Return false if alert reconfiguration fails
}
// TWAI driver is now successfully installed and started
return true; // Return true on success
}
// Function to receive messages via TWAI
void waveshare_twai_receive()
{
// Check if alert happened
uint32_t alerts_triggered;
twai_read_alerts(&alerts_triggered, pdMS_TO_TICKS(POLLING_RATE_MS)); // Read triggered alerts
twai_status_info_t twaistatus; // Create status info structure
twai_get_status_info(&twaistatus); // Get status information
// Handle alerts
if (alerts_triggered & TWAI_ALERT_ERR_PASS)
{
Serial.println("Alert: TWAI controller has become error passive."); // Print passive error alert
}
if (alerts_triggered & TWAI_ALERT_BUS_ERROR)
{
Serial.println("Alert: A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus."); // Print bus error alert
Serial.printf("Bus error count: %d\n", twaistatus.bus_error_count); // Print bus error count
}
if (alerts_triggered & TWAI_ALERT_RX_QUEUE_FULL)
{
Serial.println("Alert: The RX queue is full causing a received frame to be lost."); // Print RX queue full alert
Serial.printf("RX buffered: %d\t", twaistatus.msgs_to_rx); // Print buffered RX messages
Serial.printf("RX missed: %d\t", twaistatus.rx_missed_count); // Print missed RX count
Serial.printf("RX overrun %d\n", twaistatus.rx_overrun_count); // Print RX overrun count
}
// Check if message is received
if (alerts_triggered & TWAI_ALERT_RX_DATA)
{
// One or more messages received. Handle all.
twai_message_t message;
while (twai_receive(&message, 0) == ESP_OK)
{ // Receive messages
handle_rx_message(message); // Handle each received message
}
}
}
waveshare_twai_port.h
#ifndef __TWAI_PORT_H
#define __TWAI_PORT_H
#pragma once
#include <Arduino.h>
#include "driver/twai.h"
#include <esp_io_expander.hpp>
// Pins used to connect to CAN bus transceiver:
#define RX_PIN 19
#define TX_PIN 20
// Extend IO Pin define
#define TP_RST 1
#define LCD_BL 2
#define LCD_RST 3
#define SD_CS 4
#define USB_SEL 5
// I2C Pin define
#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_CH422G_ADDRESS)
#define EXAMPLE_I2C_SDA_PIN 8 // I2C data line pins
#define EXAMPLE_I2C_SCL_PIN 9 // I2C clock line pin
// Intervall:
#define POLLING_RATE_MS 1000
bool waveshare_twai_init();
void waveshare_twai_receive();
#endif
And here is my platformio.ini
[env:esp32-s3-devkitc-1]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
board_build.arduino.memory_type = qio_opi
board_build.flash_mode = qio
board_build.psram_type = opi
board_upload.flash_size = 16MB
board_upload.maximum_size = 16777216
board_build.partitions = default_16MB.csv
board_build.extra_flags =
-DBOARD_HAS_PSRAM
-DARDUINO_USB_MODE=1
-DARDUINO_USB_CDC_ON_BOOT=1
build_unflags = -std=gnu++11
build_flags = -std=gnu++17
monitor_speed = 115200
;lib_extra_dirs = ~/Documents/Arduino/libraries
lib_extra_dirs = ~/Desktop/GPN/GPN/DASHMSTR/LCD 7/ESP32-S3-Touch-LCD-7-Demo/Arduino/libraries
Anyone that used to encounter the problem, please guide me though this issue.