bb3a348c4b1c4184a184f99f6d5e35055fe40a44
[keystone-rtos/edma3_lld.git] / packages / ti / sdo / edma3 / drv / sample / src / bios6_edma3_drv_sample_init.c
1 /*******************************************************************************
2 **+--------------------------------------------------------------------------+**
3 **|                            ****                                          |**
4 **|                            ****                                          |**
5 **|                            ******o***                                    |**
6 **|                      ********_///_****                                   |**
7 **|                      ***** /_//_/ ****                                   |**
8 **|                       ** ** (__/ ****                                    |**
9 **|                           *********                                      |**
10 **|                            ****                                          |**
11 **|                            ***                                           |**
12 **|                                                                          |**
13 **|         Copyright (c) 1998-2006 Texas Instruments Incorporated           |**
14 **|                        ALL RIGHTS RESERVED                               |**
15 **|                                                                          |**
16 **| Permission is hereby granted to licensees of Texas Instruments           |**
17 **| Incorporated (TI) products to use this computer program for the sole     |**
18 **| purpose of implementing a licensee product based on TI products.         |**
19 **| No other rights to reproduce, use, or disseminate this computer          |**
20 **| program, whether in part or in whole, are granted.                       |**
21 **|                                                                          |**
22 **| TI makes no representation or warranties with respect to the             |**
23 **| performance of this computer program, and specifically disclaims         |**
24 **| any responsibility for any damages, special or consequential,            |**
25 **| connected with the use of this program.                                  |**
26 **|                                                                          |**
27 **+--------------------------------------------------------------------------+**
28 *******************************************************************************/
30 /** \file   bios6_edma3_drv_sample_init.c
32     \brief  Sample Initialization for the EDMA3 Driver for BIOS 6 based
33                 applications. It should be MANDATORILY done once before EDMA3 usage.
35     (C) Copyright 2006, Texas Instruments, Inc
37     \version    1.0   Anuj Aggarwal         - Created
39  */
41 #include <ti/sysbios/hal/Hwi.h>
42 #include <ti/sysbios/ipc/Semaphore.h>
43 #include <ti/sysbios/family/c64p/EventCombiner.h>
45 #include <ti/sdo/edma3/drv/sample/bios6_edma3_drv_sample.h>
47 /** @brief EDMA3 Driver Handle, used to call all the Driver APIs */
48 EDMA3_DRV_Handle hEdma = NULL;
50 /** @brief EDMA3 Driver Instance specific Semaphore handle */
51 static EDMA3_OS_Sem_Handle semHandle = NULL;
53 /**
54   * EDMA3 TC ISRs which need to be registered with the underlying OS by the user
55   * (Not all TC error ISRs need to be registered, register only for the
56   * available Transfer Controllers).
57   */
58 void (*ptrEdma3TcIsrHandler[EDMA3_MAX_TC])(unsigned int arg) =
59                                                 {
60                                                 &lisrEdma3TC0ErrHandler0,
61                                                 &lisrEdma3TC1ErrHandler0,
62                                                 &lisrEdma3TC2ErrHandler0,
63                                                 &lisrEdma3TC3ErrHandler0,
64                                                 &lisrEdma3TC4ErrHandler0,
65                                                 &lisrEdma3TC5ErrHandler0,
66                                                 &lisrEdma3TC6ErrHandler0,
67                                                 &lisrEdma3TC7ErrHandler0,
68                                                 };
71 /**  To Register the ISRs with the underlying OS, if required. */
72 static void registerEdma3Interrupts (void);
73 /**  To Unregister the ISRs with the underlying OS, if previously registered. */
74 static void unregisterEdma3Interrupts (void);
76 /* External Global Configuration Structure */
77 extern EDMA3_DRV_GblConfigParams sampleEdma3GblCfgParams;
79 /* External Instance Specific Configuration Structure */
80 extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig;
83 /**
84  * \brief   EDMA3 Initialization
85  *
86  * This function initializes the EDMA3 Driver and registers the
87  * interrupt handlers.
88  *
89   * \return  EDMA3_DRV_SOK if success, else error code
90  */
91  EDMA3_DRV_Result edma3init (void)
92     {
93     unsigned int edma3InstanceId = 0;
94     EDMA3_DRV_InitConfig initCfg;
95     EDMA3_DRV_Result    edma3Result = EDMA3_DRV_SOK;
96     Semaphore_Params semParams;
97     EDMA3_DRV_GblConfigParams *globalConfig = &sampleEdma3GblCfgParams;
98     EDMA3_DRV_InstanceInitConfig *instanceConfig = &sampleInstInitConfig;
99     EDMA3_RM_MiscParam miscParam;
101     if (NULL == hEdma)
102         {
103         /* configuration structure for the Driver */
104         initCfg.isMaster    =    TRUE;
105         initCfg.regionId = (EDMA3_RM_RegionId)1u;
106         initCfg.drvSemHandle = NULL;
108         /* Driver instance specific config NULL */
109         initCfg.drvInstInitConfig = instanceConfig;
110         initCfg.gblerrCb = NULL;
111         initCfg.gblerrData = NULL;
113         miscParam.isSlave = FALSE;
115         /* Create EDMA3 Driver Object first. */
116         edma3Result = EDMA3_DRV_create (edma3InstanceId, globalConfig, (void *)&miscParam);
118         if (edma3Result != EDMA3_DRV_SOK)
119             {
120 #ifdef EDMA3_DRV_DEBUG
121             EDMA3_DRV_PRINTF("edma3init: EDMA3_DRV_create FAILED\r\n");
122 #endif
123             }
124         else
125             {
126             /**
127               * Driver Object created successfully.
128               * Create a semaphore now for driver instance.
129               */
130                         Semaphore_Params_init(&semParams);
132             edma3Result = edma3OsSemCreate(1, &semParams, &initCfg.drvSemHandle);
133             if (edma3Result != EDMA3_DRV_SOK)
134                 {
135 #ifdef EDMA3_DRV_DEBUG
136                 EDMA3_DRV_PRINTF("edma3init: edma3OsSemCreate FAILED\r\n");
137 #endif
138                 }
139             else
140                 {
141                 /* Save the semaphore handle for future use */
142                 semHandle = initCfg.drvSemHandle;
144                 /* Open the Driver Instance */
145                 hEdma = EDMA3_DRV_open (edma3InstanceId, (void *) &initCfg,
146                                         &edma3Result);
147                 if(NULL == hEdma)
148                     {
149 #ifdef EDMA3_DRV_DEBUG
150                     EDMA3_DRV_PRINTF("edma3init: EDMA3_DRV_open FAILED\r\n");
151 #endif
152                     }
153                 else
154                     {
155                     /**
156                      * Register Interrupt Handlers for various interrupts
157                      * like transfer completion interrupt, CC error
158                      * interrupt, TC error interrupts etc, if required.
159                      */
160                     registerEdma3Interrupts();
161                     }
162                }
163             }
164         }
165     else
166         {
167         /* EDMA3 Driver already initialized, no need to do that again. */
168 #ifdef EDMA3_DRV_DEBUG
169         EDMA3_DRV_PRINTF("edma3init: EDMA3 Driver Already Initialized...Init failed\r\n");
170 #endif
171         edma3Result = EDMA3_DRV_E_INVALID_STATE;
172         }
174      return edma3Result;
175     }
178 /**  To Register the ISRs with the underlying OS, if required. */
179 static void registerEdma3Interrupts (void)
180     {
181         static UInt32 cookie = 0;
182     unsigned int numTc = 0;
184     /* Disabling the global interrupts */
185     cookie = Hwi_disable();
187     /* Enable the Xfer Completion Event Interrupt */
188     EventCombiner_dispatchPlug(ccXferCompInt, (EventCombiner_FuncPtr)(&lisrEdma3ComplHandler0),
189                                                 NULL, 0);
190     EventCombiner_enableEvent(ccXferCompInt);
192     /* Enable the CC Error Event Interrupt */
193     EventCombiner_dispatchPlug(ccErrorInt, (EventCombiner_FuncPtr)(&lisrEdma3CCErrHandler0),
194                                                                 NULL, 0);
195     EventCombiner_enableEvent(ccErrorInt);
197     /* Enable the TC Error Event Interrupt, according to the number of TCs. */
198     while (numTc < numEdma3Tc)
199             {
200         EventCombiner_dispatchPlug(tcErrorInt[numTc],
201                             (EventCombiner_FuncPtr)(ptrEdma3TcIsrHandler[numTc]),
202                             NULL, 0);
203         EventCombiner_enableEvent(tcErrorInt[numTc]);
204         numTc++;
205         }
208    /**
209     * Enabling the HWI_ID.
210     * EDMA3 interrupts (transfer completion, CC error etc.)
211     * correspond to different ECM events (SoC specific). These ECM events come
212     * under ECM block XXX (handling those specific ECM events). Normally, block
213     * 0 handles events 4-31 (events 0-3 are reserved), block 1 handles events
214     * 32-63 and so on. This ECM block XXX (or interrupt selection number XXX)
215     * is mapped to a specific HWI_INT YYY in the tcf file. So to enable this
216     * mapped HWI_INT YYY, one should use the corresponding bitmask in the
217     * API C64_enableIER(), in which the YYY bit is SET.
218     */
219         Hwi_enableInterrupt(hwIntXferComp);
220         Hwi_enableInterrupt(hwIntCcErr);
221         Hwi_enableInterrupt(hwIntTcErr);
223     /* Restore interrupts */
224     Hwi_restore(cookie);
225     }
228 /**
229  * \brief   EDMA3 De-initialization
230  *
231  * This function removes the EDMA3 Driver instance and unregisters the
232  * interrupt handlers.
233  *
234   * \return  EDMA3_DRV_SOK if success, else error code
235  */
236  EDMA3_DRV_Result edma3deinit (void)
237     {
238     unsigned int edmaInstanceId = 0;
239     EDMA3_DRV_Result    edma3Result = EDMA3_DRV_SOK;
241     /* Unregister Interrupt Handlers first */
242     unregisterEdma3Interrupts();
244     /* Delete the semaphore */
245     edma3Result = edma3OsSemDelete(semHandle);
246     if (EDMA3_DRV_SOK != edma3Result )
247         {
248 #ifdef EDMA3_DRV_DEBUG
249         EDMA3_DRV_PRINTF("edma3deinit: edma3OsSemDelete FAILED\r\n");
250 #endif
251         }
252     else
253         {
254         /* Make the semaphore handle as NULL. */
255         semHandle = NULL;
257         /* Now, close the EDMA3 Driver Instance */
258         edma3Result = EDMA3_DRV_close (hEdma, NULL);
259         if (EDMA3_DRV_SOK != edma3Result )
260             {
261 #ifdef EDMA3_DRV_DEBUG
262             EDMA3_DRV_PRINTF("edma3deinit: EDMA3_DRV_close FAILED\r\n");
263 #endif
264             }
265         else
266             {
267             /* Make the Drv handle as NULL. */
268             hEdma = NULL;
270             /* Now, delete the EDMA3 Driver Object */
271             edma3Result = EDMA3_DRV_delete (edmaInstanceId, NULL);
272             if (EDMA3_DRV_SOK != edma3Result )
273                 {
274 #ifdef EDMA3_DRV_DEBUG
275                 EDMA3_DRV_PRINTF("edma3deinit: EDMA3_DRV_delete FAILED\r\n");
276 #endif
277                 }
278             else
279                 {
280 #ifdef EDMA3_DRV_DEBUG
281                 EDMA3_DRV_PRINTF("edma3deinit: EDMA3 Deinitialization" \
282                                     " Completed...\r\n");
283 #endif
284                 }
285             }
286        }
288     return edma3Result;
289     }
292 /**  To Unregister the ISRs with the underlying OS, if previously registered. */
293 static void unregisterEdma3Interrupts (void)
294     {
295         static UInt32 cookie = 0;
296     unsigned int numTc = 0;
298     /* Disabling the global interrupts */
299     cookie = Hwi_disable();
301     /* Disable the Xfer Completion Event Interrupt */
302         EventCombiner_disableEvent(ccXferCompInt);
304     /* Disable the CC Error Event Interrupt */
305         EventCombiner_disableEvent(ccErrorInt);
307     /* Enable the TC Error Event Interrupt, according to the number of TCs. */
308     while (numTc < numEdma3Tc)
309         {
310         EventCombiner_disableEvent(tcErrorInt[numTc]);
311         numTc++;
312         }
314     /* Restore interrupts */
315     Hwi_restore(cookie);
316     }
318 /* End of File */