cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive_IT,continuous reception

sak hos
Associate III
Posted on August 23, 2017 at 01:28

Hello, i'm using STM32F103c8t6 and i'm on a project where i have to send 8 bytes from an MCU to another in a asynchronous way , so i used HAL_UART_Receive_IT() , but i remarked that the HAL_UART_RxCpltCallback() is called only for the first reception of 8 bytes

does anyone have an idea that can help me

thank you

3 REPLIES 3
Posted on August 24, 2017 at 16:54

Hi

hos.sak

,

Maybe if you could provide to us more information about your issue we could help you better, maybe some lines of your code and the settings.

In the following post you can find some information about receiving data through the serial port in polling mode not interruption, there is and example code that you can download for the same micro that you are using made with CubeMX and for System Workbench, check it and tell us more...

https://community.st.com/0D50X00009XkXwWSAV

Posted on August 24, 2017 at 17:47

Hi Elkin Granados,

thx for the reply ,

my probleme is that when i use HAL_UART_Receive_IT() i can receive the data only one time, because this function will be nver called again so my solution was to use HAL_UART_Receive_IT() in the while loop in order to receive data from uart using interrupt .

please chekc my code below.

/******************************* 

usart.c

/*******************************

void MX_USART1_UART_Init(void)

{

huart1.Instance = USART1;

huart1.Init.BaudRate = 115200;

huart1.Init.WordLength = UART_WORDLENGTH_8B;

huart1.Init.StopBits = UART_STOPBITS_1;

huart1.Init.Parity = UART_PARITY_NONE;

huart1.Init.Mode = UART_MODE_TX_RX;

huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart1.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart1) != HAL_OK)

{

Error_Handler();

}

}

/* USART2 init function */

void MX_USART2_UART_Init(void)

{

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();

}

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)

{

GPIO_InitTypeDef GPIO_InitStruct;

if(uartHandle->Instance==USART1)

{

/* USER CODE BEGIN USART1_MspInit 0 */

/* USER CODE END USART1_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_USART1_CLK_ENABLE();

/**USART1 GPIO Configuration

PA9 ------> USART1_TX

PA10 ------> USART1_RX

*/

GPIO_InitStruct.Pin = GPIO_PIN_9;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_10;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* Peripheral interrupt init */

HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART1_IRQn);

/* USER CODE BEGIN USART1_MspInit 1 */

/* USER CODE END USART1_MspInit 1 */

}

else if(uartHandle->Instance==USART2)

{

/* USER CODE BEGIN USART2_MspInit 0 */

/* USER CODE END USART2_MspInit 0 */

/* Peripheral clock enable */

__HAL_RCC_USART2_CLK_ENABLE();

/**USART2 GPIO Configuration

PA2 ------> USART2_TX

PA3 ------> USART2_RX

*/

GPIO_InitStruct.Pin = GPIO_PIN_2;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* Peripheral interrupt init */

HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(USART2_IRQn);

/* USER CODE BEGIN USART2_MspInit 1 */

/* USER CODE END USART2_MspInit 1 */

}

}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)

{

if(uartHandle->Instance==USART1)

{

/* USER CODE BEGIN USART1_MspDeInit 0 */

/* USER CODE END USART1_MspDeInit 0 */

/* Peripheral clock disable */

__HAL_RCC_USART1_CLK_DISABLE();

/**USART1 GPIO Configuration

PA9 ------> USART1_TX

PA10 ------> USART1_RX

*/

HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

/* Peripheral interrupt Deinit*/

HAL_NVIC_DisableIRQ(USART1_IRQn);

/* USER CODE BEGIN USART1_MspDeInit 1 */

/* USER CODE END USART1_MspDeInit 1 */

}

else if(uartHandle->Instance==USART2)

{

/* USER CODE BEGIN USART2_MspDeInit 0 */

/* USER CODE END USART2_MspDeInit 0 */

/* Peripheral clock disable */

__HAL_RCC_USART2_CLK_DISABLE();

/**USART2 GPIO Configuration

PA2 ------> USART2_TX

PA3 ------> USART2_RX

*/

HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

/* Peripheral interrupt Deinit*/

HAL_NVIC_DisableIRQ(USART2_IRQn);

/* USER CODE BEGIN USART2_MspDeInit 1 */

/* USER CODE END USART2_MspDeInit 1 */

}

}

/******************************/

main.c

/*****************************/

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();

/* Configure the system clock */

SystemClock_Config();

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_SPI1_Init();

MX_I2C1_Init();

MX_RTC_Init();

MX_USART1_UART_Init();

MX_USART2_UART_Init();

ScreenCurrentState=SCREEN_STATE_ON;

Screen_Init();

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

HAL_UART_Receive_IT(&huart1,ReceivedData,8);

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

Posted on August 24, 2017 at 18:18

Hi

hos.sak

‌,

Sure!, when the UART IRQ handler is called it disable the Rx ITbefore the call of the callback interrupt function :

0690X00000607viQAA.png

I suggest to you to declare a volatile globalvariable out of the main function, and change the value of this when the callback is called:

volatile uint8_t flag = 0;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

//Your code when receive the data

flag = 1;

}

and in the main:

if(flag)

{

//If you enter here the data have been received, so you must to call again the function

HAL_UART_Receive_IT(&huart1,ReceivedData,8);

flag=0; //Clear the flag

}

So the receive function is just called when some data have been received and the interruption have been disabled.