cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f767 usart2

Vaclav Zajic
Associate II
Posted on May 22, 2018 at 15:07

Hello, 

I can not get usart2 working in asynchronous mode on STM32f767zi-nucleo.

My clock configuration function contains

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_CLK48;

PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

In main() I call 

static void MX_USART2_UART_Init(void)

{

huart2.Instance = USART2;

huart2.Init.BaudRate = 115200;

huart2.Init.WordLength = /*UART_WORDLENGTH_7B*/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;

huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

When I try to send something to USART2 with HAL_UART_Transmit I get return code HAL_OK, but I can't  see anything on serial port.

Can you give me an advice?

Thank you

Vaclav 

5 REPLIES 5
Posted on May 22, 2018 at 15:20

Read back and check the relevant RCC, GPIO and UART registers.

JW

Vaclav Zajic
Associate II
Posted on May 22, 2018 at 15:43

After trying to send a message the USART2 registers look like this:

CR1 0x0

CR2 0x0

CR3 0x0

BRR 0x0

GTPR 0x0

RTOR 0x0

RQR empty

ISR 0xc0

ICR empty

RDR 0x0

TDR 0x0

Posted on May 22, 2018 at 16:30

 ,

 ,

Looks like the clock isn't actually enabled. USART2 on PA2/PA3 would look like this in a complete form. CubeMX spreads things all over the place. Check the pin/clock stuff generated in the MSP code

void USART2_Init(void)

 ,

{

 ,

 , GPIO_InitTypeDef , GPIO_InitStruct,

 , /* ♯ ♯ -1- Enable peripherals and GPIO Clocks ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ */

 ,

 , /* Enable GPIO TX/RX clock */

 ,

 , ,  ,__HAL_RCC_GPIOA_CLK_ENABLE(),

 ,

 , /* Enable USART2 clock */

 ,

 , ,  ,__HAL_RCC_USART2_CLK_ENABLE(),

 ,

 , ,  ,

 ,

 , /* ♯ ♯ -2- Configure peripheral GPIO ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ */

 ,

 , /* UART TX and RX GPIO pin configuration , */

 ,

 , GPIO_InitStruct.Pin , , , , , , = GPIO_PIN_2 | GPIO_PIN_3,

 ,

 , GPIO_InitStruct.Mode , , , , , = GPIO_MODE_AF_PP,

 ,

 , GPIO_InitStruct.Pull , , , , , = GPIO_PULLUP,

 ,

 , GPIO_InitStruct.Speed , , , , = GPIO_SPEED_FAST,

 ,

 , GPIO_InitStruct.Alternate = GPIO_AF7_USART2,

 , HAL_GPIO_Init(GPIOA, &,GPIO_InitStruct),

/* ♯ ♯ -1- Configure the UART peripheral ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ */

 ,

 , /* Put the USART peripheral in the Asynchronous mode (UART Mode) */

 ,

 , /* UART configured as follows:

 ,

 , , , , , - Word Length = 8 Bits

 ,

 , , , , , - Stop Bit = One Stop bit

 ,

 , , , , , - Parity = None

 ,

 , , , , , - BaudRate = 9600 baud

 ,

 , , , , , - Hardware flow control disabled (RTS and CTS signals) */

 ,

 , UartHandle.Instance , , , , , , , = USART2,

 , UartHandle.Init.BaudRate , , = 115200,

 ,

 , 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,

 ,

♯ ifdef UART_ADVFEATURE_NO_INIT

 ,

 , UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT,

 ,

♯ endif

 , if (HAL_UART_DeInit(&,UartHandle) != HAL_OK)

 ,

 , {

 ,

 , , , Error_Handler(),

 ,

 , }

 , if (HAL_UART_Init(&,UartHandle) != HAL_OK)

 ,

 , {

 ,

 , , , Error_Handler(),

 ,

 , }

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 22, 2018 at 18:26

As Clive said, this indicates that the USART clock is not enabled. You should verify this by reading out the relevant RCC registers.

Blind shot: Is PeriphClkInitStruct initialized in all its elements?

JW

Vaclav Zajic
Associate II
Posted on May 23, 2018 at 09:11

Hello, the problem was that I forgot to enable the USART clock by 

__USART2_CLK_ENABLE. It works now. Thank you all.

Vaclav