Skip to main content
SA.17
Associate III
August 28, 2019
Question

Microcontroller stucks when uart not connected

  • August 28, 2019
  • 0 replies
  • 543 views

i am using stm32L072

I am using uart1 in the code

I found that code doesn't works ,if i don't connect a usb to uart converter between PC and the microcontroller .

When i connect usb to uart connector and reset microcontroller ,it starts working :weary_face: or if i comment uart_init function ,the code works fine without usb-uart converter .

What might be the issue ?

Below is uart initialization code :

static void MX_USART1_UART_Init(void)
{
 
 /* USER CODE BEGIN USART1_Init 0 */
/* (1) Enable GPIO clock and configures the USART pins *********************/
 
 /* Enable the peripheral clock of GPIO Port */
 USARTx_GPIO_CLK_ENABLE();
 
 /* Configure Tx Pin as : Alternate function, High Speed, Push pull, Pull up */
 LL_GPIO_SetPinMode(USARTx_TX_GPIO_PORT, USARTx_TX_PIN, LL_GPIO_MODE_ALTERNATE);
 USARTx_SET_TX_GPIO_AF();
 LL_GPIO_SetPinSpeed(USARTx_TX_GPIO_PORT, USARTx_TX_PIN, LL_GPIO_SPEED_FREQ_LOW);
 LL_GPIO_SetPinOutputType(USARTx_TX_GPIO_PORT, USARTx_TX_PIN, LL_GPIO_OUTPUT_PUSHPULL);
 LL_GPIO_SetPinPull(USARTx_TX_GPIO_PORT, USARTx_TX_PIN, LL_GPIO_PULL_UP);
 
 /* Configure Rx Pin as : Alternate function, High Speed, Push pull, Pull up */
 LL_GPIO_SetPinMode(USARTx_RX_GPIO_PORT, USARTx_RX_PIN, LL_GPIO_MODE_ALTERNATE);
 USARTx_SET_RX_GPIO_AF();
 LL_GPIO_SetPinSpeed(USARTx_RX_GPIO_PORT, USARTx_RX_PIN, LL_GPIO_SPEED_FREQ_LOW);
 LL_GPIO_SetPinOutputType(USARTx_RX_GPIO_PORT, USARTx_RX_PIN, LL_GPIO_OUTPUT_PUSHPULL);
 LL_GPIO_SetPinPull(USARTx_RX_GPIO_PORT, USARTx_RX_PIN, LL_GPIO_PULL_UP);
 
 /* (2) NVIC Configuration for USART interrupts */
 /* - Set priority for USARTx_IRQn */
 /* - Enable USARTx_IRQn */
 NVIC_SetPriority(USARTx_IRQn, 0); 
 NVIC_EnableIRQ(USARTx_IRQn);
 
 /* (3) Enable USART peripheral clock and clock source ***********************/
 USARTx_CLK_ENABLE();
 
 /* Set clock source */
 USARTx_CLK_SOURCE();
 
 /* (4) Configure USART functional parameters ********************************/
 
 /* Disable USART prior modifying configuration registers */
 /* Note: Commented as corresponding to Reset value */
 // LL_USART_Disable(USARTx_INSTANCE);
 
 /* TX/RX direction */
 LL_USART_SetTransferDirection(USARTx_INSTANCE, LL_USART_DIRECTION_TX_RX);
 
 /* 8 data bit, 1 start bit, 1 stop bit, no parity */
 LL_USART_ConfigCharacter(USARTx_INSTANCE, LL_USART_DATAWIDTH_8B, LL_USART_PARITY_NONE, LL_USART_STOPBITS_1);
 
 /* No Hardware Flow control */
 /* Reset value is LL_USART_HWCONTROL_NONE */
 // LL_USART_SetHWFlowCtrl(USARTx_INSTANCE, LL_USART_HWCONTROL_NONE);
 
 
 
 /* Set Baudrate to 115200 using APB frequency set to 16000000 Hz */
 /* Frequency available for USART peripheral can also be calculated through LL RCC macro */
 /* Ex :
 Periphclk = LL_RCC_GetUSARTClockFreq(Instance); or LL_RCC_GetUARTClockFreq(Instance); depending on USART/UART instance
 
 In this example, Peripheral Clock is expected to be equal to 16000000 Hz => equal to SystemCoreClock
 */
 LL_USART_SetBaudRate(USARTx_INSTANCE, SystemCoreClock, LL_USART_OVERSAMPLING_16, 115200); 
 
 /* (5) Enable USART *********************************************************/
 LL_USART_Enable(USARTx_INSTANCE);
 
 /* Polling USART initialisation */
 while((!(LL_USART_IsActiveFlag_TEACK(USARTx_INSTANCE))) || (!(LL_USART_IsActiveFlag_REACK(USARTx_INSTANCE))))
 { 
 }
 
 /* Enable RXNE and Error interrupts */
 LL_USART_EnableIT_RXNE(USARTx_INSTANCE);
 LL_USART_EnableIT_ERROR(USARTx_INSTANCE);
 /* USER CODE END USART1_Init 0 */

This topic has been closed for replies.