]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A53_64-bit_UltraScale_MPSoC/RTOSDemo_A53_bsp/psu_cortexa53_0/libsrc/canps_v3_2/src/xcanps_selftest.c
Update Zynq MPSoC hardware definition and BSP files to be those shipped with the...
[freertos] / FreeRTOS / Demo / CORTEX_A53_64-bit_UltraScale_MPSoC / RTOSDemo_A53_bsp / psu_cortexa53_0 / libsrc / canps_v3_2 / src / xcanps_selftest.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2015 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_v3_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 * 2.1 adk               23/08/14 Fixed CR:798792 Peripheral test for CANPS IP in
56 *                                                SDK claims a 40kbps baud rate but it's not.
57 * 3.00  kvn    02/13/15 Modified code for MISRA_C:2012 compliance.
58 * </pre>
59 *
60 *****************************************************************************/
61
62 /***************************** Include Files ********************************/
63
64 #include "xstatus.h"
65 #include "xcanps.h"
66
67 /************************** Constant Definitions ****************************/
68
69 #define XCANPS_MAX_FRAME_SIZE_IN_WORDS ((XCANPS_MAX_FRAME_SIZE) / (sizeof(u32)))
70
71 #define FRAME_DATA_LENGTH       8U /* Frame Data field length */
72
73 /**************************** Type Definitions ******************************/
74
75 /***************** Macros (Inline Functions) Definitions ********************/
76
77 /************************** Variable Definitions ****************************/
78
79 /*
80  * Buffers to hold frames to send and receive. These are declared as global so
81  * that they are not on the stack.
82  */
83 static u32 TxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];
84 static u32 RxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];
85
86 /************************** Function Prototypes *****************************/
87
88 /*****************************************************************************/
89 /**
90 *
91 * This function runs a self-test on the CAN driver/device. The test resets
92 * the device, sets up the Loop Back mode, sends a standard frame, receives the
93 * frame, verifies the contents, and resets the device again.
94 *
95 * Note that this is a destructive test in that resets of the device are
96 * performed. Refer the device specification for the device status after
97 * the reset operation.
98 *
99 *
100 * @param        InstancePtr is a pointer to the XCanPs instance.
101 *
102 * @return
103 *               - XST_SUCCESS if the self-test passed. i.e., the frame
104 *                 received via the internal loop back has the same contents as
105 *                 the frame sent.
106 *               - XST_FAILURE   Otherwise.
107 *
108 * @note
109 *
110 * If the CAN device does not work properly, this function may enter an
111 * infinite loop and will never return to the caller.
112 * <br><br>
113 * If XST_FAILURE is returned, the device is not reset so that the caller could
114 * have a chance to check reason(s) causing the failure.
115 *
116 ******************************************************************************/
117 s32 XCanPs_SelfTest(XCanPs *InstancePtr)
118 {
119         u8 *FramePtr;
120         s32 Status;
121         u32 Index;
122         u8 GetModeResult;
123         u32 RxEmptyResult;
124
125         Xil_AssertNonvoid(InstancePtr != NULL);
126         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
127
128         XCanPs_Reset(InstancePtr);
129
130         /*
131          * The device should enter Configuration Mode immediately after
132          * reset above is finished. Now check the mode and return error code if
133          * it is not Configuration Mode.
134          */
135         if (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_CONFIG) {
136                 Status = XST_FAILURE;
137                 return Status;
138         }
139
140         /*
141          * Setup Baud Rate Prescaler Register (BRPR) and Bit Timing Register
142          * (BTR) such that CAN baud rate equals 40Kbps, given the CAN clock
143          * equal to 24MHz. For more information see the CAN 2.0A, CAN 2.0B,
144          * ISO 11898-1 specifications.
145          */
146         (void)XCanPs_SetBaudRatePrescaler(InstancePtr, (u8)29U);
147         (void)XCanPs_SetBitTiming(InstancePtr, (u8)3U, (u8)2U, (u8)15U);
148
149         /*
150          * Enter the loop back mode.
151          */
152         XCanPs_EnterMode(InstancePtr, XCANPS_MODE_LOOPBACK);
153         GetModeResult = XCanPs_GetMode(InstancePtr);
154         while (GetModeResult != ((u8)XCANPS_MODE_LOOPBACK)) {
155                 GetModeResult = XCanPs_GetMode(InstancePtr);
156         }
157
158         /*
159          * Create a frame to send with known values so we can verify them
160          * on receive.
161          */
162         TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)2000U, (u32)0U, (u32)0U, (u32)0U, (u32)0U);
163         TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)8U);
164
165         FramePtr = (u8 *)((void *)(&TxFrame[2]));
166         for (Index = 0U; Index < 8U; Index++) {
167                 if(*FramePtr != 0U) {
168                         *FramePtr = (u8)Index;
169                         FramePtr++;
170                 }
171         }
172
173         /*
174          * Send the frame.
175          */
176         Status = XCanPs_Send(InstancePtr, TxFrame);
177         if (Status != (s32)XST_SUCCESS) {
178                 Status = XST_FAILURE;
179                 return Status;
180         }
181
182         /*
183          * Wait until the frame arrives RX FIFO via internal loop back.
184          */
185         RxEmptyResult = XCanPs_ReadReg(((InstancePtr)->CanConfig.BaseAddr),
186                         XCANPS_ISR_OFFSET) & XCANPS_IXR_RXNEMP_MASK;
187
188         while (RxEmptyResult == (u32)0U) {
189                 RxEmptyResult = XCanPs_ReadReg(((InstancePtr)->CanConfig.BaseAddr),
190                                 XCANPS_ISR_OFFSET) & XCANPS_IXR_RXNEMP_MASK;
191         }
192
193         /*
194          * Receive the frame.
195          */
196         Status = XCanPs_Recv(InstancePtr, RxFrame);
197         if (Status != (s32)XST_SUCCESS) {
198                 Status = XST_FAILURE;
199                 return Status;
200         }
201
202         /*
203          * Verify Identifier and Data Length Code.
204          */
205         if (RxFrame[0] !=
206                 (u32)XCanPs_CreateIdValue((u32)2000U, (u32)0U, (u32)0U, (u32)0U, (u32)0U)) {
207                 Status = XST_FAILURE;
208                 return Status;
209         }
210
211         if ((RxFrame[1] & ~XCANPS_DLCR_TIMESTAMP_MASK) != TxFrame[1]) {
212                 Status = XST_FAILURE;
213                 return Status;
214         }
215
216
217         for (Index = 2U; Index < (XCANPS_MAX_FRAME_SIZE_IN_WORDS); Index++) {
218                 if (RxFrame[Index] != TxFrame[Index]) {
219                         Status = XST_FAILURE;
220                         return Status;
221                 }
222         }
223
224         /*
225          * Reset device again before returning to the caller.
226          */
227         XCanPs_Reset(InstancePtr);
228
229         Status = XST_SUCCESS;
230         return Status;
231 }
232
233
234 /** @} */