cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32F103 uart2 with STM32Cube Mx

rajesh2399
Associate
Posted on September 01, 2016 at 13:22

Hi, I am using a nucleo - F103RB board for testing its UART. I am using the STM32Cube Mx to generate the codes for the start. I can successfully generate working codes for Usart1 and it can successfully transmit and receive using the HAL library. But when I generate codes for Usart2 using the STM32Cube, I can no longer transmit or receive any data. Does the STM32cube not generate all the required codes for USART2? I am quite new to this so please explain why USART1 is different than USART2 in this case.

here is the auto generated code for the USART2

void MX_USART2_UART_Init(void)

{

  huart2.Instance = USART2;

  huart2.Init.BaudRate = 115200;

  huart2.Init.WordLength = UART_WORDLENGTH_8B;

  huart2.Init.StopBits = UART_STOPBITS_1;

  huart2.Init.Parity = UART_PARITY_NONE;

  huart2.Init.Mode = UART_MODE_TX_RX;

  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart2.Init.OverSampling = UART_OVERSAMPLING_16;

  if (HAL_UART_Init(&huart2) != HAL_OK)

  {

    Error_Handler();

  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(uartHandle->Instance==USART2)

  {

  /* USER CODE BEGIN USART2_MspInit 0 */

  /* USER CODE END USART2_MspInit 0 */

    /* Peripheral clock enable */

    __HAL_RCC_USART2_CLK_ENABLE();

 

    /**USART2 GPIO Configuration    

    PA2     ------> USART2_TX

    PA3     ------> USART2_RX

    */

    GPIO_InitStruct.Pin = GPIO_PIN_2;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_3;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* Peripheral DMA init*/

 

    hdma_usart2_rx.Instance = DMA1_Channel6;

    hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;

    hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;

    hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;

    hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

    hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;

    hdma_usart2_rx.Init.Mode = DMA_CIRCULAR;

    hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;

    if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)

    {

      Error_Handler();

    }

    __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx);

    /* Peripheral interrupt init */

    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(USART2_IRQn);

  /* USER CODE BEGIN USART2_MspInit 1 */

  /* USER CODE END USART2_MspInit 1 */

  }

}

#usart2 #stm32f10
1 REPLY 1
rajesh2399
Associate
Posted on September 05, 2016 at 10:12

I flagged this query to Tech support and they told me about

USART communication

The

USART2

interface available on PA2 and PA3 of the STM32 microcontroller can be

connected to ST-LINK MCU, ST morpho connector or to Arduino connector. The choice can

be changed by setting the related solder bridges. By default the USART2 communication

between the target MCU and ST-LINK MCU is enabled, in order to support Virtual Com Port

for mbed (SB13 and SB14 ON, SB62 and SB63 OFF). If the communication between the

target MCU PA2 (D1) or PA3 (D0) and shield or extension board is required, SB62 and

SB63 should be ON, SB13 and SB14 should be OFF. In such case it is possible to connect

another USART to ST-LINK MCU using flying wires between ST morpho connector and

CN3. For instance on NUCLEO-F103RB it is possible to use USART3 available on PC10

(TX) and PC11 (RX). Two flying wires

need to be connected as follow:

•PC10 (USART3_TX) available on CN7 pin 1 to CN3 pin RX

•PC11 (USART3_RX) available on CN7 pin 2 to CN3 pin TX

So I disconnected the

SB13

and

SB14

soldering points to disable the USART2 for debugging, connected

SB62

and

SB63

soldering points to enable pins at PA2(TX) and PA3(RX) and then connected PC10 to CN3 pin RX, PC11 to CN3 pin TX to enable USART3 for debugging.