cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L432KC Nucleo failing to receive serial data from PC

MBenn.2
Associate

My intention is to program the STM32L432KC to utilize UART2 to monitor incoming serial data from the PC Virtual Com Port via interrupts.

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 = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
 
  }
}
 
 
 
static void MX_USART2_UART_Init(void)
{
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  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;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
}

A minimized version of my main function:

int main(void)
{
 
 HAL_Init();
 
 /* Configure the system clock */
 
 SystemClock_Config();
 
 MX_GPIO_Init();
 
 MX_USART2_UART_Init();
 
 static uint8_t aTxMessage[] = "ABCDEFGHIJKLMNOP";
 
 static uint8_t numOfBytes = sizeof(aTxMessage) - 1;
 
// Start UART Reception in interrupt mode
 HAL_UART_Receive_IT(&huart2, (uint8_t *)aRxBuffer, numOfBytes);
 
 
 while (1) {
    if (receivedByte || uartErrorOccured ) {
 
       // Clear Flag
       receivedByte = false;
       uartErrorOccured = false;
 
 
       // Send bytes to receiver
       HAL_UART_Transmit(&huart2, (uint8_t*)aTxBuffer, numOfBytes, 5000);
 
 
 
       // Put Receiver in interrupt reception mode
       HAL_UART_Receive_IT(&huart2, (uint8_t *)aRxBuffer, numOfBytes);
 
    }
 }
}
 
 

And two call backs for when reception is complete or error occurred.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
        // Set Reception Complete User Flag
	receivedByte = true;
 
}
 
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) {
        // Set UART Error User Flag
	uartErrorOccured = true;
 
}

When my program first initializes I typically get a frame error right off the bat. I assume this is just a symptom of GPIO initialization and throw away the error and restart the reception process. I make the assumption that these error flags are cleared by software by the HAL library functions.

I probed PA3 (UART2 RX) and I do not see any activity on the pin when I'm sending data from the PC. However, PA2 (UART TX) pin does show data whenever I include a UART_Transmit call, so I know transmission to the PC is working fine.

I setup a different Nucleo board (STM32F7) just to see if I could find what I'm doing wrong with the STM32L4, but I was successful in setting up the STM32F7 to receive UART interrupts from the PC. So there is just something about the STM32L4 that I'm not understanding and I'm hoping someone can shine light on my problem because it's driving me insane.

I also looked into the solder bridges SB2 and SB3, as I have read in some documentation that these redirect the UART pins away from the internal STM32-V2 debugger. I can verify that SB2 and SB3 are ON, as is the default configuration.

Thanks for any guidance!

3 REPLIES 3
KnarfB
Principal III

> GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;

On the STM32L432KC Nucleo board UART2 RX should be connected to PA15.

I've reconfigured the pins as follows:

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
	
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  
  if(huart->Instance==USART2)
  {
	  
    /* Peripheral clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    
    
    /* INITIALIZE UART2 TX PIN : STM32L432KC : UART TO PC VIRTUAL COM PORT VIA USB */
    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 
    /* INITIALIZE UART2 RX PIN : STM32L432KC : UART TO PC VIRTUAL COM PORT VIA USB */
    GPIO_InitStruct.Pin = GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF3_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    
    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
    
    
  }
}

The board is now successfully receiving bytes from the PC.

I had tried this pin configuration in the past, but what I realized today is that I needed to change the alternate function register from AF7 to AF3 for Pin_15;

Can you please point me to the right documentation or schematic that lists Pin 15 as the USB RX Pin? Everywhere I look says Pin 2 is connected to the debugger.

Thanks again!

KnarfB
Principal III

UM1956 User manual "STM32 Nucleo-32 boards (MB1180)", STM32CubeMX5 creat project with all peripherals.

hth

KnarfB