无垠的广袤 · 1 天前

【Mini-F5375-OB开发板评测】串口通信

【Mini-F5375-OB开发板评测】串口通信

本文介绍了灵动 Mini-F5375-OB 开发板实现串口通信的项目设计,包括串口打印、echo回复等功能实现。

串口打印

由调试器原理图可知,

link_uart_pins.jpg

板载调试器的虚拟串口与主控引脚的对应关系如下

DebuggerMCU PinUART numberPin reuse
VCP_TXPC11USART3_RXAF7
VCP_RXPC10USART3_TXAF7

board_uart.jpg

代码

包括主函数、平台初始化、串口 printf 重定向、串口轮询回复等部分。

主函数

#include "platform.h"
#include "usart_polling.h"
#include "main.h"

int main(void)
{
    PLATFORM_Init();
    USART_Polling_Sample();
    while (1){
    }
}

平台初始化

/***********************************************************************************************************************
  * @brief  Initialize Platform
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void PLATFORM_Init(void)
{
    PLATFORM_InitDelay();
    PLATFORM_InitConsole(115200);
    PLATFORM_PrintInfo();
}
/***********************************************************************************************************************
  * @brief  UART configuration
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void USART_Configure(uint32_t Baudrate)
{
    GPIO_InitTypeDef  GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

    USART_StructInit(&USART_InitStruct);
    USART_InitStruct.USART_BaudRate   = Baudrate;
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct.USART_StopBits   = USART_StopBits_1;
    USART_InitStruct.USART_Parity     = USART_Parity_No;
    USART_InitStruct.USART_Mode       = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART3, &USART_InitStruct);

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_7);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_7);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_10;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStruct);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_11;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_FLOATING;
    GPIO_Init(GPIOC, &GPIO_InitStruct);

    USART_Cmd(USART3, ENABLE);
}
/***********************************************************************************************************************
  * @brief  Initialize console for printf
  * @note   none
  * @param  Baudrate : UART3 communication baudrate
  * @retval none
  *********************************************************************************************************************/
void PLATFORM_InitConsole(uint32_t Baudrate)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

    USART_StructInit(&USART_InitStruct);
    USART_InitStruct.USART_BaudRate   = Baudrate;
    USART_InitStruct.USART_StopBits   = USART_StopBits_1;
    USART_InitStruct.USART_Parity     = USART_Parity_No;
    USART_InitStruct.USART_Mode       = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART3, &USART_InitStruct);

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_7); // TX

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_10;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOC, &GPIO_InitStruct);

    USART_Cmd(USART3, ENABLE);
}

串口重定向

/***********************************************************************************************************************
  * @brief  redefine fputc function
  * @note   for printf
  * @param  ch
  * @param  f
  * @retval ch
  *********************************************************************************************************************/
int fputc(int ch, FILE *f)
{
    USART_SendData(USART3, (uint8_t)ch);

    while (RESET == USART_GetFlagStatus(USART3, USART_FLAG_TC))
    {
    }

    return (ch);
}

串口轮询函数

/***********************************************************************************************************************
  * @brief UART Polling function
  * @note   none
  * @param  none
  * @retval none
  *********************************************************************************************************************/
void USART_Polling_Sample(void)
{
    uint8_t Data = 0;

    printf("\r\nTest %s", __FUNCTION__);

    USART_Configure(115200);

    while (1)
    {
        if(RESET != USART_GetFlagStatus(USART3, USART_FLAG_PE | USART_FLAG_FE | USART_FLAG_NF | USART_FLAG_ORE))
        {
            USART_ReceiveData(USART3);
        }

        if (SET == USART_GetFlagStatus(USART3, USART_FLAG_RXNE))
        {
            Data = USART_ReceiveData(USART3);

            USART_SendData(USART3, Data);

            while (RESET == USART_GetFlagStatus(USART3, USART_FLAG_TC))
            {
            }
        }
    }
}

保存代码,连接串口调试助手,复位芯片,实现串口通信。

效果

echo 回复

短按 RST 键打印开发板参数信息、时钟频率等;

发送字符串,回复相同信息。

printf_polling.jpg

串口打印

轮询函数中添加串口打印代码

void USART_Polling_Sample(void)
{
    printf("\r\nHello World!");
    PLATFORM_DelayMS(500);
}

保存代码、编译工程并上传固件。

串口调试助手连续打印字符串,间隔为 1 秒

print_hello.gif

总结

本文介绍了灵动 Mini-F5375-OB 开发板实现串口通信的项目设计,包括串口打印、echo回复等功能实现,为后续串口中断控制做好铺垫,也为 MM32F5 系列单片机的应用和快速开发提供了参考。

推荐阅读
关注数
1
内容数
7
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息