]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/canps_v2_0/src/xcanps_selftest.c
Remove obsolete MPU demos.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / canps_v2_0 / src / xcanps_selftest.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2014 Xilinx, Inc.  All rights reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
18 *
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
25 * SOFTWARE.
26 *
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.
30 *
31 ******************************************************************************/
32 /*****************************************************************************/
33 /**
34 *
35 * @file xcanps_selftest.c
36 * @addtogroup canps_v2_0
37 * @{
38 *
39 * This file contains a diagnostic self-test function for the XCanPs driver.
40 *
41 * Read xcanps.h file for more information.
42 *
43 * @note
44 * The  Baud Rate Prescaler Register (BRPR) and Bit Timing Register(BTR)
45 * are setup such that CAN baud rate equals 40Kbps, given the CAN clock
46 * equal to 24MHz. These need to be changed based on the desired baudrate
47 * and CAN clock frequency.
48 *
49 * <pre>
50 * MODIFICATION HISTORY:
51 *
52 * Ver   Who    Date     Changes
53 * ----- -----  -------- -----------------------------------------------
54 * 1.00a xd/sv  01/12/10 First release
55 * </pre>
56 *
57 *****************************************************************************/
58
59 /***************************** Include Files ********************************/
60
61 #include "xstatus.h"
62 #include "xcanps.h"
63
64 /************************** Constant Definitions ****************************/
65
66 #define XCANPS_MAX_FRAME_SIZE_IN_WORDS (XCANPS_MAX_FRAME_SIZE / sizeof(u32))
67
68 #define FRAME_DATA_LENGTH       8 /* Frame Data field length */
69
70 /**************************** Type Definitions ******************************/
71
72 /***************** Macros (Inline Functions) Definitions ********************/
73
74 /************************** Variable Definitions ****************************/
75
76 /*
77  * Buffers to hold frames to send and receive. These are declared as global so
78  * that they are not on the stack.
79  */
80 static u32 TxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];
81 static u32 RxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];
82
83 /************************** Function Prototypes *****************************/
84
85 /*****************************************************************************/
86 /**
87 *
88 * This function runs a self-test on the CAN driver/device. The test resets
89 * the device, sets up the Loop Back mode, sends a standard frame, receives the
90 * frame, verifies the contents, and resets the device again.
91 *
92 * Note that this is a destructive test in that resets of the device are
93 * performed. Refer the device specification for the device status after
94 * the reset operation.
95 *
96 *
97 * @param        InstancePtr is a pointer to the XCanPs instance.
98 *
99 * @return
100 *               - XST_SUCCESS if the self-test passed. i.e., the frame
101 *                 received via the internal loop back has the same contents as
102 *                 the frame sent.
103 *               - XST_FAILURE   Otherwise.
104 *
105 * @note
106 *
107 * If the CAN device does not work properly, this function may enter an
108 * infinite loop and will never return to the caller.
109 * <br><br>
110 * If XST_FAILURE is returned, the device is not reset so that the caller could
111 * have a chance to check reason(s) causing the failure.
112 *
113 ******************************************************************************/
114 int XCanPs_SelfTest(XCanPs *InstancePtr)
115 {
116         u8 *FramePtr;
117         u32 Status;
118         u32 Index;
119
120         Xil_AssertNonvoid(InstancePtr != NULL);
121         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
122
123         XCanPs_Reset(InstancePtr);
124
125         /*
126          * The device should enter Configuration Mode immediately after
127          * reset above is finished. Now check the mode and return error code if
128          * it is not Configuration Mode.
129          */
130         if (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_CONFIG) {
131                 return XST_FAILURE;
132         }
133
134         /*
135          * Setup Baud Rate Prescaler Register (BRPR) and Bit Timing Register
136          * (BTR) such that CAN baud rate equals 40Kbps, given the CAN clock
137          * equal to 24MHz. For more information see the CAN 2.0A, CAN 2.0B,
138          * ISO 11898-1 specifications.
139          */
140         XCanPs_SetBaudRatePrescaler(InstancePtr, 1);
141         XCanPs_SetBitTiming(InstancePtr, 1, 3, 8);
142
143         /*
144          * Enter the loop back mode.
145          */
146         XCanPs_EnterMode(InstancePtr, XCANPS_MODE_LOOPBACK);
147         while (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_LOOPBACK);
148
149         /*
150          * Create a frame to send with known values so we can verify them
151          * on receive.
152          */
153         TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)2000, 0, 0, 0, 0);
154         TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)8);
155
156         FramePtr = (u8 *) (&TxFrame[2]);
157         for (Index = 0; Index < 8; Index++) {
158                 *FramePtr++ = (u8) Index;
159         }
160
161         /*
162          * Send the frame.
163          */
164         Status = XCanPs_Send(InstancePtr, TxFrame);
165         if (Status != XST_SUCCESS) {
166                 return XST_FAILURE;
167         }
168
169         /*
170          * Wait until the frame arrives RX FIFO via internal loop back.
171          */
172         while (XCanPs_IsRxEmpty(InstancePtr) == TRUE);
173
174         /*
175          * Receive the frame.
176          */
177         Status = XCanPs_Recv(InstancePtr, RxFrame);
178         if (Status != XST_SUCCESS) {
179                 return XST_FAILURE;
180         }
181
182         /*
183          * Verify Identifier and Data Length Code.
184          */
185         if (RxFrame[0] !=
186                 (u32)XCanPs_CreateIdValue((u32)2000, 0, 0, 0, 0)) {
187                 return XST_FAILURE;
188         }
189
190         if ((RxFrame[1] & ~XCANPS_DLCR_TIMESTAMP_MASK) != TxFrame[1]) {
191                 return XST_FAILURE;
192         }
193
194
195         for (Index = 2; Index < XCANPS_MAX_FRAME_SIZE_IN_WORDS; Index++) {
196                 if (RxFrame[Index] != TxFrame[Index]) {
197                         return XST_FAILURE;
198                 }
199         }
200
201         /*
202          * Reset device again before returning to the caller.
203          */
204         XCanPs_Reset(InstancePtr);
205
206         return XST_SUCCESS;
207 }
208
209
210 /** @} */