Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions device/prj.conf.overlays/c2usb.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CONFIG_C2USB_UDC_MAC_LOG_LEVEL_DBG=y

CONFIG_WARN_EXPERIMENTAL=n

CONFIG_C2USB=y
CONFIG_C2USB_HID_BOOT_PROTOCOL=y
CONFIG_C2USB_UDC_MAC=y

Expand Down
66 changes: 33 additions & 33 deletions patches/c2usb/0001-Add-diagnostics-logs.patch
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
From 85e5b32a49ac2d2b897ee84a276b2faf6dba33eb Mon Sep 17 00:00:00 2001
From b4de2fb0e2aec895efa6656d37f3b1b89dd60d8b Mon Sep 17 00:00:00 2001
From: Karel Tucek <kareltucek@centrum.cz>
Date: Sat, 11 Jul 2026 13:05:57 +0200
Subject: [PATCH] Add diagnostics logs.
Expand All @@ -15,19 +15,19 @@ Subject: [PATCH] Add diagnostics logs.
create mode 100644 c2usb/usb/df/mac_diag.hpp

diff --git a/c2usb/CMakeLists.txt b/c2usb/CMakeLists.txt
index 890d99d..382bbf8 100644
index 123ef42..320c189 100644
--- a/c2usb/CMakeLists.txt
+++ b/c2usb/CMakeLists.txt
@@ -70,6 +70,7 @@ target_sources(c2usb
usb/df/device.cpp
usb/df/function.cpp
usb/df/mac.cpp
+ usb/df/mac_diag.cpp
usb/df/message.cpp
usb/standard/descriptors.cpp
)
@@ -78,6 +78,7 @@ if(CONFIG_C2USB_DF)
usb/df/device.cpp
usb/df/function.cpp
usb/df/mac.cpp
+ usb/df/mac_diag.cpp
usb/df/message.cpp
usb/standard/descriptors.cpp
)
diff --git a/c2usb/port/nxp/mcux_mac.cpp b/c2usb/port/nxp/mcux_mac.cpp
index ecaa265..8ff5bd5 100644
index 3d197e7..a806314 100644
--- a/c2usb/port/nxp/mcux_mac.cpp
+++ b/c2usb/port/nxp/mcux_mac.cpp
@@ -18,6 +18,9 @@
Expand Down Expand Up @@ -98,15 +98,15 @@ index ecaa265..8ff5bd5 100644
}

usb::result mcux_mac::ep_cancel(ep_handle eph)
@@ -294,6 +313,7 @@ void mcux_mac::process_ep_notification(const _usb_device_callback_message_struct
@@ -299,6 +318,7 @@ void mcux_mac::process_ep_notification(const _usb_device_callback_message_struct
{
// control transfer is cancelled at bus reset
// react to the reset signal only, ignore this event
+ diag::record(diag::CTRL_ERROR, message.code, 0);
return;
}
if (message.isSetup)
@@ -351,6 +371,17 @@ void mcux_mac::process_ep_notification(const _usb_device_callback_message_struct
@@ -361,6 +381,17 @@ void mcux_mac::process_ep_notification(const _usb_device_callback_message_struct
{
busy_flags_.clear(addr);

Expand All @@ -124,7 +124,7 @@ index ecaa265..8ff5bd5 100644
ep_transfer_complete(addr, transfer(message.buffer, cancelled ? 0 : message.length,
not cancelled, ep_address_to_handle(addr)));
}
@@ -368,28 +399,35 @@ void mcux_mac::process_notification(const _usb_device_callback_message_struct& m
@@ -378,28 +409,35 @@ void mcux_mac::process_notification(const _usb_device_callback_message_struct& m
switch (message.code)
{
case kUSB_DeviceNotifyBusReset:
Expand Down Expand Up @@ -161,18 +161,18 @@ index ecaa265..8ff5bd5 100644
default:
process_ep_notification(message);
diff --git a/c2usb/port/zephyr/udc_mac.cpp b/c2usb/port/zephyr/udc_mac.cpp
index 03fc6f3..a1f48c9 100644
index 0dddfd1..79a9578 100644
--- a/c2usb/port/zephyr/udc_mac.cpp
+++ b/c2usb/port/zephyr/udc_mac.cpp
@@ -11,6 +11,7 @@
@@ -2,6 +2,7 @@
#include "port/zephyr/udc_mac.hpp"
#include <atomic>
#include "compatibility_helper.hpp"
+#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/message_queue.hpp>
extern "C"
@@ -18,6 +19,19 @@ extern "C"
@@ -9,6 +10,19 @@ extern "C"
#include <zephyr/drivers/usb/udc.h>
}

Expand All @@ -192,23 +192,23 @@ index 03fc6f3..a1f48c9 100644
#if defined(CONFIG_DEBUG) == defined(NDEBUG)
// for assert() to be active in debug configuration only, this is necessary
#error "Either CONFIG_DEBUG or NDEBUG must be defined"
@@ -113,6 +127,7 @@ static int udc_buf_enqueue(const ::device* dev, ::net_buf* buf)
@@ -104,6 +118,7 @@ static int udc_buf_enqueue(const ::device* dev, ::net_buf* buf)
if (ret != 0)
{
LOG_ERR("Failed to enqueue ep 0x%02x: %d", udc_get_buf_info(buf)->ep, ret);
+ diag::record(diag::ENQUEUE_FAIL, udc_get_buf_info(buf)->ep, ret);
}
return ret;
}
@@ -151,6 +166,7 @@ udc_mac::udc_mac(const ::device* dev, size_t ctrl_ep_buf_size, usb::power::state
@@ -142,6 +157,7 @@ udc_mac::udc_mac(const ::device* dev, size_t ctrl_ep_buf_size, usb::power::state
}
set_control_buffer(std::span<uint8_t>(ctrl_buf_->data, ctrl_buf_->size));

+ diag::set_mac(this);
set_driver_ctx();
}

@@ -230,9 +246,14 @@ static int udc_mac_event_dispatch(const ::device*, const udc_event* event)
@@ -221,9 +237,14 @@ static int udc_mac_event_dispatch(const ::device*, const udc_event* event)
last_full = !message_queue().try_post(*event);
if (last_full)
{
Expand All @@ -223,23 +223,23 @@ index 03fc6f3..a1f48c9 100644
#if CONFIG_C2USB_UDC_MAC_LOG_LEVEL >= LOG_LEVEL_DBG
static auto min_free_msgq_space = message_queue().free_space();
if (auto free_space = message_queue().free_space(); free_space < min_free_msgq_space)
@@ -351,6 +372,7 @@ uint16_t udc_mac::control_ep_max_packet_size(usb::speed speed) const
@@ -342,6 +363,7 @@ uint16_t udc_mac::control_ep_max_packet_size(usb::speed speed) const

void udc_mac::ctrl_stall(net_buf* buf, int err)
{
+ diag::record(diag::CTRL_STALL, 0, err);
auto addr = endpoint::address::control_in();
if ((request().direction() == usb::direction::OUT) and request().wLength and (err == -ENOMEM))
{
@@ -514,6 +536,7 @@ void udc_mac::process_ctrl_ep_event(net_buf* buf, const udc_buf_info& info)
@@ -512,6 +534,7 @@ void udc_mac::process_ctrl_ep_event(net_buf* buf, const udc_buf_info& info)
net_buf_unref(buf);
LOG_WRN("CTRL EP %x (stage %d) error: %d", info.ep,
info.setup * 0 + info.data * 1 + info.status * 2, info.err);
+ diag::record(diag::CTRL_ERROR, info.ep, info.err);
}
}

@@ -541,6 +564,29 @@ int udc_mac::event_callback(const udc_event& event)
@@ -539,6 +562,29 @@ int udc_mac::event_callback(const udc_event& event)

int udc_mac::process_event(const udc_event& event)
{
Expand Down Expand Up @@ -269,15 +269,15 @@ index 03fc6f3..a1f48c9 100644
if ((power_state() == power::state::L3_OFF) and (event.type != UDC_EVT_VBUS_READY)) [[unlikely]]
{
// flush late events after Vbus removal
@@ -619,6 +665,7 @@ void udc_mac::process_ctrl_ep(net_buf* buf, const udc_buf_info& info)
@@ -617,6 +663,7 @@ void udc_mac::process_ctrl_ep(net_buf* buf, const udc_buf_info& info)
{
LOG_WRN("CTRL EP %x (stage %d) error: %d", info.ep,
info.setup * 0 + info.data * 1 + info.status * 2, info.err);
+ diag::record(diag::CTRL_ERROR, info.ep, info.err);

if (info.setup or (info.data and (dir == direction::OUT)))
{
@@ -742,6 +789,12 @@ void udc_mac::process_ep_event(net_buf* buf)
@@ -747,6 +794,12 @@ void udc_mac::process_ep_event(net_buf* buf)
if (info.err != 0)
{
LOG_ERR("EP %x error:%d", info.ep, info.err);
Expand All @@ -290,7 +290,7 @@ index 03fc6f3..a1f48c9 100644
}
for (uint8_t i = 0; i < ep_bufs_.size(); ++i)
{
@@ -864,10 +917,12 @@ usb::result udc_mac::ep_transfer(usb::df::ep_handle eph, const transfer& t, usb:
@@ -869,10 +922,12 @@ usb::result udc_mac::ep_transfer(usb::df::ep_handle eph, const transfer& t, usb:
#endif
if ((dir == direction::IN) and (power_state() != power::state::L0_ON))
{
Expand All @@ -303,16 +303,16 @@ index 03fc6f3..a1f48c9 100644
return result::device_or_resource_busy;
}

@@ -878,6 +933,7 @@ usb::result udc_mac::ep_transfer(usb::df::ep_handle eph, const transfer& t, usb:
@@ -883,6 +938,7 @@ usb::result udc_mac::ep_transfer(usb::df::ep_handle eph, const transfer& t, usb:
auto ret = udc_ep_enqueue(dev_, buf);
if (ret != 0)
{
+ diag::record(diag::ENQUEUE_FAIL, addr, ret);
busy_flags_.clear(addr);
}
return usb::result(ret);
@@ -965,4 +1021,10 @@ usb::result udc_mac::ep_change_stall(usb::df::ep_handle eph, bool stall)
}
@@ -976,4 +1032,10 @@ bool udc_mac::setup_test_mode(uint8_t mode_selector)
return ret == 0;
}

+extern "C" void c2usb_diag_dump_port(void)
Expand All @@ -323,10 +323,10 @@ index 03fc6f3..a1f48c9 100644
+
} // namespace usb::zephyr
diff --git a/c2usb/usb/df/device.cpp b/c2usb/usb/df/device.cpp
index 5e2a088..de24e11 100644
index f7797c8..85c81dc 100644
--- a/c2usb/usb/df/device.cpp
+++ b/c2usb/usb/df/device.cpp
@@ -11,6 +11,8 @@
@@ -2,6 +2,8 @@
#include "usb/df/device.hpp"
#include "usb/df/function.hpp"
#include "usb/standard/descriptors.hpp"
Expand All @@ -335,7 +335,7 @@ index 5e2a088..de24e11 100644

using namespace magic_enum::bitwise_operators;

@@ -174,6 +176,7 @@ void device::set_configuration(message& msg)
@@ -163,6 +165,7 @@ void device::set_configuration(message& msg)
}
}

Expand Down Expand Up @@ -631,5 +631,5 @@ index 0000000..d240521
+
+#endif // __USB_DF_MAC_DIAG_HPP_
--
2.51.0
2.43.0

1 change: 1 addition & 0 deletions right/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CONFIG_USB_DEVICE_CONFIG_HID=1
#CONFIG_USB_DEVICE_CONFIG_ENDPOINTS=9
CONFIG_USB_DEVICE_CONFIG_LOW_POWER_MODE=y
CONFIG_USB_DEVICE_CONFIG_REMOTE_WAKEUP=y
CONFIG_C2USB=y
CONFIG_C2USB_MCUX_MAC=y
CONFIG_C2USB_MCUX_USB_COEXISTENCE=y
CONFIG_C2USB_HID_BOOT_PROTOCOL=y
Expand Down
11 changes: 5 additions & 6 deletions right/src/hid/keyboard_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class keyboard_app : public hid::application {
usage(generic_desktop::KEYBOARD),
collection::application(
// 6KRO input keys report
keys_input_report_descriptor<KEYS_6KRO_REPORT_ID>(),
keys_input_report<KEYS_6KRO_REPORT_ID>::descriptor(),

// LED report
leds_output_report_descriptor<LEDS_REPORT_ID>(),
output_report<LEDS_REPORT_ID>::descriptor(),

// NKRO keys report with report ID
conditional_report_id<KEYS_NKRO_REPORT_ID>(),
Expand Down Expand Up @@ -104,7 +104,7 @@ class keyboard_app : public hid::application {
usage(generic_desktop::KEYBOARD),
collection::application(
// LED report
leds_output_report_descriptor<LEDS_REPORT_ID>(),
output_report<LEDS_REPORT_ID>::descriptor(),

// NKRO keys report with report ID
conditional_report_id<KEYS_NKRO_REPORT_ID>(),
Expand Down Expand Up @@ -139,11 +139,10 @@ class keyboard_app : public hid::application {

template <uint8_t REPORT_ID = 0>
struct keys_nkro_report_base : public hid::report::base<hid::report::type::INPUT, REPORT_ID> {
hid::report_bitset<hid::page::keyboard_keypad,
hid::page::keyboard_keypad::KEYBOARD_LEFT_CONTROL,
hid::report_bitset_range<hid::page::keyboard_keypad::KEYBOARD_LEFT_CONTROL,
hid::page::keyboard_keypad::KEYBOARD_RIGHT_GUI>
modifiers;
hid::report_bitset<hid::page::keyboard_keypad, NKRO_FIRST_USAGE, NKRO_LAST_USAGE> scancodes;
hid::report_bitset_range<NKRO_FIRST_USAGE, NKRO_LAST_USAGE> scancodes;
void set_code(scancode code, bool value = true)
{
if (modifiers.set(code, value)) {
Expand Down
18 changes: 9 additions & 9 deletions right/src/hid/transport_usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ struct usb_manager {
#endif
}

using namespace usb::df;

static constexpr auto speed = usb::speed::FULL;
static usb::df::hid::function usb_kb{
keyboard_app::usb_handle(), usb::hid::boot_protocol_mode::KEYBOARD};
Expand All @@ -76,15 +78,13 @@ struct usb_manager {
static usb::df::hid::function usb_controls{controls_app::usb_handle()};

constexpr auto config_header =
usb::df::config::header(usb::df::config::power::bus(500, true));
const auto shared_config_elems = usb::df::config::join_elements(
usb::df::hid::config(usb_kb, speed, usb::endpoint::address(0x81), 1),
usb::df::hid::config(usb_mouse, speed, usb::endpoint::address(0x82), 1),
usb::df::hid::config(usb_command, speed, usb::endpoint::address(0x83), 8),
usb::df::hid::config(usb_controls, speed, usb::endpoint::address(0x84), 1));

static const auto base_config =
usb::df::config::make_config(config_header, shared_config_elems);
config::header(config::power::bus(500, config::remote_wakeup));

static const auto base_config = config::make_config(config_header,
usb_kb.config_entry(speed, usb::endpoint::address(0x81), 1),
usb_mouse.config_entry(speed, usb::endpoint::address(0x82), 1),
usb_command.config_entry(speed, usb::endpoint::address(0x83), 8),
usb_controls.config_entry(speed, usb::endpoint::address(0x84), 1));

device_.set_config(base_config);
device_.open();
Expand Down
2 changes: 1 addition & 1 deletion submanifests/c2usb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ manifest:
projects:
- name: c2usb
remote: IntergatedCircuits
revision: 0f5e9b59024efe84e5ec749c49904382928e04ca
revision: 0e11e25e72ecad59847a3de55507ab985a785ee6
import: true
5 changes: 5 additions & 0 deletions west_nrfsdk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ manifest:
- submanifests/c2usb.yml

projects:
- name: zephyrrtos-mcpp
remote: IntergatedCircuits
revision: 1055edd60a7862c963058ac267d188e9439614d3
path: modules/lib/zephyrrtos-mcpp

- name: sdk-nrf
remote: sdk
path: nrf
Expand Down