cancel
Showing results for 
Search instead for 
Did you mean: 

hi i am using STM32f746zg microcontroller with CANBUS i want to establish communication between CAN1 and CAN2 where CAN1(master) will send and CAN2(slave) should receive. CAN1 is transmitting perfectly but it is not receiving at CAN2 ..

B Vinod
Associate II

can anyone help me with the code ....thanks in advance

here is my 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 = 30;

 hcan1.Init.Mode = CAN_MODE_NORMAL;

 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan1.Init.TimeSeg1 = CAN_BS1_15TQ;

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

}

/**

 * @brief CAN2 Initialization Function

 * @param None

 * @retval None

 */

static 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 = 30;

 hcan2.Init.Mode = CAN_MODE_NORMAL;

 hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan2.Init.TimeSeg1 = CAN_BS1_15TQ;

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

}

void can1_filter(void)

{

   CAN_FilterTypeDef filter1;

   filter1.FilterActivation=CAN_FILTER_ENABLE;

   filter1.FilterBank=0;

   filter1.FilterFIFOAssignment=CAN_RX_FIFO0;

   filter1.FilterIdHigh=0x0000;

   filter1.FilterIdLow=0x0000;

   filter1.FilterMaskIdHigh=0x0000;

   filter1.FilterMaskIdLow=0x0000;

   filter1.FilterMode= CAN_FILTERMODE_IDMASK;

   filter1.FilterScale=CAN_FILTERSCALE_32BIT;

   filter1.SlaveStartFilterBank=13;

   if(HAL_CAN_ConfigFilter(&hcan1,&filter1)!=HAL_OK)

   {

      Error_Handler();

   }

}

void can2_filter(void)

{

   CAN_FilterTypeDef filter2;

   filter2.FilterActivation=CAN_FILTER_ENABLE;

   filter2.FilterBank=0;

   filter2.FilterFIFOAssignment= CAN_RX_FIFO0;

   filter2.FilterIdHigh=0x0000;

   filter2.FilterIdLow=0x0000;

   filter2.FilterMaskIdHigh=0x0000;

   filter2.FilterMaskIdLow=0x0000;

   filter2.FilterMode= CAN_FILTERMODE_IDMASK;

   filter2.FilterScale=CAN_FILTERSCALE_32BIT;

   filter2.SlaveStartFilterBank=14;

   if(HAL_CAN_ConfigFilter(&hcan2,&filter2)!=HAL_OK)

   {

      Error_Handler();

   }

}

void can(void)

{

   CAN_TxHeaderTypeDef TxHeader;

   uint32_t TxMailbox;

   uint8_t usr_msg[8];

   usr_msg[0]=0xAF;

   //usr_msg[1]=0xAD;

      TxHeader.DLC=2;

      TxHeader.RTR= CAN_RTR_DATA ;

      TxHeader.IDE= CAN_ID_STD;

      TxHeader.StdId= 0x65;

   if(HAL_CAN_AddTxMessage(&hcan1,&TxHeader,usr_msg,&TxMailbox)!=HAL_OK)

   {

            Error_Handler();

   }

       while(HAL_CAN_IsTxMessagePending(&hcan1,TxMailbox));

   HAL_GPIO_WritePin(GPIOB, LD1_Pin, GPIO_PIN_SET);

}

void RX(void)

{

   CAN_RxHeaderTypeDef RxHeader;

   uint8_t rcv_msg[10];

   while(HAL_CAN_GetRxFifoFillLevel(&hcan2,CAN_RX_FIFO0)<1);

   if(HAL_CAN_GetRxMessage(&hcan2,CAN_RX_FIFO0,&RxHeader,rcv_msg)!=HAL_OK)

   {

      //HAL_CAN_GetError(&hcan2);

      Error_Handler();

   }

   HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);

}

0 REPLIES 0