cancel
Showing results for 
Search instead for 
Did you mean: 

I'm trying Tutorial #4 (UART Communication) from CubeMX manual, but it does not work. I'm using a Discovery STM32F334C8T6.

GammaSigma
Associate III

I have followed scrupulously every step, but Tera Term (or other terminal emulators) does not show any sign of communication with my board. The board is a Discovery STM32F334C8T6: the tutorial is tailored to a Nucleo board, but the pinout of the two boards are very similar.

  • Select Debug Serial Wire under SYS: done;
  • Select Internal Clock as clock source under TIM2 peripheral: done;
  • Select the Asynchronous mode for the USART2 peripheral: done;
  • Check that the signals are properly assigned on pins: USART_RX on PA3, TCK on PA14, USART_TX on PA2, USART_RX on PA3, done. Although they have slightly different names on my board (for example, pin PA14 is assigned to SYS_JTCK-SWCLK, and pin PA13 is assigned to SYS_JTMS-SWDIO).
  • Go to the Clock Configuration tab and leave the configuration untouched: done. The Nucleo-L073RZ has a default clock of 2.097 MHz: i have selected 2 MHz on mine to match as closely as possible.
  • From the Configuration tab, click USART2 to open the peripheral Parameter Settings window and set the baud rate to 9600. Make sure the Data direction is set to “Receive and Transmit�?: done.
  • Click TIM2 and change the pre-scaler to 16000, the Word Length to 8 bits and the Counter Period to 1000: done.
  • Enable TIM2 global interrupt from the NVIC Settings tab: done.
  • Add the user code as follows:

/* USER CODE BEGIN 0 */
 
#include "stdio.h"
 
#include "string.h"
 
/* Buffer used for transmission and number of transmissions */
 
char aTxBuffer[1024];
 
int nbtime=1;
 
/* USER CODE END 0 */
 
/* USER CODE BEGIN 2 */
 
/* Start Timer event generation */
 
HAL_TIM_Base_Start_IT(&htim2);
 
/* USER CODE END 2 */
 
/* USER CODE BEGIN 4 */
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
 
sprintf(aTxBuffer,"STM32CubeMX rocks %d times \t", ++nbtime);
 
HAL_UART_Transmit(&huart2,(uint8_t *) aTxBuffer, strlen(aTxBuffer), 5000);
 
}
 
/* USER CODE END 4 */

I compile under STM32CubeIDE, no errors. Enter debug mode, code is downloaded, debug mode started. Tera Term automatically discovers the board connected to COM11 port, with the correct settings, but it does not show any message incoming.

1 ACCEPTED SOLUTION

Accepted Solutions
Chris1
Senior III

User Manual UM1735 says:

"The USART2 interface available on PB3 and PB4 of the STM32F334C8T6 can be connected to ST-LINK/V2-1 MCU to use the virtual COM port function.

By default the USART2 communication between the target STM32F334C8T6 and ST-LINK/V2-1 MCU is not enabled.

To use the virtual COM port function with:

•The on-board STM32F334C8T6, then set SB14 and SB16 ON.

View solution in original post

6 REPLIES 6
S.Ma
Principal

I would try to use regular USART instead of the SWO debug line.

There should be a UART to USB conversion through the STLink on board the discovery. Use it.

Don't put things taking time in any callback function. Callbacks should be snappy.

sprintf() may not work directly, just send a single character to see if it works.

When setting the virtual com port, use the same baudrate as you programmed on STM32.

Thank you for your kind reply. Unfortunately I'm still a learner, so please bear with me: I am unsure about what you mean when you say that there should be a UART to USB conversion through the STLink on board the Discovery. I have read on the manual that USART2 is physically connected to the Discovery's USB port, and I have activated USART2 on the peripheral list, in Asynchronous mode and with the suggested settings (which, by the way, are the same as Tera Term: Baud Rate 9600, Word Length 8 bits, no parity, 1 Stop Bits, Receive and Transmit):

0690X00000DYU85QAH.jpg

0690X00000DYU98QAH.jpg

This is the pinout, as suggested by the manual:

0690X00000DYU8UQAX.jpg

I have activated the green LED as well, to verify the Callback function. I have followed your suggestion, and tried to send a single character instead of a string:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
	{
		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_9);
		char c = 'A';
		HAL_UART_Transmit_IT(&huart2, (uint8_t*)&c, 1);
	}

Whenever the timer2 sends an interrupt, the green LED toggles, so this proves that the Callback function works. But, again, the terminal windows stays silent:

0690X00000DYU8yQAH.jpg

Thank you for your patience.

O. Mz. - 7
Associate III

Did you check for sure that the UART2 is on the COM11 on your PC? Did you check on the scheme in the PDF file that the UART2 is indeed on PA2 and PA3 on this board?

Could you try to send this:

while(1) {
 
    HAL_UART_Transmit(&huart2, (uint8_t*) "#", 1, 1000);
 
    HAL_Delay(500);
 
}

I use CoolTerm soft for monitoring UART.

Chris1
Senior III

User Manual UM1735 says:

"The USART2 interface available on PB3 and PB4 of the STM32F334C8T6 can be connected to ST-LINK/V2-1 MCU to use the virtual COM port function.

By default the USART2 communication between the target STM32F334C8T6 and ST-LINK/V2-1 MCU is not enabled.

To use the virtual COM port function with:

•The on-board STM32F334C8T6, then set SB14 and SB16 ON.

Thank you for your reply. Yes, the correct COM port number is 11: I had simply selected the wrong pins, PA2 and PA3 instead of PB3 and PB4. Now everything works fine.

Thank you again!

Thank you very much! I was absolutely adamant that the correct pins were PA2 and PA3 (they weren't): I don't know how I could miss that passage on the UM1735. Now everything works perfectly.

Thanks again!