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
37 * This file contains the functions for interrupt handling
40 * MODIFICATION HISTORY:
42 * Ver Who Date Changes
43 * ----- ------ -------- -----------------------------------------------
44 * 1.00 drg/jz 01/13/10 First Release
47 *****************************************************************************/
49 /***************************** Include Files ********************************/
53 /************************** Constant Definitions ****************************/
55 /**************************** Type Definitions ******************************/
57 /***************** Macros (Inline Functions) Definitions ********************/
59 /************************** Function Prototypes *****************************/
61 static void ReceiveDataHandler(XUartPs *InstancePtr);
62 static void SendDataHandler(XUartPs *InstancePtr, u32 isrstatus);
63 static void ReceiveErrorHandler(XUartPs *InstancePtr);
64 static void ReceiveTimeoutHandler(XUartPs *InstancePtr);
65 static void ModemHandler(XUartPs *InstancePtr);
68 /* Internal function prototypes implemented in xuartps.c */
69 extern unsigned int XUartPs_ReceiveBuffer(XUartPs *InstancePtr);
70 extern unsigned int XUartPs_SendBuffer(XUartPs *InstancePtr);
72 /************************** Variable Definitions ****************************/
74 typedef void (*Handler)(XUartPs *InstancePtr);
76 /****************************************************************************/
79 * This function gets the interrupt mask
81 * @param InstancePtr is a pointer to the XUartPs instance.
84 * The current interrupt mask. The mask indicates which interupts
89 *****************************************************************************/
90 u32 XUartPs_GetInterruptMask(XUartPs *InstancePtr)
93 * Assert validates the input argument
95 Xil_AssertNonvoid(InstancePtr != NULL);
98 * 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
123 Xil_AssertVoid(InstancePtr != NULL);
125 Mask &= XUARTPS_IXR_MASK;
128 * Write the mask to the IER Register
130 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
131 XUARTPS_IER_OFFSET, Mask);
134 * Write the inverse of the Mask to the IDR register
136 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
137 XUARTPS_IDR_OFFSET, (~Mask));
141 /****************************************************************************/
144 * This function sets the handler that will be called when an event (interrupt)
145 * occurs that needs application's attention.
147 * @param InstancePtr is a pointer to the XUartPs instance
148 * @param FuncPtr is the pointer to the callback function.
149 * @param CallBackRef is the upper layer callback reference passed back
150 * when the callback function is invoked.
156 * There is no assert on the CallBackRef since the driver doesn't know what it
159 *****************************************************************************/
160 void XUartPs_SetHandler(XUartPs *InstancePtr, XUartPs_Handler FuncPtr,
164 * Asserts validate the input arguments
165 * CallBackRef not checked, no way to know what is valid
167 Xil_AssertVoid(InstancePtr != NULL);
168 Xil_AssertVoid(FuncPtr != NULL);
169 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
171 InstancePtr->Handler = FuncPtr;
172 InstancePtr->CallBackRef = CallBackRef;
175 /****************************************************************************/
178 * This function is the interrupt handler for the driver.
179 * It must be connected to an interrupt system by the application such that it
180 * can be called when an interrupt occurs.
182 * @param InstancePtr contains a pointer to the driver instance
188 ******************************************************************************/
189 void XUartPs_InterruptHandler(XUartPs *InstancePtr)
193 Xil_AssertVoid(InstancePtr != NULL);
194 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
197 * Read the interrupt ID register to determine which
198 * interrupt is active
200 IsrStatus = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
203 IsrStatus &= XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
207 * Dispatch an appropiate handler.
209 if(0 != (IsrStatus & (XUARTPS_IXR_RXOVR | XUARTPS_IXR_RXEMPTY |
210 XUARTPS_IXR_RXFULL))) {
211 /* Recieved data interrupt */
212 ReceiveDataHandler(InstancePtr);
215 if(0 != (IsrStatus & (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL))) {
216 /* Transmit data interrupt */
217 SendDataHandler(InstancePtr, IsrStatus);
220 if(0 != (IsrStatus & (XUARTPS_IXR_OVER | XUARTPS_IXR_FRAMING |
221 XUARTPS_IXR_PARITY))) {
222 /* Recieved Error Status interrupt */
223 ReceiveErrorHandler(InstancePtr);
226 if(0 != (IsrStatus & XUARTPS_IXR_TOUT )) {
227 /* Recieved Timeout interrupt */
228 ReceiveTimeoutHandler(InstancePtr);
231 if(0 != (IsrStatus & XUARTPS_IXR_DMS)) {
232 /* Modem status interrupt */
233 ModemHandler(InstancePtr);
237 * Clear the interrupt status.
239 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_ISR_OFFSET,
244 /****************************************************************************/
247 * This function handles interrupts for receive errors which include
248 * overrun errors, framing errors, parity errors, and the break interrupt.
250 * @param InstancePtr is a pointer to the XUartPs instance.
256 *****************************************************************************/
257 static void ReceiveErrorHandler(XUartPs *InstancePtr)
260 * If there are bytes still to be received in the specified buffer
261 * go ahead and receive them. Removing bytes from the RX FIFO will
262 * clear the interrupt.
264 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
265 XUartPs_ReceiveBuffer(InstancePtr);
269 * Call the application handler to indicate that there is a receive
270 * error or a break interrupt, if the application cares about the
271 * error it call a function to get the last errors.
273 InstancePtr->Handler(InstancePtr->CallBackRef,
274 XUARTPS_EVENT_RECV_ERROR,
275 (InstancePtr->ReceiveBuffer.RequestedBytes -
276 InstancePtr->ReceiveBuffer.RemainingBytes));
279 /****************************************************************************/
282 * This function handles the receive timeout interrupt. This interrupt occurs
283 * whenever a number of bytes have been present in the RX FIFO and the receive
284 * data line has been idle for at lease 4 or more character times, (the timeout
285 * is set using XUartPs_SetrecvTimeout() function).
287 * @param InstancePtr is a pointer to the XUartPs instance
293 *****************************************************************************/
294 static void ReceiveTimeoutHandler(XUartPs *InstancePtr)
299 * If there are bytes still to be received in the specified buffer
300 * go ahead and receive them. Removing bytes from the RX FIFO will
301 * clear the interrupt.
303 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
304 XUartPs_ReceiveBuffer(InstancePtr);
308 * If there are no more bytes to receive then indicate that this is
309 * not a receive timeout but the end of the buffer reached, a timeout
310 * normally occurs if # of bytes is not divisible by FIFO threshold,
311 * don't rely on previous test of remaining bytes since receive
312 * function updates it
314 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
315 Event = XUARTPS_EVENT_RECV_TOUT;
317 Event = XUARTPS_EVENT_RECV_DATA;
321 * Call the application handler to indicate that there is a receive
322 * timeout or data event
324 InstancePtr->Handler(InstancePtr->CallBackRef, Event,
325 InstancePtr->ReceiveBuffer.RequestedBytes -
326 InstancePtr->ReceiveBuffer.RemainingBytes);
329 /****************************************************************************/
332 * This function handles the interrupt when data is in RX FIFO.
334 * @param InstancePtr is a pointer to the XUartPs instance
340 *****************************************************************************/
341 static void ReceiveDataHandler(XUartPs *InstancePtr)
344 * If there are bytes still to be received in the specified buffer
345 * go ahead and receive them. Removing bytes from the RX FIFO will
346 * clear the interrupt.
348 if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
349 XUartPs_ReceiveBuffer(InstancePtr);
353 /* If the last byte of a message was received then call the application
354 * handler, this code should not use an else from the previous check of
355 * the number of bytes to receive because the call to receive the buffer
356 * updates the bytes ramained
358 if (InstancePtr->ReceiveBuffer.RemainingBytes == 0) {
359 InstancePtr->Handler(InstancePtr->CallBackRef,
360 XUARTPS_EVENT_RECV_DATA,
361 (InstancePtr->ReceiveBuffer.RequestedBytes -
362 InstancePtr->ReceiveBuffer.RemainingBytes));
367 /****************************************************************************/
370 * This function handles the interrupt when data has been sent, the transmit
371 * FIFO is empty (transmitter holding register).
373 * @param InstancePtr is a pointer to the XUartPs instance
374 * @param IsrStatus is the register value for channel status register
380 *****************************************************************************/
381 static void SendDataHandler(XUartPs *InstancePtr, u32 IsrStatus)
385 * If there are not bytes to be sent from the specified buffer then disable
386 * the transmit interrupt so it will stop interrupting as it interrupts
387 * any time the FIFO is empty
389 if (InstancePtr->SendBuffer.RemainingBytes == 0) {
390 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
392 (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL));
394 /* Call the application handler to indicate the sending is done */
395 InstancePtr->Handler(InstancePtr->CallBackRef,
396 XUARTPS_EVENT_SENT_DATA,
397 InstancePtr->SendBuffer.RequestedBytes -
398 InstancePtr->SendBuffer.RemainingBytes);
402 * If TX FIFO is empty, send more.
404 else if(IsrStatus & XUARTPS_IXR_TXEMPTY) {
405 XUartPs_SendBuffer(InstancePtr);
410 /****************************************************************************/
413 * This function handles modem interrupts. It does not do any processing
414 * except to call the application handler to indicate a modem event.
416 * @param InstancePtr is a pointer to the XUartPs instance
422 *****************************************************************************/
423 static void ModemHandler(XUartPs *InstancePtr)
428 * Read the modem status register so that the interrupt is acknowledged
429 * and it can be passed to the callback handler with the event
431 MsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
432 XUARTPS_MODEMSR_OFFSET);
435 * Call the application handler to indicate the modem status changed,
436 * passing the modem status and the event data in the call
438 InstancePtr->Handler(InstancePtr->CallBackRef,