Skip to main content
Graduate
November 6, 2024
Solved

stm8l uart1 works but uart2 doesn't work

  • November 6, 2024
  • 2 replies
  • 1869 views

Dear all,

I'm now using stm8l15x to send 8 bits data with both UART1 & UART2. UART1 works fine & can communicate with my PC for both TX & RX via both polling & interrupt mode. But UART2 doesn't work. It doesn't output any waveform (always 1) with or without connecting to a PC. And the TC flag never set, where it set in UART1 after TX.

I tried to use nop() loop to replace reading flag status. It still output nothing. nop() loop works fine for toggling GPIO

I don't think it's a hardware problem as flag status should not be affected by hardware. Are there any other methods for me to debug?

Attached please find some of my code

 

void main(void)
{	
	// LED
	GPIO_Init(GPIOD, GPIO_Pin_5 | GPIO_Pin_4, GPIO_Mode_Out_PP_Low_Slow);
	// UART
	CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
	CLK_PeripheralClockConfig(CLK_Peripheral_USART2, ENABLE);
	GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_2, ENABLE);
	GPIO_ExternalPullUpConfig(GPIOE, GPIO_Pin_3, ENABLE);
	GPIO_Init(GPIOC, GPIO_Pin_3, GPIO_Mode_Out_PP_High_Fast);
	GPIO_Init(GPIOE, GPIO_Pin_4, GPIO_Mode_Out_PP_High_Fast);
	USART_Init(USART1, 9600, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, USART_Mode_Rx|USART_Mode_Tx);
	USART_Init(USART2, 9600, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No, USART_Mode_Rx|USART_Mode_Tx);
	USART_Cmd(USART1, ENABLE);
	USART_Cmd(USART2, ENABLE);
	
	while(1) {
		GPIO_ToggleBits(GPIOD, GPIO_Pin_4);
		USART_SendData8(USART1, '1');
		while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
		GPIO_ToggleBits(GPIOD, GPIO_Pin_5);
		USART_SendData8(USART2, '2');
		main_delay_us_polling(50000);
		//while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
	}
}

 

 

    This topic has been closed for replies.
    Best answer by AA1

    The MCU is stm8l152c6 but it only have USART1 as I said before.

    2 replies

    Visitor II
    November 6, 2024

    I don't know which board you are using. On some boards USART2 is used as VCP and is connected to ST-LINK, and by default is not available.

    SSin.1Author
    Graduate
    November 7, 2024

    Thanks for your reply. Sorry for missing hardware info

    The board I am using is STM8L-DISCOVERY development board (Ofcourse I removed the LCD display). Hust have a quick look on it's schematic. I can't find any connection between uart2 (PE3 & PE4) with on board ST Link

    Visitor II
    November 8, 2024

    STM8L-DISCOVERY board only have USART1 because MCU is stm8l152c4.

     

    Graduate II
    November 6, 2024

    Board details and schematics would help.

    Also dump out the UART2 registers, make sure baud rate, control and status are non-zero

    SSin.1Author
    Graduate
    November 7, 2024

    Thankyou. But how can I watch the register of UART2? I can only find UART1 in view > peripherial registers

    SSin1_1-1730950378980.png