1 /*
2 * sample_init.c
3 *
4 * Sample Initialization for the EDMA3 Driver for BIOS 6 based applications.
5 * It should be MANDATORILY done once before EDMA3 usage.
6 *
7 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
8 *
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the
20 * distribution.
21 *
22 * Neither the name of Texas Instruments Incorporated nor the names of
23 * its contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 */
40 #include <ti/sysbios/hal/Hwi.h>
41 #include <ti/sysbios/ipc/Semaphore.h>
42 #include <ti/sysbios/family/c64p/EventCombiner.h>
44 #include <ti/sdo/edma3/drv/sample/bios6_edma3_drv_sample.h>
46 /** @brief EDMA3 Driver Instance specific Semaphore handle */
47 extern EDMA3_OS_Sem_Handle semHandle[];
49 /** To Register the ISRs with the underlying OS, if required. */
50 extern void registerEdma3Interrupts (unsigned int edma3Id);
51 /** To Unregister the ISRs with the underlying OS, if previously registered. */
52 extern void unregisterEdma3Interrupts (unsigned int edma3Id);
54 /* To find out the DSP# */
55 extern unsigned short determineProcId();
57 /**
58 * To check whether the global EDMA3 configuration is required or not.
59 * It should be done ONCE by any of the masters present in the system.
60 * This function checks whether the global configuration is required by the
61 * current master or not. In case of many masters, it should be done only
62 * by one of the masters. Hence this function will return TRUE only once
63 * and FALSE for all other masters.
64 */
65 extern unsigned short isGblConfigRequired(unsigned int dspNum);
67 /**
68 * DSP instance number on which the executable is running. Its value is
69 * determined by reading the processor specific register DNUM.
70 */
71 unsigned int dsp_num;
73 /* Number of EDMA3 controllers present in the system */
74 extern const unsigned int numEdma3Instances;
76 /* External Global Configuration Structure */
77 extern EDMA3_DRV_GblConfigParams sampleEdma3GblCfgParams[];
79 /* External Instance Specific Configuration Structure */
80 extern EDMA3_DRV_InstanceInitConfig sampleInstInitConfig[][EDMA3_MAX_REGIONS];
82 /**
83 * \brief EDMA3 Initialization
84 *
85 * This function initializes the EDMA3 Driver and registers the
86 * interrupt handlers.
87 *
88 * \return EDMA3_DRV_SOK if success, else error code
89 */
90 EDMA3_DRV_Handle edma3init (unsigned int edma3Id, EDMA3_DRV_Result *errorCode)
91 {
92 EDMA3_DRV_Result edma3Result = EDMA3_DRV_E_INVALID_PARAM;
93 Semaphore_Params semParams;
94 EDMA3_DRV_GblConfigParams *globalConfig = NULL;
95 EDMA3_DRV_InstanceInitConfig *instanceConfig = NULL;
96 EDMA3_DRV_InitConfig initCfg;
97 EDMA3_RM_MiscParam miscParam;
98 EDMA3_DRV_Handle hEdma = NULL;
100 globalConfig = &sampleEdma3GblCfgParams[edma3Id];
101 instanceConfig = &sampleInstInitConfig[edma3Id][dsp_num];
103 if ((edma3Id >= numEdma3Instances) || (errorCode == NULL))
104 return hEdma;
106 /* DSP instance number */
107 dsp_num = determineProcId();
109 /* Configure it as master, if required */
110 miscParam.isSlave = isGblConfigRequired(dsp_num);
111 edma3Result = EDMA3_DRV_create (edma3Id, globalConfig ,
112 (void *)&miscParam);
114 if (edma3Result == EDMA3_DRV_SOK)
115 {
116 /**
117 * Driver Object created successfully.
118 * Create a semaphore now for driver instance.
119 */
120 Semaphore_Params_init(&semParams);
122 initCfg.drvSemHandle = NULL;
123 edma3Result = edma3OsSemCreate(1, &semParams, &initCfg.drvSemHandle);
124 }
126 if (edma3Result == EDMA3_DRV_SOK)
127 {
128 /* Save the semaphore handle for future use */
129 semHandle[edma3Id] = initCfg.drvSemHandle;
131 /* configuration structure for the Driver */
132 initCfg.isMaster = TRUE;
133 /* Choose shadow region according to the DSP# */
134 initCfg.regionId = (EDMA3_RM_RegionId)dsp_num;
135 /* Driver instance specific config NULL */
136 initCfg.drvInstInitConfig = instanceConfig;
138 initCfg.gblerrCb = NULL;
139 initCfg.gblerrData = NULL;
141 /* Open the Driver Instance */
142 hEdma = EDMA3_DRV_open (edma3Id, (void *) &initCfg, &edma3Result);
143 }
145 if(hEdma && (edma3Result == EDMA3_DRV_SOK))
146 {
147 /**
148 * Register Interrupt Handlers for various interrupts
149 * like transfer completion interrupt, CC error
150 * interrupt, TC error interrupts etc, if required.
151 */
152 registerEdma3Interrupts(edma3Id);
153 }
155 *errorCode = edma3Result;
156 return hEdma;
157 }
160 /**
161 * \brief EDMA3 De-initialization
162 *
163 * This function removes the EDMA3 Driver instance and unregisters the
164 * interrupt handlers.
165 *
166 * \return EDMA3_DRV_SOK if success, else error code
167 */
168 EDMA3_DRV_Result edma3deinit (unsigned int edma3Id, EDMA3_DRV_Handle hEdma)
169 {
170 EDMA3_DRV_Result edma3Result = EDMA3_DRV_E_INVALID_PARAM;
172 /* Unregister Interrupt Handlers first */
173 unregisterEdma3Interrupts(edma3Id);
175 /* Delete the semaphore */
176 edma3Result = edma3OsSemDelete(semHandle[edma3Id]);
178 if (EDMA3_DRV_SOK == edma3Result )
179 {
180 /* Make the semaphore handle as NULL. */
181 semHandle[edma3Id] = NULL;
183 /* Now, close the EDMA3 Driver Instance */
184 edma3Result = EDMA3_DRV_close (hEdma, NULL);
185 }
187 if (EDMA3_DRV_SOK == edma3Result )
188 {
189 /* Now, delete the EDMA3 Driver Object */
190 edma3Result = EDMA3_DRV_delete (edma3Id, NULL);
191 }
193 return edma3Result;
194 }
196 /* End of File */