cancel
Showing results for 
Search instead for 
Did you mean: 

Not receiving data on PC com(RS232) port, when transmitting data from STM32F407VG DISCOVERY board (Using Baseboard STM32F4 DIS-BB RS232 port ie COM1)

VTOL_Aviations
Associate II

Hello,

I am facing trouble in sending data out from STM32F407VG Discovery board (connected to base board) using UART6 (J1 and J2 jumpers are connected). 

Connected the Tx pin of RS232 port on baseboard to PC COM port Rx pin and sending data periodically. I have enabled DMA on the board and program is in FreeRTOS.

Using baud rate 115200 at both ends, 8 bits no parity, 1 stop bit.

(Using code generated using CubeMX 5.1.0 and added my code on top of it) 

Using Atollic TrueSTUDIO® for STM32, Built on Eclipse Neon.1a. Version: 9.0.1 Build id: 20180420-1214

On the receiving side, have a blocking receive call which waits for each byte (Linux) as below which is tested with some other program and working:

 read(fd,arg_buf,1);

 // arg_buf is char array pointer

The receiving side remains blocked not receiving any data. That is the issue.

Checked similar issues on forum to no avail.

Code brief (STM32 code on FreeRTOS):

-------------------------------------

#include "main.h"
#include "cmsis_os.h"
--
--
UART_HandleTypeDef huart2;
UART_HandleTypeDef huart6;
DMA_HandleTypeDef hdma_usart6_rx;
DMA_HandleTypeDef hdma_usart6_tx;
 
 
osThreadId defaultTaskHandle;
TaskHandle_t Handle;
  
void SystemClock_Config(void);
 static void MX_GPIO_Init(void);
 static void MX_DMA_Init(void);
 static void MX_USART2_UART_Init(void);
 static void MX_USART6_UART_Init(void);
 
void StartDefaultTask(void const * argument);
 void Task1(void *pvParameters)
 {
 	volatile TickType_t StartTime, EndTime, ExecutionTime, Time_value;
 	TickType_t xLastWakeTime;
 	const TickType_t xPeriod = pdMS_TO_TICKS( 10 );
 
        xLastWakeTime = xTaskGetTickCount();
        while(1)
        {
           StartTime = xTaskGetTickCount();
           ----
           ----
 		if( HAL_UART_Transmit_DMA(&huart6, tx_pos, 16)!= HAL_OK )
 		{
 			 Error_Handler();
 		}
          ----
          vTaskDelayUntil( &xLastWakeTime, xPeriod );
        }
 }
 
 
int main(void)
 {
  
   HAL_Init();
   SystemClock_Config();
 
   if( xTaskCreate( Task1, "Task1", 500, NULL, 1, &Handle ) != pdPASS)
   {
  	 return 0;
   }
   
 /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_USART6_UART_Init();
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
  osKernelStart();
  while (1)
  {
 
  }
}
 
void SystemClock_Config(void)
{
 
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 /* Configure the main internal regulator output voltage */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /* Initializes the CPU, AHB and APB busses clocks */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 50;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
   Error_Handler();
  }
 
 /** Initializes the CPU, AHB and APB busses clocks */
  
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
           
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
 {
  Error_Handler();
 }
}
 
static void MX_USART6_UART_Init(void)
{
 d.Instance = USART6;
 huart6.Init.BaudRate = 115200;
 huart6.Init.WordLength = UART_WORDLENGTH_8B;
 huart6.Init.StopBits = UART_STOPBITS_1;
 huart6.Init.Parity = UART_PARITY_NONE;
 huart6.Init.Mode = UART_MODE_TX_RX;
 huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart6.Init.OverSampling = UART_OVERSAMPLING_16;
 
 if (HAL_UART_Init(&huart6) != HAL_OK)
 {
  Error_Handler();
 }
}
static void MX_DMA_Init(void) 
{
 __HAL_RCC_DMA2_CLK_ENABLE();
  HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 5, 0);
  HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
  HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 5, 0);
  HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn); 
}

Thank you

3 REPLIES 3
Jack Peacock_2
Senior III

Do you know if the STM32 is using the right clock frequency to generate the baud rate? A common problem with eval serial ports is the on-board HSE crystal doesn't match the software. You may be selecting 115K but that doesn't mean it's the real frequency.

Also, I assume you did connect the GND ground wire on the RS-232 connector? It takes a minimum of 3 wires. Also, if either side has RTS-CTS handshake enabled you need to connect those pins too.

Jack Peacock

Try a much simpler test with USART6 PC6/PC7, as I recall, and without DMA/RTOS, and prove you have that functioning viably.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I've used HSI crystal. And APB1 and APB2 are getting 25 and 50 MHz respectively. I've written a program to receive data on PC(linux). And when the baud rate is set to 115200, nothing was coming. And I've just changed the baud rate to 9600 after seeing your reply. And boom.. It was working like charm.

I don't understand why 115200 baud wouldn't work even though the peripheral clock for APB2 bus is 50MHz(I believe UART6 is on APB2). At least I'm not getting any corrupted data also for 115200 baud.

If we keep 115200 baud, the time taken for one bit to transfer is (1/115200 = 8.6 micro seconds). And if the APB2 bus clock frequency is 50 MHz(0.02 micro seconds time period). I believe it should suffice to produce that baud rate. Or am I making a terrible mistake of making such calculation? Please enlighten.

Aditya.