bs_bcu_app/bsp/bsp_can.c

209 lines
7.1 KiB
C
Raw Normal View History

2024-11-20 17:07:14 +08:00
#include "bsp_flash.h"
2024-11-20 15:33:37 +08:00
#include "bsp_can.h"
#include "bsp_delay.h"
#include "drv_sys.h"
2024-11-12 19:03:56 +08:00
CAN_HandleTypeDef g_canx_handler; /* CANx<4E><78><EFBFBD><EFBFBD> */
CAN_TxHeaderTypeDef g_canx_txheader; /* <20><><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
CAN_RxHeaderTypeDef g_canx_rxheader; /* <20><><EFBFBD>ղ<EFBFBD><D5B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
/**
* @brief CAN<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
* @param tsjw : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ծʱ<EFBFBD>Ԫ.<EFBFBD><EFBFBD>Χ: 1~3;
* @param tbs2 : ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD>ʱ<EFBFBD>Ԫ.<EFBFBD><EFBFBD>Χ: 1~8;
* @param tbs1 : ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD>ʱ<EFBFBD>Ԫ.<EFBFBD><EFBFBD>Χ: 1~16;
* @param brp : <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʷ<EFBFBD>Ƶ<EFBFBD><EFBFBD>.<EFBFBD><EFBFBD>Χ: 1~1024;
* @note <EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD>ں<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD>κ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD><EFBFBD><EFBFBD>0
2024-11-20 15:33:37 +08:00
* CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD>APB1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>Ϊ Fpclk1 = PCLK1 = 42Mhz
2024-11-12 19:03:56 +08:00
* tq = brp * tpclk1;
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> = Fpclk1 / ((tbs1 + tbs2 + 1) * brp);
2024-11-20 15:33:37 +08:00
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> can_init(1, 6, 7, 6, 1), <EFBFBD><EFBFBD>CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ:
* 42M / ((6 + 7 + 1) * 6) = 500Kbps
2024-11-12 19:03:56 +08:00
*
2024-11-20 15:33:37 +08:00
* @param mode : CAN_MODE_NORMAL, <EFBFBD><EFBFBD>ͨģʽ;
2024-11-12 19:03:56 +08:00
CAN_MODE_LOOPBACK,<EFBFBD>ػ<EFBFBD>ģʽ;
* @retval 0, <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>;
*/
uint8_t can_init(uint32_t tsjw, uint32_t tbs2, uint32_t tbs1, uint16_t brp, uint32_t mode)
2024-11-20 15:33:37 +08:00
{
g_canx_handler.Instance = CAN1;
g_canx_handler.Init.Prescaler = brp; /* <20><>Ƶϵ<C6B5><CFB5>(Fdiv)Ϊbrp+1 */
g_canx_handler.Init.Mode = mode; /* ģʽ<C4A3><CABD><EFBFBD><EFBFBD> */
g_canx_handler.Init.SyncJumpWidth = tsjw; /* <20><><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>(Tsjw)Ϊtsjw+1<><31>ʱ<EFBFBD>䵥λ CAN_SJW_1TQ~CAN_SJW_4TQ */
g_canx_handler.Init.TimeSeg1 = tbs1; /* tbs1<73><31>ΧCAN_BS1_1TQ~CAN_BS1_16TQ */
g_canx_handler.Init.TimeSeg2 = tbs2; /* tbs2<73><32>ΧCAN_BS2_1TQ~CAN_BS2_8TQ */
g_canx_handler.Init.TimeTriggeredMode = DISABLE; /* <20><>ʱ<EFBFBD><EFBFBD><E4B4A5>ͨ<EFBFBD><CDA8>ģʽ */
g_canx_handler.Init.AutoBusOff = DISABLE; /* <20><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>߹<EFBFBD><DFB9><EFBFBD> */
g_canx_handler.Init.AutoWakeUp = DISABLE; /* ˯<><CBAF>ģʽͨ<CABD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>CAN->MCR<43><52>SLEEPλ) */
g_canx_handler.Init.AutoRetransmission = ENABLE; /* <20><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD> */
g_canx_handler.Init.ReceiveFifoLocked = DISABLE; /* <20><><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>,<2C>µĸ<C2B5><C4B8>Ǿɵ<C7BE> */
g_canx_handler.Init.TransmitFifoPriority = DISABLE; /* <20><><EFBFBD>ȼ<EFBFBD><C8BC>ɱ<EFBFBD><C9B1>ı<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (HAL_CAN_Init(&g_canx_handler) != HAL_OK)
{
return 1;
}
2024-11-12 19:03:56 +08:00
#if CAN_RX0_INT_ENABLE
2024-11-20 15:33:37 +08:00
/* ʹ<><CAB9><EFBFBD>жϽ<D0B6><CFBD><EFBFBD> */
__HAL_CAN_ENABLE_IT(&g_canx_handler, CAN_IT_RX_FIFO0_MSG_PENDING); /* FIFO0<4F><30>Ϣ<EFBFBD>Һ<EFBFBD><D2BA>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD> */
HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); /* ʹ<><CAB9>CAN<41>ж<EFBFBD> */
HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 1, 0); /* <20><>ռ<EFBFBD><D5BC><EFBFBD>ȼ<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>0 */
2024-11-12 19:03:56 +08:00
#endif
2024-11-20 15:33:37 +08:00
CAN_FilterTypeDef sFilterConfig;
/* <20><><EFBFBD><EFBFBD>CAN<41><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
sFilterConfig.FilterBank = 0; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0 */
sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000; /* 32λID */
sFilterConfig.FilterIdLow = 0x0000;
sFilterConfig.FilterMaskIdHigh = 0x0000; /* 32λMASK */
sFilterConfig.FilterMaskIdLow = 0x0000;
sFilterConfig.FilterFIFOAssignment = CAN_FILTER_FIFO0; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FIFO0 */
sFilterConfig.FilterActivation = CAN_FILTER_ENABLE; /* <20><><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD>0 */
sFilterConfig.SlaveStartFilterBank = 14;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if (HAL_CAN_ConfigFilter(&g_canx_handler, &sFilterConfig) != HAL_OK)
{
return 2;
}
/* <20><><EFBFBD><EFBFBD>CAN<41><4E>Χ<EFBFBD>豸 */
if (HAL_CAN_Start(&g_canx_handler) != HAL_OK)
{
return 3;
}
return 0;
2024-11-12 19:03:56 +08:00
}
/**
* @brief CAN<EFBFBD>ײ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD>HAL_CAN_Init()<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param hcan:CAN<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @retval <EFBFBD><EFBFBD>
*/
void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
{
2024-11-20 15:33:37 +08:00
if (CAN1 == hcan->Instance)
{
CAN_RX_GPIO_CLK_ENABLE(); /* CAN_RX<52><58>ʱ<EFBFBD><CAB1>ʹ<EFBFBD><CAB9> */
CAN_TX_GPIO_CLK_ENABLE(); /* CAN_TX<54><58>ʱ<EFBFBD><CAB1>ʹ<EFBFBD><CAB9> */
__HAL_RCC_CAN1_CLK_ENABLE(); /* ʹ<><CAB9>CAN1ʱ<31><CAB1> */
GPIO_InitTypeDef gpio_init_struct;
gpio_init_struct.Pin = CAN_TX_GPIO_PIN;
gpio_init_struct.Mode = GPIO_MODE_AF_PP;
gpio_init_struct.Pull = GPIO_PULLUP;
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init_struct.Alternate = GPIO_AF9_CAN1;
HAL_GPIO_Init(CAN_TX_GPIO_PORT, &gpio_init_struct); /* CAN_TX<54><58> ģʽ<C4A3><CABD><EFBFBD><EFBFBD> */
gpio_init_struct.Pin = CAN_RX_GPIO_PIN;
HAL_GPIO_Init(CAN_RX_GPIO_PORT, &gpio_init_struct); /* CAN_RX<52><58> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD>ģʽ */
}
2024-11-12 19:03:56 +08:00
}
#if CAN_RX0_INT_ENABLE /* ʹ<><CAB9>RX0<58>ж<EFBFBD> */
/**
* @brief CAN RX0 <EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @note <EFBFBD><EFBFBD><EFBFBD><EFBFBD>CAN FIFO0<EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
* @param <EFBFBD><EFBFBD>
* @retval <EFBFBD><EFBFBD>
*/
void USB_LP_CAN1_RX0_IRQHandler(void)
{
2024-11-20 15:33:37 +08:00
uint8_t rxbuf[8];
uint32_t id;
can_receive_msg(id, rxbuf);
printf("id:%d\r\n", g_canx_rxheader.StdId);
printf("ide:%d\r\n", g_canx_rxheader.IDE);
printf("rtr:%d\r\n", g_canx_rxheader.RTR);
printf("len:%d\r\n", g_canx_rxheader.DLC);
printf("rxbuf[0]:%d\r\n", rxbuf[0]);
printf("rxbuf[1]:%d\r\n", rxbuf[1]);
printf("rxbuf[2]:%d\r\n", rxbuf[2]);
printf("rxbuf[3]:%d\r\n", rxbuf[3]);
printf("rxbuf[4]:%d\r\n", rxbuf[4]);
printf("rxbuf[5]:%d\r\n", rxbuf[5]);
printf("rxbuf[6]:%d\r\n", rxbuf[6]);
printf("rxbuf[7]:%d\r\n", rxbuf[7]);
2024-11-12 19:03:56 +08:00
}
#endif
/**
* @brief CAN <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @note <EFBFBD><EFBFBD><EFBFBD>͸<EFBFBD>ʽ<EFBFBD>̶<EFBFBD>Ϊ: <EFBFBD><EFBFBD>׼ID, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡
* @param id : <EFBFBD><EFBFBD>׼ID(11λ)
2024-11-20 15:33:37 +08:00
* @param msg : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @param len : <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
2024-11-12 19:03:56 +08:00
* @retval <EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ 0, <EFBFBD>ɹ<EFBFBD>; 1, ʧ<EFBFBD><EFBFBD>;
*/
uint8_t can_send_msg(uint32_t id, uint8_t *msg, uint8_t len)
{
2024-11-20 15:33:37 +08:00
uint16_t t = 0;
uint32_t TxMailbox = CAN_TX_MAILBOX0;
g_canx_txheader.StdId = id; /* <20><>׼<EFBFBD><D7BC>ʶ<EFBFBD><CAB6> */
g_canx_txheader.ExtId = id; /* <20><>չ<EFBFBD><D5B9>ʶ<EFBFBD><CAB6>(29λ) */
g_canx_txheader.IDE = CAN_ID_STD; /* ʹ<>ñ<EFBFBD>׼֡ */
g_canx_txheader.RTR = CAN_RTR_DATA; /* <20><><EFBFBD><EFBFBD>֡ */
g_canx_txheader.DLC = len;
if (HAL_CAN_AddTxMessage(&g_canx_handler, &g_canx_txheader, msg, &TxMailbox) != HAL_OK) /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ */
{
return 1;
}
while (HAL_CAN_GetTxMailboxesFreeLevel(&g_canx_handler) != 3) /* <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA> */
{
t++;
if (t > 0xFFF)
{
HAL_CAN_AbortTxRequest(&g_canx_handler, TxMailbox); /* <20><>ʱ<EFBFBD><CAB1>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
return 1;
}
}
2024-11-12 19:03:56 +08:00
2024-11-20 15:33:37 +08:00
return 0;
2024-11-12 19:03:56 +08:00
}
/**
* @brief CAN <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD>ѯ
* @note <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD>ʽ<EFBFBD>̶<EFBFBD>Ϊ: <EFBFBD><EFBFBD>׼ID, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡
* @param id : Ҫ<EFBFBD><EFBFBD>ѯ<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>׼ID(11λ)
* @param buf : <EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @retval <EFBFBD><EFBFBD><EFBFBD>ս<EFBFBD><EFBFBD><EFBFBD>
* @arg 0 , <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>;
* @arg <EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
*/
uint8_t can_receive_msg(uint32_t id, uint8_t *buf)
{
2024-11-20 15:33:37 +08:00
if (HAL_CAN_GetRxFifoFillLevel(&g_canx_handler, CAN_RX_FIFO0) == 0) /* û<>н<EFBFBD><D0BD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD> */
{
return 0;
}
2024-11-12 19:03:56 +08:00
2024-11-20 15:33:37 +08:00
if (HAL_CAN_GetRxMessage(&g_canx_handler, CAN_RX_FIFO0, &g_canx_rxheader, buf) != HAL_OK) /* <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD> */
{
return 0;
}
if (g_canx_rxheader.StdId!= id || g_canx_rxheader.IDE != CAN_ID_STD || g_canx_rxheader.RTR != CAN_RTR_DATA) /* <20><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD>ID<49><44><EFBFBD><EFBFBD> / <20><><EFBFBD>DZ<EFBFBD>׼֡ / <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡ */
{
return 0;
}
return g_canx_rxheader.DLC;
}