2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
\r
3 * Modifications Copyright (c) 2006 Christian Walter <wolti@sil.at>
\r
4 * All rights reserved.
\r
6 * Redistribution and use in source and binary forms, with or without modification,
\r
7 * are permitted provided that the following conditions are met:
\r
9 * 1. Redistributions of source code must retain the above copyright notice,
\r
10 * this list of conditions and the following disclaimer.
\r
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
12 * this list of conditions and the following disclaimer in the documentation
\r
13 * and/or other materials provided with the distribution.
\r
14 * 3. The name of the author may not be used to endorse or promote products
\r
15 * derived from this software without specific prior written permission.
\r
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
\r
20 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
\r
22 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
\r
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
\r
28 * This file is part of the lwIP TCP/IP stack.
\r
30 * Author: Adam Dunkels <adam@sics.se>
\r
31 * Modifcations: Christian Walter <wolti@sil.at>
\r
33 * $Id: sys_arch.c,v 1.6 2006/09/24 22:04:53 wolti Exp $
\r
36 /* ------------------------ System includes ------------------------------- */
\r
42 /* ------------------------ FreeRTOS includes ----------------------------- */
\r
43 #include "FreeRTOS.h"
\r
47 /* ------------------------ lwIP includes --------------------------------- */
\r
48 #include "lwip/debug.h"
\r
49 #include "lwip/def.h"
\r
50 #include "lwip/sys.h"
\r
51 #include "lwip/mem.h"
\r
52 #include "lwip/sio.h"
\r
53 #include "lwip/stats.h"
\r
55 /* ------------------------ Project includes ------------------------------ */
\r
57 /* ------------------------ Defines --------------------------------------- */
\r
58 /* This is the number of threads that can be started with sys_thead_new() */
\r
59 #define SYS_MBOX_SIZE ( 16 )
\r
60 #define MS_TO_TICKS( ms ) \
\r
61 ( portTickType )( ( portTickType ) ( ms ) / portTICK_RATE_MS )
\r
62 #define TICKS_TO_MS( ticks ) \
\r
63 ( unsigned long )( ( portTickType ) ( ticks ) * portTICK_RATE_MS )
\r
64 #define THREAD_STACK_SIZE ( 1024 )
\r
65 #define THREAD_NAME "lwIP"
\r
67 #define THREAD_INIT( tcb ) \
\r
70 tcb->pid = ( xTaskHandle )0; \
\r
71 tcb->timeouts.next = NULL; \
\r
74 /* ------------------------ Type definitions ------------------------------ */
\r
75 typedef struct sys_tcb
\r
77 struct sys_tcb *next;
\r
78 struct sys_timeouts timeouts;
\r
82 /* ------------------------ Prototypes ------------------------------------ */
\r
84 /* ------------------------ Static functions ------------------------------ */
\r
85 sys_tcb_t *sys_thread_current( void );
\r
87 /* ------------------------ Static variables ------------------------------ */
\r
88 static sys_tcb_t *tasks = NULL;
\r
90 /* ------------------------ Start implementation -------------------------- */
\r
94 LWIP_ASSERT( "sys_init: not called first", tasks == NULL );
\r
99 * This optional function does a "fast" critical region protection and returns
\r
100 * the previous protection level. This function is only called during very short
\r
101 * critical regions. An embedded system which supports ISR-based drivers might
\r
102 * want to implement this function by disabling interrupts. Task-based systems
\r
103 * might want to implement this by using a mutex or disabling tasking. This
\r
104 * function should support recursive calls from the same task or interrupt. In
\r
105 * other words, sys_arch_protect() could be called while already protected. In
\r
106 * that case the return value indicates that it is already protected.
\r
108 * sys_arch_protect() is only required if your port is supporting an operating
\r
112 sys_arch_protect( void )
\r
114 vPortEnterCritical( );
\r
119 * This optional function does a "fast" set of critical region protection to the
\r
120 * value specified by pval. See the documentation for sys_arch_protect() for
\r
121 * more information. This function is only required if your port is supporting
\r
122 * an operating system.
\r
125 sys_arch_unprotect( sys_prot_t pval )
\r
128 vPortExitCritical( );
\r
132 * Prints an assertion messages and aborts execution.
\r
135 sys_assert( const char *msg )
\r
137 fputs( msg, stderr );
\r
138 fputs( "\n\r", stderr );
\r
139 vPortEnterCritical( );
\r
144 sys_debug( const char *const fmt, ... )
\r
148 va_start( ap, fmt );
\r
149 ( void )vprintf( fmt, ap );
\r
150 ( void )putchar( '\r' );
\r
154 /* ------------------------ Start implementation ( Threads ) -------------- */
\r
157 sys_thread_new( void ( *thread ) ( void *arg ), void *arg, int prio )
\r
159 return sys_arch_thread_new( thread, arg, prio, THREAD_STACK_SIZE );
\r
163 * Starts a new thread with priority "prio" that will begin its execution in the
\r
164 * function "thread()". The "arg" argument will be passed as an argument to the
\r
165 * thread() function. The argument "ssize" is the requested stack size for the
\r
166 * new thread. The id of the new thread is returned. Both the id and the
\r
167 * priority are system dependent.
\r
170 sys_arch_thread_new( void ( *thread ) ( void *arg ), void *arg, int prio, size_t ssize )
\r
172 sys_thread_t thread_hdl = SYS_THREAD_NULL;
\r
175 char thread_name[ configMAX_TASK_NAME_LEN ];
\r
177 /* We disable the FreeRTOS scheduler because it might be the case that the new
\r
178 * tasks gets scheduled inside the xTaskCreate function. To prevent this we
\r
179 * disable the scheduling. Note that this can happen although we have interrupts
\r
180 * disabled because xTaskCreate contains a call to taskYIELD( ).
\r
182 vPortEnterCritical( );
\r
186 /* We are called the first time. Initialize it. */
\r
189 p = pvPortMalloc( sizeof( sys_tcb_t ) );
\r
197 /* First task already counter. */
\r
199 /* Cycle to the end of the list. */
\r
200 while( p->next != NULL )
\r
205 p->next = pvPortMalloc( sizeof( sys_tcb_t ) );
\r
211 /* Memory allocated. Initialize the data structure. */
\r
213 ( void )snprintf( thread_name, configMAX_TASK_NAME_LEN, "lwIP%d", i );
\r
215 /* Now q points to a free element in the list. */
\r
216 if( xTaskCreate( thread, thread_name, ssize, arg, prio, &p->pid ) == pdPASS )
\r
226 vPortExitCritical( );
\r
231 sys_arch_thread_remove( sys_thread_t hdl )
\r
233 sys_tcb_t *current = tasks, *prev;
\r
234 sys_tcb_t *toremove = hdl;
\r
235 xTaskHandle pid = ( xTaskHandle ) 0;
\r
237 LWIP_ASSERT( "sys_arch_thread_remove: assertion hdl != NULL failed!", hdl != NULL );
\r
239 /* If we have to remove the first task we must update the global "tasks"
\r
241 vPortEnterCritical( );
\r
245 while( ( current != NULL ) && ( current != toremove ) )
\r
248 current = current->next;
\r
251 if( current == toremove )
\r
253 /* Not the first entry in the list. */
\r
256 prev->next = toremove->next;
\r
260 tasks = toremove->next;
\r
262 LWIP_ASSERT( "sys_arch_thread_remove: can't remove thread with timeouts!",
\r
263 toremove->timeouts.next == NULL );
\r
264 pid = toremove->pid;
\r
265 THREAD_INIT( toremove );
\r
266 vPortFree( toremove );
\r
269 /* We are done with accessing the shared datastructure. Release the
\r
272 vPortExitCritical( );
\r
273 if( pid != ( xTaskHandle ) 0 )
\r
275 vTaskDelete( pid );
\r
281 * Returns the thread control block for the currently active task. In case
\r
282 * of an error the functions returns NULL.
\r
285 sys_arch_thread_current( void )
\r
287 sys_tcb_t *p = tasks;
\r
288 xTaskHandle pid = xTaskGetCurrentTaskHandle( );
\r
290 vPortEnterCritical( );
\r
291 while( ( p != NULL ) && ( p->pid != pid ) )
\r
295 vPortExitCritical( );
\r
300 * Returns a pointer to the per-thread sys_timeouts structure. In lwIP,
\r
301 * each thread has a list of timeouts which is represented as a linked
\r
302 * list of sys_timeout structures. The sys_timeouts structure holds a
\r
303 * pointer to a linked list of timeouts. This function is called by
\r
304 * the lwIP timeout scheduler and must not return a NULL value.
\r
306 * In a single threaded sys_arch implementation, this function will
\r
307 * simply return a pointer to a global sys_timeouts variable stored in
\r
308 * the sys_arch module.
\r
310 struct sys_timeouts *
\r
311 sys_arch_timeouts( void )
\r
315 ptask = sys_arch_thread_current( );
\r
316 LWIP_ASSERT( "sys_arch_timeouts: ptask != NULL", ptask != NULL );
\r
317 return ptask != NULL ? &( ptask->timeouts ) : NULL;
\r
320 /* ------------------------ Start implementation ( Semaphores ) ----------- */
\r
322 /* Creates and returns a new semaphore. The "count" argument specifies
\r
323 * the initial state of the semaphore.
\r
326 sys_sem_new( u8_t count )
\r
328 xSemaphoreHandle xSemaphore;
\r
330 vSemaphoreCreateBinary( xSemaphore );
\r
331 if( xSemaphore != SYS_SEM_NULL )
\r
335 xSemaphoreTake( xSemaphore, 1 );
\r
338 vPortEnterCritical( );
\r
339 lwip_stats.sys.sem.used++;
\r
340 if( lwip_stats.sys.sem.used > lwip_stats.sys.sem.max )
\r
342 lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
\r
344 vPortExitCritical( );
\r
349 LWIP_ASSERT( "sys_sem_new: xSemaphore == SYS_SEM_NULL", xSemaphore != SYS_SEM_NULL );
\r
355 /* Deallocates a semaphore */
\r
357 sys_sem_free( sys_sem_t sem )
\r
359 LWIP_ASSERT( "sys_sem_free: sem != SYS_SEM_NULL", sem != SYS_SEM_NULL );
\r
360 if( sem != SYS_SEM_NULL )
\r
363 vPortEnterCritical( );
\r
364 lwip_stats.sys.sem.used--;
\r
365 vPortExitCritical( );
\r
367 vQueueDelete( sem );
\r
371 /* Signals a semaphore */
\r
373 sys_sem_signal( sys_sem_t sem )
\r
375 LWIP_ASSERT( "sys_sem_signal: sem != SYS_SEM_NULL", sem != SYS_SEM_NULL );
\r
376 xSemaphoreGive( sem );
\r
380 * Blocks the thread while waiting for the semaphore to be
\r
381 * signaled. If the "timeout" argument is non-zero, the thread should
\r
382 * only be blocked for the specified time (measured in
\r
385 * If the timeout argument is non-zero, the return value is the number of
\r
386 * milliseconds spent waiting for the semaphore to be signaled. If the
\r
387 * semaphore wasn't signaled within the specified time, the return value is
\r
388 * SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
\r
389 * (i.e., it was already signaled), the function may return zero.
\r
391 * Notice that lwIP implements a function with a similar name,
\r
392 * sys_sem_wait(), that uses the sys_arch_sem_wait() function.
\r
395 sys_arch_sem_wait( sys_sem_t sem, u32_t timeout )
\r
397 portBASE_TYPE xStatus;
\r
398 portTickType xTicksStart, xTicksEnd, xTicksElapsed;
\r
401 LWIP_ASSERT( "sys_arch_sem_wait: sem != SYS_SEM_NULL", sem != SYS_SEM_NULL );
\r
402 xTicksStart = xTaskGetTickCount( );
\r
407 xStatus = xSemaphoreTake( sem, MS_TO_TICKS( 100 ) );
\r
409 while( xStatus != pdTRUE );
\r
413 xStatus = xSemaphoreTake( sem, MS_TO_TICKS( timeout ) );
\r
416 /* Semaphore was signaled. */
\r
417 if( xStatus == pdTRUE )
\r
419 xTicksEnd = xTaskGetTickCount( );
\r
420 xTicksElapsed = xTicksEnd - xTicksStart;
\r
421 timespent = TICKS_TO_MS( xTicksElapsed );
\r
425 timespent = SYS_ARCH_TIMEOUT;
\r
431 /* ------------------------ Start implementation ( Mailboxes ) ------------ */
\r
433 /* Creates an empty mailbox. */
\r
435 sys_mbox_new( void )
\r
439 mbox = xQueueCreate( SYS_MBOX_SIZE, sizeof( void * ) );
\r
440 if( mbox != SYS_MBOX_NULL )
\r
443 vPortEnterCritical( );
\r
444 lwip_stats.sys.mbox.used++;
\r
445 if( lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max )
\r
447 lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
\r
449 vPortExitCritical( );
\r
456 Deallocates a mailbox. If there are messages still present in the
\r
457 mailbox when the mailbox is deallocated, it is an indication of a
\r
458 programming error in lwIP and the developer should be notified.
\r
461 sys_mbox_free( sys_mbox_t mbox )
\r
465 LWIP_ASSERT( "sys_mbox_free: mbox != SYS_MBOX_NULL", mbox != SYS_MBOX_NULL );
\r
466 if( mbox != SYS_MBOX_NULL )
\r
468 while( uxQueueMessagesWaiting( mbox ) != 0 )
\r
470 if( sys_arch_mbox_fetch( mbox, &msg, 1 ) != SYS_ARCH_TIMEOUT )
\r
472 LWIP_ASSERT( "sys_mbox_free: memory leak (msg != NULL)", msg == NULL );
\r
475 vQueueDelete( mbox );
\r
477 vPortEnterCritical( );
\r
478 lwip_stats.sys.mbox.used--;
\r
479 vPortExitCritical( );
\r
485 * This function sends a message to a mailbox. It is unusual in that no error
\r
486 * return is made. This is because the caller is responsible for ensuring that
\r
487 * the mailbox queue will not fail. The caller does this by limiting the number
\r
488 * of msg structures which exist for a given mailbox.
\r
491 sys_mbox_post( sys_mbox_t mbox, void *data )
\r
493 portBASE_TYPE xQueueSent;
\r
495 /* Queue must not be full - Otherwise it is an error. */
\r
496 xQueueSent = xQueueSend( mbox, &data, 0 );
\r
497 LWIP_ASSERT( "sys_mbox_post: xQueueSent == pdPASS", xQueueSent == pdPASS );
\r
501 * Blocks the thread until a message arrives in the mailbox, but does
\r
502 * not block the thread longer than "timeout" milliseconds (similar to
\r
503 * the sys_arch_sem_wait() function). The "msg" argument is a result
\r
504 * parameter that is set by the function (i.e., by doing "*msg =
\r
505 * ptr"). The "msg" parameter maybe NULL to indicate that the message
\r
506 * should be dropped.
\r
508 * Note that a function with a similar name, sys_mbox_fetch(), is
\r
509 * implemented by lwIP.
\r
512 sys_arch_mbox_fetch( sys_mbox_t mbox, void **msg, u32_t timeout )
\r
515 portBASE_TYPE xStatus;
\r
516 portTickType xTicksStart, xTicksEnd, xTicksElapsed;
\r
519 LWIP_ASSERT( "sys_arch_mbox_fetch: mbox != SYS_MBOX_NULL", mbox != SYS_MBOX_NULL );
\r
520 xTicksStart = xTaskGetTickCount( );
\r
525 xStatus = xQueueReceive( mbox, &ret_msg, MS_TO_TICKS( 100 ) );
\r
527 while( xStatus != pdTRUE );
\r
531 xStatus = xQueueReceive( mbox, &ret_msg, MS_TO_TICKS( timeout ) );
\r
534 if( xStatus == pdTRUE )
\r
540 xTicksEnd = xTaskGetTickCount( );
\r
541 xTicksElapsed = xTicksEnd - xTicksStart;
\r
542 timespent = TICKS_TO_MS( xTicksElapsed );
\r
550 timespent = SYS_ARCH_TIMEOUT;
\r
556 sys_jiffies( void )
\r
558 portTickType xTicks = xTaskGetTickCount( );
\r
560 return ( u32_t )TICKS_TO_MS( xTicks );
\r