目錄
前言
準備工作
Daplink連接
調試配置(cortex-debug)
燒錄配置
坑點
前言
? ? ? 昨天有驚無險的構建了一個簡單的閃燈程序,今天來挑戰下調試+燒錄,看看有什么坑。
準備工作
- OpenOCD下載:openocd-xpack
- Daplink調試器
Daplink連接
- 接線,如下
Daplink? ? ? ? ?單片機
TCK/CK ----- SWCLK
TMS/IO? ----- SWIO
3.3v? ? ? ? ----- 3.3v
GND? ? ? ?----- GND
- 插上電腦,直接會識別(我的是windows10)
調試配置(cortex-debug)
- 需要提前安裝好cortex-debug插件,并添加launch.json配置文件,這里需要注意.cfg要配置成符合自己單片機的,文件在openocd的“openocd\scripts\target”路徑下
- 插件不會幫助你安裝openocd需要自己下載,并配置環境變量,配置完之后,務必關閉VSCode,再重新打開
- 配置完成后,就可以直接點擊上圖中左上方的運行按鈕,程序會自動燒錄到單片機中,并可執行斷點調試。
燒錄配置
- 如果不需要調試,那直接配如下所述的就可以了
- 點擊開發工具右上方的燒錄按鈕,即可完成燒錄
異常解決
- 今天相對比較順利,遇到兩個問題,一是VSCode始終讀不到配置好的openocd的環境變量,重啟多次后可以了;二是調式報錯以及無法燒錄程序,這個問題比較刁鉆,多種報錯信息:
Output radix now set to decimal 10, hex a, octal 12.
Input radix now set to decimal 10, hex a, octal 12.
0x0800025e in HAL_MspInit () at ./Core/Src/stm32f1xx_hal_msp.c:77
77 __HAL_AFIO_REMAP_SWJ_DISABLE();
Program stopped, probably due to a reset and/or halt issued by debugger
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x0800016c msp: 0x20000660
[stm32f1x.cpu] halted due to breakpoint, current mode: Thread
xPSR: 0x01000000 pc: 0x08000c34 msp: 0x20005000
Error connecting DP: cannot read IDRProgramstopped.
0x00000000 in ?? ()
Error: Error connecting DP: cannot read IDR
Error: [stm32f1x.cpu] Polling failed, trying to reexamine
Error: [stm32f1x.cpu] Examination failed
Error: [stm32f1x.cpu] Examination failed, GDB will be halted. Polling again in 300ms
Error: Error connecting DP: cannot read IDR
Error: Failed to read memory and, additionally, failed to find out where
Error: Error connecting DP: cannot read IDR
解決:
原來是STM32CubeMx生成代碼的時候,有一行禁止調試的代碼:
注釋掉!即可!重新構建。
- 但我們之前的程序已經燒錄,怎么辦?考研手速的時候到了!用單片機上的Reset按鈕,按住,點燒錄,看到成功連接OCD后立刻松開,便可以正常燒錄了。
- 由于這行代碼是STM32CubeMx生成的,下次有什么引腳變化,還得用他生成,那么就需要直接一下,讓它不要生成,如下:
后記
? ? ? 又是有驚無險的一通瞎操作,接下來要開始進入此次旅程的核心 → 用Make編譯出bin文件,并燒錄到單片機中。