]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Kintex7_EthernetLite/BSP/microblaze_0/libsrc/emaclite_v4_2/src/xemaclite_intr.c
Update the Microblaze hardware design and BSP to the latest IP and tool versions.
[freertos] / FreeRTOS / Demo / MicroBlaze_Kintex7_EthernetLite / BSP / microblaze_0 / libsrc / emaclite_v4_2 / src / xemaclite_intr.c
1 /******************************************************************************
2 *
3 * Copyright (C) 2004 - 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 xemaclite_intr.c
36 * @addtogroup emaclite_v4_1
37 * @{
38 *
39 * Functions in this file are for the interrupt driven processing functionality.
40 * See xemaclite.h for a detailed description of the driver.
41 *
42 * <pre>
43 * MODIFICATION HISTORY:
44 *
45 * Ver   Who  Date     Changes
46 * ----- ---- -------- --------------------------------------------------------
47 * 1.01a ecm  03/31/04 First release
48 * 1.11a mta  03/21/07 Updated to new coding style
49 * 2.01a ktn  07/20/09 Modified the XEmacLite_EnableInterrupts and
50 *                     XEmacLite_DisableInterrupts functions to enable/disable
51 *                     the interrupt in the Ping buffer as this is used to enable
52 *                     the interrupts for both Ping and Pong Buffers.
53 *                     The interrupt enable bit in the Pong buffer is not used by
54 *                     the HW.
55 * 3.00a ktn  10/22/09 Updated file to use the HAL Processor APIs/macros.
56 *                     The macros have been renamed to remove _m from the name.
57 * 4.2   sk   11/10/15 Used UINTPTR instead of u32 for Baseaddress CR# 867425.
58 *
59 * </pre>
60 ******************************************************************************/
61
62 /***************************** Include Files *********************************/
63
64 #include "xemaclite_i.h"
65 #include "xil_io.h"
66 #include "xemaclite.h"
67
68 /************************** Constant Definitions *****************************/
69
70 /**************************** Type Definitions *******************************/
71
72 /***************** Macros (Inline Functions) Definitions *********************/
73
74 /************************** Function Prototypes ******************************/
75
76 /************************** Variable Definitions *****************************/
77 /*****************************************************************************/
78 /**
79 *
80 * Enable the EmacLite Interrupts.
81 *
82 * This function must be called before other functions to send or receive data
83 * in interrupt driven mode. The user should have connected the
84 * interrupt handler of the driver to an interrupt source such as an interrupt
85 * controller or the processor interrupt prior to this function being called.
86 *
87 * @param        InstancePtr is a pointer to the XEmacLite instance.
88 *
89 * @return
90 *               - XST_SUCCESS if the device interrupts were enabled
91 *                       successfully.
92 *               - XST_NO_CALLBACK if the callbacks were not set.
93 *
94 * @note         None.
95 *
96 ******************************************************************************/
97 int XEmacLite_EnableInterrupts(XEmacLite *InstancePtr)
98 {
99         u32 Register;
100         UINTPTR BaseAddress;
101
102         /*
103          * Verify that each of the inputs are valid.
104          */
105         Xil_AssertNonvoid(InstancePtr != NULL);
106         Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
107
108         BaseAddress = InstancePtr->EmacLiteConfig.BaseAddress;
109
110         /*
111          * Verify that the handlers are in place.
112          */
113         if ((InstancePtr->RecvHandler == (XEmacLite_Handler) StubHandler) ||
114             (InstancePtr->SendHandler == (XEmacLite_Handler) StubHandler)) {
115                 return XST_NO_CALLBACK;
116         }
117
118         /*
119          * Enable the TX interrupts for both the buffers, the Interrupt Enable
120          * is common for the both the buffers and is defined in the
121          * Ping buffer.
122          */
123         Register = XEmacLite_GetTxStatus(BaseAddress);
124         Register |= XEL_TSR_XMIT_IE_MASK;
125         XEmacLite_SetTxStatus(BaseAddress, Register);
126
127         /*
128          * Enable the RX interrupts for both the buffers, the Interrupt Enable
129          * is common for the both the buffers and is defined in the
130          * Ping buffer.
131          */
132         Register = XEmacLite_GetRxStatus(BaseAddress);
133         Register |= XEL_RSR_RECV_IE_MASK;
134         XEmacLite_SetRxStatus(BaseAddress, Register);
135
136         /*
137          * Enable the global interrupt output.
138          */
139         XEmacLite_WriteReg(BaseAddress, XEL_GIER_OFFSET, XEL_GIER_GIE_MASK);
140
141         return XST_SUCCESS;
142 }
143
144 /*****************************************************************************/
145 /**
146 *
147 * Disables the interrupts from the device (the higher layer software is
148 * responsible for disabling interrupts at the interrupt controller).
149 *
150 * To start using the device again, _EnableInterrupts must be called.
151 *
152 * @param        InstancePtr is a pointer to the XEmacLite instance .
153 *
154 * @return       None.
155 *
156 * @note         None.
157 *
158 ******************************************************************************/
159 void XEmacLite_DisableInterrupts(XEmacLite *InstancePtr)
160 {
161         u32 Register;
162         UINTPTR BaseAddress;
163
164         /*
165          * Verify that each of the inputs are valid.
166          */
167         Xil_AssertVoid(InstancePtr != NULL);
168         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
169
170
171         BaseAddress = InstancePtr->EmacLiteConfig.BaseAddress;
172
173         /*
174          * Disable the global interrupt output.
175          */
176         XEmacLite_WriteReg(BaseAddress, XEL_GIER_OFFSET, 0);
177
178         /*
179          * Disable the TX interrupts for both the buffers, the Interrupt Enable
180          * is common for the both the buffers and is defined in the
181          * Ping buffer.
182          */
183         Register = XEmacLite_GetTxStatus(BaseAddress);
184         Register &= ~XEL_TSR_XMIT_IE_MASK;
185         XEmacLite_SetTxStatus(BaseAddress, Register);
186
187         /*
188          * Disable the RX interrupts for both the buffers, the Interrupt Enable
189          * is common for the both the buffers and is defined in the
190          * Ping buffer.
191          */
192         Register = XEmacLite_GetRxStatus(BaseAddress);
193         Register &= ~XEL_RSR_RECV_IE_MASK;
194         XEmacLite_SetRxStatus(BaseAddress, Register);
195
196 }
197
198 /*****************************************************************************/
199 /**
200 *
201 * Interrupt handler for the EmacLite driver. It performs the following
202 * processing:
203 *
204 *       - Get the interrupt status from the registers to determine the source
205 *       of the interrupt.
206 *       - Call the appropriate handler based on the source of the interrupt.
207 *
208 * @param        InstancePtr contains a pointer to the EmacLite device instance
209 *               for the interrupt.
210 *
211 * @return       None.
212 *
213 * @note         None.
214 *
215 *
216 ******************************************************************************/
217 void XEmacLite_InterruptHandler(void *InstancePtr)
218 {
219
220         XEmacLite *EmacLitePtr;
221         int TxCompleteIntr = FALSE;
222         UINTPTR BaseAddress;
223         u32 TxStatus;
224
225         /*
226          * Verify that each of the inputs are valid.
227          */
228         Xil_AssertVoid(InstancePtr != NULL);
229
230         /*
231          * Convert the non-typed pointer to an EmacLite instance pointer
232          * such that there is access to the device.
233          */
234         EmacLitePtr = (XEmacLite *) InstancePtr;
235         BaseAddress = EmacLitePtr->EmacLiteConfig.BaseAddress;
236
237         if ((XEmacLite_IsRxEmpty(BaseAddress) != TRUE) ||
238                 (XEmacLite_IsRxEmpty(BaseAddress +
239                         XEL_BUFFER_OFFSET) != TRUE)) {
240                 /*
241                  * Call the RX callback.
242                  */
243                 EmacLitePtr->RecvHandler(EmacLitePtr->RecvRef);
244
245         }
246
247         TxStatus = XEmacLite_GetTxStatus(BaseAddress);
248         if (((TxStatus & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
249                 (TxStatus & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
250
251                 /*
252                  * Clear the Tx Active bit in the Tx Status Register.
253                  */
254                 TxStatus &= ~XEL_TSR_XMIT_ACTIVE_MASK;
255                 XEmacLite_SetTxStatus(BaseAddress, TxStatus);
256
257                 /*
258                  * Update the flag indicating that there was a Tx Interrupt.
259                  */
260                 TxCompleteIntr = TRUE;
261
262         }
263
264         TxStatus = XEmacLite_GetTxStatus(BaseAddress + XEL_BUFFER_OFFSET);
265         if (((TxStatus & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
266                 (TxStatus & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
267
268                 /*
269                  * Clear the Tx Active bit in the Tx Status Register.
270                  */
271                 TxStatus &= ~XEL_TSR_XMIT_ACTIVE_MASK;
272                 XEmacLite_SetTxStatus(BaseAddress + XEL_BUFFER_OFFSET,
273                                         TxStatus);
274                 /*
275                  * Update the flag indicating that there was a Tx Interrupt.
276                  */
277                 TxCompleteIntr = TRUE;
278         }
279
280         /*
281          * If there was a TX interrupt, call the callback.
282          */
283         if (TxCompleteIntr == TRUE) {
284
285                 /*
286                  * Call the TX callback.
287                  */
288                 EmacLitePtr->SendHandler(EmacLitePtr->SendRef);
289
290         }
291 }
292
293 /*****************************************************************************/
294 /**
295 *
296 * Sets the callback function for handling received frames in interrupt mode.
297 * The upper layer software should call this function during initialization.
298 * The callback is called when a frame is received. The callback function
299 * should communicate the data to a thread such that the processing is not
300 * performed in an interrupt context.
301 *
302 * The callback is invoked by the driver within interrupt context, so it needs
303 * to do its job quickly. If there are other potentially slow operations
304 * within the callback, these should be done at task-level.
305 *
306 * @param        InstancePtr is a pointer to the XEmacLite instance..
307 * @param        CallBackRef is a reference pointer to be passed back to the
308 *               application in the callback. This helps the application
309 *               correlate the callback to a particular driver.
310 * @param        FuncPtr is the pointer to the callback function.
311 *
312 * @return       None.
313 *
314 * @note         None.
315 *
316 ******************************************************************************/
317 void XEmacLite_SetRecvHandler(XEmacLite *InstancePtr, void *CallBackRef,
318                               XEmacLite_Handler FuncPtr)
319 {
320         Xil_AssertVoid(InstancePtr != NULL);
321         Xil_AssertVoid(FuncPtr != NULL);
322         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
323
324         InstancePtr->RecvHandler = FuncPtr;
325         InstancePtr->RecvRef = CallBackRef;
326 }
327
328
329 /*****************************************************************************/
330 /**
331 *
332 * Sets the callback function for handling transmitted frames in interrupt mode.
333 * The upper layer software should call this function during initialization.
334 * The callback is called when a frame is transmitted. The callback function
335 * should communicate the data to a thread such that the processing is not
336 * performed in an interrupt context.
337 *
338 * The callback is invoked by the driver within interrupt context, so it needs
339 * to do its job quickly. If there are other potentially slow operations
340 * within the callback, these should be done at task-level.
341 *
342 * @param        InstancePtr is a pointer to the XEmacLite instance.
343 * @param        CallBackRef is a reference pointer to be passed back to the
344 *               application in the callback. This helps the application
345 *               correlate the callback to a particular driver.
346 * @param        FuncPtr is the pointer to the callback function.
347 *
348 * @return       None.
349 *
350 * @note         None.
351 *
352 ******************************************************************************/
353 void XEmacLite_SetSendHandler(XEmacLite *InstancePtr, void *CallBackRef,
354                               XEmacLite_Handler FuncPtr)
355 {
356         Xil_AssertVoid(InstancePtr != NULL);
357         Xil_AssertVoid(FuncPtr != NULL);
358         Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
359
360         InstancePtr->SendHandler = FuncPtr;
361         InstancePtr->SendRef = CallBackRef;
362 }
363 /** @} */