cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F103] Send CAN message in CAN receive callback interrupt

QTLe
Associate

Hello there,

I am working with CAN on STM32F103. I want my MCU to send a message right when it receives a message so I put HAL_CAN_AddTxMessage function in HAL_CAN_RxFifo0MsgPendingCallback. I tried it with STM32F4 like this and it worked fine.

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
	/* Do some other stuff here
        * ... 
        */
	HAL_CAN_AddTxMessage(&hcan1, &TxHeader0, CAN1Tx0, &TxMailbox[0]);
	HAL_CAN_AddTxMessage(&hcan1, &TxHeader1, CAN1Tx1, &TxMailbox[1]);
}

But I could not do so with STM32F1

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
        /* Do some stuff here
        * ...
        */
	HAL_CAN_AddTxMessage(&hcan, &TxHeader, CANTx, &TxMailbox);
}

the compiler gave me warning

passing argument 1 of 'HAL_CAN_AddTxMessage' from incompatible pointer type [-Wincompatible-pointer-types] main.c

expected 'CAN_HandleTypeDef *' {aka 'struct __CAN_HandleTypeDef *'} but argument is of type 'CAN_HandleTypeDef **' {aka 'struct __CAN_HandleTypeDef **'}

Upload the code to the MCU and it could not send the message.

I also tried to put HAL_CAN_AddTxMessage in HAL_CAN_IRQHandler or USB_LP_CAN1_RX0_IRQHandler but it gave the same result. Any way to solve this? Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

hcan enters as a pointer you don't need the &

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

View solution in original post

3 REPLIES 3

hcan enters as a pointer you don't need the &

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

Thanks so much, but in other project with NUCLEO-L432KC with the same code, I must be to write '&', ...why!?Thanks in advance for clarify this question !!

 
Karl Yamashita
Lead III

Because in the STM32F4 you're calling HAL_CAN_AddTxMessage and passing hcan1 which needs a pointer.

Then hcan in the STM32F1 interrupt is passed as a pointer so it doesn't need the '&' when you're calling HAL_CAN_AddTxMessage

If you changed hcan to &hcan1 then it'll work just like the STM32F4

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.