📄 xcanps_selftest.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_selftest.c** This file contains a diagnostic self-test function for the XCanPs driver.** Read xcanps.h file for more information.** @note* The Baud Rate Prescaler Register (BRPR) and Bit Timing Register(BTR)* are setup such that CAN baud rate equals 40Kbps, given the CAN clock* equal to 24MHz. These need to be changed based on the desired baudrate* and CAN clock frequency.** <pre>* MODIFICATION HISTORY:** Ver Who Date Changes* ----- ----- -------- -----------------------------------------------* 1.00a xd/sv 01/12/10 First release* </pre>******************************************************************************//***************************** Include Files ********************************/#include "xstatus.h"#include "xcanps.h"/************************** Constant Definitions ****************************/#define XCANPS_MAX_FRAME_SIZE_IN_WORDS (XCANPS_MAX_FRAME_SIZE / sizeof(u32))#define FRAME_DATA_LENGTH 8 /* Frame Data field length *//**************************** Type Definitions ******************************//***************** Macros (Inline Functions) Definitions ********************//************************** Variable Definitions ****************************//* * Buffers to hold frames to send and receive. These are declared as global so * that they are not on the stack. */static u32 TxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];static u32 RxFrame[XCANPS_MAX_FRAME_SIZE_IN_WORDS];/************************** Function Prototypes *****************************//*****************************************************************************//**** This function runs a self-test on the CAN driver/device. The test resets* the device, sets up the Loop Back mode, sends a standard frame, receives the* frame, verifies the contents, and resets the device again.** Note that this is a destructive test in that resets of the device are* performed. Refer the device specification for the device status after* the reset operation.*** @param InstancePtr is a pointer to the XCanPs instance.** @return* - XST_SUCCESS if the self-test passed. i.e., the frame* received via the internal loop back has the same contents as* the frame sent.* - XST_FAILURE Otherwise.** @note** If the CAN device does not work properly, this function may enter an* infinite loop and will never return to the caller.* <br><br>* If XST_FAILURE is returned, the device is not reset so that the caller could* have a chance to check reason(s) causing the failure.*******************************************************************************/int XCanPs_SelfTest(XCanPs *InstancePtr){ u8 *FramePtr; u32 Status; u32 Index; Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); XCanPs_Reset(InstancePtr); /* * The device should enter Configuration Mode immediately after * reset above is finished. Now check the mode and return error code if * it is not Configuration Mode. */ if (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_CONFIG) { return XST_FAILURE; } /* * Setup Baud Rate Prescaler Register (BRPR) and Bit Timing Register * (BTR) such that CAN baud rate equals 40Kbps, given the CAN clock * equal to 24MHz. For more information see the CAN 2.0A, CAN 2.0B, * ISO 11898-1 specifications. */ XCanPs_SetBaudRatePrescaler(InstancePtr, 1); XCanPs_SetBitTiming(InstancePtr, 1, 3, 8); /* * Enter the loop back mode. */ XCanPs_EnterMode(InstancePtr, XCANPS_MODE_LOOPBACK); while (XCanPs_GetMode(InstancePtr) != XCANPS_MODE_LOOPBACK); /* * Create a frame to send with known values so we can verify them * on receive. */ TxFrame[0] = (u32)XCanPs_CreateIdValue((u32)2000, 0, 0, 0, 0); TxFrame[1] = (u32)XCanPs_CreateDlcValue((u32)8); FramePtr = (u8 *) (&TxFrame[2]); for (Index = 0; Index < 8; Index++) { *FramePtr++ = (u8) Index; } /* * Send the frame. */ Status = XCanPs_Send(InstancePtr, TxFrame); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Wait until the frame arrives RX FIFO via internal loop back. */ while (XCanPs_IsRxEmpty(InstancePtr) == TRUE); /* * Receive the frame. */ Status = XCanPs_Recv(InstancePtr, RxFrame); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Verify Identifier and Data Length Code. */ if (RxFrame[0] != (u32)XCanPs_CreateIdValue((u32)2000, 0, 0, 0, 0)) { return XST_FAILURE; } if ((RxFrame[1] & ~XCANPS_DLCR_TIMESTAMP_MASK) != TxFrame[1]) { return XST_FAILURE; } for (Index = 2; Index < XCANPS_MAX_FRAME_SIZE_IN_WORDS; Index++) { if (RxFrame[Index] != TxFrame[Index]) { return XST_FAILURE; } } /* * Reset device again before returning to the caller. */ XCanPs_Reset(InstancePtr); return XST_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -