]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_Demo_Rowley_ARM7/lwip-1.1.0/contrib/port/FreeRTOS/AT91SAM7X/sys_arch.c
4d39c37cd5d820a18f9b8530d5fe5b82fef8025d
[freertos] / FreeRTOS / Demo / lwIP_Demo_Rowley_ARM7 / lwip-1.1.0 / contrib / port / FreeRTOS / AT91SAM7X / sys_arch.c
1 /*\r
2  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.\r
3  * All rights reserved. \r
4  * \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
7  *\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
15  *\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
25  * OF SUCH DAMAGE.\r
26  *\r
27  * This file is part of the lwIP TCP/IP stack.\r
28  * \r
29  * Author: Adam Dunkels <adam@sics.se>\r
30  *\r
31  */\r
32 \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 \r
39 /* Message queue constants. */\r
40 #define archMESG_QUEUE_LENGTH   ( 6 )\r
41 #define archPOST_BLOCK_TIME_MS  ( ( unsigned long ) 10000 )\r
42 \r
43 struct timeoutlist \r
44 {\r
45         struct sys_timeouts timeouts;\r
46         xTaskHandle pid;\r
47 };\r
48 \r
49 /* This is the number of threads that can be started with sys_thread_new() */\r
50 #define SYS_THREAD_MAX 4\r
51 \r
52 #define lwipTCP_STACK_SIZE                      600\r
53 #define lwipBASIC_SERVER_STACK_SIZE     250\r
54 \r
55 static struct timeoutlist timeoutlist[SYS_THREAD_MAX];\r
56 static u16_t nextthread = 0;\r
57 int intlevel = 0;\r
58 \r
59 \r
60 /*-----------------------------------------------------------------------------------*/\r
61 //  Creates an empty mailbox.\r
62 sys_mbox_t\r
63 sys_mbox_new(void)\r
64 {\r
65         xQueueHandle mbox;\r
66 \r
67         mbox = xQueueCreate( archMESG_QUEUE_LENGTH, sizeof( void * ) );\r
68 \r
69         return mbox;\r
70 }\r
71 \r
72 /*-----------------------------------------------------------------------------------*/\r
73 /*\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
77 */\r
78 void\r
79 sys_mbox_free(sys_mbox_t mbox)\r
80 {\r
81         if( uxQueueMessagesWaiting( mbox ) )\r
82         {\r
83                 /* Line for breakpoint.  Should never break here! */\r
84                 __asm volatile ( "NOP" );\r
85         }\r
86 \r
87         vQueueDelete( mbox ); \r
88 }\r
89 \r
90 /*-----------------------------------------------------------------------------------*/\r
91 //   Posts the "msg" to the mailbox.\r
92 void\r
93 sys_mbox_post(sys_mbox_t mbox, void *data)\r
94 {   \r
95         xQueueSend( mbox, &data, ( portTickType ) ( archPOST_BLOCK_TIME_MS / portTICK_RATE_MS ) );\r
96 }\r
97 \r
98 \r
99 /*-----------------------------------------------------------------------------------*/\r
100 /*\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
106   should be dropped.\r
107 \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
110   timeout.\r
111 \r
112   Note that a function with a similar name, sys_mbox_fetch(), is\r
113   implemented by lwIP. \r
114 */\r
115 u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout)\r
116 {\r
117 void *dummyptr;\r
118 portTickType StartTime, EndTime, Elapsed;\r
119 \r
120         StartTime = xTaskGetTickCount();\r
121 \r
122         if( msg == NULL )\r
123         {\r
124                 msg = &dummyptr;\r
125         }\r
126                 \r
127         if(     timeout != 0 )\r
128         {\r
129                 if(pdTRUE == xQueueReceive( mbox, &(*msg), timeout ) )\r
130                 {\r
131                         EndTime = xTaskGetTickCount();\r
132                         Elapsed = EndTime - StartTime;\r
133                         if( Elapsed == 0 )\r
134                         {\r
135                                 Elapsed = 1;\r
136                         }\r
137                         return ( Elapsed );\r
138                 }\r
139                 else // timed out blocking for message\r
140                 {\r
141                         *msg = NULL;\r
142                         return SYS_ARCH_TIMEOUT;\r
143                 }\r
144         }\r
145         else // block forever for a message.\r
146         {\r
147                 while( pdTRUE != xQueueReceive( mbox, &(*msg), 10000 ) ) // time is arbitrary\r
148                 {\r
149                         ;\r
150                 }\r
151                 EndTime = xTaskGetTickCount();\r
152                 Elapsed = EndTime - StartTime;\r
153                 if( Elapsed == 0 )\r
154                 {\r
155                         Elapsed = 1;\r
156                 }\r
157                 return ( Elapsed ); // return time blocked TBD test     \r
158         }\r
159 }\r
160 \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
164 sys_sem_t\r
165 sys_sem_new(u8_t count)\r
166 {\r
167         xSemaphoreHandle  xSemaphore;\r
168 \r
169         portENTER_CRITICAL();\r
170         vSemaphoreCreateBinary( xSemaphore );\r
171         if(count == 0)  // Means it can't be taken\r
172         {\r
173                 xSemaphoreTake(xSemaphore,1);\r
174         }\r
175         portEXIT_CRITICAL();\r
176 \r
177         if( xSemaphore == NULL )\r
178         {\r
179                 return NULL;    // TBD need assert\r
180         }\r
181         else\r
182         {\r
183                 return xSemaphore;\r
184         }\r
185 }\r
186 \r
187 /*-----------------------------------------------------------------------------------*/\r
188 /*\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
192   milliseconds).\r
193 \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
199 \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
202 */\r
203 u32_t\r
204 sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)\r
205 {\r
206 portTickType StartTime, EndTime, Elapsed;\r
207 \r
208         StartTime = xTaskGetTickCount();\r
209 \r
210         if(     timeout != 0)\r
211         {\r
212                 if( xSemaphoreTake( sem, timeout ) == pdTRUE )\r
213                 {\r
214                         EndTime = xTaskGetTickCount();\r
215                         Elapsed = EndTime - StartTime;\r
216                         if( Elapsed == 0 )\r
217                         {\r
218                                 Elapsed = 1;\r
219                         }\r
220                         return (Elapsed); // return time blocked TBD test       \r
221                 }\r
222                 else\r
223                 {\r
224                         return SYS_ARCH_TIMEOUT;\r
225                 }\r
226         }\r
227         else // must block without a timeout\r
228         {\r
229                 while( xSemaphoreTake( sem, 10000 ) != pdTRUE )\r
230                 {\r
231                         ;\r
232                 }\r
233                 EndTime = xTaskGetTickCount();\r
234                 Elapsed = EndTime - StartTime;\r
235                 if( Elapsed == 0 )\r
236                 {\r
237                         Elapsed = 1;\r
238                 }\r
239 \r
240                 return ( Elapsed ); // return time blocked      \r
241                  \r
242         }\r
243 }\r
244 \r
245 /*-----------------------------------------------------------------------------------*/\r
246 // Signals a semaphore\r
247 void\r
248 sys_sem_signal(sys_sem_t sem)\r
249 {\r
250         xSemaphoreGive( sem );\r
251 }\r
252 \r
253 /*-----------------------------------------------------------------------------------*/\r
254 // Deallocates a semaphore\r
255 void\r
256 sys_sem_free(sys_sem_t sem)\r
257 {\r
258         vQueueDelete( sem ); \r
259 }\r
260 \r
261 /*-----------------------------------------------------------------------------------*/\r
262 // Initialize sys arch\r
263 void\r
264 sys_init(void)\r
265 {\r
266 \r
267         int i;\r
268 \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
272         {\r
273                 timeoutlist[i].pid = 0;\r
274         }\r
275 \r
276         // keep track of how many threads have been created\r
277         nextthread = 0;\r
278 }\r
279 \r
280 /*-----------------------------------------------------------------------------------*/\r
281 /*\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
287 \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
291 */\r
292 struct sys_timeouts *\r
293 sys_arch_timeouts(void)\r
294 {\r
295 int i;\r
296 xTaskHandle pid;\r
297 struct timeoutlist *tl;  \r
298 \r
299         pid = xTaskGetCurrentTaskHandle( ); \r
300 \r
301         for(i = 0; i < nextthread; i++) \r
302         {\r
303                 tl = &timeoutlist[i];\r
304                 if(tl->pid == pid) \r
305                 {\r
306                         return &(tl->timeouts);\r
307                 }\r
308         }\r
309 \r
310         // Error\r
311         return NULL;\r
312 }\r
313 \r
314 /*-----------------------------------------------------------------------------------*/\r
315 /*-----------------------------------------------------------------------------------*/\r
316 // TBD \r
317 /*-----------------------------------------------------------------------------------*/\r
318 /*\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
323 */\r
324 sys_thread_t sys_thread_new(void (* thread)(void *arg), void *arg, int prio)\r
325 {\r
326 xTaskHandle CreatedTask;\r
327 int result;\r
328 static int iCall = 0;\r
329 \r
330         if( iCall == 0 )\r
331         {\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
334                 iCall++;\r
335         }\r
336         else\r
337         {\r
338                 result = xTaskCreate( thread, ( signed char * ) "WEBSvr", lwipBASIC_SERVER_STACK_SIZE, arg, prio, &CreatedTask );\r
339         }\r
340 \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
344 \r
345         if(result == pdPASS)\r
346         {\r
347                 return CreatedTask;\r
348         }\r
349         else\r
350         {\r
351                 return NULL;\r
352         }\r
353 }\r
354 \r
355 /*\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
364 \r
365   sys_arch_protect() is only required if your port is supporting an operating\r
366   system.\r
367 */\r
368 sys_prot_t sys_arch_protect(void)\r
369 {\r
370         vPortEnterCritical();\r
371         return 1;\r
372 }\r
373 \r
374 /*\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
379 */\r
380 void sys_arch_unprotect(sys_prot_t pval)\r
381\r
382         ( void ) pval;\r
383         vPortExitCritical();\r
384 }\r
385 \r