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
61 mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) );
\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
68 #endif /* SYS_STATS */
\r
73 /*-----------------------------------------------------------------------------------*/
\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
79 void sys_mbox_free(sys_mbox_t mbox)
\r
81 if( uxQueueMessagesWaiting( mbox ) )
\r
83 /* Line for breakpoint. Should never break here! */
\r
86 lwip_stats.sys.mbox.err++;
\r
87 #endif /* SYS_STATS */
\r
89 // TODO notify the user of failure.
\r
92 vQueueDelete( mbox );
\r
95 --lwip_stats.sys.mbox.used;
\r
96 #endif /* SYS_STATS */
\r
99 /*-----------------------------------------------------------------------------------*/
\r
100 // Posts the "msg" to the mailbox.
\r
101 void sys_mbox_post(sys_mbox_t mbox, void *data)
\r
103 while ( xQueueSendToBack(mbox, &data, portMAX_DELAY ) != pdTRUE ){}
\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
113 if ( xQueueSend( mbox, &msg, 0 ) == pdPASS )
\r
118 // could not post, queue must be full
\r
122 lwip_stats.sys.mbox.err++;
\r
123 #endif /* SYS_STATS */
\r
130 /*-----------------------------------------------------------------------------------*/
\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
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
143 Note that a function with a similar name, sys_mbox_fetch(), is
\r
144 implemented by lwIP.
\r
146 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)
\r
149 portTickType StartTime, EndTime, Elapsed;
\r
151 StartTime = xTaskGetTickCount();
\r
158 if ( timeout != 0 )
\r
160 if ( pdTRUE == xQueueReceive( mbox, &(*msg), timeout / portTICK_RATE_MS ) )
\r
162 EndTime = xTaskGetTickCount();
\r
163 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
165 return ( Elapsed );
\r
167 else // timed out blocking for message
\r
171 return SYS_ARCH_TIMEOUT;
\r
174 else // block forever for a message.
\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
180 return ( Elapsed ); // return time blocked TODO test
\r
184 /*-----------------------------------------------------------------------------------*/
\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
189 u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg)
\r
198 if ( pdTRUE == xQueueReceive( mbox, &(*msg), 0 ) )
\r
204 return SYS_MBOX_EMPTY;
\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
213 xSemaphoreHandle xSemaphore;
\r
215 vSemaphoreCreateBinary( xSemaphore );
\r
217 if( xSemaphore == NULL )
\r
221 ++lwip_stats.sys.sem.err;
\r
222 #endif /* SYS_STATS */
\r
224 return SYS_SEM_NULL; // TODO need assert
\r
227 if(count == 0) // Means it can't be taken
\r
229 xSemaphoreTake(xSemaphore,1);
\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
237 #endif /* SYS_STATS */
\r
242 /*-----------------------------------------------------------------------------------*/
\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
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
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
258 u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)
\r
260 portTickType StartTime, EndTime, Elapsed;
\r
262 StartTime = xTaskGetTickCount();
\r
266 if( xSemaphoreTake( sem, timeout / portTICK_RATE_MS ) == pdTRUE )
\r
268 EndTime = xTaskGetTickCount();
\r
269 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
271 return (Elapsed); // return time blocked TODO test
\r
275 return SYS_ARCH_TIMEOUT;
\r
278 else // must block without a timeout
\r
280 while( xSemaphoreTake( sem, portMAX_DELAY ) != pdTRUE ){}
\r
281 EndTime = xTaskGetTickCount();
\r
282 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
284 return ( Elapsed ); // return time blocked
\r
289 /*-----------------------------------------------------------------------------------*/
\r
290 // Signals a semaphore
\r
291 void sys_sem_signal(sys_sem_t sem)
\r
293 xSemaphoreGive( sem );
\r
296 /*-----------------------------------------------------------------------------------*/
\r
297 // Deallocates a semaphore
\r
298 void sys_sem_free(sys_sem_t sem)
\r
301 --lwip_stats.sys.sem.used;
\r
302 #endif /* SYS_STATS */
\r
304 vQueueDelete( sem );
\r
307 /*-----------------------------------------------------------------------------------*/
\r
308 // Initialize sys arch
\r
309 void sys_init(void)
\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
317 s_timeoutlist[i].pid = 0;
\r
318 s_timeoutlist[i].timeouts.next = NULL;
\r
321 // keep track of how many threads have been created
\r
325 /*-----------------------------------------------------------------------------------*/
\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
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
337 struct sys_timeouts *sys_arch_timeouts(void)
\r
341 struct timeoutlist *tl;
\r
343 pid = xTaskGetCurrentTaskHandle( );
\r
345 for(i = 0; i < s_nextthread; i++)
\r
347 tl = &(s_timeoutlist[i]);
\r
350 return &(tl->timeouts);
\r
358 /*-----------------------------------------------------------------------------------*/
\r
359 /*-----------------------------------------------------------------------------------*/
\r
361 /*-----------------------------------------------------------------------------------*/
\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
368 sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio)
\r
370 xTaskHandle CreatedTask;
\r
373 if ( s_nextthread < SYS_THREAD_MAX )
\r
375 result = xTaskCreate( thread, ( signed portCHAR * ) name, stacksize, arg, prio, &CreatedTask );
\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
381 if(result == pdPASS)
\r
383 return CreatedTask;
\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
406 sys_arch_protect() is only required if your port is supporting an operating
\r
409 sys_prot_t sys_arch_protect(void)
\r
411 vPortEnterCritical();
\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
421 void sys_arch_unprotect(sys_prot_t pval)
\r
424 vPortExitCritical();
\r
428 * Prints an assertion messages and aborts execution.
\r
430 void sys_assert( const char *msg )
\r
433 /*FSL:only needed for debugging
\r
437 vPortEnterCritical( );
\r