cancel
Showing results for 
Search instead for 
Did you mean: 

How to make correct timing when using UART RX/TX and EXTI_Callback function at the sametime?

gsung
Associate

Hi, I'm using NUCLEO-F103RB board and I want to transmit data to PC using with UART.

The code below is that if I receive data from PC, the specific data is transmitted to PC.

int main() {
	while (1) {
		HAL_UART_Receive(&huart2, uartRxData, 2, 1000);
 
		recvPortNum = uartRxData[0];
		masterChecksum = recvPortNum + uartRxData[1];
 
		if ((masterChecksum == 0) && ((recvPortNum == PORTA) || (recvPortNum == PORTB) || (recvPortNum == PORTC))) {
			HAL_UART_Transmit(&huart2, uartTxData[recvPortNum], ARRSIZE, 10);
		}
		HAL_Delay(1);
}

And independently, I made external interrupt fuction to get values of electric meter periodically (every 1 second).

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
	if (GPIO_Pin == GPIO_PIN_5) {
		readAllElectricMeter(PORTA);
		readAllElectricMeter(PORTB);
		readAllElectricMeter(PORTC);
	}
}

​But here's the problem.

If I use HAL_Delay(1) in the while loop, the EXTI_Callback function doesn't work periodically but UART_Receive works good. Otherwise, if I set HAL_delay(1000), the EXTI_Callback fuction is work but UART_Receive makes timeout.

I have no idea why the weird situation happens.

Can you give me some advice to solve this problem?

Thanks in advance.

0 REPLIES 0