Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

例程 stm32f103-msh 串口无法正常输入 #5

Open
fungaren opened this issue Jul 10, 2020 · 0 comments
Open

例程 stm32f103-msh 串口无法正常输入 #5

fungaren opened this issue Jul 10, 2020 · 0 comments

Comments

@fungaren
Copy link

fungaren commented Jul 10, 2020

根据官方文档,在 Ubuntu 上自行编译 RT-Thread Nano 中的例程 bsp/stm32f103-msh,发现串口无法正常输入。

然后,测试 RT-Thread v4.0.3,使用例程 bsp/stm32/stm32f103-mini-system,发现工作正常。

最后自行研究了两天,发现问题出在 Nano 版本的 bsp/stm32f103-msh/drivers/board.c 文件,原来代码是这样的:

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    if (huart->Instance == USART2)
    {
        __HAL_RCC_USART2_CLK_ENABLE();

        __HAL_RCC_GPIOA_CLK_ENABLE();
        /**USART2 GPIO Configuration
        PA2     ------> USART2_TX
        PA3     ------> USART2_RX
        */
        GPIO_InitStruct.Pin = USART_TX_Pin | USART_RX_Pin;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
}

改为:

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    if (huart->Instance == USART2)
    {
        __HAL_RCC_USART2_CLK_ENABLE();

        __HAL_RCC_GPIOA_CLK_ENABLE();
        GPIO_InitStruct.Pin = USART_TX_Pin;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
        /* Configure USART Rx as alternate function push-pull */
        GPIO_InitStruct.Pin =  USART_RX_Pin;
        GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
}

然后再测试,就能正常输入了。

PS. 似乎串口的 Rx 必须设置为 GPIO_MODE_INPUT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant