<strong>前言</strong>
在 STM32 开发中,库函数开发相比寄存器方式具有开发周期短、代码可读性好、便于移植等优点,而使用 Keil 环境的第一步就是新建工程。本文以 STM32F401CE 芯片为例,介绍使用标准库函数新建工程的步骤。
<strong>材料准备</strong>
• STM32F4xx 固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.4.0.rar
• Keil-MDK
• 一点耐心
<strong>新建库函数工程注意事项</strong>
不同芯片在新建工程时的配置略有区别,主要体现在以下几点:
<ol>
<li>
<p>工程目标 Device 选择的芯片型号不同。</p>
</li>
<li>
<p>添加的启动文件不同。要根据芯片型号在 <code>arm</code> 目录下选择相应的 <code>.s</code> 文件。</p>
</li>
<li>
<p>C/C++ 选项卡的芯片型号宏定义不同。具体有哪些选择可在 <code>stm32f4xx.h</code> 头文件中的条件编译指令中找到。不确定选哪个的话可以根据芯片主频从 <code>system_stm32f4xx.c</code> 文件的 PLL 分频参数反推宏定义(要求对时钟树比较熟)。</p>
</li>
<li>
<p>工程所包含的外设库函数不同。MDK 会在编译时根据芯片型号宏定义进行寄存器映射,所以要对芯片所没有的外设库文件要排除编译(如文中的 fmc 和 fsmc),否则会报标识符未定义错误。</p>
</li>
</ol>
<strong>补充:如何确定芯片有无某个外设?</strong>
芯片数据手册中描述了芯片的所有资源,当想要了解具体某一型号芯片的外设时,应该查阅数据手册而不是参考手册(参考手册针对的是整个系列芯片的通用说明)。
<strong>Cortex-M4 新建库函数工程步骤</strong>
<strong>一、新建工程文件夹</strong>
<ol>
<li>
<p>新建一个文件夹 <code>template</code> 用于存放工程模板。</p>
</li>
<li>
<p>在 <code>template</code> 文件夹内分别新建 <code>Doc</code>(存放文档)、<code>User</code>(存放用户文件)两个文件夹。</p>
</li>
<li>
<p>在 <code>User</code> 文件夹中新建 <code>inc</code> 和 <code>src</code> 两个文件夹分别存放用户头文件和源文件。</p>
</li>
</ol>
<strong>二、复制库函数文件</strong>
<strong>1. 复制 Libraries 文件夹</strong>
<p>打开固件库目录 <code>STM32F4xx_DSP_StdPeriph_Lib_V1.4.0</code>, 复制 <code>Libraries</code> 文件夹到工程文件夹<code>template</code> 中。</p>
<strong>2. 裁剪 Libraries 文件夹</strong>
<p>由于固件库的 <code>Libraries</code> 文件夹是对整个 Cortex-M4 系列通用的,包含了一些项目所用不到的文件,为了节约空间,可以把用不到的多余文件删除。</p>
<ul>
<li>
<p>删除 <code>template\Libraries\CMSIS</code> 目录下除 <code>Device</code> 和 <code>Include</code> 外的所有文件夹。</p>
</li>
<li>
<p>删除 <code>template\Libraries\CMSIS\Device\ST\STM32F4xx\Source\Templates</code> 目录下除 <code>arm</code> 文件夹和 <code>system_stm32f4xx.c</code> 外的所有文件夹。</p>
</li>
<li>
<p>删除 <code>template\Libraries\CMSIS\Device\ST\STM32F4xx\Source\Templates\arm</code> 目录下除<code>startup_stm32f401xx.s</code> 外的其余启动文件。</p>
</li>
<li>
<p>固件库里类似 <code>Release_Notes.html</code> 的说明文件也可以删了。</p>
</li>
</ul>
<strong>3. 添加文件到 User 文件夹</strong>
<p>往 <code>template\User</code> 目录中添加以下三个库文件,并新建 <code>main.c</code> 文件。</p>
<ul>
<li>
<p>打开 <code>STM32F4xx_DSP_StdPeriph_Lib_V1.4.0\Project\STM32F4xx_StdPeriph_Templates</code> 目录,找到 <code>stm32f4xx_conf.h</code>、<code>stm32f4xx_it.h</code>、<code>stm32f4xx_it.c</code> 三个文件。</p>
</li>
<li>
<p>复制 <code>stm32f4xx_conf.h</code> 文件到 <code>template\User\inc</code> 目录。</p>
</li>
<li>
<p>复制 <code>stm32f4xx_it.h</code> 文件到 <code>template\User\inc</code> 目录。</p>
</li>
<li>
<p>复制 <code>stm32f4xx_it.c</code> 文件到 <code>template\User\src</code> 目录。</p>
</li>
<li>
<p>在 <code>template\User\src</code> 目录中新建 <code>main.c</code> 文件,并编写以下主函数代码保存。</p>
</li>
</ul>
<pre style="overflow-x:auto; background-color:#e9e9e9;"><code>#include "stm32f4xx.h"
int main(void)
{
while (1);
}</code></pre>
<p>其中 <code>stm32f4xx_conf.h</code> 文件包含了所有库函数头文件,<code>stm32f4xx_it.c</code> 文件用于编写中断服务函数,便于统一管理,使工程结构更加规范。</p>
<p>完成以上工作后整个工程目录结构如下图:</p>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25990-1.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
<strong>三、Keil 新建工程</strong>
把工程文件夹建好并复制相关库文件后,就可以打开 Keil 软件新建工程了。
<strong>1. 新建工程</strong>
<p>打开 Keil 软件,点击 Project 菜单下的 New uVision Project 选项新建工程,并保存到新建的工程文件夹 <code>template</code> 中。</p>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25991-2.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25992-3.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
<strong>2. 选择芯片型号</strong>
在弹出的芯片选型窗口中选择目标板的芯片型号,这里选 STMicroelectronics(ST公司)下的 STM32F401CE(根据具体硬件选择)。
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25993-4.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
关掉弹出的组件添加窗口,因为我们采用的是手动添加库文件的方式。
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25994-5.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
<strong>3. 新建工程分组</strong>
点击「品字形」按钮打开工程管理界面,最左侧是工程名称,可以给工程改个名,然后在中间组管理中点虚框图标新建 CMSIS、STM32F4xx_StdPeriph_Driver、USER、DOC 四个分组,对应工程文件夹中的分类。
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25995-6.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
<strong>4. 添加文件到分组</strong>
<p>依然是在工程管理界面下,这一步要做的事情是把准备好的库文件添加到 Keil 工程中,具体操作如下:</p>
<ul>
<li>
<p>往分组 CMSIS 中添加 <code>system_stm32f4xx.c</code> 系统配置文件和 <code>startup_stm32f401xx.s</code> 启动文件。</p>
</li>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25996-7.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
<li>
<p>往分组 STM32F4xx_StdPeriph_Driver 中添加 <code>Libraries\STM32F4xx_StdPeriph_Driver\src</code>目录下的所有 <code>.c</code> 文件。</p>
</li>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25997-8.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
<li>
<p>往分组 USER 中添加文件 <code>main.c</code> 和 <code>stm32f4xx_it.c</code>。</p>
</li>
</ul>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25998-9.jpg&q…; alt="STM32F4 新建标准库函数工程"></center>
<strong>四、工程配置</strong>
点击「魔术棒」按钮打开工程选项界面,进行必要的工程配置。
<strong>1. Target 选项卡配置</strong>
<p>勾选 Use Micro LIB 选项,为了在工程中使用 printf() 函数。</p>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-25999-10.jpg&…; alt="STM32F4 新建标准库函数工程"></center>
<strong>2. Output 选项卡配置</strong>
<p>如需生成 <code>.hex</code> 文件,则需勾选 Create HEX File 选项。</p>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-26000-11.jpg&…; alt="STM32F4 新建标准库函数工程"></center>
<strong>3. C/C++ 选项卡配置</strong>
<ul>
<li>
<p>在「预处理符号」Preprocessor Symbols 下的 Define 一栏中添加 <code>STM32F401xx</code> 和<code>USE_STDPERIPH_DRIVER</code> 两个宏定义(用逗号分隔)。</p>
</li>
<li>
<p>在 Include Paths 中添加以下头文件路径,注意要具体到头文件上一层目录。</p>
<ol>
<li>
<p><code>.\Libraries\CMSIS\Device\ST\STM32F4xx\Include</code></p>
</li>
<li>
<p><code>.\Libraries\CMSIS\Include</code></p>
</li>
<li>
<p><code>.\Libraries\STM32F4xx_StdPeriph_Driver\inc</code></p>
</li>
<li>
<p><code>.\User\inc</code></p>
</li>
</ol>
</li>
</ul>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-26001-12.jpg&…; alt="STM32F4 新建标准库函数工程"></center>
<strong>五、下载器配置</strong>
依然是在工程选项界面下,进行下载器配置
<strong>Debug 选项卡配置</strong>
在右上角选择所使用的调试器,根据实际情况选择。这里我用的是 ST-Link,选 ST-Link Debugger。
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-26002-13.jpg&…; alt="STM32F4 新建标准库函数工程"></center>
点击 setting 按钮,在 Falsh Download 选项卡中勾选 Reset and Run 选项,确保程序下载后能自动复位运行,最后点击确定按钮保存所有的工程配置。
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-26003-14.jpg&…; alt="STM32F4 新建标准库函数工程"></center>
<strong>六、最后小整改</strong>
此时编译整个工程依然会有大量错误,为了能使工程顺利编译最后还要稍作修改,具体如下:
<strong>取消编译 fmc 和 fsmc 库文件</strong>
<p>查阅数据手册得知 STM32F401 系列芯片没有 fmc 和 fsmc 外设,所以去掉 fmc 和 fsmc 部分的库文件。</p>
<p>在工程文件视图下展开 STM32F4xx_StdPeriph_Driver 分组,选中 <code>stm32f4xx_fmc.c</code> 文件,右键调出 Options for File stm32f4xx_fmc.c 窗口,取消勾选 Include in Target Build 选项,排除<code>stm32f4xx_fmc.c</code> 文件参与编译。<code>stm32f4xx_fsmc.c</code> 用文件同样方式处理(或者直接从工程中移除这两个文件)。</p>
<center><img width="600" src="http://mcu.eetrend.com/files/2017-09/wen_zhang_/100007897-26004-15.jpg&…; alt="STM32F4 新建标准库函数工程"></center>
<strong>修改 stm32f4xx_it.c 文件</strong>
<ol>
<li>
<p>删除 <code>stm32f4xx_it.c</code> 文件 32 行的代码 #include “main.h”(因为没有写这个文件)。</p>
</li>
<li>
<p>删除 <code>stm32f4xx_it.c</code> 文件 144 行的代码 TimingDelay_Decrement();(滴答定时器延时相关,暂时用不到)。</p>
</li>
</ol>
<strong>七、编译工程、下载验证</strong>
最后点击编译按钮,如果工程配置正确就会看到令人愉悦的 0 Error(s), 0 Warning(s) ,通过下载器连接板子和电脑,烧写程序检验成果,见证令人激动的时刻吧!
转自: <a href="https://mp.weixin.qq.com/s/bnq0uoIibQJ7HidHexTbNA">开源嵌入式</a>