CAN2 cannot communicate problem

1. Issues to pay attention to in CAN2 communication

Insert image description here

It is easy to see from this picture that can1 is the master and can2 is the slave. Therefore, the prerequisite for starting can2 is that can1 has been started.
can communication can2 is based on can1
can1 must be initialized before using can2
When using CAN2 alone, be sure to turn on the clocks of CAN1 and CAN2 at the same time, otherwise CAN2 will not work properly, because CAN2 is the slave CAN and CAN1 is the master CAN
Therefore, for example, if FIFO0 is used, the filter Assignment:
The filter group of can1 starts from 0 and ranges from 0-14
CAN_FilterInitStructure.CAN_FilterNumber=0;
can2 The filter group starts from 14 and ranges from 14-27
CAN_FilterInitStructure.CAN_FilterNumber=14;
Note: can filter configuration must be in hexadecimal a>
If it still doesn’t work, check whether the STB pin of the CAN chip is enabled! ! ! ,This is very important!
When CAN is working normally, the STB pin is low level. When the STB pin is high level, the CAN bus is idle

void can_filter(CAN_HandleTypeDef* hcan)
{
    
    
	CAN_FilterTypeDef		CAN_FilterConfigStructure;
	if (hcan == &hcan2)
	{
    
    
	    CAN_FilterConfigStructure.FilterBank = 14;//***********************!!
	}
	if (hcan == &hcan1)
	{
    
    
		CAN_FilterConfigStructure.FilterBank = 0;
	}
	CAN_FilterConfigStructure.FilterMode = CAN_FILTERMODE_IDMASK;
	CAN_FilterConfigStructure.FilterScale = CAN_FILTERSCALE_32BIT;
	CAN_FilterConfigStructure.FilterIdHigh = 0x0000;
	CAN_FilterConfigStructure.FilterIdLow = 0x0000;
	CAN_FilterConfigStructure.FilterMaskIdHigh = 0x0000;
	CAN_FilterConfigStructure.FilterMaskIdLow = 0x0000;

	CAN_FilterConfigStructure.FilterFIFOAssignment = CAN_RX_FIFO0;
	CAN_FilterConfigStructure.SlaveStartFilterBank = 14;
	CAN_FilterConfigStructure.FilterActivation = ENABLE;
    HAL_CAN_ConfigFilter(hcan, &CAN_FilterConfigStructure);      
}

  MX_CAN1_Init();//先初始化can1!!!!!
  MX_CAN2_Init();
 can_filter(hcan1);
 can_filter(hcan2);

2. The problem that CAN interrupt cannot be entered

When the mailbox has been occupied, there is a problem with CAN communication. As a protection mechanism, CAN should be re-initialized
Origin of the problem - I have been doing communication with 16s smart batteries recently. , but the drone suddenly shows that the hub voltage is 24v (this hub only supports 2s-12s batteries and cannot display the 16s battery voltage), which can easily cause a low voltage return, but the actual voltage is still 64v
I have always suspected It's a crc verification problem. Finally, I used fdr to record data and found that the battery interrupt was not entered. Then I checked further and found that the entire CAN2 interrupt was not entered.
So don't limit your search to your own problems. On the one hand, it may not be your own problem, so gradually expand the investigation

  transmit_timeout = 0;
    while(!(((CANx->TSR)&CAN_TSR_TME0)&&((CANx->TSR)&CAN_TSR_TME1) &&((CANx->TSR)&CAN_TSR_TME2)))
    {
    
    
      transmit_timeout++;
      if(transmit_timeout == 65535)
      {
    
        
        if(CANx == CAN1)
        {
    
    
          CAN_DeInit(CAN1);
          CAN_DeInit(CAN2);
          CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_Normal);
        }
        else
        {
    
      
          CAN_DeInit(CAN2);
          CAN2_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,CAN_Mode_Normal);
        } 
      }
    }
  }

Guess you like

Origin blog.csdn.net/weixin_44057803/article/details/134303381