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

【经验分享】STM32开发项目:STM32CubeIDE中如何手动修改MCU的型号

[复制链接]
STMCU小助手 发布时间:2022-4-14 10:31
背景介绍
笔者在开发的时候,习惯应用自己的一套函数扩展库与工程模板,因此每次开始新的项目开发时,就会将上一个项目工程直接复制一份,在其基础上进行开发。当新项目与原项目的单片机型号不一样的时候,就涉及到如何将工程中的单片机型号的修改过来的问题。

本文将以工程中MCU型号从STM32F103C8Tx改为STM32F103VCTx为例,说明一下具体的操作步骤。

操作步骤
修改工程文件.cproject

用文本编辑器(NotePad++)打开.cproject工程文件,采用查找替换的方式,将工程文件中的原单片机型号“STM32F103C8”替换成“STM32F103VC”并保存。.project工程文件中没有需要修改的内容。

删除部分工程文件
删除XXX.elf.launch文件与XXX.map文件,这两个文件默认在工程目录文件夹中,在设置Run/Debug Configurations的时候会自动重新生成。

替换启动文件
将原来型号的启动文件(XXX.s文件)替换为startup_stm32f103vctx.s,这个启动文件可以通过在STM32CubeIDE中新建一个MCU型号为STM32F103VCTx的工程并自动生成代码而获得。也可以在源码中同时保留这些文件,而在Makefile中指定调用的文件名。

替换LinkerScript文件
将原来型号的链接文件(XXX.ld文件)替换为STM32F103VCTX_FLASH.ld,这个链接文件可以通过在STM32CubeIDE中新建一个MCU型号为STM32F103VCTx的工程并自动生成代码而获得。也可以在源码中同时保留这些文件,而在Makefile中指定调用的文件名。

当然也可以直接修改原来的链接文件:

  1. /* Highest address of the user mode stack */
  2. _estack = ORIGIN(RAM) + LENGTH(RAM);        /* end of "RAM" Ram type memory */

  3. _Min_Heap_Size = 0x200 ;        /* required amount of heap  */
  4. _Min_Stack_Size = 0x400 ;        /* required amount of stack */

  5. /* Memories definition */
  6. MEMORY
  7. {
  8.   RAM    (xrw)    : ORIGIN = 0x20000000,   LENGTH = 48K
  9.   FLASH    (rx)    : ORIGIN = 0x8000000,   LENGTH = 256K
  10. }
复制代码

主要修改的地方就在堆栈空间分配与RAM, Flash空间的定义。

修改Makefile文件
主要修改涉及到单片机型号、启动文件、链接文件等的内容:

  1. LINK_SCRIPT = "STM32F103VCTX_FLASH.ld"

  2. ASSEMBLER_FLAGS =-c -g -O0 -mcpu=cortex-m3  -mthumb  \
  3.         -D"STM32F10X_HD" -D"USE_STDPERIPH_DRIVER"  -x assembler-with-cpp $(INCLUDE_DIR)
  4.         
  5. COMPILER_FLAGS =-c -g -MMD -mcpu=cortex-m3  -O0 -Wall \
  6.         -ffunction-sections -fdata-sections -mthumb  \
  7.         -D"STM32F10X_HD" -D"USE_STDPERIPH_DRIVER" $(INCLUDE_DIR)
复制代码

启动文件由于属于汇编文件,因此只要替换即可:

  1. SRCSASM :=         $(wildcard */*/*/*/*/*/*/*/*.s) \
  2.         $(wildcard */*/*/*/*/*/*/*.s) \
  3.         $(wildcard */*/*/*/*/*/*.s) \
  4.         $(wildcard */*/*/*/*/*.s) \
  5.         $(wildcard */*/*/*/*.s) \
  6.         $(wildcard */*/*/*.s) \
  7.         $(wildcard */*/*.s) \
  8.         $(wildcard */*.s)
复制代码

当然,如果存在多个启动文件,可以排除不匹配的型号对应的文件:

  1. #SRCSASM := $(filter-out Source/StdPeriLib/Startup/startup_stm32f103vctx.s, $(SRCSASM))
  2. SRCSASM := $(filter-out Source/StdPeriLib/Startup/startup_stm32f10x_md.s, $(SRCSASM))
复制代码

Alwhales库的修改
需要修改eeprom.h中关于flash划分作为eeprom的地址范围。

对于C8T6型号,flash的结束地址为“0x08010000”;
对于VCT6型号,flash的结束地址为“0x08040000”;

  1. #if defined (STM32F10X_LD) || defined (STM32F10X_MD)
  2.   #define PAGE_SIZE  (uint16_t)0x400  /* Page size = 1KByte */
  3.   #define FLASH_END_ADDRESS 0x08010000

  4. #elif defined (STM32F10X_HD) || defined (STM32F10X_CL)
  5.   #define PAGE_SIZE  (uint16_t)0x800  /* Page size = 2KByte */
  6.   #define FLASH_END_ADDRESS 0x08040000
  7. #endif

  8. /* EEPROM start address in Flash */
  9. #define EEPROM_START_ADDRESS    ((uint32_t)(FLASH_END_ADDRESS - 2 * PAGE_SIZE))
复制代码

由于已经设置好了条件编译,只需要在合适的位置(默认在stm32f10x.h中):

  1. #define STM32F10X_HD
复制代码

标准库的修改
在stm32f10x.h文件中,需要根据所使用的MCU型号,选择对应的宏定义:

  1. /* Uncomment the line below according to the target STM32 device used in your
  2.    application
  3.   */

  4. #if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && !defined (STM32F10X_CL)
  5.   /* #define STM32F10X_LD */     /*!< STM32F10X_LD: STM32 Low density devices */
  6.   /* #define STM32F10X_LD_VL */  /*!< STM32F10X_LD_VL: STM32 Low density Value Line devices */  
  7.   /* #define STM32F10X_MD */     /*!< STM32F10X_MD: STM32 Medium density devices */
  8.   /* #define STM32F10X_MD_VL */  /*!< STM32F10X_MD_VL: STM32 Medium density Value Line devices */  
  9.   #define STM32F10X_HD     /*!< STM32F10X_HD: STM32 High density devices */
  10.   /* #define STM32F10X_HD_VL */  /*!< STM32F10X_HD_VL: STM32 High density value line devices */  
  11.   /* #define STM32F10X_XL */     /*!< STM32F10X_XL: STM32 XL-density devices */
  12.   /* #define STM32F10X_CL */     /*!< STM32F10X_CL: STM32 Connectivity line devices */
  13. #endif
  14. /*  Tip: To avoid modifying this file each time you need to switch between these
  15.         devices, you can define the device in your toolchain compiler preprocessor.

  16. - Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers
  17.    where the Flash memory density ranges between 16 and 32 Kbytes.
  18. - Low-density value line devices are STM32F100xx microcontrollers where the Flash
  19.    memory density ranges between 16 and 32 Kbytes.
  20. - Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx microcontrollers
  21.    where the Flash memory density ranges between 64 and 128 Kbytes.
  22. - Medium-density value line devices are STM32F100xx microcontrollers where the
  23.    Flash memory density ranges between 64 and 128 Kbytes.   
  24. - High-density devices are STM32F101xx and STM32F103xx microcontrollers where
  25.    the Flash memory density ranges between 256 and 512 Kbytes.
  26. - High-density value line devices are STM32F100xx microcontrollers where the
  27.    Flash memory density ranges between 256 and 512 Kbytes.   
  28. - XL-density devices are STM32F101xx and STM32F103xx microcontrollers where
  29.    the Flash memory density ranges between 512 and 1024 Kbytes.
  30. - Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
  31.   */


复制代码

收藏 评论0 发布时间:2022-4-14 10:31

举报

0个回答

所属标签

相似分享

官网相关资源

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