体验用VS Code 里 EIDE 进行芯片开发的快乐~ 开源、免费、更简便、支
优采云 发布时间: 2022-05-01 16:09体验用VS Code 里 EIDE 进行芯片开发的快乐~ 开源、免费、更简便、支
出品 21ic论坛yang377156216
网站:
整体概览
作为 ARM Cotex M 系列内核的 32 位单片机开发者,在平时工作中经常会因为芯片平台的不同而去切换各种开发环境,幸好市面上有着各式各样的工具能够满足开发攻城狮们的需求,大体上这些开发环境可以分为几大类:较为通用的集成环境(编辑、编译、链接、下载和调试等功能合为一体),比较有代表性的有 Keil MDK、IAR EWARM、SEGGER Embedded Studio 、Mbed Studio 等等;厂家自制的专用 IDE ,主流的代表作有 STM32CubeIDE 、MCUXpresso IDE 、RT-Thread Studio 、MPLAB IDE 等等;还有一种 ”混搭版“ 的风格,开发者可以在 Eclipse 、VS Code 、Visual Studio 等工具中搭配 ARM GCC 编译工具链进行项目软件开发。在眼花缭乱的工具中,根据是否免费、编辑功能体验度是否高、上手与熟悉难度是否适宜等因素综合考虑,今天选择了一种开源、免费且更加简便的 VS Code +EIDE + GCC + J-Link 方式进行 MM32F0144C6P 芯片的软件开发。接下来的内容整体可分为以下几点:
一、单片机软件开发流程和其中一些知识点
一个 ARM Cortex M 系列内核的 MCU 软件开发者需要清楚完成一个软件项目的大致流程是需经过以下几步的:
编写代码。通常是 C/C++ 语言,但是在考究执行效率的时候就得用到 ASM 汇编,讲究面向对象的结构化编程时甚至用上 Python、Lua 等其它高级语言。
将各种语言进行编译和链接最终生成机器二进制文件。相信绝大部分人都亲身体验过这一步,在很多集成环境中就是一键 Build ,然后默认生成 hex 格式烧录文件。
烧录和调试。一般烧录方式分为 ISP 、ICP 和 IAP ,根据 MCU 的不同选择的烧录工具也不尽相同。有些厂商,例如国产的 STC,只支持串口方式的 ISP ,且配套的在线仿真调试器也是基于串口形式的;而 ARM 内核的大部分 MCU 都支持 JTAG/SWD 接口形式的 ICP 方式,配套的调试工具可以是 J-Link、U-Link、CMSIS DAP-Link 和 ST-Link 等等,也几乎都有配套的上位机给予支持。
运行程序。基本经过一番调试后,最终能够得到满足功能需求的代码了,此时终于可以结案交付了,当然,后面可能还会面对大大小小的 Bug ,还得继续 “升级打怪”。
以上内容简述了整个软件开发的流程,其中涉及到的编译和链接这个知识点比较重要。为了对这些名词有更深的理解,特意查了资料。通俗讲,编译器会将 C 程序转换成一种机器能理解的符号形式的汇编语言程序,包括了各种伪指令和符号表,然后汇编器将这些代码转换成目标文件,包括了机器语言指令、数据和指令正确放入内存所需要的信息,最后由系统程序(链接编译器) 将各个独立汇编的机器语言程序组合起来并且解释所有未定义的标记,直到生成可执行文件。这其中也会涉及到很多文件,比如后缀名为 .c / .s / .o / .a / .lib / .elf / .axf / .bin / .hex 等等文件,有些是中间文件,另外一些是结果可执行文件。以下这幅图用于帮助理解这个过程:
既然编译链接这个过程有着举足轻重的地位,那市面上的又有哪些主流工具可供选择呢?据了解,目前针对于 ARM平台的主流编译器主要有以下一些:
用到比较多的 KEIL AC5/AC6 是闭源和收费的,编译速度在大型项目上 AC6 非常有优势,它们都具备多种优化等级可调,编出来的代码大小较小且运行更为安全,另外也都可以在 ARM 官网单独下载,比较适合用于产品开发中;IAR 的 ICC 编译器也更加高性能而被广泛使用,在很多 benchmark 跑分测试中同颗芯片的运行结果效率都更高些,且编出来的代码大小也适中;Keil MDK、IAR 等工具都是收费的,在使用中很可能牵扯到一定的版权问题,而 GCC(GNU Compiler Collection)作为GNU计划的一部分,**是完全免费的,这就是最大的优势**,尽管使用 GCC 是需要付出一定代价的——对编译后造成的不良后果负全责(比如编译出来的代码量非常大,程序跑飞从而致使板级器件烧毁,系统死机崩溃导致丢失关键数据之类的情况)。
这里特别感谢硬汉大哥和傻孩子大哥针对各家编译器做出的实测比较和探讨,可参见:
二、VS Code 优势和 EIDE 插件介绍
这里选择 ARM GCC 交叉编译工具链作为最重要的一环,除了看重它开源、免费、资料多等优势以外,更重要的是可以跨平台。除了“内核”,那再来聊聊为什么选择 VS Code 作为外壳吧。
考虑 Windows 环境下能够使用 Eclipse IDE for C/C++ Developers 来搭建 ARM 开发环境,但是整个 JAVA 环境占用了太大的 PC 资源,完整地安装下来会非常臃肿,性价比不高,所以转而会考虑一个跨平台且非常流行的编辑器 VS Code ,其特点有:
等等……
好的开发环境就像一把好刀,能让我们开发速度达到事半功倍,主流的就是对的,下图显示了 VS Code 的受欢迎程度:
VS Code 里面的 EIDE 插件是个什么东西?很多人可能没有接触过,不知道它能让 KEIL 工程导入到 VS Code 中有多方便。下面来简单介绍。
EIDE是 keil-assistant 插件的升级版,它们同属一个开发团队,这是一款适用于 8051/STM8/Cortex-M/RISC-V 的单片机开发环境。能够在 VS Code 上提供 8051, STM8, Cortex-M, RISC-V 项目的开发, 编译, 烧录功能。通俗点说,它就是那个披上 VS Code 外衣然后可以将 GCC 工具、各种调试工具集大成的 “后来者”,有多种实用功能,能让开发工作变得更加简单高效。更多相关资源可以查看官方提供的文档:#/ 。
三、准备资源
硬件资源如下:
软件资源如下:
提示:本文中的展示基于 WIN10 64 位 PC 系统,用户需要根据自己的电脑系统下载对应版本的资源。既可通过上述超链接获取,也可直接使用压缩包内的,为更好对照文中步骤实现环境搭建,建议尽量使用附件提供的资源包。工具软件的安装可以根据自己的习惯自定义路径,也可以一直 next 选择默认模式,记得将 gcc-arm-none-eabi 工具安装路径加入系统环境变量中,保险起见其它几个也可以一并添加。
由于官方不提供 MM32F0144C6P GCC 启动文件对应的链接文件,那自己动手制作,思路是找到 STM32F030x 相关的文件来做修改,因为它们两者外设资源上极为相似。要注意的是,需要根据 MM32F0144C6P 实际的中断向量表去做修改。以下为修改好的 startup_mm32f0140_gcc.s 文件:
/**<br /> ******************************************************************************<br /> * @file startup_mm32f0140_gcc.s<br /> * @author <br /> * @brief MM32F014x devices vector table for GCC toolchain.<br /> * This module performs:<br /> * - Set the initial SP<br /> * - Set the initial PC == Reset_Handler,<br /> * - Set the vector table entries with the exceptions ISR address<br /> * - Branches to main in the C library (which eventually<br /> * calls main()).<br /> * After Reset the Cortex-M0 processor is in Thread mode,<br /> * priority is Privileged, and the Stack is set to Main.<br /> *<br /> ******************************************************************************<br /> */<br /><br /><br /> .syntax unified<br /> .cpu cortex-m0<br /> .fpu softvfp<br /> .thumb<br /><br /><br />.global g_pfnVectors<br />.global Default_Handler<br /><br /><br />/* start address for the initialization values of the .data section.<br />defined in linker script */<br />.word _sidata<br />/* start address for the .data section. defined in linker script */<br />.word _sdata<br />/* end address for the .data section. defined in linker script */<br />.word _edata<br />/* start address for the .bss section. defined in linker script */<br />.word _sbss<br />/* end address for the .bss section. defined in linker script */<br />.word _ebss<br /><br /><br />/**<br /> * @brief This is the code that gets called when the processor first<br /> * starts execution following a reset event. Only the absolutely<br /> * necessary set is performed, after which the application<br /> * supplied main() routine is called.<br /> * @param None<br /> * @retval : None<br />*/<br /><br /><br /> .section .text.Reset_Handler<br /> .weak Reset_Handler<br /> .type Reset_Handler, %function<br />Reset_Handler:<br /> ldr r0, =_estack<br /> mov sp, r0 /* set stack pointer */<br /><br /><br />/* Copy the data segment initializers from flash to SRAM */<br /> ldr r0, =_sdata<br /> ldr r1, =_edata<br /> ldr r2, =_sidata<br /> movs r3, #0<br /> b LoopCopyDataInit<br /><br /><br />CopyDataInit:<br /> ldr r4, [r2, r3]<br /> str r4, [r0, r3]<br /> adds r3, r3, #4<br /><br /><br />LoopCopyDataInit:<br /> adds r4, r0, r3<br /> cmp r4, r1<br /> bcc CopyDataInit<br /> <br />/* Zero fill the bss segment. */<br /> ldr r2, =_sbss<br /> ldr r4, =_ebss<br /> movs r3, #0<br /> b LoopFillZerobss<br /><br /><br />FillZerobss:<br /> str r3, [r2]<br /> adds r2, r2, #4<br /><br /><br />LoopFillZerobss:<br /> cmp r2, r4<br /> bcc FillZerobss<br /><br /><br />/* Call the clock system intitialization function.*/<br /> bl SystemInit<br />/* Call static constructors */<br /> bl __libc_init_array<br />/* Call the application's entry point.*/<br /> bl main<br /><br /><br />LoopForever:<br /> b LoopForever<br /><br /><br /><br /><br />.size Reset_Handler, .-Reset_Handler<br /><br /><br />/**<br /> * @brief This is the code that gets called when the processor receives an<br /> * unexpected interrupt. This simply enters an infinite loop, preserving<br /> * the system state for examination by a debugger.<br /> *<br /> * @param None<br /> * @retval : None<br />*/<br /> .section .text.Default_Handler,"ax",%progbits<br />Default_Handler:<br />Infinite_Loop:<br /> b Infinite_Loop<br /> .size Default_Handler, .-Default_Handler<br />/******************************************************************************<br />** The minimal vector table for a Cortex M0. Note that the proper constructs<br />* must be placed on this to ensure that it ends up at physical address<br />* 0x0000.0000.<br />*******************************************************************************/<br /> .section .isr_vector,"a",%progbits<br /> .type g_pfnVectors, %object<br /> .size g_pfnVectors, .-g_pfnVectors<br /><br /><br /><br /><br />g_pfnVectors:<br /> .word _estack<br /> .word Reset_Handler<br /> .word NMI_Handler<br /> .word HardFault_Handler<br /> .word 0<br /> .word 0<br /> .word 0<br /> .word 0<br /> .word 0<br /> .word 0<br /> .word 0<br /> .word SVC_Handler<br /> .word 0<br /> .word 0<br /> .word PendSV_Handler<br /> .word SysTick_Handler<br /> .word WWDG_IRQHandler /* Window WatchDog */<br /> .word PVD_IRQHandler /* PVD through EXTI Line detect */<br /> .word MIPI_IRQHandler /* MIPI */<br /> .word FLASH_IRQHandler /* FLASH */<br /> .word RCC_IRQHandler /* RCC */<br /> .word EXTI0_1_IRQHandler /* EXTI Line 0 and 1 */<br /> .word EXTI2_3_IRQHandler /* EXTI Line 2 and 3 */<br /> .word EXTI4_15_IRQHandler /* EXTI Line 4 to 15 */<br /> .word HWDIV_IRQHandler /* HWDIV */<br /> .word DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */<br /> .word DMA1_Channel2_3_IRQHandler /* DMA1 Channel 2 and Channel 3 */<br /> .word DMA1_Channel4_5_IRQHandler /* DMA1 Channel 4 and Channel 5 */<br /> .word ADC1_COMP_IRQHandler /* ADC1 & COMP */<br /> .word TIM1_BRK_UP_TRG_COM_IRQHandler /* TIM1 Break, Update, Trigger and Commutation */<br /> .word TIM1_CC_IRQHandler /* TIM1 Capture Compare */<br /> .word TIM2_IRQHandler /* TIM2 */<br /> .word TIM3_IRQHandler /* TIM3 */<br /> .word 0 /* Reserved */<br /> .word 0 /* Reserved */<br /> .word TIM14_IRQHandler /* TIM14 */<br /> .word 0 /* Reserved */<br /> .word TIM16_IRQHandler /* TIM16 */<br /> .word TIM17_IRQHandler /* TIM17 */<br /> .word I2C1_IRQHandler /* I2C1 */<br /> .word 0 /* Reserved */<br /> .word SPI1_IRQHandler /* SPI1 */<br /> .word SPI2_IRQHandler /* SPI2 */<br /> .word UART1_IRQHandler /* UART1 */<br /> .word UART2_IRQHandler /* UART2 */<br /> .word UART3_IRQHandler /* UART3 */<br /> .word FLEX_CAN_IRQHandler /* FLEX_CAN */<br /> .word 0 /* Reserved */<br /><br /><br /><br /><br />/*******************************************************************************<br />** Provide weak aliases for each Exception handler to the Default_Handler.<br />* As they are weak aliases, any function with the same name will override<br />* this definition.<br />********************************************************************************/<br /><br /><br /> .weak NMI_Handler<br /> .thumb_set NMI_Handler,Default_Handler<br /><br /><br /> .weak HardFault_Handler<br /> .thumb_set HardFault_Handler,Default_Handler<br /><br /><br /> .weak SVC_Handler<br /> .thumb_set SVC_Handler,Default_Handler<br /><br /><br /> .weak PendSV_Handler<br /> .thumb_set PendSV_Handler,Default_Handler<br /><br /><br /> .weak SysTick_Handler<br /> .thumb_set SysTick_Handler,Default_Handler<br /><br /><br /> .weak WWDG_IRQHandler<br /> .thumb_set WWDG_IRQHandler,Default_Handler<br /><br /><br /> .weak PVD_IRQHandler<br /> .thumb_set PVD_IRQHandler,Default_Handler<br /><br /><br /> .weak MIPI_IRQHandler<br /> .thumb_set MIPI_IRQHandler,Default_Handler<br /><br /><br /> .weak FLASH_IRQHandler<br /> .thumb_set FLASH_IRQHandler,Default_Handler<br /><br /><br /> .weak RCC_IRQHandler<br /> .thumb_set RCC_IRQHandler,Default_Handler<br /><br /><br /> .weak EXTI0_1_IRQHandler<br /> .thumb_set EXTI0_1_IRQHandler,Default_Handler<br /><br /><br /> .weak EXTI2_3_IRQHandler<br /> .thumb_set EXTI2_3_IRQHandler,Default_Handler<br /><br /><br /> .weak EXTI4_15_IRQHandler<br /> .thumb_set EXTI4_15_IRQHandler,Default_Handler<br /><br /><br /> .weak HWDIV_IRQHandler<br /> .thumb_set HWDIV_IRQHandler,Default_Handler<br /> <br /> .weak DMA1_Channel1_IRQHandler<br /> .thumb_set DMA1_Channel1_IRQHandler,Default_Handler<br /><br /><br /> .weak DMA1_Channel2_3_IRQHandler<br /> .thumb_set DMA1_Channel2_3_IRQHandler,Default_Handler<br /><br /><br /> .weak DMA1_Channel4_5_IRQHandler<br /> .thumb_set DMA1_Channel4_5_IRQHandler,Default_Handler<br /><br /><br /> .weak ADC1_COMP_IRQHandler<br /> .thumb_set ADC1_COMP_IRQHandler,Default_Handler<br /><br /><br /> .weak TIM1_BRK_UP_TRG_COM_IRQHandler<br /> .thumb_set TIM1_BRK_UP_TRG_COM_IRQHandler,Default_Handler<br /><br /><br /> .weak TIM1_CC_IRQHandler<br /> .thumb_set TIM1_CC_IRQHandler,Default_Handler<br /><br /><br /> .weak TIM2_IRQHandler<br /> .thumb_set TIM2_IRQHandler,Default_Handler<br /><br /><br /> .weak TIM3_IRQHandler<br /> .thumb_set TIM3_IRQHandler,Default_Handler<br /><br /><br /> .weak TIM14_IRQHandler<br /> .thumb_set TIM14_IRQHandler,Default_Handler<br /><br /><br /> .weak TIM16_IRQHandler<br /> .thumb_set TIM16_IRQHandler,Default_Handler<br /><br /><br /> .weak TIM17_IRQHandler<br /> .thumb_set TIM17_IRQHandler,Default_Handler<br /><br /><br /> .weak I2C1_IRQHandler<br /> .thumb_set I2C1_IRQHandler,Default_Handler<br /><br /><br /> .weak SPI1_IRQHandler<br /> .thumb_set SPI1_IRQHandler,Default_Handler<br /><br /><br /> .weak SPI2_IRQHandler<br /> .thumb_set SPI2_IRQHandler,Default_Handler<br /> <br /> .weak UART1_IRQHandler<br /> .thumb_set UART1_IRQHandler,Default_Handler<br /> <br /> .weak UART2_IRQHandler<br /> .thumb_set UART2_IRQHandler,Default_Handler<br /><br /><br /> .weak UART3_IRQHandler<br /> .thumb_set UART3_IRQHandler,Default_Handler<br /> <br /> .weak FLEX_CAN_IRQHandler<br /> .thumb_set FLEX_CAN_IRQHandler,Default_Handler<br />
以下为修改好的 MM32F0144C6P_FLASH 文件:
/*<br />*****************************************************************************<br />**<br />** File : mm32_flash.ld<br />**<br />** Abstract : Linker script for MM32F0144C6P Device with<br />** 64KByte FLASH, 16KByte RAM<br />**<br />** Set heap size, stack size and stack location according<br />** to application requirements.<br />**<br />** Set memory bank area and size if external memory is used.<br />**<br />** Target : MM32<br />**<br />** Environment : VScode<br />**<br />** Distribution: The file is distributed “as is,” without any warranty<br />** of any kind.<br />*****************************************************************************<br />*/<br /><br /><br />/* Entry Point */<br />ENTRY(Reset_Handler)<br /><br /><br />/* Highest address of the user mode stack */<br />_estack = 0x20002000; /* end of 16K RAM */<br /><br /><br />/* Generate a link error if heap and stack don't fit into RAM */<br />_Min_Heap_Size = 0x200; /* required amount of heap */<br />_Min_Stack_Size = 0x400; /* required amount of stack */<br /><br /><br />/* Specify the memory areas */<br />MEMORY<br />{<br /> FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K<br /> RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K<br />}<br /><br /><br />/* Define output sections */<br />SECTIONS<br />{<br /> /* The startup code goes first into FLASH */<br /> .isr_vector :<br /> {<br /> . = ALIGN(4);<br /> KEEP(*(.isr_vector)) /* Startup code */<br /> . = ALIGN(4);<br /> } >FLASH<br /><br /><br /> /* The program code and other data goes into FLASH */<br /> .text :<br /> {<br /> . = ALIGN(4);<br /> *(.text) /* .text sections (code) */<br /> *(.text*) /* .text* sections (code) */<br /> *(.glue_7) /* glue arm to thumb code */<br /> *(.glue_7t) /* glue thumb to arm code */<br /> *(.eh_frame)<br /><br /><br /> KEEP (*(.init))<br /> KEEP (*(.fini))<br /><br /><br /> . = ALIGN(4);<br /> _etext = .; /* define a global symbols at end of code */<br /> } >FLASH<br /><br /><br /> /* Constant data goes into FLASH */<br /> .rodata :<br /> {<br /> . = ALIGN(4);<br /> *(.rodata) /* .rodata sections (constants, strings, etc.) */<br /> *(.rodata*) /* .rodata* sections (constants, strings, etc.) */<br /> . = ALIGN(4);<br /> } >FLASH<br /><br /><br /> .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH<br /> .ARM : {<br /> __exidx_start = .;<br /> *(.ARM.exidx*)<br /> __exidx_end = .;<br /> } >FLASH<br /><br /><br /> .preinit_array :<br /> {<br /> PROVIDE_HIDDEN (__preinit_array_start = .);<br /> KEEP (*(.preinit_array*))<br /> PROVIDE_HIDDEN (__preinit_array_end = .);<br /> } >FLASH<br /> .init_array :<br /> {<br /> PROVIDE_HIDDEN (__init_array_start = .);<br /> KEEP (*(SORT(.init_array.*)))<br /> KEEP (*(.init_array*))<br /> PROVIDE_HIDDEN (__init_array_end = .);<br /> } >FLASH<br /> .fini_array :<br /> {<br /> PROVIDE_HIDDEN (__fini_array_start = .);<br /> KEEP (*(SORT(.fini_array.*)))<br /> KEEP (*(.fini_array*))<br /> PROVIDE_HIDDEN (__fini_array_end = .);<br /> } >FLASH<br /><br /><br /> /* used by the startup to initialize data */<br /> _sidata = LOADADDR(.data);<br /><br /><br /> /* Initialized data sections goes into RAM, load LMA copy after code */<br /> .data : <br /> {<br /> . = ALIGN(4);<br /> _sdata = .; /* create a global symbol at data start */<br /> *(.data) /* .data sections */<br /> *(.data*) /* .data* sections */<br /><br /><br /> . = ALIGN(4);<br /> _edata = .; /* define a global symbol at data end */<br /> } >RAM AT> FLASH<br /><br /><br /> /* Uninitialized data section */<br /> . = ALIGN(4);<br /> .bss :<br /> {<br /> /* This is used by the startup in order to initialize the .bss secion */<br /> _sbss = .; /* define a global symbol at bss start */<br /> __bss_start__ = _sbss;<br /> *(.bss)<br /> *(.bss*)<br /> *(COMMON)<br /><br /><br /> . = ALIGN(4);<br /> _ebss = .; /* define a global symbol at bss end */<br /> __bss_end__ = _ebss;<br /> } >RAM<br /><br /><br /> /* User_heap_stack section, used to check that there is enough RAM left */<br /> ._user_heap_stack :<br /> {<br /> . = ALIGN(4);<br /> PROVIDE ( end = . );<br /> PROVIDE ( _end = . );<br /> . = . + _Min_Heap_Size;<br /> . = . + _Min_Stack_Size;<br /> . = ALIGN(4);<br /> } >RAM<br /><br /><br /> /* Remove information from the standard libraries */<br /> /DISCARD/ :<br /> {<br /> libc.a ( * )<br /> libm.a ( * )<br /> libgcc.a ( * )<br /> }<br /><br /><br /> .ARM.attributes 0 : { *(.ARM.attributes) }<br />}
四、着手搭建环境
有了前面的准备就可以开始配置整合开发环境了。限于篇幅,这里略过在 VS Code 中下载安装 EIDE 和 Cortex-Debug(可以让 VS Code + EIDE 环境具备调试功能,非必须,可以使用 O-Zone 调试) 插件,可点击参考超链接文章说明,这里重点说明一下关键配置。
Cortex-Debug 插件的配置页需要配置两个:Arm Toolchain Path 和 Jlink GDBserver Path。如果 GNU for Arm和 Jlink GDBserver Path 已经加入到系统环境变量中,就可以不用配置了,另外如果在 EIDE 中已经配置了 GCC 工具链和 J-Link 驱动安装路径的话,那在这也可以不再配置。而重点的 EIDE 插件配置信息需要根据自己安装情况来填写,主要包括以下几个内容:
以下为我的配置情况:
1. 按照上述配置好后基本就可以使用 EIDE 和 Cortex-Debug 插件了
2. 首先将准备好的 KEIL 工程导入到 EIDE 中,建立好一个位于 VS Code 中的 EIDE KEIL 工程,名为 KEILPRJ.code-workspace,该文件后面可以直接在 EIDE 中打开 类似 KEIL 的 .uvprojx 工程描述文件
3. 然后在项目资源包中将原先 KEIL 的启动文件替换为 GCC 平台的
4. 添加 MM32F0140_DFP 芯片支持包并且选择对应芯片 MM32F0144C6P
5. Build 配置选择 GCC ,链接脚本选择准备好的 MM32F0144C6P_FLASH.ld 所在路径
6. 烧录器选项选择 Jlink,对应好芯片名称
7. 项目属性中的包含目录将之前 KEIL 平台相关的替换为 GCC 平台的即可,不动也没关系因为就差了个 .s 文件
8. 其它按照默认配置即可,最后类似在 KEIL 中操作一样,一键编译和烧录
9. 进而转到 Cortex-Debug 中进行调试
实际调试过程中,遇到 2 个问题:
第一个是由于意识里认为 MM32F0144C6P 芯片的 RAM 大小为 16KB ,在 .ld 链接文件中填写的也自然是 16KB,编译烧录后发现 LED 并未闪烁且串口无打印输出,再使用 Cortex-Debug 调试,发现只要一运行 bl SystemInit 就会跳到 HardFault 里面去,心里面慌了,会是 .s 没做好?再调试也未定位到问题点,于是转而使用更加出色的 O-Zone 工具去调试,结合丰富的资源显示 ,好不容易定位到一 PUSH 就会触发错误,想来应该是栈大小和地址的问题,最后查到原来使用的芯片应该是 8KB 的 RAM 才对。.s 文件并无问题,改过 .ld 文件后,解决。
第二个是由于官方 lib samples 里面的串口重定向并未考虑到 GCC 平台的使用,未适配好导致 printf 打印功能失效,于是使用了自定义 printf 方法改造了程序,解决。
void vprint(const char *fmt, va_list argp){<br /> char string[200];<br /> if(0 < vsprintf(string,fmt,argp)) // build string<br /> {<br /> for (int i = 0; i < strlen(string); i++) {<br /> while ((UART1->CSR & UART_IT_TXIEN) == 0)<br /> ; // The loop is sent until it is finished<br /> UART1->TDR = (u8)string[i];<br /> }<br /> }<br />}<br /><br /><br />void my_printf(const char *fmt, ...) // custom printf() function{<br /> va_list argp;<br /> va_start(argp, fmt);<br /> vprint(fmt, argp);<br /> va_end(argp);<br />}
操作到这里已经可以尽情享受 VS Code 开发 MM32 MCU 的快乐了,既不用写 makefile ,还能保持原来在 KEIL IDE 中的一些打包工程的操作习惯。另外,依托于工具强大的插件库,我们还可以在 VS Code 中安装配置 Astyle 格式化工具,使得代码结构整洁美观并且规范;还可以在 VS Code 部署好本地 Git 仓,利用 Github 进行工程迭代管理,使得项目开发变得井然有序;还可以使用 Settings Sync 插件进行多台 PC 机上的 VS Code 配置同步,再也不用担心更换电脑后重新又得重新安装配置之前一直使用的那么多插件了。总而言之,VS Code 真香!
五、编译、烧录及调试演示视频
实验中导入的示例工程为自己移植的 nr micro shell 组件测试代码,可以作为 Template 使用。编译烧录好程序后,开发板的串口会输出调试信息, LED 灯快速闪烁一会儿然后停止,此时可以使用 EIDE 中的串口工具终端与开发板进行指令交互,根据 LED 的亮灭状态来看终端给开发板发出的命令是否有被正确执行。