STM32 HAL library development series - CAN communication filter

Detailed introduction of CAN communication

CAN (Controller Area Network) communication is a serial bus technology that is widely used in automotive electronics, industrial automation, medical equipment, home automation and other fields.

CAN communication network consists of a group of interconnected nodes, each node can receive and send data. All nodes share a bus for data transmission, and identify data frames by unique numbers (called frame IDs).

CAN communication is characterized by a high degree of fault tolerance and high efficiency. It can support many different devices and applications, and it works well in environments with high interference levels.

CAN communication uses two types of frames to transmit data: data frames and remote frames. Data frames are used to transmit data between nodes, and remote frames are used to request other nodes to send specific data.

Under the HAL library of STM32, CAN communication can be initialized with the following code.

/**
  * @brief          CAN筛选器
  */
HAL_StatusTypeDef Can_Filter_Init(void)
{
    
    
	/***	CAN1	***/
    CAN_FilterTypeDef sFilterConfig;
	sFilterConfig.FilterActivation = ENABLE;			// 激活过滤器
	sFilterConfig.FilterBank = 0;						// 配置主CAN筛选器组编号
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;	// 配置工作模式为列表模式
	sFilterConfig.FilterScale = CAN_FILTERSCALE_16BIT;	// 配置筛选器的尺度为16位长
	sFilterConfig.FilterIdHigh = 0x0000;				// CAN_FxR1寄存器
	sFilterConfig.FilterIdLow = 0x0000;
	sFilterConfig.FilterMaskIdHigh = 0x0000;			// CAN_FxR2寄存器
	sFilterConfig.FilterMaskIdLow = 0x0000;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;	// 筛选器接筛选报文关联到FIFO0
	
	if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK) {
    
    						// 配置CAN1接收筛选过滤器
		Error_Handler();
	}
	if (HAL_CAN_Start(&hcan1) != HAL_OK) {
    
    												// 开启CAN1
		Error_Handler();
	}
	if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK) {
    
    	// 开启CAN1的FIFO0接收中断
		Error_Handler();
	}
//	if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING) != HAL_OK) {	// 开启CAN1的FIFO1接收中断
//		Error_Handler();
//	}

For the second CAN of STM32, the same configuration method is adopted. But pay attention to the sFilterConfig2.FilterBank and sFilterConfig2.SlaveStartFilterBank of CAN2, that is, the CAN filter bank number and the start filter of CAN2 are different from the above.

	/***	CAN2	***/
	CAN_FilterTypeDef sFilterConfig2;
	sFilterConfig2.FilterActivation = ENABLE;
	sFilterConfig2.FilterBank = 0;
	sFilterConfig2.FilterMode = CAN_FILTERMODE_IDMASK;
	sFilterConfig2.FilterScale = CAN_FILTERSCALE_16BIT;
	sFilterConfig2.FilterIdHigh = 0x0000;
	sFilterConfig2.FilterIdLow = 0x0000;
	sFilterConfig2.FilterMaskIdHigh = 0x0000;
	sFilterConfig2.FilterMaskIdLow = 0x0000;
	sFilterConfig2.FilterFIFOAssignment = CAN_RX_FIFO0;
	sFilterConfig2.FilterBank = 14;						// 设置从CAN过滤器组编号
	sFilterConfig2.SlaveStartFilterBank = 14;			// 设置CAN2的起始过滤器组

	if (HAL_CAN_ConfigFilter(&hcan2, &sFilterConfig2) != HAL_OK) {
    
    
		Error_Handler();
	}
	if (HAL_CAN_Start(&hcan2) != HAL_OK) {
    
    												// 开启CAN2
		Error_Handler();
	}
	if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK) {
    
    	// 开启CAN2的FIFO0接收中断
		Error_Handler();
	}

	return HAL_OK;
}

The CAN communication network also supports a variety of advanced functions, such as network management, error detection and correction, and so on. It is also adaptive, automatically adjusting the communication rate according to the load and performance demands in the network.

Guess you like

Origin blog.csdn.net/sorcererr/article/details/128701375