本人嵌入式小白一个,目前在弄一个OBD与汽车模拟器之间的通信。基于新唐给的BSP弄了一个交互的程序,发送数据OK,模拟器也有反应,正常情况下模拟器应该有反馈的数据可以接收的,可是我接收到的数据包是空的?不知道问题到底在哪里?希望有懂的大神帮我排解一下问题。我把主程序的代码传一下,大家帮我看看。谢谢。#include <stdio.h>
#include "NUC131.h"
#define PLL_CLOCK 48000000
/*---------------------------------------------------------------------------*/
/* Function Declare */
/*---------------------------------------------------------------------------*/
extern char GetChar(void);
void CAN_ShowMsg(STR_CANMSG_T* Msg);
/* Declare a CAN message structure */
STR_CANMSG_T tMsg; //transmit data message structure
STR_CANMSG_T rrMsg; //recieve data message structure
CAN_T *tCAN;
/*---------------------------------------------------------------------------------------------------------*/
/* ISR to handle CAN interrupt event */
/*---------------------------------------------------------------------------------------------------------*/
void CAN_MsgInterrupt(CAN_T *tCAN, uint32_t u32IIDR)
{
printf("\nThe recieved data is:\n");
if(u32IIDR == 1)
{
CAN_Receive(tCAN, 0, &rrMsg);
CAN_ShowMsg(&rrMsg);
}
if(u32IIDR == 5 + 1)
{
CAN_Receive(tCAN, 5, &rrMsg);
CAN_ShowMsg(&rrMsg);
}
if(u32IIDR == 31 + 1)
{
CAN_Receive(tCAN, 31, &rrMsg);
CAN_ShowMsg(&rrMsg);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* CAN0 interrupt handler */
/*---------------------------------------------------------------------------------------------------------*/
void CAN0_IRQHandler(void)
{
uint32_t u8IIDRstatus;
u8IIDRstatus = CAN0->IIDR;
if(u8IIDRstatus == 0x00008000) /* Check Status Interrupt Flag (Error status Int and Status change Int) */
{
/**************************/
/* Status Change interrupt*/
/**************************/
if(CAN0->STATUS & CAN_STATUS_RXOK_Msk)
{
CAN0->STATUS &= ~CAN_STATUS_RXOK_Msk; /* Clear RxOK status*/
}
if(CAN0->STATUS & CAN_STATUS_TXOK_Msk)
{
CAN0->STATUS &= ~CAN_STATUS_TXOK_Msk; /* Clear TxOK status*/
}
/**************************/
/* Error Status interrupt */
/**************************/
if(CAN0->STATUS & CAN_STATUS_BOFF_Msk)
{
printf("BOFF INT\n") ;
}
else if(CAN0->STATUS & CAN_STATUS_EWARN_Msk)
{
printf("EWARN INT\n") ;
}
else if((CAN0->ERR & CAN_ERR_TEC_Msk) != 0)
{
printf("Transmit error!\n") ;
}
else if((CAN0->ERR & CAN_ERR_REC_Msk) != 0)
{
printf("Receive error!\n") ;
}
}
else if((u8IIDRstatus >= 0x1) || (u8IIDRstatus <= 0x20))
{
CAN_MsgInterrupt(CAN0, u8IIDRstatus);
CAN_CLR_INT_PENDING_BIT(CAN0, (u8IIDRstatus - 1)); /* Clear Interrupt Pending */
}
else if(CAN0->WU_STATUS == 1)
{
printf("Wake up\n");
CAN0->WU_STATUS = 0; /* Write '0' to clear */
}
}
/*---------------------------------------------------------------------------*/
/* Show Message Function */
/*---------------------------------------------------------------------------*/
void CAN_ShowMsg(STR_CANMSG_T* Msg)
{
uint8_t i;
/* Show the message information */
printf("Read ID=0x%X, Type=%s, DLC=%d, Data=", Msg->Id, Msg->IdType ? "EXT" : "STD", Msg->DLC);
for(i = 0; i < Msg->DLC; i++)
printf("%X,", Msg->Data[i]);
printf("\n\n");
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable Internal RC 22.1184MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
/* Waiting for Internal RC clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
/* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
/* Enable external XTAL 12MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
/* Waiting for external XTAL clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
/* Set core clock as PLL_CLOCK from PLL */
CLK_SetCoreClock(PLL_CLOCK);
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable CAN module clock */
CLK_EnableModuleClock(CAN0_MODULE);
//CLK_EnableModuleClock(CAN1_MODULE);
/* Select UART module clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
SYS->GPB_MFP |= SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
/* Set PD multi-function pins for CANTX0, CANRX0 */
SYS->GPD_MFP &= ~(SYS_GPD_MFP_PD6_Msk | SYS_GPD_MFP_PD7_Msk);
SYS->GPD_MFP = SYS_GPD_MFP_PD6_CAN0_RXD | SYS_GPD_MFP_PD7_CAN0_TXD;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
void UART0_Init()
{
/* Reset IP */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
/*----------------------------------------------------------------------------*/
/* Check the real baud-rate */
/*----------------------------------------------------------------------------*/
void BaudRateCheck(uint32_t u32BaudRate, uint32_t u32RealBaudRate)
{
/* Get Core Clock Frequency */
SystemCoreClockUpdate();
if(u32BaudRate != u32RealBaudRate)
{
printf("\nSetting CAN baud-rate failed.\n");
printf("Real baud-rate value(bps): %d\n", u32RealBaudRate);
printf("CAN baud-rate calculation equation as below:\n");
printf("CAN baud-rate(bps) = Fin/(BPR+1)*(Tseg1+Tseg2+3)\n");
printf("where: Fin: System clock freq.(Hz)\n");
printf(" BRP: The baud rate prescale. It is composed of BRP (CAN_BTIME[5:0]) and BRPE (CAN_BRPE[3:0]).\n");
printf(" Tseg1: Time Segment before the sample point. You can set tseg1 (CAN_BTIME[11:8]).\n");
printf(" Tseg2: Time Segment after the sample point. You can set tseg2 (CAN_BTIME[14:12]).\n");
if(SystemCoreClock % u32BaudRate != 0)
printf("\nThe BPR does not calculate, the Fin must be a multiple of the CAN baud-rate.\n");
else
printf("\nThe BPR does not calculate, the (Fin/(CAN baud-rate)) must be a multiple of the (Tseg1+Tseg1+3).\n");
}
else
printf("\nReal baud-rate value(bps): %d\n", u32RealBaudRate);
}
/*----------------------------------------------------------------------------*/
/* Set the CAN speed */
/*----------------------------------------------------------------------------*/
void SetCANSpeed(CAN_T *tCAN)
{
uint32_t BaudRate = 0, RealBaudRate = 0;
BaudRate = 250000;
RealBaudRate = CAN_Open(tCAN, BaudRate, CAN_NORMAL_MODE);
/* Check the real baud-rate is OK */
BaudRateCheck(BaudRate, RealBaudRate);
}
//set the param of the corresponding mode
void Set_NormalMode_Tx(CAN_T *tCAN)
{
CAN_EnableInt(tCAN, CAN_CON_IE_Msk | CAN_CON_SIE_Msk); /* Enable CAN interrupt and corresponding NVIC of CAN */
NVIC_SetPriority(CAN0_IRQn, (1 << __NVIC_PRIO_BITS) - 2); /* Install CAN call back functions */
NVIC_EnableIRQ(CAN0_IRQn);
printf("Use Message Object No.0 to send STD_ID:0x7DF, Data[02,01]\n\n\n");
tMsg.FrameType = CAN_DATA_FRAME; //接收帧的类型
tMsg.IdType = CAN_STD_ID; //标准帧报文
tMsg.Id = 0x7DF; //目的地址
tMsg.DLC = 2; //发送报文字节数
tMsg.Data[0] = 0X02;
tMsg.Data[1] = 0x01;
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
tCAN = (CAN_T *) CAN0;
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, IP clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 for printf */
UART0_Init();
/* Configuring the Bit Timing */
SetCANSpeed(tCAN);
Set_NormalMode_Tx(tCAN);
while(1)
{
CLK_SysTickDelay(100000);
if(CAN_Transmit(tCAN, MSG(0), &tMsg)==TRUE)
printf("Transmit succeeded.\n");
else
printf("Transmit failed.\n");
if(CAN_SetRxMsg(tCAN, MSG(0), rrMsg.IdType, rrMsg.Id) == FALSE) //配置接收数据的报文对象
printf("Set Rx Msg Object failed\n");
}
}
|