XilinxProcessorIPLib/drivers/can/src/xcan.c
void XCan_EnterMode(XCan *InstancePtr, u8 OperationMode)
{
	u8 CurrentMode;

	Xil_AssertVoid(InstancePtr != NULL);
	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertVoid((OperationMode == XCAN_MODE_CONFIG) ||
			(OperationMode == XCAN_MODE_SLEEP) ||
			(OperationMode == XCAN_MODE_NORMAL) ||
			(OperationMode == XCAN_MODE_LOOPBACK));

	/* Get current mode */

	CurrentMode = XCan_GetMode(InstancePtr);

	/*
	 * If current mode is Normal Mode and the mode to enter is Sleep Mode,
	 * or if current mode is Sleep Mode and the mode to enter is Normal
	 * Mode, no transition through Configuration Mode is needed.
	 */
	if ((CurrentMode == XCAN_MODE_NORMAL) &&
		(OperationMode == XCAN_MODE_SLEEP)) {

		/*
		 * Normal Mode ---> Sleep Mode
		 */
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET,
				XCAN_MSR_SLEEP_MASK);

		/*
		 * Mode transition is finished in this case and return to the
		 * caller
		 */
		return;
	}

	else if ((CurrentMode == XCAN_MODE_SLEEP) &&
		 (OperationMode == XCAN_MODE_NORMAL)) {
		/* Sleep Mode ---> Normal Mode */
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET, 0);

		/*
		 * Mode transition is finished in this case and return to the
		 * caller
		 */
		return;
	}


	/*
	 * If the mode transition is not any of the two cases above, CAN must
	 * enter Configuration Mode before switching into the target operation
	 * mode.
	 */
	XCan_WriteReg(InstancePtr->BaseAddress, XCAN_SRR_OFFSET, 0);

	/*
	 * Check if the device has entered Configuration Mode, if not, return to
	 * the caller.
	 */
	if (XCan_GetMode(InstancePtr) != XCAN_MODE_CONFIG) {
		return;
	}

	switch (OperationMode) {
	case XCAN_MODE_CONFIG:	/* Configuration Mode */

		/*
		 * As CAN is in Configuration Mode already.
		 * Nothing is needed to be done here
		 */

		break;

	case XCAN_MODE_SLEEP:	/* Sleep Mode */

		/* Switch the device into Sleep Mode */
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET,
				XCAN_MSR_SLEEP_MASK);
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_SRR_OFFSET,
				XCAN_SRR_CEN_MASK);

		break;

	case XCAN_MODE_NORMAL:	/* Normal Mode */

		/* Switch the device into Normal Mode */
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET, 0);
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_SRR_OFFSET,
				XCAN_SRR_CEN_MASK);

		break;

	case XCAN_MODE_LOOPBACK:	/* Loop back Mode */

		/* Switch the device into Loop back Mode */
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET,
				XCAN_MSR_LBACK_MASK);
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_SRR_OFFSET,
				XCAN_SRR_CEN_MASK);

		break;
	}
}
/*****************************************************************************/
/**
*
* This function allows the CAN device to enter one of the following operation
* modes:
*
* - Configuration Mode: Pass in parameter XCAN_MODE_CONFIG
* - Sleep Mode: Pass in parameter XCAN_MODE_SLEEP
* - Normal Mode: Pass in parameter XCAN_MODE_NORMAL
* - Loop Back Mode: Pass in parameter XCAN_MODE_LOOPBACK.
*
* Read xcan.h and device specification for detailed description of each
* operation mode.
*
* @param	InstancePtr is a pointer to the XCan instance to be worked on.
* @param	OperationMode specify which operation mode to enter. Valid value
*		is any of XCAN_MODE_* defined in xcan.h. Please note no multiple
* 		modes could be entered at the same time.
*
* @return	None.
*
* @note
*
* This function does NOT ensure CAN device enters the specified operation mode
* before returns the control to the caller. The caller is responsible for
* checking current operation mode using XCan_GetMode().
*
******************************************************************************/
This function allows the CAN device to enter one of the following operation
modes.

Configuration Mode: Pass in parameter XCAN_MODE_CONFIG
Sleep Mode: Pass in parameter XCAN_MODE_SLEEP
Normal Mode: Pass in parameter XCAN_MODE_NORMAL
Loop Back Mode: Pass in parameter XCAN_MODE_LOOPBACK.

Read xcan.h and device specification for detailed description of each
operation mode.
This function allows the CAN device to enter one of the following operation modes . Configuration Mode : Pass in parameter XCAN_MODE_CONFIG Sleep Mode : Pass in parameter XCAN_MODE_SLEEP Normal Mode : Pass in parameter XCAN_MODE_NORMAL Loop Back Mode : Pass in parameter XCAN_MODE_LOOPBACK . Read xcan . h and device specification for detailed description of each operation mode .
void XCan_EnterMode(XCan *InstancePtr, u8 OperationMode)
{
	u8 CurrentMode;
	Xil_AssertVoid(InstancePtr != NULL);
	Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
	Xil_AssertVoid((OperationMode == XCAN_MODE_CONFIG) ||
			(OperationMode == XCAN_MODE_SLEEP) ||
			(OperationMode == XCAN_MODE_NORMAL) ||
			(OperationMode == XCAN_MODE_LOOPBACK));
	CurrentMode = XCan_GetMode(InstancePtr);
	if ((CurrentMode == XCAN_MODE_NORMAL) &&
		(OperationMode == XCAN_MODE_SLEEP)) {
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET,
				XCAN_MSR_SLEEP_MASK);
		return;
	}
	else if ((CurrentMode == XCAN_MODE_SLEEP) &&
		 (OperationMode == XCAN_MODE_NORMAL)) {
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET, 0);
		return;
	}
	XCan_WriteReg(InstancePtr->BaseAddress, XCAN_SRR_OFFSET, 0);
	if (XCan_GetMode(InstancePtr) != XCAN_MODE_CONFIG) {
		return;
	}
	switch (OperationMode) {
	case XCAN_MODE_CONFIG:	
		break;
	case XCAN_MODE_SLEEP:	
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET,
				XCAN_MSR_SLEEP_MASK);
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_SRR_OFFSET,
				XCAN_SRR_CEN_MASK);
		break;
	case XCAN_MODE_NORMAL:	
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET, 0);
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_SRR_OFFSET,
				XCAN_SRR_CEN_MASK);
		break;
	case XCAN_MODE_LOOPBACK:	
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_MSR_OFFSET,
				XCAN_MSR_LBACK_MASK);
		XCan_WriteReg(InstancePtr->BaseAddress, XCAN_SRR_OFFSET,
				XCAN_SRR_CEN_MASK);
		break;
	}
}
void XCan_EnterMode ( XCan * InstancePtr , u8 OperationMode ) { u8 CurrentMode ; Xil_AssertVoid ( InstancePtr != NULL ) ; Xil_AssertVoid ( InstancePtr -> IsReady == XIL_COMPONENT_IS_READY ) ; Xil_AssertVoid ( ( OperationMode == XCAN_MODE_CONFIG ) || ( OperationMode == XCAN_MODE_SLEEP ) || ( OperationMode == XCAN_MODE_NORMAL ) || ( OperationMode == XCAN_MODE_LOOPBACK ) ) ; CurrentMode = XCan_GetMode ( InstancePtr ) ; if ( ( CurrentMode == XCAN_MODE_NORMAL ) && ( OperationMode == XCAN_MODE_SLEEP ) ) { XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_MSR_OFFSET , XCAN_MSR_SLEEP_MASK ) ; return ; } else if ( ( CurrentMode == XCAN_MODE_SLEEP ) && ( OperationMode == XCAN_MODE_NORMAL ) ) { XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_MSR_OFFSET , 0 ) ; return ; } XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_SRR_OFFSET , 0 ) ; if ( XCan_GetMode ( InstancePtr ) != XCAN_MODE_CONFIG ) { return ; } switch ( OperationMode ) { case XCAN_MODE_CONFIG : break ; case XCAN_MODE_SLEEP : XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_MSR_OFFSET , XCAN_MSR_SLEEP_MASK ) ; XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_SRR_OFFSET , XCAN_SRR_CEN_MASK ) ; break ; case XCAN_MODE_NORMAL : XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_MSR_OFFSET , 0 ) ; XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_SRR_OFFSET , XCAN_SRR_CEN_MASK ) ; break ; case XCAN_MODE_LOOPBACK : XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_MSR_OFFSET , XCAN_MSR_LBACK_MASK ) ; XCan_WriteReg ( InstancePtr -> BaseAddress , XCAN_SRR_OFFSET , XCAN_SRR_CEN_MASK ) ; break ; } }
This function allows the CAN device to enter one of the following operation
modes:
This function allows the CAN device to enter one of the following operation modes :
/* Get current mode */ /*
	 * If current mode is Normal Mode and the mode to enter is Sleep Mode,
	 * or if current mode is Sleep Mode and the mode to enter is Normal
	 * Mode, no transition through Configuration Mode is needed.
	 */ /*
		 * Normal Mode ---> Sleep Mode
		 */ /*
		 * Mode transition is finished in this case and return to the
		 * caller
		 */ /* Sleep Mode ---> Normal Mode */ /*
		 * Mode transition is finished in this case and return to the
		 * caller
		 */ /*
	 * If the mode transition is not any of the two cases above, CAN must
	 * enter Configuration Mode before switching into the target operation
	 * mode.
	 */ /*
	 * Check if the device has entered Configuration Mode, if not, return to
	 * the caller.
	 */ /* Configuration Mode */ /*
		 * As CAN is in Configuration Mode already.
		 * Nothing is needed to be done here
		 */ /* Sleep Mode */ /* Switch the device into Sleep Mode */ /* Normal Mode */ /* Switch the device into Normal Mode */ /* Loop back Mode */ /* Switch the device into Loop back Mode */