首页 > 其他分享 >gd32F470串口重定义

gd32F470串口重定义

时间:2024-07-11 09:54:47浏览次数:15  
标签:定义 PIN gd32F470 KEY EVAL 串口 GPIO COM0 define

c代码:

/*
 * @Author: Bleaach008
 * @Date: 2024-07-10 17:31:01
 * @LastEditTime: 2024-07-11 09:42:06
 * @FilePath: \MDK-ARMd:\Code\GD32\GD01_UART\MyApplication\Public.c
 * @Description:
 *
 * Copyright (c) 2024 by 008, All Rights Reserved.
 */
/* Includes ------------------------------------------------------------------*/
#include "MyApplication.h"

/* Private define-------------------------------------------------------------*/

/* Private variables----------------------------------------------------------*/

/* Public variables-----------------------------------------------------------*/

/* Private function prototypes------------------------------------------------*/
/**
 * @name: usart_debug_init
 * @brief: 串口调试口初始化
 * @param: {None}
 * @retval: 
 */
void usart_debug_init()
{
    /* 初始化GPIO外设 */
    rcu_periph_clock_enable(EVAL_COM0_GPIO_CLK);
    /* 初始化USART外设 */
    rcu_periph_clock_enable(EVAL_COM0_CLK); // 使能串口0时钟
                                            /*GPIO复用功能*/
    gpio_af_set(EVAL_COM0_GPIO_PORT, EVAL_COM0_AF, EVAL_COM0_TX_PIN);
    gpio_af_set(EVAL_COM0_GPIO_PORT, EVAL_COM0_AF, EVAL_COM0_RX_PIN);
    /* TX管脚,PA9,复用推挽输出,速度50MHz */
    gpio_mode_set(EVAL_COM0_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, EVAL_COM0_TX_PIN);
    /* RX管脚,PA10,下拉输入,速度50MHz */
    gpio_mode_set(EVAL_COM0_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, EVAL_COM0_RX_PIN);
    /*配置TX为推挽输出 50MHZ*/
    gpio_output_options_set(EVAL_COM0_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, EVAL_COM0_TX_PIN);
    /*配置RX为推挽输出 50MHZ*/
    gpio_output_options_set(EVAL_COM0_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, EVAL_COM0_RX_PIN);

    usart_deinit(EVAL_COM0);
    usart_baudrate_set(EVAL_COM0, 115200);                   // 波特率115200
    usart_parity_config(EVAL_COM0, USART_PM_NONE);           // 无校检
    usart_word_length_set(EVAL_COM0, USART_WL_8BIT);         // 8位数据位
    usart_stop_bit_set(EVAL_COM0, USART_STB_1BIT);           // 1位停止位
    usart_transmit_config(EVAL_COM0, USART_TRANSMIT_ENABLE); // 使能串口发送
    usart_receive_config(EVAL_COM0, USART_RECEIVE_ENABLE);   // 使能串口接收
    usart_enable(EVAL_COM0);                                 // 使能串口
}
int fputc(int ch, FILE *f)
{
    // 通过查询的方式循环发送
    /* 发送一个字节数据到串口 */
    usart_data_transmit(EVAL_COM0, (uint8_t)ch);

    /* 等待发送完毕 */
    while (usart_flag_get(EVAL_COM0, USART_FLAG_TBE) == RESET)
        ;

    return (ch);
}

/// 重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
    /* 等待串口输入数据 */
    while (usart_flag_get(EVAL_COM0, USART_FLAG_RBNE) == RESET)
        ;

    return (int)usart_data_receive(EVAL_COM0);
}
/********************************************************
  End Of File
********************************************************/

头文件定义:

/*!
    \file    gd32f470i_eval.h
    \brief   definitions for GD32F470_EVAL's leds, keys and COM ports hardware resources
  
    \version 2022-04-18, V2.0.0, demo 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 GD32F470I_EVAL_H
#define GD32F470I_EVAL_H

#ifdef __cplusplus
 extern "C" {
#endif

#include "gd32f4xx.h"
   
/* exported types */
typedef enum 
{
    LED1 = 0,
    LED2 = 1,
    LED3 = 2
} led_typedef_enum;

typedef enum 
{
    KEY_WAKEUP = 0,
    KEY_TAMPER = 1,
    KEY_USER = 2
} key_typedef_enum;

typedef enum 
{
    KEY_MODE_GPIO = 0,
    KEY_MODE_EXTI = 1
} keymode_typedef_enum;

/* eval board low layer led */
#define LEDn                             3U

#define LED1_PIN                         GPIO_PIN_2
#define LED1_GPIO_PORT                   GPIOE
#define LED1_GPIO_CLK                    RCU_GPIOE
  
#define LED2_PIN                         GPIO_PIN_3
#define LED2_GPIO_PORT                   GPIOE
#define LED2_GPIO_CLK                    RCU_GPIOE
  
#define LED3_PIN                         GPIO_PIN_10
#define LED3_GPIO_PORT                   GPIOF
#define LED3_GPIO_CLK                    RCU_GPIOF

#define COMn                             1U
#define EVAL_COM0                        USART0
#define EVAL_COM0_CLK                    RCU_USART0

#define EVAL_COM0_TX_PIN                 GPIO_PIN_9
#define EVAL_COM0_RX_PIN                 GPIO_PIN_10

#define EVAL_COM0_GPIO_PORT              GPIOA
#define EVAL_COM0_GPIO_CLK               RCU_GPIOA
#define EVAL_COM0_AF                     GPIO_AF_7

#define KEYn                             3U

/* tamper push-button */
#define TAMPER_KEY_PIN                   GPIO_PIN_13
#define TAMPER_KEY_GPIO_PORT             GPIOC
#define TAMPER_KEY_GPIO_CLK              RCU_GPIOC
#define TAMPER_KEY_EXTI_LINE             EXTI_13
#define TAMPER_KEY_EXTI_PORT_SOURCE      EXTI_SOURCE_GPIOC
#define TAMPER_KEY_EXTI_PIN_SOURCE       EXTI_SOURCE_PIN13
#define TAMPER_KEY_EXTI_IRQn             EXTI10_15_IRQn

/* wakeup push-button */
#define WAKEUP_KEY_PIN                   GPIO_PIN_0
#define WAKEUP_KEY_GPIO_PORT             GPIOA
#define WAKEUP_KEY_GPIO_CLK              RCU_GPIOA
#define WAKEUP_KEY_EXTI_LINE             EXTI_0
#define WAKEUP_KEY_EXTI_PORT_SOURCE      EXTI_SOURCE_GPIOA
#define WAKEUP_KEY_EXTI_PIN_SOURCE       EXTI_SOURCE_PIN0
#define WAKEUP_KEY_EXTI_IRQn             EXTI0_IRQn  

/* user push-button */
#define USER_KEY_PIN                     GPIO_PIN_14
#define USER_KEY_GPIO_PORT               GPIOB
#define USER_KEY_GPIO_CLK                RCU_GPIOB
#define USER_KEY_EXTI_LINE               EXTI_14
#define USER_KEY_EXTI_PORT_SOURCE        EXTI_SOURCE_GPIOB
#define USER_KEY_EXTI_PIN_SOURCE         EXTI_SOURCE_PIN14
#define USER_KEY_EXTI_IRQn               EXTI10_15_IRQn

/* function declarations */
/* configures led GPIO */
void gd_eval_led_init(led_typedef_enum lednum);
/* turn on selected led */
void gd_eval_led_on(led_typedef_enum lednum);
/* turn off selected led */
void gd_eval_led_off(led_typedef_enum lednum);
/* toggle the selected led */
void gd_eval_led_toggle(led_typedef_enum lednum);
/* configure key */
void gd_eval_key_init(key_typedef_enum key_num, keymode_typedef_enum key_mode);
/* return the selected button state */
uint8_t gd_eval_key_state_get(key_typedef_enum button);
/* configure COM port */
void gd_eval_com_init(uint32_t com);


#ifdef __cplusplus
}
#endif

#endif /* GD32F470I_EVAL_H */

测试结果:

image

image

标签:定义,PIN,gd32F470,KEY,EVAL,串口,GPIO,COM0,define
From: https://www.cnblogs.com/rose24/p/18295442/gd32f470-serial-port-redemption-z18tbvk

相关文章

  • Perl词法作用域:自定义编程环境的构建术
    ......
  • WHAT - Tailwind 样式方案(不写任何自定义样式)
    目录一、官网二、设计理念三、示例DesignSystem一、官网https://tailwindcss.com/TailwindCSSworksbyscanningallofyourHTMLfiles,JavaScriptcomponents,andanyothertemplatesforclassnames,generatingthecorrespondingstylesandthenwrit......
  • Android自定义View游戏方向轮盘转向盘方向盘
    在画之前做一些规划,把View分成6份:一份就是小圆的半径,两份就是大圆的半径,大圆的圆心始终是中心,小圆位置根据手指变化,当我们手指超出大圆的范围时候就把他固定在大圆的边缘上(利用相似三角形来计算位置)。privatevoidcomputePosition(floatx,floaty){float......
  • 打造个性化科学工具箱:使用conda-build自定义软件包
    打造个性化科学工具箱:使用conda-build自定义软件包引言Conda是一个强大的包管理系统,广泛用于Python社区,尤其在数据科学和机器学习领域。除了安装现成的包,Conda还允许用户通过conda-build工具构建和分享自己的软件包。本文将详细介绍如何在Conda环境中使用conda-build构建......
  • 极限的严格定义
    极限的严格定义在数学中是非常重要的,它为微积分及其后续理论提供了坚实的基础。下面分别给出数列极限和函数极限的严格定义。数列极限的严格定义设\((a_n)_{n=1}^{\infty}\)是一个实数数列,\(a\)是实数。我们说数列\((a_n)\)的极限是\(a\),记作\[\lim_{n\to\infty}a_n=a,\]......
  • VisualStudio各版本_MSC_VER和_MSC_FULL_VER宏定义值列表
        这些值可以用于在C++中判断版本和C++特性支持情况。   大版本产品名VC++版本号_MSC_VER定义_MSC_FULL_VER定义2022VisualStudio2022version17.9.214.3919391939335212022VisualStudio2022version17.8.314.381938193833133202......
  • 一起了解自定义流程表单开发的主要优势
    当前,想要进行流程化办公,应用专业的软件平台产品是非常重要的。低代码技术平台拥有可视化操作界面、很灵活、易维护等优势特点,可以应用于很多中小型流程化办公中。那么,低代码技术平台、自定义流程表单开发的主要优势特点表现在哪来?跟着小编的节奏,一起来了解它们的主要优势吧。什么......
  • jmeter自定义函数开发—Web3钱包生成工具
    之前使用Jmeter进行接口测试时,有生成钱包地址的需求,于是有时间就简单写了个自定义函数环境说明JDK1.8.0,Jmeter5.4.3,maven构建工具实现代码新建一个简单的maven项目即可,以下是pom.xml配置<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org......
  • 定制化正则化:在Mojo模型中动态应用自定义方法
    定制化正则化:在Mojo模型中动态应用自定义方法在机器学习模型的训练过程中,正则化是一种用于防止过拟合的技术,它通过在损失函数中添加一个额外的惩罚项来实现。Mojo模型,作为H2O.ai提供的一种模型部署格式,主要用于模型的序列化和预测。虽然Mojo模型本身不支持在模型部署后动态......
  • OceanBase 配置项&系统变量实现及应用详解(2):系统变量的定义及使用场景
    在上一篇博客,配置项的定义及使用方法,详细阐述了配置项的概念及其基本应用方式,这些配置项能够调控集群或租户的行为方式。然而,在实际使用OceanBase的过程中,我们有时仅希望针对当前会话调整某些行为特性,且在关闭会话连接后,这些调整不会影响后续的使用。此时,我们就需要借助“系统......