cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 USART can send but not receive on Putty

mereldawu
Associate
Posted on August 27, 2015 at 12:05

Hi all, I'm very new to this so please bear with me.

I'm using STM32L053 Nucleo and I set up normal polling USART to COM4 of the PC. 

I can send data to the serial monitor (PuTTy) but I can't receive anything back from PuTTy. 

When I try to type on PuTTy it doesn't seem to register anything, but since I can send I'm assuming that my USART is set up correctly..

In my main:

char *msg = ''Press a key.\n\r'';

HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 0xFFFF);

HAL_UART_Receive(&huart2, (char*)c, strlen((char*)c), 0xFFFF);

HAL_UART_Transmit(&huart2, (uint8_t*)endl, strlen(endl), 0xFFFF);

msg = ''You pressed this key: '';

HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 0xFFFF);

The serial monitor prints out the following, and no matter what I press it makes no difference:

>> Press a key.

>> You pressed this key:

This is how I intialised USART:

    /* USART2 init function */

    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_ONEBIT_SAMPLING_DISABLED;

      huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

      HAL_UART_Init(&huart2);

    

    }

    /* Initialise USART MSP */

    void HAL_UART_MspInit(UART_HandleTypeDef* huart)

    {

      GPIO_InitTypeDef GPIO_InitStruct;

      if(huart->Instance==USART2)

      {

        /* Peripheral clock enable */

        __USART2_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_PULLUP;

        GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

        GPIO_InitStruct.Alternate = GPIO_AF4_USART2;

        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

      }   

    }

At this point I'm not sure if it's the code, the PuTTy setting, or the hardware. Any suggestion is appreciated. Thanks!

#usart #stm32 #stm32l053
2 REPLIES 2
raptorhal2
Lead
Posted on August 27, 2015 at 15:32

I believe your problem is that you have set up a receive without knowing when a byte has been received from puTTy. What is needed is either a receive interrupt handler, or polling looking for a received byte.

Since you seem to be attempting polling, I recommend adapting the ''UART/UART_TwoBoards_ComPolling'' example project in the STM32L0 HAL Library.

Edit: How are you assembling the input ''c'' into a transmit message buffer ?

Cheers, Hal

mereldawu
Associate
Posted on August 27, 2015 at 16:10

Hi,

Thank you so much for pointing me in the right direction!

Added the code below and it works now. 

Thanks! 🙂

if(HAL_UART_Receive(&huart2, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 0xFFFF) != HAL_OK)

  {

    Error_Handler();  

  }