1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
35 * @file xcanps_selftest.c
37 * This file contains a diagnostic self-test function for the XCanPs driver.
39 * Read xcanps.h file for more information.
42 * The Baud Rate Prescaler Register (BRPR) and Bit Timing Register(BTR)
43 * are setup such that CAN baud rate equals 40Kbps, given the CAN clock
44 * equal to 24MHz. These need to be changed based on the desired baudrate
45 * and CAN clock frequency.
48 * MODIFICATION HISTORY:
50 * Ver Who Date Changes
51 * ----- ----- -------- -----------------------------------------------
52 * 1.00a xd/sv 01/12/10 First release
53 * 2.1 adk 23/08/14 Fixed CR:798792 Peripheral test for CANPS IP in
54 * SDK claims a 40kbps baud rate but it's not.
55 * 3.00 kvn 02/13/15 Modified code for MISRA_C:2012 compliance.
58 *****************************************************************************/
60 /***************************** Include Files ********************************/
65 /************************** Constant Definitions ****************************/
67 #define XCANPS_MAX_FRAME_SIZE_IN_WORDS ((XCANPS_MAX_FRAME_SIZE) / (sizeof(u32)))
69 #define FRAME_DATA_LENGTH 8U /* Frame Data field length */
71 /**************************** Type Definitions ******************************/
73 /***************** Macros (Inline Functions) Definitions ********************/
75 /************************** Variable Definitions ****************************/
78 * Buffers to hold frames to send and receive. These are declared as global so
79 * that they are not on the stack.
81 static u32 TxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];
82 static u32 RxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];
84 /************************** Function Prototypes *****************************/
86 /*****************************************************************************/
89 * This function runs a self-test on the CAN driver/device. The test resets
90 * the device, sets up the Loop Back mode, sends a standard frame, receives the
91 * frame, verifies the contents, and resets the device again.
93 * Note that this is a destructive test in that resets of the device are
94 * performed. Refer the device specification for the device status after
95 * the reset operation.
98 * @param InstancePtr is a pointer to the XCanPs instance.
101 * - XST_SUCCESS if the self-test passed. i.e., the frame
102 * received via the internal loop back has the same contents as
104 * - XST_FAILURE Otherwise.
108 * If the CAN device does not work properly, this function may enter an
109 * infinite loop and will never return to the caller.
111 * If XST_FAILURE is returned, the device is not reset so that the caller could
112 * have a chance to check reason(s) causing the failure.
114 ******************************************************************************/
115 s32 XCanPs_SelfTest(XCanPs *InstancePtr)
123 Xil_AssertNonvoid(InstancePtr != NULL);
124 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
126 XCanPs_Reset(InstancePtr);
129 * The device should enter Configuration Mode immediately after
130 * reset above is finished. Now check the mode and return error code if
131 * it is not Configuration Mode.
133 if (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_CONFIG) {
134 Status = XST_FAILURE;
139 * Setup Baud Rate Prescaler Register (BRPR) and Bit Timing Register
140 * (BTR) such that CAN baud rate equals 40Kbps, given the CAN clock
141 * equal to 24MHz. For more information see the CAN 2.0A, CAN 2.0B,
142 * ISO 11898-1 specifications.
144 (void)XCanPs_SetBaudRatePrescaler(InstancePtr, (u8)29U);
145 (void)XCanPs_SetBitTiming(InstancePtr, (u8)3U, (u8)2U, (u8)15U);
148 * Enter the loop back mode.
150 XCanPs_EnterMode(InstancePtr, XCANPS_MODE_LOOPBACK);
151 GetModeResult = XCanPs_GetMode(InstancePtr);
152 while (GetModeResult != ((u8)XCANPS_MODE_LOOPBACK)) {
153 GetModeResult = XCanPs_GetMode(InstancePtr);
157 * Create a frame to send with known values so we can verify them
160 TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)2000U, (u32)0U, (u32)0U, (u32)0U, (u32)0U);
161 TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)8U);
163 FramePtr = (u8 *)((void *)(&TxFrame[2]));
164 for (Index = 0U; Index < 8U; Index++) {
165 if(*FramePtr != 0U) {
166 *FramePtr = (u8)Index;
174 Status = XCanPs_Send(InstancePtr, TxFrame);
175 if (Status != (s32)XST_SUCCESS) {
176 Status = XST_FAILURE;
181 * Wait until the frame arrives RX FIFO via internal loop back.
183 RxEmptyResult = XCanPs_ReadReg(((InstancePtr)->CanConfig.BaseAddr),
184 XCANPS_ISR_OFFSET) & XCANPS_IXR_RXNEMP_MASK;
186 while (RxEmptyResult == (u32)0U) {
187 RxEmptyResult = XCanPs_ReadReg(((InstancePtr)->CanConfig.BaseAddr),
188 XCANPS_ISR_OFFSET) & XCANPS_IXR_RXNEMP_MASK;
194 Status = XCanPs_Recv(InstancePtr, RxFrame);
195 if (Status != (s32)XST_SUCCESS) {
196 Status = XST_FAILURE;
201 * Verify Identifier and Data Length Code.
204 (u32)XCanPs_CreateIdValue((u32)2000U, (u32)0U, (u32)0U, (u32)0U, (u32)0U)) {
205 Status = XST_FAILURE;
209 if ((RxFrame[1] & ~XCANPS_DLCR_TIMESTAMP_MASK) != TxFrame[1]) {
210 Status = XST_FAILURE;
215 for (Index = 2U; Index < (XCANPS_MAX_FRAME_SIZE_IN_WORDS); Index++) {
216 if (RxFrame[Index] != TxFrame[Index]) {
217 Status = XST_FAILURE;
223 * Reset device again before returning to the caller.
225 XCanPs_Reset(InstancePtr);
227 Status = XST_SUCCESS;