cancel
Showing results for 
Search instead for 
Did you mean: 

How can I pass pointer info from hal_uart_receive_it into the callback function?

mikem1017
Associate II

I am trying to port over some code from PIC to STM32. In the pic code, the ISR took the pointer data from the received uart message (1 byte) and then sent that pointer data to a circular_buffer_put function (effectively putting it into the circular buffer).

The wall I'm running into is that the callback function is defined as only having one parameter - the huart handle - so I can't really do it the same way as before. All the examples I've seen online have the pointer data stored as a modular variable, which I'd really prefer not to do, and so the pointer data can be accessed in any function, including that of the callback.

Do any of you have a good idea on how to solve this problem?

Here's some example code of effectively what I'd like to do...

void HAL_UART_RxCpltCallback(uart_device_t *uart_struct, uint8_t *data)
{
    circular_buf_put(&RxBuffer, *data);
    HAL_UART_Receive_IT(uart_struct->huart, *data, 1);
}

14 REPLIES 14
S.Ma
Principal

True, for variable incoming message size, such as console emulation, UART HAL is less appealing than LL. Crude method with per byte interrupt is a simple start point. The other way is dma with cyclic buffer checked at regular time interval. Decide if reactivity and optimized RAM is higher priority to choose.

Thanks for taking the time to respond.

Cast it back to the super structure.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)

{

UART_SUPER_HandleTypeDef *ptr = (UART_SUPER_HandleTypeDef *)UartHandle;

...

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

0693W00000VOc5GQAT.pngOk, I've gotten this mostly working using the pointer inside of the uart handle struct. However, I'm noticing a slightly weird behaviour. When I echo back the received character, I'm getting an incremented letter. For example, if I send a, it echos back b.

Here's my init code:

	uart_struct->huart = huart;
 
	uint8_t *rxdata = (uint8_t*) (&uart_struct->huart->pRxBuffPtr);
 
	if (HAL_UART_Receive_IT(uart_struct->huart, rxdata, 1) != HAL_OK)
		return EID_INT_UART_RECEIVE_FAIL;
	else
		return EID_OKAY;

Here's my callback:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
	uint8_t *rxdata = (uint8_t*) (&huart->pRxBuffPtr);
 
	// MAIN UART (MAX22502EATC+)
	if (huart->Instance == USART3) {
		HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
		// Reinitialize HAL_UART_Receive_IT
		HAL_UART_Transmit(huart, rxdata, 1, HAL_MAX_DELAY);
		HAL_UART_Receive_IT(huart, rxdata, 1);
	}

Any ideas what might be causing the character to increment?

I've done some variable isolation and I can confirm now that the issue above is caused by using the pRxBuffPtr variable inside the struct. Is there a reason it's incrementing the data inside this ptr by 1?