]> git.sur5r.net Git - freertos/blob - Demo/Common/ethernet/lwIP_130/contrib/port/FreeRTOS/sys_arch.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / Common / ethernet / lwIP_130 / contrib / port / FreeRTOS / 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 #include "lwip/stats.h"\r
39 \r
40 struct timeoutlist\r
41 {\r
42         struct sys_timeouts timeouts;\r
43         xTaskHandle pid;\r
44 };\r
45 \r
46 /* This is the number of threads that can be started with sys_thread_new() */\r
47 #define SYS_THREAD_MAX 4\r
48 \r
49 static struct timeoutlist s_timeoutlist[SYS_THREAD_MAX];\r
50 static u16_t s_nextthread = 0;\r
51 \r
52 \r
53 /*-----------------------------------------------------------------------------------*/\r
54 //  Creates an empty mailbox.\r
55 sys_mbox_t sys_mbox_new(int size)\r
56 {\r
57         xQueueHandle mbox;\r
58         \r
59         ( void ) size;\r
60         \r
61         mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) );\r
62 \r
63 #if SYS_STATS\r
64       ++lwip_stats.sys.mbox.used;\r
65       if (lwip_stats.sys.mbox.max < lwip_stats.sys.mbox.used) {\r
66          lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;\r
67           }\r
68 #endif /* SYS_STATS */\r
69 \r
70         return mbox;\r
71 }\r
72 \r
73 /*-----------------------------------------------------------------------------------*/\r
74 /*\r
75   Deallocates a mailbox. If there are messages still present in the\r
76   mailbox when the mailbox is deallocated, it is an indication of a\r
77   programming error in lwIP and the developer should be notified.\r
78 */\r
79 void 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                 portNOP();\r
85 #if SYS_STATS\r
86             lwip_stats.sys.mbox.err++;\r
87 #endif /* SYS_STATS */\r
88                         \r
89                 // TODO notify the user of failure.\r
90         }\r
91 \r
92         vQueueDelete( mbox );\r
93 \r
94 #if SYS_STATS\r
95      --lwip_stats.sys.mbox.used;\r
96 #endif /* SYS_STATS */\r
97 }\r
98 \r
99 /*-----------------------------------------------------------------------------------*/\r
100 //   Posts the "msg" to the mailbox.\r
101 void sys_mbox_post(sys_mbox_t mbox, void *data)\r
102 {\r
103         while ( xQueueSendToBack(mbox, &data, portMAX_DELAY ) != pdTRUE ){}\r
104 }\r
105 \r
106 \r
107 /*-----------------------------------------------------------------------------------*/\r
108 //   Try to post the "msg" to the mailbox.\r
109 err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg)\r
110 {\r
111 err_t result;\r
112 \r
113    if ( xQueueSend( mbox, &msg, 0 ) == pdPASS )\r
114    {\r
115       result = ERR_OK;\r
116    }\r
117    else {\r
118       // could not post, queue must be full\r
119       result = ERR_MEM;\r
120                         \r
121 #if SYS_STATS\r
122       lwip_stats.sys.mbox.err++;\r
123 #endif /* SYS_STATS */\r
124                         \r
125    }\r
126 \r
127    return result;\r
128 }\r
129 \r
130 /*-----------------------------------------------------------------------------------*/\r
131 /*\r
132   Blocks the thread until a message arrives in the mailbox, but does\r
133   not block the thread longer than "timeout" milliseconds (similar to\r
134   the sys_arch_sem_wait() function). The "msg" argument is a result\r
135   parameter that is set by the function (i.e., by doing "*msg =\r
136   ptr"). The "msg" parameter maybe NULL to indicate that the message\r
137   should be dropped.\r
138 \r
139   The return values are the same as for the sys_arch_sem_wait() function:\r
140   Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a\r
141   timeout.\r
142 \r
143   Note that a function with a similar name, sys_mbox_fetch(), is\r
144   implemented by lwIP.\r
145 */\r
146 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)\r
147 {\r
148 void *dummyptr;\r
149 portTickType StartTime, EndTime, Elapsed;\r
150 \r
151         StartTime = xTaskGetTickCount();\r
152 \r
153         if ( msg == NULL )\r
154         {\r
155                 msg = &dummyptr;\r
156         }\r
157                 \r
158         if ( timeout != 0 )\r
159         {\r
160                 if ( pdTRUE == xQueueReceive( mbox, &(*msg), timeout / portTICK_RATE_MS ) )\r
161                 {\r
162                         EndTime = xTaskGetTickCount();\r
163                         Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;\r
164                         \r
165                         return ( Elapsed );\r
166                 }\r
167                 else // timed out blocking for message\r
168                 {\r
169                         *msg = NULL;\r
170                         \r
171                         return SYS_ARCH_TIMEOUT;\r
172                 }\r
173         }\r
174         else // block forever for a message.\r
175         {\r
176                 while( pdTRUE != xQueueReceive( mbox, &(*msg), portMAX_DELAY ) ){} // time is arbitrary\r
177                 EndTime = xTaskGetTickCount();\r
178                 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;\r
179                 \r
180                 return ( Elapsed ); // return time blocked TODO test    \r
181         }\r
182 }\r
183 \r
184 /*-----------------------------------------------------------------------------------*/\r
185 /*\r
186   Similar to sys_arch_mbox_fetch, but if message is not ready immediately, we'll\r
187   return with SYS_MBOX_EMPTY.  On success, 0 is returned.\r
188 */\r
189 u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg)\r
190 {\r
191 void *dummyptr;\r
192 \r
193         if ( msg == NULL )\r
194         {\r
195                 msg = &dummyptr;\r
196         }\r
197 \r
198    if ( pdTRUE == xQueueReceive( mbox, &(*msg), 0 ) )\r
199    {\r
200       return ERR_OK;\r
201    }\r
202    else\r
203    {\r
204       return SYS_MBOX_EMPTY;\r
205    }\r
206 }\r
207 \r
208 /*-----------------------------------------------------------------------------------*/\r
209 //  Creates and returns a new semaphore. The "count" argument specifies\r
210 //  the initial state of the semaphore.\r
211 sys_sem_t sys_sem_new(u8_t count)\r
212 {\r
213         xSemaphoreHandle  xSemaphore;\r
214 \r
215         vSemaphoreCreateBinary( xSemaphore );\r
216         \r
217         if( xSemaphore == NULL )\r
218         {\r
219                 \r
220 #if SYS_STATS\r
221       ++lwip_stats.sys.sem.err;\r
222 #endif /* SYS_STATS */\r
223                         \r
224                 return SYS_SEM_NULL;    // TODO need assert\r
225         }\r
226         \r
227         if(count == 0)  // Means it can't be taken\r
228         {\r
229                 xSemaphoreTake(xSemaphore,1);\r
230         }\r
231 \r
232 #if SYS_STATS\r
233         ++lwip_stats.sys.sem.used;\r
234         if (lwip_stats.sys.sem.max < lwip_stats.sys.sem.used) {\r
235                 lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;\r
236         }\r
237 #endif /* SYS_STATS */\r
238                 \r
239         return xSemaphore;\r
240 }\r
241 \r
242 /*-----------------------------------------------------------------------------------*/\r
243 /*\r
244   Blocks the thread while waiting for the semaphore to be\r
245   signaled. If the "timeout" argument is non-zero, the thread should\r
246   only be blocked for the specified time (measured in\r
247   milliseconds).\r
248 \r
249   If the timeout argument is non-zero, the return value is the number of\r
250   milliseconds spent waiting for the semaphore to be signaled. If the\r
251   semaphore wasn't signaled within the specified time, the return value is\r
252   SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore\r
253   (i.e., it was already signaled), the function may return zero.\r
254 \r
255   Notice that lwIP implements a function with a similar name,\r
256   sys_sem_wait(), that uses the sys_arch_sem_wait() function.\r
257 */\r
258 u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)\r
259 {\r
260 portTickType StartTime, EndTime, Elapsed;\r
261 \r
262         StartTime = xTaskGetTickCount();\r
263 \r
264         if(     timeout != 0)\r
265         {\r
266                 if( xSemaphoreTake( sem, timeout / portTICK_RATE_MS ) == pdTRUE )\r
267                 {\r
268                         EndTime = xTaskGetTickCount();\r
269                         Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;\r
270                         \r
271                         return (Elapsed); // return time blocked TODO test      \r
272                 }\r
273                 else\r
274                 {\r
275                         return SYS_ARCH_TIMEOUT;\r
276                 }\r
277         }\r
278         else // must block without a timeout\r
279         {\r
280                 while( xSemaphoreTake( sem, portMAX_DELAY ) != pdTRUE ){}\r
281                 EndTime = xTaskGetTickCount();\r
282                 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;\r
283 \r
284                 return ( Elapsed ); // return time blocked      \r
285                 \r
286         }\r
287 }\r
288 \r
289 /*-----------------------------------------------------------------------------------*/\r
290 // Signals a semaphore\r
291 void sys_sem_signal(sys_sem_t sem)\r
292 {\r
293         xSemaphoreGive( sem );\r
294 }\r
295 \r
296 /*-----------------------------------------------------------------------------------*/\r
297 // Deallocates a semaphore\r
298 void sys_sem_free(sys_sem_t sem)\r
299 {\r
300 #if SYS_STATS\r
301       --lwip_stats.sys.sem.used;\r
302 #endif /* SYS_STATS */\r
303                         \r
304         vQueueDelete( sem );\r
305 }\r
306 \r
307 /*-----------------------------------------------------------------------------------*/\r
308 // Initialize sys arch\r
309 void sys_init(void)\r
310 {\r
311         int i;\r
312 \r
313         // Initialize the the per-thread sys_timeouts structures\r
314         // make sure there are no valid pids in the list\r
315         for(i = 0; i < SYS_THREAD_MAX; i++)\r
316         {\r
317                 s_timeoutlist[i].pid = 0;\r
318                 s_timeoutlist[i].timeouts.next = NULL;\r
319         }\r
320 \r
321         // keep track of how many threads have been created\r
322         s_nextthread = 0;\r
323 }\r
324 \r
325 /*-----------------------------------------------------------------------------------*/\r
326 /*\r
327   Returns a pointer to the per-thread sys_timeouts structure. In lwIP,\r
328   each thread has a list of timeouts which is represented as a linked\r
329   list of sys_timeout structures. The sys_timeouts structure holds a\r
330   pointer to a linked list of timeouts. This function is called by\r
331   the lwIP timeout scheduler and must not return a NULL value.\r
332 \r
333   In a single threaded sys_arch implementation, this function will\r
334   simply return a pointer to a global sys_timeouts variable stored in\r
335   the sys_arch module.\r
336 */\r
337 struct sys_timeouts *sys_arch_timeouts(void)\r
338 {\r
339 int i;\r
340 xTaskHandle pid;\r
341 struct timeoutlist *tl;\r
342 \r
343         pid = xTaskGetCurrentTaskHandle( );\r
344 \r
345         for(i = 0; i < s_nextthread; i++)\r
346         {\r
347                 tl = &(s_timeoutlist[i]);\r
348                 if(tl->pid == pid)\r
349                 {\r
350                         return &(tl->timeouts);\r
351                 }\r
352         }\r
353 \r
354         // Error\r
355         return NULL;\r
356 }\r
357 \r
358 /*-----------------------------------------------------------------------------------*/\r
359 /*-----------------------------------------------------------------------------------*/\r
360 // TODO\r
361 /*-----------------------------------------------------------------------------------*/\r
362 /*\r
363   Starts a new thread with priority "prio" that will begin its execution in the\r
364   function "thread()". The "arg" argument will be passed as an argument to the\r
365   thread() function. The id of the new thread is returned. Both the id and\r
366   the priority are system dependent.\r
367 */\r
368 sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio)\r
369 {\r
370 xTaskHandle CreatedTask;\r
371 int result;\r
372 \r
373    if ( s_nextthread < SYS_THREAD_MAX )\r
374    {\r
375       result = xTaskCreate( thread, ( signed portCHAR * ) name, stacksize, arg, prio, &CreatedTask );\r
376 \r
377            // For each task created, store the task handle (pid) in the timers array.\r
378            // This scheme doesn't allow for threads to be deleted\r
379            s_timeoutlist[s_nextthread++].pid = CreatedTask;\r
380 \r
381            if(result == pdPASS)\r
382            {\r
383                    return CreatedTask;\r
384            }\r
385            else\r
386            {\r
387                    return NULL;\r
388            }\r
389    }\r
390    else\r
391    {\r
392       return NULL;\r
393    }\r
394 }\r
395 \r
396 /*\r
397   This optional function does a "fast" critical region protection and returns\r
398   the previous protection level. This function is only called during very short\r
399   critical regions. An embedded system which supports ISR-based drivers might\r
400   want to implement this function by disabling interrupts. Task-based systems\r
401   might want to implement this by using a mutex or disabling tasking. This\r
402   function should support recursive calls from the same task or interrupt. In\r
403   other words, sys_arch_protect() could be called while already protected. In\r
404   that case the return value indicates that it is already protected.\r
405 \r
406   sys_arch_protect() is only required if your port is supporting an operating\r
407   system.\r
408 */\r
409 sys_prot_t sys_arch_protect(void)\r
410 {\r
411         vPortEnterCritical();\r
412         return 1;\r
413 }\r
414 \r
415 /*\r
416   This optional function does a "fast" set of critical region protection to the\r
417   value specified by pval. See the documentation for sys_arch_protect() for\r
418   more information. This function is only required if your port is supporting\r
419   an operating system.\r
420 */\r
421 void sys_arch_unprotect(sys_prot_t pval)\r
422 {\r
423         ( void ) pval;\r
424         vPortExitCritical();\r
425 }\r
426 \r
427 /*\r
428  * Prints an assertion messages and aborts execution.\r
429  */\r
430 void sys_assert( const char *msg )\r
431 {       \r
432         ( void ) msg;\r
433         /*FSL:only needed for debugging\r
434         printf(msg);\r
435         printf("\n\r");\r
436         */\r
437     vPortEnterCritical(  );\r
438     for(;;)\r
439     ;\r
440 }\r