你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

Windows+VS Code+EIDE+STM32+gcc编程环境搭建

[复制链接]
攻城狮Melo 发布时间:2024-6-15 15:37
“ win+vscode+eide+gcc下的STM32编程环境搭建。”

微信图片_20240615160241.png

1.VSCode安装与配置
a.卸载
b.安装
c.git安装与配置
d.安装插件


2.Embedded IDE配置
a.安装EIDE插件
b.安装实用工具到EIDE
  • 安装arm-none-embed-gcc
  • 添加其bin文件夹到环境变量
  • 安装OpenOCD
  • 在vscode中安装Makefile Tools
3.Embedded IDE项目建立-stm32标准库
a.工程项目建立
b.在EIDE中使用标准库时的设置(无Makefile)
c.编译工程

4.一些问题的处理:
5.调试工程

01VSCode安装与配置
1.完全卸载之前安装过的VSCode
a.卸载软件
b.删除插件:找到C:\Users\用户名\.vscode`文件夹并删除它,以彻底清除已安装的插件。
c.删除用户信息和缓存信息:访问C:\Users\用户名\AppData\Roaming路径下,删除Code和Visual Studio Code文件夹,以彻底删除用户信息和缓存信息。

2.下载安装vscode

3.git安装与配置
安装git

微信图片_20240615160248.png

安装完成后,在开始菜单中找到并打开Git Bash

微信图片_20240615160250.png

在代码托管平台网站新建仓库,按照gitee等克隆下载的要求设置git

微信图片_20240615160253.png

在git bash中输入配置
  1. git config --global user.name 'kevin.kerry'
  2. git config --global user.email 'ct15762287606@outlook.com'
  3. //检查当前仓库状态
  4. git status
  5. //确认git配置
  6. git config user.name
  7. git config user.email
  8. //查看错误信息
  9. git commit -v
  10. //清理缓存
  11. git rm -r --cached .
  12. git add .
  13. git commit -m "Your commit message"
复制代码

微信图片_20240615160255.png

4.安装插件,如下图:

微信图片_20240615160258.jpg

微信图片_20240615160301.png

微信图片_20240615160303.png

微信图片_20240615160306.png

微信图片_20240615160309.png

微信图片_20240615160312.png

微信图片_20240615160317.png

微信图片_20240615160320.png

微信图片_20240615160322.png

微信图片_20240615160325.png

微信图片_20240615160328.png

微信图片_20240615160331.png

微信图片_20240615160334.png

微信图片_20240615160337.png

微信图片_20240615160341.png

微信图片_20240615160344.png

微信图片_20240615160347.png

微信图片_20240615160349.png

微信图片_20240615160404.png

02Embedded IDE配置
1.安装EIDE插件到VS Code
2.安装实用工具到EIDE环境
a.安装arm-none-embed-gcc工具链

微信图片_20240615160533.png

b.添加其bin文件夹到环境变量
  • 在EIDE中安装的gcc插件安装目录通常为:C:\Users\${用户名}\.eide\tools\gcc_arm\bin
  • "windows"键唤出搜索,输入编辑系统环境变量。找到Path进行编辑。添加上述gcc命令地址

微信图片_20240615160536.png

微信图片_20240615160540.png

在cmd/VSCode的集成终端输入 arm-none-eabi-gcc -v测试

  1. PS C:\Users\ct\Desktop\win-stm32-gcc> arm-none-eabi-gcc -v
复制代码

c.安装OpenOCD



d.在vscode中安装Makefile Tools

微信图片_20240615160554.png

  • 找到该工具的安装目录

微信图片_20240615160557.png

  • 将该Make工具的bin路径添加到环境变量,如前
  • 重启vscode,测试make -v命令是否正常
  1. PS C:\Users\ct\Desktop\win-stm32-gcc> make -v
  2. GNU Make 3.81
  3. Copyright (C) 2006  Free Software Foundation, Inc.
  4. This is free software; see the source for copying conditions.
  5. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
  6. PARTICULAR PURPOSE.


  7. This program built for i686-pc-msys
复制代码

03Embedded IDE项目建立-stm32标准库
1.工程项目建立
a.切换到EIDE环境,点击操作-新建项目-空项目图片

微信图片_20240615160602.png

b.选择一个合适的MCU项目类型,如Cortex-M项目

微信图片_20240615160605.png

c.点击右下角继续,跳转到工作区

微信图片_20240615160608.png

d.添加工程文件到工作区路径下。——标准库


微信图片_20240615160612.png

  • 将Libraries文件夹复制到根目录,删除Libraries文件夹中的无用文件,保留core_cm3.c,core_cm3.h,stm32f10x.h文件,startup文件选择固件库里的Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\TrueSTUDIO 里对应芯片的s文件,一定要TrueSTUDIO 目录里的启动文件。

  • 新建User目录,将STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template目录下的stm32f10x_conf.h,stm32f10x_it.c,stm32f10x_it.h,system_stm32f10x.c复制到User目录,并新建main.c文件。

  • 将固件库对应芯片下的(例如)Project\STM32F10x_StdPeriph_Template\TrueSTUDIO\STM3210C-EVAL\stm32_flash.ld拷贝到根目录并在EIDE中指定gcc所需的链接文件



微信图片_20240615160615.png

stm32_flash.ld
  1. /*
  2. *****************************************************************************
  3. **
  4. **  File        : stm32_flash.ld
  5. **
  6. **  Abstract    : Linker script for STM32F107VC Device with
  7. **                256KByte FLASH, 64KByte RAM
  8. **
  9. **                Set heap size, stack size and stack location according
  10. **                to application requirements.
  11. **
  12. **                Set memory bank area and size if external memory is used.
  13. **
  14. **  Target      : STMicroelectronics STM32
  15. **
  16. **  Environment : Atollic TrueSTUDIO(R)
  17. **
  18. **  Distribution: The file is distributed �as is,� without any warranty
  19. **                of any kind.
  20. **
  21. **  (c)Copyright Atollic AB.
  22. **  You may use this file as-is or modify it according to the needs of your
  23. **  project. Distribution of this file (unmodified or modified) is not
  24. **  permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the
  25. **  rights to distribute the assembled, compiled & linked contents of this
  26. **  file as part of an application binary file, provided that it is built
  27. **  using the Atollic TrueSTUDIO(R) toolchain.
  28. **
  29. *****************************************************************************
  30. */


  31. /* Entry Point */
  32. ENTRY(Reset_Handler)


  33. /* Highest address of the user mode stack */
  34. _estack = ORIGIN(RAM) + LENGTH(RAM);    /* end of 64K RAM */


  35. /* Generate a link error if heap and stack don't fit into RAM */
  36. _Min_Heap_Size = 0;      /* required amount of heap  */
  37. _Min_Stack_Size = 0x200; /* required amount of stack */


  38. /* Specify the memory areas */
  39. MEMORY
  40. {
  41. RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 20K
  42. FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 64K
  43. }


  44. /* Define output sections */
  45. SECTIONS
  46. {
  47.   /* The startup code goes first into FLASH */
  48.   .isr_vector :
  49.   {
  50.     . = ALIGN(4);
  51.     KEEP(*(.isr_vector)) /* Startup code */
  52.     . = ALIGN(4);
  53.   } >FLASH


  54.   /* The program code and other data goes into FLASH */
  55.   .text :
  56.   {
  57.     . = ALIGN(4);
  58.     *(.text)           /* .text sections (code) */
  59.     *(.text*)          /* .text* sections (code) */
  60.     *(.rodata)         /* .rodata sections (constants, strings, etc.) */
  61.     *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
  62.     *(.glue_7)         /* glue arm to thumb code */
  63.     *(.glue_7t)        /* glue thumb to arm code */


  64.     KEEP (*(.init))
  65.     KEEP (*(.fini))


  66.     . = ALIGN(4);
  67.     _etext = .;        /* define a global symbols at end of code */
  68.   } >FLASH




  69.    .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
  70.     .ARM : {
  71.     __exidx_start = .;
  72.       *(.ARM.exidx*)
  73.       __exidx_end = .;
  74.     } >FLASH


  75.   .ARM.attributes : { *(.ARM.attributes) } > FLASH


  76.   .preinit_array     :
  77.   {
  78.     PROVIDE_HIDDEN (__preinit_array_start = .);
  79.     KEEP (*(.preinit_array*))
  80.     PROVIDE_HIDDEN (__preinit_array_end = .);
  81.   } >FLASH
  82.   .init_array :
  83.   {
  84.     PROVIDE_HIDDEN (__init_array_start = .);
  85.     KEEP (*(SORT(.init_array.*)))
  86.     KEEP (*(.init_array*))
  87.     PROVIDE_HIDDEN (__init_array_end = .);
  88.   } >FLASH
  89.   .fini_array :
  90.   {
  91.     PROVIDE_HIDDEN (__fini_array_start = .);
  92.     KEEP (*(.fini_array*))
  93.     KEEP (*(SORT(.fini_array.*)))
  94.     PROVIDE_HIDDEN (__fini_array_end = .);
  95.   } >FLASH


  96.   /* used by the startup to initialize data */
  97.   _sidata = .;


  98.   /* Initialized data sections goes into RAM, load LMA copy after code */
  99.   .data : AT ( _sidata )
  100.   {
  101.     . = ALIGN(4);
  102.     _sdata = .;        /* create a global symbol at data start */
  103.     *(.data)           /* .data sections */
  104.     *(.data*)          /* .data* sections */


  105.     . = ALIGN(4);
  106.     _edata = .;        /* define a global symbol at data end */
  107.   } >RAM


  108.   /* Uninitialized data section */
  109.   . = ALIGN(4);
  110.   .bss :
  111.   {
  112.     /* This is used by the startup in order to initialize the .bss secion */
  113.     _sbss = .;         /* define a global symbol at bss start */
  114.     __bss_start__ = _sbss;
  115.     *(.bss)
  116.     *(.bss*)
  117.     *(COMMON)


  118.     . = ALIGN(4);
  119.     _ebss = .;         /* define a global symbol at bss end */
  120.     __bss_end__ = _ebss;
  121.   } >RAM


  122.   PROVIDE ( end = _ebss );
  123.   PROVIDE ( _end = _ebss );


  124.   /* User_heap_stack section, used to check that there is enough RAM left */
  125.   ._user_heap_stack :
  126.   {
  127.     . = ALIGN(4);
  128.     . = . + _Min_Heap_Size;
  129.     . = . + _Min_Stack_Size;
  130.     . = ALIGN(4);
  131.   } >RAM


  132.   /* MEMORY_bank1 section, code must be located here explicitly            */
  133.   /* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */
  134.   .memory_b1_text :
  135.   {
  136.     *(.mb1text)        /* .mb1text sections (code) */
  137.     *(.mb1text*)       /* .mb1text* sections (code)  */
  138.     *(.mb1rodata)      /* read-only data (constants) */
  139.     *(.mb1rodata*)
  140.   } >MEMORY_B1


  141.   /* Remove information from the standard libraries */
  142.   /DISCARD/ :
  143.   {
  144.     libc.a ( * )
  145.     libm.a ( * )
  146.     libgcc.a ( * )
  147.   }
  148. }
复制代码

  • 最终目录文件如下

微信图片_20240615160620.png

2.在EIDE中使用标准库时的设置(无Makefile)
a.切换到EIDE环境下

微信图片_20240615160623.png

微信图片_20240615160627.png

b.右键项目资源-点击添加源文件夹-选择普通文件夹,导入Keil工程文件及文件夹

微信图片_20240615160629.png

微信图片_20240615160632.png

微信图片_20240615160635.png

文件目录如下:

微信图片_20240615160638.png

c.点击+号安装CMSIS芯片支持包

微信图片_20240615160641.png

微信图片_20240615160652.png

微信图片_20240615160656.png

d.修改构建配置为gcc

微信图片_20240615160659.png

e.烧录配置为STLink/OpenOCD

微信图片_20240615160704.png

f.添加arm-none-embed-gcc安装目录下的/ARM/ARMCC/include和/ARM/ARMCC/include到包含目录。

微信图片_20240615160707.png

g.添加__C_ARM、STM32F10x_MD、USE_STDPERIPH_DRIVER到预处理宏定义

微信图片_20240615160710.png

h.此时编译可能会有下述报错
  1. C:\416: Error: registers may not be the same -- `strexb r0,r0,[r1]'
  2. C:\436: Error: registers may not be the same -- `strexh r0,r0,[r1]'
复制代码



打开src\Libraries\CMSIS\CoreSupport\core_cm3.c文件,将 736行,753行 中的 “=r"修改为”=&r",如下
  1. uint32_t __STREXB(uint8_t value, uint8_t *addr)
  2. {
  3.    uint32_t result=0;

  4.    __ASM volatile ("strexb %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );
  5.    return(result);
  6. }

  7. uint32_t __STREXH(uint16_t value, uint16_t *addr)
  8. {
  9.    uint32_t result=0;

  10.    __ASM volatile ("strexh %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );
  11.    return(result);
  12. }
复制代码

  • ctrl+shift+p,配置c_cpp_properties.json文件图片

微信图片_20240615160713.png
  1. {
  2.     "configurations": [
  3.         {
  4.             "name": "Win32",
  5.             "includePath": [
  6.                 "${workspaceFolder}/**",
  7.                 "C:/Keil_v5/ARM/ARMCC/include/**",
  8.                 "C:/Keil_v5/ARM/ARMCC/include/rw/**",
  9.                 "${workspaceFolder}/CORE"
  10.             ],
  11.             "defines": [
  12.                 "_DEBUG",
  13.                 "UNICODE",
  14.                 "_UNICODE",
  15.                 "STM32F10X_MD",
  16.                 "USE_STDPERIPH_DRIVER",
  17.                 "__CC_ARM"
  18.             ],
  19.             "cStandard": "c99"
  20.         }
  21.     ],
  22.     "version": 4

复制代码

3.编译工程

4.一些问题的处理:
a.编译成功,但下载后没有任何反应。
注意检查stm32_flash.ld文件内容
  1. /* Highest address of the user mode stack */
  2. _estack = ORIGIN(RAM) + LENGTH(RAM);    /* end of 64K RAM */
复制代码

注意对应芯片型号
  1. /* Specify the memory areas */
  2. MEMORY
  3. {
  4. RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 20K
  5. FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 64K
  6. }
复制代码

b.vscode提交(commit)代码时卡住

微信图片_20240615160715.png

微信图片_20240615160718.png

微信图片_20240615160722.png

设置的这里选择的默认选项影响了commit,当commit却不添加任何消息时,会默认生成一个文件来替代消息并提交,而服务器无法接受这样的消息。

c.结束

5.调试工程
a.添加launch.json
进入一个C文件,点击右上角按钮生成launch.json

微信图片_20240615160726.png

b.修改launch.json为下述内容
  1. {
  2.     // 使用 IntelliSense 了解相关属性。
  3.     // 悬停以查看现有属性的描述。
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5.     "version": "0.2.0",
  6.     "configurations": [
  7.         {
  8.             "name": "Cortex Debug",
  9.             "cwd": "${workspaceRoot}",  // 输出路径
  10.             "executable": "./build/Debug/${workspaceRootFolderName}.elf",    // 要调试的程序
  11.             "request": "launch",
  12.             "type": "cortex-debug",
  13.             "servertype": "openocd",    // 调试器选择
  14.             "device": "STM32F103C8",    // 使用J-link GDB Server时必须;其他GBD Server时可选(有可能帮助自动选择 SVD文件)。支持的设备见 https://www.segger.com/downloads/supported-devices.php
  15.             //"svdFile": "./STM32L15xC.svd",  // svd文件,有这个文件才能查看寄存器的值,每个单片机都不同。可以在以下地址找到 https://github.com/posborne/cmsis-svd下载
  16.             "interface": "swd",
  17.             "configFiles": [
  18.                 //"${workspaceRoot}/openocd.cfg",  // *可以通过加载该文件来配置
  19.                 "C:/Users/ct/.eide/tools/openocd_7a1adfbec_mingw32/share/openocd/scripts/interface/stlink-v2.cfg",
  20.                 "C:/Users/ct/.eide/tools/openocd_7a1adfbec_mingw32/share/openocd/scripts/target/stm32f1x.cfg"
  21.             ],
  22.             "runToEntryPoint": "false",
  23.             //"runToMain": false,
  24.             //"preLaunchTask": "Build",   // 在调试前预先执行的任务,此处是tasks.json中的
  25.             //"armToolchainPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin"    // 如果没有把 arm工具链路径添加到系统环境变量,则需要这条指令添加你的路径
  26.         }

  27. }
复制代码

上述最重要的是引入openocd的路径。


c.点击播放按钮进行调试

微信图片_20240615160729.png

6.结束



转载自: kevin
如有侵权请联系删除


微信图片_20240615160549.png
收藏 评论0 发布时间:2024-6-15 15:37

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版