初级会员
- 积分
- 95
- 金钱
- 95
- 注册时间
- 2018-8-8
- 在线时间
- 12 小时
|
楼主 |
发表于 2018-8-8 16:44:49
|
显示全部楼层
void flexcanInit(void)
{
flexcan_config_t flexcanConfig;
flexcan_rx_mb_config_t mbConfig;
CLOCK_SetMux(kCLOCK_CanMux, FLEXCAN_CLOCK_SOURCE_SELECT);
CLOCK_SetDiv(kCLOCK_CanDiv, FLEXCAN_CLOCK_SOURCE_DIVIDER);
/* Get FlexCAN module default Configuration. */
/*
* flexcanConfig.clkSrc = kFLEXCAN_ClkSrcOsc;
* flexcanConfig.baudRate = 1000000U;
* flexcanConfig.maxMbNum = 16;
* flexcanConfig.enableLoopBack = false;
* flexcanConfig.enableSelfWakeup = false;
* flexcanConfig.enableIndividMask = false;
* flexcanConfig.enableDoze = false;
*/
FLEXCAN_GetDefaultConfig(&flexcanConfig);
#if (!defined(FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE)) || !FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE
flexcanConfig.clkSrc = kFLEXCAN_ClkSrcPeri;
#endif /* FSL_FEATURE_FLEXCAN_SUPPORT_ENGINE_CLK_SEL_REMOVE */
flexcanConfig.baudRate=500000U;
flexcanConfig.enableLoopBack = true;
FLEXCAN_Init(CAN1, &flexcanConfig, CAN_CLK_FREQ);
FLEXCAN_TransferCreateHandle(CAN1, &flexcanHandle, flexcan_callback, NULL);
FLEXCAN_SetRxMbGlobalMask(CAN1, FLEXCAN_RX_MB_EXT_MASK(rxIdentifier, 0, 0));
mbConfig.format = kFLEXCAN_FrameFormatExtend;
mbConfig.type = kFLEXCAN_FrameTypeData;
mbConfig.id = FLEXCAN_ID_EXT(0x0ff46f00);
FLEXCAN_SetRxMbConfig(CAN1, RX_MESSAGE_BUFFER_NUM, &mbConfig, true);
PRINTF("Press any key to trigger one-shot transmission\r\n\r\n");
FLEXCAN_SetTxMbConfig(CAN1, TX_MESSAGE_BUFFER_NUM, true);
frame.id = FLEXCAN_ID_EXT(0x0ff46f00);
frame.format = kFLEXCAN_FrameFormatExtend;
frame.type = kFLEXCAN_FrameTypeData;
frame.length = 4;
frame.dataWord0 = CAN_WORD0_DATA_BYTE_0(0x11) | CAN_WORD0_DATA_BYTE_1(0x22) | CAN_WORD0_DATA_BYTE_2(0x33) |
CAN_WORD0_DATA_BYTE_3(0x44);
//frame.dataByte0=0x64;
//frame.dataByte1=0x00;
//frame.dataByte1=0x55;
txXfer.frame = &frame;
txXfer.mbIdx = TX_MESSAGE_BUFFER_NUM;
FLEXCAN_TransferSendNonBlocking(CAN1, &flexcanHandle, &txXfer);
if(tets==kStatus_Success)
PRINTF("HHH\r\n");
//PRINTF("TEST1\r\n\r\n");
while (!txComplete)
{
};
txComplete = false;
PRINTF("Start to Wait data from Node A\r\n\r\n");
rxXfer.mbIdx = RX_MESSAGE_BUFFER_NUM;
rxXfer.frame = &rxframe;
FLEXCAN_TransferReceiveNonBlocking(CAN1, &flexcanHandle, &rxXfer);
while (!rxComplete)
{
};
rxComplete = false;
PRINTF("Rx MB ID: 0x%x, Rx MB data: 0x%x\r\n", rxframe.id >> CAN_ID_STD_SHIFT, rxframe.dataWord0);
frame.id = FLEXCAN_ID_EXT(0x0ff46f00);
txXfer.mbIdx = TX_MESSAGE_BUFFER_NUM;
while (1)
{
__WFI();
}
}
static void flexcan_callback(CAN_Type *base, flexcan_handle_t *handle, status_t status, uint32_t result, void *userData)
{
PRINTF("TEST\r\n\r\n");
switch (status)
{
case kStatus_FLEXCAN_RxIdle:
if (RX_MESSAGE_BUFFER_NUM == result)
{
rxComplete = true;
}
break;
case kStatus_FLEXCAN_TxIdle:
if (TX_MESSAGE_BUFFER_NUM == result)
{
txComplete = true;
}
break;
default:
break;
}
} |
|