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 //*****************************************************************************
\r
35 // Include OS functionality.
\r
37 //*****************************************************************************
\r
39 /* ------------------------ System architecture includes ----------------------------- */
\r
40 #include "arch/sys_arch.h"
\r
42 /* ------------------------ lwIP includes --------------------------------- */
\r
43 #include "lwip/opt.h"
\r
45 #include "lwip/debug.h"
\r
46 #include "lwip/def.h"
\r
47 #include "lwip/sys.h"
\r
48 #include "lwip/mem.h"
\r
49 #include "lwip/stats.h"
\r
51 /*---------------------------------------------------------------------------*
\r
53 *---------------------------------------------------------------------------*/
\r
59 struct sys_timeouts timeouts;
\r
63 /* This is the number of threads that can be started with sys_thread_new() */
\r
64 #define SYS_THREAD_MAX 4
\r
66 static u16_t s_nextthread = 0;
\r
68 static struct timeoutlist s_timeoutlist[SYS_THREAD_MAX];
\r
71 /*-----------------------------------------------------------------------------------*/
\r
73 /*---------------------------------------------------------------------------*
\r
74 * Routine: sys_mbox_new
\r
75 *---------------------------------------------------------------------------*
\r
77 * Creates a new mailbox
\r
79 * int size -- Size of elements in the mailbox
\r
81 * sys_mbox_t -- Handle to new mailbox
\r
82 *---------------------------------------------------------------------------*/
\r
83 err_t sys_mbox_new(sys_mbox_t *mbox, int size)
\r
85 err_t lwip_err= ERR_MEM;
\r
87 *mbox = xQueueCreate( size, sizeof( void * ) );
\r
89 // Created succesfully?
\r
94 SYS_STATS_INC(mbox.used);
\r
95 #endif /* SYS_STATS */
\r
102 /*---------------------------------------------------------------------------*
\r
103 * Routine: sys_mbox_free
\r
104 *---------------------------------------------------------------------------*
\r
106 * Deallocates a mailbox. If there are messages still present in the
\r
107 * mailbox when the mailbox is deallocated, it is an indication of a
\r
108 * programming error in lwIP and the developer should be notified.
\r
110 * sys_mbox_t mbox -- Handle of mailbox
\r
112 * sys_mbox_t -- Handle to new mailbox
\r
113 *---------------------------------------------------------------------------*/
\r
114 void sys_mbox_free(sys_mbox_t *mbox)
\r
116 unsigned portBASE_TYPE uxMessagesWaiting;
\r
118 uxMessagesWaiting = uxQueueMessagesWaiting( *mbox );
\r
119 configASSERT( ( uxMessagesWaiting == 0 ) );
\r
122 if (uxMessagesWaiting != 0U)
\r
124 SYS_STATS_INC(mbox.err);
\r
127 SYS_STATS_DEC(mbox.used);
\r
128 #endif /* SYS_STATS */
\r
130 vQueueDelete( *mbox );
\r
133 /*---------------------------------------------------------------------------*
\r
134 * Routine: sys_mbox_post
\r
135 *---------------------------------------------------------------------------*
\r
137 * Post the "msg" to the mailbox.
\r
139 * sys_mbox_t mbox -- Handle of mailbox
\r
140 * void *data -- Pointer to data to post
\r
141 *---------------------------------------------------------------------------*/
\r
142 void sys_mbox_post(sys_mbox_t *mbox, void *msg)
\r
144 while( xQueueSendToBack( *mbox, &msg, portMAX_DELAY ) != pdTRUE );
\r
147 /*---------------------------------------------------------------------------*
\r
148 * Routine: sys_mbox_trypost
\r
149 *---------------------------------------------------------------------------*
\r
151 * Try to post the "msg" to the mailbox. Returns immediately with
\r
154 * sys_mbox_t mbox -- Handle of mailbox
\r
155 * void *msg -- Pointer to data to post
\r
157 * err_t -- ERR_OK if message posted, else ERR_MEM
\r
159 *---------------------------------------------------------------------------*/
\r
160 err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
\r
164 if ( xQueueSend( *mbox, &msg, 0 ) == pdPASS )
\r
170 // could not post, queue must be full
\r
173 SYS_STATS_INC(mbox.err);
\r
174 #endif /* SYS_STATS */
\r
179 /*---------------------------------------------------------------------------*
\r
180 * Routine: sys_arch_mbox_fetch
\r
181 *---------------------------------------------------------------------------*
\r
183 * Blocks the thread until a message arrives in the mailbox, but does
\r
184 * not block the thread longer than "timeout" milliseconds (similar to
\r
185 * the sys_arch_sem_wait() function). The "msg" argument is a result
\r
186 * parameter that is set by the function (i.e., by doing "*msg =
\r
187 * ptr"). The "msg" parameter maybe NULL to indicate that the message
\r
188 * should be dropped.
\r
190 * The return values are the same as for the sys_arch_sem_wait() function:
\r
191 * Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
\r
194 * Note that a function with a similar name, sys_mbox_fetch(), is
\r
195 * implemented by lwIP.
\r
197 * sys_mbox_t mbox -- Handle of mailbox
\r
198 * void **msg -- Pointer to pointer to msg received
\r
199 * u32_t timeout -- Number of milliseconds until timeout
\r
201 * u32_t -- SYS_ARCH_TIMEOUT if timeout, else number
\r
202 * of milliseconds until received.
\r
203 *---------------------------------------------------------------------------*/
\r
204 u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
\r
207 portTickType StartTime, EndTime, Elapsed;
\r
209 StartTime = xTaskGetTickCount();
\r
218 if ( pdTRUE == xQueueReceive( *mbox, &(*msg), timeout / portTICK_RATE_MS ) )
\r
220 EndTime = xTaskGetTickCount();
\r
221 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
223 return ( Elapsed );
\r
225 else // timed out blocking for message
\r
229 return SYS_ARCH_TIMEOUT;
\r
234 while( pdTRUE != xQueueReceive( mbox, &(*msg), portMAX_DELAY ) ); // time is arbitrary
\r
235 EndTime = xTaskGetTickCount();
\r
236 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
243 // return time blocked TBD test
\r
248 /*---------------------------------------------------------------------------*
\r
249 * Routine: sys_arch_mbox_tryfetch
\r
250 *---------------------------------------------------------------------------*
\r
252 * Similar to sys_arch_mbox_fetch, but if message is not ready
\r
253 * immediately, we'll return with SYS_MBOX_EMPTY. On success, 0 is
\r
256 * sys_mbox_t mbox -- Handle of mailbox
\r
257 * void **msg -- Pointer to pointer to msg received
\r
259 * u32_t -- SYS_MBOX_EMPTY if no messages. Otherwise,
\r
261 *---------------------------------------------------------------------------*/
\r
262 u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
\r
271 if ( pdTRUE == xQueueReceive( *mbox, &(*msg), 0 ) )
\r
277 return SYS_MBOX_EMPTY;
\r
281 /*---------------------------------------------------------------------------*
\r
282 * Routine: sys_sem_new
\r
283 *---------------------------------------------------------------------------*
\r
285 * Creates and returns a new semaphore. The "count" argument specifies
\r
286 * the initial state of the semaphore.
\r
287 * NOTE: Currently this routine only creates counts of 1 or 0
\r
289 * sys_mbox_t mbox -- Handle of mailbox
\r
290 * u8_t count -- Initial count of semaphore (1 or 0)
\r
292 * sys_sem_t -- Created semaphore or 0 if could not create.
\r
293 *---------------------------------------------------------------------------*/
\r
294 err_t sys_sem_new(sys_sem_t *sem, u8_t count)
\r
296 err_t lwip_err = ERR_MEM;
\r
298 vSemaphoreCreateBinary( (*sem) );
\r
302 // Means it can't be taken
\r
305 xSemaphoreTake(*sem, 1);
\r
311 SYS_STATS_INC(sem.used);
\r
317 SYS_STATS_INC(sem.err);
\r
324 /*---------------------------------------------------------------------------*
\r
325 * Routine: sys_arch_sem_wait
\r
326 *---------------------------------------------------------------------------*
\r
328 * Blocks the thread while waiting for the semaphore to be
\r
329 * signaled. If the "timeout" argument is non-zero, the thread should
\r
330 * only be blocked for the specified time (measured in
\r
333 * If the timeout argument is non-zero, the return value is the number of
\r
334 * milliseconds spent waiting for the semaphore to be signaled. If the
\r
335 * semaphore wasn't signaled within the specified time, the return value is
\r
336 * SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
\r
337 * (i.e., it was already signaled), the function may return zero.
\r
339 * Notice that lwIP implements a function with a similar name,
\r
340 * sys_sem_wait(), that uses the sys_arch_sem_wait() function.
\r
342 * sys_sem_t sem -- Semaphore to wait on
\r
343 * u32_t timeout -- Number of milliseconds until timeout
\r
345 * u32_t -- Time elapsed or SYS_ARCH_TIMEOUT.
\r
346 *---------------------------------------------------------------------------*/
\r
347 u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
\r
349 portTickType StartTime, EndTime, Elapsed;
\r
351 StartTime = xTaskGetTickCount();
\r
355 if( xSemaphoreTake( *sem, timeout / portTICK_RATE_MS ) == pdTRUE )
\r
357 EndTime = xTaskGetTickCount();
\r
358 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
360 return (Elapsed); // return time blocked TODO test
\r
364 return SYS_ARCH_TIMEOUT;
\r
370 while( xSemaphoreTake( sem, portMAX_DELAY ) != pdTRUE );
\r
371 EndTime = xTaskGetTickCount();
\r
372 Elapsed = (EndTime - StartTime) * portTICK_RATE_MS;
\r
379 // return time blocked
\r
384 /** Create a new mutex
\r
385 * @param mutex pointer to the mutex to create
\r
386 * @return a new mutex */
\r
387 err_t sys_mutex_new(sys_mutex_t *mutex)
\r
389 err_t lwip_err = ERR_MEM;
\r
391 *mutex = xQueueCreateMutex();
\r
393 if( *mutex != NULL )
\r
397 SYS_STATS_INC(mutex.used);
\r
403 SYS_STATS_INC(mutex.err);
\r
411 * @param mutex the mutex to lock */
\r
412 void sys_mutex_lock(sys_mutex_t *mutex)
\r
414 while( xSemaphoreTake( *mutex, portMAX_DELAY ) != pdPASS );
\r
418 * @param mutex the mutex to unlock */
\r
419 void sys_mutex_unlock(sys_mutex_t *mutex)
\r
421 xSemaphoreGive(*mutex);
\r
425 /** Delete a semaphore
\r
426 * @param mutex the mutex to delete */
\r
427 void sys_mutex_free(sys_mutex_t *mutex)
\r
430 SYS_STATS_DEC(mutex.used);
\r
431 #endif /* SYS_STATS */
\r
432 vQueueDelete(*mutex);
\r
436 /*---------------------------------------------------------------------------*
\r
437 * Routine: sys_sem_signal
\r
438 *---------------------------------------------------------------------------*
\r
440 * Signals (releases) a semaphore
\r
442 * sys_sem_t sem -- Semaphore to signal
\r
443 *---------------------------------------------------------------------------*/
\r
444 void sys_sem_signal(sys_sem_t * sem)
\r
446 //LWIP_ASSERT( "sys_sem_signal: sem != SYS_SEM_NULL", sem != SYS_SEM_NULL );
\r
447 xSemaphoreGive(*sem);
\r
450 /*---------------------------------------------------------------------------*
\r
451 * Routine: sys_sem_free
\r
452 *---------------------------------------------------------------------------*
\r
454 * Deallocates a semaphore
\r
456 * sys_sem_t sem -- Semaphore to free
\r
457 *---------------------------------------------------------------------------*/
\r
458 void sys_sem_free(sys_sem_t * sem)
\r
460 //LWIP_ASSERT( "sys_sem_free: sem != SYS_SEM_NULL", sem != SYS_SEM_NULL );
\r
463 SYS_STATS_DEC(sem.used);
\r
464 #endif /* SYS_STATS */
\r
466 vQueueDelete(*sem);
\r
469 /*---------------------------------------------------------------------------*
\r
470 * Routine: sys_init
\r
471 *---------------------------------------------------------------------------*
\r
473 * Initialize sys arch
\r
474 *---------------------------------------------------------------------------*/
\r
475 void sys_init(void)
\r
480 // Initialize the the per-thread sys_timeouts structures
\r
481 // make sure there are no valid pids in the list
\r
482 for (i = 0; i < SYS_THREAD_MAX; i++)
\r
484 s_timeoutlist[i].pid = SYS_THREAD_NULL;
\r
485 // s_timeoutlist[i].timeouts.next = NULL;
\r
488 // keep track of how many threads have been created
\r
493 u32_t sys_now(void)
\r
495 return xTaskGetTickCount();
\r
500 u32_t sys_jiffies(void)
\r
502 return UEZTickCounterGet();
\r
506 /*---------------------------------------------------------------------------*
\r
507 * Routine: sys_arch_timeouts
\r
508 *---------------------------------------------------------------------------*
\r
510 * Returns a pointer to the per-thread sys_timeouts structure. In lwIP,
\r
511 * each thread has a list of timeouts which is represented as a linked
\r
512 * list of sys_timeout structures. The sys_timeouts structure holds a
\r
513 * pointer to a linked list of timeouts. This function is called by
\r
514 * the lwIP timeout scheduler and must not return a NULL value.
\r
516 * In a single threaded sys_arch implementation, this function will
\r
517 * simply return a pointer to a global sys_timeouts variable stored in
\r
518 * the sys_arch module.
\r
520 * sys_timeouts * -- Pointer to per-thread timeouts.
\r
521 *---------------------------------------------------------------------------*/
\r
523 struct sys_timeouts *sys_arch_timeouts(void)
\r
527 struct timeoutlist *tl;
\r
529 pid = UEZTaskGetCurrent();
\r
531 for (i = 0; i < s_nextthread; i++)
\r
533 tl = &(s_timeoutlist[i]);
\r
534 if (tl->pid == pid)
\r
536 // return &(tl->timeouts);
\r
544 /*---------------------------------------------------------------------------*
\r
545 * Routine: sys_thread_new
\r
546 *---------------------------------------------------------------------------*
\r
548 * Starts a new thread with priority "prio" that will begin its
\r
549 * execution in the function "thread()". The "arg" argument will be
\r
550 * passed as an argument to the thread() function. The id of the new
\r
551 * thread is returned. Both the id and the priority are system
\r
554 * char *name -- Name of thread
\r
555 * void (* thread)(void *arg) -- Pointer to function to run.
\r
556 * void *arg -- Argument passed into function
\r
557 * int stacksize -- Required stack amount in bytes
\r
558 * int prio -- Thread priority
\r
560 * sys_thread_t -- Pointer to per-thread timeouts.
\r
561 *---------------------------------------------------------------------------*/
\r
562 sys_thread_t sys_thread_new(const char *name, void(* thread)(void *arg), void *arg, int stacksize, int prio)
\r
564 xTaskHandle CreatedTask;
\r
567 result = xTaskCreate( thread, ( signed portCHAR * ) name, stacksize, arg, prio, &CreatedTask );
\r
569 if(result == pdPASS)
\r
571 return CreatedTask;
\r
579 /*---------------------------------------------------------------------------*
\r
580 * Routine: sys_arch_protect
\r
581 *---------------------------------------------------------------------------*
\r
583 * This optional function does a "fast" critical region protection and
\r
584 * returns the previous protection level. This function is only called
\r
585 * during very short critical regions. An embedded system which supports
\r
586 * ISR-based drivers might want to implement this function by disabling
\r
587 * interrupts. Task-based systems might want to implement this by using
\r
588 * a mutex or disabling tasking. This function should support recursive
\r
589 * calls from the same task or interrupt. In other words,
\r
590 * sys_arch_protect() could be called while already protected. In
\r
591 * that case the return value indicates that it is already protected.
\r
593 * sys_arch_protect() is only required if your port is supporting an
\r
594 * operating system.
\r
596 * sys_prot_t -- Previous protection level (not used here)
\r
597 *---------------------------------------------------------------------------*/
\r
598 sys_prot_t sys_arch_protect(void)
\r
600 vPortEnterCritical();
\r
604 /*---------------------------------------------------------------------------*
\r
605 * Routine: sys_arch_unprotect
\r
606 *---------------------------------------------------------------------------*
\r
608 * This optional function does a "fast" set of critical region
\r
609 * protection to the value specified by pval. See the documentation for
\r
610 * sys_arch_protect() for more information. This function is only
\r
611 * required if your port is supporting an operating system.
\r
613 * sys_prot_t -- Previous protection level (not used here)
\r
614 *---------------------------------------------------------------------------*/
\r
615 void sys_arch_unprotect(sys_prot_t pval)
\r
618 taskEXIT_CRITICAL();
\r
622 * Prints an assertion messages and aborts execution.
\r
624 void sys_assert(const char *msg)
\r
632 /*-------------------------------------------------------------------------*
\r
633 * End of File: sys_arch.c
\r
634 *-------------------------------------------------------------------------*/
\r