Added first documentation
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
# USB Command Handler
|
||||
|
||||
Implementation of command handler for this project.
|
||||
|
||||
|
||||
### ACK
|
||||
|
||||
Every command returns either an ACK or NACK.
|
||||
|
||||
### Commands
|
||||
```c
|
||||
typedef enum {
|
||||
COMMAND_ACK,
|
||||
COMMAND_NACK,
|
||||
LED_TOGGLE,
|
||||
LED_SET,
|
||||
SERVO_SET,
|
||||
SERVO_SET_ALL,
|
||||
ADC_READ_RAW,
|
||||
ADC_READ_RAW_ALL,
|
||||
ADC_READ,
|
||||
ADC_READ_ALL,
|
||||
ADC_SET_READ,
|
||||
DIGITAL_IN,
|
||||
DIGITAL_LEG_OUT,
|
||||
POWER_MONITOR_SET_READ,
|
||||
POWER_MONITOR_READ,
|
||||
SETTINGS_TEST_SAVE,
|
||||
SETTINGS_TEST_READ,
|
||||
|
||||
MOVE_COMMAND_START,
|
||||
// All the commands going to robot thread go here
|
||||
TEST_COMMAND, // 16
|
||||
|
||||
MOVE_COMMAND_END
|
||||
|
||||
} commands_e;
|
||||
```
|
||||
|
||||
### Command Structure
|
||||
|
||||
```c
|
||||
#define COMMAND_PREFIX 0x69
|
||||
#define COMMAND_ID 0x00
|
||||
#define COMMAND_DATA_SIZE 160
|
||||
#define COMMAND_HEADER_SIZE 13
|
||||
|
||||
struct command_message_t {
|
||||
uint8_t prefix;
|
||||
uint8_t length;
|
||||
uint8_t id;
|
||||
uint8_t command;
|
||||
int64_t tick;
|
||||
uint8_t crc;
|
||||
uint8_t data[COMMAND_DATA_SIZE];
|
||||
} __attribute__((packed));
|
||||
```
|
||||
|
||||
### CRC Calculation Function
|
||||
|
||||
```c
|
||||
uint8_t command_calculate_crc(struct command_message_t *msg) {
|
||||
uint32_t sum = 0;
|
||||
uint8_t crc = 0;
|
||||
|
||||
uint8_t *byte_ptr = (uint8_t *)msg;
|
||||
int loop_length = (sizeof(struct command_message_t) - sizeof(msg->data) + msg->length);
|
||||
|
||||
for (int i = 0; i < loop_length; i++) {
|
||||
if (i == (COMMAND_HEADER_SIZE - 1)) { continue; }
|
||||
sum += byte_ptr[i];
|
||||
}
|
||||
|
||||
crc = 0x100 - (sum & 0xff);
|
||||
|
||||
return crc;
|
||||
}
|
||||
```
|
||||
|
||||
### Python test script (AI generated)
|
||||
|
||||
**Command Usage:**
|
||||
|
||||
```bash
|
||||
python3 scripts/send_cmd.py --help
|
||||
```
|
||||
|
||||
This script sends/reads commands
|
||||
+33
-47
@@ -1,56 +1,42 @@
|
||||
# Zephyr USB Command Handler
|
||||
# Servo controller for Servo2350 board
|
||||
|
||||
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
|
||||
A zephyr app for a hexapod project.
|
||||
|
||||
```c
|
||||
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));
|
||||
/*
|
||||
|
||||
(1) (6)
|
||||
\ /
|
||||
\ /
|
||||
\ /
|
||||
*---(F)---*
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
(2)---* o *---(5)
|
||||
\ /
|
||||
\ /
|
||||
\ /
|
||||
*---(B)---*
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
(3) (4)
|
||||
|
||||
*/
|
||||
```
|
||||
|
||||
### CRC Calculation Function
|
||||
## Project overview
|
||||
From [../pins.drawio](../pins.drawio)
|
||||

|
||||
|
||||
```c
|
||||
uint8_t calculate_crc(struct command_message_t *msg) {
|
||||
uint32_t sum = 0;
|
||||
uint8_t crc = 0;
|
||||
## Hardware
|
||||
This project uses custom open source RP2350B board developed in [Servo2350](https://git.aintmina.org/aintgit/servo2350).
|
||||
|
||||
uint8_t *byte_ptr = (uint8_t *)msg;
|
||||
Schematics can be found in [boards/arm/servo2350/docs/Servo2350.pdf](boards/arm/servo2350/docs/Servo2350.pdf).
|
||||
|
||||
for (int i = 0; i < (sizeof(struct command_message_t) - sizeof(msg->data) + msg->length) - 1; i++) {
|
||||
sum += byte_ptr[i];
|
||||
}
|
||||
## Zephyr environment setup
|
||||
See [SETUP.md](SETUP.md)
|
||||
|
||||
crc = 0x100 - (sum & 0xff);
|
||||
|
||||
return crc;
|
||||
}
|
||||
```
|
||||
|
||||
### Python test script (AI generated)
|
||||
|
||||
**Command Usage:**
|
||||
|
||||
```bash
|
||||
python3 scripts/led_blink.py
|
||||
```
|
||||
|
||||
This script sends LED toggle commands to the device connected via USB.
|
||||
## Communication
|
||||
See [COMMANDS.md](COMMANDS.md)
|
||||
@@ -0,0 +1,95 @@
|
||||
# Installation Guide
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Install required tools:
|
||||
|
||||
```bash
|
||||
python -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install west pyserial pyocd
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
zephyr/
|
||||
├── venv/ # Python virtual environment
|
||||
├── sdk/zephyr/ # Zephyr source code
|
||||
├── zephyr-sdk-1.0.1/ # Zephyr SDK installation
|
||||
├── project/ # Git root
|
||||
| └── servo_controller # Zephyr project
|
||||
└── env.sh # Environment setup script
|
||||
```
|
||||
|
||||
## Setup
|
||||
|
||||
### 1. Clone Zephyr SDK
|
||||
|
||||
```bash
|
||||
# in zephyr/
|
||||
mkdir sdk
|
||||
cd sdk
|
||||
git clone https://github.com/zephyrproject-rtos/zephyr.git
|
||||
```
|
||||
|
||||
### 2. Install Zephyr SDK
|
||||
|
||||
```bash
|
||||
# in zephyr/
|
||||
wget https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v1.0.1/zephyr-sdk-1.0.1_linux-x86_64_gnu.tar.xz
|
||||
tar xf zephyr-sdk-1.0.1_linux-x86_64_gnu.tar.xz
|
||||
```
|
||||
|
||||
### 3. Initialize West
|
||||
|
||||
```bash
|
||||
# in zephyr/sdk/zephyr
|
||||
west init -l .
|
||||
west update
|
||||
```
|
||||
|
||||
### 4. Export a Zephyr CMake package.
|
||||
|
||||
```bash
|
||||
# in zephyr/sdk/zephyr
|
||||
west zephyr-export
|
||||
```
|
||||
|
||||
### 5. Install Python dependencies using west packages.
|
||||
|
||||
```bash
|
||||
# in zephyr/sdk/zephyr
|
||||
west packages pip --install
|
||||
```
|
||||
|
||||
### 6. Create env.sh
|
||||
|
||||
Create `zephyr/env.sh` with the following content:
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
|
||||
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
|
||||
|
||||
export ZEPHYR_BASE="${SCRIPT_DIR}/sdk/zephyr"
|
||||
echo "export ZEPHYR_BASE=\"${SCRIPT_DIR}/sdk/zephyr\""
|
||||
|
||||
export ZEPHYR_SDK_INSTALL_DIR="${SCRIPT_DIR}/zephyr-sdk-1.0.1"
|
||||
echo "export ZEPHYR_SDK_INSTALL_DIR=\"${SCRIPT_DIR}/zephyr-sdk-1.0.1\""
|
||||
|
||||
source "${SCRIPT_DIR}/venv/bin/activate"
|
||||
echo "source \"${SCRIPT_DIR}/venv/bin/activate\""
|
||||
```
|
||||
|
||||
### 7. Build the zephyr app
|
||||
|
||||
```bash
|
||||
cd <project_name>/servo_controller
|
||||
|
||||
# Source the environment
|
||||
source ../../zephyr/env.sh
|
||||
|
||||
# Build the app
|
||||
west build -p -b servo2350/rp2350b/m33
|
||||
```
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 256 KiB |
@@ -0,0 +1,4 @@
|
||||
# Python codes
|
||||
|
||||
> [!NOTE]
|
||||
> These python test scripts are all vibe coded to save time.
|
||||
Reference in New Issue
Block a user