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
39 /* Message queue constants. */
\r
40 #define archMESG_QUEUE_LENGTH ( 6 )
\r
41 #define archPOST_BLOCK_TIME_MS ( ( unsigned long ) 10000 )
\r
45 struct sys_timeouts timeouts;
\r
49 /* This is the number of threads that can be started with sys_thread_new() */
\r
50 #define SYS_THREAD_MAX 4
\r
52 #define lwipTCP_STACK_SIZE 600
\r
53 #define lwipBASIC_SERVER_STACK_SIZE 250
\r
55 static struct timeoutlist timeoutlist[SYS_THREAD_MAX];
\r
56 static u16_t nextthread = 0;
\r
60 /*-----------------------------------------------------------------------------------*/
\r
61 // Creates an empty mailbox.
\r
67 mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) );
\r
72 /*-----------------------------------------------------------------------------------*/
\r
74 Deallocates a mailbox. If there are messages still present in the
\r
75 mailbox when the mailbox is deallocated, it is an indication of a
\r
76 programming error in lwIP and the developer should be notified.
\r
79 sys_mbox_free(sys_mbox_t mbox)
\r
81 if( uxQueueMessagesWaiting( mbox ) )
\r
83 /* Line for breakpoint. Should never break here! */
\r
84 __asm volatile ( "NOP" );
\r
87 vQueueDelete( mbox );
\r
90 /*-----------------------------------------------------------------------------------*/
\r
91 // Posts the "msg" to the mailbox.
\r
93 sys_mbox_post(sys_mbox_t mbox, void *data)
\r
95 xQueueSend( mbox, &data, ( portTickType ) ( archPOST_BLOCK_TIME_MS / portTICK_RATE_MS ) );
\r
99 /*-----------------------------------------------------------------------------------*/
\r
101 Blocks the thread until a message arrives in the mailbox, but does
\r
102 not block the thread longer than "timeout" milliseconds (similar to
\r
103 the sys_arch_sem_wait() function). The "msg" argument is a result
\r
104 parameter that is set by the function (i.e., by doing "*msg =
\r
105 ptr"). The "msg" parameter maybe NULL to indicate that the message
\r
108 The return values are the same as for the sys_arch_sem_wait() function:
\r
109 Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a
\r
112 Note that a function with a similar name, sys_mbox_fetch(), is
\r
113 implemented by lwIP.
\r
115 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)
\r
118 portTickType StartTime, EndTime, Elapsed;
\r
120 StartTime = xTaskGetTickCount();
\r
129 if(pdTRUE == xQueueReceive( mbox, &(*msg), timeout ) )
\r
131 EndTime = xTaskGetTickCount();
\r
132 Elapsed = EndTime - StartTime;
\r
137 return ( Elapsed );
\r
139 else // timed out blocking for message
\r
142 return SYS_ARCH_TIMEOUT;
\r
145 else // block forever for a message.
\r
147 while( pdTRUE != xQueueReceive( mbox, &(*msg), 10000 ) ) // time is arbitrary
\r
151 EndTime = xTaskGetTickCount();
\r
152 Elapsed = EndTime - StartTime;
\r
157 return ( Elapsed ); // return time blocked TBD test
\r
161 /*-----------------------------------------------------------------------------------*/
\r
162 // Creates and returns a new semaphore. The "count" argument specifies
\r
163 // the initial state of the semaphore. TBD finish and test
\r
165 sys_sem_new(u8_t count)
\r
167 xSemaphoreHandle xSemaphore;
\r
169 portENTER_CRITICAL();
\r
170 vSemaphoreCreateBinary( xSemaphore );
\r
171 if(count == 0) // Means it can't be taken
\r
173 xSemaphoreTake(xSemaphore,1);
\r
175 portEXIT_CRITICAL();
\r
177 if( xSemaphore == NULL )
\r
179 return NULL; // TBD need assert
\r
187 /*-----------------------------------------------------------------------------------*/
\r
189 Blocks the thread while waiting for the semaphore to be
\r
190 signaled. If the "timeout" argument is non-zero, the thread should
\r
191 only be blocked for the specified time (measured in
\r
194 If the timeout argument is non-zero, the return value is the number of
\r
195 milliseconds spent waiting for the semaphore to be signaled. If the
\r
196 semaphore wasn't signaled within the specified time, the return value is
\r
197 SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore
\r
198 (i.e., it was already signaled), the function may return zero.
\r
200 Notice that lwIP implements a function with a similar name,
\r
201 sys_sem_wait(), that uses the sys_arch_sem_wait() function.
\r
204 sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)
\r
206 portTickType StartTime, EndTime, Elapsed;
\r
208 StartTime = xTaskGetTickCount();
\r
212 if( xSemaphoreTake( sem, timeout ) == pdTRUE )
\r
214 EndTime = xTaskGetTickCount();
\r
215 Elapsed = EndTime - StartTime;
\r
220 return (Elapsed); // return time blocked TBD test
\r
224 return SYS_ARCH_TIMEOUT;
\r
227 else // must block without a timeout
\r
229 while( xSemaphoreTake( sem, 10000 ) != pdTRUE )
\r
233 EndTime = xTaskGetTickCount();
\r
234 Elapsed = EndTime - StartTime;
\r
240 return ( Elapsed ); // return time blocked
\r
245 /*-----------------------------------------------------------------------------------*/
\r
246 // Signals a semaphore
\r
248 sys_sem_signal(sys_sem_t sem)
\r
250 xSemaphoreGive( sem );
\r
253 /*-----------------------------------------------------------------------------------*/
\r
254 // Deallocates a semaphore
\r
256 sys_sem_free(sys_sem_t sem)
\r
258 vQueueDelete( sem );
\r
261 /*-----------------------------------------------------------------------------------*/
\r
262 // Initialize sys arch
\r
269 // Initialize the the per-thread sys_timeouts structures
\r
270 // make sure there are no valid pids in the list
\r
271 for(i = 0; i < SYS_THREAD_MAX; i++)
\r
273 timeoutlist[i].pid = 0;
\r
276 // keep track of how many threads have been created
\r
280 /*-----------------------------------------------------------------------------------*/
\r
282 Returns a pointer to the per-thread sys_timeouts structure. In lwIP,
\r
283 each thread has a list of timeouts which is represented as a linked
\r
284 list of sys_timeout structures. The sys_timeouts structure holds a
\r
285 pointer to a linked list of timeouts. This function is called by
\r
286 the lwIP timeout scheduler and must not return a NULL value.
\r
288 In a single threaded sys_arch implementation, this function will
\r
289 simply return a pointer to a global sys_timeouts variable stored in
\r
290 the sys_arch module.
\r
292 struct sys_timeouts *
\r
293 sys_arch_timeouts(void)
\r
297 struct timeoutlist *tl;
\r
299 pid = xTaskGetCurrentTaskHandle( );
\r
301 for(i = 0; i < nextthread; i++)
\r
303 tl = &timeoutlist[i];
\r
304 if(tl->pid == pid)
\r
306 return &(tl->timeouts);
\r
314 /*-----------------------------------------------------------------------------------*/
\r
315 /*-----------------------------------------------------------------------------------*/
\r
317 /*-----------------------------------------------------------------------------------*/
\r
319 Starts a new thread with priority "prio" that will begin its execution in the
\r
320 function "thread()". The "arg" argument will be passed as an argument to the
\r
321 thread() function. The id of the new thread is returned. Both the id and
\r
322 the priority are system dependent.
\r
324 sys_thread_t sys_thread_new(void (* thread)(void *arg), void *arg, int prio)
\r
326 xTaskHandle CreatedTask;
\r
328 static int iCall = 0;
\r
332 /* The first time this is called we are creating the lwIP handler. */
\r
333 result = xTaskCreate( thread, ( signed char * ) "lwIP", lwipTCP_STACK_SIZE, arg, prio, &CreatedTask );
\r
338 result = xTaskCreate( thread, ( signed char * ) "WEBSvr", lwipBASIC_SERVER_STACK_SIZE, arg, prio, &CreatedTask );
\r
341 // For each task created, store the task handle (pid) in the timers array.
\r
342 // This scheme doesn't allow for threads to be deleted
\r
343 timeoutlist[nextthread++].pid = CreatedTask;
\r
345 if(result == pdPASS)
\r
347 return CreatedTask;
\r
356 This optional function does a "fast" critical region protection and returns
\r
357 the previous protection level. This function is only called during very short
\r
358 critical regions. An embedded system which supports ISR-based drivers might
\r
359 want to implement this function by disabling interrupts. Task-based systems
\r
360 might want to implement this by using a mutex or disabling tasking. This
\r
361 function should support recursive calls from the same task or interrupt. In
\r
362 other words, sys_arch_protect() could be called while already protected. In
\r
363 that case the return value indicates that it is already protected.
\r
365 sys_arch_protect() is only required if your port is supporting an operating
\r
368 sys_prot_t sys_arch_protect(void)
\r
370 vPortEnterCritical();
\r
375 This optional function does a "fast" set of critical region protection to the
\r
376 value specified by pval. See the documentation for sys_arch_protect() for
\r
377 more information. This function is only required if your port is supporting
\r
378 an operating system.
\r
380 void sys_arch_unprotect(sys_prot_t pval)
\r
383 vPortExitCritical();
\r