1 /*\r
2 * sample_cs.c\r
3 *\r
4 * Sample functions showing the implementation of critical section entry/exit\r
5 * routines and various semaphore related routines (all OS depenedent). These\r
6 * implementations MUST be provided by the user / application, using the EDMA3\r
7 * Resource Manager, for its correct functioning.\r
8 *\r
9 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/\r
10 *\r
11 *\r
12 * Redistribution and use in source and binary forms, with or without\r
13 * modification, are permitted provided that the following conditions\r
14 * are met:\r
15 *\r
16 * Redistributions of source code must retain the above copyright\r
17 * notice, this list of conditions and the following disclaimer.\r
18 *\r
19 * Redistributions in binary form must reproduce the above copyright\r
20 * notice, this list of conditions and the following disclaimer in the\r
21 * documentation and/or other materials provided with the\r
22 * distribution.\r
23 *\r
24 * Neither the name of Texas Instruments Incorporated nor the names of\r
25 * its contributors may be used to endorse or promote products derived\r
26 * from this software without specific prior written permission.\r
27 *\r
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
39 *\r
40 */\r
41 \r
42 #include <ti/sysbios/family/c64p/EventCombiner.h>\r
43 #include <ti/sysbios/hal/Hwi.h>\r
44 #include <ti/sysbios/knl/Task.h>\r
45 #include <ti/sysbios/ipc/Semaphore.h>\r
46 \r
47 #include <ti/sdo/edma3/rm/sample/bios6_edma3_rm_sample.h>\r
48 \r
49 extern unsigned int ccXferCompInt[][EDMA3_MAX_REGIONS];\r
50 extern unsigned int ccErrorInt[];\r
51 extern unsigned int tcErrorInt[][EDMA3_MAX_TC];\r
52 \r
53 /**\r
54 * Shadow Region on which the executable is running. Its value is\r
55 * populated with the DSP Instance Number here in this case.\r
56 */\r
57 extern unsigned int region_id;\r
58 \r
59 /**\r
60 * \brief EDMA3 OS Protect Entry\r
61 *\r
62 * This function saves the current state of protection in 'intState'\r
63 * variable passed by caller, if the protection level is\r
64 * EDMA3_OS_PROTECT_INTERRUPT. It then applies the requested level of\r
65 * protection.\r
66 * For EDMA3_OS_PROTECT_INTERRUPT_XFER_COMPLETION and\r
67 * EDMA3_OS_PROTECT_INTERRUPT_CC_ERROR, variable 'intState' is ignored,\r
68 * and the requested interrupt is disabled.\r
69 * For EDMA3_OS_PROTECT_INTERRUPT_TC_ERROR, '*intState' specifies the\r
70 * Transfer Controller number whose interrupt needs to be disabled.\r
71 *\r
72 * \param level is numeric identifier of the desired degree of protection.\r
73 * \param intState is memory location where current state of protection is\r
74 * saved for future use while restoring it via edma3OsProtectExit() (Only\r
75 * for EDMA3_OS_PROTECT_INTERRUPT protection level).\r
76 * \return None\r
77 */\r
78 void edma3OsProtectEntry (unsigned int edma3InstanceId, \r
79 int level, unsigned int *intState)\r
80 {\r
81 if (((level == EDMA3_OS_PROTECT_INTERRUPT)\r
82 || (level == EDMA3_OS_PROTECT_INTERRUPT_TC_ERROR))\r
83 && (intState == NULL))\r
84 {\r
85 return;\r
86 }\r
87 else\r
88 {\r
89 switch (level)\r
90 {\r
91 /* Disable all (global) interrupts */\r
92 case EDMA3_OS_PROTECT_INTERRUPT :\r
93 *intState = Hwi_disable();\r
94 break;\r
95 \r
96 /* Disable scheduler */\r
97 case EDMA3_OS_PROTECT_SCHEDULER :\r
98 Task_disable();\r
99 break;\r
100 \r
101 /* Disable EDMA3 transfer completion interrupt only */\r
102 case EDMA3_OS_PROTECT_INTERRUPT_XFER_COMPLETION :\r
103 EventCombiner_disableEvent(ccXferCompInt[edma3InstanceId][region_id]);\r
104 break;\r
105 \r
106 /* Disable EDMA3 CC error interrupt only */\r
107 case EDMA3_OS_PROTECT_INTERRUPT_CC_ERROR :\r
108 EventCombiner_disableEvent(ccErrorInt[edma3InstanceId]);\r
109 break;\r
110 \r
111 /* Disable EDMA3 TC error interrupt only */\r
112 case EDMA3_OS_PROTECT_INTERRUPT_TC_ERROR :\r
113 switch (*intState)\r
114 {\r
115 case 0:\r
116 case 1:\r
117 case 2:\r
118 case 3:\r
119 case 4:\r
120 case 5:\r
121 case 6:\r
122 case 7:\r
123 /* Fall through... */\r
124 /* Disable the corresponding interrupt */\r
125 EventCombiner_disableEvent(tcErrorInt[edma3InstanceId][*intState]);\r
126 break;\r
127 \r
128 default:\r
129 break;\r
130 }\r
131 \r
132 break;\r
133 \r
134 default:\r
135 break;\r
136 }\r
137 }\r
138 }\r
139 \r
140 \r
141 /**\r
142 * \brief EDMA3 OS Protect Exit\r
143 *\r
144 * This function undoes the protection enforced to original state\r
145 * as is specified by the variable 'intState' passed, if the protection\r
146 * level is EDMA3_OS_PROTECT_INTERRUPT.\r
147 * For EDMA3_OS_PROTECT_INTERRUPT_XFER_COMPLETION and\r
148 * EDMA3_OS_PROTECT_INTERRUPT_CC_ERROR, variable 'intState' is ignored,\r
149 * and the requested interrupt is enabled.\r
150 * For EDMA3_OS_PROTECT_INTERRUPT_TC_ERROR, 'intState' specifies the\r
151 * Transfer Controller number whose interrupt needs to be enabled.\r
152 * \param level is numeric identifier of the desired degree of protection.\r
153 * \param intState is original state of protection at time when the\r
154 * corresponding edma3OsProtectEntry() was called (Only\r
155 * for EDMA3_OS_PROTECT_INTERRUPT protection level).\r
156 * \return None\r
157 */\r
158 void edma3OsProtectExit (unsigned int edma3InstanceId,\r
159 int level, unsigned int intState)\r
160 {\r
161 switch (level)\r
162 {\r
163 /* Enable all (global) interrupts */\r
164 case EDMA3_OS_PROTECT_INTERRUPT :\r
165 Hwi_restore(intState);\r
166 break;\r
167 \r
168 /* Enable scheduler */\r
169 case EDMA3_OS_PROTECT_SCHEDULER :\r
170 Task_enable();\r
171 break;\r
172 \r
173 /* Enable EDMA3 transfer completion interrupt only */\r
174 case EDMA3_OS_PROTECT_INTERRUPT_XFER_COMPLETION :\r
175 EventCombiner_enableEvent(ccXferCompInt[edma3InstanceId][region_id]);\r
176 break;\r
177 \r
178 /* Enable EDMA3 CC error interrupt only */\r
179 case EDMA3_OS_PROTECT_INTERRUPT_CC_ERROR :\r
180 EventCombiner_enableEvent(ccErrorInt[edma3InstanceId]);\r
181 break;\r
182 \r
183 /* Enable EDMA3 TC error interrupt only */\r
184 case EDMA3_OS_PROTECT_INTERRUPT_TC_ERROR :\r
185 switch (intState)\r
186 {\r
187 case 0:\r
188 case 1:\r
189 case 2:\r
190 case 3:\r
191 case 4:\r
192 case 5:\r
193 case 6:\r
194 case 7:\r
195 /* Fall through... */\r
196 /* Enable the corresponding interrupt */\r
197 EventCombiner_enableEvent(tcErrorInt[edma3InstanceId][intState]);\r
198 break;\r
199 \r
200 default:\r
201 break;\r
202 }\r
203 \r
204 break;\r
205 \r
206 default:\r
207 break;\r
208 }\r
209 }\r
210 \r
211 \r
212 /**\r
213 * Counting Semaphore related functions (OS dependent) should be\r
214 * called/implemented by the application. A handle to the semaphore\r
215 * is required while opening the resource manager instance.\r
216 */\r
217 \r
218 /**\r
219 * \brief EDMA3 OS Semaphore Create\r
220 *\r
221 * This function creates a counting semaphore with specified\r
222 * attributes and initial value. It should be used to create a semaphore\r
223 * with initial value as '1'. The semaphore is then passed by the user\r
224 * to the EDMA3 RM for proper sharing of resources.\r
225 * \param initVal [IN] is initial value for semaphore\r
226 * \param semParams [IN] is the semaphore attributes.\r
227 * \param hSem [OUT] is location to recieve the handle to just created\r
228 * semaphore\r
229 * \return EDMA3_RM_SOK if succesful, else a suitable error code.\r
230 */\r
231 EDMA3_RM_Result edma3OsSemCreate(int initVal,\r
232 const Semaphore_Params *semParams,\r
233 EDMA3_OS_Sem_Handle *hSem)\r
234 {\r
235 EDMA3_RM_Result semCreateResult = EDMA3_RM_SOK;\r
236 \r
237 if(NULL == hSem)\r
238 {\r
239 semCreateResult = EDMA3_RM_E_INVALID_PARAM;\r
240 }\r
241 else\r
242 {\r
243 *hSem = (EDMA3_OS_Sem_Handle)Semaphore_create(initVal, semParams, NULL);\r
244 if ( (*hSem) == NULL )\r
245 {\r
246 semCreateResult = EDMA3_RM_E_SEMAPHORE;\r
247 }\r
248 }\r
249 \r
250 return semCreateResult;\r
251 }\r
252 \r
253 \r
254 /**\r
255 * \brief EDMA3 OS Semaphore Delete\r
256 *\r
257 * This function deletes or removes the specified semaphore\r
258 * from the system. Associated dynamically allocated memory\r
259 * if any is also freed up.\r
260 * \param hSem [IN] handle to the semaphore to be deleted\r
261 * \return EDMA3_RM_SOK if succesful else a suitable error code\r
262 */\r
263 EDMA3_RM_Result edma3OsSemDelete(EDMA3_OS_Sem_Handle hSem)\r
264 {\r
265 EDMA3_RM_Result semDeleteResult = EDMA3_RM_SOK;\r
266 \r
267 if(NULL == hSem)\r
268 {\r
269 semDeleteResult = EDMA3_RM_E_INVALID_PARAM;\r
270 }\r
271 else\r
272 {\r
273 Semaphore_delete(hSem);\r
274 }\r
275 \r
276 return semDeleteResult;\r
277 }\r
278 \r
279 \r
280 /**\r
281 * \brief EDMA3 OS Semaphore Take\r
282 *\r
283 * This function takes a semaphore token if available.\r
284 * If a semaphore is unavailable, it blocks currently\r
285 * running thread in wait (for specified duration) for\r
286 * a free semaphore.\r
287 * \param hSem [IN] is the handle of the specified semaphore\r
288 * \param mSecTimeout [IN] is wait time in milliseconds\r
289 * \return EDMA3_RM_Result if successful else a suitable error code\r
290 */\r
291 EDMA3_RM_Result edma3OsSemTake(EDMA3_OS_Sem_Handle hSem, int mSecTimeout)\r
292 {\r
293 EDMA3_RM_Result semTakeResult = EDMA3_RM_SOK;\r
294 unsigned short semPendResult;\r
295 \r
296 if(NULL == hSem)\r
297 {\r
298 semTakeResult = EDMA3_RM_E_INVALID_PARAM;\r
299 }\r
300 else\r
301 {\r
302 semPendResult = Semaphore_pend(hSem, mSecTimeout);\r
303 if (semPendResult == FALSE)\r
304 {\r
305 semTakeResult = EDMA3_RM_E_SEMAPHORE;\r
306 }\r
307 }\r
308 \r
309 return semTakeResult;\r
310 }\r
311 \r
312 \r
313 /**\r
314 * \brief EDMA3 OS Semaphore Give\r
315 *\r
316 * This function gives or relinquishes an already\r
317 * acquired semaphore token\r
318 * \param hSem [IN] is the handle of the specified semaphore\r
319 * \return EDMA3_RM_Result if successful else a suitable error code\r
320 */\r
321 EDMA3_RM_Result edma3OsSemGive(EDMA3_OS_Sem_Handle hSem)\r
322 {\r
323 EDMA3_RM_Result semGiveResult = EDMA3_RM_SOK;\r
324 \r
325 if(NULL == hSem)\r
326 {\r
327 semGiveResult = EDMA3_RM_E_INVALID_PARAM;\r
328 }\r
329 else\r
330 {\r
331 Semaphore_post(hSem);\r
332 }\r
333 \r
334 return semGiveResult;\r
335 }\r
336 \r
337 /* End of File */\r
338 \r