Added first documentation

This commit is contained in:
Your Name
2026-10-08 13:27:57 +03:00
parent 3cc32639d5
commit 35ebba09e0
6 changed files with 223 additions and 50 deletions
+3 -3
View File
@@ -1,6 +1,6 @@
<mxfile host="Electron" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/29.2.9 Chrome/140.0.7339.249 Electron/38.7.2 Safari/537.36" pages="4"> <mxfile host="Electron" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/29.2.9 Chrome/140.0.7339.249 Electron/38.7.2 Safari/537.36" pages="4">
<diagram name="Blocks" id="qTBlaR7WWd7gh2COTHJy"> <diagram name="Blocks" id="qTBlaR7WWd7gh2COTHJy">
<mxGraphModel dx="1088" dy="746" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0"> <mxGraphModel dx="1285" dy="882" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root> <root>
<mxCell id="0" /> <mxCell id="0" />
<mxCell id="1" parent="0" /> <mxCell id="1" parent="0" />
@@ -1122,7 +1122,7 @@
</Array> </Array>
</mxGeometry> </mxGeometry>
</mxCell> </mxCell>
<mxCell id="gFHrDWAFEzCY88qi_DX8-39" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=light-dark(#8B00FE,#891AFF);fontColor=light-dark(#FFFFFF,#FFFFFF);strokeColor=#3700CC;" value="LoRa" vertex="1"> <mxCell id="gFHrDWAFEzCY88qi_DX8-39" parent="1" style="rounded=0;whiteSpace=wrap;html=1;fillColor=light-dark(#8B00FE,#891AFF);fontColor=light-dark(#FFFFFF,#FFFFFF);strokeColor=#3700CC;" value="LoRa?" vertex="1">
<mxGeometry height="80" width="120" x="1240" y="540" as="geometry" /> <mxGeometry height="80" width="120" x="1240" y="540" as="geometry" />
</mxCell> </mxCell>
<mxCell id="gFHrDWAFEzCY88qi_DX8-46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;" value="IRQ" vertex="1"> <mxCell id="gFHrDWAFEzCY88qi_DX8-46" parent="1" style="rounded=0;whiteSpace=wrap;html=1;" value="IRQ" vertex="1">
@@ -1136,7 +1136,7 @@
</Array> </Array>
</mxGeometry> </mxGeometry>
</mxCell> </mxCell>
<mxCell id="gFHrDWAFEzCY88qi_DX8-47" parent="1" style="rounded=0;whiteSpace=wrap;html=1;" value="LoRa&lt;br&gt;&lt;div&gt;thread&lt;/div&gt;" vertex="1"> <mxCell id="gFHrDWAFEzCY88qi_DX8-47" parent="1" style="rounded=0;whiteSpace=wrap;html=1;" value="SPI&lt;br&gt;&lt;div&gt;thread&lt;/div&gt;" vertex="1">
<mxGeometry height="60" width="90" x="1720" y="570" as="geometry" /> <mxGeometry height="60" width="90" x="1720" y="570" as="geometry" />
</mxCell> </mxCell>
<mxCell id="gFHrDWAFEzCY88qi_DX8-59" parent="1" style="rounded=0;whiteSpace=wrap;html=1;" value="IRQ" vertex="1"> <mxCell id="gFHrDWAFEzCY88qi_DX8-59" parent="1" style="rounded=0;whiteSpace=wrap;html=1;" value="IRQ" vertex="1">
+88
View File
@@ -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
View File
@@ -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. A zephyr app for a hexapod project.
### 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
```c ```c
struct command_message_t { /*
uint8_t prefix; // 0x69
uint8_t length; (1) (6)
uint8_t id; \ /
uint8_t command; \ /
uint8_t crc; \ /
uint8_t data[160]; *---(F)---*
} __attribute__((packed)); / \
/ \
/ \
(2)---* o *---(5)
\ /
\ /
\ /
*---(B)---*
/ \
/ \
/ \
(3) (4)
*/
``` ```
### CRC Calculation Function ## Project overview
From [../pins.drawio](../pins.drawio)
![image](project_overview.png)
```c ## Hardware
uint8_t calculate_crc(struct command_message_t *msg) { This project uses custom open source RP2350B board developed in [Servo2350](https://git.aintmina.org/aintgit/servo2350).
uint32_t sum = 0;
uint8_t crc = 0;
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++) { ## Zephyr environment setup
sum += byte_ptr[i]; See [SETUP.md](SETUP.md)
}
crc = 0x100 - (sum & 0xff); ## Communication
See [COMMANDS.md](COMMANDS.md)
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.
+95
View File
@@ -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

+4
View File
@@ -0,0 +1,4 @@
# Python codes
> [!NOTE]
> These python test scripts are all vibe coded to save time.