Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: can: stm32: support for dual CAN instances #46646

Merged
merged 4 commits into from
Jun 22, 2022

Conversation

martinjaeger
Copy link
Member

This PR implements support for the STM32 dual CAN, where CAN1 and CAN2 share the memory for the filter banks.

Compared to the previous proposal #40531, this implementation only supports an equal split of the available 28 filter banks, which leads to the same amount of 14 filter banks per instance independent of the number of instances available in the MCU. This simplifies the driver implementation significantly and should cover most use cases.

The implementation was tested on a Nucleo F446RE and passes all tests with the following command (the board definition was updated to use can2 instance instead of can1 for testing):

scripts/twister --device-testing --device-serial /dev/ttyACM0 -p nucleo_f446re -T tests/drivers/can

In theory, also CAN3 available on some STM32F7 series MCUs should work if the missing devicetree nodes are added, but I don't have any hardware to test.

Copy link
Collaborator

@str4t0m str4t0m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this enhancement! I have not tested it on hardware, but I'd like to highlight two points.

  • one-shot mode:
    The config initialization got lost during the multi instance refactoring.
  • locking of filter registers:
    Locking is done per instance, but both instances access the filter registers of CAN1 (master_can).
    I believe we should make sure that the filter configuration happens mutually exclusive.

drivers/can/can_stm32.c Show resolved Hide resolved
@towen
Copy link

towen commented Jun 17, 2022

I have tested this on hardware, and can confirm it works :)

Caveats: I am not using one-shot mode or any special filter config, but I am using heavy traffic (ISO-TP bootloader on CAN2) while simultaneously sending messages from CAN1.

This commit unifies the driver initialization for can1 and can2
instances so that a single macro can be used.

Enabling the master clock for can2 as introduced in 8ab81b0 had to
be moved to devicetree in order to have the same CAN_STM32_CONFIG_INST
macro for all instances.

Signed-off-by: Martin Jäger <[email protected]>
Remove two functions which were only called from one place and did
not improve readibility of the code.

Signed-off-by: Martin Jäger <[email protected]>
CAN1 and CAN2 share the memory for the filter banks.

This commit adds an offset for filters installed for CAN2, allowing to
use both CAN instances simultaneously.

The hardware allows to freely split the avalable 28 filters between
CAN1 and CAN2. In order to simplify the driver implementation, it only
supports an equal split of 14 filters per instance. This is the same
amount of filters as available in MCUs with only a single CAN.

Signed-off-by: Martin Jäger <[email protected]>
Choose can2 as the zephyr,canbus instance and add necessary settings
for testing of the STM32 dual CAN driver implementation.

Signed-off-by: Martin Jäger <[email protected]>
@martinjaeger
Copy link
Member Author

  • one-shot mode:
    The config initialization got lost during the multi instance refactoring.

Good spot! I must have accidentally dropped that line. It's now added back.

  • locking of filter registers:
    Locking is done per instance, but both instances access the filter registers of CAN1 (master_can).
    I believe we should make sure that the filter configuration happens mutually exclusive.

That's also a good point. Added a static filter_mutex now. This is not the most efficient way if there are 3 CAN instances (as the third one is independent), but I think that's acceptable as it's only affecting the configuration of the filters, which is usually not done very often.

@martinjaeger martinjaeger requested a review from str4t0m June 18, 2022 06:21
Copy link
Member

@henrikbrixandersen henrikbrixandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, one suggestion:

drivers/can/can_stm32.c Show resolved Hide resolved
Copy link
Member

@erwango erwango left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good improvement, thanks!

@carlescufi carlescufi merged commit ac47200 into zephyrproject-rtos:main Jun 22, 2022
@martinjaeger martinjaeger deleted the stm32-dual-can branch July 14, 2022 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants