From 722b759c2afcd29d30a29c90c6a0d1d4b3b81936 Mon Sep 17 00:00:00 2001 From: Haorui Zhou WFR VM Date: Wed, 18 Mar 2026 20:57:04 -0400 Subject: [PATCH] esp32s3: expose canbus machine property and wire TWAI peripheral MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ESP32-S3 TWAI device (esp32s3_twai.c, inheriting esp32_twai.c) already implements a 'canbus' link property and calls can_sja_connect_to_bus() in its realize handler — but the machine (esp32s3.c) never exposed a corresponding property or set the link, leaving the TWAI peripheral permanently CAN-isolated. This patch wires the two together: - Add CanBusState *canbus field to Esp32s3MachineState - Expose it as a machine property via object_class_property_add_link - Set the TWAI 'canbus' link in machine_init before qdev_realize Usage: -object can-bus,id=canbus0 -object can-host-socketcan,id=can-host0,if=vcan0,canbus=canbus0 -machine esp32s3,canbus=canbus0 Tested with ESP32-S3 firmware on vcan0 (Linux virtual SocketCAN). Firmware CAN TX and RX confirmed working via candump. --- hw/xtensa/esp32s3.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hw/xtensa/esp32s3.c b/hw/xtensa/esp32s3.c index 4a24a040fa142..fc6765c0d1ed9 100644 --- a/hw/xtensa/esp32s3.c +++ b/hw/xtensa/esp32s3.c @@ -64,6 +64,7 @@ #include "hw/misc/esp32s3_xts_aes.h" #include "hw/misc/esp32s3_pms.h" #include "hw/net/can/esp32s3_twai.h" +#include "net/can_emu.h" #include "cpu_esp32s3.h" @@ -318,6 +319,7 @@ struct Esp32s3MachineState { Esp32s3SocState esp32s3; DeviceState *flash_dev; + CanBusState *canbus; }; #define TYPE_ESP32S3_MACHINE MACHINE_TYPE_NAME("esp32s3") @@ -633,6 +635,11 @@ static void esp32s3_machine_init(MachineState *machine) // qdev_prop_set_chr(DEVICE(ss), "serial1", serial_hd(1)); // qdev_prop_set_chr(DEVICE(ss), "serial2", serial_hd(2)); + if (ms->canbus) { + object_property_set_link(OBJECT(&ss->twai), "canbus", + OBJECT(ms->canbus), &error_abort); + } + qdev_realize(DEVICE(ss), NULL, &error_fatal); object_initialize_child(OBJECT(ss), "extmem", &ss->cache, TYPE_ESP32S3_CACHE); @@ -984,6 +991,11 @@ static void esp32s3_machine_class_init(ObjectClass *oc, void *data) mc->default_cpus = 2; mc->default_ram_size = 0; mc->fixup_ram_size = esp32s3_fixup_ram_size; + + object_class_property_add_link(oc, "canbus", TYPE_CAN_BUS, + offsetof(Esp32s3MachineState, canbus), + object_property_allow_set_link, + OBJ_PROP_LINK_STRONG); } static const TypeInfo esp32s3_info = {