fa89c89fbe76af299fc57ea895b973b12b1e44ad
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
- command_handler.c/h: Handles incoming commands, processes them, and performs actions based on the command type.
- command_message.c/h: Manages the creation, validation, and logging of command messages, including CRC calculation.
- usb.c/h: Manages USB initialization, data reception via UART, and sending responses. Includes a USB thread.
- usb_conf.c/h: Configures the USB device.
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.
Description
Languages
Python
63.3%
C
36.1%
CMake
0.6%