--- /dev/null
+/*\r
+ * Copyright (c) 2001-2003 Swedish Institute of Computer Science.\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without modification,\r
+ * are permitted provided that the following conditions are met:\r
+ *\r
+ * 1. Redistributions of source code must retain the above copyright notice,\r
+ * this list of conditions and the following disclaimer.\r
+ * 2. Redistributions in binary form must reproduce the above copyright notice,\r
+ * this list of conditions and the following disclaimer in the documentation\r
+ * and/or other materials provided with the distribution.\r
+ * 3. The name of the author may not be used to endorse or promote products\r
+ * derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\r
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT\r
+ * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT\r
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY\r
+ * OF SUCH DAMAGE.\r
+ *\r
+ * This file is part of the lwIP TCP/IP stack.\r
+ *\r
+ * Author: Adam Dunkels <adam@sics.se>\r
+ *\r
+ */\r
+\r
+//*****************************************************************************\r
+//\r
+// Include OS functionality.\r
+//\r
+//*****************************************************************************\r
+\r
+/* ------------------------ System architecture includes ----------------------------- */\r
+#include "arch/sys_arch.h"\r
+\r
+/* ------------------------ lwIP includes --------------------------------- */\r
+#include "lwip/opt.h"\r
+\r
+#include "lwip/debug.h"\r
+#include "lwip/def.h"\r
+#include "lwip/sys.h"\r
+#include "lwip/mem.h"\r
+#include "lwip/stats.h"\r
+\r
+/* Very crude mechanism used to determine if the critical section handling\r
+functions are being called from an interrupt context or not. This relies on\r
+the interrupt handler setting this variable manually. */\r
+BaseType_t xInsideISR = pdFALSE;\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_mbox_new\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Creates a new mailbox\r
+ * Inputs:\r
+ * int size -- Size of elements in the mailbox\r
+ * Outputs:\r
+ * sys_mbox_t -- Handle to new mailbox\r
+ *---------------------------------------------------------------------------*/\r
+err_t sys_mbox_new( sys_mbox_t *pxMailBox, int iSize )\r
+{\r
+err_t xReturn = ERR_MEM;\r
+\r
+ *pxMailBox = xQueueCreate( iSize, sizeof( void * ) );\r
+\r
+ if( *pxMailBox != NULL )\r
+ {\r
+ xReturn = ERR_OK;\r
+ SYS_STATS_INC_USED( mbox );\r
+ }\r
+\r
+ return xReturn;\r
+}\r
+\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_mbox_free\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Deallocates a mailbox. If there are messages still present in the\r
+ * mailbox when the mailbox is deallocated, it is an indication of a\r
+ * programming error in lwIP and the developer should be notified.\r
+ * Inputs:\r
+ * sys_mbox_t mbox -- Handle of mailbox\r
+ * Outputs:\r
+ * sys_mbox_t -- Handle to new mailbox\r
+ *---------------------------------------------------------------------------*/\r
+void sys_mbox_free( sys_mbox_t *pxMailBox )\r
+{\r
+unsigned long ulMessagesWaiting;\r
+\r
+ ulMessagesWaiting = uxQueueMessagesWaiting( *pxMailBox );\r
+ configASSERT( ( ulMessagesWaiting == 0 ) );\r
+\r
+ #if SYS_STATS\r
+ {\r
+ if( ulMessagesWaiting != 0UL )\r
+ {\r
+ SYS_STATS_INC( mbox.err );\r
+ }\r
+\r
+ SYS_STATS_DEC( mbox.used );\r
+ }\r
+ #endif /* SYS_STATS */\r
+\r
+ vQueueDelete( *pxMailBox );\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_mbox_post\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Post the "msg" to the mailbox.\r
+ * Inputs:\r
+ * sys_mbox_t mbox -- Handle of mailbox\r
+ * void *data -- Pointer to data to post\r
+ *---------------------------------------------------------------------------*/\r
+void sys_mbox_post( sys_mbox_t *pxMailBox, void *pxMessageToPost )\r
+{\r
+ while( xQueueSendToBack( *pxMailBox, &pxMessageToPost, portMAX_DELAY ) != pdTRUE );\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_mbox_trypost\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Try to post the "msg" to the mailbox. Returns immediately with\r
+ * error if cannot.\r
+ * Inputs:\r
+ * sys_mbox_t mbox -- Handle of mailbox\r
+ * void *msg -- Pointer to data to post\r
+ * Outputs:\r
+ * err_t -- ERR_OK if message posted, else ERR_MEM\r
+ * if not.\r
+ *---------------------------------------------------------------------------*/\r
+err_t sys_mbox_trypost( sys_mbox_t *pxMailBox, void *pxMessageToPost )\r
+{\r
+err_t xReturn;\r
+portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
+\r
+ if( xInsideISR != pdFALSE )\r
+ {\r
+ xReturn = xQueueSendFromISR( *pxMailBox, &pxMessageToPost, &xHigherPriorityTaskWoken );\r
+ portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
+ }\r
+ else\r
+ {\r
+ xReturn = xQueueSend( *pxMailBox, &pxMessageToPost, ( TickType_t ) 0 );\r
+ }\r
+\r
+ if( xReturn == pdPASS )\r
+ {\r
+ xReturn = ERR_OK;\r
+ }\r
+ else\r
+ {\r
+ /* The queue was already full. */\r
+ xReturn = ERR_MEM;\r
+ SYS_STATS_INC( mbox.err );\r
+ }\r
+\r
+ return xReturn;\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_arch_mbox_fetch\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Blocks the thread until a message arrives in the mailbox, but does\r
+ * not block the thread longer than "timeout" milliseconds (similar to\r
+ * the sys_arch_sem_wait() function). The "msg" argument is a result\r
+ * parameter that is set by the function (i.e., by doing "*msg =\r
+ * ptr"). The "msg" parameter maybe NULL to indicate that the message\r
+ * should be dropped.\r
+ *\r
+ * The return values are the same as for the sys_arch_sem_wait() function:\r
+ * Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a\r
+ * timeout.\r
+ *\r
+ * Note that a function with a similar name, sys_mbox_fetch(), is\r
+ * implemented by lwIP.\r
+ * Inputs:\r
+ * sys_mbox_t mbox -- Handle of mailbox\r
+ * void **msg -- Pointer to pointer to msg received\r
+ * u32_t timeout -- Number of milliseconds until timeout\r
+ * Outputs:\r
+ * u32_t -- SYS_ARCH_TIMEOUT if timeout, else number\r
+ * of milliseconds until received.\r
+ *---------------------------------------------------------------------------*/\r
+u32_t sys_arch_mbox_fetch( sys_mbox_t *pxMailBox, void **ppvBuffer, u32_t ulTimeOut )\r
+{\r
+void *pvDummy;\r
+TickType_t xStartTime, xEndTime, xElapsed;\r
+unsigned long ulReturn;\r
+\r
+ xStartTime = xTaskGetTickCount();\r
+\r
+ if( NULL == ppvBuffer )\r
+ {\r
+ ppvBuffer = &pvDummy;\r
+ }\r
+\r
+ if( ulTimeOut != 0UL )\r
+ {\r
+ configASSERT( xInsideISR == ( portBASE_TYPE ) 0 );\r
+\r
+ if( pdTRUE == xQueueReceive( *pxMailBox, &( *ppvBuffer ), ulTimeOut/ portTICK_PERIOD_MS ) )\r
+ {\r
+ xEndTime = xTaskGetTickCount();\r
+ xElapsed = ( xEndTime - xStartTime ) * portTICK_PERIOD_MS;\r
+\r
+ ulReturn = xElapsed;\r
+ }\r
+ else\r
+ {\r
+ /* Timed out. */\r
+ *ppvBuffer = NULL;\r
+ ulReturn = SYS_ARCH_TIMEOUT;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ while( pdTRUE != xQueueReceive( *pxMailBox, &( *ppvBuffer ), portMAX_DELAY ) );\r
+ xEndTime = xTaskGetTickCount();\r
+ xElapsed = ( xEndTime - xStartTime ) * portTICK_PERIOD_MS;\r
+\r
+ if( xElapsed == 0UL )\r
+ {\r
+ xElapsed = 1UL;\r
+ }\r
+\r
+ ulReturn = xElapsed;\r
+ }\r
+\r
+ return ulReturn;\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_arch_mbox_tryfetch\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Similar to sys_arch_mbox_fetch, but if message is not ready\r
+ * immediately, we'll return with SYS_MBOX_EMPTY. On success, 0 is\r
+ * returned.\r
+ * Inputs:\r
+ * sys_mbox_t mbox -- Handle of mailbox\r
+ * void **msg -- Pointer to pointer to msg received\r
+ * Outputs:\r
+ * u32_t -- SYS_MBOX_EMPTY if no messages. Otherwise,\r
+ * return ERR_OK.\r
+ *---------------------------------------------------------------------------*/\r
+u32_t sys_arch_mbox_tryfetch( sys_mbox_t *pxMailBox, void **ppvBuffer )\r
+{\r
+void *pvDummy;\r
+unsigned long ulReturn;\r
+long lResult;\r
+portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
+\r
+ if( ppvBuffer== NULL )\r
+ {\r
+ ppvBuffer = &pvDummy;\r
+ }\r
+\r
+ if( xInsideISR != pdFALSE )\r
+ {\r
+ lResult = xQueueReceiveFromISR( *pxMailBox, &( *ppvBuffer ), &xHigherPriorityTaskWoken );\r
+ portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
+ }\r
+ else\r
+ {\r
+ lResult = xQueueReceive( *pxMailBox, &( *ppvBuffer ), 0UL );\r
+ }\r
+\r
+ if( lResult == pdPASS )\r
+ {\r
+ ulReturn = ERR_OK;\r
+ }\r
+ else\r
+ {\r
+ ulReturn = SYS_MBOX_EMPTY;\r
+ }\r
+\r
+ return ulReturn;\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_sem_new\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Creates and returns a new semaphore. The "ucCount" argument specifies\r
+ * the initial state of the semaphore.\r
+ * NOTE: Currently this routine only creates counts of 1 or 0\r
+ * Inputs:\r
+ * sys_mbox_t mbox -- Handle of mailbox\r
+ * u8_t ucCount -- Initial ucCount of semaphore (1 or 0)\r
+ * Outputs:\r
+ * sys_sem_t -- Created semaphore or 0 if could not create.\r
+ *---------------------------------------------------------------------------*/\r
+err_t sys_sem_new( sys_sem_t *pxSemaphore, u8_t ucCount )\r
+{\r
+err_t xReturn = ERR_MEM;\r
+\r
+ //vSemaphoreCreateBinary( ( *pxSemaphore ) );\r
+ *pxSemaphore = xSemaphoreCreateCounting( 0xffff, ( unsigned long ) ucCount );\r
+\r
+ if( *pxSemaphore != NULL )\r
+ {\r
+ if( ucCount == 0U )\r
+ {\r
+// xSemaphoreTake( *pxSemaphore, 1UL );\r
+ }\r
+\r
+ xReturn = ERR_OK;\r
+ SYS_STATS_INC_USED( sem );\r
+ }\r
+ else\r
+ {\r
+ SYS_STATS_INC( sem.err );\r
+ }\r
+\r
+ return xReturn;\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_arch_sem_wait\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Blocks the thread while waiting for the semaphore to be\r
+ * signaled. If the "timeout" argument is non-zero, the thread should\r
+ * only be blocked for the specified time (measured in\r
+ * milliseconds).\r
+ *\r
+ * If the timeout argument is non-zero, the return value is the number of\r
+ * milliseconds spent waiting for the semaphore to be signaled. If the\r
+ * semaphore wasn't signaled within the specified time, the return value is\r
+ * SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore\r
+ * (i.e., it was already signaled), the function may return zero.\r
+ *\r
+ * Notice that lwIP implements a function with a similar name,\r
+ * sys_sem_wait(), that uses the sys_arch_sem_wait() function.\r
+ * Inputs:\r
+ * sys_sem_t sem -- Semaphore to wait on\r
+ * u32_t timeout -- Number of milliseconds until timeout\r
+ * Outputs:\r
+ * u32_t -- Time elapsed or SYS_ARCH_TIMEOUT.\r
+ *---------------------------------------------------------------------------*/\r
+u32_t sys_arch_sem_wait( sys_sem_t *pxSemaphore, u32_t ulTimeout )\r
+{\r
+TickType_t xStartTime, xEndTime, xElapsed;\r
+unsigned long ulReturn;\r
+\r
+ xStartTime = xTaskGetTickCount();\r
+\r
+ if( ulTimeout != 0UL )\r
+ {\r
+ if( xSemaphoreTake( *pxSemaphore, ulTimeout / portTICK_PERIOD_MS ) == pdTRUE )\r
+ {\r
+ xEndTime = xTaskGetTickCount();\r
+ xElapsed = (xEndTime - xStartTime) * portTICK_PERIOD_MS;\r
+ ulReturn = xElapsed;\r
+ }\r
+ else\r
+ {\r
+ ulReturn = SYS_ARCH_TIMEOUT;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ while( xSemaphoreTake( *pxSemaphore, portMAX_DELAY ) != pdTRUE );\r
+ xEndTime = xTaskGetTickCount();\r
+ xElapsed = ( xEndTime - xStartTime ) * portTICK_PERIOD_MS;\r
+\r
+ if( xElapsed == 0UL )\r
+ {\r
+ xElapsed = 1UL;\r
+ }\r
+\r
+ ulReturn = xElapsed;\r
+ }\r
+\r
+ return ulReturn;\r
+}\r
+\r
+/** Create a new mutex\r
+ * @param mutex pointer to the mutex to create\r
+ * @return a new mutex */\r
+err_t sys_mutex_new( sys_mutex_t *pxMutex )\r
+{\r
+err_t xReturn = ERR_MEM;\r
+\r
+ *pxMutex = xSemaphoreCreateMutex();\r
+\r
+ if( *pxMutex != NULL )\r
+ {\r
+ xReturn = ERR_OK;\r
+ SYS_STATS_INC_USED( mutex );\r
+ }\r
+ else\r
+ {\r
+ SYS_STATS_INC( mutex.err );\r
+ }\r
+\r
+ return xReturn;\r
+}\r
+\r
+/** Lock a mutex\r
+ * @param mutex the mutex to lock */\r
+void sys_mutex_lock( sys_mutex_t *pxMutex )\r
+{\r
+BaseType_t xGotSemaphore;\r
+BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
+\r
+ if( xInsideISR == 0 )\r
+ {\r
+ while( xSemaphoreTake( *pxMutex, portMAX_DELAY ) != pdPASS );\r
+ }\r
+ else\r
+ {\r
+ xGotSemaphore = xSemaphoreTakeFromISR( *pxMutex, &xHigherPriorityTaskWoken );\r
+ configASSERT( xGotSemaphore );\r
+ portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
+\r
+ /* Prevent compiler warnings if configASSERT() is not defined. */\r
+ ( void ) xGotSemaphore;\r
+ }\r
+}\r
+\r
+/** Unlock a mutex\r
+ * @param mutex the mutex to unlock */\r
+void sys_mutex_unlock(sys_mutex_t *pxMutex )\r
+{\r
+ xSemaphoreGive( *pxMutex );\r
+}\r
+\r
+\r
+/** Delete a semaphore\r
+ * @param mutex the mutex to delete */\r
+void sys_mutex_free( sys_mutex_t *pxMutex )\r
+{\r
+ SYS_STATS_DEC( mutex.used );\r
+ vQueueDelete( *pxMutex );\r
+}\r
+\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_sem_signal\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Signals (releases) a semaphore\r
+ * Inputs:\r
+ * sys_sem_t sem -- Semaphore to signal\r
+ *---------------------------------------------------------------------------*/\r
+void sys_sem_signal( sys_sem_t *pxSemaphore )\r
+{\r
+portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
+\r
+ if( xInsideISR != pdFALSE )\r
+ {\r
+ xSemaphoreGiveFromISR( *pxSemaphore, &xHigherPriorityTaskWoken );\r
+ portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
+ }\r
+ else\r
+ {\r
+ xSemaphoreGive( *pxSemaphore );\r
+ }\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_sem_free\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Deallocates a semaphore\r
+ * Inputs:\r
+ * sys_sem_t sem -- Semaphore to free\r
+ *---------------------------------------------------------------------------*/\r
+void sys_sem_free( sys_sem_t *pxSemaphore )\r
+{\r
+ SYS_STATS_DEC(sem.used);\r
+ vQueueDelete( *pxSemaphore );\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_init\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Initialize sys arch\r
+ *---------------------------------------------------------------------------*/\r
+void sys_init(void)\r
+{\r
+}\r
+\r
+u32_t sys_now(void)\r
+{\r
+ return xTaskGetTickCount();\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_thread_new\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * Starts a new thread with priority "prio" that will begin its\r
+ * execution in the function "thread()". The "arg" argument will be\r
+ * passed as an argument to the thread() function. The id of the new\r
+ * thread is returned. Both the id and the priority are system\r
+ * dependent.\r
+ * Inputs:\r
+ * char *name -- Name of thread\r
+ * void (* thread)(void *arg) -- Pointer to function to run.\r
+ * void *arg -- Argument passed into function\r
+ * int stacksize -- Required stack amount in bytes\r
+ * int prio -- Thread priority\r
+ * Outputs:\r
+ * sys_thread_t -- Pointer to per-thread timeouts.\r
+ *---------------------------------------------------------------------------*/\r
+sys_thread_t sys_thread_new( const char *pcName, void( *pxThread )( void *pvParameters ), void *pvArg, int iStackSize, int iPriority )\r
+{\r
+TaskHandle_t xCreatedTask;\r
+portBASE_TYPE xResult;\r
+sys_thread_t xReturn;\r
+\r
+ xResult = xTaskCreate( pxThread, pcName, iStackSize, pvArg, iPriority, &xCreatedTask );\r
+\r
+ if( xResult == pdPASS )\r
+ {\r
+ xReturn = xCreatedTask;\r
+ }\r
+ else\r
+ {\r
+ xReturn = NULL;\r
+ }\r
+\r
+ return xReturn;\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_arch_protect\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * This optional function does a "fast" critical region protection and\r
+ * returns the previous protection level. This function is only called\r
+ * during very short critical regions. An embedded system which supports\r
+ * ISR-based drivers might want to implement this function by disabling\r
+ * interrupts. Task-based systems might want to implement this by using\r
+ * a mutex or disabling tasking. This function should support recursive\r
+ * calls from the same task or interrupt. In other words,\r
+ * sys_arch_protect() could be called while already protected. In\r
+ * that case the return value indicates that it is already protected.\r
+ *\r
+ * sys_arch_protect() is only required if your port is supporting an\r
+ * operating system.\r
+ * Outputs:\r
+ * sys_prot_t -- Previous protection level (not used here)\r
+ *---------------------------------------------------------------------------*/\r
+sys_prot_t sys_arch_protect( void )\r
+{\r
+ if( xInsideISR == pdFALSE )\r
+ {\r
+ taskENTER_CRITICAL();\r
+ }\r
+ return ( sys_prot_t ) 1;\r
+}\r
+\r
+/*---------------------------------------------------------------------------*\r
+ * Routine: sys_arch_unprotect\r
+ *---------------------------------------------------------------------------*\r
+ * Description:\r
+ * This optional function does a "fast" set of critical region\r
+ * protection to the value specified by pval. See the documentation for\r
+ * sys_arch_protect() for more information. This function is only\r
+ * required if your port is supporting an operating system.\r
+ * Inputs:\r
+ * sys_prot_t -- Previous protection level (not used here)\r
+ *---------------------------------------------------------------------------*/\r
+void sys_arch_unprotect( sys_prot_t xValue )\r
+{\r
+ (void) xValue;\r
+ if( xInsideISR == pdFALSE )\r
+ {\r
+ taskEXIT_CRITICAL();\r
+ }\r
+}\r
+\r
+/*\r
+ * Prints an assertion messages and aborts execution.\r
+ */\r
+void sys_assert( const char *pcMessage )\r
+{\r
+ (void) pcMessage;\r
+\r
+ for (;;)\r
+ {\r
+ }\r
+}\r
+/*-------------------------------------------------------------------------*\r
+ * End of File: sys_arch.c\r
+ *-------------------------------------------------------------------------*/\r
+\r