connectivity-usb-can
npx machina-cli add skill beriberikix/zephyr-agent-skills/connectivity-usb-can --openclawZephyr Connectivity: USB & CAN
Build versatile hardware interfaces using Zephyr's modular USB device stack and robust CAN controller support.
Core Workflows
1. USB Device Stack
Configure and enable standard USB classes for host communication.
- Reference: usb_device_stack.md
- Key Tools:
CONFIG_USB_DEVICE_STACK,usb_enable(), CDC ACM, HID.
2. USB-to-CAN Integration
Implement high-performance bridge patterns for CAN bus diagnostics and adapters.
- Reference: usb_to_can.md
- Key Tools:
can_send(),k_msgq, binary packetization, CAN filtering.
Quick Start (USB CDC ACM)
# prj.conf
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_CDC_ACM=y
#include <zephyr/usb/usb_device.h>
void main(void) {
usb_enable(NULL);
}
Professional Patterns (Adapter Design)
- Binary Protocols: Use established protocols like
gs_usbfor robust data transfer over the USB-to-CAN bridge. - Hardware Filtering: Rely on the CAN controller's hardware filters to minimize CPU overhead from irrelevant bus traffic.
- Thread Safety: Use Zephyr's kernel IPCs (
k_msgq,k_fifo) to safely move data between high-priority CAN interrupts and the USB processing thread.
Resources
- References:
usb_device_stack.md: Configuring USB classes and descriptors.usb_to_can.md: Adapter patterns and buffering strategies.
Source
git clone https://github.com/beriberikix/zephyr-agent-skills/blob/main/skills/connectivity-usb-can/SKILL.mdView on GitHub Overview
Build and configure Zephyr USB device stack classes (CDC ACM, HID, MSC) and integrate a CAN controller to create USB-to-CAN adapters. This skill emphasizes robust bridging patterns, buffering, and binary packetization to facilitate reliable CAN data transfer and hardware diagnostics.
How This Skill Works
Enable the Zephyr USB device stack and relevant class drivers (CDC ACM, HID, MSC). Implement a high-performance USB-to-CAN bridge using can_send, k_msgq or k_fifo for safe data movement between CAN interrupts and the USB processing thread, and employ a binary protocol such as gs_usb with hardware CAN filtering to minimize CPU load.
When to Use It
- When adding USB interfaces to a Zephyr device
- When building a USB-to-CAN adapter or diagnostic tool
- When you need a high-throughput CAN bridge with reliable buffering
- When integrating multiple USB classes (CDC ACM, HID, MSC) on a board
- When optimizing data transfer between CAN interrupts and USB processing
Quick Start
- Step 1: In prj.conf, enable the USB device stack and CDC ACM (and optionally HID/MSC): CONFIG_USB_DEVICE_STACK=y and CONFIG_USB_CDC_ACM=y
- Step 2: In your main.c, include usb_device.h and call usb_enable(NULL) after initialization
- Step 3: Implement the bridge logic: initialize CAN, set up a k_msgq/k_fifo, and use a binary protocol (gs_usb) with hardware filtering to move data between CAN and USB
Best Practices
- Enable the USB device stack and the desired USB classes in prj.conf
- Rely on hardware CAN filters to minimize bus traffic and CPU load
- Use a robust binary protocol like gs_usb for data framing on the bridge
- Leverage k_msgq or k_fifo to safely move data between CAN IRQs and USB processing
- Keep interrupt handlers lean and offload processing to dedicated threads
Example Use Cases
- USB to CAN bridge for vehicle diagnostics and maintenance
- Zephyr device exposing CDC ACM over USB for console and CAN data
- High-speed CAN bus analyzer with USB interface
- USB multi-class device (CDC ACM + MSC) on a development kit
- GS_USB based USB-CAN adapter implementing buffering and protocol framing