Files
command_handler/README.md
T
Your Name 973b8bdf39 Second
2026-07-27 15:13:22 +03:00

1.4 KiB

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.