cancel
Showing results for 
Search instead for 
Did you mean: 

STm32CubeMX bug : CAN1 wrong configuration

b2399
Associate II
Posted on March 02, 2016 at 11:57

Hi,

I tried to configure my STm32F107 using the ''Cube'' to use the CAN1 and user ledsand 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. #stm32
4 REPLIES 4
Amel NASRI
ST Employee
Posted on March 02, 2016 at 18:01

Hicattin.bernard,

Could you please precise the CubeMX version you are using and share the .ioc file of your configuration? With CubeMX 4.13, I don't have the same generated code:

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_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
__HAL_AFIO_REMAP_CAN1_3();
/* USER CODE BEGIN CAN1_MspInit 1 */
/* USER CODE END CAN1_MspInit 1 */
}
}

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

b2399
Associate II
Posted on March 08, 2016 at 10:57

Hi Mayla,

thanks for your response.

I think I'm using the 4.13 version of CubeMX :

STM32CubeMX4.0.201602081222com.st.microxplorer.feature.groupSTMicroelectronics

The .ioc file is attached.

Bernard.

________________

Attachments :

STM32Eval_demo.ioc : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HtpZ&d=%2Fa%2F0X0000000aXR%2FDBjqrCJNMbZq8SrDWdb.Gs3yNODJYFPTBNY.tuU1npE&asPdf=false
stm32cube-t
Senior III
Posted on April 15, 2016 at 11:52

Dear user,

Thank you for reporting this issue.

It shall be fixed in 4.15.

As a workaround you will have to go via the MCU selector tab rather than the Board tab to start your project, and manually change CAN GPIO configuration for alternate functions.

Best regards
stm32cube-t
Senior III
Posted on May 13, 2016 at 13:02

Dear user,

Coming back to you on CAN1_Rx configuration.

As stated in the reference manual and as provided with STM32CubeMX tool, the CAN1_Rx should be configured as follows:

0690X00000604ubQAA.png