[开发工具] STM32CUBEMX应对不同版本库的解决方案

[复制链接]
 楼主| Jiangxiaopi 发表于 2025-7-7 17:35 | 显示全部楼层 |阅读模式
一、问题由来
1.1、从官网下载的最新版本默认采用最新的HAL版本库,由于HAL库本身的问题,不兼容旧版本的库,导致旧代码可维护性差,甚至需要重新生成代码。

1.2、新版本的HAL库将一些配置操作统一到了xxx_hal.c中,暴露在用户可配置的参数太少,导致找不到熟悉的配置项,从而拖累开发进度。

二、解决方案
以旧代码的HAL库版本为V1.7.12、cubemx版本为6.6.1为例。

2.1、下载旧代码对应的cubemx版本
HAL版本在xxx_hal.c中查看,如stm32f4xx_hal.c:

/**
  ******************************************************************************
  * @file    stm32f4xx_hal.c
  * @author  MCD Application Team
  * @brief   HAL module driver.
  *          This is the common part of the HAL initialization
  *
  @verbatim
  ==============================================================================
                     ##### How to use this driver #####
  ==============================================================================
    [..]
    The common HAL driver contains a set of generic and common APIs that can be
    used by the PPP peripheral drivers and the user to start using the HAL.
    [..]
    The HAL contains two APIs' categories:
         (+) Common HAL APIs
         (+) Services HAL APIs
  @endverbatim
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
/** @addtogroup STM32F4xx_HAL_Driver
  * @{
  */
/** @defgroup HAL HAL
  * @brief HAL module driver.
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/** @addtogroup HAL_Private_Constants
  * @{
  */
/**
  * @brief STM32F4xx HAL Driver version number V1.7.12
  */



官网最新的cubemx版本为1.14.1,可通过下拉的方式找到旧的cubemx,并下载安装:

cubemx下载地址

9377686b639a7704d.png

安装完成后,用6.6.1打开旧的cubemx工程,其HAL默认的是V1.8.4,生成的代码xxx_HAL.c如下:

******************************************************************************
  * @file    stm32f4xx_hal.c
  * @author  MCD Application Team
  * @brief   HAL module driver.
  *          This is the common part of the HAL initialization
  *
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2017 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  @verbatim
  ==============================================================================
                     ##### How to use this driver #####
  ==============================================================================
    [..]
    The common HAL driver contains a set of generic and common APIs that can be
    used by the PPP peripheral drivers and the user to start using the HAL.
    [..]
    The HAL contains two APIs' categories:
         (+) Common HAL APIs
         (+) Services HAL APIs
  @endverbatim
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
/** @addtogroup STM32F4xx_HAL_Driver
  * @{
  */
/** @defgroup HAL HAL
  * @brief HAL module driver.
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/** @addtogroup HAL_Private_Constants
  * @{
  */
/**
  * @brief STM32F4xx HAL Driver version number V1.8.4
  */



2.2、安装旧代码对应的HAL库版本
这不是我们想要的,下一步,点击:Help->Manage .....。找到旧代码HAL库对应的版本,点击安装。

89951686b638e8c776.png

注意:此界面显示的版本号,如1.26.1并不是HAL库内显示的版本号,需要多下几个版本,生成代码后看HAL库版本号,对比确认,直到找到自己想要的版本。

22949686b638892f4f.png

2.3、选择旧代码对应的HAL库版本生成代码
在生产代码界面,取消勾选Use latest.... 和 Use Default.... 并手动选择想要的版本库

52489686b6381bbc26.png

26838686b637ca22cf.png


点击生成代码,打开xxxHAL.c确认是否为正确的版本号。

三、注意事项
3.1、如何先用新库生成的代码,再用旧库重新生成。会出现新库的代码没有全部擦出的情况,导致编译报错,需要手动调整解决。
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/ID349287114/article/details/149040201

LOVEEVER 发表于 2025-7-29 23:52 | 显示全部楼层
最新的IDE问题多吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

55

主题

224

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部