打印
[研电赛技术支持]

gd32F470串口重定义

[复制链接]
573|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tpgf|  楼主 | 2024-7-12 08:40 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
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 */

测试结果:

​​




————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/weixin_46066669/article/details/140367243

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1645

主题

14850

帖子

10

粉丝