Skip to main content
Iviti.1
Associate II
September 8, 2021
Solved

STM32F0 HAL_CAN_ActivateNotification guide

I am trying to set up a hal can receive interrupt. After thorough googling, I found that HAL_CAN_Receive_IT is deprecated and that I should now be using HAL_CAN_ActivateNotification with a callback function. Unfortunately no one describes how to do this. In fact, if I search the HAL manual (um1785) for "HAL_CAN_ActivateNotification" I get 0 results. It looks like the STM32F0 line just doesnt have CAN interrupt capabilities.

This topic has been closed for replies.
mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
September 9, 2021

Dear @Iviti.1​ 

You can refer to the example provided under STM32CubeF0 downloadable from this link.

So unzip the file and go under either:

Projects\STM32072B_EVAL\Examples\CAN\CAN_Networking

or

Projects\STM32091C_EVAL\Examples\CAN\CAN_Networking

The example calls HAL_CAN_ActivateNotification() API and uses CAN Rx in interrupt context.

If you find this answers your question, please mark this topic as answered by selecting Select as best. This will help other users find that answer faster 

SofLit

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.

10 replies

mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
September 9, 2021

Dear @Iviti.1​ 

You can refer to the example provided under STM32CubeF0 downloadable from this link.

So unzip the file and go under either:

Projects\STM32072B_EVAL\Examples\CAN\CAN_Networking

or

Projects\STM32091C_EVAL\Examples\CAN\CAN_Networking

The example calls HAL_CAN_ActivateNotification() API and uses CAN Rx in interrupt context.

If you find this answers your question, please mark this topic as answered by selecting Select as best. This will help other users find that answer faster 

SofLit

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Iviti.1
Iviti.1Author
Associate II
September 9, 2021

I am not entirely sure how to run that example without an .ioc code. Inspecting it visually, I see that it sends out information using the

HAL_CAN_AddTxMessage(&CanHandle, &TxHeader, TxData, &TxMailbox)

command.

In my code, my system goes into there a few times then error's out. Nothing is being sent out. Almost like the message gets put into the transmit mailboxes but then nothing gets sent out.

mƎALLEm
ST Technical Moderator
September 10, 2021

Hello,

CubeMx is used only for HW configuration not to built an application. So HAL_CAN_AddTxMessage() is not part of CubeMx code generation but it's up to the user to add his own code related to his specific application and this should be between:

/* USER CODE BEGIN <XX> */

/* USER CODE END <XX> */

The example that I've pointed to, contains the sequence of code to use CAN in Rx in interrupt context and you can inspire from that example to adapt it to your usage.

Please refer to readme file located in example folder in order to have an idea how to use the example and on which HW condition this should be used then you can have idea how to adapt it to your needs.. 

SofLit

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Iviti.1
Iviti.1Author
Associate II
September 10, 2021

I understand that in order to do can rx, first you need to tell it that you will be expecting a message, then you need a callback to the can interrupt. What I was saying with the last message is that can Tx is not even working for me. Perhaps I should make that a new post.

Iviti.1
Iviti.1Author
Associate II
September 20, 2021

turned out the stby pin on the can transceiver needed to be explicitly grounded. Now I am able to Tx on the stm32f0. Unfortunately Rx does not work. There is no option to turn on rx interrupt handler in nvic settings

mƎALLEm
ST Technical Moderator
September 21, 2021

This is the option to enable CAN interrupt in CubeMx for F0:0693W00000Dnv16QAB.png

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Iviti.1
Iviti.1Author
Associate II
September 21, 2021

Running through the code in debug mode, I find that instead of interrupting to "HAL_CAN_RxFifo0MsPendingCallback" it seems to be interrupting to "CEC_CAN_IRQHandler". In fact, if I do "HAL_CAN_GetRxMessage" there, I can read the message.

Iviti.1
Iviti.1Author
Associate II
September 21, 2021

unfortunately I cant get it to naturally reach the callback because of the line :

 if ((interrupts & CAN_IT_RX_FIFO0_FULL) != 0U)

where it compares the fifo 0 full interrupt flag with the interrupt enable register. interrupts = 2, CAN_IT_RX_FIFO0_FULL = 4

Iviti.1
Iviti.1Author
Associate II
September 21, 2021

I can set

		hcan.Instance->IER = 4;

to get past that if statement, but then I am stuck at

 if ((rf0rflags & CAN_RF0R_FULL0) != 0U)

where rf0rflags = 2 and can_rf0r_full0 = 8

Iviti.1
Iviti.1Author
Associate II
September 21, 2021

Although interrupts is now equal to 6, so I am back to

 if ((interrupts & CAN_IT_RX_FIFO0_FULL) != 0U)