📄 drvcan.c
字号:
return (u8MsgObj < 16 ? CAN->u32TXREQ1 & (1 << u8MsgObj) : CAN->u32TXREQ2 & (1 << (u8MsgObj-16)));
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvCAN_IsIntPending */
/* */
/* Parameter: */
/* u8MsgObj: specifies the Message object number, from 0 to 31. */
/* Returns: */
/* A non-zero value if the corresponding message has an interrupt pending, else 0. */
/* Description: */
/* This function is used to get the interrupt status of a message object */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvCAN_IsIntPending(uint8_t u8MsgObj)
{
return (u8MsgObj < 16 ? CAN->u32IPND1 & (1 << u8MsgObj) : CAN->u32IPND2 & (1 << (u8MsgObj-16)));
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvCAN_IsObjectValid */
/* */
/* Parameter: */
/* u8MsgObj: specifies the Message object number, from 0 to 31. */
/* Returns: */
/* A non-zero value if the corresponding message object is valid, else 0. */
/* Description: */
/* This function is used to test the validity of a message object (ready to use). */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvCAN_IsObjectValid(uint8_t u8MsgObj)
{
return (u8MsgObj < 16 ? CAN->u32MVLD1 & (1 << u8MsgObj) : CAN->u32MVLD2 & (1 << (u8MsgObj-16)));
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvCAN_ResetIF */
/* */
/* Parameter: */
/* u8IF_Num: specifies the message interface, 0 or 1. */
/* Returns: */
/* None. */
/* Description: */
/* This function is used to reset message interface parameters. */
/*---------------------------------------------------------------------------------------------------------*/
void DrvCAN_ResetIF(uint8_t u8IF_Num)
{
if(u8IF_Num > 1)
return;
CAN->sMsgObj[u8IF_Num].u32CREQ = 0x0; // set bit15 for sending
CAN->sMsgObj[u8IF_Num].u32CMASK = 0x0;
CAN->sMsgObj[u8IF_Num].u32MASK1 = 0x0; // useless in basic mode
CAN->sMsgObj[u8IF_Num].u32MASK2 = 0x0; // useless in basic mode
CAN->sMsgObj[u8IF_Num].u32ARB1 = 0x0; // ID15~0
CAN->sMsgObj[u8IF_Num].u32ARB2 = 0x0; // MsgVal, eXt, xmt, ID28~16
CAN->sMsgObj[u8IF_Num].u32MCON = 0x0; // DLC
CAN->sMsgObj[u8IF_Num].u32DAT_A1 = 0x0; // data0,1
CAN->sMsgObj[u8IF_Num].u32DAT_A2 = 0x0; // data2,3
CAN->sMsgObj[u8IF_Num].u32DAT_B1 = 0x0; // data4,5
CAN->sMsgObj[u8IF_Num].u32DAT_B2 = 0x0; // data6,7
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvCAN_WaitMsg */
/* */
/* Parameter: */
/* None */
/* Returns: */
/* None */
/* Description: */
/* This function is used to wait message into message buffer in basic mode. Please notice the */
/* function is polling NEWDAT bit of MCON register by while loop and it is used in basic mode.*/
/*---------------------------------------------------------------------------------------------------------*/
void DrvCAN_WaitMsg(void)
{
CAN->u32STATUS = 0x0; /* clr status */
while (1)
{
if ( CAN->sMsgObj[1].MCON.NEWDAT == 1 ) /* check new data */
{
DEBUG_PRINTF("New Data IN\n");
break;
}
if ( CAN->STATUS.RXOK == 1)
DEBUG_PRINTF("Rx OK\n");
if ( CAN->STATUS.LEC)
{
DEBUG_PRINTF("Error\n");
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvCAN_EnableInt */
/* */
/* Parameters: */
/* u16IntEnable - Interrupt Enable (CAN_CON_IE or CAN_CON_SIE or CAN_CON_EIE) */
/* CAN_CON_IE : Module Interrupt Enable */
/* CAN_CON_SIE : Status Change Interrupt Enable */
/* CAN_CON_EIE : Error Interrupt Enable */
/* Returns: */
/* E_SUCCESS : Success */
/* */
/* Description: */
/* Enable CAN interrupt and corresponding NVIC of CAN */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvCAN_EnableInt(uint16_t u16IntEnable)
{
DrvCAN_EnterInitMode();
CAN->u32CON = (CAN->u32CON & 0xF1) | ((u16IntEnable & CAN_CON_IE )? CAN_CON_IE :0)
| ((u16IntEnable & CAN_CON_SIE )? CAN_CON_SIE:0)
| ((u16IntEnable & CAN_CON_EIE )? CAN_CON_EIE:0);
NVIC_SetPriority(CAN0_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
NVIC_EnableIRQ(CAN0_IRQn);
DrvCAN_LeaveInitMode();
return E_SUCCESS;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvCAN_DisableInt */
/* */
/* Parameters: */
/* u16IntEnable - Interrupt Enable (CAN_CON_IE or CAN_CON_SIE or CAN_CON_EIE) */
/* CAN_CON_IE : Module Interrupt Enable */
/* CAN_CON_SIE : Status Change Interrupt Enable */
/* CAN_CON_EIE : Error Interrupt Enable */
/* */
/* Returns: */
/* E_SUCCESS : Success */
/* */
/* Description: */
/* Disable CAN interrupt and corresponding NVIC of CAN */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvCAN_DisableInt(uint16_t u16IntEnable)
{
DrvCAN_EnterInitMode();
CAN->u32CON = CAN->u32CON & ~(CAN_CON_IE | ((u16IntEnable & CAN_CON_SIE )?CAN_CON_SIE:0)
| ((u16IntEnable & CAN_CON_EIE )?CAN_CON_EIE:0));
NVIC_DisableIRQ(CAN0_IRQn);
DrvCAN_LeaveInitMode();
return E_SUCCESS;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvCAN_InstallCallback */
/* */
/* Parameters: */
/* Type - [in] It could be CALLBACK_RXOK / CALLBACK_TXOK / CALLBACK_EWARN */
/* CALLBACK_BOFF / CALLBACK_MSG / CALLBACK_WAKEUP. */
/* */
/* CALLBACK_RXOK: Received a Message Successfully callback fn. */
/* CALLBACK_TXOK: Transmitted a Message Successfully callback fn. */
/* CALLBACK_EWARN:Error Warning Status callback fn. */
/* CALLBACK_BOFF: Bus-off Status callback fn. */
/* CALLBACK_MSG:Target message object transmitted/received callback fn.*/
/* CALLBACK_WAKEUP: Wakeup callback fn. */
/* */
/* callbackfn - [in] Call back function pointer */
/* Returns: */
/* E_SUCCESS Success */
/* E_DRVCAN_ERR_ARGUMENT Failed */
/* */
/* Description: */
/* Install CAN call back function for CAN normal function */
/* MSG,RXOK,TXOK,EWARN,BOFF,WAKEUP */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvCAN_InstallCallback(E_CAN_CALLBACK_TYPE Type, CAN_CALLBACK callbackfn)
{
switch(Type)
{
case CALLBACK_MSG:
CANHandler.MessageCallBackFn = callbackfn;
break;
case CALLBACK_RXOK:
CANHandler.RxOkCallBackFn = callbackfn;
break;
case CALLBACK_TXOK:
CANHandler.TxOkCallBackFn = callbackfn;
break;
case CALLBACK_EWARN:
CANHandler.ErrorWarningCallBackFn = callbackfn;
break;
case CALLBACK_BOFF:
CANHandler.BusOffCallBackFn = callbackfn;
break;
case CALLBACK_WAKEUP:
CANHandler.WakeupCallBackFn = callbackfn;
break;
default:
return E_DRVCAN_ERR_ARGUMENT;
}
return E_SUCCESS;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvCAN_UninstallCallback */
/* */
/* Parameters: */
/* Type - [in] It could be CALLBACK_RXOK / CALLBACK_TXOK / CALLBACK_EWARN */
/* CALLBACK_BOFF / CALLBACK_MSG / CALLBACK_WAKEUP. */
/* */
/* CALLBACK_RXOK: Received a Message Successfully callback fn. */
/* CALLBACK_TXOK: Transmitted a Message Successfully callback fn. */
/* CALLBACK_EWARN:Error Warning Status callback fn. */
/* CALLBACK_BOFF: Bus-off Status callback fn. */
/* CALLBACK_MSG:Target message object transmitted/received callback fn.*/
/* CALLBACK_WAKEUP: Wakeup callback fn. */
/* */
/* Returns:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -