cancel
Showing results for 
Search instead for 
Did you mean: 

Hi i am using stm32f746zg nucleo board and i want to establish communication between two CAN modules where CAN1 should transmit and CAN2 should receive the msg but its not happening dont know why ..can anyone help me with the code ..?

B Vinod
Associate II

here is my code

void can(void)

{

   CAN_TxHeaderTypeDef TxHeader;

   uint32_t TxMailbox;

   uint8_t usr_msg[10]={'h','e','l','l','o'};

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

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

   {

      Error_Handler();

   }

   HAL_GPIO_WritePin(GPIOB, LD2_Pin, GPIO_PIN_SET);

}

14 REPLIES 14
KnarfB
Principal III

HAL_CAN_GetRxFifoFillLevel returns the number of available messages. To wait for "at least one message is in the fifo", you should write

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

I don't see the Rx filter setup in the code provided. Filters are another common source of errors.

 void CAN1_Init(void)

{

 hcan1.Instance = CAN1;

 hcan1.Init.Prescaler = 16;

 hcan1.Init.Mode = CAN_MODE_NORMAL;

 hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;

 hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;

 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();

 }

}

void CAN2_Init(void)

{

 hcan2.Instance = CAN2;

 hcan2.Init.Prescaler = 16;

 hcan2.Init.Mode = CAN_MODE_NORMAL;

 hcan2.Init.SyncJumpWidth = CAN_SJW_1TQ;

 hcan2.Init.TimeSeg1 = CAN_BS1_1TQ;

 hcan2.Init.TimeSeg2 = CAN_BS2_1TQ;

 hcan2.Init.TimeTriggeredMode = DISABLE;

 hcan2.Init.AutoBusOff = ENABLE;

 hcan2.Init.AutoWakeUp = DISABLE;

 hcan2.Init.AutoRetransmission = ENABLE;

 hcan2.Init.ReceiveFifoLocked = DISABLE;

 hcan2.Init.TransmitFifoPriority = DISABLE;

 if (HAL_CAN_Init(&hcan2) != HAL_OK)

 {

   Error_Handler();

 }

}

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

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

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

   {

      Error_Handler();

   }

}

sir have you seen my filter configurations ? are they correct..?

KnarfB
Principal III

The filters look okay to me. How are CAN1 and CAN2 connected, with PHYs?

The _1TQ timing look like defaults. Have you calculated it like with http://www.bittiming.can-wiki.info/

CAN1 and CAN2 are connected physically with jumper wires and i am using cjmcu230 canbus module. i changed the bit rate to 100kbps its transmitting perfectly but at reception side it is entering into Error_Handler(); .0693W000001szTLQAY.jpg

There is a HAL_CAN_GetError function telling you a bit more about the error. Or: debug-step through HAL_CAN_GetRxMessage

when i debug the code in this part it is entering into Error_Handler();

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

   {

      Error_Handler();

   }

Yes, I got that. Ideas:

  1. There is a HAL_CAN_GetError function telling you a bit more about the error, add it to your code.
  2. Debug-step through HAL_CAN_GetRxMessage and find out what went wrong.

sir when i debug the code its not showing any error code related to can. its just simply entering into that function and coming back