1 /******************************************************************************
3 * Copyright (C) 2010 - 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 xuartps_intr.c
36 * @addtogroup uartps_v2_1
39 * This file contains the functions for interrupt handling
42 * MODIFICATION HISTORY:
44 * Ver Who Date Changes
45 * ----- ------ -------- -----------------------------------------------
46 * 1.00 drg/jz 01/13/10 First Release
49 *****************************************************************************/
51 /***************************** Include Files ********************************/
55 /************************** Constant Definitions ****************************/
57 /**************************** Type Definitions ******************************/
59 /***************** Macros (Inline Functions) Definitions ********************/
61 /************************** Function Prototypes *****************************/
63 static void ReceiveDataHandler(XUartPs *InstancePtr);
64 static void SendDataHandler(XUartPs *InstancePtr, u32 isrstatus);
65 static void ReceiveErrorHandler(XUartPs *InstancePtr);
66 static void ReceiveTimeoutHandler(XUartPs *InstancePtr);
67 static void ModemHandler(XUartPs *InstancePtr);
70 /* Internal function prototypes implemented in xuartps.c */
71 extern unsigned int XUartPs_ReceiveBuffer(XUartPs *InstancePtr);
72 extern unsigned int XUartPs_SendBuffer(XUartPs *InstancePtr);
74 /************************** Variable Definitions ****************************/
76 typedef void (*Handler)(XUartPs *InstancePtr);
78 /****************************************************************************/
81 * This function gets the interrupt mask
83 * @param InstancePtr is a pointer to the XUartPs instance.
86 * The current interrupt mask. The mask indicates which interupts
91 *****************************************************************************/
92 u32 XUartPs_GetInterruptMask(XUartPs *InstancePtr)
95 * Assert validates the input argument
97 Xil_AssertNonvoid(InstancePtr != NULL);
100 * Read the Interrupt Mask register
102 return (XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
103 XUARTPS_IMR_OFFSET));
106 /****************************************************************************/
109 * This function sets the interrupt mask.
111 * @param InstancePtr is a pointer to the XUartPs instance
112 * @param Mask contains the interrupts to be enabled or disabled.
113 * A '1' enables an interupt, and a '0' disables.
119 *****************************************************************************/
120 void XUartPs_SetInterruptMask(XUartPs *InstancePtr, u32 Mask)
123 * Assert validates the input arguments
125 Xil_AssertVoid(InstancePtr != NULL);
127 Mask &= XUARTPS_IXR_MASK;
130 * Write the mask to the IER Register
132 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
133 XUARTPS_IER_OFFSET, Mask);
136 * Write the inverse of the Mask to the IDR register
138 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
139 XUARTPS_IDR_OFFSET, (~Mask));
143 /****************************************************************************/
146 * This function sets the handler that will be called when an event (interrupt)
147 * occurs that needs application's attention.
149 * @param InstancePtr is a pointer to the XUartPs instance
150 * @param FuncPtr is the pointer to the callback function.
151 * @param CallBackRef is the upper layer callback reference passed back
152 * when the callback function is invoked.
158 * There is no assert on the CallBackRef since the driver doesn't know what it
161 *****************************************************************************/
162 void XUartPs_SetHandler(XUartPs *InstancePtr, XUartPs_Handler FuncPtr,
166 * Asserts validate the input arguments
167 * CallBackRef not checked, no way to know what is valid
169 Xil_AssertVoid(InstancePtr != NULL);
170 Xil_AssertVoid(FuncPtr != NULL);
171 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
173 InstancePtr->Handler = FuncPtr;
174 InstancePtr->CallBackRef = CallBackRef;
177 /****************************************************************************/
180 * This function is the interrupt handler for the driver.
181 * It must be connected to an interrupt system by the application such that it
182 * can be called when an interrupt occurs.
184 * @param InstancePtr contains a pointer to the driver instance
190 ******************************************************************************/
191 void XUartPs_InterruptHandler(XUartPs *InstancePtr)
195 Xil_AssertVoid(InstancePtr != NULL);
196 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
199 * Read the interrupt ID register to determine which
200 * interrupt is active
202 IsrStatus = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
205 IsrStatus &= XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
209 * Dispatch an appropiate handler.
211 if(0 != (IsrStatus & (XUARTPS_IXR_RXOVR | XUARTPS_IXR_RXEMPTY |
212 XUARTPS_IXR_RXFULL))) {
213 /* Recieved data interrupt */
214 ReceiveDataHandler(InstancePtr);
217 if(0 != (IsrStatus & (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL))) {
218 /* Transmit data interrupt */
219 SendDataHandler(InstancePtr, IsrStatus);
222 if(0 != (IsrStatus & (XUARTPS_IXR_OVER | XUARTPS_IXR_FRAMING |
223 XUARTPS_IXR_PARITY))) {
224 /* Recieved Error Status interrupt */
225 ReceiveErrorHandler(InstancePtr);
228 if(0 != (IsrStatus & XUARTPS_IXR_TOUT )) {
229 /* Recieved Timeout interrupt */
230 ReceiveTimeoutHandler(InstancePtr);
233 if(0 != (IsrStatus & XUARTPS_IXR_DMS)) {
234 /* Modem status interrupt */
235 ModemHandler(InstancePtr);
239 * Clear the interrupt status.
241 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_ISR_OFFSET,
246 /****************************************************************************/
249 * This function handles interrupts for receive errors which include
250 * overrun errors, framing errors, parity errors, and the break interrupt.
252 * @param InstancePtr is a pointer to the XUartPs instance.
258 *****************************************************************************/
259 static void ReceiveErrorHandler(XUartPs *InstancePtr)
262 * If there are bytes still to be received in the specified buffer
263 * go ahead and receive them. Removing bytes from the RX FIFO will
264 * clear the interrupt.
266 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
267 XUartPs_ReceiveBuffer(InstancePtr);
271 * Call the application handler to indicate that there is a receive
272 * error or a break interrupt, if the application cares about the
273 * error it call a function to get the last errors.
275 InstancePtr->Handler(InstancePtr->CallBackRef,
276 XUARTPS_EVENT_RECV_ERROR,
277 (InstancePtr->ReceiveBuffer.RequestedBytes -
278 InstancePtr->ReceiveBuffer.RemainingBytes));
281 /****************************************************************************/
284 * This function handles the receive timeout interrupt. This interrupt occurs
285 * whenever a number of bytes have been present in the RX FIFO and the receive
286 * data line has been idle for at lease 4 or more character times, (the timeout
287 * is set using XUartPs_SetrecvTimeout() function).
289 * @param InstancePtr is a pointer to the XUartPs instance
295 *****************************************************************************/
296 static void ReceiveTimeoutHandler(XUartPs *InstancePtr)
301 * If there are bytes still to be received in the specified buffer
302 * go ahead and receive them. Removing bytes from the RX FIFO will
303 * clear the interrupt.
305 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
306 XUartPs_ReceiveBuffer(InstancePtr);
310 * If there are no more bytes to receive then indicate that this is
311 * not a receive timeout but the end of the buffer reached, a timeout
312 * normally occurs if # of bytes is not divisible by FIFO threshold,
313 * don't rely on previous test of remaining bytes since receive
314 * function updates it
316 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
317 Event = XUARTPS_EVENT_RECV_TOUT;
319 Event = XUARTPS_EVENT_RECV_DATA;
323 * Call the application handler to indicate that there is a receive
324 * timeout or data event
326 InstancePtr->Handler(InstancePtr->CallBackRef, Event,
327 InstancePtr->ReceiveBuffer.RequestedBytes -
328 InstancePtr->ReceiveBuffer.RemainingBytes);
331 /****************************************************************************/
334 * This function handles the interrupt when data is in RX FIFO.
336 * @param InstancePtr is a pointer to the XUartPs instance
342 *****************************************************************************/
343 static void ReceiveDataHandler(XUartPs *InstancePtr)
346 * If there are bytes still to be received in the specified buffer
347 * go ahead and receive them. Removing bytes from the RX FIFO will
348 * clear the interrupt.
350 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
351 XUartPs_ReceiveBuffer(InstancePtr);
355 /* If the last byte of a message was received then call the application
356 * handler, this code should not use an else from the previous check of
357 * the number of bytes to receive because the call to receive the buffer
358 * updates the bytes ramained
360 if (InstancePtr->ReceiveBuffer.RemainingBytes == 0) {
361 InstancePtr->Handler(InstancePtr->CallBackRef,
362 XUARTPS_EVENT_RECV_DATA,
363 (InstancePtr->ReceiveBuffer.RequestedBytes -
364 InstancePtr->ReceiveBuffer.RemainingBytes));
369 /****************************************************************************/
372 * This function handles the interrupt when data has been sent, the transmit
373 * FIFO is empty (transmitter holding register).
375 * @param InstancePtr is a pointer to the XUartPs instance
376 * @param IsrStatus is the register value for channel status register
382 *****************************************************************************/
383 static void SendDataHandler(XUartPs *InstancePtr, u32 IsrStatus)
387 * If there are not bytes to be sent from the specified buffer then disable
388 * the transmit interrupt so it will stop interrupting as it interrupts
389 * any time the FIFO is empty
391 if (InstancePtr->SendBuffer.RemainingBytes == 0) {
392 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
394 (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL));
396 /* Call the application handler to indicate the sending is done */
397 InstancePtr->Handler(InstancePtr->CallBackRef,
398 XUARTPS_EVENT_SENT_DATA,
399 InstancePtr->SendBuffer.RequestedBytes -
400 InstancePtr->SendBuffer.RemainingBytes);
404 * If TX FIFO is empty, send more.
406 else if(IsrStatus & XUARTPS_IXR_TXEMPTY) {
407 XUartPs_SendBuffer(InstancePtr);
412 /****************************************************************************/
415 * This function handles modem interrupts. It does not do any processing
416 * except to call the application handler to indicate a modem event.
418 * @param InstancePtr is a pointer to the XUartPs instance
424 *****************************************************************************/
425 static void ModemHandler(XUartPs *InstancePtr)
430 * Read the modem status register so that the interrupt is acknowledged
431 * and it can be passed to the callback handler with the event
433 MsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
434 XUARTPS_MODEMSR_OFFSET);
437 * Call the application handler to indicate the modem status changed,
438 * passing the modem status and the event data in the call
440 InstancePtr->Handler(InstancePtr->CallBackRef,