cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 CANbus is not sending/receiving message

JThom.15
Associate III

I am trying to send messages from Can1 to Can2 in an stm32f205vet6. HAL_CAN_AddTxMessage is returning HAL_OK but I am not able to receive any messages in CAN2. And I cant see any messages transmitted in a scope. 

clock speed = 120Mhz

Can speed = 500 kbps

My code below

CAN1_init

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 = 15;

 hcan1.Init.Mode = CAN_MODE_NORMAL;

 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;

 hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;

 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 */

 /* USER CODE BEGIN CAN_Init 2 */

 CAN_FilterTypeDef canfilterconfig;

 canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;

 canfilterconfig.FilterBank = 0; // which filter bank to use from the assigned ones

 canfilterconfig.FilterFIFOAssignment = CAN_FILTER_FIFO1;

 canfilterconfig.FilterIdHigh = 0;

 canfilterconfig.FilterIdLow = 0;

 canfilterconfig.FilterMaskIdHigh = 0;

 canfilterconfig.FilterMaskIdLow = 0;

 canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;

 canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;

 canfilterconfig.SlaveStartFilterBank = 0; // doesn't matter in single can controllers

 HAL_CAN_ConfigFilter(&hcan1, &canfilterconfig);

 /* USER CODE END CAN_Init 2 */

}

CAN2_INIt

void MX_CAN2_Init(void)

{

 /* USER CODE BEGIN CAN2_Init 0 */

 /* USER CODE END CAN2_Init 0 */

 /* USER CODE BEGIN CAN2_Init 1 */

 /* USER CODE END CAN2_Init 1 */

 hcan2.Instance = CAN2;

 hcan2.Init.Prescaler = 15;

 hcan2.Init.Mode = CAN_MODE_NORMAL;

 hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan2.Init.TimeSeg1 = CAN_BS1_1TQ;

 hcan2.Init.TimeSeg2 = CAN_BS2_2TQ;

 hcan2.Init.TimeTriggeredMode = DISABLE;

 hcan2.Init.AutoBusOff = DISABLE;

 hcan2.Init.AutoWakeUp = DISABLE;

 hcan2.Init.AutoRetransmission = DISABLE;

 hcan2.Init.ReceiveFifoLocked = DISABLE;

 hcan2.Init.TransmitFifoPriority = DISABLE;

 if (HAL_CAN_Init(&hcan2) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN CAN2_Init 2 */

 /* USER CODE END CAN2_Init 2 */

 /* USER CODE BEGIN CAN1_Init 2 */

 /* USER CODE END CAN1_Init 2 */

 /* USER CODE BEGIN CAN_Init 2 */

 CAN_FilterTypeDef canfilterconfig;

 canfilterconfig.FilterActivation = CAN_FILTER_ENABLE;

 canfilterconfig.FilterBank = 0; // which filter bank to use from the assigned ones

 canfilterconfig.FilterFIFOAssignment = CAN_FILTER_FIFO1;

 canfilterconfig.FilterIdHigh = 0;

 canfilterconfig.FilterIdLow = 0;

 canfilterconfig.FilterMaskIdHigh = 0;

 canfilterconfig.FilterMaskIdLow = 0;

 canfilterconfig.FilterMode = CAN_FILTERMODE_IDMASK;

 canfilterconfig.FilterScale = CAN_FILTERSCALE_32BIT;

 canfilterconfig.SlaveStartFilterBank = 0; // doesn't matter in single can controllers

 HAL_CAN_ConfigFilter(&hcan2, &canfilterconfig);

 /* USER CODE END CAN_Init 2 */

}

CAN_MSPInit

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_ENABLED++;

  if(HAL_RCC_CAN1_CLK_ENABLED==1){

   __HAL_RCC_CAN1_CLK_ENABLE();

  }

  __HAL_RCC_GPIOB_CLK_ENABLE();

  /**CAN1 GPIO Configuration

  PB8  ------> CAN1_RX

  PB9  ------> CAN1_TX

  */

  GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;

  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(GPIOB, &GPIO_InitStruct);

  

 /* USER CODE BEGIN CAN1_MspInit 1 */

  HAL_NVIC_SetPriority(CAN1_RX1_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn);

 /* USER CODE END CAN1_MspInit 1 */

 }

 else if(hcan->Instance==CAN2)

 {

 /* USER CODE BEGIN CAN2_MspInit 0 */

 /* USER CODE END CAN2_MspInit 0 */

  /* Peripheral clock enable */

  __HAL_RCC_CAN2_CLK_ENABLE();

  HAL_RCC_CAN1_CLK_ENABLED++;

  if(HAL_RCC_CAN1_CLK_ENABLED==1){

   __HAL_RCC_CAN1_CLK_ENABLE();

  }  

  __HAL_RCC_GPIOB_CLK_ENABLE();

  /**CAN2 GPIO Configuration

  PB12  ------> CAN2_RX

  PB13  ------> CAN2_TX

  */

  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;

  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_CAN2;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);  

  

 /* USER CODE BEGIN CAN2_MspInit 1 */

  HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);

 /* USER CODE END CAN2_MspInit 1 */

 }

}

Can transmit and Recieve

CAN_TxHeaderTypeDef tx_header;

CAN_RxHeaderTypeDef rx_header;

tx_header.DLC = 2; // data length

  tx_header.IDE = CAN_ID_STD;

  tx_header.RTR = CAN_RTR_DATA;

  tx_header.StdId = 0x103; // ID

  can_tx_Data[0] = 2; 

  can_tx_Data[1] = 8;

// Activate the notification

 HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING);

 // Start CAN

 if(HAL_CAN_Start(&hcan2)!= HAL_OK)

 {  

  Test[] = "failed to start CAN2"; //Data to send

 HAL_UART_Transmit(&test_uart,(uint8_t *)Test,sizeof(Test),10);  

 }

if(HAL_CAN_Start(&hcan1)!= HAL_OK)

 {  

  Test[] = "failed to start CAN1"; //Data to send

 HAL_UART_Transmit(&test_uart,(uint8_t *)Test,sizeof(Test),10);  

 }

if (HAL_CAN_AddTxMessage(&hcan2, &tx_header, can_tx_Data, &tx_mailbox) == HAL_OK)

    {

     uint8_t Test[] = "CAN message added\r\n"; //Data to send

     HAL_UART_Transmit(&test_uart,Test,sizeof(Test),10);

    }

    while (HAL_CAN_IsTxMessagePending(&hcan2,tx_mailbox))

    {

     Test[] = "CAN message pending\r\n"; //Data to send

     HAL_UART_Transmit(&test_uart,Test,sizeof(Test),10);

     HAL_Delay(1000);

    }

    Test[] = "CAN message sent successfully"; //Data to send

    HAL_UART_Transmit(&test_uart,(uint8_t *)Test,sizeof(Test),10);

extern "C"

void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)

{

 HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &rx_header, can_rx_Data);

 Test[] = "received can message"; //Data to send

 HAL_UART_Transmit(&test_uart,(uint8_t *)Test,sizeof(Test),10); 

}

10 REPLIES 10
Parvez Akhtar
Associate II

How come there is same answer everywhere and it is still not working. Did any try to use only CAN2. Tried all these filters settings it still doesn't work. I didn't try configuring both CANs at the same time.

Please come up with some working and good answers.