Use STM32CubeIDE Notes (02): STM32CubeMX configuration and code generation

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Naisu_kun/article/details/97392790

purpose

STM32CubeIDE STM32CubeMX to configure the integrated chip generates initialization code resources herein, this will introduce STM32CubeMX use.

User Interface

The main interface consists of four parts and pin-out configuration, clock configuration, project management tools. Which more closely associated with the first two parts, we must work together to adjust. The following is a specific description (mainly in image-based, you can click to see larger image):

And pin-out configuration

This page is mainly used to configure the pin, peripheral and Middleware:
Here Insert Picture Description
Here Insert Picture Description
This tab peripherals and middleware detailed settings mainly related to their specific functions related to property, according to the specific circumstances need to understand settings.

Clock Configuration

The tab portions of the main clock for the configuration of the system:
Here Insert Picture Description

Project management

The tab is provided mainly for code generation options parameter output aspects:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

tool

This page is currently on a tool used to simulate the computing power:
Here Insert Picture Description
This tool how should I say, the individual feels like to be a reference, do not spend too much time here.

Code Generation

STM32CubeMX generated code structure will be different according to different settings, here mainly to talk about the structure of the generated code by default:

  • Used Drivers / CMSIS, Drivers / STM32Fxxx_HAL_Driver middleware is copied from the firmware package directly to the project;
  • The user configuration generates C code to initialize (.c / .h files) corresponding to, and stored in the Inc and
    Src folder, by default, includes the following files:
    • stm32fxxx_hal_conf.h文件:此文件定义了使能的HAL模块,并将一些参数(如
      外部高速振荡器频率)设为预定义的默认值,或根据用户配置(时钟树)进行设
      置;
    • stm32fxxx_hal_msp.c(MSP = MCU支持包):此文件定义了所有初始化函数,以便
      根据用户配置(引脚分配、使能时钟、使用DMA和中断)配置外设实例;
    • main.c负责:
      通过调用重置所有外设、初始化闪存接口和SysTick的HAL_init()函数将MCU重
      置为已知状态。
      配置和初始化系统时钟。
      配置和初始化外设未使用的GPIO。
      为每个已配置的外设定义和调用外设初始化函数,该函数定义了将传递给相应外设
      HAL init函数(转而调用外设HAL MSP初始化函数)的句柄结构。请注意,当使用
      LwIP(各自的USB)中间件时,底层以太网(各自的USB外设)的初始化C代码
      将从main.c移至LwIP(各自的USB)初始化C代码本身。
    • main.h文件:
      此文件包含与通过 引脚布局选项卡所设置的引脚标签相对应的定义语句,以及通过
      配置选项卡所添加的用户项目常量,比如将某个GPIO口用户标签定义为LED,那么在main.h文件中就会出现 #define LED_Pin GPIO_PIN_xx #define LED_GPIO_Port GPIOx 的定义;(一般来说这个文件已经链接了所有库资源,用户自己的文件只要包含这个文件就可以调用其他东西了)

STM32CubeMX生成的文件中,用户需要添加代码的话需要添加在特定区间中,不然会在STM32CubeMX重新生成代码后丢失,见下面:

/* USER CODE BEGIN xxx */
写在这之间的代码在STM32CubeMX重新生成代码时不会丢失
/* USER CODE END xxx */

用户额外添加的文件只要不和STM32CubeMX生成的文件文件名重复,也不会在STM32CubeMX生成代码时丢失。

更换芯片

更换芯片稍微麻烦点,需要去ST官网下载独立版本的STM32CubeMX并进行安装,然后具体的更换步骤如下(更换操作前注意备份原始项目):

  1. 使用STM32CubeMX新建项目,选择新的目标芯片;
  2. 项目建立后不要进行任何操作,直接 File > Import Project ... ,选择原始项目的ioc文件导入;
  3. 导入后核对Import Project页面中新旧芯片资源的兼容性信息,并选择合适参数,然后点OK确认;
  4. 检查当前的STM32CubeMX项目,没问题的话就保存为原始项目同名的ioc文件,然后替换到原始STM32CubeIDE项目中(也可以保存时直接覆盖);
  5. 重新打开STM32CubeIDE项目中ioc文件,应该可以看到芯片已经被替换,检查相关资源配置参数和设置,然后点击重新生成代码;
  6. 编译项目,如果编译报错则根据错误提示手动进行调整直至编译通过;

Here Insert Picture Description
Here Insert Picture Description

总结

For STM32CubeMX here only touched down, more instructions can refer to the official website of the ST user manual UM1718 (official documents written in this very, very detailed, there are plenty of examples, for example), another user in the network as well as Chinese of Chinese ST Manual "STM32CubeMX STM32 configuration and initialization for C code generation", download the following address (the document is not the latest version of translation, for reference only):
https://www.stmcu.com.cn/Designresource/design_resource_detail/file/454999 / lang / ZH / token / 59f8ce3903a16c6cc354ece119011f63

Guess you like

Origin blog.csdn.net/Naisu_kun/article/details/97392790