### Initializing DummyRobot Class C++ Source: https://github.com/peng-zhihui/dummy-robot/blob/main/README.md This snippet shows the constructor signature and initialization logic for the main `DummyRobot` class. It requires parameters defining the robot's structure (DH parameters, zero position) and the configuration and interfaces for the six motor drivers. This setup is fundamental for configuring the robot's kinematic model and its interaction with the joint actuators. ```C++ DummyRobot::DummyRobot( RobotParam_t param, /* 机器人结构参数,包括DH参数,零位等 */ MotorParam_t motor_param[6], /* 6个关节的电机驱动参数 */ MotorDriver::MotorDriver* motor[6] /* 6个关节的电机驱动接口 */ ) { _motor = motor; _robot_param = param; _motor_param = motor_param; _end_pos = Eigen::Matrix4f::Identity(); _end_rot = Eigen::Matrix3f::Identity(); _target_theta = Eigen::Matrix::Zero(); _current_theta = Eigen::Matrix::Zero(); _current_theta_raw = Eigen::Matrix::Zero(); _target_pos = Eigen::Matrix::Zero(); _target_rot = Eigen::Matrix3f::Identity(); _target_rpy = Eigen::Matrix::Zero(); _is_init = false; _is_running = false; _is_servo_on = false; _error_code = 0; } ``` -------------------------------- ### Adding Post-Build Commands for Firmware Images Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Defines custom commands to be executed after the main executable target (`${PROJECT_NAME}.elf`) is built. These commands use `arm-none-eabi-objcopy` to convert the ELF file into Intel Hex (`.hex`) and raw binary (`.bin`) formats, which are commonly used for flashing embedded devices. ```CMake add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD COMMAND ${CMAKE_OBJCOPY} -Oihex $ ${HEX_FILE} COMMAND ${CMAKE_OBJCOPY} -Obinary $ ${BIN_FILE} COMMENT "Building ${HEX_FILE} Building ${BIN_FILE}") ``` -------------------------------- ### Discovering Source Files (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Uses the `file(GLOB_RECURSE)` command to find all source files (`*.*`) recursively within specified directories like `startup`, `Drivers`, `Core`, `UserApp`, `3rdParty`, `Middlewares`, `USB_DEVICE`, `Robot`, and `Bsp`, storing the list in the `SOURCES` variable. ```CMake file(GLOB_RECURSE SOURCES "startup/*.*" "Drivers/*.*" "Core/*.*" "UserApp/*.*" "3rdParty/*.*" "Middlewares/*.*" "USB_DEVICE/*.*" "Robot/*.*" "Bsp/*.*" ) ``` -------------------------------- ### Adding Post-Build Commands - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Defines custom commands that run after the main executable target (${PROJECT_NAME}.elf) is built. These commands use `arm-none-eabi-objcopy` to convert the ELF file into Intel HEX (.hex) and raw binary (.bin) formats, which are commonly used for flashing embedded devices. ```CMake add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD COMMAND ${CMAKE_OBJCOPY} -Oihex $ ${HEX_FILE} COMMAND ${CMAKE_OBJCOPY} -Obinary $ ${BIN_FILE} COMMENT "Building ${HEX_FILE} Building ${BIN_FILE}") ``` -------------------------------- ### Adding Project Include Directories (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Specifies the list of directories where the compiler should search for header files. This includes paths for standard HAL, CMSIS, FreeRTOS, USB library components, and various project-specific (Bsp, Robot, UserApp, 3rdParty) include paths. ```CMake include_directories( Core/Inc Drivers/STM32F4xx_HAL_Driver/Inc Drivers/STM32F4xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32F4xx/Include Drivers/CMSIS/Include Middlewares/Third_Party/FreeRTOS/Source/include Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F Middlewares/ST/STM32_USB_Device_Library/Core/Inc Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc USB_DEVICE/App USB_DEVICE/Target 3rdParty/fibre/cpp/include 3rdParty/u8g2 3rdParty/u8g2/cpp Bsp Bsp/imu Bsp/imu/filters Bsp/communication Bsp/memory Bsp/utils Bsp/gpio Bsp/utils/software_i2c Bsp/utils/arm_math Robot UserApp ) ``` -------------------------------- ### Creating Executable Target Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Defines the main executable target for the project, named `${PROJECT_NAME}.elf`. It includes all collected source files (`${SOURCES}`) and the specified linker script (`${LINKER_SCRIPT}`). ```CMake add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT}) ``` -------------------------------- ### Link Libraries and Directories Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Adds the directory containing CMSIS libraries to the search path and links the specific 'arm_cortexM4lf_math.a' library, likely containing optimized floating-point math functions for the Cortex-M4F. ```CMake link_directories("Drivers/CMSIS/Lib") link_libraries("arm_cortexM4lf_math.a") ``` -------------------------------- ### Adding Post-Build Conversion Commands (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Configures custom commands to run after the ELF executable is successfully built. These commands use `arm-none-eabi-objcopy` to convert the ELF file into standard Intel HEX (.hex) and raw binary (.bin) formats, commonly used for flashing embedded devices. ```CMake set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex) set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin) add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD COMMAND ${CMAKE_OBJCOPY} -Oihex $ ${HEX_FILE} COMMAND ${CMAKE_OBJCOPY} -Obinary $ ${BIN_FILE} COMMENT "Building ${HEX_FILE} Building ${BIN_FILE}") ``` -------------------------------- ### Add Post-Build Commands for Hex and Bin Output Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Defines custom commands that run after the main executable is built. These commands use `arm-none-eabi-objcopy` to convert the ELF executable into Intel HEX (`.hex`) and raw binary (`.bin`) formats, placing them in the binary output directory. ```CMake set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex) set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin) add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD COMMAND ${CMAKE_OBJCOPY} -Oihex $ ${HEX_FILE} COMMAND ${CMAKE_OBJCOPY} -Obinary $ ${BIN_FILE} COMMENT "Building ${HEX_FILE}\nBuilding ${BIN_FILE}") ``` -------------------------------- ### Gathering Source Files Recursively Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Uses the `file(GLOB_RECURSE)` command to find all source files matching the pattern `*.*` within the specified directories and their subdirectories, storing the resulting list in the `SOURCES` variable. ```CMake file(GLOB_RECURSE SOURCES "startup/*.*" "Drivers/*.*" "Core/*.*" "Ctrl/*.*" "UserApp/*.*" "Port/*.*" ) ``` -------------------------------- ### Defining Executable Target (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Creates the main executable target for the project named `${PROJECT_NAME}.elf`. It uses the collected source files (`${SOURCES}`) and the specified linker script (`${LINKER_SCRIPT}`) to build the final firmware executable. ```CMake add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT}) ``` -------------------------------- ### Setting Linker Options (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Adds various linker options, including enabling garbage collection for unused sections, printing memory usage during linking, generating a map file, specifying the target architecture (Cortex-M4, Thumb), and providing the path to the linker script. ```CMake add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map) add_link_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork) add_link_options(-T ${LINKER_SCRIPT}) ``` -------------------------------- ### Linking Libraries and Directories (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Specifies the directory (`Drivers/CMSIS/Lib`) where static libraries are located and links the ARM CMSIS DSP library (`arm_cortexM4lf_math.a`) to the project's executable target. ```CMake link_directories("Drivers/CMSIS/Lib") link_libraries("arm_cortexM4lf_math.a") ``` -------------------------------- ### Adding Linker Options - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Applies linker options. These include garbage collection of unused sections, printing memory usage statistics, generating a map file, specifying the target CPU and Thumb mode for linking, and providing the path to the linker script. ```CMake add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map) add_link_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork) add_link_options(-T ${LINKER_SCRIPT}) ``` -------------------------------- ### Setting Compile Options Based on Build Type Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Applies specific optimization and debug flags depending on the chosen build type (Release, RelWithDebInfo, MinSizeRel, or default). This allows tailoring the build for speed, size, or debugging. ```CMake if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") message(STATUS "Maximum optimization for speed") add_compile_options(-Ofast) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") message(STATUS "Maximum optimization for speed, debug info included") add_compile_options(-Ofast -g) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") message(STATUS "Maximum optimization for size") add_compile_options(-Os) else () message(STATUS "Minimal optimization, debug info included") add_compile_options(-Og -g) endif () ``` -------------------------------- ### Adding Linker Options Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Applies specific linker flags to the final link step. These options include garbage collection of unused sections, printing memory usage statistics, generating a map file, and configuring for the target Cortex-M3 architecture and thumb instruction set, and specifying the linker script. ```CMake add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map) add_link_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork) add_link_options(-T ${LINKER_SCRIPT}) ``` -------------------------------- ### Discover Source Files Recursively Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Uses the `file(GLOB_RECURSE ...)` command to find all source files (`*.*`) within specified subdirectories (startup, Drivers, Core, etc.) and stores the list in the `SOURCES` variable. ```CMake file(GLOB_RECURSE SOURCES "startup/*.*" "Drivers/*.*" "Core/*.*" "UserApp/*.*" "3rdParty/*.*" "Middlewares/*.*" "USB_DEVICE/*.*" "Robot/*.*" "Bsp/*.*" ) ``` -------------------------------- ### Setting CMake System Name and Version Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Configures the target system name and version for the build. 'Generic' is used here, indicating a non-standard or embedded system build, while the version is set to '1'. ```CMake set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_SYSTEM_VERSION 1) ``` -------------------------------- ### Setting Output File Variables Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Defines variables to hold the names for the output hex and binary files, based on the project name and binary directory. ```CMake set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex) set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin) ``` -------------------------------- ### Defining Project Name and Languages Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Declares the project name as 'Ctrl-Step-STM32-fw' and specifies the programming languages used in the project: C, C++, and Assembly (ASM). ```CMake project(Ctrl-Step-STM32-fw C CXX ASM) ``` -------------------------------- ### Adding Common Compile Options Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Applies general compilation flags to all source files. These flags configure the compiler for the target Cortex-M3 architecture, thumb instruction set, interworking, function/data sectioning, and disable common symbol placement. ```CMake add_compile_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork) add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) ``` -------------------------------- ### Defining CAN Commands C++ Source: https://github.com/peng-zhihui/dummy-robot/blob/main/README.md This snippet, likely from `interface_can.cpp`, illustrates the structure for handling commands and responses via the CAN bus. Comments define various command types (Set Target, Set PID, Set Gear Ratio, Set CAN ID, Set Mode, Broadcast) identified by the higher bits of the CAN ID and targeting specific motors via the lower bits. Function signatures for parsing commands and sending responses are also shown. ```C++ /* * CAN指令说明 * CAN指令ID分为两部分,高四位代表指令类型,低八位代表电机ID * 指令类型: * 0x0:无指令 * 0x1:设置目标值(根据驱动器模式,可以是位置、速度、力矩) * 0x2:设置PID参数 * 0x3:设置减速比 * 0x4:设置CAN ID * 0x5:设置运行模式 * 0xF:广播指令,用于同步所有电机 */ void parse_can_cmd(CAN_RxHeaderTypeDef* header, uint8_t data[8]) { // ... parsing logic ... } /* * CAN应答说明 * CAN应答ID分为两部分,高四位代表应答类型,低八位代表电机ID * 应答类型: * 0x0:无应答 * 0x1:当前位置 * 0x2:当前速度 * 0x3:当前力矩 * 0x4:错误代码 * 0xF:广播应答,用于同步所有电机 */ void send_can_response(uint8_t cmd_type, uint8_t motor_id, uint8_t data[8]) { // ... sending logic ... } ``` -------------------------------- ### Finding Source Files - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Uses the file command with GLOB_RECURSE to find all source files (`*.*`) within specified subdirectories. The list of found files is stored in the `SOURCES` variable, which will be used to build the executable. ```CMake file(GLOB_RECURSE SOURCES "startup/*.*" "Drivers/*.*" "Core/*.*" "Ctrl/*.*" "UserApp/*.*" "Port/*.*" ) ``` -------------------------------- ### Including Directories for Header Files Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Adds specified directories to the include search path for the compiler. This allows the compiler to find necessary header files from core code, HAL drivers, CMSIS, and project-specific modules. ```CMake include_directories( Core/Inc Drivers/STM32F1xx_HAL_Driver/Inc Drivers/STM32F1xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32F1xx/Include Drivers/CMSIS/Include Ctrl Ctrl/Sensor Ctrl/Signal Ctrl/LowLevel Ctrl/MotorControl UserApp Port ) ``` -------------------------------- ### Specifying ARM Cross-Compilers and Tools Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Defines the specific GNU ARM Embedded toolchain executables to be used for compiling C, C++, and Assembly code, as well as the archiving, object copying, object dumping, and size tools. ```CMake set(CMAKE_C_COMPILER arm-none-eabi-gcc) set(CMAKE_CXX_COMPILER arm-none-eabi-g++) set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) set(CMAKE_AR arm-none-eabi-ar) set(CMAKE_OBJCOPY arm-none-eabi-objcopy) set(CMAKE_OBJDUMP arm-none-eabi-objdump) set(SIZE arm-none-eabi-size) ``` -------------------------------- ### Setting Common Compile/Link Flags (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Configures linker flags to enable printf and scanf support for floating-point numbers using newlib-nano specs. It also adds compiler and linker options for hardware floating-point support targeting the FPV4-SP-D16 FPU. ```CMake set(COMMON_FLAGS "-specs=nosys.specs -specs=nano.specs -u _printf_float -u _scanf_float") #Uncomment for hardware floating point add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING) add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) #Uncomment for software floating point #add_compile_options(-mfloat-abi=soft) ``` -------------------------------- ### Add Include Directories Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Specifies a comprehensive list of directories for the compiler to search for header files, covering various components like Core, HAL drivers, CMSIS, FreeRTOS, USB Device library, 3rd Party libraries, and BSP modules. ```CMake include_directories( Core/Inc Drivers/STM32F4xx_HAL_Driver/Inc Drivers/STM32F4xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32F4xx/Include Drivers/CMSIS/Include Middlewares/Third_Party/FreeRTOS/Source/include Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F Middlewares/ST/STM32_USB_Device_Library/Core/Inc Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc USB_DEVICE/App USB_DEVICE/Target 3rdParty/fibre/cpp/include 3rdParty/u8g2 3rdParty/u8g2/cpp Bsp Bsp/imu Bsp/imu/filters Bsp/communication Bsp/memory Bsp/utils Bsp/gpio Bsp/utils/software_i2c Bsp/utils/arm_math Robot UserApp ) ``` -------------------------------- ### Add Linker Options Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Specifies linker options including garbage collection of unused sections, printing memory usage information, generating a map file, setting the CPU architecture and floating-point ABI, and specifying the linker script to use. ```CMake add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map) add_link_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork) add_link_options(-T ${LINKER_SCRIPT}) ``` -------------------------------- ### Setting Minimum CMake Version Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Specifies the minimum version of CMake required to build the project. This ensures that features used in the CMakeLists.txt file are supported by the build environment. ```CMake cmake_minimum_required(VERSION 3.20) ``` -------------------------------- ### Configuring Floating Point Options (Commented) - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt These commented lines show options for configuring floating-point support. They demonstrate how to enable either hardware floating-point (-mfloat-abi=hard) or software floating-point (-mfloat-abi=soft) by adding specific compile definitions and options. ```CMake #Uncomment for hardware floating point #add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING) #add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) #add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) #Uncomment for software floating point #add_compile_options(-mfloat-abi=soft) ``` -------------------------------- ### Defining UART Commands C++ Source: https://github.com/peng-zhihui/dummy-robot/blob/main/README.md This snippet from `interface_uart.cpp` outlines the command and response format for the UART interface. Commands follow a `#CMDID P1 P2...\r\n` structure, and responses a `$RSPID P1 P2...\r\n` structure. It defines various command IDs (Set Target, Set PID, etc.) and response IDs (Current Position, Current Speed, etc.) for communicating with the motor drivers or the core controller via a serial connection. ```C++ /* * UART指令说明 * UART指令格式:#CMDID P1 P2 P3 ... \r\n * CMDID: 指令ID,两字节,HEX格式 * P1 P2 P3 ...: 参数,根据指令不同参数数量和格式不同 * 参数格式:浮点数或者整数 * 示例:#0101 100.0 20.0 0.1 \r\n * 指令ID: * 0x0101:设置目标值(根据驱动器模式,可以是位置、速度、力矩) * 0x0102:设置PID参数 * 0x0103:设置减速比 * 0x0104:设置CAN ID * 0x0105:设置运行模式 * ... 其他指令 */ void parse_uart_cmd(uint8_t* buffer, uint16_t len) { // ... parsing logic ... } /* * UART应答说明 * UART应答格式:$RSPID P1 P2 P3 ... \r\n * RSPID: 应答ID,两字节,HEX格式 * P1 P2 P3 ...: 参数,根据应答不同参数数量和格式不同 * 参数格式:浮点数或者整数 * 示例:$0201 123.45 \r\n * 应答ID: * 0x0201:当前位置 * 0x0202:当前速度 * 0x0203:当前力矩 * 0x0204:错误代码 * ... 其他应答 */ void send_uart_response(uint16_t rsp_id, ...) { // ... sending logic ... } ``` -------------------------------- ### Specifying Cross-Compiler Toolchain (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Defines the paths or names for the ARM-None-EABI cross-compilers and binutils (gcc, g++, ar, objcopy, objdump, size) used for building firmware. It also sets the default target type for try_compile to STATIC_LIBRARY. ```CMake set(CMAKE_C_COMPILER arm-none-eabi-gcc) set(CMAKE_CXX_COMPILER arm-none-eabi-g++) set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) set(CMAKE_AR arm-none-eabi-ar) set(CMAKE_OBJCOPY arm-none-eabi-objcopy) set(CMAKE_OBJDUMP arm-none-eabi-objdump) set(SIZE arm-none-eabi-size) set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) ``` -------------------------------- ### Adding Preprocessor Definitions Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Defines preprocessor macros that are passed to the compiler for all source files. These typically enable specific features or configurations based on the target hardware and libraries, such as enabling the HAL driver, microlib, and specifying the target microcontroller series and part. ```CMake add_definitions(-DUSE_HAL_DRIVER -D__MICROLIB -DSTM32F1 -DSTM32F1xx -DSTM32F103xB) ``` -------------------------------- ### Adding Executable Target - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Defines the main executable target for the project. It names the output executable `${PROJECT_NAME}.elf` and lists the source files found previously, along with the linker script, as dependencies for this target. ```CMake add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT}) ``` -------------------------------- ### Setting C++ and C Language Standards Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Specifies the required standard versions for C++ and C source files within the project. C++ is set to standard 17 and C is set to standard 11. ```CMake set(CMAKE_CXX_STANDARD 17) set(CMAKE_C_STANDARD 11) ``` -------------------------------- ### Configure Hardware Floating-Point Unit (FPU) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Adds preprocessor definitions and compiler/linker options to enable hardware floating-point support using the FPU on the Cortex-M4 (fpv4-sp-d16). This is enabled by default in this file. ```CMake add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING) add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) ``` -------------------------------- ### Specifying Cross-Compiler Toolchain - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Sets the paths to the ARM-none-eabi cross-compilers and associated tools (gcc, g++, ar, objcopy, objdump, size). It also specifies that try_compile checks should build static libraries, which is typical for embedded development. ```CMake set(CMAKE_C_COMPILER arm-none-eabi-gcc) set(CMAKE_CXX_COMPILER arm-none-eabi-g++) set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) set(CMAKE_AR arm-none-eabi-ar) set(CMAKE_OBJCOPY arm-none-eabi-objcopy) set(CMAKE_OBJDUMP arm-none-eabi-objdump) set(SIZE arm-none-eabi-size) set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) ``` -------------------------------- ### Adding Preprocessor Definitions (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Defines symbols used by the compiler for conditional compilation, such as enabling the HAL driver, specifying the microcontroller series (STM32F4, STM32F4xx), the specific variant (STM32F405xx), and indicating that the FreeRTOS heap is application-allocated. ```CMake add_definitions(-DUSE_HAL_DRIVER -DSTM32F4 -DSTM32F4xx -DSTM32F405xx -DconfigAPPLICATION_ALLOCATED_HEAP) ``` -------------------------------- ### Defining System and CMake Version (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Sets the target system name and version for a generic embedded build and specifies the minimum required CMake version (3.19) for the project. ```CMake set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_SYSTEM_VERSION 1) cmake_minimum_required(VERSION 3.19) ``` -------------------------------- ### Setting General Compile Options (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Adds general compiler options for the target architecture (Cortex-M4, Thumb mode) to the build. It also enables optimizations for function/data sections and disables common symbols and message length limits. ```CMake add_compile_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork) add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) ``` -------------------------------- ### Specifying Linker Script (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Sets the `LINKER_SCRIPT` variable to the path of the linker script file (`STM32F405RGTx_FLASH.ld`). This script is essential for defining the memory layout and section placement on the target microcontroller. ```CMake set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F405RGTx_FLASH.ld) ``` -------------------------------- ### Configuring Project Name and Standards (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Sets the project name to 'Core-STM32F4-fw' and specifies the programming languages used (C, C++, ASM). It also sets the required C++ standard to 17 and the C standard to 11 for the project build. ```CMake project(Core-STM32F4-fw C CXX ASM) set(CMAKE_CXX_STANDARD 17) set(CMAKE_C_STANDARD 11) ``` -------------------------------- ### Setting System and Version - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Configures the target system name to 'Generic' and sets the system version to '1'. This is common practice when building for bare-metal embedded systems where there isn't a specific operating system CMake needs to interact with. ```CMake set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_SYSTEM_VERSION 1) ``` -------------------------------- ### Setting Output File Names - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Sets variables for the names of the desired output files in the binary directory. These will be the .hex and .bin files generated from the final .elf executable. ```CMake set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex) set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin) ``` -------------------------------- ### Adding Global Definitions - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Adds preprocessor definitions that are available to all source files. These typically enable specific features or identify the target hardware and software layer (HAL driver, Microlib, STM32F1 series, specific chip variant STM32F103xB). ```CMake add_definitions(-DUSE_HAL_DRIVER -D__MICROLIB -DSTM32F1 -DSTM32F1xx -DSTM32F103xB) ``` -------------------------------- ### Define Project Name and C/CXX Standards Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Declares the project name as 'Core-STM32F4-fw' and specifies that it uses C, C++, and ASM languages. Sets the C++ standard to 17 and the C standard to 11. ```CMake project(Core-STM32F4-fw C CXX ASM) set(CMAKE_CXX_STANDARD 17) set(CMAKE_C_STANDARD 11) ``` -------------------------------- ### Specify ARM GCC Toolchain Compilers and Utilities Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Defines the specific ARM-NONE-EABI GCC tools to be used for C, C++, and Assembly compilation, as well as archiving, object copying, object dumping, and size reporting. ```CMake set(CMAKE_C_COMPILER arm-none-eabi-gcc) set(CMAKE_CXX_COMPILER arm-none-eabi-g++) set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) set(CMAKE_AR arm-none-eabi-ar) set(CMAKE_OBJCOPY arm-none-eabi-objcopy) set(CMAKE_OBJDUMP arm-none-eabi-objdump) set(SIZE arm-none-eabi-size) set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) ``` -------------------------------- ### Specifying Include Directories - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Adds directories to the list of include paths the compiler will search for header files. This includes various driver files (HAL, CMSIS), core project files, and application/port-specific directories. ```CMake include_directories( Core/Inc Drivers/STM32F1xx_HAL_Driver/Inc Drivers/STM32F1xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32F1xx/Include Drivers/CMSIS/Include Ctrl Ctrl/Sensor Ctrl/Signal Ctrl/LowLevel Ctrl/MotorControl UserApp Port ) ``` -------------------------------- ### Adding Common Compile Options - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Applies common compiler options for the Cortex-M3 target microcontroller. These options include specifying the CPU architecture, enabling Thumb instruction mode, enabling interworking, and optimizing for function and data section placement to facilitate garbage collection. ```CMake add_compile_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork) add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) ``` -------------------------------- ### Select Optimization Level Based on Build Type Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Applies different compiler optimization flags (`-Ofast`, `-Os`, `-Og`) and debug info flags (`-g`) based on the CMake build type (Release, RelWithDebInfo, MinSizeRel, or default). ```CMake if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") message(STATUS "Maximum optimization for speed") add_compile_options(-Ofast) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") message(STATUS "Maximum optimization for speed, debug info included") add_compile_options(-Ofast -g) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") message(STATUS "Maximum optimization for size") add_compile_options(-Os) else () message(STATUS "Minimal optimization, debug info included") add_compile_options(-Og -g) endif () ``` -------------------------------- ### Applying Build Type Specific Optimizations (CMake) Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists_template.txt Configures compiler optimization flags based on the current CMake build type (Release, RelWithDebInfo, MinSizeRel, default). It applies aggressive optimizations like -Ofast or -Os and includes debug information (-g) for certain build types. ```CMake if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") message(STATUS "Maximum optimization for speed") add_compile_options(-Ofast) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") message(STATUS "Maximum optimization for speed, debug info included") add_compile_options(-Ofast -g) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") message(STATUS "Maximum optimization for size") add_compile_options(-Os) else () message(STATUS "Minimal optimization, debug info included") add_compile_options(-Og -g) endif () ``` -------------------------------- ### Add Executable Target Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Defines the main executable target named based on the project name (PROJECT_NAME.elf) using the collected source files and the specified linker script. ```CMake add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT}) ``` -------------------------------- ### Setting Try-Compile Target Type Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Sets the type of target that CMake should build when performing try-compile checks. Here, it's set to `STATIC_LIBRARY`, common for embedded projects where executables depend on specific startup code. ```CMake set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) ``` -------------------------------- ### Defining Project and Standards - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Defines the project name as 'Ctrl-Step-STM32-fw' and specifies the languages used (C, C++, ASM). It also sets the required C++ standard to C++17 and the C standard to C11 for the project. ```CMake project(Ctrl-Step-STM32-fw C CXX ASM) set(CMAKE_CXX_STANDARD 17) set(CMAKE_C_STANDARD 11) ``` -------------------------------- ### Setting Minimum CMake Version - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Specifies the minimum required version of CMake to process this script. This ensures that all commands and features used in the script are supported by the CMake version being used. ```CMake cmake_minimum_required(VERSION 3.20) ``` -------------------------------- ### Setting Linker Script Variable Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists_template.txt Defines a variable `LINKER_SCRIPT` holding the path to the linker script file used for controlling the memory layout and section placement of the compiled firmware. ```CMake set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F103CBTx_FLASH.ld) ``` -------------------------------- ### Set Common GCC Flags for Printf/Scanf Float Support Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Configures GCC flags to enable support for floating-point numbers in standard C library functions like `printf` and `scanf` by specifying `nosys.specs`, `nano.specs`, and explicitly including float support. ```CMake set(COMMON_FLAGS "-specs=nosys.specs -specs=nano.specs -u _printf_float -u _scanf_float") ``` -------------------------------- ### Add Preprocessor Definitions Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Defines symbols used for conditional compilation, including those specific to the STM32 HAL driver, the STM32F4 family, the specific STM32F405xx device, and a FreeRTOS configuration macro. ```CMake add_definitions(-DUSE_HAL_DRIVER -DSTM32F4 -DSTM32F4xx -DSTM32F405xx -DconfigAPPLICATION_ALLOCATED_HEAP) ``` -------------------------------- ### Configure Core Architecture and Optimization Flags Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Adds compiler options specifying the target architecture (Cortex-M4, Thumb instruction set) and enabling optimizations like function/data sectioning and disabling common sections. ```CMake add_compile_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork) add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) ``` -------------------------------- ### Setting Linker Script Path - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Sets the variable `LINKER_SCRIPT` to the absolute path of the linker script file (`STM32F103CBTx_FLASH.ld`). This script defines the memory layout and section placement for the final executable. ```CMake set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F103CBTx_FLASH.ld) ``` -------------------------------- ### Configure CMake System and Version Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Sets the CMake system name to 'Generic' and the version to '1'. This is a common practice for embedded projects where CMake isn't building for a standard OS environment. ```CMake set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_SYSTEM_VERSION 1) cmake_minimum_required(VERSION 3.19) ``` -------------------------------- ### Configuring Optimization Based on Build Type - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt Conditionally adds compiler optimization flags based on the CMake build type (Release, RelWithDebInfo, MinSizeRel, Debug). It selects between -Ofast (speed) and -Os (size), adding debug info (-g) for debugging build types. ```CMake if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") message(STATUS "Maximum optimization for speed") add_compile_options(-Ofast) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") message(STATUS "Maximum optimization for speed, debug info included") add_compile_options(-Ofast -g) elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") message(STATUS "Maximum optimization for size") add_compile_options(-Os) else () message(STATUS "Minimal optimization, debug info included") add_compile_options(-Og -g) endif () ``` -------------------------------- ### Define Linker Script Path Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Core-STM32F4-fw/CMakeLists.txt Sets the `LINKER_SCRIPT` variable to the path of the linker script file relative to the CMake source directory. ```CMake set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F405RGTx_FLASH.ld) ``` -------------------------------- ### Disabling C++17 Register Warning (Commented) - CMake Source: https://github.com/peng-zhihui/dummy-robot/blob/main/2.Firmware/Ctrl-Step-Driver-STM32F1-fw/CMakeLists.txt This commented line shows how to suppress the C++17 deprecation warning related to the 'register' keyword. It modifies the C++ flags to add the specific warning suppression option. ```CMake # uncomment to mitigate c++17 absolute addresses warnings #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.