S32K144-EVB开发板CAN实例代码
时间: 2023-10-01 10:06:55 浏览: 155
S32K144的boot,可修改普通CAN
5星 · 资源好评率100%
以下是S32K144-EVB开发板CAN的示例代码,可以作为参考:
```c
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_flexcan.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/* Define constants for FlexCAN baudrate calculation */
#define PCLK_FREQ (CLOCK_GetPllFreq(kCLOCK_SystemPll1Ctrl) / (CLOCK_GetRootPreDivider(kCLOCK_RootCan1)) / \
(CLOCK_GetRootPostDivider(kCLOCK_RootCan1)) / 2U)
#define FLEXCAN_EXAMPLE_FRAME_COUNT (1U)
#define EXAMPLE_FLEXCAN (CAN0)
#define EXAMPLE_CAN_CLK_FREQ (PCLK_FREQ)
#define EXAMPLE_CAN_IRQn CAN0_IRQn
#define EXAMPLE_CAN_IRQHandler CAN0_IRQHandler
#define EXAMPLE_FLEXCAN_DMA_IRQn DMA0_DMA16_IRQn
#define EXAMPLE_RX_MESSAGE_BUFFER_NUM (5U)
#define EXAMPLE_TX_MESSAGE_BUFFER_NUM (4U)
#define RX_MAILBOX_NUM (7U)
#define TX_MAILBOX_NUM (8U)
#define DLC (8U)
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
/* ESR register */
volatile uint32_t g_err_cnt = 0;
volatile uint32_t g_esr = 0;
/* CAN message buffer */
flexcan_mb_transfer_t rxXfer, txXfer;
/* Received data */
flexcan_frame_t rxFrames[EXAMPLE_FLEXCAN_FRAME_COUNT];
/* Remote request data */
flexcan_frame_t txRemoteFrame;
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief FlexCAN user callback function.
*
* This function is called when FlexCAN receive a frame or finished a transfer.
*
* @param base FlexCAN peripheral base address.
* @param handle Pointer to FlexCAN handle structure.
* @param status FlexCAN transfer status.
* @param userData User data.
*/
void FLEXCAN_UserCallback(CAN_Type *base, flexcan_handle_t *handle, status_t status, void *userData)
{
switch (status)
{
/* Process FlexCAN Rx event. */
case kStatus_FLEXCAN_RxIdle:
PRINTF("Frame received successfully!\r\n");
break;
/* Process FlexCAN Tx event. */
case kStatus_FLEXCAN_TxIdle:
PRINTF("Frame transmitted successfully!\r\n");
break;
/* Process FlexCAN error event. */
case kStatus_FLEXCAN_ErrorStatus:
PRINTF("FlexCAN error happened: %#x!\r\n", handle->errorCode);
break;
default:
break;
}
}
/*!
* @brief Main function.
*/
int main(void)
{
flexcan_config_t flexcanConfig;
flexcan_frame_t txFrame;
flexcan_handle_t flexcanHandle;
uint32_t i;
/* Initialize board hardware. */
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
PRINTF("\r\nFlexCAN example -- Start.\r\n");
/* Initialize FlexCAN module. */
flexcanConfig.clkSrc = kFLEXCAN_ClkSrcPeri;
flexcanConfig.baudRate = 125000U;
flexcanConfig.enableLoopBack = false;
flexcanConfig.enableSelfWakeup = false;
flexcanConfig.enableIndividMask = false;
flexcanConfig.maxMbNum = 16U;
FLEXCAN_Init(EXAMPLE_FLEXCAN, &flexcanConfig, EXAMPLE_CAN_CLK_FREQ);
/* Create FlexCAN handle structure and set call back function. */
FLEXCAN_TransferCreateHandle(EXAMPLE_FLEXCAN, &flexcanHandle, FLEXCAN_UserCallback, NULL);
/* Setup Rx Message Buffer. */
rxXfer.frame = &rxFrames[0];
rxXfer.mbIdx = RX_MAILBOX_NUM;
FLEXCAN_SetRxMbConfig(EXAMPLE_FLEXCAN, rxXfer.mbIdx, true);
/* Setup Tx Message Buffer. */
txXfer.frame = &txFrame;
txXfer.mbIdx = TX_MAILBOX_NUM;
FLEXCAN_SetTxMbConfig(EXAMPLE_FLEXCAN, txXfer.mbIdx, true);
/* Start receive data through polling mode. */
rxXfer.frame = &rxFrames[0];
rxXfer.mbIdx = RX_MAILBOX_NUM;
/* Set up remote request data. */
txRemoteFrame.format = kFLEXCAN_FrameFormatStandard;
txRemoteFrame.type = kFLEXCAN_FrameTypeRemote;
txRemoteFrame.id = FLEXCAN_ID_STD(0x123);
txRemoteFrame.length = DLC;
while (1)
{
/* Wait for receive data. */
while (kStatus_FLEXCAN_RxIdle != FLEXCAN_TransferReceiveNonBlocking(EXAMPLE_FLEXCAN, &flexcanHandle, &rxXfer))
{
}
/* Copy remote request data to Tx Message Buffer. */
for (i = 0; i < DLC; i++)
{
txFrame.dataByte[i] = txRemoteFrame.dataByte[i];
}
txFrame.format = kFLEXCAN_FrameFormatStandard;
txFrame.type = kFLEXCAN_FrameTypeData;
txFrame.id = FLEXCAN_ID_STD(0x321);
txFrame.length = DLC;
/* Wait until previous FlexCAN transfer finished. */
while (kStatus_FLEXCAN_TxIdle != FLEXCAN_GetTransferStatusFlags(EXAMPLE_FLEXCAN, txXfer.mbIdx))
{
}
/* Transmit remote request frame. */
FLEXCAN_TransferSendNonBlocking(EXAMPLE_FLEXCAN, &flexcanHandle, &txXfer);
/* Wait until transfer finished. */
while (kStatus_FLEXCAN_TxIdle != FLEXCAN_GetTransferStatusFlags(EXAMPLE_FLEXCAN, txXfer.mbIdx))
{
}
}
}
```
这个示例程序演示了如何使用S32K144-EVB开发板上CAN模块。在这个例子中,我们使用了两个邮箱,其中一个用于接收数据,另一个用于发送数据。在主函数中,我们使用了一个 while 循环,不断地等待接收数据,并且发送一条远程请求帧作为响应。可以根据自己的需求进行修改。
阅读全文