You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.6 KiB

5 months ago
/*
*********************************************************************************************************
*
* ģ : FDCANģ
* ļ : bsp_can.c
* : V1.0
* ˵ : CAN.
*
* ޸ļ¼ :
* ˵
* V1.0 2018-11-14 armfly ʽ
*
* Copyright (C), 2018-2030, www.armfly.com
*
*********************************************************************************************************
*/
#include "bsp_can.h"
#include "bsp_msg.h"
#include "fdcan.h"
/*
CAN1ҪV7ϵJ12J13ñ1-2šȱʡDz
CNA2Ӳߣ̫ҪΣŸã
*/
/* FDCAN1 GPIO���� */
#define FDCAN1_TX_PIN GPIO_PIN_12
#define FDCAN1_TX_GPIO_PORT GPIOA
#define FDCAN1_TX_AF GPIO_AF9_FDCAN1
#define FDCAN1_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define FDCAN1_RX_PIN GPIO_PIN_11
#define FDCAN1_RX_GPIO_PORT GPIOA
#define FDCAN1_RX_AF GPIO_AF9_FDCAN1
#define FDCAN1_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
FDCAN_HandleTypeDef hfdcan1;
FDCAN_FilterTypeDef sFilterConfig1;
/* FDCAN1 GPIO���� */
#define FDCAN2_TX_PIN GPIO_PIN_13
#define FDCAN2_TX_GPIO_PORT GPIOB
#define FDCAN2_TX_AF GPIO_AF9_FDCAN2
#define FDCAN2_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define FDCAN2_RX_PIN GPIO_PIN_12
#define FDCAN2_RX_GPIO_PORT GPIOB
#define FDCAN2_RX_AF GPIO_AF9_FDCAN2
#define FDCAN2_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
FDCAN_FilterTypeDef sFilterConfig2;
FDCAN_RxHeaderTypeDef g_Can2RxHeader;
uint8_t g_Can2RxData[64];
/*
*********************************************************************************************************
* : bsp_InitCan1
* ˵: ʼCAN1
* :
* ֵ:
*********************************************************************************************************
*/
void bsp_InitCan1(void)
{
}
/*
*********************************************************************************************************
* : bsp_InitCan2
* ˵: ʼCAN2
* :
* ֵ:
*********************************************************************************************************
*/
void bsp_InitCan2(void)
{
MX_FDCAN2_Init();
}
/*
*********************************************************************************************************
* : bsp_DeInitCan1
* ˵: ͷCAN1
* :
* ֵ:
*********************************************************************************************************
*/
void bsp_DeInitCan1(void)
{
HAL_FDCAN_MspDeInit(&hfdcan1);
}
/*
*********************************************************************************************************
* : bsp_DeInitCan2
* ˵: ͷCAN2
* :
* ֵ:
*********************************************************************************************************
*/
void bsp_DeInitCan2(void)
{
HAL_FDCAN_MspDeInit(&hfdcan2);
}
/*
*********************************************************************************************************
* : HAL_FDCAN_RxFifo0Callback
* ˵: CANжϷ-ص
* : hfdcan
* ֵ:
*********************************************************************************************************
*/
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{
if (hfdcan == &hfdcan2)
{
/* Retreive Rx messages from RX FIFO0 */
if(HAL_FDCAN_GetRxMessage(hfdcan, FDCAN_RX_FIFO0, &g_Can2RxHeader, g_Can2RxData)==HAL_OK)
{
/* Activate Rx FIFO 0 watermark notification */
HAL_FDCAN_ActivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_WATERMARK, 0);
if(g_Can2RxHeader.Identifier == 0x110 && g_Can2RxHeader.IdType == FDCAN_STANDARD_ID)
{
if(g_Can2RxData[0]==0x55)
{
Update_EN = 1;
}
}
if (g_Can2RxHeader.Identifier == 0x222 && g_Can2RxHeader.IdType == FDCAN_STANDARD_ID)
{
bsp_PutMsg(MSG_CAN2_RX, 0); /* ����Ϣ�յ����ݰ���������g_Can1RxHeader�� g_Can1RxData */
}
}
}
}
/*
*********************************************************************************************************
* : can2_SendPacket
* ˵: һ
* Σ_DataBuf ݻ
* _Len ݳ, 0-8ֽ
* ֵ:
*********************************************************************************************************
*/
FDCAN_TxHeaderTypeDef TxHeader;
void can2_SendPacket(uint8_t *_DataBuf, uint8_t _Len)
{
if (_Len > 8)
{
return;
}
/* Prepare Tx Header */
TxHeader.Identifier = 0x1FF;
TxHeader.IdType = FDCAN_STANDARD_ID;
TxHeader.TxFrameType = FDCAN_DATA_FRAME;
TxHeader.DataLength = (uint32_t)_Len << 16;
TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
TxHeader.BitRateSwitch = FDCAN_BRS_OFF;
TxHeader.FDFormat = FDCAN_CLASSIC_CAN;
TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
TxHeader.MessageMarker = 0;
/* Add messages to TX FIFO */
HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan2, &TxHeader, _DataBuf);
}
/*
*********************************************************************************************************
* : FDCAN1_IT0_IRQHandler
* ˵: CANжϷ
* : hfdcan
* ֵ:
*********************************************************************************************************
*/
void FDCAN1_IT0_IRQHandler(void)
{
HAL_FDCAN_IRQHandler(&hfdcan1);
}
void FDCAN1_IT1_IRQHandler(void)
{
HAL_FDCAN_IRQHandler(&hfdcan1);
}
void FDCAN2_IT1_IRQHandler(void)
{
HAL_FDCAN_IRQHandler(&hfdcan2);
}
void FDCAN_CAL_IRQHandler(void)
{
HAL_FDCAN_IRQHandler(&hfdcan1);
HAL_FDCAN_IRQHandler(&hfdcan2); // ???
}
/***************************** ���������� www.armfly.com (END OF FILE) *********************************/