Skip to main content
QTLe
Visitor II
February 17, 2022
Solved

[STM32F103] Send CAN message in CAN receive callback interrupt

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!

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
February 17, 2022

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

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

3 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
February 17, 2022

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

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Associate
August 22, 2023

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
Principal
August 23, 2023

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

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source