网友:郑爷爷,什么是朋友?
郑渊洁:孩子将玩具当朋友,成人将朋友当玩具。
如想获取本文相关资料和最后的工程文件,请关注公众号《嵌入式小散修》,回复 GD32F10x系列建立keil模板工程资料
。
准备
- 首先,下载
GD32F10x
系列软件包和 keil支持包:http://www.gd32mcu.com/cn/download/7?kw=GD32F1
目前GD32F10x
固件库的最新版本是2.2.2
,keil支持包的版本为2.02
,如下所示:(写于2022-3-2
)
- 安装
GD32F10x
系列的keil
支持包
看到如下界面,说明keil
支持包安装成功。
建立 GD32F10X 对应的Keil 工程(以GD32F103C8T6为例)
-
新建文件夹如下:
-
新建
keil
的空工程,存放在上图中的KeilProject
文件夹
-
选择对应芯片
-
新建工程下的目录如下
-
添加
CMSIS Cortex-M3 Device Peripheral Access Layer Source File
,对于所有 GD32F10x 系列都有效。
-
添加芯片启动文件(因为GD32F103C8T6是中等容量,所以选择
startup_gd32f10x_md.s
,别的芯片按照其容量选择)
-
添加 GB32F10x 标准库文件
-
从标准库中的模板工程拷贝如下器个文件到 User 文件夹。(???有标准模板,我在这折腾啥?算了就当熟悉结构吧)
-
将上述文件中的
.c
文件加入工程。
-
标准库需要增加两个宏定义才能使用:
USE_STDPERIPH_DRIVER,GD32F10X_MD
-
添加头文件路径:
-
如果你的板子没有外部晶振,得配置成内部晶振,如下操作:
-
将 main 函数修改成如下代码:
/*!
\file main.c
\brief led spark with systick, USART print and key example
\version 2014-12-26, V1.0.0, firmware for GD32F10x
\version 2017-06-20, V2.0.0, firmware for GD32F10x
\version 2018-07-31, V2.1.0, firmware for GD32F10x
\version 2020-09-30, V2.2.0, firmware for GD32F10x
*/
/*
Copyright (c) 2020, 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 "gd32f10x.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "gd32f10x_eval.h"
/*!
\brief toggle the led every 500ms
\param[in] none
\param[out] none
\retval none
*/
void led_spark(void)
{
static __IO uint32_t timingdelaylocal = 0U;
if(timingdelaylocal){
if(timingdelaylocal < 500U){
gd_eval_led_on(LED2);
}else{
gd_eval_led_off(LED2);
}
timingdelaylocal--;
}else{
timingdelaylocal = 1000U;
}
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* configure systick */
systick_config();
/* initilize the LEDs, USART and key */
gd_eval_led_init(LED2);
gd_eval_led_init(LED3);
gd_eval_led_init(LED4);
gd_eval_com_init(EVAL_COM0);
gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
/* print out the clock frequency of system, AHB, APB1 and APB2 */
printf("\r\nCK_SYS is %d", rcu_clock_freq_get(CK_SYS));
printf("\r\nCK_AHB is %d", rcu_clock_freq_get(CK_AHB));
printf("\r\nCK_APB1 is %d", rcu_clock_freq_get(CK_APB1));
printf("\r\nCK_APB2 is %d", rcu_clock_freq_get(CK_APB2));
while(1){
if(RESET == gd_eval_key_state_get(KEY_WAKEUP)){
gd_eval_led_on(LED3);
delay_1ms(500);
gd_eval_led_off(LED3);
gd_eval_led_toggle(LED4);
}
}
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(EVAL_COM0, (uint8_t)ch);
while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
return ch;
}
-
连接烧录器和开发板,配置烧录器(这里以jlink为例)
如果是 SWD 模式,选择SW:
-
编译OK,然后下载。