cancel
Showing results for 
Search instead for 
Did you mean: 

usart receive

soundarya_h_s
Associate III

USART receive is not providing the output please check and reply me asap

selected USART2 made it to asynchronous also enabled global interrupt
code lines that i have added

uint8_t txbuf[10]="hello ";

uint8_t rxbuf[10];

 

while (1)

{

/* USER CODE END WHILE */

HAL_UART_Receive(&huart2, rxbuf, sizeof(rxbuf), 100);

/* USER CODE BEGIN 3 */

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

HAL_UART_Transmit(&huart2, rxbuf, sizeof(rxbuf), 100);

}

11 REPLIES 11
KDJEM.1
ST Employee

Hello @soundarya_h_s ,

If you using the interrupt mode, the transmit and receive functions are HAL_UART_Transmit_IT() and HAL_UART_Receive_IT(). For that, I recommend you to take a look to this FAQ: How to use STM32 HAL UART driver API and Callbacks ?

Also, I think Getting started with UART can help you.

Thank you.

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Karl Yamashita
Lead III

A better approach is to use the idle interrupt. That way you can receive any length string. You make your array size the longest string/bytes you'll expect. Then when you call HAL_UARTEx_ReceiveToIdle_IT, you pass an argument twice the size of your array. That way you'll either get an idle interrupt or if you're expecting like 5 bytes from "hello", it'll do a half callback interrupt. 

 

 

#define RX_BUFFER_SIZE 10
typedef struct
{
	int dataRdy;
	uint32_t size;
}RX_Status;
uint8_t rxbuf[RX_BUFFER_SIZE];

RX_Status rx_status = {0};


int main(void)
{
  HAL_UARTEx_ReceiveToIdle_IT(&huart2, rxbuf, RX_BUFFER_SIZE * 2);
  while (1)
  {
	  if(rx_status.dataRdy)
	  {
		  rx_status.dataRdy = 0;
		  HAL_UART_Transmit_IT(&huart2, rxbuf, rx_status.size);
	  }
  }
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if(huart == &huart2)
	{
		rx_status.dataRdy = 1;
		rx_status.size = Size;
		HAL_UARTEx_ReceiveToIdle_IT(&huart2, rxbuf, RX_BUFFER_SIZE * 2);
	}
}

 

 

Make sure you enable UART interrupt

UART Enable Interrupt.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.

@KDJEM.1 i tried disabling the global interrupt but i am not getting output

 

HAL_UART_RxCpltCallback only works when interrupt is enabled. HAL_UART_Receive is only polling and will not trigger an interrupt callback. If you want polling then you have to rewrite your code to transmit what you received.

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.

#define ADC_BUF_SIZE 8

#define __VREFANALOG_VOLTAGE__ 3300

uint32_t ADC_buffer[ADC_BUF_SIZE];

uint8_t txbuf[30];

uint8_t adc_flag=0;

uint8_t led_flag=1;

uint8_t idx;

if(HAL_OK!=HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED))

{

Error_Handler();

}

if(HAL_OK!=HAL_ADC_Start_DMA(&hadc1,ADC_buffer, ADC_BUF_SIZE))

Error_Handler();

if(HAL_OK!=HAL_TIM_Base_Start(&htim2))

Error_Handler();

HAL_UART_Transmit(&huart2,txbuf,1,0);

while (1)

{

if(led_flag==1)

{

HAL_GPIO_TogglePin(led_green_GPIO_Port, led_green_Pin);

HAL_Delay(500);

}

if(1==adc_flag)

{

for(idx=0;idx<ADC_BUF_SIZE;idx++)

{

ADC_buffer[idx]=__LL_ADC_CALC_TEMPERATURE(__VREFANALOG_VOLTAGE__,ADC_buffer[idx],LL_ADC_RESOLUTION_12B);

txbuf[0]=ADC_buffer[idx];

}

adc_flag=0;

}

 

 

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)

{

adc_flag=1;

}

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

 

led_flag=!led_flag;

HAL_UART_Transmit(&huart2,txbuf,1, 0);

 

}

this is my code for blinking led for every one second on pressing user button led should stop bliking and also should send the ADC reading in the uart
but i cannot see the uart transmitted output on the window

Use code sample so we can see formatted code to make it easier to read

Code Snippet.jpg

 

int main(void)
{
	
#define ADC_BUF_SIZE 8
#define __VREFANALOG_VOLTAGE__ 3300
uint32_t ADC_buffer[ADC_BUF_SIZE];
uint8_t txbuf[30];
uint8_t adc_flag = 0;
uint8_t led_flag = 1;
uint8_t idx;

	if (HAL_OK != HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED))

	{

		Error_Handler();

	}

	if (HAL_OK != HAL_ADC_Start_DMA(&hadc1, ADC_buffer, ADC_BUF_SIZE))
		Error_Handler();

	if (HAL_OK != HAL_TIM_Base_Start(&htim2))
		Error_Handler();

	HAL_UART_Transmit(&huart2, txbuf, 1, 0);

	while (1)
	{
		if (led_flag == 1)
		{
			HAL_GPIO_TogglePin(led_green_GPIO_Port, led_green_Pin);
			HAL_Delay(500);
		}

		if (1 == adc_flag)
		{
			for (idx = 0; idx < ADC_BUF_SIZE; idx++)
			{
				ADC_buffer[idx] = __LL_ADC_CALC_TEMPERATURE(
						__VREFANALOG_VOLTAGE__, ADC_buffer[idx],
						LL_ADC_RESOLUTION_12B);
				txbuf[0] = ADC_buffer[idx];
			}
			adc_flag = 0;
		}
		/* USER CODE END WHILE */

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

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
	adc_flag = 1;
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	led_flag = !led_flag;
	HAL_UART_Transmit(&huart2, txbuf, 1, 0);
}

 

Did you enable NVIC interrupt for the GPIO?

 

 

 

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.
Piranha
Chief II

Those zero timeouts for the HAL_UART_Transmit() calls are especially cool...

Making my head spin, things that need to be global and volatile.

Cramming data into a single byte, that might go out on an EXTI

Pulling data from an active DMA buffer.

Lot of disconnected code, ideas and concepts here.

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

@Karl Yamashita 
yes i have enabled the NVIC interrupt for GPIO
also i am getting output for every other thing but not for uart