# sys_arch.c File Reference

## Detailed Description

lwIP abstraction layer for AVR32 UC3.

- Compiler: GNU GCC for AVR32
- Supported devices: All AVR32 devices can be used.
- AppNote:

Author:
  Atmel Corporation: http://www.atmel.com
  Support and FAQ: http://support.atmel.no/

Definition in file sys_arch.c.

`#include "lwip/debug.h"`
`#include "lwip/sys.h"`
`#include "lwip/opt.h"`
`#include "lwip/stats.h"`

Go to the source code of this file.

## Data Structures
struct | TimeoutlistPerThread
## Defines
#define | SYS_ARCH_BLOCKING_TICKTIMEOUT ((portTickType)10000)
## Functions
u32_t | sys_arch_mbox_fetch (sys_mbox_t mbox, void **msg, u32_t timeout)
u32_t | sys_arch_mbox_tryfetch (sys_mbox_t mbox, void **msg)
sys_prot_t | sys_arch_protect (void)
u32_t | sys_arch_sem_wait (sys_sem_t sem, u32_t timeout)
struct sys_timeouts * | sys_arch_timeouts (void)
void | sys_arch_unprotect (sys_prot_t pval)
void | sys_init (void)
void | sys_mbox_free (sys_mbox_t mbox)
sys_mbox_t | sys_mbox_new (int size)
void | sys_mbox_post (sys_mbox_t mbox, void *msg)
err_t | sys_mbox_trypost (sys_mbox_t mbox, void *msg)
void | sys_sem_free (sys_sem_t sem)
sys_sem_t | sys_sem_new (u8_t count)
void | sys_sem_signal (sys_sem_t sem)
sys_thread_t | sys_thread_new (char *name, void(*thread)(void *arg), void *arg, int stacksize, int prio)
## Variables
static u16_t | NbActiveThreads = 0
static struct TimeoutlistPerThread | Threads_TimeoutsList [SYS_THREAD_MAX]
volatile unsigned portLONG | ulCriticalNesting

## Define Documentation

#define SYS_ARCH_BLOCKING_TICKTIMEOUT ((portTickType)10000)

Definition at line 51 of file sys_arch.c.

Referenced by sys_arch_mbox_fetch(), sys_arch_sem_wait(), and sys_mbox_post().

## Function Documentation

u32_t sys_arch_mbox_fetch | ( | sys_mbox_t | mbox,
|  | void ** | msg,
|  | u32_t | timeout |
| ) |  |  |

Definition at line 267 of file sys_arch.c.

References SYS_ARCH_BLOCKING_TICKTIMEOUT.

    00268 {
    00269   portTickType TickStart;
    00270   portTickType TickStop;
    00271   portTickType TickElapsed = (portTickType)(timeout*portTICK_RATE_MS); // Express the timeout in OS tick.
    00272   void         *tempoptr;
    00273
    00274
    00275   // NOTE: we assume mbox != SYS_MBOX_NULL; iow, we assume the calling function
    00276   // takes care of checking the mbox validity before calling this function.
    00277
    00278   if(msg == NULL)
    00279   {
    00280       msg = &tempoptr;
    00281   }
    00282
    00283   // NOTE: INCLUDE_xTaskGetSchedulerState must be set to 1 in FreeRTOSConfig.h
    00284   // for xTaskGetTickCount() to be available.
    00285   if( 0 == TickElapsed )
    00286   {
    00287     TickStart = xTaskGetTickCount();
    00288     // If "timeout" is 0, the thread should be blocked until a message arrives.
    00289     while( pdFALSE == xQueueReceive( mbox, &(*msg), SYS_ARCH_BLOCKING_TICKTIMEOUT ) );
    00290   }
    00291   else
    00292   {
    00293     TickStart = xTaskGetTickCount();
    00294     if( pdFALSE == xQueueReceive( mbox, &(*msg), TickElapsed ) )
    00295     {
    00296       *msg = NULL;
    00297       // if the function times out, it should return SYS_ARCH_TIMEOUT.
    00298       return( SYS_ARCH_TIMEOUT );
    00299     }
    00300   }
    00301   // If the function gets a msg, it should return the number of ms spent waiting.
    00302   TickStop = xTaskGetTickCount();
    00303   // Take care of wrap-around.
    00304   if( TickStop >= TickStart )
    00305     TickElapsed = TickStop - TickStart;
    00306   else
    00307     TickElapsed = portMAX_DELAY - TickStart + TickStop ;
    00308   return( TASK_DELAY_MS( TickElapsed ) );
    00309 }

u32_t sys_arch_mbox_tryfetch | ( | sys_mbox_t | mbox,
|  | void ** | msg |
| ) |  |  |

Definition at line 316 of file sys_arch.c.

    00317 {
    00318   void  *tempoptr;
    00319
    00320
    00321   if(msg == NULL)
    00322   {
    00323     msg = &tempoptr;
    00324   }
    00325   if( pdFALSE == xQueueReceive( mbox, &(*msg), 0 ) )
    00326     // if a message is not present in the mailbox, it immediately returns with
    00327     // the code SYS_MBOX_EMPTY.
    00328     return( SYS_MBOX_EMPTY );
    00329   // On success 0 is returned.
    00330   return( 0 );
    00331 }

sys_prot_t sys_arch_protect | ( | void |  | ) |

Definition at line 416 of file sys_arch.c.

References vPortEnterCritical().

    00417 {
    00418   vPortEnterCritical();
    00419   return 1; // Not used
    00420 }

u32_t sys_arch_sem_wait | ( | sys_sem_t | sem,
|  | u32_t | timeout |
| ) |  |  |

Definition at line 154 of file sys_arch.c.

References SYS_ARCH_BLOCKING_TICKTIMEOUT.

    00155 {
    00156   portTickType TickStart;
    00157   portTickType TickStop;
    00158   portTickType TickElapsed = (portTickType)(timeout*portTICK_RATE_MS); // Express the timeout in OS tick.
    00159
    00160
    00161   // NOTE: we assume sem != SYS_SEM_NULL; iow, we assume the calling function
    00162   // takes care of checking the sem validity before calling this function.
    00163
    00164   // NOTE: INCLUDE_xTaskGetSchedulerState must be set to 1 in FreeRTOSConfig.h
    00165   // for xTaskGetTickCount() to be available.
    00166   if( 0 == TickElapsed )
    00167   {
    00168     TickStart = xTaskGetTickCount();
    00169     // If timeout=0, then the function should block indefinitely.
    00170     while( pdFALSE == xSemaphoreTake( sem, SYS_ARCH_BLOCKING_TICKTIMEOUT ) );
    00171   }
    00172   else
    00173   {
    00174     TickStart = xTaskGetTickCount();
    00175     if( pdFALSE == xSemaphoreTake( sem, TickElapsed ) )
    00176       // if the function times out, it should return SYS_ARCH_TIMEOUT.
    00177       return( SYS_ARCH_TIMEOUT );
    00178   }
    00179   // If the function acquires the semaphore, it should return how many milliseconds
    00180   // expired while waiting for the semaphore.
    00181   TickStop = xTaskGetTickCount();
    00182   // Take care of wrap-around.
    00183   if( TickStop >= TickStart )
    00184     TickElapsed = TickStop - TickStart;
    00185   else
    00186     TickElapsed = portMAX_DELAY - TickStart + TickStop ;
    00187   return( TASK_DELAY_MS( TickElapsed ) );
    00188 }

struct sys_timeouts* sys_arch_timeouts | ( | void |  | ) | `[read]`

Definition at line 342 of file sys_arch.c.

References NbActiveThreads, TimeoutlistPerThread::pid, Threads_TimeoutsList, and TimeoutlistPerThread::timeouts.

    00343 {
    00344   int                         i;
    00345   sys_thread_t                pid;
    00346   struct TimeoutlistPerThread *tl;
    00347
    00348
    00349   // Get the current thread id.
    00350   // Note: INCLUDE_xTaskGetCurrentTaskHandle must be set to 1 in FreeRTOSConfig.h
    00351   // for xTaskGetCurrentTaskHandle() to be available.
    00352   pid = xTaskGetCurrentTaskHandle( );
    00353
    00354   for(i = 0; i < NbActiveThreads; i++)
    00355   {
    00356     tl = &(Threads_TimeoutsList[i]);
    00357     if(tl->pid == pid)
    00358     {
    00359       return &(tl->timeouts);
    00360     }
    00361   }
    00362
    00363   // If we're here, this means the scheduler gave the focus to the task as it was
    00364   // being created(because of a higher priority). Since Threads_TimeoutsList[]
    00365   // update is done just after the task creation, the array is not up-to-date.
    00366   // => the last array entry must be the one of the current task.
    00367   return( &(Threads_TimeoutsList[NbActiveThreads].timeouts) );
    00368 }

void sys_arch_unprotect | ( | sys_prot_t | pval | ) |

Definition at line 425 of file sys_arch.c.

References vPortExitCritical().

    00426 {
    00427   vPortExitCritical();
    00428 }

void sys_init | ( | void |  | ) |

Definition at line 72 of file sys_arch.c.

References NbActiveThreads, TimeoutlistPerThread::pid, SYS_THREAD_MAX, Threads_TimeoutsList, and TimeoutlistPerThread::timeouts.

    00073 {
    00074   int i;
    00075
    00076
    00077   // Initialize the the per-thread sys_timeouts structures
    00078   // make sure there are no valid pids in the list
    00079   for(i = 0; i < SYS_THREAD_MAX; i++)
    00080   {
    00081     Threads_TimeoutsList[i].pid = 0;
    00082     Threads_TimeoutsList[i].timeouts.next = NULL;
    00083   }
    00084
    00085   // keep track of how many threads have been created
    00086   NbActiveThreads = 0;
    00087 }

void sys_mbox_free | ( | sys_mbox_t | mbox | ) |

Definition at line 221 of file sys_arch.c.

References SYS_MBOX_NULL.

    00222 {
    00223   if( SYS_MBOX_NULL != mbox )
    00224   {
    00225 #ifdef SYS_STATS
    00226     lwip_stats.sys.mbox.used--;
    00227 #endif /* SYS_STATS */
    00228     vQueueDelete( mbox );
    00229   }
    00230 }

sys_mbox_t sys_mbox_new | ( | int | size | ) |

Definition at line 197 of file sys_arch.c.

References SYS_MBOX_NULL.

    00198 {
    00199   sys_mbox_t mBoxNew;
    00200
    00201
    00202   mBoxNew = xQueueCreate( size, sizeof( void * ) );
    00203 #if SYS_STATS
    00204   if( SYS_MBOX_NULL != mBoxNew )
    00205   {
    00206     lwip_stats.sys.mbox.used++;
    00207     if (lwip_stats.sys.mbox.used > lwip_stats.sys.mbox.max)
    00208     {
    00209       lwip_stats.sys.mbox.max = lwip_stats.sys.mbox.used;
    00210     }
    00211   }
    00212 #endif /* SYS_STATS */
    00213   return( mBoxNew );
    00214 }

void sys_mbox_post | ( | sys_mbox_t | mbox,
|  | void * | msg |
| ) |  |  |

Definition at line 235 of file sys_arch.c.

References SYS_ARCH_BLOCKING_TICKTIMEOUT.

    00236 {
    00237   // NOTE: we assume mbox != SYS_MBOX_NULL; iow, we assume the calling function
    00238   // takes care of checking the mbox validity before calling this function.
    00239   while( pdTRUE != xQueueSend( mbox, &msg, SYS_ARCH_BLOCKING_TICKTIMEOUT ) );
    00240 }

err_t sys_mbox_trypost | ( | sys_mbox_t | mbox,
|  | void * | msg |
| ) |  |  |

Definition at line 246 of file sys_arch.c.

    00247 {
    00248   if( errQUEUE_FULL == xQueueSend( mbox, &msg, 0 ) )
    00249     return( ERR_MEM );
    00250   else
    00251     return( ERR_OK );
    00252 }

void sys_sem_free | ( | sys_sem_t | sem | ) |

Definition at line 124 of file sys_arch.c.

References SYS_SEM_NULL.

Referenced by prvlwIPInit().

    00125 {
    00126   if( SYS_SEM_NULL != sem )
    00127   {
    00128 #ifdef SYS_STATS
    00129     lwip_stats.sys.sem.used--;
    00130 #endif /* SYS_STATS */
    00131     vQueueDelete( sem );
    00132   }
    00133 }

sys_sem_t sys_sem_new | ( | u8_t | count | ) |

Definition at line 95 of file sys_arch.c.

References SYS_SEM_NULL.

Referenced by prvlwIPInit().

    00096 {
    00097   sys_sem_t  xSemaphore = SYS_SEM_NULL;
    00098
    00099   portENTER_CRITICAL();
    00100
    00101   vSemaphoreCreateBinary( xSemaphore );
    00102   if( xSemaphore != SYS_SEM_NULL )
    00103   {
    00104 #if SYS_STATS
    00105     lwip_stats.sys.sem.used++;
    00106     if (lwip_stats.sys.sem.used > lwip_stats.sys.sem.max) {
    00107       lwip_stats.sys.sem.max = lwip_stats.sys.sem.used;
    00108     }
    00109 #endif /* SYS_STATS */
    00110
    00111     if( 0 == count )  // Means we want the sem to be unavailable at init state.
    00112     {
    00113       xSemaphoreTake( xSemaphore, 1);
    00114     }
    00115   }
    00116
    00117   portEXIT_CRITICAL();
    00118
    00119   return xSemaphore;
    00120 }

void sys_sem_signal | ( | sys_sem_t | sem | ) |

Definition at line 137 of file sys_arch.c.

Referenced by tcpip_init_done().

    00138 {
    00139   xSemaphoreGive( sem );
    00140 }

sys_thread_t sys_thread_new | ( | char * | name,
|  | void(*)(void *arg) | thread,
|  | void * | arg,
|  | int | stacksize,
|  | int | prio |
| ) |  |  |

Definition at line 378 of file sys_arch.c.

References NbActiveThreads, TimeoutlistPerThread::pid, and Threads_TimeoutsList.

Referenced by low_level_init(), and portTASK_FUNCTION().

    00379 {
    00380   sys_thread_t    newthread;
    00381   portBASE_TYPE   result;
    00382   SYS_ARCH_DECL_PROTECT(protectionLevel);
    00383
    00384   result = xTaskCreate( thread, (signed portCHAR *)name, stacksize, arg, prio, &newthread );
    00385
    00386   // Need to protect this -- preemption here could be a problem!
    00387   SYS_ARCH_PROTECT(protectionLevel);
    00388   if( pdPASS == result )
    00389   {
    00390     // For each task created, store the task handle (pid) in the timers array.
    00391     // This scheme doesn't allow for threads to be deleted
    00392     Threads_TimeoutsList[NbActiveThreads++].pid = newthread;
    00393   }
    00394   else
    00395   {
    00396     newthread = NULL;
    00397   }
    00398   SYS_ARCH_UNPROTECT(protectionLevel);
    00399
    00400   return( newthread );
    00401 }

## Variable Documentation

u16_t NbActiveThreads = 0`[static]`

Definition at line 67 of file sys_arch.c.

Referenced by sys_arch_timeouts(), sys_init(), and sys_thread_new().

struct TimeoutlistPerThread Threads_TimeoutsList[SYS_THREAD_MAX]`[static]`

Definition at line 64 of file sys_arch.c.

Referenced by sys_arch_timeouts(), sys_init(), and sys_thread_new().

volatile unsigned portLONG ulCriticalNesting

Definition at line 83 of file GCC/AVR32_UC3/port.c.

Referenced by vPortEnterCritical(), and vPortExitCritical().