前言
本期我们介绍一下GD32F4标准外设库,看一看如何从0搭建一个项目模版。首先我们先来了解一下GD32,GD32是一款由中国芯片制造商GigaDevice推出的32位ARM Cortex-M微控制器系列。它们与STMicroelectronics的STM32系列微控制器非常相似,使用相同的ARM Cortex-M内核,提供广泛的外设和功能,包括各种通信接口、模拟输入输出、数字输入输出、定时器、中断控制器等等。开发板我们用的是立创·梁山派 GD32F470ZGT6开发板。
https://item.szlcsc.com/5810323.html立创·梁山派 GD32F470ZGT6开发板:https://item.szlcsc.com/5810323.html
标准外设库获取
标准固件库获取我们可以从官网进行下载。
下载链接:兆易创新GigaDevice-资料下载兆易创新GD32 MCU
目录介绍
打开下载的 GD32F4xx 标准固件库,里面的目录如图:
Examples:此文件夹包含的是官方编写的示例代码,涉及芯片的大部分功能。
Firmware:此文件夹里面有 3 个文件夹,包含 CMSIS,标准外设库和 USB 库,存放官方封装的一些库函数,方便用户开发使用。
Template:此文件夹是工程模板文件夹,里面包含 IAR 和 Keil 的工程示例。
Utilities:此文件夹包含一些第三方组件和 GD32 配套的开发板文件。
Firmware
CMSIS:微控制器软件接口标准(CMSIS:Cortex Microcontroller Software Interface Standard) 是Cortex-M 处理器系列的与供应商无关的硬件抽象层。规定了处理器内核与外设的接口,统一了内核访问外设寄存器的方法。
GD32F4xx_standard_peripheral: 从名称也可以看出,这个是 GD32F4 系列的标准外设库,存放一些封装了寄存器的库函数,我们后面编程也是依赖于这个库进行开发。
GD32F4xx_usb_library: 这个是 GD32F4 系列的 USB 库函数,可以帮助我们开发一些关于 USB 的应用,如鼠标,键盘,CDC 串口,模拟 U 盘等等。
CMSIS
内部主要是汇编代码,这个是直接和芯片进行交互的,每种不同的芯片需要配套不同的cmsis接口实现。
GD32F4xx_standard_peripheral
这个里面都是一些外设库文件,包含 GD32F4 芯片的绝大部分功能,包含 ADC,CAN,SDIO,SPI 等。
这个库是嫁接在cmsis的基础上的,F4系列不同的芯片可用公用这一套实现。
项目模版搭建
前期准备
已经安装好 Keil 软件
已经安装好 GD32F4xx 的 Pack 包
已经下载好 GD32F4xx 标准固件库
工程文件目录创建
Project:放工程文件,编译文件等。
Firmware:放 ARM 内核文件,标准外设库文件等。
Hardware:放开发板的硬件驱动文件。
User:放 main 函数,gd32f4xx_it 文件,systick 文件。
Doc: 放 readme.txt 文件,工程说明文件。
固件库移植
找到我们的固件库的下载目录,将GD32F4xx_Firmware_Library_V3.1.0\GD32F4xx_Firmware_Library\Firmware 文件夹下的内容全部拷贝到新建目录的Firmware 下。
程序入口移植
找到我们的固件库的下载目录,将GD32F4xx_Firmware_Library_V3.1.0\Template中的如下文件进行拷贝:
将文件拷贝到工程目录的User目录中:
keil工程创建
打开 keil,点击最上面的 Project 选项卡,选择 New uVision Project 选项新建一个工程
选择保存路径为我们刚才创建的文件夹下的 Project,文件名为 project,然后点击保存。
设备选择
点击保存之后,弹出工程配置窗口,选择所需芯片,这里依次选择GigaDevice->GD32F4xx Series->GD32F470->GD32F470ZG,然后点击 ok。
确定所需芯片之后,弹出 RTE 的环境配置对话框,选择工程所需的组件,不用配置,点击取消
keil分组创建
我们的工程已经创建完成,但是可以看到工程里面还没有文件,我们可以创建一些分组和添加一些文件。
我们打开管理工程项去创建分组和添加文件
首先我们新建分组:
User
CMSIS
Firmware
Hardware
Doc
CMSIS分组添加
来到Firmware\CMSIS\GD\GD32F4xx\Source目录下,选择如下:
来到Firmware\CMSIS\GD\GD32F4xx\Source\ARM目录下,选择如下:
Firmware分组添加
来到Firmware\GD32F4xx_standard_peripheral\Source目录下,选择如下:
程序代码修改
打开 main.c 文件,删除一些不必要的代码,剩余部分如图:
打开 gd32f4xx_it.c 文件,拉到最后面,然后删掉 Systick_Handler 下面的代码,剩余部分如图:
增加延时函数(可选)
打开`systick.h文件,添加delay_1us(uint32_t count)函数,如下图:
打开systick.c文件,修改systick_config函数:
原来的:
void systick_config(void)
{
/* setup systick timer for 1000Hz interrupts */
if(SysTick_Config(SystemCoreClock / 1000U)) {
/* capture error */
while(1) {
}
}
/* configure the systick handler priority */
NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
修改后的:
void systick_config(void)
{
/* setup systick timer for 1000000Hz interrupts */
if(SysTick_Config(SystemCoreClock / 1000000U)) {
/* capture error */
while(1) {
}
}
/* configure the systick handler priority */
NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
修改delay_1ms()函数:
原来的:
void delay_1ms(uint32_t count)
{
delay = count;
while(0U != delay) {
}
}
现在的:
void delay_1ms(uint32_t count)
{
delay = count * 1000;
while(0U != delay) {
}
}
增加`delay_1us()`函数:
void delay_1us(uint32_t count)
{
delay = count;
while(0U != delay) {
}
}
完整代码如下:
systick.h:
/*!
\file systick.h
\brief the header file of systick
\version 2016-08-15, V1.0.0, firmware for GD32F4xx
\version 2018-12-12, V2.0.0, firmware for GD32F4xx
\version 2020-09-30, V2.1.0, firmware for GD32F4xx
\version 2022-03-09, V3.0.0, firmware for GD32F4xx
*/
/*
Copyright (c) 2022, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#ifndef SYS_TICK_H
#define SYS_TICK_H
#include <stdint.h>
/* configure systick */
void systick_config(void);
/* delay a time in milliseconds */
void delay_1ms(uint32_t count);
/* delay decrement */
void delay_decrement(void);
void delay_1us(uint32_t count);
#endif /* SYS_TICK_H */
systick.c:
/*!
\file systick.c
\brief the systick configuration file
\version 2016-08-15, V1.0.0, firmware for GD32F4xx
\version 2018-12-12, V2.0.0, firmware for GD32F4xx
\version 2020-09-30, V2.1.0, firmware for GD32F4xx
\version 2022-03-09, V3.0.0, firmware for GD32F4xx
*/
/*
Copyright (c) 2022, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "gd32f4xx.h"
#include "systick.h"
volatile static uint32_t delay;
/*!
\brief configure systick
\param[in] none
\param[out] none
\retval none
*/
void systick_config(void)
{
/* setup systick timer for 1000000Hz interrupts */
if(SysTick_Config(SystemCoreClock / 1000000U)) {
/* capture error */
while(1) {
}
}
/* configure the systick handler priority */
NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
/*!
\brief delay a time in milliseconds
\param[in] count: count in milliseconds
\param[out] none
\retval none
*/
void delay_1ms(uint32_t count)
{
delay = count * 1000;
while(0U != delay) {
}
}
/*!
\brief delay decrement
\param[in] none
\param[out] none
\retval none
*/
void delay_decrement(void)
{
if(0U != delay) {
delay--;
}
}
void delay_1us(uint32_t count)
{
delay = count;
while(0U != delay) {
}
}
工程配置
Target配置
点击魔法棒,进入配置中的Target,选择ARM compiler 为V6
勾选Use MicroLIB
OutPut配置
C/C++配置
Define配置为:USE_STDPERIPH_DRIVER,GD32F470
Warings选择AC5-like warnings
选择c99和c++03
Optimization选择-O1
Include配置
添加如下几个目录:
User
Firmware\CMSIS\GD\GD32F4xx\Include
Firmware\CMSIS
Firmware\GD32F4xx_standard_peripheral\Include
总结
可能会有小伙伴在搭建过程中遇到兼容性问题,很多芯片官网给出的示例代码是在Keil4老版本工具下创建的,如果使用的三方项目使用Keil4创建的工程,则需要根据官网建议,执行Keil5的兼容性提升操作:https://developer.arm.com/documentation/ka002800/latest
依次点击:Project -> Manage -> Migrate to Version 5 Format.
报错类似下图:
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_43580381/article/details/136211544
|
|