A USB command handler reference code
  • Python 63.3%
  • C 36.1%
  • CMake 0.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-05 16:18:51 +03:00
.vscode First 2026-07-27 15:09:32 +03:00
scripts Vibe coded some untrustworthy test scripts 2026-07-31 14:06:13 +03:00
src Fixed CRC problem 2026-08-05 16:18:51 +03:00
.gitignore First 2026-07-27 15:09:32 +03:00
app.overlay First 2026-07-27 15:09:32 +03:00
CMakeLists.txt First 2026-07-27 15:09:32 +03:00
Kconfig First 2026-07-27 15:09:32 +03:00
prj.conf Improved the comm 2026-07-31 14:04:28 +03:00
README.md Second 2026-07-27 15:13:22 +03:00

Zephyr USB Command Handler

This repository contains the implementation of a USB command handler for the Zephyr RTOS. The system allows devices to communicate via USB using custom command messages.

Components

  1. command_handler.c/h: Handles incoming commands, processes them, and performs actions based on the command type.
  2. command_message.c/h: Manages the creation, validation, and logging of command messages, including CRC calculation.
  3. usb.c/h: Manages USB initialization, data reception via UART, and sending responses. Includes a USB thread.
  4. usb_conf.c/h: Configures the USB device.

ACK

Every command returns either an ACK or NACK.

Command Structure

struct command_message_t {
    uint8_t prefix;     // 0x69
    uint8_t length;
    uint8_t id;
    uint8_t command;
    uint8_t crc;
    uint8_t data[160];
} __attribute__((packed));

CRC Calculation Function

uint8_t calculate_crc(struct command_message_t *msg) {
    uint32_t sum = 0;
    uint8_t crc = 0;

    uint8_t *byte_ptr = (uint8_t *)msg;

    for (int i = 0; i < (sizeof(struct command_message_t) - sizeof(msg->data) + msg->length) - 1; i++) {
        sum += byte_ptr[i];
    }

    crc = 0x100 - (sum & 0xff);

    return crc;
}

Python test script (AI generated)

Command Usage:

python3 scripts/led_blink.py

This script sends LED toggle commands to the device connected via USB.