Skip to main content
Visitor II
June 11, 2025
Solved

Performing an action after receiving a CAN message

  • June 11, 2025
  • 2 replies
  • 324 views

Question moved from this thread (a new question)

Thank you very much for the solution. However, I’m still unclear about the core point.

My main concern is that I need CAN files such that, when the code is flashed onto the STM32L4R9I-EVAL board, it should behave in the following way: upon receiving a CAN message, the board should either print something which is predefined or perform an action in response to that message.

Could you please help me with this


    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    I recommend to use the Rx interrupts.

    To print messages, you need to do it outside the interrupt handlers and the callback. You can for example set a flag in the interrupt handler and in the main if you check that flag was set, print the message you want.

    If for example you need to toggle a LED, you can do it inside the interrupt.

    You can inspire from this example: https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_Networking/Src/main.c

    Hope that helps.

    2 replies

    mƎALLEmAnswer
    Technical Moderator
    June 11, 2025

    Hello,

    I recommend to use the Rx interrupts.

    To print messages, you need to do it outside the interrupt handlers and the callback. You can for example set a flag in the interrupt handler and in the main if you check that flag was set, print the message you want.

    If for example you need to toggle a LED, you can do it inside the interrupt.

    You can inspire from this example: https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM324xG_EVAL/Examples/CAN/CAN_Networking/Src/main.c

    Hope that helps.

    Super User
    June 11, 2025

    @sonic_solutions wrote:

    should behave in the following way: upon receiving a CAN message, the board should either print something which is predefined or perform an action in response to that message.


    Well, that's pretty much the definition of any embedded system: it receives events from external interfaces, and then does something in response.

    Your job, as the embedded system developer, is to write the code to implement that something.

    As @mƎALLEm said, that might typically involve saving any data from the interrupt and/or setting a flag (or flags) to tell the rest of your code that data is available, and to do its work on it.