2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
\r
3 * All rights reserved.
\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
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
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
27 * This file is part of the lwIP TCP/IP stack.
\r
29 * Author: Adam Dunkels <adam@sics.se>
\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
42 struct sys_timeouts timeouts;
\r
46 /* This is the number of threads that can be started with sys_thread_new() */
\r
47 #define SYS_THREAD_MAX 4
\r
49 static struct timeoutlist s_timeoutlist[SYS_THREAD_MAX];
\r
50 static u16_t s_nextthread = 0;
\r
53 /*-----------------------------------------------------------------------------------*/
\r
54 // Creates an empty mailbox.
\r
55 sys_mbox_t sys_mbox_new(int size)
\r
59 mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) );
\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
66 #endif /* SYS_STATS */
\r
71 /*-----------------------------------------------------------------------------------*/
\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
77 void sys_mbox_free(sys_mbox_t mbox)
\r
79 if( uxQueueMessagesWaiting( mbox ) )
\r
81 /* Line for breakpoint. Should never break here! */
\r
84 lwip_stats.sys.mbox.err++;
\r
85 #endif /* SYS_STATS */
\r
87 // TODO notify the user of failure.
\r
90 vQueueDelete( mbox );
\r
93 --lwip_stats.sys.mbox.used;
\r
94 #endif /* SYS_STATS */
\r
97 /*-----------------------------------------------------------------------------------*/
\r
98 // Posts the "msg" to the mailbox.
\r
99 void sys_mbox_post(sys_mbox_t mbox, void *data)
\r
101 while ( xQueueSendToBack(mbox, &data, portMAX_DELAY ) != pdTRUE );
\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
111 if ( xQueueSend( mbox, &msg, 0 ) == pdPASS )
\r
116 // could not post, queue must be full
\r
120 lwip_stats.sys.mbox.err++;
\r
121 #endif /* SYS_STATS */
\r
128 /*-----------------------------------------------------------------------------------*/
\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
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
141 Note that a function with a similar name, sys_mbox_fetch(), is
\r
142 implemented by lwIP.
\r
144 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)
\r
147 portTickType StartTime, EndTime, Elapsed;
\r
149 StartTime = xTaskGetTickCount();
\r
156 if ( timeout != 0 )
\r
158 if ( pdTRUE == xQueueReceive( mbox, &(*msg), timeout / portTICK_RATE_MS ) )
\r
160 EndTime = xTaskGetTickCount();
\r
161 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
163 return ( Elapsed );
\r
165 else // timed out blocking for message
\r
169 return SYS_ARCH_TIMEOUT;
\r
172 else // block forever for a message.
\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
178 return ( Elapsed ); // return time blocked TODO test
\r
182 /*-----------------------------------------------------------------------------------*/
\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
187 u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg)
\r
196 if ( pdTRUE == xQueueReceive( mbox, &(*msg), 0 ) )
\r
202 return SYS_MBOX_EMPTY;
\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
211 xSemaphoreHandle xSemaphore;
\r
213 vSemaphoreCreateBinary( xSemaphore );
\r
215 if( xSemaphore == NULL )
\r
219 ++lwip_stats.sys.sem.err;
\r
220 #endif /* SYS_STATS */
\r
222 return SYS_SEM_NULL; // TODO need assert
\r
225 if(count == 0) // Means it can't be taken
\r
227 xSemaphoreTake(xSemaphore,1);
\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
235 #endif /* SYS_STATS */
\r
240 /*-----------------------------------------------------------------------------------*/
\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
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
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
256 u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)
\r
258 portTickType StartTime, EndTime, Elapsed;
\r
260 StartTime = xTaskGetTickCount();
\r
264 if( xSemaphoreTake( sem, timeout / portTICK_RATE_MS ) == pdTRUE )
\r
266 EndTime = xTaskGetTickCount();
\r
267 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
269 return (Elapsed); // return time blocked TODO test
\r
273 return SYS_ARCH_TIMEOUT;
\r
276 else // must block without a timeout
\r
278 while( xSemaphoreTake( sem, portMAX_DELAY ) != pdTRUE );
\r
279 EndTime = xTaskGetTickCount();
\r
280 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
282 return ( Elapsed ); // return time blocked
\r
287 /*-----------------------------------------------------------------------------------*/
\r
288 // Signals a semaphore
\r
289 void sys_sem_signal(sys_sem_t sem)
\r
291 xSemaphoreGive( sem );
\r
294 /*-----------------------------------------------------------------------------------*/
\r
295 // Deallocates a semaphore
\r
296 void sys_sem_free(sys_sem_t sem)
\r
299 --lwip_stats.sys.sem.used;
\r
300 #endif /* SYS_STATS */
\r
302 vQueueDelete( sem );
\r
305 /*-----------------------------------------------------------------------------------*/
\r
306 // Initialize sys arch
\r
307 void sys_init(void)
\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
315 s_timeoutlist[i].pid = 0;
\r
316 s_timeoutlist[i].timeouts.next = NULL;
\r
319 // keep track of how many threads have been created
\r
323 /*-----------------------------------------------------------------------------------*/
\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
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
335 struct sys_timeouts *sys_arch_timeouts(void)
\r
339 struct timeoutlist *tl;
\r
341 pid = xTaskGetCurrentTaskHandle( );
\r
343 for(i = 0; i < s_nextthread; i++)
\r
345 tl = &(s_timeoutlist[i]);
\r
348 return &(tl->timeouts);
\r
356 /*-----------------------------------------------------------------------------------*/
\r
357 /*-----------------------------------------------------------------------------------*/
\r
359 /*-----------------------------------------------------------------------------------*/
\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
366 sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio)
\r
368 xTaskHandle CreatedTask;
\r
371 if ( s_nextthread < SYS_THREAD_MAX )
\r
373 result = xTaskCreate( thread, ( signed portCHAR * ) name, stacksize, arg, prio, &CreatedTask );
\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
379 if(result == pdPASS)
\r
381 return CreatedTask;
\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
404 sys_arch_protect() is only required if your port is supporting an operating
\r
407 sys_prot_t sys_arch_protect(void)
\r
409 vPortEnterCritical();
\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
419 void sys_arch_unprotect(sys_prot_t pval)
\r
422 vPortExitCritical();
\r
426 * Prints an assertion messages and aborts execution.
\r
428 void sys_assert( const char *msg )
\r
430 /*FSL:only needed for debugging
\r
434 vPortEnterCritical( );
\r