//CAN1 初始化
void bsp_can1_init(void)
{
/*过滤器 CAN1过滤器范围0-13 ,CAN2过滤器范围14-27*/
CAN_FilterTypeDef sFilterConfig;
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
// FilterConfig_Scale32_Null(&hcan1,0);//不过滤
// FilterConfig_Scale32_IdMask_StandardIdOnly(&hcan1,1,(uint16_t *)StdIdArray);//单标准ID
// Filter_Scale32_IdMask_ExtendIdOnly(&hcan1,2,(uint32_t *)ExtIdArray);//单扩展ID
// Filter_Scale32_IdMask_StandardId_ExtendId_Mix(&hcan1,3,(uint16_t *)StdIdArray,(uint32_t *)ExtIdArray);//标准ID + 扩展ID
/*波特率重新调整*/
CAN_Baud_Process(250,&hcan1);
/* Start the CAN peripheral */
if (HAL_CAN_Start(&hcan1) != HAL_OK)
{
/* Start Error */
Error_Handler();
}
/* Activate CAN RX notification */
if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
{
/* Notification Error */
Error_Handler();
}
// /* Activate CAN BUSOFF notification ###################################*/
// if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_BUSOFF) != HAL_OK)
// {
// /* Notification Error */
// Error_Handler();
// }
// /* Activate CAN TX notification */
// if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_TX_MAILBOX_EMPTY) != HAL_OK)
// {
// /* Notification Error */
// Error_Handler();
// }
}
//CAN2 初始化
void bsp_can2_init(void)
{
/*过滤器 CAN1过滤器范围0-13 ,CAN2过滤器范围14-27*/
CAN_FilterTypeDef sFilterConfig;
sFilterConfig.FilterBank = 14;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000;
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
sFilterConfig.FilterActivation = ENABLE;
sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&hcan1, &sFilterConfig) != HAL_OK)
{
/* Filter configuration Error */
Error_Handler();
}
// FilterConfig_Scale32_Null(&hcan2,14);//不过滤
// FilterConfig_Scale32_IdMask_StandardIdOnly(&hcan1,15,(uint16_t *)StdIdArray);//单标准ID
// Filter_Scale32_IdMask_ExtendIdOnly(&hcan1,16,(uint32_t *)ExtIdArray);//单扩展ID
// Filter_Scale32_IdMask_StandardId_ExtendId_Mix(&hcan1,17,(uint16_t *)StdIdArray,(uint32_t *)ExtIdArray);//标准ID + 扩展ID
/*波特率重新调整*/
CAN_Baud_Process(250,&hcan2);
/* Start the CAN peripheral */
if (HAL_CAN_Start(&hcan2) != HAL_OK)
{
/* Start Error */
Error_Handler();
}
/* Activate CAN RX notification */
if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING) != HAL_OK)
{
/* Notification Error */
Error_Handler();
}
// /* Activate CAN BUSOFF notification ###################################*/
// if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_BUSOFF) != HAL_OK)
// {
// /* Notification Error */
// Error_Handler();
// }
// /* Activate CAN TX notification */
// if (HAL_CAN_ActivateNotification(&hcan2, CAN_IT_TX_MAILBOX_EMPTY) != HAL_OK)
// {
// /* Notification Error */
// Error_Handler();
// }
}
/**
* @brief Rx FIFO 0 full callback.
* @param hcan pointer to a CAN_HandleTypeDef structure that contains
* the configuration information for the specified CAN.
* @retval None
*/
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
//CAN1 -FIFO0 接收处理
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &bsp_can1.RxMessage, bsp_can1.RxData) != HAL_OK) {
Error_Handler();
}
uint32_t r_std_id = bsp_can1.RxMessage.StdId;
uint32_t r_ext_id = bsp_can1.RxMessage.ExtId;
printf("CAN1->0x%03X, 0x%08X, %02X, %02X, %02X, %02X, %02X, %02X, %02X, %02X\r\n",
r_std_id, r_ext_id,
bsp_can1.RxData[0], bsp_can1.RxData[1], bsp_can1.RxData[2], bsp_can1.RxData[3],
bsp_can1.RxData[4], bsp_can1.RxData[5], bsp_can1.RxData[6], bsp_can1.RxData[7]);
bsp_can1.RxMessage.StdId = 0;
bsp_can1.RxMessage.ExtId = 0;
}
/**
* @brief Rx FIFO 1 message pending callback.
* @param hcan pointer to a CAN_HandleTypeDef structure that contains
* the configuration information for the specified CAN.
* @retval None
*/
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
//CAN2 -FIFO1 接收处理
if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &bsp_can2.RxMessage, bsp_can2.RxData) != HAL_OK) {
Error_Handler();
}
uint32_t r_std_id = bsp_can2.RxMessage.StdId;
uint32_t r_ext_id = bsp_can2.RxMessage.ExtId;
printf("CAN2->0x%03X, 0x%08X, %02X, %02X, %02X, %02X, %02X, %02X, %02X, %02X\r\n",
r_std_id, r_ext_id,
bsp_can2.RxData[0], bsp_can2.RxData[1], bsp_can2.RxData[2], bsp_can2.RxData[3],
bsp_can2.RxData[4], bsp_can2.RxData[5], bsp_can2.RxData[6], bsp_can2.RxData[7]);
bsp_can2.RxMessage.StdId = 0;
bsp_can2.RxMessage.ExtId = 0;
/* NOTE : This function Should not be modified, when the callback is needed,
the HAL_CAN_RxFifo1MsgPendingCallback could be implemented in the
user file
*/
}
/**
* @brief CAN1 发送数据
* @param StdId 标准ID,不使用则传0
* @param ExtId 扩展ID,不使用则传0
* @param pData 发送数据
* @param Len 发送长度
*/
uint8_t CAN1_SendNormalData(uint16_t StdId,uint32_t ExtId, uint16_t Len)
{
HAL_StatusTypeDef HAL_RetVal;
uint8_t FreeTxNum = 0;
bsp_can1.TxMessage.StdId = StdId;
bsp_can1.TxMessage.ExtId = ExtId;
if(ExtId)bsp_can1.TxMessage.IDE = CAN_ID_EXT;
else bsp_can1.TxMessage.IDE = CAN_ID_STD;
bsp_can1.TxMessage.TransmitGlobalTime = DISABLE;
bsp_can1.TxMessage.RTR = CAN_RTR_DATA;//使用标准帧
if(Len > 8 || !Len)return 1;
bsp_can1.TxMessage.DLC = Len;
FreeTxNum = HAL_CAN_GetTxMailboxesFreeLevel(&hcan1);
while(0 == FreeTxNum)
{
osDelay(1);
FreeTxNum = HAL_CAN_GetTxMailboxesFreeLevel(&hcan1);
if(0 == FreeTxNum)
{
return 2;
}
}
HAL_RetVal = HAL_CAN_AddTxMessage(&hcan1, &bsp_can1.TxMessage, bsp_can1.TxData, &bsp_can1.TxMailbox);
if(HAL_RetVal != HAL_OK)
{
return 2;
}
return 0;
}
/*** @brief CAN2 发送数据
* @param StdId 标准ID,不使用则传0
* @param ExtId 扩展ID,不使用则传0
* @param pData 发送数据
* @param Len 发送长度
*/
uint8_t CAN2_SendNormalData(uint16_t StdId,uint32_t ExtId, uint16_t Len)
{
HAL_StatusTypeDef HAL_RetVal;
uint8_t FreeTxNum = 0;
bsp_can2.TxMessage.StdId = StdId;
bsp_can2.TxMessage.ExtId = ExtId;
if(ExtId)bsp_can2.TxMessage.IDE = CAN_ID_EXT;
else bsp_can2.TxMessage.IDE = CAN_ID_STD;
bsp_can2.TxMessage.TransmitGlobalTime = DISABLE;
bsp_can2.TxMessage.RTR = CAN_RTR_DATA;//使用标准帧
|