]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo_bsp/ps7_cortexa9_0/libsrc/uartps_v2_1/src/xuartps_intr.c
Preparing for next release...
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo_bsp / ps7_cortexa9_0 / libsrc / uartps_v2_1 / src / xuartps_intr.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2010 - 2014 Xilinx, Inc.  All rights reserved.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
18 *
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
25 * SOFTWARE.
26 *
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.
30 *
31 ******************************************************************************/
32 /****************************************************************************/
33 /**
34 *
35 * @file xuartps_intr.c
36 *
37 * This file contains the functions for interrupt handling
38 *
39 * <pre>
40 * MODIFICATION HISTORY:
41 *
42 * Ver   Who    Date     Changes
43 * ----- ------ -------- -----------------------------------------------
44 * 1.00  drg/jz 01/13/10 First Release
45 * </pre>
46 *
47 *****************************************************************************/
48
49 /***************************** Include Files ********************************/
50
51 #include "xuartps.h"
52
53 /************************** Constant Definitions ****************************/
54
55 /**************************** Type Definitions ******************************/
56
57 /***************** Macros (Inline Functions) Definitions ********************/
58
59 /************************** Function Prototypes *****************************/
60
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);
66
67
68 /* Internal function prototypes implemented in xuartps.c */
69 extern unsigned int XUartPs_ReceiveBuffer(XUartPs *InstancePtr);
70 extern unsigned int XUartPs_SendBuffer(XUartPs *InstancePtr);
71
72 /************************** Variable Definitions ****************************/
73
74 typedef void (*Handler)(XUartPs *InstancePtr);
75
76 /****************************************************************************/
77 /**
78 *
79 * This function gets the interrupt mask
80 *
81 * @param        InstancePtr is a pointer to the XUartPs instance.
82 *
83 * @return
84 *               The current interrupt mask. The mask indicates which interupts
85 *               are enabled.
86 *
87 * @note         None.
88 *
89 *****************************************************************************/
90 u32 XUartPs_GetInterruptMask(XUartPs *InstancePtr)
91 {
92         /*
93          * Assert validates the input argument
94          */
95         Xil_AssertNonvoid(InstancePtr != NULL);
96
97         /*
98          * Read the Interrupt Mask register
99          */
100         return (XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
101                          XUARTPS_IMR_OFFSET));
102 }
103
104 /****************************************************************************/
105 /**
106 *
107 * This function sets the interrupt mask.
108 *
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.
112 *
113 * @return       None.
114 *
115 * @note         None.
116 *
117 *****************************************************************************/
118 void XUartPs_SetInterruptMask(XUartPs *InstancePtr, u32 Mask)
119 {
120         /*
121          * Assert validates the input arguments
122          */
123         Xil_AssertVoid(InstancePtr != NULL);
124
125         Mask &= XUARTPS_IXR_MASK;
126
127         /*
128          * Write the mask to the IER Register
129          */
130         XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
131                  XUARTPS_IER_OFFSET, Mask);
132
133         /*
134          * Write the inverse of the Mask to the IDR register
135          */
136         XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
137                  XUARTPS_IDR_OFFSET, (~Mask));
138
139 }
140
141 /****************************************************************************/
142 /**
143 *
144 * This function sets the handler that will be called when an event (interrupt)
145 * occurs that needs application's attention.
146 *
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.
151 *
152 * @return       None.
153 *
154 * @note
155 *
156 * There is no assert on the CallBackRef since the driver doesn't know what it
157 * is (nor should it)
158 *
159 *****************************************************************************/
160 void XUartPs_SetHandler(XUartPs *InstancePtr, XUartPs_Handler FuncPtr,
161                  void *CallBackRef)
162 {
163         /*
164          * Asserts validate the input arguments
165          * CallBackRef not checked, no way to know what is valid
166          */
167         Xil_AssertVoid(InstancePtr != NULL);
168         Xil_AssertVoid(FuncPtr != NULL);
169         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
170
171         InstancePtr->Handler = FuncPtr;
172         InstancePtr->CallBackRef = CallBackRef;
173 }
174
175 /****************************************************************************/
176 /**
177 *
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.
181 *
182 * @param        InstancePtr contains a pointer to the driver instance
183 *
184 * @return       None.
185 *
186 * @note         None.
187 *
188 ******************************************************************************/
189 void XUartPs_InterruptHandler(XUartPs *InstancePtr)
190 {
191         u32 IsrStatus;
192
193         Xil_AssertVoid(InstancePtr != NULL);
194         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
195
196         /*
197          * Read the interrupt ID register to determine which
198          * interrupt is active
199          */
200         IsrStatus = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
201                                    XUARTPS_IMR_OFFSET);
202
203         IsrStatus &= XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
204                                    XUARTPS_ISR_OFFSET);
205
206         /*
207          * Dispatch an appropiate handler.
208          */
209         if(0 != (IsrStatus & (XUARTPS_IXR_RXOVR | XUARTPS_IXR_RXEMPTY |
210                                  XUARTPS_IXR_RXFULL))) {
211                 /* Recieved data interrupt */
212                 ReceiveDataHandler(InstancePtr);
213         }
214
215         if(0 != (IsrStatus & (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL))) {
216                 /* Transmit data interrupt */
217                 SendDataHandler(InstancePtr, IsrStatus);
218         }
219
220         if(0 != (IsrStatus & (XUARTPS_IXR_OVER | XUARTPS_IXR_FRAMING |
221                                 XUARTPS_IXR_PARITY))) {
222                 /* Recieved Error Status interrupt */
223                 ReceiveErrorHandler(InstancePtr);
224         }
225
226         if(0 != (IsrStatus & XUARTPS_IXR_TOUT )) {
227                 /* Recieved Timeout interrupt */
228                 ReceiveTimeoutHandler(InstancePtr);
229         }
230
231         if(0 != (IsrStatus & XUARTPS_IXR_DMS)) {
232                 /* Modem status interrupt */
233                 ModemHandler(InstancePtr);
234         }
235
236         /*
237          * Clear the interrupt status.
238          */
239         XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_ISR_OFFSET,
240                 IsrStatus);
241
242 }
243
244 /****************************************************************************/
245 /*
246 *
247 * This function handles interrupts for receive errors which include
248 * overrun errors, framing errors, parity errors, and the break interrupt.
249 *
250 * @param        InstancePtr is a pointer to the XUartPs instance.
251 *
252 * @return       None.
253 *
254 * @note         None.
255 *
256 *****************************************************************************/
257 static void ReceiveErrorHandler(XUartPs *InstancePtr)
258 {
259         /*
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.
263          */
264         if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
265                 XUartPs_ReceiveBuffer(InstancePtr);
266         }
267
268         /*
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.
272          */
273         InstancePtr->Handler(InstancePtr->CallBackRef,
274                                 XUARTPS_EVENT_RECV_ERROR,
275                                 (InstancePtr->ReceiveBuffer.RequestedBytes -
276                                 InstancePtr->ReceiveBuffer.RemainingBytes));
277
278 }
279 /****************************************************************************/
280 /**
281 *
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).
286 *
287 * @param        InstancePtr is a pointer to the XUartPs instance
288 *
289 * @return       None.
290 *
291 * @note         None.
292 *
293 *****************************************************************************/
294 static void ReceiveTimeoutHandler(XUartPs *InstancePtr)
295 {
296         u32 Event;
297
298         /*
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.
302          */
303         if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
304                 XUartPs_ReceiveBuffer(InstancePtr);
305         }
306
307         /*
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
313          */
314         if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
315                 Event = XUARTPS_EVENT_RECV_TOUT;
316         } else {
317                 Event = XUARTPS_EVENT_RECV_DATA;
318         }
319
320         /*
321          * Call the application handler to indicate that there is a receive
322          * timeout or data event
323          */
324         InstancePtr->Handler(InstancePtr->CallBackRef, Event,
325                                  InstancePtr->ReceiveBuffer.RequestedBytes -
326                                  InstancePtr->ReceiveBuffer.RemainingBytes);
327
328 }
329 /****************************************************************************/
330 /**
331 *
332 * This function handles the interrupt when data is in RX FIFO.
333 *
334 * @param        InstancePtr is a pointer to the XUartPs instance
335 *
336 * @return       None.
337 *
338 * @note         None.
339 *
340 *****************************************************************************/
341 static void ReceiveDataHandler(XUartPs *InstancePtr)
342 {
343         /*
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.
347          */
348          if (InstancePtr->ReceiveBuffer.RemainingBytes != 0) {
349                 XUartPs_ReceiveBuffer(InstancePtr);
350         }
351
352
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
357          */
358         if (InstancePtr->ReceiveBuffer.RemainingBytes == 0) {
359                 InstancePtr->Handler(InstancePtr->CallBackRef,
360                                 XUARTPS_EVENT_RECV_DATA,
361                                 (InstancePtr->ReceiveBuffer.RequestedBytes -
362                                 InstancePtr->ReceiveBuffer.RemainingBytes));
363         }
364
365 }
366
367 /****************************************************************************/
368 /**
369 *
370 * This function handles the interrupt when data has been sent, the transmit
371 * FIFO is empty (transmitter holding register).
372 *
373 * @param        InstancePtr is a pointer to the XUartPs instance
374 * @param        IsrStatus is the register value for channel status register
375 *
376 * @return       None.
377 *
378 * @note         None.
379 *
380 *****************************************************************************/
381 static void SendDataHandler(XUartPs *InstancePtr, u32 IsrStatus)
382 {
383
384         /*
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
388          */
389         if (InstancePtr->SendBuffer.RemainingBytes == 0) {
390                 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
391                                 XUARTPS_IDR_OFFSET,
392                                 (XUARTPS_IXR_TXEMPTY | XUARTPS_IXR_TXFULL));
393
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);
399         }
400
401         /*
402          * If TX FIFO is empty, send more.
403          */
404         else if(IsrStatus & XUARTPS_IXR_TXEMPTY) {
405                 XUartPs_SendBuffer(InstancePtr);
406         }
407
408 }
409
410 /****************************************************************************/
411 /**
412 *
413 * This function handles modem interrupts.  It does not do any processing
414 * except to call the application handler to indicate a modem event.
415 *
416 * @param        InstancePtr is a pointer to the XUartPs instance
417 *
418 * @return       None.
419 *
420 * @note         None.
421 *
422 *****************************************************************************/
423 static void ModemHandler(XUartPs *InstancePtr)
424 {
425         u32 MsrRegister;
426
427         /*
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
430          */
431         MsrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
432                           XUARTPS_MODEMSR_OFFSET);
433
434         /*
435          * Call the application handler to indicate the modem status changed,
436          * passing the modem status and the event data in the call
437          */
438         InstancePtr->Handler(InstancePtr->CallBackRef,
439                                   XUARTPS_EVENT_MODEM,
440                                   MsrRegister);
441
442 }
443