cancel
Showing results for 
Search instead for 
Did you mean: 

Two STM32 - UART communication

Raf_al
Associate III

Hello,

I've been doing a small project lately and ran into a problem in one part. I have two STM32 microcontrollers and I want to communicate both of them with eachother over UART. One of them is designed to send a fixed array of 6 characters

uint8_t k[6]={3,7,9,12,16,19};

The other microcontroller is simply to receive it. When I send a single byte over the UART it all works without a problem. When I want to send the whole array it only fills the first element.This first element is filled with random numbers from this sent array.

I know for sure there is no problem on the sending side. I checked it on an oscilloscope. The problem lies in the receiving side.

I suspect that the problem lies in the timing, but I don't know how to fix it

In both microcontrollers the baud rate is the same – 9600. The data frame is also the same 8N1.

 

RX device code

 

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

uint8_t buff[6];

/* USER CODE END 0 */

 

/* USER CODE BEGIN WHILE */

while (1)

  {

 

       HAL_UART_Receive(&huart2, buff, sizeof(buff), 10000);

 

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

  }

/* USER CODE END 3 */

 

TX device code

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

uint8_t k[6]={3,7,9,12,16,19};

/* USER CODE END 0 */

 

/* USER CODE BEGIN WHILE */

while (1)

  {

       HAL_UART_Transmit(&huart2, k, sizeof(k), 10);

 

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

 

Raf_al_0-1713282252041.pngRaf_al_1-1713282260393.png

Raf_al_2-1713282267462.png

 

12 REPLIES 12

@Raf_al wrote:

That's my mistake. I mean integers not characters. Even when I start transmission after 10 minutes after power up it fills only first element of the array.


Do yourself a favour, and make them actual printable characters.

Then you can test each part separately against a terminal on a PC.

And, when you connect the two STM32s together, you can use PC terminals to monitor what's happening between the them:

https://www.avrfreaks.net/s/topic/a5C3l000000UaFXEA0/t153137?comment=P-1212903

 

#DebugSerialComms

Karl Yamashita
Lead III

Try using interrupt method instead of polling. 

 

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t buff[6];
volatile bool dataRdy = false;
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive_IT(&huart2, buff, sizeof(buff)); // initialize to receive
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  if(dataRdy)
	  {
		  dataRdy = 0;
		  if(buff[0] == 3) // check to see if first byte is 3
		  {
			  HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); // toggle led if data is valid
		  }
	  }


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}



/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart == &huart2)
	{
		dataRdy = true;
		HAL_UART_Receive_IT(&huart2, buff, sizeof(buff)); // re-initialize again
	}
}
/* USER CODE END 4 */

 

Add a delay to your transmit

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

uint8_t k[6]={3,7,9,12,16,19};

/* USER CODE END 0 */

 

/* USER CODE BEGIN WHILE */

while (1)

  {

       HAL_UART_Transmit(&huart2, k, sizeof(k), 10);
    HAL_Delay(10);
 

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

 

Make sure you have NVIC enabled

KarlYamashita_0-1713386850733.png

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
Raf_al
Associate III

So I did what I wrote in my previous post:

 


@Raf_al wrote:

OK, tomorrow I will do test, beacuse now I dont have access to those boards. First I will power up receiver and it will be waiting to receive 6 bytes (I will change timeout in receive function to HAL_MAX_DELAY). Then I will change code on TX side a little bit, and after pushing some button I will send those 6 bytes.

 

 


And it works like a charm. Problem was with timing, probably my receive hit somewhere in the middle of transmit frame.

Thanks for help.