Skip to main content
Julien33
Visitor II
February 17, 2022
Question

Receiving just one byte with an UART interrupt

Hi,

I'm currently programming on the board F446RE with STM32CubeIDE, I'm only trying to get an array from the UART but I'm quite block.

When I send an array with my computer only the first byte is sent and update in my array on the STM32. While I'm following the instruction from this youtube video: https://www.youtube.com/watch?v=xE6GVt7XuJI.

Currently my code look like this:

Here is my UART configuration:

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

static void MX_USART2_UART_Init(void)

{

 /* USER CODE BEGIN USART2_Init 0 */

 /* USER CODE END USART2_Init 0 */

 /* USER CODE BEGIN USART2_Init 1 */

 /* USER CODE END USART2_Init 1 */

 huart2.Instance = USART2;

 huart2.Init.BaudRate = 115200;

 huart2.Init.WordLength = UART_WORDLENGTH_8B;

 huart2.Init.StopBits = UART_STOPBITS_1;

 huart2.Init.Parity = UART_PARITY_NONE;

 huart2.Init.Mode = UART_MODE_TX_RX;

 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 huart2.Init.OverSampling = UART_OVERSAMPLING_16;

 if (HAL_UART_Init(&huart2) != HAL_OK)

 {

   Error_Handler();

 }

 /* USER CODE BEGIN USART2_Init 2 */

 /* USER CODE END USART2_Init 2 */

}

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

Here my Callback functions

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

/* USER CODE BEGIN 4 */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

   HAL_UART_Receive_IT(&huart2, rx_buff, 5);

   __NOP();

}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

   __NOP();

}

void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)

{

   __NOP();

}

/* USER CODE END 4 */

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

Here my main:

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_Transmit_IT(&huart2, mess, 20);

 HAL_UART_Receive_IT(&huart2, rx_buff, 5);

 HAL_UART_Transmit_IT(&huart2, tx_buff, 5);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

   /* USER CODE END WHILE */

   /* USER CODE BEGIN 3 */

    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

    HAL_Delay(500);

 }

 /* USER CODE END 3 */

}

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

To send the UART array I use the python's code

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

import serial

#Test un envoie sur l'interruption

ser = serial.Serial("/dev/ttyACM0", baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=500)

array = bytearray([8,2,3,4,5])

ser.write(array) #Envoie sur le port UART

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

Could you help me please? :(

Sincerely yours,

Julien

This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
February 17, 2022

HAL_UART_Receive_IT() return immediately

HAL_UART_Transmit_IT(&huart2, tx_buff, 5); will then send whatever's floating in the tx_buff

If you want to send the 5 bytes received, you're going to have to move the data in the buffers in the receive call-back, and do the HAL_UART_Transmit_IT() there.

Synchronization here is also a bit hopeful.. it will respond every time 5 bytes total are received

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

memcpy(tx_buff, rx_buff, 5);

   HAL_UART_Receive_IT(huart, rx_buff, 5);

HAL_UART_Transmit_IT(huart, tx_buff, 5);

}

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Piranha
Principal III
February 17, 2022