📄 xuartps.c
字号:
/******************************************************************************* (c) Copyright 2010-12 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 xuartps.c** This file contains the implementation of the interface functions for XUartPs* driver. Refer to the header file xuartps.h for more detailed information.** <pre>* MODIFICATION HISTORY:** Ver Who Date Changes* ----- ------ -------- ----------------------------------------------* 1.00 drg/jz 01/13/10 First Release* </pre>******************************************************************************//***************************** Include Files ********************************/#include "xstatus.h"#include "xuartps.h"#include "xil_io.h"/************************** Constant Definitions ****************************//* The following constant defines the amount of error that is allowed for * a specified baud rate. This error is the difference between the actual * baud rate that will be generated using the specified clock and the * desired baud rate. */#define XUARTPS_MAX_BAUD_ERROR_RATE 3 /* max % error allowed *//**************************** Type Definitions ******************************//***************** Macros (Inline Functions) Definitions ********************//************************** Function Prototypes *****************************/static void XUartPs_StubHandler(void *CallBackRef, u32 Event, unsigned int ByteCount);unsigned int XUartPs_SendBuffer(XUartPs *InstancePtr);unsigned int XUartPs_ReceiveBuffer(XUartPs *InstancePtr);/************************** Variable Definitions ****************************//****************************************************************************//**** Initializes a specific XUartPs instance such that it is ready to be used.* The data format of the device is setup for 8 data bits, 1 stop bit, and no* parity by default. The baud rate is set to a default value specified by* Config->DefaultBaudRate if set, otherwise it is set to 19.2K baud. The* receive FIFO threshold is set for 8 bytes. The default operating mode of the* driver is polled mode.** @param InstancePtr is a pointer to the XUartPs instance.* @param Config is a reference to a structure containing information* about a specific XUartPs driver.* @param EffectiveAddr is the device base address in the virtual memory* address space. The caller is responsible for keeping the address* mapping from EffectiveAddr to the device physical base address* unchanged once this function is invoked. Unexpected errors may* occur if the address mapping changes after this function is* called. If address translation is not used, pass in the physical* address instead.** @return** - XST_SUCCESS if initialization was successful* - XST_UART_BAUD_ERROR if the baud rate is not possible because* the inputclock frequency is not divisible with an acceptable* amount of error** @note** The default configuration for the UART after initialization is:** - 19,200 bps or XPAR_DFT_BAUDRATE if defined* - 8 data bits* - 1 stop bit* - no parity* - FIFO's are enabled with a receive threshold of 8 bytes* - The RX timeout is enabled with a timeout of 1 (4 char times)** All interrupts are disabled.******************************************************************************/int XUartPs_CfgInitialize(XUartPs *InstancePtr, XUartPs_Config * Config, u32 EffectiveAddr){ int Status; u32 ModeRegister; u32 BaudRate; /* * Assert validates the input arguments */ Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(Config != NULL); /* * Setup the driver instance using passed in parameters */ InstancePtr->Config.BaseAddress = EffectiveAddr; InstancePtr->Config.InputClockHz = Config->InputClockHz; InstancePtr->Config.ModemPinsConnected = Config->ModemPinsConnected; /* * Initialize other instance data to default values */ InstancePtr->Handler = XUartPs_StubHandler; InstancePtr->SendBuffer.NextBytePtr = NULL; InstancePtr->SendBuffer.RemainingBytes = 0; InstancePtr->SendBuffer.RequestedBytes = 0; InstancePtr->ReceiveBuffer.NextBytePtr = NULL; InstancePtr->ReceiveBuffer.RemainingBytes = 0; InstancePtr->ReceiveBuffer.RequestedBytes = 0; /* * Flag that the driver instance is ready to use */ InstancePtr->IsReady = XIL_COMPONENT_IS_READY; /* * Set the default baud rate here, can be changed prior to * starting the device */ BaudRate = XUARTPS_DFT_BAUDRATE; Status = XUartPs_SetBaudRate(InstancePtr, BaudRate); if (Status != XST_SUCCESS) { InstancePtr->IsReady = 0; return Status; } /* * Set up the default data format: 8 bit data, 1 stop bit, no * parity */ ModeRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET); /* * Mask off what's already there */ ModeRegister &= ~(XUARTPS_MR_CHARLEN_MASK | XUARTPS_MR_STOPMODE_MASK | XUARTPS_MR_PARITY_MASK); /* * Set the register value to the desired data format */ ModeRegister |= (XUARTPS_MR_CHARLEN_8_BIT | XUARTPS_MR_STOPMODE_1_BIT | XUARTPS_MR_PARITY_NONE); /* * Write the mode register out */ XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET, ModeRegister); /* * Set the RX FIFO trigger at 8 data bytes. */ XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_RXWM_OFFSET, 0x08); /* * Set the RX timeout to 1, which will be 4 character time */ XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_RXTOUT_OFFSET, 0x01); /* * Disable all interrupts, polled mode is the default */ XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IDR_OFFSET, XUARTPS_IXR_MASK); return XST_SUCCESS;}/****************************************************************************//**** This functions sends the specified buffer using the device in either* polled or interrupt driven mode. This function is non-blocking, if the device* is busy sending data, it will return and indicate zero bytes were sent.* Otherwise, it fills the TX FIFO as much as it can, and return the number of* bytes sent.** In a polled mode, this function will only send as much data as TX FIFO can* buffer. The application may need to call it repeatedly to send the entire* buffer.** In interrupt mode, this function will start sending the specified buffer,* then the interrupt handler will continue sending data until the entire* buffer has been sent. A callback function, as specified by the application,* will be called to indicate the completion of sending.** @param InstancePtr is a pointer to the XUartPs instance.* @param BufferPtr is pointer to a buffer of data to be sent.* @param NumBytes contains the number of bytes to be sent. A value of* zero will stop a previous send operation that is in progress* in interrupt mode. Any data that was already put into the* transmit FIFO will be sent.** @return The number of bytes actually sent.** @note** The number of bytes is not asserted so that this function may be called with* a value of zero to stop an operation that is already in progress.* <br><br>******************************************************************************/unsigned int XUartPs_Send(XUartPs *InstancePtr, u8 *BufferPtr, unsigned int NumBytes){ unsigned int BytesSent; /* * Asserts validate the input arguments */ Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(BufferPtr != NULL); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); /* * Disable the UART transmit interrupts to allow this call to stop a * previous operation that may be interrupt driven. */ XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IDR_OFFSET, (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL)); /* * Setup the buffer parameters */ InstancePtr->SendBuffer.RequestedBytes = NumBytes; InstancePtr->SendBuffer.RemainingBytes = NumBytes; InstancePtr->SendBuffer.NextBytePtr = BufferPtr; /* * Transmit interrupts will be enabled in XUartPs_SendBuffer(), after * filling the TX FIFO. */ BytesSent = XUartPs_SendBuffer(InstancePtr); return BytesSent;}/****************************************************************************//**** This function attempts to receive a specified number of bytes of data* from the device and store it into the specified buffer. This function works* for both polled or interrupt driven modes. It is non-blocking.** In a polled mode, this function will only receive the data already in the* RX FIFO. The application may need to call it repeatedly to receive the* entire buffer. Polled mode is the default mode of operation for the device.** In interrupt mode, this function will start the receiving, if not the entire* buffer has been received, the interrupt handler will continue receiving data* until the entire buffer has been received. A callback function, as specified* by the application, will be called to indicate the completion of the* receiving or error conditions.** @param InstancePtr is a pointer to the XUartPs instance* @param BufferPtr is pointer to buffer for data to be received into* @param NumBytes is the number of bytes to be received. A value of zero* will stop a previous receive operation that is in progress in* interrupt mode.** @return The number of bytes received.** @note** The number of bytes is not asserted so that this function may be called* with a value of zero to stop an operation that is already in progress.******************************************************************************/unsigned int XUartPs_Recv(XUartPs *InstancePtr, u8 *BufferPtr, unsigned int NumBytes){ unsigned int ReceivedCount; u32 ImrRegister; /* * Assert validates the input arguments
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -