1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 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_v3_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
47 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
48 * 3.1 kvn 04/10/15 Modified code for latest RTL changes.
51 *****************************************************************************/
53 /***************************** Include Files ********************************/
57 /************************** Constant Definitions ****************************/
59 /**************************** Type Definitions ******************************/
61 /***************** Macros (Inline Functions) Definitions ********************/
63 /************************** Function Prototypes *****************************/
65 static void ReceiveDataHandler(XUartPs *InstancePtr);
66 static void SendDataHandler(XUartPs *InstancePtr, u32 IsrStatus);
67 static void ReceiveErrorHandler(XUartPs *InstancePtr, u32 IsrStatus);
68 static void ReceiveTimeoutHandler(XUartPs *InstancePtr);
69 static void ModemHandler(XUartPs *InstancePtr);
72 /* Internal function prototypes implemented in xuartps.c */
73 extern u32 XUartPs_ReceiveBuffer(XUartPs *InstancePtr);
74 extern u32 XUartPs_SendBuffer(XUartPs *InstancePtr);
76 /************************** Variable Definitions ****************************/
78 typedef void (*Handler)(XUartPs *InstancePtr);
80 /****************************************************************************/
83 * This function gets the interrupt mask
85 * @param InstancePtr is a pointer to the XUartPs instance.
88 * The current interrupt mask. The mask indicates which interupts
93 *****************************************************************************/
94 u32 XUartPs_GetInterruptMask(XUartPs *InstancePtr)
96 /* Assert validates the input argument */
97 Xil_AssertNonvoid(InstancePtr != NULL);
99 /* Read the Interrupt Mask register */
100 return (XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
101 XUARTPS_IMR_OFFSET));
104 /****************************************************************************/
107 * This function sets the interrupt mask.
109 * @param InstancePtr is a pointer to the XUartPs instance
110 * @param Mask contains the interrupts to be enabled or disabled.
111 * A '1' enables an interupt, and a '0' disables.
117 *****************************************************************************/
118 void XUartPs_SetInterruptMask(XUartPs *InstancePtr, u32 Mask)
121 /* Assert validates the input arguments */
122 Xil_AssertVoid(InstancePtr != NULL);
124 TempMask &= (u32)XUARTPS_IXR_MASK;
126 /* Write the mask to the IER Register */
127 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
128 XUARTPS_IER_OFFSET, TempMask);
130 /* Write the inverse of the Mask to the IDR register */
131 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
132 XUARTPS_IDR_OFFSET, (~TempMask));
136 /****************************************************************************/
139 * This function sets the handler that will be called when an event (interrupt)
140 * occurs that needs application's attention.
142 * @param InstancePtr is a pointer to the XUartPs instance
143 * @param FuncPtr is the pointer to the callback function.
144 * @param CallBackRef is the upper layer callback reference passed back
145 * when the callback function is invoked.
151 * There is no assert on the CallBackRef since the driver doesn't know what it
154 *****************************************************************************/
155 void XUartPs_SetHandler(XUartPs *InstancePtr, XUartPs_Handler FuncPtr,
159 * Asserts validate the input arguments
160 * CallBackRef not checked, no way to know what is valid
162 Xil_AssertVoid(InstancePtr != NULL);
163 Xil_AssertVoid(FuncPtr != NULL);
164 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
166 InstancePtr->Handler = FuncPtr;
167 InstancePtr->CallBackRef = CallBackRef;
170 /****************************************************************************/
173 * This function is the interrupt handler for the driver.
174 * It must be connected to an interrupt system by the application such that it
175 * can be called when an interrupt occurs.
177 * @param InstancePtr contains a pointer to the driver instance
183 ******************************************************************************/
184 void XUartPs_InterruptHandler(XUartPs *InstancePtr)
188 Xil_AssertVoid(InstancePtr != NULL);
189 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
192 * Read the interrupt ID register to determine which
193 * interrupt is active
195 IsrStatus = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
198 IsrStatus &= XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
201 /* Dispatch an appropriate handler. */
202 if((IsrStatus & ((u32)XUARTPS_IXR_RXOVR | (u32)XUARTPS_IXR_RXEMPTY |
203 (u32)XUARTPS_IXR_RXFULL)) != (u32)0) {
204 /* Received data interrupt */
205 ReceiveDataHandler(InstancePtr);
208 if((IsrStatus & ((u32)XUARTPS_IXR_TXEMPTY | (u32)XUARTPS_IXR_TXFULL))
210 /* Transmit data interrupt */
211 SendDataHandler(InstancePtr, IsrStatus);
214 /* XUARTPS_IXR_RBRK is applicable only for Zynq Ultrascale+ MP */
215 if ((IsrStatus & ((u32)XUARTPS_IXR_OVER | (u32)XUARTPS_IXR_FRAMING |
216 (u32)XUARTPS_IXR_PARITY | (u32)XUARTPS_IXR_RBRK)) != (u32)0) {
217 /* Received Error Status interrupt */
218 ReceiveErrorHandler(InstancePtr, IsrStatus);
221 if((IsrStatus & ((u32)XUARTPS_IXR_TOUT)) != (u32)0) {
222 /* Received Timeout interrupt */
223 ReceiveTimeoutHandler(InstancePtr);
226 if((IsrStatus & ((u32)XUARTPS_IXR_DMS)) != (u32)0) {
227 /* Modem status interrupt */
228 ModemHandler(InstancePtr);
231 /* Clear the interrupt status. */
232 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_ISR_OFFSET,
237 /****************************************************************************/
240 * This function handles interrupts for receive errors which include
241 * overrun errors, framing errors, parity errors, and the break interrupt.
243 * @param InstancePtr is a pointer to the XUartPs instance.
249 *****************************************************************************/
250 static void ReceiveErrorHandler(XUartPs *InstancePtr, u32 IsrStatus)
252 u32 ByteStatusValue, EventData;
255 InstancePtr->is_rxbs_error = 0;
257 if ((InstancePtr->Platform == XPLAT_ZYNQ_ULTRA_MP) &&
258 (IsrStatus & ((u32)XUARTPS_IXR_PARITY | (u32)XUARTPS_IXR_RBRK
259 | (u32)XUARTPS_IXR_FRAMING))) {
260 InstancePtr->is_rxbs_error = 1;
263 * If there are bytes still to be received in the specified buffer
264 * go ahead and receive them. Removing bytes from the RX FIFO will
265 * clear the interrupt.
268 (void)XUartPs_ReceiveBuffer(InstancePtr);
270 if (!(InstancePtr->is_rxbs_error)) {
271 Event = XUARTPS_EVENT_RECV_ERROR;
272 EventData = InstancePtr->ReceiveBuffer.RequestedBytes -
273 InstancePtr->ReceiveBuffer.RemainingBytes;
276 * Call the application handler to indicate that there is a receive
277 * error or a break interrupt, if the application cares about the
278 * error it call a function to get the last errors.
280 InstancePtr->Handler(InstancePtr->CallBackRef,
286 /****************************************************************************/
289 * This function handles the receive timeout interrupt. This interrupt occurs
290 * whenever a number of bytes have been present in the RX FIFO and the receive
291 * data line has been idle for at lease 4 or more character times, (the timeout
292 * is set using XUartPs_SetrecvTimeout() function).
294 * @param InstancePtr is a pointer to the XUartPs instance
300 *****************************************************************************/
301 static void ReceiveTimeoutHandler(XUartPs *InstancePtr)
306 * If there are bytes still to be received in the specified buffer
307 * go ahead and receive them. Removing bytes from the RX FIFO will
308 * clear the interrupt.
310 if (InstancePtr->ReceiveBuffer.RemainingBytes != (u32)0) {
311 (void)XUartPs_ReceiveBuffer(InstancePtr);
315 * If there are no more bytes to receive then indicate that this is
316 * not a receive timeout but the end of the buffer reached, a timeout
317 * normally occurs if # of bytes is not divisible by FIFO threshold,
318 * don't rely on previous test of remaining bytes since receive
319 * function updates it
321 if (InstancePtr->ReceiveBuffer.RemainingBytes != (u32)0) {
322 Event = XUARTPS_EVENT_RECV_TOUT;
324 Event = XUARTPS_EVENT_RECV_DATA;
328 * Call the application handler to indicate that there is a receive
329 * timeout or data event
331 InstancePtr->Handler(InstancePtr->CallBackRef, Event,
332 InstancePtr->ReceiveBuffer.RequestedBytes -
333 InstancePtr->ReceiveBuffer.RemainingBytes);
336 /****************************************************************************/
339 * This function handles the interrupt when data is in RX FIFO.
341 * @param InstancePtr is a pointer to the XUartPs instance
347 *****************************************************************************/
348 static void ReceiveDataHandler(XUartPs *InstancePtr)
351 * If there are bytes still to be received in the specified buffer
352 * go ahead and receive them. Removing bytes from the RX FIFO will
353 * clear the interrupt.
355 if (InstancePtr->ReceiveBuffer.RemainingBytes != (u32)0) {
356 (void)XUartPs_ReceiveBuffer(InstancePtr);
359 /* If the last byte of a message was received then call the application
360 * handler, this code should not use an else from the previous check of
361 * the number of bytes to receive because the call to receive the buffer
362 * updates the bytes ramained
364 if (InstancePtr->ReceiveBuffer.RemainingBytes == (u32)0) {
365 InstancePtr->Handler(InstancePtr->CallBackRef,
366 XUARTPS_EVENT_RECV_DATA,
367 (InstancePtr->ReceiveBuffer.RequestedBytes -
368 InstancePtr->ReceiveBuffer.RemainingBytes));
373 /****************************************************************************/
376 * This function handles the interrupt when data has been sent, the transmit
377 * FIFO is empty (transmitter holding register).
379 * @param InstancePtr is a pointer to the XUartPs instance
380 * @param IsrStatus is the register value for channel status register
386 *****************************************************************************/
387 static void SendDataHandler(XUartPs *InstancePtr, u32 IsrStatus)
391 * If there are not bytes to be sent from the specified buffer then disable
392 * the transmit interrupt so it will stop interrupting as it interrupts
393 * any time the FIFO is empty
395 if (InstancePtr->SendBuffer.RemainingBytes == (u32)0) {
396 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
398 ((u32)XUARTPS_IXR_TXEMPTY | (u32)XUARTPS_IXR_TXFULL));
400 /* Call the application handler to indicate the sending is done */
401 InstancePtr->Handler(InstancePtr->CallBackRef,
402 XUARTPS_EVENT_SENT_DATA,
403 InstancePtr->SendBuffer.RequestedBytes -
404 InstancePtr->SendBuffer.RemainingBytes);
407 /* If TX FIFO is empty, send more. */
408 else if((IsrStatus & ((u32)XUARTPS_IXR_TXEMPTY)) != (u32)0) {
409 (void)XUartPs_SendBuffer(InstancePtr);
412 /* Else with dummy entry for MISRA-C Compliance.*/
417 /****************************************************************************/
420 * This function handles modem interrupts. It does not do any processing
421 * except to call the application handler to indicate a modem event.
423 * @param InstancePtr is a pointer to the XUartPs instance
429 *****************************************************************************/
430 static void ModemHandler(XUartPs *InstancePtr)
435 * Read the modem status register so that the interrupt is acknowledged
436 * and it can be passed to the callback handler with the event
438 MsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
439 XUARTPS_MODEMSR_OFFSET);
442 * Call the application handler to indicate the modem status changed,
443 * passing the modem status and the event data in the call
445 InstancePtr->Handler(InstancePtr->CallBackRef,