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
41 /* Message queue constants. */
\r
42 #define archMESG_QUEUE_LENGTH ( 6 )
\r
43 #define archPOST_BLOCK_TIME_MS ( ( unsigned long ) 10000 )
\r
47 struct sys_timeouts timeouts;
\r
51 /* This is the number of threads that can be started with sys_thread_new() */
\r
52 #define SYS_THREAD_MAX 4
\r
54 static struct timeoutlist timeoutlist[SYS_THREAD_MAX];
\r
55 static u16_t nextthread = 0;
\r
58 static sys_arch_state_t s_sys_arch_state;
\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
279 s_sys_arch_state.nTaskCount = 0;
\r
280 sys_set_default_state();
\r
283 /*-----------------------------------------------------------------------------------*/
\r
285 Returns a pointer to the per-thread sys_timeouts structure. In lwIP,
\r
286 each thread has a list of timeouts which is represented as a linked
\r
287 list of sys_timeout structures. The sys_timeouts structure holds a
\r
288 pointer to a linked list of timeouts. This function is called by
\r
289 the lwIP timeout scheduler and must not return a NULL value.
\r
291 In a single threaded sys_arch implementation, this function will
\r
292 simply return a pointer to a global sys_timeouts variable stored in
\r
293 the sys_arch module.
\r
295 struct sys_timeouts *
\r
296 sys_arch_timeouts(void)
\r
300 struct timeoutlist *tl;
\r
302 pid = xTaskGetCurrentTaskHandle( );
\r
304 for(i = 0; i < nextthread; i++)
\r
306 tl = &timeoutlist[i];
\r
309 return &(tl->timeouts);
\r
317 /*-----------------------------------------------------------------------------------*/
\r
318 /*-----------------------------------------------------------------------------------*/
\r
320 /*-----------------------------------------------------------------------------------*/
\r
322 Starts a new thread with priority "prio" that will begin its execution in the
\r
323 function "thread()". The "arg" argument will be passed as an argument to the
\r
324 thread() function. The id of the new thread is returned. Both the id and
\r
325 the priority are system dependent.
\r
327 sys_thread_t sys_thread_new(void (* thread)(void *arg), void *arg, int prio)
\r
329 xTaskHandle CreatedTask;
\r
332 result = xTaskCreate(thread, ( signed char * ) s_sys_arch_state.cTaskName, s_sys_arch_state.nStackDepth, arg, prio, &CreatedTask );
\r
334 // For each task created, store the task handle (pid) in the timers array.
\r
335 // This scheme doesn't allow for threads to be deleted
\r
336 timeoutlist[nextthread++].pid = CreatedTask;
\r
338 if(result == pdPASS)
\r
340 ++s_sys_arch_state.nTaskCount;
\r
342 return CreatedTask;
\r
351 This optional function does a "fast" critical region protection and returns
\r
352 the previous protection level. This function is only called during very short
\r
353 critical regions. An embedded system which supports ISR-based drivers might
\r
354 want to implement this function by disabling interrupts. Task-based systems
\r
355 might want to implement this by using a mutex or disabling tasking. This
\r
356 function should support recursive calls from the same task or interrupt. In
\r
357 other words, sys_arch_protect() could be called while already protected. In
\r
358 that case the return value indicates that it is already protected.
\r
360 sys_arch_protect() is only required if your port is supporting an operating
\r
363 sys_prot_t sys_arch_protect(void)
\r
365 vPortEnterCritical();
\r
370 This optional function does a "fast" set of critical region protection to the
\r
371 value specified by pval. See the documentation for sys_arch_protect() for
\r
372 more information. This function is only required if your port is supporting
\r
373 an operating system.
\r
375 void sys_arch_unprotect(sys_prot_t pval)
\r
378 vPortExitCritical();
\r
381 void sys_set_default_state()
\r
383 s_sys_arch_state.nStackDepth = configMINIMAL_STACK_SIZE;
\r
384 sprintf(s_sys_arch_state.cTaskName, "thread%d", s_sys_arch_state.nTaskCount);
\r
387 void sys_set_state(signed char *pTaskName, unsigned short nStackSize)
\r
389 s_sys_arch_state.nStackDepth = nStackSize;
\r
390 sprintf(s_sys_arch_state.cTaskName, "%s", pTaskName);
\r