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

Modify Stm32Can to support multiple CAN interfaces on ST Micro MCUs that have multiple CAN IFs #816

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

RobertPHeller
Copy link
Contributor

This is a modification to the Stm32Can that allows for multiple CAN IFs running on those ST Micro MCUs that have multiple CAN IFs. When the class is instanciated, the trailing digit in the path name determines which CAN IF this instance is for. If the path ends in '0', CAN1 (or the only) interface is used, if it ends in '1', CAN2 is used (if it exists), and if the path ends in '2', CAN3 is used.

Typical usage:

In HwInit.cxx:

/** CAN 0 CAN driver instance */
static Stm32Can can0("/dev/can0");
/** CAN 1 CAN driver instance */
static Stm32Can can1("/dev/can1");

Later:

   __HAL_RCC_CAN1_CLK_ENABLE();
   __HAL_RCC_CAN2_CLK_ENABLE();

and:

    /* CAN1 pinmux on PB8 and PB9 */
    gpio_init.Mode = GPIO_MODE_AF_PP;
    gpio_init.Pull = GPIO_PULLUP;
    gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
    gpio_init.Alternate = GPIO_AF9_CAN1;
    gpio_init.Pin = GPIO_PIN_8;
    HAL_GPIO_Init(GPIOB, &gpio_init);
    gpio_init.Pin = GPIO_PIN_9;
    HAL_GPIO_Init(GPIOB, &gpio_init);

    /* CAN2 pinmux on PB12 and PB13 */
    gpio_init.Mode = GPIO_MODE_AF_PP;
    gpio_init.Pull = GPIO_PULLUP;
    gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
    gpio_init.Alternate = GPIO_AF9_CAN2;
    gpio_init.Pin = GPIO_PIN_12;
    HAL_GPIO_Init(GPIOB, &gpio_init);
    gpio_init.Pin = GPIO_PIN_13;
    HAL_GPIO_Init(GPIOB, &gpio_init);

Then in main.cxx:

    HubDeviceSelect<CanHubFlow> seg1(&can_hub0,"/dev/can0");
    HubDeviceSelect<CanHubFlow> seg2(&can_hub0,"/dev/can1");

I have tested this:

https://github.com/RobertPHeller/RPi-RRCircuits/tree/master/STM32F767ZI_LCC_PNET_Router

@RobertPHeller
Copy link
Contributor Author

RobertPHeller commented Jan 31, 2025 via email

instances[index] = this;
switch (index)
{
case 0: /* CAN1... */
Copy link
Owner

Choose a reason for hiding this comment

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

The style guidelines require case labels to be indented four spaces.

/** Default constructor.
*/
Stm32Can();


Copy link
Owner

Choose a reason for hiding this comment

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

Remove the extra empty line. Only one empty line required for spacing.

@@ -103,13 +132,58 @@ Stm32Can::Stm32Can(const char *name)
: Can(name)
, state_(CAN_STATE_STOPPED)
{
/* only one instance allowed */
HASSERT(instances[0] == NULL);
/* Get dev num (digit at the end of the devname). */
Copy link
Owner

Choose a reason for hiding this comment

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

I don't like that we have programmatically tied the device "name" to the hardware instance. As far as I recall, we don't have any such tie anywhere else in the device file system. I think it makes since to instead pass the hardware references in (base address and interrupt number), similar to what we do in the Tiva driver:

TivaCan::TivaCan(const char *name, unsigned long base, uint32_t interrupt)

I strongly encourage that with the addition of these parameters, either we have an overloaded constructor that is backwards compatible fixed to the first CAN instance, or we use "default" values for the parameters to point to the first CAN instance. That way we don't break other existing uses.

@@ -45,6 +45,12 @@

#include "stm32f_hal_conf.hxx"

// Max possible number of CAN ifs across the whole STM32 -- the constructor
Copy link
Owner

Choose a reason for hiding this comment

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

Please turn this into a Doxygen style comment with three slashes.

@@ -91,11 +97,17 @@ private:
static unsigned int intCount;

uint8_t state_; ///< present bus state


CAN_TypeDef *can_;
Copy link
Owner

Choose a reason for hiding this comment

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

Please add Doxygen comments for these members.

// will calculate the actual max for the specific chip compiling for
#define MAXCANIFS 3

//struct CAN_TypeDef;
Copy link
Owner

Choose a reason for hiding this comment

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

Is this dead code?

@@ -36,6 +36,7 @@
#include "Stm32Can.hxx"

#include <stdint.h>
#include <string.h>
Copy link
Owner

Choose a reason for hiding this comment

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

This include might be able to be removed if the programatic select of device instance by name is also removed.

@RobertPHeller
Copy link
Contributor Author

RobertPHeller commented Feb 3, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants