1 /******************************************************************************
3 * Copyright (C) 2002 - 2014 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 xuartlite_intr.c
37 * This file contains interrupt-related functions for the UART Lite component
41 * MODIFICATION HISTORY:
43 * Ver Who Date Changes
44 * ----- ---- -------- -----------------------------------------------
45 * 1.00a ecm 08/31/01 First release
46 * 1.00b jhl 02/21/02 Repartitioned the driver for smaller files
47 * 1.02a rpm 02/14/07 Added check for outstanding transmission before
48 * calling the send callback (avoids extraneous
49 * callback invocations)
50 * 2.00a ktn 10/20/09 Updated to use HAL Processor APIs. The macros have been
51 * renamed to remove _m from the name.
54 *****************************************************************************/
56 /***************************** Include Files ********************************/
58 #include "xuartlite.h"
59 #include "xuartlite_i.h"
62 /************************** Constant Definitions ****************************/
64 /**************************** Type Definitions ******************************/
66 /***************** Macros (Inline Functions) Definitions ********************/
68 /************************** Function Prototypes *****************************/
70 static void ReceiveDataHandler(XUartLite *InstancePtr);
71 static void SendDataHandler(XUartLite *InstancePtr);
73 /************************** Variable Definitions ****************************/
75 typedef void (*Handler)(XUartLite *InstancePtr);
77 /****************************************************************************/
80 * This function sets the handler that will be called when an event (interrupt)
81 * occurs in the driver. The purpose of the handler is to allow application
82 * specific processing to be performed.
84 * @param InstancePtr is a pointer to the XUartLite instance.
85 * @param FuncPtr is the pointer to the callback function.
86 * @param CallBackRef is the upper layer callback reference passed back
87 * when the callback function is invoked.
91 * @note There is no assert on the CallBackRef since the driver doesn't
92 * know what it is (nor should it)
94 *****************************************************************************/
95 void XUartLite_SetRecvHandler(XUartLite *InstancePtr,
96 XUartLite_Handler FuncPtr, void *CallBackRef)
99 * Assert validates the input arguments
100 * CallBackRef not checked, no way to know what is valid
102 Xil_AssertVoid(InstancePtr != NULL);
103 Xil_AssertVoid(FuncPtr != NULL);
104 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
106 InstancePtr->RecvHandler = FuncPtr;
107 InstancePtr->RecvCallBackRef = CallBackRef;
110 /****************************************************************************/
113 * This function sets the handler that will be called when an event (interrupt)
114 * occurs in the driver. The purpose of the handler is to allow application
115 * specific processing to be performed.
117 * @param InstancePtr is a pointer to the XUartLite instance .
118 * @param FuncPtr is the pointer to the callback function.
119 * @param CallBackRef is the upper layer callback reference passed back
120 * when the callback function is invoked.
124 * @note There is no assert on the CallBackRef since the driver doesn't
125 * know what it is (nor should it)
127 *****************************************************************************/
128 void XUartLite_SetSendHandler(XUartLite *InstancePtr,
129 XUartLite_Handler FuncPtr, void *CallBackRef)
132 * Assert validates the input arguments
133 * CallBackRef not checked, no way to know what is valid
135 Xil_AssertVoid(InstancePtr != NULL);
136 Xil_AssertVoid(FuncPtr != NULL);
137 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
139 InstancePtr->SendHandler = FuncPtr;
140 InstancePtr->SendCallBackRef = CallBackRef;
143 /****************************************************************************/
146 * This function is the interrupt handler for the UART lite driver.
147 * It must be connected to an interrupt system by the user such that it is
148 * called when an interrupt for any UART lite occurs. This function
149 * does not save or restore the processor context such that the user must
150 * ensure this occurs.
152 * @param InstancePtr contains a pointer to the instance of the UART that
153 * the interrupt is for.
159 ******************************************************************************/
160 void XUartLite_InterruptHandler(XUartLite *InstancePtr)
164 Xil_AssertVoid(InstancePtr != NULL);
167 * Read the status register to determine which, coulb be both
168 * interrupt is active
170 IsrStatus = XUartLite_ReadReg(InstancePtr->RegBaseAddress,
171 XUL_STATUS_REG_OFFSET);
173 if ((IsrStatus & (XUL_SR_RX_FIFO_FULL |
174 XUL_SR_RX_FIFO_VALID_DATA)) != 0) {
175 ReceiveDataHandler(InstancePtr);
178 if (((IsrStatus & XUL_SR_TX_FIFO_EMPTY) != 0) &&
179 (InstancePtr->SendBuffer.RequestedBytes > 0)) {
180 SendDataHandler(InstancePtr);
184 /****************************************************************************/
187 * This function handles the interrupt when data is received, either a single
188 * byte when FIFOs are not enabled, or multiple bytes with the FIFO.
190 * @param InstancePtr is a pointer to the XUartLite instance.
196 *****************************************************************************/
197 static void ReceiveDataHandler(XUartLite *InstancePtr)
200 * If there are bytes still to be received in the specified buffer
201 * go ahead and receive them
203 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
204 XUartLite_ReceiveBuffer(InstancePtr);
208 * If the last byte of a message was received then call the application
209 * handler, this code should not use an else from the previous check of
210 * the number of bytes to receive because the call to receive the buffer
211 * updates the bytes to receive
213 if (InstancePtr->ReceiveBuffer.RemainingBytes == 0) {
214 InstancePtr->RecvHandler(InstancePtr->RecvCallBackRef,
215 InstancePtr->ReceiveBuffer.RequestedBytes -
216 InstancePtr->ReceiveBuffer.RemainingBytes);
220 * Update the receive stats to reflect the receive interrupt
222 InstancePtr->Stats.ReceiveInterrupts++;
225 /****************************************************************************/
228 * This function handles the interrupt when data has been sent, the transmit
229 * FIFO is empty (transmitter holding register).
231 * @param InstancePtr is a pointer to the XUartLite instance .
237 *****************************************************************************/
238 static void SendDataHandler(XUartLite *InstancePtr)
241 * If there are not bytes to be sent from the specified buffer,
242 * call the callback function
244 if (InstancePtr->SendBuffer.RemainingBytes == 0) {
248 * Save and zero the requested bytes since transmission
251 SaveReq = InstancePtr->SendBuffer.RequestedBytes;
252 InstancePtr->SendBuffer.RequestedBytes = 0;
255 * Call the application handler to indicate
256 * the data has been sent
258 InstancePtr->SendHandler(InstancePtr->SendCallBackRef, SaveReq);
261 * Otherwise there is still more data to send in the specified buffer
262 * so go ahead and send it
265 XUartLite_SendBuffer(InstancePtr);
269 * Update the transmit stats to reflect the transmit interrupt
271 InstancePtr->Stats.TransmitInterrupts++;
275 /*****************************************************************************/
278 * This function disables the UART interrupt. After calling this function,
279 * data may still be received by the UART but no interrupt will be generated
280 * since the hardware device has no way to disable the receiver.
282 * @param InstancePtr is a pointer to the XUartLite instance.
288 *****************************************************************************/
289 void XUartLite_DisableInterrupt(XUartLite *InstancePtr)
291 Xil_AssertVoid(InstancePtr != NULL);
292 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
295 * Write to the control register to disable the interrupts, the only
296 * other bits in this register are the FIFO reset bits such that
297 * writing them to zero will not affect them.
299 XUartLite_WriteReg(InstancePtr->RegBaseAddress,
300 XUL_CONTROL_REG_OFFSET, 0);
303 /*****************************************************************************/
306 * This function enables the UART interrupt such that an interrupt will occur
307 * when data is received or data has been transmitted. The device contains
308 * 16 byte receive and transmit FIFOs such that an interrupt is generated
309 * anytime there is data in the receive FIFO and when the transmit FIFO
310 * transitions from not empty to empty.
312 * @param InstancePtr is a pointer to the XUartLite instance.
318 *****************************************************************************/
319 void XUartLite_EnableInterrupt(XUartLite *InstancePtr)
321 Xil_AssertVoid(InstancePtr != NULL);
322 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
325 * Write to the control register to enable the interrupts, the only
326 * other bits in this register are the FIFO reset bits such that
327 * writing them to zero will not affect them.
329 XUartLite_WriteReg(InstancePtr->RegBaseAddress,
330 XUL_CONTROL_REG_OFFSET, XUL_CR_ENABLE_INTR);