stm32 can bus summary

/* CAN init function */
void MX_CAN_Init(void)
{
  CAN_FilterConfTypeDef filter;
  hcan.Instance = CAN1;
  hcan.Init.Prescaler = 3;
//  hcan.Init.Mode = CAN_MODE_NORMAL;
  hcan.Init.Mode = CAN_MODE_LOOPBACK;
  hcan.Init.SJW = CAN_SJW_1TQ;
  hcan.Init.BS1 = CAN_BS1_12TQ;
  hcan.Init.BS2 = CAN_BS2_2TQ;
  hcan.Init.TTCM = DISABLE;
  hcan.Init.ABOM = DISABLE;
  hcan.Init.AWUM = DISABLE;
  hcan.Init.NART = DISABLE;
  hcan.Init.RFLM = DISABLE;
  hcan.Init.TXFP = ENABLE;

  //Add the following three items on the basis of cube generation to save the content of transmission and reception
  hcan.pTxMsg = &CAN_TX_MSG; //Use to save the content to be sent
  hcan.pRxMsg = &CAN_RX0_MSG; //Corresponding to fifo0 to receive
  hcan.pRx1Msg = &CAN_RX1_MSG ; //Corresponding to fifo1 receive
  if (HAL_CAN_Init(&hcan) != HAL_OK)  
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  filter.FilterNumber=1;
  filter.FilterMode=CAN_FILTERMODE_IDMASK;
  filter.FilterScale=CAN_FILTERSCALE_32BIT;
  filter.FilterIdHigh=0x0000;
  filter .FilterIdLow=0x0000 ; filter.FilterMaskIdHigh
  =0x0000;
  filter.FilterMaskIdLow=0x0000;
  filter.FilterFIFOAssignment=CAN_FIFO0;
  filter.FilterActivation=ENABLE;
 
  if( HAL_CAN_ConfigFilter(&hcan,&filter) != HAL_OK) //At the beginning, because the filter configuration was not added, the data could not be received, so the receiving interrupt could not enter.
  {
      Error_Handler();
  }
    
  if(HAL_CAN_Receive_IT(&hcan,CAN_FIFO0) != HAL_OK) // Open the receive interrupt (note that the interrupt processing automatically generated by the cube will clear the interrupt enable again, so it can only be accepted once, if you want to receive it repeatedly each time Reception must be reopened)
  {
      Error_Handler();
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324690862&siteId=291194637