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