{"id":28964571,"url":"https://github.com/mytechnotalent/pico_c_stepper","last_synced_at":"2025-06-24T05:07:14.304Z","repository":{"id":299264057,"uuid":"1002500651","full_name":"mytechnotalent/pico_c_stepper","owner":"mytechnotalent","description":"A professional embedded C application for the Raspberry Pi Pico that combines LED blinking with 4-channel stepper motor control using ULN2003 driver boards. The project demonstrates GPIO control, timing functions, and modular code organization suitable for embedded development.","archived":false,"fork":false,"pushed_at":"2025-06-15T19:25:43.000Z","size":34532,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-15T21:04:17.173Z","etag":null,"topics":["c","pico","pico-sdk","robotics","rp2040","stepper-motor","stepper-motor-control","stepper-motor-driver"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mytechnotalent.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-15T15:55:25.000Z","updated_at":"2025-06-15T19:25:46.000Z","dependencies_parsed_at":"2025-06-15T21:17:17.176Z","dependency_job_id":null,"html_url":"https://github.com/mytechnotalent/pico_c_stepper","commit_stats":null,"previous_names":["mytechnotalent/stepper","mytechnotalent/pico_c_stepper"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mytechnotalent/pico_c_stepper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mytechnotalent%2Fpico_c_stepper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mytechnotalent%2Fpico_c_stepper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mytechnotalent%2Fpico_c_stepper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mytechnotalent%2Fpico_c_stepper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mytechnotalent","download_url":"https://codeload.github.com/mytechnotalent/pico_c_stepper/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mytechnotalent%2Fpico_c_stepper/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261611075,"owners_count":23184020,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","pico","pico-sdk","robotics","rp2040","stepper-motor","stepper-motor-control","stepper-motor-driver"],"created_at":"2025-06-24T05:06:54.665Z","updated_at":"2025-06-24T05:07:14.295Z","avatar_url":"https://github.com/mytechnotalent.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"![image](https://github.com/mytechnotalent/stepper/blob/main/stepper.jpeg?raw=true)\n\n# Pico C Stepper\n\nA professional embedded C application for the Raspberry Pi Pico that combines LED blinking with 4-channel stepper motor control using ULN2003 driver boards. The project demonstrates GPIO control, timing functions, and modular code organization suitable for embedded development.\n\n\u003cbr\u003e\n\n## FREE Reverse Engineering Self-Study Course [HERE](https://github.com/mytechnotalent/Reverse-Engineering-Tutorial)\n\n\u003cbr\u003e\n\n## Wiring\n![image](https://github.com/mytechnotalent/stepper/blob/main/diagrams/Debug-Probe-Wiring.png?raw=true)\n![image](https://github.com/mytechnotalent/stepper/blob/main/diagrams/stepper.png?raw=true)\n\n## Features\n\n- **LED Control**: Onboard LED blinking with 1-second cycles\n- **4 Stepper Motors**: Individual control of ULN2003 28BYJ-48 stepper motors\n- **UART-Safe GPIO**: Avoids UART pins to prevent communication conflicts\n- **Professional Code Structure**: Modular design with comprehensive documentation\n- **5V Power Support**: Utilizes VBUS for optimal stepper motor performance\n\n## Hardware Requirements\n\n### Components\n- Raspberry Pi Pico development board\n- 4× ULN2003 stepper motor driver boards  \n- 4× 28BYJ-48 stepper motors (5V, 4-phase)\n- USB cable for power and programming\n- Breadboard and jumper wires\n\n### Power Specifications\n- **Logic Power**: 3.3V (from Pico internal regulator)\n- **Motor Power**: 5V (from USB VBUS pin)\n- **Current per Motor**: ~160mA\n- **Total Current**: 640mA (well within USB 900mA limit)\n\n## GPIO Pin Assignments\n\n| Component           | GPIO Pins      | Description        |\n| ------------------- | -------------- | ------------------ |\n| **Stepper Motor 1** | 2, 3, 6, 7     | IN1, IN2, IN3, IN4 |\n| **Stepper Motor 2** | 10, 11, 14, 15 | IN1, IN2, IN3, IN4 |\n| **Stepper Motor 3** | 18, 19, 20, 21 | IN1, IN2, IN3, IN4 |\n| **Stepper Motor 4** | 22, 26, 27, 28 | IN1, IN2, IN3, IN4 |\n| **Onboard LED**     | 25             | Built-in LED       |\n\n### Avoided UART Pins\nGPIO pins 0, 1, 4, 5, 8, 9, 12, 13, 16, 17 are intentionally avoided to prevent conflicts with UART communication.\n\n## Wiring Connections\n\n### ULN2003 Driver Connections\n```\nPico → ULN2003 (per motor)\nGPIO → IN1, IN2, IN3, IN4\n3.3V → VCC (logic power)\nGND  → GND\n\nULN2003 → 28BYJ-48 Motor\nOUT1 → Blue wire\nOUT2 → Pink wire  \nOUT3 → Yellow wire\nOUT4 → Orange wire\nVCC  → Red wire (5V from Pico VBUS)\n```\n\n### Power Distribution\n```\nUSB 5V → Pico VBUS → Motor power (red wires)\nPico 3.3V → ULN2003 VCC → Logic power\nCommon GND for all components\n```\n\nFor detailed wiring diagrams, see [STEPPER_WIRING.md](STEPPER_WIRING.md).\n\n## Building the Project\n\n### Prerequisites\n- Raspberry Pi Pico SDK installed\n- CMake 3.13 or higher\n- ARM GCC toolchain\n- VS Code with Pico extension (recommended)\n\n### Build Commands\n```bash\nmkdir build\ncd build\ncmake ..\nmake\n```\n\n### Output Files\n- `stepper.uf2` - Main firmware file for drag-and-drop programming\n- `stepper.elf` - ELF executable for debugging\n- `stepper.bin` - Raw binary file\n- `stepper.hex` - Intel HEX format\n\n## Programming the Pico\n\n1. Hold BOOTSEL button while connecting USB\n2. Drag `stepper.uf2` to the RPI-RP2 drive\n3. Pico will automatically reboot and start the application\n\n## Operation\n\n### Program Behavior\n1. **Initialization**: LED and stepper motors configured\n2. **LED Blinking**: Continuous 1-second cycles with serial output\n3. **Stepper Demo**: Every 5 LED cycles, all motors demonstrate movement\n4. **Serial Output**: Status messages via USB serial (115200 baud)\n\n### Serial Output Example\n```\nAll stepper motors initialized successfully!\nStarting LED blink and stepper motor control loop...\nLED ON\nLED OFF\nLED ON\nLED OFF\n[... continues for 5 cycles ...]\nRunning stepper motor demonstration sequence...\nMoving stepper motor 1 clockwise 45 degrees\nMoving stepper motor 1 counter-clockwise 45 degrees\n[... continues for all 4 motors ...]\nStepper sequence complete\n```\n\n### Performance Characteristics\n- **Step Timing**: 3ms delay between steps (configurable)\n- **LED Cycle**: 1 second (500ms on, 500ms off)\n- **Stepper Demo**: Every 5 LED cycles (45° CW, then 45° CCW per motor)\n- **Serial Output**: Status messages for debugging\n\n## API Reference\n\nSee header files for complete API documentation:\n- `src/run.h` - Main application interface\n- `src/stepper.h` - Stepper motor driver interface\n\n## Reverse Engineering \u0026 Analysis\n\nThis project includes a comprehensive reverse engineering data generation script for educational purposes and deep analysis of the compiled binary.\n\n### Generating Analysis Data\n\n```bash\n# Generate complete reverse engineering dataset\n./generate_reverse_engineering_data.sh\n```\n\nThe script creates a `data/` folder containing comprehensive analysis files **and generates a professional PDF report**: \n\n#### 📚 **\"Hacking Embedded Stepper\" by Kevin Thomas**\n- **Complete PDF Guide** - Professional reverse engineering documentation\n- **Cover Artwork** - Uses `stepper.jpeg` as the front cover\n- **8 Comprehensive Chapters** - From basic analysis to advanced topics\n- **Educational Focus** - Perfect for learning embedded systems reverse engineering\n\n**Requirements for PDF generation:**\n```bash\n# Install pandoc (if not already installed)\nbrew install pandoc              # macOS\nsudo apt install pandoc         # Ubuntu/Debian\n```\n\n### Binary Analysis Results\n\n#### Memory Layout\nThe compiled binary has the following memory organization:\n\n```\nFlash Memory (2MB total):\n├── .boot2      (0x10000000): 256 bytes   - RP2040 bootloader\n├── .text       (0x10000100): 16,512 bytes - Program code\n├── .rodata     (0x10004180): 1,284 bytes  - Read-only data\n└── .binary_info(0x10004684): 32 bytes    - Binary metadata\n\nSRAM (264KB total):\n├── .ram_vector_table: 192 bytes  - Interrupt vector table\n├── .data      : 296 bytes        - Initialized variables\n├── .bss       : 1,000 bytes      - Uninitialized variables\n├── .heap      : 2,048 bytes      - Dynamic memory\n└── .stack     : 2,048 bytes      - Function call stack\n```\n\n#### Key Functions Analysis\n\n**Main Function Disassembly:**\n```assembly\n100002d4 \u003cmain\u003e:\n100002d4: b510         push    {r4, lr}         ; Save registers\n100002d6: f003 fe07    bl      0x10003ee8 \u003cstdio_init_all\u003e  ; Initialize UART\n100002da: f000 f803    bl      0x100002e4 \u003crun\u003e             ; Call main loop\n100002de: 2000         movs    r0, #0x0                     ; Return 0\n100002e0: bd10         pop     {r4, pc}                     ; Restore \u0026 return\n```\n\n**Run Function (Main Loop):**\n```assembly\n100002e4 \u003crun\u003e:\n100002e4: b5f0         push    {r4, r5, r6, r7, lr}        ; Save registers\n100002e6: 46de         mov     lr, r11                      ; High register save\n100002e8: 4657         mov     r7, r10\n100002ea: 464e         mov     r6, r9\n100002ec: 4645         mov     r5, r8\n100002ee: b5e0         push    {r5, r6, r7, lr}            ; Push high regs\n100002f0: 2019         movs    r0, #0x19                   ; GPIO 25 (LED)\n100002f2: b0a5         sub     sp, #0x94                   ; Allocate stack space\n100002f4: f000 fa1c    bl      0x10000730 \u003cgpio_init\u003e      ; Initialize LED GPIO\n```\n\n**Stepper Initialization:**\n```assembly\n10000614 \u003cstepper_init\u003e:\n10000614: b5f8         push    {r3, r4, r5, r6, r7, lr}   ; Save registers\n10000616: 4647         mov     r7, r8\n10000618: 46ce         mov     lr, r9\n1000061a: 0004         movs    r4, r0                      ; Motor structure ptr\n1000061c: b580         push    {r7, lr}\n1000061e: 4690         mov     r8, r2                      ; GPIO pin 2\n10000620: 000f         movs    r7, r1                      ; GPIO pin 1\n10000622: 001e         movs    r6, r3                      ; GPIO pin 3\n10000624: 2800         cmp     r0, #0x0                    ; Check null pointer\n10000626: d040         beq     0x100006aa \u003cstepper_init+0x96\u003e  ; Branch if null\n10000628: 6083         str     r3, [r0, #0x8]             ; Store pin 3\n1000062a: 9b08         ldr     r3, [sp, #0x20]            ; Load pin 4 from stack\n1000062c: 2501         movs    r5, #0x1                   ; Set enabled flag\n1000062e: 60c3         str     r3, [r0, #0xc]             ; Store pin 4\n10000630: 9b09         ldr     r3, [sp, #0x24]            ; Load more parameters\n10000632: 6001         str     r1, [r0]                   ; Store pin 1\n10000634: 6103         str     r3, [r0, #0x10]            ; Store parameter\n10000636: 2300         movs    r3, #0x0                   ; Clear position\n10000638: 6042         str     r2, [r0, #0x4]             ; Store pin 2\n1000063a: 6143         str     r3, [r0, #0x14]            ; Clear position counter\n1000063c: 7605         strb    r5, [r0, #0x18]            ; Set enabled flag\n```\n\n#### GPIO Control Implementation\n\n**GPIO Register Manipulation:**\n```assembly\n; GPIO base address: 0xd0000000\n; Set GPIO pin high: Write to GPIO_OUT_SET (offset 0x24)\n; Clear GPIO pin: Write to GPIO_OUT_CLR (offset 0x28)\n\n1000065a: 002b         movs    r3, r5                      ; Copy pin mask\n1000065c: 21d0         movs    r1, #0xd0                   ; GPIO base (high)\n1000065e: 40bb         lsls    r3, r7                      ; Shift mask to pin\n10000660: 0609         lsls    r1, r1, #0x18              ; Complete GPIO base\n10000662: 624b         str     r3, [r1, #0x24]            ; Write to GPIO_OUT_SET\n```\n\n#### String Analysis\n\n**Embedded Debug Strings:**\n```c\n// Found at addresses in .rodata section:\n\"Failed to initialize stepper motor %d\"\n\"Stepper motor initialization failed. Exiting...\"\n\"Stepper motor %d initialized on pins %d,%d,%d,%d\"\n\"All stepper motors initialized successfully!\"\n\"Starting LED blink and stepper motor control loop...\"\n\"Running stepper motor demonstration sequence...\"\n```\n\n#### Function Symbol Table\n\n**Key Functions (272 total):**\n```\nAddress   Type  Function Name\n10000000  T     __boot2_start__\n100001e8  T     _entry_point\n100002d4  T     main\n100002e4  T     run\n10000414  t     stepper_rotate_multiple_degrees.part.0\n10000614  T     stepper_init\n100006b0  T     stepper_demo_sequence\n10000730  T     gpio_init\n```\n\n### Assembly Analysis Insights\n\n#### ARM Cortex-M0+ Optimization Patterns\n\n**1. Register Usage Optimization:**\n- Frequent use of high registers (r8-r11) for temporary storage\n- Stack manipulation for parameter passing\n- Efficient register spilling during function calls\n\n**2. GPIO Bit Manipulation:**\n```assembly\n; Efficient bit setting using shifts and masks\n40bb         lsls    r3, r7          ; Create pin mask\n624b         str     r3, [r1, #0x24] ; Atomic GPIO set\n```\n\n**3. Function Inlining:**\n- Critical stepper control functions partially inlined\n- Reduced call overhead for time-sensitive operations\n\n**4. Thumb-2 Instruction Usage:**\n- 16-bit instructions for common operations\n- 32-bit instructions for complex immediate values\n- Optimal code density for Cortex-M0+\n\n#### Performance Analysis\n\n**Timing Characteristics:**\n- GPIO switching: ~8 CPU cycles (60ns at 133MHz)\n- Function call overhead: ~6 cycles\n- Stack frame setup: ~4 cycles\n- Stepper step sequence: ~400 cycles total\n\n**Memory Efficiency:**\n- Code size: 16.5KB (0.8% of flash)\n- RAM usage: 3.5KB (1.3% of SRAM)\n- No dynamic allocation in critical paths\n- Efficient data structure packing\n\n### Advanced Reverse Engineering Techniques\n\n#### Control Flow Analysis\n\n**Main Program Flow:**\n```\nmain() → stdio_init_all() → run()\n  ↓\nLED GPIO initialization\n  ↓\nStepper motor initialization (4 motors)\n  ↓\nMain control loop:\n  ├── LED toggle every 500ms\n  ├── Serial output status\n  └── Stepper demo every 5 seconds\n```\n\n**Stepper Control Flow:**\n```\nstepper_init() → gpio_init() (for each pin)\n  ↓\nstepper_demo_sequence()\n  ↓\nstepper_rotate_multiple_degrees()\n  ↓\nGPIO bit manipulation (4-phase sequence)\n```\n\n#### Security Analysis\n\n**Attack Vectors:**\n1. **GPIO Manipulation**: Direct hardware register access\n2. **Timing Analysis**: Predictable step sequences\n3. **Debug Interface**: UART communication exposure\n4. **Memory Layout**: Predictable function addresses\n\n**Defensive Measures:**\n- Input validation on GPIO parameters\n- Bounds checking for stepper commands\n- Disable debug output in production\n- Use address randomization if available\n\n#### Compiler Optimization Analysis\n\n**GCC Optimization Flags Detected:**\n- `-O2` optimization level (inferred from code patterns)\n- Function inlining for performance-critical code\n- Dead code elimination\n- Constant folding for GPIO addresses\n\n**Optimization Evidence:**\n```assembly\n; Immediate value optimization\n21d0         movs    r1, #0xd0       ; Instead of loading from memory\n0609         lsls    r1, r1, #0x18   ; Shifted to create 0xd0000000\n```\n\n## Troubleshooting\n\n### Common Issues\n- **Motors not moving**: Check 5V power connections to ULN2003 VCC\n- **Weak rotation**: Ensure adequate power supply (USB 2.0+ recommended)\n- **No serial output**: Check USB connection and terminal settings\n- **Compilation errors**: Verify Pico SDK installation and environment\n\n### Power Supply Notes\n- USB 2.0 provides adequate current for 4 motors\n- USB 1.1 or weak power supplies may cause erratic behavior\n- External 5V supply can be used for higher current applications\n\n## License\n\nCopyright (c) 2025 Kevin Thomas\n\n## Contributing\n\nThis project follows professional embedded C standards with comprehensive documentation and modular design principles.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmytechnotalent%2Fpico_c_stepper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmytechnotalent%2Fpico_c_stepper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmytechnotalent%2Fpico_c_stepper/lists"}