### ESC Serial Communication Functions (C++) Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Provides low-level functions for half-duplex serial communication with ESCs at 19200 baud. SendESC transmits data with optional CRC, and GetESC receives data with timeout handling. Requires specific hardware setup for Atmega328P using SoftwareSerial. ```cpp // Hardware setup for Atmega328P: // - ESC_RX pin (default: 11) - connect to ESC signal wire // - ESC_TX pin (default: 10) - connect 1k resistor to ESC_RX // This creates a half-duplex connection using SoftwareSerial // SendESC: Transmit data to ESC // - Calculates and appends CRC if ESC_CRC is true // - Uses ByteCrc() for CRC16 calculation uint8_t data[] = {CMD_SET_ADDRESS, 0x00, 0x10, 0x00}; SendESC(data, 4); // Set flash address to 0x1000 // GetESC: Receive data from ESC with timeout // - Returns number of bytes received // - Returns 0 on timeout uint8_t response[256]; uint16_t rxLen = GetESC(response, 200); // 200ms timeout if (rxLen > 0 && response[0] == 0x30) { // brSUCCESS // Command successful } // ESC Bootloader commands: // CMD_SET_ADDRESS (0xFF): Set memory address for next operation // CMD_READ_FLASH_SIL (0x03): Read flash memory // CMD_PROG_FLASH (0x01): Program flash memory // CMD_ERASE_FLASH (0x02): Erase flash page // CMD_SET_BUFFER (0xFE): Initialize write buffer // CMD_KEEP_ALIVE (0xFD): Keep bootloader active ``` -------------------------------- ### RP2040: Initialize ESC Passthrough Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Initializes the ESC passthrough system on RP2040 using PIO for half-duplex serial communication at 19200 baud. Supports 1-8 motor pins and adjusts the system clock to 132MHz for optimal PIO timing. Requires the 'esc_passthrough.h' header. ```cpp #include "esc_passthrough.h" void setup() { pinMode(LED_BUILTIN, OUTPUT); // Option 1: Single ESC on pin 10 beginPassthrough(10); // Option 2: Multiple ESCs (up to 8) uint8_t pins[4] = {10, 11, 12, 13}; beginPassthrough(pins, 4); // Option 3: Specify PIO block and state machine // beginPassthrough(pins, 4, pio0, 2); while (true) { bool disconnected = processPassthrough(); if (disconnected) break; // User clicked "disconnect" in BLHeliSuite32 } endPassthrough(); // Restore pin functions and free PIO resources } void loop() { // Blink LED after disconnect digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } ``` -------------------------------- ### 4-Way Interface Protocol Handling Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Handles 4-Way interface protocol commands for direct ESC communication, implementing the BLHeli bootloader protocol. This includes device initialization, flash read/write, and page erase operations, with specific command codes and packet structures for communication. ```cpp // Check_4Way processes these 4-Way commands: // - cmd_InterfaceTestAlive (0x30): Keep-alive ping // - cmd_ProtocolGetVersion (0x31): Returns protocol version 108 // - cmd_InterfaceGetName (0x32): Returns "m4wFCIntf" // - cmd_InterfaceGetVersion (0x33): Returns version 20.04 // - cmd_InterfaceExit (0x34): Exit passthrough mode // - cmd_DeviceReset (0x35): Reset connected ESC // - cmd_DeviceInitFlash (0x37): Initialize ESC bootloader // - cmd_DevicePageErase (0x39): Erase flash page // - cmd_DeviceRead (0x3A): Read from ESC flash // - cmd_DeviceWrite (0x3B): Write to ESC flash // - cmd_InterfaceSetMode (0x3F): Set interface mode (imARM_BLB = 4) // - cmd_DeviceVerify (0x40): Verify written data // 4-Way packet structure: // Escape: '/' (0x2F) // Command: 1 byte // Address: 2 bytes (high, low) // Param Length: 1 byte (0 = 256 bytes) // Params: variable // CRC16: 2 bytes (XMODEM CRC) // Response structure: // Escape: '.' (0x2E) // Command: 1 byte (echoed) // Address: 2 bytes // Param Length: 1 byte // Params: variable // ACK: 1 byte (0x00 = OK) // CRC16: 2 bytes ``` -------------------------------- ### MSP Command Handling Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Processes MSP (MultiWii Serial Protocol) commands to respond to flight controller queries. It emulates a Betaflight flight controller to enable ESC passthrough mode, supporting various commands like version info, status, and configuration. ```cpp // MSP_Check processes these commands: // - MSP_API_VERSION (0x01): Returns API version info // - MSP_FC_VARIANT (0x02): Returns "BTFL" (Betaflight identifier) // - MSP_FC_VERSION (0x03): Returns firmware version // - MSP_BOARD_INFO (0x04): Returns board name "BPPT" (BrushlessPower) // - MSP_BUILD_INFO (0x05): Returns build information // - MSP_STATUS (0x65): Returns flight controller status // - MSP_MOTOR_CONFIG (0x83): Returns motor configuration // - MSP_MOTOR (0x68): Returns motor values // - MSP_SET_4WAY_IF (0xF5): Enables 4-Way interface mode // - MSP_ADVANCED_CONFIG (0x5A): Returns advanced config (protocol type) // Example MSP packet structure: // Header: $M< (0x24 0x4D 0x3C) // Size: 1 byte (payload length) // Type: 1 byte (command ID) // Payload: variable // CRC: 1 byte (XOR of size, type, and payload) // When MSP_SET_4WAY_IF is received: // - InitSerialOutput() is called to start ESC serial // - Enable4Way flag is set to true // - Returns number of connected ESCs (default: 1) ``` -------------------------------- ### RP2040: Cleanup Passthrough Resources Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Cleans up passthrough resources on RP2040 by releasing the PIO state machine, restoring original pin functions, removing PIO programs, and resetting the system clock. Essential for proper shutdown after a passthrough session. Requires 'esc_passthrough.h'. ```cpp #include "esc_passthrough.h" void setup() { uint8_t pins[2] = {10, 11}; beginPassthrough(pins, 2); // Run passthrough for 60 seconds max unsigned long startTime = millis(); while (millis() - startTime < 60000) { if (processPassthrough()) break; } // Always call endPassthrough() to cleanup endPassthrough(); // After this call: // - PIO state machine is released // - Pins restored to their previous functions (PWM, GPIO, etc.) // - System clock restored to original frequency // - PIO programs removed from memory } void loop() { // Normal application code can now use the pins analogWrite(10, 128); // PWM output restored } ``` -------------------------------- ### ESP32: Process BLE Data Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Handles incoming BLE data on ESP32, routing commands via MSP or 4-Way protocols. It manages MTU negotiation and packet fragmentation for large responses, and monitors BLE connection status. ```cpp #include #include "Global.h" #include "ble_comm.h" #include "serial_comm.h" void setup() { Serial.begin(115200); init_ble(); } void loop() { // process_ble() handles: // - MSP commands (starting with 0x24 0x4D 0x3C = "$M<") // - 4-Way commands (starting with 0x2F = "/") // - Connection/disconnection events // - MTU-based packet fragmentation // - ESC serial cleanup on disconnect process_serial(); process_ble(); // BLE connection state can be monitored extern bool ble_connected; if (ble_connected) { // Client is connected via BLE } } ``` -------------------------------- ### ESP32: Initialize Bluetooth Low Energy for ESC Config Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Initializes BLE on ESP32 to enable wireless ESC configuration via the BLHeli32 Android app. It sets up a BLE server with a custom service (UUID 0x1000) and characteristics for communication. Requires Arduino.h, Global.h, serial_comm.h, and ble_comm.h. ```cpp #include #include "Global.h" #include "serial_comm.h" #include "ble_comm.h" void setup() { Serial.begin(115200); // Initialize BLE with device name "SBBUA_ESC" // Configures: // - MTU size: 517 bytes // - TX power: ESP_PWR_LVL_P9 (maximum) // - Service UUID: 0x1000 // - Write characteristic: 0x1001 // - Read/Notify characteristic: 0x1002 init_ble(); Serial.println("BLE initialized - connect with BLHeli32 Android app"); } void loop() { process_serial(); // Handle USB serial connections process_ble(); // Handle BLE connections } ``` -------------------------------- ### Platform-Specific ESC Pin Configuration (C++) Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Configures GPIO pins for ESC signal connections on ESP32, Atmega328P, and RP2040 platforms. Each platform has specific requirements for half-duplex serial communication and may require library modifications or specific cores. ```cpp // === ESP32 Configuration === // Default: GPIO16 for ESC signal // Edit ESC_Serial.cpp to change: #define ESC_RX 16 // Required libraries (Arduino Library Manager): // - Espressif ESP32 (arduino-esp32) // - NimBLE-Arduino (for BLE support) // - ESPSoftwareSerial ``` ```cpp // === Atmega328P Configuration === // Default pins: // - Servo RX: GPIO11 (connect to ESC signal) // - Servo TX: GPIO10 (connect 1k resistor to GPIO11) // CRITICAL: Modify SoftwareSerial buffer size! // Location (Windows): // C:\Users\\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.x.x\libraries\SoftwareSerial\src // Location (macOS): // /Users//Library/Arduino15/packages/arduino/hardware/avr/1.x.x/libraries/SoftwareSerial/src // // In SoftwareSerial.h, change: // #define _SS_MAX_RX_BUFF 64 // To: // #define _SS_MAX_RX_BUFF 300 ``` ```cpp // === RP2040 Configuration === // Any GPIO pins can be used (up to 8 ESCs) uint8_t escPins[4] = {10, 11, 12, 13}; beginPassthrough(escPins, 4); // Recommended Arduino core: // https://arduino-pico.readthedocs.io/en/latest/install.html // No external libraries required ``` -------------------------------- ### RP2040: Process ESC Passthrough Data Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt The main processing loop for RP2040 that handles incoming serial data for MSP and 4-Way protocols. It returns true when a disconnect command is received from the configuration software, signaling a graceful shutdown. Requires 'esc_passthrough.h'. ```cpp #include "esc_passthrough.h" void setup() { uint8_t motorPins[4] = {10, 11, 12, 13}; beginPassthrough(motorPins, 4); unsigned long lastActivity = millis(); while (true) { // processPassthrough() returns: // - 0 (false): Continue normal operation // - 1 (true): Disconnect requested by user bool shouldExit = processPassthrough(); if (shouldExit) { Serial.println("Disconnect requested"); break; } // Optional: Add timeout logic if (Serial.available()) { lastActivity = millis(); } } endPassthrough(); } void loop() { // Post-passthrough code } ``` -------------------------------- ### Atmega328P: Process Serial Data Source: https://context7.com/brushlesspower/blheli-passthrough/llms.txt Main serial processing function for Atmega328P, handling incoming USB serial data. It automatically detects and routes MSP or 4-Way protocol commands to their respective handlers, requiring a modification to the SoftwareSerial buffer size. ```cpp #include #include "Global.h" #include "serial_comm.h" void setup() { Serial.begin(115200); // Note: For Atmega328P, you must modify SoftwareSerial buffer size // Change _SS_MAX_RX_BUFF from 64 to 300 in SoftwareSerial.h } void loop() { // process_serial() automatically: // - Buffers incoming serial data // - Detects MSP commands: "$M<" (0x24 0x4D 0x3C) // - Detects 4-Way commands: "/" (0x2F) // - Routes to MSP_Check() or Check_4Way() // - Sends response back via Serial process_serial(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.