cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407VE and CAN2 is impossible to initialize

Antos.Jiri
Associate II
Posted on June 02, 2016 at 14:52

Hello. I have initialized CAN1 and it works properly. I need initialize after a while CAN2 and it is finishing on timeout because INAK is stil TRUE. Where can be problem?

Thanks, Jiri

#can2-stm32f407-inak
8 REPLIES 8
Posted on June 02, 2016 at 15:07

I've used both CAN buses on the F407, so its not the chip.

Perhaps we should look more critically at exactly what you are doing on the software/hardware side.

CAN2 depends on CAN1 for resources and clocks

CAN2 uses different filter levels.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Antos.Jiri
Associate II
Posted on June 02, 2016 at 15:19

Hi clive1.

I know it. I am using filter 0 for CAN1 and filter 14 for CAN2.

I successfully use CAN1. These initialization of CAN2 fails:

  /* Enable GPIO clock */

  //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    __GPIOB_CLK_ENABLE();

  /* Enable CAN clock */

    __CAN2_CLK_ENABLE();

  /* CAN1 TX GPIO pin configuration */

  GPIO_InitStruct.Pin = GPIO_PIN_5;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Pull = GPIO_PULLUP;

  GPIO_InitStruct.Alternate =  GPIO_AF9_CAN2;

 

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

  /* CAN1 RX GPIO pin configuration */

  GPIO_InitStruct.Pin = GPIO_PIN_6;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

  /* CAN configuration ********************************************************/  

  CanHandle2.Instance = CAN2;

  /* CAN register init */

  //HAL_CAN_DeInit(&CanHandle2);

  /* CAN cell init */

  CanHandle2.pTxMsg = &TxMessage2;

  CanHandle2.pRxMsg = &RxMessage2;

    

  CanHandle2.Init.TTCM = DISABLE;

  CanHandle2.Init.ABOM = DISABLE;

  CanHandle2.Init.AWUM = DISABLE;

  CanHandle2.Init.NART = ENABLE; //DISABLE;

  CanHandle2.Init.RFLM = DISABLE;

  CanHandle2.Init.TXFP = DISABLE;

  CanHandle2.Init.Mode = CAN_MODE_NORMAL;

  CanHandle2.Init.SJW = CAN_SJW_1TQ;

  CanHandle2.Init.BS1 = CAN_BS1_3TQ; //6TQ;

  CanHandle2.Init.BS2 = CAN_BS2_2TQ;    //8TQ;

  CanHandle2.Init.Prescaler = 7 << Pre; // 0 ... 1 MBaud, 1 .. 500k, 2 .. 250k, 3    .. 125k        

    if(HAL_CAN_Init(&CanHandle2) != HAL_OK)

  {

    /* Initialization Error */

    for(;;);    // ToDo remove

    / / ********** It is ending here with timeout error and CAN2->MSR is 9

  }

Walid FTITI_O
Senior II
Posted on June 02, 2016 at 16:54

Hi antos.jiri,

You have to enable also CAN1 clock.

As mentionned in reference manual RM0090, “Dual CAN� part ( page 1069) :

''- CAN1: Master bxCAN for managing the communication between a Slave

bxCAN and the 512-byte SRAM memory.

-CAN2: Slave bxCAN, with no direct access to the SRAM memory.''

Hence when using CAN2 it's obligatory to enable the Clock of CAN1

accordingly.

-Hannibal-

Hi clive1.

I know it. I am using filter 0 for CAN1 and filter 14 for CAN2.

I successfully use CAN1. These initialization of CAN2 fails:

  /* Enable GPIO clock */

  //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    __GPIOB_CLK_ENABLE();

  /* Enable CAN clock */

    __CAN2_CLK_ENABLE();

  /* CAN1 TX GPIO pin configuration */

  GPIO_InitStruct.Pin = GPIO_PIN_5;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Pull = GPIO_PULLUP;

  GPIO_InitStruct.Alternate =  GPIO_AF9_CAN2;

 

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

  /* CAN1 RX GPIO pin configuration */

  GPIO_InitStruct.Pin = GPIO_PIN_6;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

  /* CAN configuration ********************************************************/  

  CanHandle2.Instance = CAN2;

  /* CAN register init */

  //HAL_CAN_DeInit(&CanHandle2);

  /* CAN cell init */

  CanHandle2.pTxMsg = &TxMessage2;

  CanHandle2.pRxMsg = &RxMessage2;

    

  CanHandle2.Init.TTCM = DISABLE;

  CanHandle2.Init.ABOM = DISABLE;

  CanHandle2.Init.AWUM = DISABLE;

  CanHandle2.Init.NART = ENABLE; //DISABLE;

  CanHandle2.Init.RFLM = DISABLE;

  CanHandle2.Init.TXFP = DISABLE;

  CanHandle2.Init.Mode = CAN_MODE_NORMAL;

  CanHandle2.Init.SJW = CAN_SJW_1TQ;

  CanHandle2.Init.BS1 = CAN_BS1_3TQ; //6TQ;

  CanHandle2.Init.BS2 = CAN_BS2_2TQ;    //8TQ;

  CanHandle2.Init.Prescaler = 7 << Pre; // 0 ... 1 MBaud, 1 .. 500k, 2 .. 250k, 3    .. 125k        

    if(HAL_CAN_Init(&CanHandle2) != HAL_OK)

  {

    /* Initialization Error */

    for(;;);    // ToDo remove

    / / ********** It is ending here with timeout error and CAN2->MSR is 9

  }

Antos.Jiri
Associate II
Posted on June 03, 2016 at 07:28

Hi Hannibal,

I have enabled CAN1 clock. My SW working by this way:

1. I receive command by CAN1 and set some flag

2. main loop, when this flag is true, starts initialization of CAN2

I have an idea, that there is something wrong in HW, but driver is same as for CAN1 and connection is too simple - 2 wires between micro and driver.

Jiri

Antos.Jiri
Associate II
Posted on June 03, 2016 at 09:28

Hi everybody,

I want to use GPIOB.5 and .6 to connect CAN2 driver.

Wires GPIOB.12 and .13 I have free, therefore I change CAN2 pins to this.

CAN2 is starting now.

I am testing crazy configuration - I have enabled both pins for CAN2TX (B.5 and B.12) and CAN2RX (B.6 and B.13).

Initalisation is finished OK. Will it work for communication?

Jiri

Antos.Jiri
Associate II
Posted on June 03, 2016 at 13:48

Hi everybody.

STM32F407 is OK. I have something wrong on secondary side of CAN driver.

Jiri

Antos.Jiri
Associate II
Posted on June 03, 2016 at 14:35

I have pull-up resistor on CAN-H line and pull-down resistor in CAN-L line.

When I removed them, everythink is OK.

Jiri

Walid FTITI_O
Senior II
Posted on June 03, 2016 at 16:43

Hi antos.jiri,

It is nice to hear that. Keep the good work.

-Hannibal-