0.前言
正文可以跳過這段
初識clion,應該是2015年首次發布的時候, 那會還是大三,被一則推介廣告吸引到,當時還在用vs studio,但是就喜歡鼓搗新工具,然后下載安裝試用了clion,但是當時對cmake規則知之甚少,而且覺得還麻煩,所以之后就沒有再用clion了。一晃10年已逝。
從事嵌入式MCU開發工作的,現在用過Keil MDK、IAR、VS-Code、RT-thread studio(eclipse核),現在從我輩楷模這里了解用Clion也能做嵌入式開發,那必須試試,
1.為了從github上 clone 稚暉的項目學習進步;
2.了解多種IDE的開發方式,知己知彼,后續可以根據項目適用哪種平臺,靈活的選擇開發方式:
比如,我現在喜歡RT-Thread Studio,因為基于HAL庫的初始化代碼可以很方面的使用cubemx的ioc工程配置,rt-thread系統的管理 也很方便。
Clion 也可以使用ioc工程完成基于HAL庫的的初始化配置。
所以可以根據項目的特點 選擇最易開發的平臺。
3.另外就是大家說的開源問題,商業軟件都涉及licenses,能用開源的方式還是開源的方式,雖然clion也不是開源的,但是商業化的便捷也可以試試到底有多好用。
參考稚暉君-配置CLion用于STM32開發【優雅の嵌入式開發】,隨著系統版本更新,各軟件也都更新,與時俱進,參考配置的過程也不是一帆風順,故記錄一下,我的配置環境和過程,方便交流學習。
1.環境及所需工具
軟件環境:
軟件名 | 版本號 | 描述 |
---|---|---|
Windows 11 | ||
STM32CubeMX | V6.5.0 | 官網直達 |
Clion | 2024.3.4 | 官網直達 |
MinGW-W64 | MSYS2更新最新版 | 官網直達 |
OpenOCD | OpenOCD是用于對STM32進行下載仿真的工具,是一個開源軟件包 | |
arm-none-eabi-gcc |
關于交叉編譯工具鏈的詳細可跳轉到我的另外一篇博文windows平臺下交叉編譯工具鏈和環境詳解
硬件環境:
軟件名 | 版本號 | 描述 |
---|---|---|
STM32F103VET6 | ||
J-Link | V9 |
2.需要重點注意的點
稚暉的文檔已經很詳細了,我這里只說點卡點
Clion的配置上,在配置預設的MinGW和Cmake環境上,我是使用MSYS2安裝的MinGW-W64環境,
這是正確的配置圖
提供一個錯誤的配置圖,該mingw64-環境 是用于配置rt-thread studio的環境和工具鏈的,這個mingw64-環境是我在其它平臺上正常可以使用的環境,也不知道為什么不能正常工作,這里先將其配置為msys下配置的mingw環境 先解決問題;
其實 對于 插件管理式的平臺,針對嵌入式開發 離不開這些環境的配置;比如vscode eclipse clion等。
目前clion的這個環境文件很重要,即使我不更改下面的其它可執行文件,即為配置成這樣也可以正常構建
我現在有一個問題是 clion 是 如何查找捆綁的cmake gdb的 ?
上述配置完成后 基本上是可以 構建程序了 完成了很重要的一步。
openocd下載和運行
openocd 在稚暉君的教程中需要配置一下 這個 進入方式
進入后需要在該界面下新建一個openocd下載和運行的配置環境,類似于keil里面需要下載什么文件進去,使用什么調試工具。
按照教程 配置 和稚暉的區別是他使用的DAP 可能windows下 clion支持挺好的 , 我用的jlink 就出現一個錯誤
使用的stm32f1x.cfg文件內容如下
# 選擇 J-Link 適配器
adapter driver jlink# 選擇 SWD 協議
transport select swd# 配置目標芯片(STM32F103VE)
source [find target/stm32f1x.cfg]
錯誤情況:
Open On-Chip Debugger 0.12.0
Licensed under GNU GPL v2
For bug reports, readhttp://openocd.org/doc/doxygen/bugs.html
Warn : Failed to open device: LIBUSB_ERROR_NOT_SUPPORTED
Error: No J-Link device found
使用 OpenOCD 調試模式
openocd -d3 -f stm32f103c8_blue_pill.cfg
得到的詳細debug 報告
PS D:\MyProjectRepos\MCUDevelopProject\Ctrl-FOC-Lite\2.Firmware\STM32_HAL_version\Ctrl-FOC-Lite-fw> openocd -d3 -f stm32f103c8_blue_pill.cfg
Open On-Chip Debugger 0.12.0
Licensed under GNU GPL v2
For bug reports, readhttp://openocd.org/doc/doxygen/bugs.html
User : 3 2 options.c:52 configuration_output_handler(): debug_level: 3
User : 4 4 options.c:52 configuration_output_handler():
Debug: 5 6 options.c:233 add_default_dirs(): bindir=D:/a/msys64/mingw64/bin
Debug: 6 7 options.c:234 add_default_dirs(): pkgdatadir=D:/a/msys64/mingw64/share/openocd
Debug: 7 9 options.c:235 add_default_dirs(): exepath=D:/msys64/mingw64/bin
Debug: 8 11 options.c:236 add_default_dirs(): bin2data=../share/openocd
Debug: 9 13 configuration.c:33 add_script_search_dir(): adding C:/Users/leesincere/AppData/Roaming/OpenOCD
Debug: 10 16 configuration.c:33 add_script_search_dir(): adding D:/msys64/mingw64/bin/../share/openocd/site
Debug: 11 19 configuration.c:33 add_script_search_dir(): adding D:/msys64/mingw64/bin/../share/openocd/scripts
Debug: 12 22 command.c:155 script_debug(): command - ocd_find stm32f103c8_blue_pill.cfg
Debug: 13 24 configuration.c:88 find_file(): found stm32f103c8_blue_pill.cfg
Debug: 14 28 command.c:155 script_debug(): command - adapter driver jlink
Debug: 15 30 command.c:155 script_debug(): command - transport select swd
Debug: 16 32 command.c:155 script_debug(): command - ocd_find target/stm32f1x.cfg
Debug: 17 35 configuration.c:88 find_file(): found D:/msys64/mingw64/bin/../share/openocd/scripts/target/stm32f1x.cfg
Debug: 18 38 command.c:155 script_debug(): command - ocd_find target/swj-dp.tcl
Debug: 19 41 configuration.c:88 find_file(): found D:/msys64/mingw64/bin/../share/openocd/scripts/target/swj-dp.tcl
Debug: 20 44 command.c:155 script_debug(): command - transport select
Debug: 21 46 command.c:155 script_debug(): command - ocd_find mem_helper.tcl
Debug: 22 48 configuration.c:88 find_file(): found D:/msys64/mingw64/bin/../share/openocd/scripts/mem_helper.tcl
Debug: 23 52 command.c:155 script_debug(): command - add_usage_text mrw address
Debug: 24 54 command.c:155 script_debug(): command - add_help_text mrw Returns value of word in memory.
Debug: 25 56 command.c:155 script_debug(): command - add_usage_text mrh address
Debug: 26 59 command.c:155 script_debug(): command - add_help_text mrh Returns value of halfword in memory.
Debug: 27 62 command.c:155 script_debug(): command - add_usage_text mrb address
Debug: 28 64 command.c:155 script_debug(): command - add_help_text mrb Returns value of byte in memory.
Debug: 29 67 command.c:155 script_debug(): command - add_usage_text mmw address setbits clearbits
Debug: 30 69 command.c:155 script_debug(): command - add_help_text mmw Modify word in memory. new_val = (old_val & ~clearbits) | setbits;
Debug: 31 73 command.c:155 script_debug(): command - transport select
Debug: 32 74 command.c:155 script_debug(): command - expr [ string first "jtag" $_TRANSPORT ] != -1
Debug: 33 76 command.c:155 script_debug(): command - transport select
Debug: 34 78 command.c:155 script_debug(): command - expr [ string first "jtag" $_TRANSPORT ] != -1
Debug: 35 81 command.c:155 script_debug(): command - transport select
Debug: 36 82 command.c:155 script_debug(): command - expr [ string first "swd" $_TRANSPORT ] != -1
Debug: 37 85 command.c:155 script_debug(): command - swd newdap stm32f1x cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id 0x1ba01477
Debug: 38 88 tcl.c:557 jim_newtap_cmd(): Creating New Tap, Chip: stm32f1x, Tap: cpu, Dotted: stm32f1x.cpu, 8 params
Debug: 39 92 core.c:1474 jtag_tap_init(): Created Tap: stm32f1x.cpu @ abs position 0, irlen 0, capture: 0x0 mask: 0x0
Debug: 40 95 command.c:155 script_debug(): command - dap create stm32f1x.dap -chain-position stm32f1x.cpu
Debug: 41 98 command.c:155 script_debug(): command - transport select
Debug: 42 100 command.c:155 script_debug(): command - expr [ string first "jtag" $_TRANSPORT ] != -1
Debug: 43 103 command.c:155 script_debug(): command - target create stm32f1x.cpu cortex_m -endian little -dap stm32f1x.dap
Debug: 44 107 command.c:289 register_command(): command 'tpiu' is already registered
Debug: 45 109 command.c:289 register_command(): command 'rtt' is already registered
Debug: 46 111 command.c:155 script_debug(): command - stm32f1x.cpu configure -work-area-phys 0x20000000 -work-area-size 0x1000 -work-area-backup 0
Debug: 47 115 target.c:2199 target_free_all_working_areas_restore(): freeing all working areas
Debug: 48 118 target.c:2199 target_free_all_working_areas_restore(): freeing all working areas
Debug: 49 120 target.c:2199 target_free_all_working_areas_restore(): freeing all working areas
Debug: 50 123 command.c:155 script_debug(): command - flash bank stm32f1x.flash stm32f1x 0x08000000 0 0 0 stm32f1x.cpu
Debug: 51 126 tcl.c:1305 handle_flash_bank_command(): 'stm32f1x' driver usage field missing
Debug: 52 129 command.c:155 script_debug(): command - adapter speed 1000
Debug: 53 132 adapter.c:251 adapter_config_khz(): handle adapter khz
Debug: 54 134 adapter.c:215 adapter_khz_to_speed(): convert khz to adapter specific speed value
Debug: 55 136 adapter.c:215 adapter_khz_to_speed(): convert khz to adapter specific speed value
Debug: 56 139 command.c:155 script_debug(): command - adapter srst delay 100
Debug: 57 142 command.c:155 script_debug(): command - transport select
Debug: 58 144 command.c:155 script_debug(): command - expr [ string first "jtag" $_TRANSPORT ] != -1
Debug: 59 147 command.c:155 script_debug(): command - reset_config srst_nogate
Debug: 60 149 command.c:155 script_debug(): command - transport select
Debug: 61 150 command.c:155 script_debug(): command - expr [ string first "hla" $_TRANSPORT ] != -1
Debug: 62 153 command.c:155 script_debug(): command - cortex_m reset_config sysresetreq
Debug: 63 155 command.c:155 script_debug(): command - stm32f1x.cpu configure -event examine-end# DBGMCU_CR |= DBG_WWDG_STOP | DBG_IWDG_STOP |# DBG_STANDBY | DBG_STOP | DBG_SLEEPmmw 0xE0042004 0x00000307 0Debug: 64 161 command.c:155 script_debug(): command - tpiu create stm32f1x.tpiu -dap stm32f1x.dap -ap-num 0 -baseaddr 0xE0040000
Debug: 65 165 command.c:155 script_debug(): command - stm32f1x.tpiu configure -event pre-enable _proc_pre_enable_stm32f1x.tpiu stm32f1x.cpu
Debug: 66 169 command.c:155 script_debug(): command - adapter speed 10000
Debug: 67 171 adapter.c:251 adapter_config_khz(): handle adapter khz
Debug: 68 173 adapter.c:215 adapter_khz_to_speed(): convert khz to adapter specific speed value
Debug: 69 176 adapter.c:215 adapter_khz_to_speed(): convert khz to adapter specific speed value
User : 70 179 options.c:52 configuration_output_handler(): adapter speed: 10000 kHz
User : 71 181 options.c:52 configuration_output_handler():
Info : 72 184 server.c:297 add_service(): Listening on port 6666 for tcl connections
Info : 73 187 server.c:297 add_service(): Listening on port 4444 for telnet connections
Debug: 74 189 command.c:155 script_debug(): command - init
Debug: 75 191 command.c:155 script_debug(): command - target init
Debug: 76 193 command.c:155 script_debug(): command - target names
Debug: 77 195 command.c:155 script_debug(): command - stm32f1x.cpu cget -event gdb-flash-erase-start
Debug: 78 198 command.c:155 script_debug(): command - stm32f1x.cpu configure -event gdb-flash-erase-start reset init
Debug: 79 201 command.c:155 script_debug(): command - stm32f1x.cpu cget -event gdb-flash-write-end
Debug: 80 204 command.c:155 script_debug(): command - stm32f1x.cpu configure -event gdb-flash-write-end reset halt
Debug: 81 208 command.c:155 script_debug(): command - stm32f1x.cpu cget -event gdb-attach
Debug: 82 210 command.c:155 script_debug(): command - stm32f1x.cpu configure -event gdb-attach halt 1000
Debug: 83 213 target.c:1657 handle_target_init_command(): Initializing targets...
Debug: 84 215 semihosting_common.c:109 semihosting_common_init():
Debug: 85 217 jlink.c:635 jlink_init(): Using libjaylink 0.4.0 (compiled with 0.4.0)
Debug: 86 235 jlink.c:514 jaylink_log_handler(): Found device (VID:PID = 1366:0101, bus:address = 001:018)
Warn : 87 238 jlink.c:514 jaylink_log_handler(): Failed to open device: LIBUSB_ERROR_NOT_SUPPORTED
Debug: 88 241 jlink.c:514 jaylink_log_handler(): Found 0 USB device(s)
Error: 89 242 jlink.c:688 jlink_init(): No J-Link device found
Debug: 90 244 command.c:544 run_command(): Command 'init' failed with error code -100
User : 91 247 command.c:608 command_run_line():
Debug: 92 249 target.c:2199 target_free_all_working_areas_restore(): freeing all working areas
根據調試日志分析,根本原因是 ?Windows 系統下 libusb 與 SEGGER 官方驅動沖突,導致 OpenOCD 無法正確訪問 J-Link 設備。以下是逐行解析和終極解決方案:
?錯誤日志關鍵點分析
?Found device (VID:PID = 1366:0101)
J-Link 設備已被系統識別,VID/PID 正確。
?Failed to open device: LIBUSB_ERROR_NOT_SUPPORTED
OpenOCD 的 libjaylink 庫嘗試通過 libusb 訪問 J-Link,但被系統拒絕。
?No J-Link device found
最終因驅動沖突導致設備訪問失敗。
?根本原因
?Windows 系統同時存在 libusb 和 SEGGER 驅動,OpenOCD 默認優先使用 libusb,但 SEGGER 官方驅動已獨占設備訪問權。
?OpenOCD 配置未完全禁用 libusb,導致底層庫(libjaylink)仍嘗試通過 libusb 通信。
但是嘗試了很多辦法 不知道如何使clion使用segger的驅動,也嘗試了禁用libusb在cfg文件內加入以下代碼 好像無效,
# 禁用 libusb,強制使用 SEGGER 驅動
jlink config use_libusb false
jlink usb 0
根據wiki 文檔 那segger+jlink就不是一個好的調試選項,因為切換后,因為他更換了jlink的驅動程序,使用keil iar 等程序時,就不太好用了,
那不如換調試器了,DAP或者st link
總結
至此,總算是能完成的搭建好 構建 debug的環境配置了,盡管jlink+openocd不是一個聯合調試的好選項。
參考
1.參考1 解決openocd無法識別jlink的問題LIBUSB_ERROR_NOT_SUPPORTED
2.解決libusb問題參考2
3.segger wiki原參考文檔