]> git.sur5r.net Git - freertos/blob - Demo/ARM9_STR91X_IAR/lwip/api/sys_arch.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / ARM9_STR91X_IAR / lwip / api / sys_arch.c
1 /*\r
2  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without modification,\r
6  * are permitted provided that the following conditions are met:\r
7  *\r
8  * 1. Redistributions of source code must retain the above copyright notice,\r
9  *    this list of conditions and the following disclaimer.\r
10  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
11  *    this list of conditions and the following disclaimer in the documentation\r
12  *    and/or other materials provided with the distribution.\r
13  * 3. The name of the author may not be used to endorse or promote products\r
14  *    derived from this software without specific prior written permission.\r
15  *\r
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT\r
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT\r
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY\r
25  * OF SUCH DAMAGE.\r
26  *\r
27  * This file is part of the lwIP TCP/IP stack.\r
28  *\r
29  * Author: Adam Dunkels <adam@sics.se>\r
30  *\r
31  */\r
32 \r
33 /* lwIP includes. */\r
34 #include "lwip/debug.h"\r
35 #include "lwip/def.h"\r
36 #include "lwip/sys.h"\r
37 #include "lwip/mem.h"\r
38 \r
39 #include <stdio.h>\r
40 \r
41 /* Message queue constants. */\r
42 #define archMESG_QUEUE_LENGTH   ( 6 )\r
43 #define archPOST_BLOCK_TIME_MS  ( ( unsigned long ) 10000 )\r
44 \r
45 struct timeoutlist\r
46 {\r
47         struct sys_timeouts timeouts;\r
48         xTaskHandle pid;\r
49 };\r
50 \r
51 /* This is the number of threads that can be started with sys_thread_new() */\r
52 #define SYS_THREAD_MAX 4\r
53 \r
54 static struct timeoutlist timeoutlist[SYS_THREAD_MAX];\r
55 static u16_t nextthread = 0;\r
56 int intlevel = 0;\r
57 \r
58 static sys_arch_state_t s_sys_arch_state;\r
59 \r
60 /*-----------------------------------------------------------------------------------*/\r
61 //  Creates an empty mailbox.\r
62 sys_mbox_t\r
63 sys_mbox_new(void)\r
64 {\r
65         xQueueHandle mbox;\r
66 \r
67         mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) );\r
68 \r
69         return mbox;\r
70 }\r
71 \r
72 /*-----------------------------------------------------------------------------------*/\r
73 /*\r
74   Deallocates a mailbox. If there are messages still present in the\r
75   mailbox when the mailbox is deallocated, it is an indication of a\r
76   programming error in lwIP and the developer should be notified.\r
77 */\r
78 void\r
79 sys_mbox_free(sys_mbox_t mbox)\r
80 {\r
81         if( uxQueueMessagesWaiting( mbox ) )\r
82         {\r
83                 /* Line for breakpoint.  Should never break here! */\r
84 //              __asm volatile ( "NOP" );\r
85         }\r
86 \r
87         vQueueDelete( mbox );\r
88 }\r
89 \r
90 /*-----------------------------------------------------------------------------------*/\r
91 //   Posts the "msg" to the mailbox.\r
92 void\r
93 sys_mbox_post(sys_mbox_t mbox, void *data)\r
94 {\r
95         xQueueSend( mbox, &data, ( portTickType ) ( archPOST_BLOCK_TIME_MS / portTICK_RATE_MS ) );\r
96 }\r
97 \r
98 \r
99 /*-----------------------------------------------------------------------------------*/\r
100 /*\r
101   Blocks the thread until a message arrives in the mailbox, but does\r
102   not block the thread longer than "timeout" milliseconds (similar to\r
103   the sys_arch_sem_wait() function). The "msg" argument is a result\r
104   parameter that is set by the function (i.e., by doing "*msg =\r
105   ptr"). The "msg" parameter maybe NULL to indicate that the message\r
106   should be dropped.\r
107 \r
108   The return values are the same as for the sys_arch_sem_wait() function:\r
109   Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a\r
110   timeout.\r
111 \r
112   Note that a function with a similar name, sys_mbox_fetch(), is\r
113   implemented by lwIP.\r
114 */\r
115 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)\r
116 {\r
117 void *dummyptr;\r
118 portTickType StartTime, EndTime, Elapsed;\r
119 \r
120         StartTime = xTaskGetTickCount();\r
121 \r
122         if( msg == NULL )\r
123         {\r
124                 msg = &dummyptr;\r
125         }\r
126                 \r
127         if(     timeout != 0 )\r
128         {\r
129                 if(pdTRUE == xQueueReceive( mbox, &(*msg), timeout ) )\r
130                 {\r
131                         EndTime = xTaskGetTickCount();\r
132                         Elapsed = EndTime - StartTime;\r
133                         if( Elapsed == 0 )\r
134                         {\r
135                                 Elapsed = 1;\r
136                         }\r
137                         return ( Elapsed );\r
138                 }\r
139                 else // timed out blocking for message\r
140                 {\r
141                         *msg = NULL;\r
142                         return SYS_ARCH_TIMEOUT;\r
143                 }\r
144         }\r
145         else // block forever for a message.\r
146         {\r
147                 while( pdTRUE != xQueueReceive( mbox, &(*msg), 10000 ) ) // time is arbitrary\r
148                 {\r
149                         ;\r
150                 }\r
151                 EndTime = xTaskGetTickCount();\r
152                 Elapsed = EndTime - StartTime;\r
153                 if( Elapsed == 0 )\r
154                 {\r
155                         Elapsed = 1;\r
156                 }\r
157                 return ( Elapsed ); // return time blocked TBD test     \r
158         }\r
159 }\r
160 \r
161 /*-----------------------------------------------------------------------------------*/\r
162 //  Creates and returns a new semaphore. The "count" argument specifies\r
163 //  the initial state of the semaphore. TBD finish and test\r
164 sys_sem_t\r
165 sys_sem_new(u8_t count)\r
166 {\r
167         xSemaphoreHandle  xSemaphore;\r
168 \r
169         portENTER_CRITICAL();\r
170         vSemaphoreCreateBinary( xSemaphore );\r
171         if(count == 0)  // Means it can't be taken\r
172         {\r
173                 xSemaphoreTake(xSemaphore,1);\r
174         }\r
175         portEXIT_CRITICAL();\r
176 \r
177         if( xSemaphore == NULL )\r
178         {\r
179                 return NULL;    // TBD need assert\r
180         }\r
181         else\r
182         {\r
183                 return xSemaphore;\r
184         }\r
185 }\r
186 \r
187 /*-----------------------------------------------------------------------------------*/\r
188 /*\r
189   Blocks the thread while waiting for the semaphore to be\r
190   signaled. If the "timeout" argument is non-zero, the thread should\r
191   only be blocked for the specified time (measured in\r
192   milliseconds).\r
193 \r
194   If the timeout argument is non-zero, the return value is the number of\r
195   milliseconds spent waiting for the semaphore to be signaled. If the\r
196   semaphore wasn't signaled within the specified time, the return value is\r
197   SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore\r
198   (i.e., it was already signaled), the function may return zero.\r
199 \r
200   Notice that lwIP implements a function with a similar name,\r
201   sys_sem_wait(), that uses the sys_arch_sem_wait() function.\r
202 */\r
203 u32_t\r
204 sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)\r
205 {\r
206 portTickType StartTime, EndTime, Elapsed;\r
207 \r
208         StartTime = xTaskGetTickCount();\r
209 \r
210         if(     timeout != 0)\r
211         {\r
212                 if( xSemaphoreTake( sem, timeout ) == pdTRUE )\r
213                 {\r
214                         EndTime = xTaskGetTickCount();\r
215                         Elapsed = EndTime - StartTime;\r
216                         if( Elapsed == 0 )\r
217                         {\r
218                                 Elapsed = 1;\r
219                         }\r
220                         return (Elapsed); // return time blocked TBD test       \r
221                 }\r
222                 else\r
223                 {\r
224                         return SYS_ARCH_TIMEOUT;\r
225                 }\r
226         }\r
227         else // must block without a timeout\r
228         {\r
229                 while( xSemaphoreTake( sem, 10000 ) != pdTRUE )\r
230                 {\r
231                         ;\r
232                 }\r
233                 EndTime = xTaskGetTickCount();\r
234                 Elapsed = EndTime - StartTime;\r
235                 if( Elapsed == 0 )\r
236                 {\r
237                         Elapsed = 1;\r
238                 }\r
239 \r
240                 return ( Elapsed ); // return time blocked      \r
241                 \r
242         }\r
243 }\r
244 \r
245 /*-----------------------------------------------------------------------------------*/\r
246 // Signals a semaphore\r
247 void\r
248 sys_sem_signal(sys_sem_t sem)\r
249 {\r
250         xSemaphoreGive( sem );\r
251 }\r
252 \r
253 /*-----------------------------------------------------------------------------------*/\r
254 // Deallocates a semaphore\r
255 void\r
256 sys_sem_free(sys_sem_t sem)\r
257 {\r
258         vQueueDelete( sem );\r
259 }\r
260 \r
261 /*-----------------------------------------------------------------------------------*/\r
262 // Initialize sys arch\r
263 void\r
264 sys_init(void)\r
265 {\r
266 \r
267         int i;\r
268 \r
269         // Initialize the the per-thread sys_timeouts structures\r
270         // make sure there are no valid pids in the list\r
271         for(i = 0; i < SYS_THREAD_MAX; i++)\r
272         {\r
273                 timeoutlist[i].pid = 0;\r
274         }\r
275 \r
276         // keep track of how many threads have been created\r
277         nextthread = 0;\r
278         \r
279         s_sys_arch_state.nTaskCount = 0;\r
280         sys_set_default_state();\r
281 }\r
282 \r
283 /*-----------------------------------------------------------------------------------*/\r
284 /*\r
285   Returns a pointer to the per-thread sys_timeouts structure. In lwIP,\r
286   each thread has a list of timeouts which is represented as a linked\r
287   list of sys_timeout structures. The sys_timeouts structure holds a\r
288   pointer to a linked list of timeouts. This function is called by\r
289   the lwIP timeout scheduler and must not return a NULL value.\r
290 \r
291   In a single threaded sys_arch implementation, this function will\r
292   simply return a pointer to a global sys_timeouts variable stored in\r
293   the sys_arch module.\r
294 */\r
295 struct sys_timeouts *\r
296 sys_arch_timeouts(void)\r
297 {\r
298 int i;\r
299 xTaskHandle pid;\r
300 struct timeoutlist *tl;\r
301 \r
302         pid = xTaskGetCurrentTaskHandle( );\r
303 \r
304         for(i = 0; i < nextthread; i++)\r
305         {\r
306                 tl = &timeoutlist[i];\r
307                 if(tl->pid == pid)\r
308                 {\r
309                         return &(tl->timeouts);\r
310                 }\r
311         }\r
312 \r
313         // Error\r
314         return NULL;\r
315 }\r
316 \r
317 /*-----------------------------------------------------------------------------------*/\r
318 /*-----------------------------------------------------------------------------------*/\r
319 // TBD\r
320 /*-----------------------------------------------------------------------------------*/\r
321 /*\r
322   Starts a new thread with priority "prio" that will begin its execution in the\r
323   function "thread()". The "arg" argument will be passed as an argument to the\r
324   thread() function. The id of the new thread is returned. Both the id and\r
325   the priority are system dependent.\r
326 */\r
327 sys_thread_t sys_thread_new(void (* thread)(void *arg), void *arg, int prio)\r
328 {\r
329 xTaskHandle CreatedTask;\r
330 int result;\r
331 \r
332         result = xTaskCreate(thread, ( signed char * ) s_sys_arch_state.cTaskName, s_sys_arch_state.nStackDepth, arg, prio, &CreatedTask );\r
333 \r
334         // For each task created, store the task handle (pid) in the timers array.\r
335         // This scheme doesn't allow for threads to be deleted\r
336         timeoutlist[nextthread++].pid = CreatedTask;\r
337 \r
338         if(result == pdPASS)\r
339         {\r
340                 ++s_sys_arch_state.nTaskCount;\r
341                 \r
342                 return CreatedTask;\r
343         }\r
344         else\r
345         {\r
346                 return NULL;\r
347         }\r
348 }\r
349 \r
350 /*\r
351   This optional function does a "fast" critical region protection and returns\r
352   the previous protection level. This function is only called during very short\r
353   critical regions. An embedded system which supports ISR-based drivers might\r
354   want to implement this function by disabling interrupts. Task-based systems\r
355   might want to implement this by using a mutex or disabling tasking. This\r
356   function should support recursive calls from the same task or interrupt. In\r
357   other words, sys_arch_protect() could be called while already protected. In\r
358   that case the return value indicates that it is already protected.\r
359 \r
360   sys_arch_protect() is only required if your port is supporting an operating\r
361   system.\r
362 */\r
363 sys_prot_t sys_arch_protect(void)\r
364 {\r
365         vPortEnterCritical();\r
366         return 1;\r
367 }\r
368 \r
369 /*\r
370   This optional function does a "fast" set of critical region protection to the\r
371   value specified by pval. See the documentation for sys_arch_protect() for\r
372   more information. This function is only required if your port is supporting\r
373   an operating system.\r
374 */\r
375 void sys_arch_unprotect(sys_prot_t pval)\r
376 {\r
377         ( void ) pval;\r
378         vPortExitCritical();\r
379 }\r
380 \r
381 void sys_set_default_state()\r
382 {\r
383         s_sys_arch_state.nStackDepth = configMINIMAL_STACK_SIZE;\r
384         sprintf(s_sys_arch_state.cTaskName, "thread%d", s_sys_arch_state.nTaskCount);\r
385 }\r
386 \r
387 void sys_set_state(signed char *pTaskName, unsigned short nStackSize)\r
388 {\r
389         s_sys_arch_state.nStackDepth = nStackSize;\r
390         sprintf(s_sys_arch_state.cTaskName, "%s", pTaskName);\r
391 }\r