cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F413 CAN BUS Problem

RBall.1
Associate

Hi, guys!

I'm using a board developed in my company that uses a STM32F413VGT3. It has a MCP2551 and I have to use CAN BUS to comunicate.

The problem is that we're using PD0 and PD1 as CAN1_RX and CAN1_TX, because PA12 is used to USART1_RTS. I'm able to receive data from CAN but I can't send anything. I've used the same code in another board with STM32F103 without a problem (using the standard pin map), so I think that the problem is at alternate function of PD0 and PD1. I used STM32CubeIDE to do the remapping and it seems ok to me in function HAL_CAN_MspInit.

Is there any knowed problem with alternate function of can pins? Any ideas of what should I search for?

Code:

static void MX_CAN1_Init(void)

{

 /* USER CODE BEGIN CAN1_Init 0 */

 /* USER CODE END CAN1_Init 0 */

 /* USER CODE BEGIN CAN1_Init 1 */

 /* USER CODE END CAN1_Init 1 */

 hcan1.Instance = CAN1;

 hcan1.Init.Prescaler = 25;

 hcan1.Init.Mode = CAN_MODE_NORMAL;

 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan1.Init.TimeSeg1 = CAN_BS1_3TQ;

 hcan1.Init.TimeSeg2 = CAN_BS2_4TQ;

 hcan1.Init.TimeTriggeredMode = DISABLE;

 hcan1.Init.AutoBusOff = DISABLE;

 hcan1.Init.AutoWakeUp = DISABLE;

 hcan1.Init.AutoRetransmission = DISABLE;

 hcan1.Init.ReceiveFifoLocked = DISABLE;

 hcan1.Init.TransmitFifoPriority = DISABLE;

 if (HAL_CAN_Init(&hcan1) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN CAN1_Init 2 */

 /* USER CODE END CAN1_Init 2 */

}

/**

* @brief CAN MSP Initialization

* This function configures the hardware resources used in this example

* @param hcan: CAN handle pointer

* @retval None

*/

void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(hcan->Instance==CAN1)

 {

 /* USER CODE BEGIN CAN1_MspInit 0 */

 /* USER CODE END CAN1_MspInit 0 */

  /* Peripheral clock enable */

  __HAL_RCC_CAN1_CLK_ENABLE();

  __HAL_RCC_GPIOD_CLK_ENABLE();

  /**CAN1 GPIO Configuration

  PD0   ------> CAN1_RX

  PD1   ------> CAN1_TX

  */

  GPIO_InitStruct.Pin = GPIO_PIN_0;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;

  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_1;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_PULLUP;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF9_CAN1;

  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /* CAN1 interrupt Init */

  HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);

 /* USER CODE BEGIN CAN1_MspInit 1 */

 /* USER CODE END CAN1_MspInit 1 */

 }

}

1 REPLY 1
RBall.1
Associate

Ive just found the problem. It was a problem with mcp2551.