cancel
Showing results for 
Search instead for 
Did you mean: 

I want CAN commuication CAN1 to CAN2 in STM32F4 Serinse.

KimJeBu
Associate II

I want CAN commuication CAN1 to CAN2 in STM32F4 Serinse.

I wanted CAN1 to send a message to CAN2, and CAN2 to send the received message to CAN 1.

But I can't solve this problem.

Please check the main.c.


/* USER CODE BEGIN PV */
CAN_FilterTypeDef canFilter1;
CAN_FilterTypeDef canFilter2;
CAN_RxHeaderTypeDef canRxHeader;
CAN_TxHeaderTypeDef canTxHeader;
uint8_t can1Rx0Data[8];
//uint8_t can2Rx0Data[8];
uint32_t TxMailBox;
uint8_t can1Tx0Data[8];
//uint8_t can2Tx0Data[8];

volatile uint8_t can1_rx0_flag = 0;
volatile uint8_t can1_tx0_flag = 0;
volatile uint8_t can2_rx0_flag = 0;
volatile uint8_t can2_tx0_flag = 0;

/* USER CODE END PV */
 
/* USER CODE BEGIN 0 */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
  /* Get RX message */
  if (hcan->Instance == CAN1)
  {
    /* Get RX message */
    if (HAL_CAN_GetRxMessage(&hcan1, CAN_RX_FIFO0, &canRxHeader, can1Rx0Data) != HAL_OK)
    {
      /* Reception Error */
      Error_Handler();
    }
    can1_rx0_flag = 1;
  }
  if (hcan->Instance == CAN1)
   {
    if (HAL_CAN_AddTxMessage(&hcan1, &canTxHeader, can1Tx0Data, &TxMailBox) != HAL_OK)
    {
      Error_Handler();
    }
    can1_tx0_flag = 1;
   }
 
 if (hcan->Instance == CAN2)
 {
  if (HAL_CAN_GetRxMessage(&hcan2, CAN_RX_FIFO0, &canRxHeader, can1Rx0Data) != HAL_OK)
  {
    /* Reception Error */
    Error_Handler();
  }
  can2_rx0_flag = 1;
 }
 if (hcan->Instance == CAN2)
 {
  if (HAL_CAN_AddTxMessage(&hcan2, &canTxHeader, can1Tx0Data, &TxMailBox) != HAL_OK)
  {
    Error_Handler();
  }
  can2_tx0_flag = 1;
 }

}
/* USER CODE END 0 */
 
/* USER CODE BEGIN 2 */

  /* CAN Filter Setting */
  canFilter1.FilterBank = 0;
  canFilter1.FilterMode = CAN_FILTERMODE_IDMASK;
  canFilter1.FilterScale = CAN_FILTERSCALE_32BIT;
  canFilter1.FilterIdHigh = 0x0000;
  canFilter1.FilterIdLow = 0x0000;
  canFilter1.FilterMaskIdHigh = 0x0000;              // 0x00000000 = 모든 ID를 받아들이겠다
  canFilter1.FilterMaskIdLow = 0x0000;              
  canFilter1.FilterFIFOAssignment = CAN_RX_FIFO0;
  canFilter1.FilterActivation = ENABLE;
  canFilter1.SlaveStartFilterBank = 14;    

  if (HAL_CAN_ConfigFilter(&hcan1, &canFilter1) != HAL_OK)
   {
     /* Filter configuration Error */
     Error_Handler();
   }

  canFilter2.FilterBank = 0;
  canFilter2.FilterMode = CAN_FILTERMODE_IDMASK;
  canFilter2.FilterScale = CAN_FILTERSCALE_32BIT;
  canFilter2.FilterIdHigh = 0x0000;
  canFilter2.FilterIdLow = 0x0000;
  canFilter2.FilterMaskIdHigh = 0x0000;              // 0x00000000 = 모든 ID를 받아들이겠다
  canFilter2.FilterMaskIdLow = 0x0000;              
  canFilter2.FilterFIFOAssignment = CAN_RX_FIFO0;
  canFilter2.FilterActivation = ENABLE;
  canFilter2.SlaveStartFilterBank = 20;           // CAN2의 FilterBank시작 위치, CAN2를 사용한다면 FilterBank를 SlaveStartFilterBank보다 크게 설정해야 함.

  if (HAL_CAN_ConfigFilter(&hcan2, &canFilter2) != HAL_OK)
   {
     /* Filter configuration Error */
     Error_Handler();
   }

  /* Configure Transmission process */
  canTxHeader.StdId = 0x321;                 // Standard Identifier, 0 ~ 0x7FF
  canTxHeader.ExtId = 0x01;                  // Extended Identifier, 0 ~ 0x1FFFFFFF
  canTxHeader.RTR = CAN_RTR_DATA;            // 전송하는 메세지의 프레임 타입, DATA or REMOTE
  canTxHeader.IDE = CAN_ID_STD;              // 전송하는 메세지의 식별자 타입, STD or EXT
  canTxHeader.DLC = 8;                       // 송신 프레임 길이, 0 ~ 8 byte
  canTxHeader.TransmitGlobalTime = DISABLE;  // 프레임 전송 시작될 때 timestamp counter 값을 capture.

  /* Set the data to be transmitted */
  can1Tx0Data[0] = 1;
  can1Tx0Data[1] = 2;
  can1Tx0Data[2] = 3;
  can1Tx0Data[3] = 4;
  can1Tx0Data[4] = 5;
  can1Tx0Data[5] = 6;
  can1Tx0Data[6] = 7;
  can1Tx0Data[7] = 8;

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

/* Activate CAN RX notification */
  if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  {
    /* Notification Error */
    Error_Handler();
  }
  if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
  {
    /* Notification Error */
    Error_Handler();
  }

  /* Can Start */
  if (HAL_CAN_Start(&hcan1) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }
  if (HAL_CAN_Start(&hcan2) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }

  /* Start the Transmission process */
   if (HAL_CAN_AddTxMessage(&hcan1, &canTxHeader, can1Tx0Data, &TxMailBox) != HAL_OK)
   {
       Error_Handler();
   }

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
1 REPLY 1
Issamos
Lead II

Hello @KimJeBu 

Those postes are discussing similar issues you may find help for your case:

Best regards.

II