1 /******************************************************************************
3 * (c) Copyright 2010-11 Xilinx, Inc. All rights reserved.
5 * This file contains confidential and proprietary information of Xilinx, Inc.
6 * and is protected under U.S. and international copyright and other
7 * intellectual property laws.
10 * This disclaimer is not a license and does not grant any rights to the
11 * materials distributed herewith. Except as otherwise provided in a valid
12 * license issued to you by Xilinx, and to the maximum extent permitted by
13 * applicable law: (1) THESE MATERIALS ARE MADE AVAILABLE "AS IS" AND WITH ALL
14 * FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS,
15 * IMPLIED, OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
16 * MERCHANTABILITY, NON-INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE;
17 * and (2) Xilinx shall not be liable (whether in contract or tort, including
18 * negligence, or under any other theory of liability) for any loss or damage
19 * of any kind or nature related to, arising under or in connection with these
20 * materials, including for any direct, or any indirect, special, incidental,
21 * or consequential loss or damage (including loss of data, profits, goodwill,
22 * or any type of loss or damage suffered as a result of any action brought by
23 * a third party) even if such damage or loss was reasonably foreseeable or
24 * Xilinx had been advised of the possibility of the same.
26 * CRITICAL APPLICATIONS
27 * Xilinx products are not designed or intended to be fail-safe, or for use in
28 * any application requiring fail-safe performance, such as life-support or
29 * safety devices or systems, Class III medical devices, nuclear facilities,
30 * applications related to the deployment of airbags, or any other applications
31 * that could lead to death, personal injury, or severe property or
32 * environmental damage (individually and collectively, "Critical
33 * Applications"). Customer assumes the sole risk and liability of any use of
34 * Xilinx products in Critical Applications, subject only to applicable laws
35 * and regulations governing limitations on product liability.
37 * THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS PART OF THIS FILE
40 ******************************************************************************/
41 /*****************************************************************************/
44 * @file xcanps_selftest.c
46 * This file contains a diagnostic self-test function for the XCanPs driver.
48 * Read xcanps.h file for more information.
51 * The Baud Rate Prescaler Register (BRPR) and Bit Timing Register(BTR)
52 * are setup such that CAN baud rate equals 40Kbps, given the CAN clock
53 * equal to 24MHz. These need to be changed based on the desired baudrate
54 * and CAN clock frequency.
57 * MODIFICATION HISTORY:
59 * Ver Who Date Changes
60 * ----- ----- -------- -----------------------------------------------
61 * 1.00a xd/sv 01/12/10 First release
64 *****************************************************************************/
66 /***************************** Include Files ********************************/
71 /************************** Constant Definitions ****************************/
73 #define XCANPS_MAX_FRAME_SIZE_IN_WORDS (XCANPS_MAX_FRAME_SIZE / sizeof(u32))
75 #define FRAME_DATA_LENGTH 8 /* Frame Data field length */
77 /**************************** Type Definitions ******************************/
79 /***************** Macros (Inline Functions) Definitions ********************/
81 /************************** Variable Definitions ****************************/
84 * Buffers to hold frames to send and receive. These are declared as global so
85 * that they are not on the stack.
87 static u32 TxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];
88 static u32 RxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];
90 /************************** Function Prototypes *****************************/
92 /*****************************************************************************/
95 * This function runs a self-test on the CAN driver/device. The test resets
96 * the device, sets up the Loop Back mode, sends a standard frame, receives the
97 * frame, verifies the contents, and resets the device again.
99 * Note that this is a destructive test in that resets of the device are
100 * performed. Refer the device specification for the device status after
101 * the reset operation.
104 * @param InstancePtr is a pointer to the XCanPs instance.
107 * - XST_SUCCESS if the self-test passed. i.e., the frame
108 * received via the internal loop back has the same contents as
110 * - XST_FAILURE Otherwise.
114 * If the CAN device does not work properly, this function may enter an
115 * infinite loop and will never return to the caller.
117 * If XST_FAILURE is returned, the device is not reset so that the caller could
118 * have a chance to check reason(s) causing the failure.
120 ******************************************************************************/
121 int XCanPs_SelfTest(XCanPs *InstancePtr)
127 Xil_AssertNonvoid(InstancePtr != NULL);
128 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
130 XCanPs_Reset(InstancePtr);
133 * The device should enter Configuration Mode immediately after
134 * reset above is finished. Now check the mode and return error code if
135 * it is not Configuration Mode.
137 if (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_CONFIG) {
142 * Setup Baud Rate Prescaler Register (BRPR) and Bit Timing Register
143 * (BTR) such that CAN baud rate equals 40Kbps, given the CAN clock
144 * equal to 24MHz. For more information see the CAN 2.0A, CAN 2.0B,
145 * ISO 11898-1 specifications.
147 XCanPs_SetBaudRatePrescaler(InstancePtr, 1);
148 XCanPs_SetBitTiming(InstancePtr, 1, 3, 8);
151 * Enter the loop back mode.
153 XCanPs_EnterMode(InstancePtr, XCANPS_MODE_LOOPBACK);
154 while (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_LOOPBACK);
157 * Create a frame to send with known values so we can verify them
160 TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)2000, 0, 0, 0, 0);
161 TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)8);
163 FramePtr = (u8 *) (&TxFrame[2]);
164 for (Index = 0; Index < 8; Index++) {
165 *FramePtr++ = (u8) Index;
171 Status = XCanPs_Send(InstancePtr, TxFrame);
172 if (Status != XST_SUCCESS) {
177 * Wait until the frame arrives RX FIFO via internal loop back.
179 while (XCanPs_IsRxEmpty(InstancePtr) == TRUE);
184 Status = XCanPs_Recv(InstancePtr, RxFrame);
185 if (Status != XST_SUCCESS) {
190 * Verify Identifier and Data Length Code.
193 (u32)XCanPs_CreateIdValue((u32)2000, 0, 0, 0, 0)) {
197 if ((RxFrame[1] & ~XCANPS_DLCR_TIMESTAMP_MASK) != TxFrame[1]) {
202 for (Index = 2; Index < XCANPS_MAX_FRAME_SIZE_IN_WORDS; Index++) {
203 if (RxFrame[Index] != TxFrame[Index]) {
209 * Reset device again before returning to the caller.
211 XCanPs_Reset(InstancePtr);