AnsweredAssumed Answered

usart doesn't work on stm32 Nucleo F401

Question asked by denis.lamard on Sep 28, 2014
Latest reply on Sep 29, 2014 by Montassar BEN ROMDHANE
Hi friends,

I have nio clue why I can't send anu data to usart. I use a stm32 Nucleo F401 on the usart 2 and absolutly no signal on GPIOA Pin 2 and 3.

If you could help me.

Thanks a lot

My source code is very simple :

UART_HandleTypeDef UartHandle;

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    __GPIOA_CLK_ENABLE();
    __USART2_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void init_usart(void)
{
    UartHandle.Instance = USART2;

    UartHandle.Init.BaudRate = 9600;
    UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
    UartHandle.Init.StopBits = UART_STOPBITS_1;
    UartHandle.Init.Parity = UART_PARITY_NONE;
    UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    UartHandle.Init.Mode = UART_MODE_TX_RX;

    HAL_UART_Init(&UartHandle);

    __HAL_UART_ENABLE(&UartHandle);
}

int main(int argc, char* argv[])
{
    char str[5] = { "tests" };

    init_usart();

    HAL_UART_Transmit(&UartHandle, (uint8_t*) str, 1, 0XFFFF);
    HAL_UART_Transmit(&UartHandle, (uint8_t*) str, 1, 0XFFFF);
    while (1)
    {
        //HAL_UART_Transmit(&UartHandle, &ch, 1, 0XFFFF);
    }
}

Outcomes