• gd32F470串口重定义


    c代码:

    1. /*
    2. * @Author: Bleaach008
    3. * @Date: 2024-07-10 17:31:01
    4. * @LastEditTime: 2024-07-11 09:42:06
    5. * @FilePath: \MDK-ARMd:\Code\GD32\GD01_UART\MyApplication\Public.c
    6. * @Description:
    7. *
    8. * Copyright (c) 2024 by 008, All Rights Reserved.
    9. */
    10. /* Includes ------------------------------------------------------------------*/
    11. #include "MyApplication.h"
    12. /* Private define-------------------------------------------------------------*/
    13. /* Private variables----------------------------------------------------------*/
    14. /* Public variables-----------------------------------------------------------*/
    15. /* Private function prototypes------------------------------------------------*/
    16. /**
    17. * @name: usart_debug_init
    18. * @brief: 串口调试口初始化
    19. * @param: {None}
    20. * @retval:
    21. */
    22. void usart_debug_init()
    23. {
    24. /* 初始化GPIO外设 */
    25. rcu_periph_clock_enable(EVAL_COM0_GPIO_CLK);
    26. /* 初始化USART外设 */
    27. rcu_periph_clock_enable(EVAL_COM0_CLK); // 使能串口0时钟
    28. /*GPIO复用功能*/
    29. gpio_af_set(EVAL_COM0_GPIO_PORT, EVAL_COM0_AF, EVAL_COM0_TX_PIN);
    30. gpio_af_set(EVAL_COM0_GPIO_PORT, EVAL_COM0_AF, EVAL_COM0_RX_PIN);
    31. /* TX管脚,PA9,复用推挽输出,速度50MHz */
    32. gpio_mode_set(EVAL_COM0_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, EVAL_COM0_TX_PIN);
    33. /* RX管脚,PA10,下拉输入,速度50MHz */
    34. gpio_mode_set(EVAL_COM0_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, EVAL_COM0_RX_PIN);
    35. /*配置TX为推挽输出 50MHZ*/
    36. gpio_output_options_set(EVAL_COM0_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, EVAL_COM0_TX_PIN);
    37. /*配置RX为推挽输出 50MHZ*/
    38. gpio_output_options_set(EVAL_COM0_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, EVAL_COM0_RX_PIN);
    39. usart_deinit(EVAL_COM0);
    40. usart_baudrate_set(EVAL_COM0, 115200); // 波特率115200
    41. usart_parity_config(EVAL_COM0, USART_PM_NONE); // 无校检
    42. usart_word_length_set(EVAL_COM0, USART_WL_8BIT); // 8位数据位
    43. usart_stop_bit_set(EVAL_COM0, USART_STB_1BIT); // 1位停止位
    44. usart_transmit_config(EVAL_COM0, USART_TRANSMIT_ENABLE); // 使能串口发送
    45. usart_receive_config(EVAL_COM0, USART_RECEIVE_ENABLE); // 使能串口接收
    46. usart_enable(EVAL_COM0); // 使能串口
    47. }
    48. int fputc(int ch, FILE *f)
    49. {
    50. // 通过查询的方式循环发送
    51. /* 发送一个字节数据到串口 */
    52. usart_data_transmit(EVAL_COM0, (uint8_t)ch);
    53. /* 等待发送完毕 */
    54. while (usart_flag_get(EVAL_COM0, USART_FLAG_TBE) == RESET)
    55. ;
    56. return (ch);
    57. }
    58. /// 重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
    59. int fgetc(FILE *f)
    60. {
    61. /* 等待串口输入数据 */
    62. while (usart_flag_get(EVAL_COM0, USART_FLAG_RBNE) == RESET)
    63. ;
    64. return (int)usart_data_receive(EVAL_COM0);
    65. }
    66. /********************************************************
    67. End Of File
    68. ********************************************************/

    头文件定义:

    1. /*!
    2. \file gd32f470i_eval.h
    3. \brief definitions for GD32F470_EVAL's leds, keys and COM ports hardware resources
    4. \version 2022-04-18, V2.0.0, demo for GD32F4xx
    5. */
    6. /*
    7. Copyright (c) 2022, GigaDevice Semiconductor Inc.
    8. Redistribution and use in source and binary forms, with or without modification,
    9. are permitted provided that the following conditions are met:
    10. 1. Redistributions of source code must retain the above copyright notice, this
    11. list of conditions and the following disclaimer.
    12. 2. Redistributions in binary form must reproduce the above copyright notice,
    13. this list of conditions and the following disclaimer in the documentation
    14. and/or other materials provided with the distribution.
    15. 3. Neither the name of the copyright holder nor the names of its contributors
    16. may be used to endorse or promote products derived from this software without
    17. specific prior written permission.
    18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
    27. OF SUCH DAMAGE.
    28. */
    29. #ifndef GD32F470I_EVAL_H
    30. #define GD32F470I_EVAL_H
    31. #ifdef __cplusplus
    32. extern "C" {
    33. #endif
    34. #include "gd32f4xx.h"
    35. /* exported types */
    36. typedef enum
    37. {
    38. LED1 = 0,
    39. LED2 = 1,
    40. LED3 = 2
    41. } led_typedef_enum;
    42. typedef enum
    43. {
    44. KEY_WAKEUP = 0,
    45. KEY_TAMPER = 1,
    46. KEY_USER = 2
    47. } key_typedef_enum;
    48. typedef enum
    49. {
    50. KEY_MODE_GPIO = 0,
    51. KEY_MODE_EXTI = 1
    52. } keymode_typedef_enum;
    53. /* eval board low layer led */
    54. #define LEDn 3U
    55. #define LED1_PIN GPIO_PIN_2
    56. #define LED1_GPIO_PORT GPIOE
    57. #define LED1_GPIO_CLK RCU_GPIOE
    58. #define LED2_PIN GPIO_PIN_3
    59. #define LED2_GPIO_PORT GPIOE
    60. #define LED2_GPIO_CLK RCU_GPIOE
    61. #define LED3_PIN GPIO_PIN_10
    62. #define LED3_GPIO_PORT GPIOF
    63. #define LED3_GPIO_CLK RCU_GPIOF
    64. #define COMn 1U
    65. #define EVAL_COM0 USART0
    66. #define EVAL_COM0_CLK RCU_USART0
    67. #define EVAL_COM0_TX_PIN GPIO_PIN_9
    68. #define EVAL_COM0_RX_PIN GPIO_PIN_10
    69. #define EVAL_COM0_GPIO_PORT GPIOA
    70. #define EVAL_COM0_GPIO_CLK RCU_GPIOA
    71. #define EVAL_COM0_AF GPIO_AF_7
    72. #define KEYn 3U
    73. /* tamper push-button */
    74. #define TAMPER_KEY_PIN GPIO_PIN_13
    75. #define TAMPER_KEY_GPIO_PORT GPIOC
    76. #define TAMPER_KEY_GPIO_CLK RCU_GPIOC
    77. #define TAMPER_KEY_EXTI_LINE EXTI_13
    78. #define TAMPER_KEY_EXTI_PORT_SOURCE EXTI_SOURCE_GPIOC
    79. #define TAMPER_KEY_EXTI_PIN_SOURCE EXTI_SOURCE_PIN13
    80. #define TAMPER_KEY_EXTI_IRQn EXTI10_15_IRQn
    81. /* wakeup push-button */
    82. #define WAKEUP_KEY_PIN GPIO_PIN_0
    83. #define WAKEUP_KEY_GPIO_PORT GPIOA
    84. #define WAKEUP_KEY_GPIO_CLK RCU_GPIOA
    85. #define WAKEUP_KEY_EXTI_LINE EXTI_0
    86. #define WAKEUP_KEY_EXTI_PORT_SOURCE EXTI_SOURCE_GPIOA
    87. #define WAKEUP_KEY_EXTI_PIN_SOURCE EXTI_SOURCE_PIN0
    88. #define WAKEUP_KEY_EXTI_IRQn EXTI0_IRQn
    89. /* user push-button */
    90. #define USER_KEY_PIN GPIO_PIN_14
    91. #define USER_KEY_GPIO_PORT GPIOB
    92. #define USER_KEY_GPIO_CLK RCU_GPIOB
    93. #define USER_KEY_EXTI_LINE EXTI_14
    94. #define USER_KEY_EXTI_PORT_SOURCE EXTI_SOURCE_GPIOB
    95. #define USER_KEY_EXTI_PIN_SOURCE EXTI_SOURCE_PIN14
    96. #define USER_KEY_EXTI_IRQn EXTI10_15_IRQn
    97. /* function declarations */
    98. /* configures led GPIO */
    99. void gd_eval_led_init(led_typedef_enum lednum);
    100. /* turn on selected led */
    101. void gd_eval_led_on(led_typedef_enum lednum);
    102. /* turn off selected led */
    103. void gd_eval_led_off(led_typedef_enum lednum);
    104. /* toggle the selected led */
    105. void gd_eval_led_toggle(led_typedef_enum lednum);
    106. /* configure key */
    107. void gd_eval_key_init(key_typedef_enum key_num, keymode_typedef_enum key_mode);
    108. /* return the selected button state */
    109. uint8_t gd_eval_key_state_get(key_typedef_enum button);
    110. /* configure COM port */
    111. void gd_eval_com_init(uint32_t com);
    112. #ifdef __cplusplus
    113. }
    114. #endif
    115. #endif /* GD32F470I_EVAL_H */

    测试结果:

    image

    image

  • 相关阅读:
    TensorFlow和CUDA、cudnn、Pytorch以及英伟达显卡对应版本对照表
    java数据库linux面试(详细)
    国庆作业day10.4
    13年老鸟整理,性能测试技术知识体系总结,从零开始打通...
    用AI打造一个属于自己的歌手,让她C位霸气出道
    如何将“龙”插入到富文本编辑器中?
    vue实现文件上传压缩优化处理
    OpenJudge NOI 2.1 2472:子串计算
    YAYA LIVE CTO 唐鸿斌:真正本地化,要让产品没有「产地」属性
    【Maven系列】maven基础(三)
  • 原文地址:https://blog.csdn.net/weixin_46066669/article/details/140367243