status_t CAN001_ReadMsgObj ( const CAN001_HandleType* Handle, CAN001_MessageHandleType* SwMsgObjptr, uint8_t MsgObjnr ) { uint32_t Error = (uint32_t)CAN001_ERROR; uint32_t Count = 0U; bool RxPnd = 0U; bool NewData = 0U; uint8_t MsgNo = (uint8_t)(Handle->FirstMOMapping+(MsgObjnr-1U)); /* Mapping to message object offset value*/ CAN_MO_TypeDef* CAN_MOxRegs = \ GET_MO_OFFSET(MsgNo); DBG002_N((MsgObjnr == 0U)||(MsgObjnr > Handle->NodeMONo)); /* check if message object is a receive message object */ /*<<<DD_CAN001_API_6_1>>>*/ if( RD_REG(CAN_MOxRegs->MOSTAT, CAN_MO_MOSTAT_DIR_Msk, \ CAN_MO_MOSTAT_DIR_Pos) != (uint32_t)RECMSGOBJ) { Error = (uint32_t)CAN001_MO_NOT_ACCEPTABLE; DBG002_ERROR(APP_GID, DBG002_MESSAGEID_LITERAL, sizeof(Error), &Error); } /* check if reception is ongoing on message object */ /*<<<DD_CAN001_API_6_2>>>*/ else if( RD_REG(CAN_MOxRegs->MOSTAT, CAN_MO_MOSTAT_RXUPD_Msk, \ CAN_MO_MOSTAT_RXUPD_Pos) == 1U) { Error = (uint32_t)CAN001_MO_BUSY; DBG002_ERROR(APP_GID, DBG002_MESSAGEID_LITERAL, sizeof(Error), &Error); } else { /* read message parameters */ /*<<<DD_CAN001_API_6_3>>>*/ do { CAN_MOxRegs->MOCTR = CAN_MO_MOCTR_RESNEWDAT_Msk; if((RD_REG(CAN_MOxRegs->MOAR, CAN_MO_MOAR_IDE_Msk , \ CAN_MO_MOAR_IDE_Pos)) == 0U) { SwMsgObjptr->IDExten = (uint8_t)STANDARDTYPE; SwMsgObjptr->Identifier = (CAN_MOxRegs->MOAR & (uint32_t)CAN_MO_MOAR_STDID_Msk) >> \ CAN_MO_MOAR_STDID_Pos; SwMsgObjptr->IDEMask = (uint8_t)((uint32_t)(CAN_MOxRegs->MOAMR & (uint32_t)CAN_MO_MOAMR_MIDE_Msk) >> \ CAN_MO_MOAMR_MIDE_Pos); if(SwMsgObjptr->IDEMask == 1U) { SwMsgObjptr->IDMask = (CAN_MOxRegs->MOAMR & (uint32_t)CAN_MO_MOAR_STDID_Msk) >> \ CAN_MO_MOAR_STDID_Pos; } else { SwMsgObjptr->IDMask = CAN_MOxRegs->MOAMR & (uint32_t)CAN_MO_MOAMR_AM_Msk; } } else {
/* This function will initialize node with the given handle */ static void CAN001_lNodeInit(const CAN001_HandleType* Handle) { uint32_t Count = 0U; uint8_t MsgNo; CAN_MO_TypeDef* CAN_MOxRegs = CAN_MO0; /* <<<DD_CAN001_nonAPI_4>>> */ /* Map to node register offset as per node ID */ CAN_NODE_TypeDef* CAN_NodexRegs = Handle->CanNodeRegs; DBG002_FUNCTION_ENTRY(DBG002_GID_CAN001,CAN001_FUNCTION_ENTRY); /*<<<DD_CAN001_API_1>>>*/ /* wait until panel has finished initialization */ while ( (CAN->PANCTR & CAN_PANCTR_BUSY_Msk)) {} /* set CCE and INIT bit NCR for node configuration */ /* Enable Alert and last error code interrupt */ CAN_NodexRegs->NCR |= ((uint32_t)CAN_NODE_NCR_INIT_Msk | (uint32_t)CAN_NODE_NCR_CCE_Msk); /* Configure bit timing register */ CAN_NodexRegs->NBTR = ((uint32_t)Handle->BaudRate.DIV8 << \ CAN_NODE_NBTR_DIV8_Pos) | \ ((uint32_t)Handle->BaudRate.TimeSEG2 << \ CAN_NODE_NBTR_TSEG2_Pos) | \ ((uint32_t)Handle->BaudRate.TimeSEG1 << \ CAN_NODE_NBTR_TSEG1_Pos) | \ ((uint32_t)Handle->BaudRate.SyncJumpWidth << \ CAN_NODE_NBTR_SJW_Pos) | \ (uint32_t)Handle->BaudRate.BaudRatePresc; /* Check whether loop back mode is to be enabled */ if ( Handle->LoopBackModeEn == CAN001_ENABLE) { SET_BIT(CAN_NodexRegs->NPCR, CAN_NODE_NPCR_LBM_Pos); } /* Allocate required number of message object to node list * and configure message object */ for(Count = 0U; Count < Handle->NodeMONo; Count++) { CAN001_lAllocateMOtoNodeList(Handle->NodeID, (uint8_t)(Handle->FirstMOMapping + Count)); MsgNo = (uint8_t)(Handle->FirstMOMapping + Count); CAN_MOxRegs = GET_MO_OFFSET(MsgNo); /* Configure MPN */ CAN_MOxRegs->MOIPR = (((uint32_t)Handle->NodeID << \ (CAN_MO_MOIPR_MPN_Pos + 5)) | \ (Count << CAN_MO_MOIPR_MPN_Pos)); } DBG002_FUNCTION_EXIT(DBG002_GID_CAN001,CAN001_FUNCTION_EXIT); }
status_t CAN001_SendRemoteFrame(const CAN001_HandleType* Handle, uint8_t MsgObjnr) { uint32_t Error = 0U; uint8_t MsgNo = (uint8_t)(Handle->FirstMOMapping+(MsgObjnr-1U)); /* Mapping to message object offset value*/ CAN_MO_TypeDef* CAN_MOxRegs = \ GET_MO_OFFSET(MsgNo); /*<<<DD_CAN001_API_5>>>*/ DBG002_FUNCTION_ENTRY(DBG002_GID_CAN001,CAN001_FUNCTION_ENTRY); DBG002_N ((MsgObjnr == 0U)||(MsgObjnr > Handle->NodeMONo)); /* check if message object is a receive message object */ /*<<<DD_CAN001_API_5_1>>>*/ if( RD_REG(CAN_MOxRegs->MOSTAT, CAN_MO_MOSTAT_DIR_Msk, \ CAN_MO_MOSTAT_DIR_Pos) != (uint32_t)RECMSGOBJ) { Error = (uint32_t)CAN001_MO_NOT_ACCEPTABLE; DBG002_ERROR(DBG002_GID_CAN001,Error, 0, NULL); } /* check if message is disabled */ else if( RD_REG(CAN_MOxRegs->MOSTAT, CAN_MO_MOSTAT_MSGVAL_Msk, \ CAN_MO_MOSTAT_MSGVAL_Pos) == 0U) { Error = (uint32_t)CAN001_MSGOBJ_DISABLED; DBG002_INFO(DBG002_GID_CAN001,Error, 0, NULL); } /* check if transmission is ongoing on message object */ /*<<<DD_CAN001_API_5_2>>>*/ else if( RD_REG(CAN_MOxRegs->MOSTAT, CAN_MO_MOSTAT_TXRQ_Msk, \ CAN_MO_MOSTAT_TXRQ_Pos) == 1U) { Error = (uint32_t)CAN001_MO_BUSY; DBG002_INFO(DBG002_GID_CAN001,Error, 0, NULL); } else { /* Put transmit request to message object */ /*<<<DD_CAN001_API_5_3>>>*/ CAN_MOxRegs->MOCTR = CAN_MO_MOCTR_SETTXRQ_Msk; Error = (uint32_t)DAVEApp_SUCCESS; } DBG002_FUNCTION_EXIT(DBG002_GID_CAN001,CAN001_FUNCTION_EXIT); return Error; }
status_t CAN001_SendDataFrame(const CAN001_HandleType* Handle, uint8_t MsgObjnr) { uint32_t Error = (uint32_t)CAN001_ERROR; uint8_t MsgNo = (uint8_t)(Handle->FirstMOMapping+(MsgObjnr-1U)); /* Mapping to message object offset value*/ CAN_MO_TypeDef* CAN_MOxRegs = \ GET_MO_OFFSET(MsgNo); DBG002_N((MsgObjnr == 0U)||(MsgObjnr > Handle->NodeMONo)); /* check if message object is not a transmit message object */ /*<<<DD_CAN001_API_4_1>>>*/ if( RD_REG(CAN_MOxRegs->MOSTAT, CAN_MO_MOSTAT_DIR_Msk, \ CAN_MO_MOSTAT_DIR_Pos) != (uint32_t)TRANSMSGOBJ) { Error = (uint32_t)CAN001_MO_NOT_ACCEPTABLE; DBG002_ERROR(APP_GID, DBG002_MESSAGEID_LITERAL, sizeof(Error), &Error); } /* check if message is disabled */ else if( RD_REG(CAN_MOxRegs->MOSTAT, CAN_MO_MOSTAT_MSGVAL_Msk, \ CAN_MO_MOSTAT_MSGVAL_Pos) == 0U) { Error = (uint32_t)CAN001_MSGOBJ_DISABLED; DBG002_INFO(APP_GID, DBG002_MESSAGEID_LITERAL, sizeof(Error), &Error); } /* check if transmission is ongoing on message object */ /*<<<DD_CAN001_API_4_2>>>*/ else if( RD_REG(CAN_MOxRegs->MOSTAT, CAN_MO_MOSTAT_TXRQ_Msk, \ CAN_MO_MOSTAT_TXRQ_Pos) == 1U) { Error = (uint32_t)CAN001_MO_BUSY; DBG002_INFO(APP_GID, DBG002_MESSAGEID_LITERAL, sizeof(Error), &Error); } else { /* set TXRQ bit */ /*<<<DD_CAN001_API_4_3>>>*/ CAN_MOxRegs->MOCTR = CAN_MO_MOCTR_SETTXRQ_Msk; Error = (uint32_t)DAVEApp_SUCCESS; } return Error; }
status_t CAN001_ConfigMsgObj ( const CAN001_HandleType* Handle, const CAN001_MessageHandleType* SwMsgObjptr, uint8_t MsgObjnr ) { uint32_t Error = (uint32_t)CAN001_MO_NOT_FOUND; uint8_t MsgNo = (uint8_t)(Handle->FirstMOMapping+(MsgObjnr-1U)); /* Mapping to message object offset value*/ CAN_MO_TypeDef* CAN_MOxRegs = \ GET_MO_OFFSET(MsgNo); /* <<<DD_CAN001_API_3>>> */ DBG002_FUNCTION_ENTRY(DBG002_GID_CAN001,CAN001_FUNCTION_ENTRY); DBG002_N((MsgObjnr == 0U)||(MsgObjnr > Handle->NodeMONo)); if (((SwMsgObjptr->IDExten != (uint8_t)STANDARDTYPE) && \ (SwMsgObjptr->IDExten != (uint8_t)EXTENDEDTYPE)) ||((SwMsgObjptr->MsgObjEN != CAN001_ENABLE) && (SwMsgObjptr->MsgObjEN != CAN001_DISABLE)) ||((SwMsgObjptr->MsgObjType != RECMSGOBJ) && (SwMsgObjptr->MsgObjType != TRANSMSGOBJ))) { Error = (uint32_t)CAN001_INVALID_INPUT; ERROR(DBG002_GID_CAN001,Error, 0, NULL); } /* check if message object is to be disabled */ /*<<<DD_CAN001_API_3_1>>>*/ else if (SwMsgObjptr->MsgObjEN == CAN001_DISABLE) { /* Reset MSGVAL bit */ CAN_MOxRegs->MOCTR = CAN_MO_MOCTR_RESMSGVAL_Msk; Error = (uint32_t)DAVEApp_SUCCESS; } /* if (SwMsgObjptr->MsgObjEN == CAN001_DISABLE) */ else { CAN001_lConfigMORegs(SwMsgObjptr, (uint8_t)(Handle->FirstMOMapping+(MsgObjnr-1U))); Error = (uint32_t)DAVEApp_SUCCESS; } /*if (SwMsgObjptr->MsgObjEN == CAN001_ENABLE)*/ DBG002_FUNCTION_EXIT(DBG002_GID_CAN001,CAN001_FUNCTION_EXIT); return Error; }
/*<<<DD_CAN001_API_2>>>*/ void CAN001_NodeDeInit(const CAN001_HandleType* Handle) { int32_t Count = 0; uint8_t MsgNo; uint32_t* MsgPndRegOffset = 0; CAN_MO_TypeDef* CAN_MOxRegs = CAN_MO0; /* Map to node register offser as per node ID */ CAN_NODE_TypeDef* CAN_NodexRegs = Handle->CanNodeRegs; /* Reset all message object registers and deallocate to free list */ for(Count = (int32_t)(Handle->NodeMONo)-1; Count >= 0; Count--) { MsgNo = (uint8_t)(Handle->FirstMOMapping+(uint32_t)Count); /* Mapping to message object offset value*/ CAN_MOxRegs = GET_MO_OFFSET(MsgNo); /* Reset all message objects registers */ CAN_MOxRegs->MOFCR = (0x00000000U); CAN_MOxRegs->MOIPR &= (0x000000FFU); CAN_MOxRegs->MOAMR = (0x3FFFFFFFU); CAN_MOxRegs->MODATAL = (0x00000000U); CAN_MOxRegs->MODATAH = (0x00000000U); CAN_MOxRegs->MOAR = (0x00000000U); CAN_MOxRegs->MOCTR = (0x00000FFFU); CAN001_lDeallocateMsgObj(Handle->FirstMOMapping+(uint32_t)Count); } /* set CCE and INIT bit NCR for disconnecting the node */ CAN_NodexRegs->NCR = (0x00000041U); /* reset all node registers */ CAN_NodexRegs->NSR = (0x00000000U); CAN_NodexRegs->NPCR &= (0x0000000FU); CAN_NodexRegs->NECNT = (0x00600000U); CAN_NodexRegs->NFCR = (0x00000000U); CAN_NodexRegs->NBTR = (0x00000000U); MsgPndRegOffset = GET_MSGPND_OFFSET(Handle->NodeID); (*MsgPndRegOffset) = (0x00000000U); }
/* Function to configure message object registers */ static void CAN001_lConfigMORegs(const CAN001_MessageHandleType* MsgObjptr, uint8_t MsgObjnr) { CAN_MO_TypeDef* CAN_MOxRegs = GET_MO_OFFSET(MsgObjnr); uint32_t Count = 0U; /* <<<DD_CAN001_nonAPI_3>>> */ /* Disable Message object */ CAN_MOxRegs->MOCTR = CAN_MO_MOCTR_RESMSGVAL_Msk; if ((CAN_FrameType)MsgObjptr->IDExten == STANDARDTYPE) { /* Configure standard identifier and identifier mask*/ CAN_MOxRegs->MOAR = ((((uint32_t)2UL << CAN_MO_MOAR_PRI_Pos) & \ (uint32_t)CAN_MO_MOAR_PRI_Msk) | \ ((uint32_t)(MsgObjptr->Identifier << CAN_MO_MOAR_STDID_Pos) \ & (uint32_t)CAN_MO_MOAR_STDID_Msk)); if(MsgObjptr->IDEMask == 1U) { CAN_MOxRegs->MOAMR = ((((uint32_t)1UL << CAN_MO_MOAMR_MIDE_Pos) & \ (uint32_t)CAN_MO_MOAMR_MIDE_Msk) | \ ((uint32_t)(MsgObjptr->IDMask << CAN_MO_MOAR_STDID_Pos) \ & (uint32_t)CAN_MO_MOAR_STDID_Msk)); } else { CAN_MOxRegs->MOAMR = ((((uint32_t)0UL << CAN_MO_MOAMR_MIDE_Pos) & \ (uint32_t)CAN_MO_MOAMR_MIDE_Msk) | \ ((uint32_t)CAN_MO_MOAMR_AM_Msk & (uint32_t)MsgObjptr->IDMask)); } } /* if (MsgObjptr->IDExten == STANDARDTYPE) */ else { /* Configure Extended identifier and identifier mask*/ CAN_MOxRegs->MOAR = ((((uint32_t)2UL << CAN_MO_MOAR_PRI_Pos) & \ (uint32_t)CAN_MO_MOAR_PRI_Msk) | \ ((uint32_t)(CAN_MO_MOAR_ID_Msk & MsgObjptr->Identifier)) | \ (((uint32_t)1UL << CAN_MO_MOAR_IDE_Pos ) & \ (uint32_t)CAN_MO_MOAR_IDE_Msk)); CAN_MOxRegs->MOAMR = (((uint32_t)((uint32_t)MsgObjptr->IDEMask << CAN_MO_MOAMR_MIDE_Pos) & \ (uint32_t)CAN_MO_MOAMR_MIDE_Msk) | \ (uint32_t)(CAN_MO_MOAMR_AM_Msk & MsgObjptr->IDMask)); } /* Check whether message object is transmit message object */ /*<<<DD_CAN001_API_3_2>>>*/ if (MsgObjptr->MsgObjType == TRANSMSGOBJ) { /* Configure data length */ WR_REG(CAN_MOxRegs->MOFCR, (uint32_t)CAN_MO_MOFCR_DLC_Msk, \ CAN_MO_MOFCR_DLC_Pos, (uint32_t)MsgObjptr->DataLength); /* Configure Data registers*/ for(Count = 0U; Count < MsgObjptr->DataLength; Count++) { if(Count < 4U) { WR_REG(CAN_MOxRegs->MODATAL, (uint32_t)((uint32_t)CAN_MO_MODATAL_DB0_Msk << (Count*8U)), \ (Count*8U),(uint32_t)MsgObjptr->data[Count]); } else { WR_REG(CAN_MOxRegs->MODATAH, (uint32_t)(CAN_MO_MODATAL_DB0_Msk << ((Count-4U)*8U)), \ ((Count-4U)*8U), (uint32_t)MsgObjptr->data[Count]); } /* if(Count < 4) */ } /* for(Count = 0; Count < MsgObjptr->DataLength; Count++) */ /* Set MO as Transmit message object and set NEWDAT bit */ CAN_MOxRegs->MOCTR = (CAN_MO_MOCTR_SETDIR_Msk | \ CAN_MO_MOCTR_SETNEWDAT_Msk); }/*if (MsgType == TRANSMSGOBJ)*/ /*<<<DD_CAN001_API_3_3>>>*/ else { /* Set MO as Receive message object and set RXEN bit */ CAN_MOxRegs->MOCTR = CAN_MO_MOCTR_RESDIR_Msk; } /*if(MsgType == RECMSGOBJ)*/ /* Reset RTSEL and Set MSGVAL ,TXEN0 and TXEN1 bits */ CAN_MOxRegs->MOCTR = (CAN_MO_MOCTR_SETTXEN0_Msk | \ CAN_MO_MOCTR_SETTXEN1_Msk | \ CAN_MO_MOCTR_SETRXEN_Msk | \ CAN_MO_MOCTR_SETMSGVAL_Msk ); }