AnsweredAssumed Answered

STm32CubeMX bug : CAN1 wrong configuration

Question asked by cattin.bernard on Mar 2, 2016
Latest reply on May 13, 2016 by STM32Cube-T
Hi,

I tried to configure my STm32F107 using the "Cube" to use the CAN1 and user leds and I found a bug.

This is the initialization code generated by the "Cube":
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  if(hcan->Instance==CAN1)
  {
  /* USER CODE BEGIN CAN1_MspInit 0 */
  
  /* USER CODE END CAN1_MspInit 0 */
    /* Peripheral clock enable */
    __CAN1_CLK_ENABLE();
    
    /**CAN1 GPIO Configuration    
    PD0     ------> CAN1_RX
    PD1     ------> CAN1_TX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  
    __HAL_AFIO_REMAP_CAN1_3();
  
  /* USER CODE BEGIN CAN1_MspInit 1 */
  
  /* USER CODE END CAN1_MspInit 1 */
  }
}

The structure definition of GPIO_InitStruct is :
typedef struct
{
  uint32_t Pin;       /*!< Specifies the GPIO pins to be configured.
                           This parameter can be any value of @ref GPIO_pins_define */
  
  uint32_t Mode;      /*!< Specifies the operating mode for the selected pins.
                           This parameter can be a value of @ref GPIO_mode_define */
                             
  uint32_t Pull;      /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.
                           This parameter can be a value of @ref GPIO_pull_define */
                             
  uint32_t Speed;     /*!< Specifies the speed for the selected pins.
                           This parameter can be a value of @ref GPIO_speed_define */
}GPIO_InitTypeDef;

You can see that the field Speed is not initiated.
This field is then used in the routine HAL_GPIO_Init :
...
            config = GPIO_CR_MODE_INPUT + GPIO_CR_CNF_INPUT_PU_PD;
...
     MODIFY_REG((*configregister), ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << registeroffset ), (config << registeroffset));
...

The consequence is that the other bits of the register are affected and the led which is on PD3 doesn't work !

The solution is to initiated the field Speed in the HAL_CAN_MspInit routine :
...
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
...

After that, all is OK !

Bernard.

Outcomes