一、概述
在平时编写STM32单片机代码时,我们经常会遇到某一个函数或某一个变量需要反复调试的情况,而常用的方法只能是在源码修改并下载至单片机调试。反复这样不仅麻烦,而且反复烧写单片机对其FLASH也有影响,因此就考虑编写一款小工具,可以实现:
1)通过串口控制单片机执行我们期望的函数,同时函数参数最大支持5个,其参数类型支持char、short、int、float及其无符号类型和相应的指针,不支持long及double。
2)对于含有对字符串及数组操作的函数,需要通过数组传值后,在调用函数时写入该变量地址才能实现对这些变量的操作。支持函数返回值得显示。
3)支持对全局变量进行任意的修改。
4)支持十进制与十六进制切换.
5)通讯超时自动重传或关闭串口。建议配合KEIL一起使用,效果更好。本软件使用C#编写,运行环境为NET 4.5。先让大家看看效果,感兴趣的话可以继续往下看:
-
上位机调试设置2.函数调用
3.全局变量的写入 4.通讯超时处理
二、上位机的处理 2.1 原理 在使用keil编译STM32后,我们会在.hex文件的同一个文件夹中发现一个.map文件。这个.map文件包含了源码中函数与全局变量的地址、大小、优化等信息。这里贴一个简化的.map文件给大家看一下:
Component: ARM Compiler 5.06 update 6 (build 750) Tool: armlink [4d35ed]
==============================================================================
Section Cross References
startup_stm32f103xe.o(STACK) refers (Special) to heapauxi.o(.text) for __use_two_region_memory
startup_stm32f103xe.o(HEAP) refers (Special) to heapauxi.o(.text) for __use_two_region_memory
startup_stm32f103xe.o(RESET) refers (Special) to heapauxi.o(.text) for __use_two_region_memory
startup_stm32f103xe.o(RESET) refers to startup_stm32f103xe.o(STACK) for __initial_sp
==============================================================================
Removing Unused input sections from the image.
Removing main.o(.rev16_text), (4 bytes).
Removing main.o(.revsh_text), (4 bytes).
Removing main.o(.rrx_text), (6 bytes).
Removing gpio.o(.rev16_text), (4 bytes).
Removing gpio.o(.revsh_text), (4 bytes).
384 unused section(s) (total 34104 bytes) removed from the image.
==============================================================================
Image Symbol Table
Local Symbols
Symbol Name Value Ov Type SizeObject(Section)
../Core/Src/gpio.c 0x00000000 Number 0gpio.o ABSOLUTE
../Core/Src/main.c 0x00000000 Number 0main.o ABSOLUTE
../Core/Src/stm32f1xx_hal_msp.c 0x00000000 Number 0stm32f1xx_hal_msp.o ABSOLUTE
../Core/Src/stm32f1xx_it.c 0x00000000 Number 0stm32f1xx_it.o ABSOLUTE
../Core/Src/system_stm32f1xx.c 0x00000000 Number 0system_stm32f1xx.o ABSOLUTE
../Core/Src/tim.c 0x00000000 Number 0tim.o ABSOLUTE
../Core/Src/usart.c 0x00000000 Number 0usart.o ABSOLUTE
../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c 0x00000000 Number 0stm32f1xx_hal.o ABSOLUTE
../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c 0x00000000 Number 0stm32f1xx_hal_cortex.o ABSOLUTE
../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c 0x00000000 Number 0stm32f1xx_hal_dma.o ABSOLUTE
../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c 0x00000000 Number 0stm32f1xx_hal_exti.o ABSOLUTE
Global Symbols
Symbol Name Value Ov Type SizeObject(Section)
BuildAttributes$THM_ISAv4$P$D$K$B$S$PE$A:L22UL41UL21$X:L11$S22US41US21$IEEE1$IW$USESV6$~STKCKD$USESV7$~SHL$OSPACE$ROPI$EBA8$UX$STANDARDLIB$REQ8$PRES8$EABIv2 0x00000000 Number 0anon$obj.o ABSOLUTE
__ARM_use_no_argv 0x00000000 Number 0main.o ABSOLUTE
__ARM_exceptions_init - Undefined Weak Reference
__alloca_initialize - Undefined Weak Reference
__arm_preinit_ - Undefined Weak Reference
__cpp_initialize__aeabi_ - Undefined Weak Reference
_terminate_alloc - Undefined Weak Reference
_terminate_user_alloc - Undefined Weak Reference
_terminateio - Undefined Weak Reference
__Vectors_Size 0x00000130 Number 0startup_stm32f103xe.o ABSOLUTE
__Vectors 0x08000000 Data 4startup_stm32f103xe.o(RESET)
__Vectors_End 0x08000130 Data 0startup_stm32f103xe.o(RESET)
__main 0x08000131 Thumb Code 8__main.o(!!!main)
in 0x2000001c Data 4main.o(.data)
uin 0x20000020 Data 4main.o(.data)
uwTick 0x20000024 Data 4stm32f1xx_hal.o(.data)
uwTickPrio 0x20000028 Data 4stm32f1xx_hal.o(.data)
uwTickFreq 0x2000002c Data 1stm32f1xx_hal.o(.data)
==============================================================================
Memory Map of the image
Image Entry point : 0x08000131
Load Region LR_IROM1 (Base: 0x08000000, Size: 0x00002de8, Max: 0x00080000, ABSOLUTE, COMPRESSED[0x00002da8])
Execution Region ER_IROM1 (Exec base: 0x08000000, Load base: 0x08000000, Size: 0x00002b94, Max: 0x00080000, ABSOLUTE)
Exec Addr Load Addr Size Type Attr Idx E Section Name Object
0x08000000 0x08000000 0x00000130 Data RO 3 RESET startup_stm32f103xe.o
0x08000130 0x08000130 0x00000008 Code RO 2955* !!!main c_w.l(__main.o)
0x08000138 0x08000138 0x00000034 Code RO 3143 !!!scatter c_w.l(__scatter.o)
0x0800016c 0x0800016c 0x0000003a Code RO 3141 !!dczerorl c_w.l(__dczerorl.o)
0x080001a6 0x080001a6 0x00000002 PAD
0x080001a8 0x080001a8 0x0000001c Code RO 3145 !!handler_zi c_w.l(__scatter_zi.o)
Execution Region RW_IRAM1 (Exec base: 0x20000000, Load base: 0x08002b94, Size: 0x00008bb0, Max: 0x00010000, ABSOLUTE, COMPRESSED[0x00000214])
Exec Addr Load Addr Size Type Attr Idx E Section Name Object
0x20000000 COMPRESSED 0x00000010 Data RW 18 .data test.o
0x20000010 COMPRESSED 0x00000014 Data RW 78 .data main.o
0x20000024 COMPRESSED 0x00000009 Data RW 1481 .data stm32f1xx_hal.o
0x2000002d COMPRESSED 0x00000003 PAD
0x20000030 COMPRESSED 0x00000004 Data RW 2832 .data system_stm32f1xx.o
0x20000034 COMPRESSED 0x00000004 PAD
0x20000038 COMPRESSED 0x0000021c Data RW 2910 .data debug_revice.o
==============================================================================
Image component sizes
Code (inc. data) RO Data RW Data ZI Data Debug Object Name
172 6 0 0 0 3002 debug_function.o
580 98 0 540 2104 3763 debug_revice.o
36 4 0 0 0 767 gpio.o
288 24 0 20 50 486558 main.o
64 26 304 0 32768 820 startup_stm32f103xe.o
152 32 0 9 0 5977 stm32f1xx_hal.o
304 22 0 0 0 29503 stm32f1xx_hal_cortex.o
510 10 0 0 0 1927 stm32f1xx_hal_dma.o
832 40 0 0 0 2092 stm32f1xx_hal_gpio.o
84 8 0 0 0 918 stm32f1xx_hal_msp.o
1784 110 0 0 0 6112 stm32f1xx_hal_rcc.o
1260 44 0 0 0 9974 stm32f1xx_hal_tim.o
160 22 0 0 0 2453 stm32f1xx_hal_tim_ex.o
1844 10 0 0 0 11460 stm32f1xx_hal_uart.o
66 12 0 0 0 4980 stm32f1xx_it.o
2 0 24 4 0 1155 system_stm32f1xx.o
134 10 0 16 0 6385 test.o
192 18 0 0 72 1702 tim.o
220 26 0 0 68 1778 usart.o
----------------------------------------------------------------------
8702 522 362 596 35068 581326 Object Totals
0 0 32 0 0 0 (incl. Generated)
18 0 2 7 6 0 (incl. Padding)
----------------------------------------------------------------------
Code (inc. data) RO Data RW Data ZI Data Debug Library Member Name
58 0 0 0 0 0 __dczerorl.o
8 0 0 0 0 68 __main.o
0 0 0 0 0 0 __rtentry.o
12 0 0 0 0 0 __rtentry2.o
6 0 0 0 0 0 __rtentry4.o