📄 xcanps.c
字号:
/******************************************************************************** (c) Copyright 2010-11 Xilinx, Inc. All rights reserved.** This file contains confidential and proprietary information of Xilinx, Inc.* and is protected under U.S. and international copyright and other* intellectual property laws.** DISCLAIMER* This disclaimer is not a license and does not grant any rights to the* materials distributed herewith. Except as otherwise provided in a valid* license issued to you by Xilinx, and to the maximum extent permitted by* applicable law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND WITH ALL* FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS,* IMPLIED, OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF* MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE;* and (2) Xilinx shall not be liable (whether in contract or tort, including* negligence, or under any other theory of liability) for any loss or damage* of any kind or nature related to, arising under or in connection with these* materials, including for any direct, or any indirect, special, incidental,* or consequential loss or damage (including loss of data, profits, goodwill,* or any type of loss or damage suffered as a result of any action brought by* a third party) even if such damage or loss was reasonably foreseeable or* Xilinx had been advised of the possibility of the same.** CRITICAL APPLICATIONS* Xilinx products are not designed or intended to be fail-safe, or for use in* any application requiring fail-safe performance, such as life-support or* safety devices or systems, Class III medical devices, nuclear facilities,* applications related to the deployment of airbags, or any other applications* that could lead to death, personal injury, or severe property or* environmental damage (individually and collectively, "Critical* Applications"). Customer assumes the sole risk and liability of any use of* Xilinx products in Critical Applications, subject only to applicable laws* and regulations governing limitations on product liability.** THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS PART OF THIS FILE* AT ALL TIMES.*******************************************************************************//*****************************************************************************//**** @file xcanps.c** Functions in this file are the minimum required functions for the XCanPs* driver. See xcanps.h for a detailed description of the driver.** @note None.*** <pre>* MODIFICATION HISTORY:** Ver Who Date Changes* ----- ----- -------- -----------------------------------------------* 1.00a xd/sv 01/12/10 First release* 1.01a bss 12/27/11 Added the APIs XCanPs_SetTxIntrWatermark and* XCanPs_GetTxIntrWatermark.* </pre>*******************************************************************************//***************************** Include Files *********************************/#include "xcanps.h"/************************** Constant Definitions *****************************//**************************** Type Definitions *******************************//***************** Macros (Inline Functions) Definitions *********************//************************** Variable Definitions *****************************//************************** Function Prototypes ******************************/static void StubHandler(void);/*****************************************************************************//*** This function initializes a XCanPs instance/driver.** The initialization entails:* - Initialize all members of the XCanPs structure.* - Reset the CAN device. The CAN device will enter Configuration Mode* immediately after the reset is finished.** @param InstancePtr is a pointer to the XCanPs instance.* @param ConfigPtr points to the XCanPs device configuration structure.* @param EffectiveAddr is the device base address in the virtual memory* address space. If the address translation is not used then the* physical address is passed.* Unexpected errors may occur if the address mapping is changed* after this function is invoked.** @return XST_SUCCESS always.** @note None.*******************************************************************************/int XCanPs_CfgInitialize(XCanPs *InstancePtr, XCanPs_Config *ConfigPtr, u32 EffectiveAddr){ Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(ConfigPtr != NULL); /* * Set some default values for instance data, don't indicate the device * is ready to use until everything has been initialized successfully. */ InstancePtr->IsReady = 0; InstancePtr->CanConfig.BaseAddr = EffectiveAddr; InstancePtr->CanConfig.DeviceId = ConfigPtr->DeviceId; /* * Set all handlers to stub values, let user configure this data later. */ InstancePtr->SendHandler = (XCanPs_SendRecvHandler) StubHandler; InstancePtr->RecvHandler = (XCanPs_SendRecvHandler) StubHandler; InstancePtr->ErrorHandler = (XCanPs_ErrorHandler) StubHandler; InstancePtr->EventHandler = (XCanPs_EventHandler) StubHandler; /* * Indicate the component is now ready to use. */ InstancePtr->IsReady = XIL_COMPONENT_IS_READY; /* * Reset the device to get it into its initial state. */ XCanPs_Reset(InstancePtr); return XST_SUCCESS;}/*****************************************************************************//**** This function resets the CAN device. Calling this function resets the device* immediately, and any pending transmission or reception is terminated at once.* Both Object Layer and Transfer Layer are reset. This function does not reset* the Physical Layer. All registers are reset to the default values, and no* previous status will be restored. TX FIFO, RX FIFO and TX High Priority* Buffer are also reset.** When a reset is required due to an internal error, the driver notifies the* upper layer software of this need through the error status code or interrupts.* The upper layer software is responsible for calling this Reset function and* then re-configuring the device.** The CAN device will be in Configuration Mode immediately after this function* returns.** @param InstancePtr is a pointer to the XCanPs instance.** @return None.** @note None.*******************************************************************************/void XCanPs_Reset(XCanPs *InstancePtr){ Xil_AssertVoid(InstancePtr != NULL); Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_SRR_OFFSET, \ XCANPS_SRR_SRST_MASK);}/****************************************************************************//**** This routine returns the current operation mode of the CAN device.** @param InstancePtr is a pointer to the XCanPs instance.** @return* - XCANPS_MODE_CONFIG if the device is in Configuration Mode.* - XCANPS_MODE_SLEEP if the device is in Sleep Mode.* - XCANPS_MODE_NORMAL if the device is in Normal Mode.* - XCANPS_MODE_LOOPBACK if the device is in Loop Back Mode.* - XCANPS_MODE_SNOOP if the device is in Snoop Mode.** @note None.******************************************************************************/u8 XCanPs_GetMode(XCanPs *InstancePtr){ u32 StatusReg; Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); StatusReg = XCanPs_GetStatus(InstancePtr); if (StatusReg & XCANPS_SR_CONFIG_MASK) { return XCANPS_MODE_CONFIG; } else if (StatusReg & XCANPS_SR_SLEEP_MASK) { return XCANPS_MODE_SLEEP; } else if (StatusReg & XCANPS_SR_NORMAL_MASK) { if (StatusReg & XCANPS_SR_SNOOP_MASK) { return XCANPS_MODE_SNOOP; } else { return XCANPS_MODE_NORMAL; } } else { /* * If this line is reached, the device is in Loop Back Mode. */ return XCANPS_MODE_LOOPBACK; }}/*****************************************************************************//**** This function allows the CAN device to enter one of the following operation* modes:* - Configuration Mode: Pass in parameter XCANPS_MODE_CONFIG* - Sleep Mode: Pass in parameter XCANPS_MODE_SLEEP* - Normal Mode: Pass in parameter XCANPS_MODE_NORMAL* - Loop Back Mode: Pass in parameter XCANPS_MODE_LOOPBACK.* - Snoop Mode: Pass in parameter XCANPS_MODE_SNOOP.** Read the xcanps.h file and device specification for detailed description of* each operation mode.** @param InstancePtr is a pointer to the XCanPs instance.* @param OperationMode specify which operation mode to enter. Valid value* is any of XCANPS_MODE_* defined in xcanps.h. Multiple modes* can not be entered at the same time.** @return None.** @note** This function does NOT ensure CAN device enters the specified operation mode* before it returns the control to the caller. The caller is responsible for* checking current operation mode using XCanPs_GetMode().*******************************************************************************/void XCanPs_EnterMode(XCanPs *InstancePtr, u8 OperationMode){ u8 CurrentMode; Xil_AssertVoid(InstancePtr != NULL); Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); Xil_AssertVoid((OperationMode == XCANPS_MODE_CONFIG) || (OperationMode == XCANPS_MODE_SLEEP) || (OperationMode == XCANPS_MODE_NORMAL) || (OperationMode == XCANPS_MODE_LOOPBACK) || (OperationMode == XCANPS_MODE_SNOOP)); CurrentMode = XCanPs_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 == XCANPS_MODE_NORMAL) && (OperationMode == XCANPS_MODE_SLEEP)) { /* * Normal Mode ---> Sleep Mode */ XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_MSR_OFFSET, XCANPS_MSR_SLEEP_MASK); return; } else if ((CurrentMode == XCANPS_MODE_SLEEP) && (OperationMode == XCANPS_MODE_NORMAL)) { /* * Sleep Mode ---> Normal Mode */ XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_MSR_OFFSET, 0); 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. */ XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_SRR_OFFSET, 0); /* * Check if the device has entered Configuration Mode, if not, return to * the caller. */ if (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_CONFIG) { return; } switch (OperationMode) { case XCANPS_MODE_CONFIG: /* * As CAN is in Configuration Mode already. * Nothing is needed to be done here. */ break; case XCANPS_MODE_SLEEP: XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_MSR_OFFSET, XCANPS_MSR_SLEEP_MASK); XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_SRR_OFFSET, XCANPS_SRR_CEN_MASK); break; case XCANPS_MODE_NORMAL: XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_MSR_OFFSET, 0); XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_SRR_OFFSET, XCANPS_SRR_CEN_MASK); break; case XCANPS_MODE_LOOPBACK: XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_MSR_OFFSET, XCANPS_MSR_LBACK_MASK); XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_SRR_OFFSET, XCANPS_SRR_CEN_MASK); break; case XCANPS_MODE_SNOOP: XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_MSR_OFFSET, XCANPS_MSR_SNOOP_MASK); XCanPs_WriteReg(InstancePtr->CanConfig.BaseAddr, XCANPS_SRR_OFFSET, XCANPS_SRR_CEN_MASK); break; }}/*****************************************************************************//**** This function returns Status value from Status Register (SR). Use the* XCANPS_SR_* constants defined in xcanps_hw.h to interpret the returned* value.** @param InstancePtr is a pointer to the XCanPs instance.** @return The 32-bit value read from Status Register.** @note None.*******************************************************************************/u32 XCanPs_GetStatus(XCanPs *InstancePtr){ Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); return XCanPs_ReadReg(InstancePtr->CanConfig.BaseAddr, XCANPS_SR_OFFSET);}/*****************************************************************************//**** This function reads Receive and Transmit error counters.** @param InstancePtr is a pointer to the XCanPs instance.* @param RxErrorCount is a pointer to data in which the Receive Error* counter value is returned.* @param TxErrorCount is a pointer to data in which the Transmit Error* counter value is returned.** @return None.** @note None.*******************************************************************************/void XCanPs_GetBusErrorCounter(XCanPs *InstancePtr, u8 *RxErrorCount, u8 *TxErrorCount){ u32 ErrorCount; Xil_AssertVoid(InstancePtr != NULL); Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); Xil_AssertVoid(RxErrorCount != NULL); Xil_AssertVoid(TxErrorCount != NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -