STM32G474 fdcan 接收中断与发送例程及代码实现
时间: 2023-09-28 17:11:02 浏览: 852
STM32之can 实例+代码解析.c
以下是STM32G474 FDCAN的发送和接收中断例程,其中包含了FDCAN的中断处理函数和数据处理函数。请注意,此处仅提供参考代码,具体实现过程中需要根据实际情况进行相应的修改。
发送中断例程:
```
#include "stm32g4xx_hal.h"
FDCAN_HandleTypeDef hfdcan1;
void FDCAN_Config(void)
{
/* Configure the FDCAN peripheral */
hfdcan1.Instance = FDCAN1;
hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
hfdcan1.Init.AutoRetransmission = ENABLE;
hfdcan1.Init.TransmitPause = DISABLE;
hfdcan1.Init.ProtocolException = DISABLE;
hfdcan1.Init.NominalPrescaler = 1;
hfdcan1.Init.NominalSyncJumpWidth = 1;
hfdcan1.Init.NominalTimeSeg1 = 6;
hfdcan1.Init.NominalTimeSeg2 = 3;
hfdcan1.Init.DataPrescaler = 1;
hfdcan1.Init.DataSyncJumpWidth = 1;
hfdcan1.Init.DataTimeSeg1 = 6;
hfdcan1.Init.DataTimeSeg2 = 3;
hfdcan1.Init.StdFiltersNbr = 1;
hfdcan1.Init.ExtFiltersNbr = 0;
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
hfdcan1.Init.TxFifoQueueSize = 2;
hfdcan1.Init.RxFifo0QueueSize = 2;
hfdcan1.Init.RxFifo1QueueSize = 2;
hfdcan1.Init.RxBuffersNbr = 0;
hfdcan1.Init.TxEventsNbr = 0;
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
{
Error_Handler();
}
/* Configure the FDCAN filter */
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = 0x321;
sFilterConfig.FilterID2 = 0x7FF;
if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
{
Error_Handler();
}
/* Start the FDCAN module */
if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_FDCAN_TxBufferCompleteCallback(FDCAN_HandleTypeDef* hfdcan, uint32_t BufferIndexes)
{
/* Handle the transmission complete event here */
}
void FDCAN_Send(uint32_t id, uint8_t* data, uint32_t len)
{
FDCAN_TxHeaderTypeDef txHeader;
txHeader.Identifier = id;
txHeader.IdType = FDCAN_STANDARD_ID;
txHeader.TxFrameType = FDCAN_DATA_FRAME;
txHeader.DataLength = len;
txHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
txHeader.BitRateSwitch = FDCAN_BRS_OFF;
txHeader.FDFormat = FDCAN_CLASSIC_CAN;
txHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
txHeader.MessageMarker = 0;
if (HAL_FDCAN_AddMessageToTxFifoQ(&hfdcan1, &txHeader, data) != HAL_OK)
{
Error_Handler();
}
}
```
接收中断例程:
```
#include "stm32g4xx_hal.h"
FDCAN_HandleTypeDef hfdcan1;
void FDCAN_Config(void)
{
/* Configure the FDCAN peripheral */
hfdcan1.Instance = FDCAN1;
hfdcan1.Init.FrameFormat = FDCAN_FRAME_CLASSIC;
hfdcan1.Init.Mode = FDCAN_MODE_NORMAL;
hfdcan1.Init.AutoRetransmission = ENABLE;
hfdcan1.Init.TransmitPause = DISABLE;
hfdcan1.Init.ProtocolException = DISABLE;
hfdcan1.Init.NominalPrescaler = 1;
hfdcan1.Init.NominalSyncJumpWidth = 1;
hfdcan1.Init.NominalTimeSeg1 = 6;
hfdcan1.Init.NominalTimeSeg2 = 3;
hfdcan1.Init.DataPrescaler = 1;
hfdcan1.Init.DataSyncJumpWidth = 1;
hfdcan1.Init.DataTimeSeg1 = 6;
hfdcan1.Init.DataTimeSeg2 = 3;
hfdcan1.Init.StdFiltersNbr = 1;
hfdcan1.Init.ExtFiltersNbr = 0;
hfdcan1.Init.TxFifoQueueMode = FDCAN_TX_FIFO_OPERATION;
hfdcan1.Init.TxFifoQueueSize = 2;
hfdcan1.Init.RxFifo0QueueSize = 2;
hfdcan1.Init.RxFifo1QueueSize = 2;
hfdcan1.Init.RxBuffersNbr = 0;
hfdcan1.Init.TxEventsNbr = 0;
if (HAL_FDCAN_Init(&hfdcan1) != HAL_OK)
{
Error_Handler();
}
/* Configure the FDCAN filter */
FDCAN_FilterTypeDef sFilterConfig;
sFilterConfig.IdType = FDCAN_STANDARD_ID;
sFilterConfig.FilterIndex = 0;
sFilterConfig.FilterType = FDCAN_FILTER_MASK;
sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
sFilterConfig.FilterID1 = 0x321;
sFilterConfig.FilterID2 = 0x7FF;
if (HAL_FDCAN_ConfigFilter(&hfdcan1, &sFilterConfig) != HAL_OK)
{
Error_Handler();
}
/* Start the FDCAN module */
if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
{
Error_Handler();
}
}
void HAL_FDCAN_RxFifo0MsgPendingCallback(FDCAN_HandleTypeDef* hfdcan)
{
FDCAN_RxHeaderTypeDef rxHeader;
uint8_t rxData[8];
uint32_t len;
if (HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_FIFO0, &rxHeader, rxData) != HAL_OK)
{
Error_Handler();
}
/* Handle the received message here */
}
void FDCAN_Receive(uint32_t* id, uint8_t* data, uint32_t* len)
{
/* Not used in this example */
}
```
在上述代码中,发送中断处理函数为`HAL_FDCAN_TxBufferCompleteCallback`,接收中断处理函数为`HAL_FDCAN_RxFifo0MsgPendingCallback`。同时,发送函数为`FDCAN_Send`,接收函数为`FDCAN_Receive`(此处未实现)。
在实现过程中,您需要将以上代码集成到您的工程中,并根据实际情况进行相应的修改。同时,您需要在主函数中使能相应的中断,如下所示:
```
/* Enable FDCAN interrupts */
HAL_NVIC_SetPriority(FDCAN_IT_TX_COMPLETE, 0, 0);
HAL_NVIC_EnableIRQ(FDCAN_IT_TX_COMPLETE);
HAL_NVIC_SetPriority(FDCAN_IT_RX_FIFO0, 0, 0);
HAL_NVIC_EnableIRQ(FDCAN_IT_RX_FIFO0);
```
以上代码中,使能了发送完成和接收FIFO0消息挂起两个中断。
阅读全文