/* * Copyright (c) 2001-2003 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels * */ /* lwIP includes. */ #include "lwip/debug.h" #include "lwip/def.h" #include "lwip/sys.h" #include "lwip/mem.h" #include "lpc_arch.h" #include "app_cfg.h" #include #if NO_SYS==0 /* ------------------------ System architecture includes ----------------------------- */ #include "arch/sys_arch.h" /* ------------------------ lwIP includes --------------------------------- */ #include "lwip/opt.h" #include "lwip/stats.h" /*----------------------------------------------------------------------------*/ /* DEFINITIONS */ /*----------------------------------------------------------------------------*/ //#define OS_CFG_TICK_RATE_HZ 1000 /*----------------------------------------------------------------------------*/ /* VARIABLES */ /*----------------------------------------------------------------------------*/ //定义lwip线程所需要的资源 static OS_MEM StackMem; const void * const pvNullPointer = (mem_ptr_t*)0xffffffff; __align(4) CPU_STK LwIP_Task_Stk[LWIP_TASK_MAX*LWIP_STK_SIZE]; __align(4) CPU_INT08U LwIP_task_priopity_stask[LWIP_TASK_MAX]; OS_TCB LwIP_task_TCB[LWIP_TASK_MAX]; /*--------------------------------------------------------------------------- * * prototypes dealing with mailbox *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* * Routine: sys_mbox_new ucosiii version *---------------------------------------------------------------------------* * Description: * Creates a new mailbox * Inputs: * sys_mbox_t *pxMailBox -- Handle to new mailbox * s32_t size -- Size of elements in the mailbox * Outputs: * err_t -- lwip error type *---------------------------------------------------------------------------*/ err_t sys_mbox_new( sys_mbox_t *pxMailBox, s32_t size ) { OS_ERR ucErr; OSQCreate(pxMailBox,"LWIP queue", size, &ucErr); //debugging interface need to defined by user if (ucErr == OS_ERR_NONE) { return ERR_OK; } return ERR_MEM; } /*---------------------------------------------------------------------------* * Routine: sys_mbox_free ucosiii version *---------------------------------------------------------------------------* * Description: * Deallocates a mailbox. Flush message queue and the deallocate it * different from freertos * Inputs: * sys_mbox_t *pxMailBox -- Handle of mailbox *---------------------------------------------------------------------------*/ void sys_mbox_free( sys_mbox_t *pxMailBox ) { OS_ERR ucErr; //LWIP_ASSERT( "sys_mbox_free ", pxMailBox != SYS_MBOX_NULL ); //清空消息队列 OSQFlush(pxMailBox,& ucErr); OSQDel(pxMailBox, OS_OPT_DEL_ALWAYS, &ucErr); //LWIP_ASSERT( "OSQDel ", ucErr == OS_ERR_NONE ); } /*---------------------------------------------------------------------------* * Routine: sys_mbox_post ucosiii version *---------------------------------------------------------------------------* * Description: * Post the "pxMessageToPost" to the mailbox. * Inputs: * sys_mbox_t pxMailBox -- Handle of mailbox * void *pxMessageToPost -- Pointer to data to post *---------------------------------------------------------------------------*/ void sys_mbox_post( sys_mbox_t *pxMailBox, void *pxMessageToPost ) { OS_ERR ucErr; CPU_INT08U i=0; //消息指针为空,则指向一个默认的消息 if( pxMessageToPost == NULL ) pxMessageToPost = (void*)&pvNullPointer; /* try 10 times */ while(i<10){ OSQPost(pxMailBox, pxMessageToPost,sizeof(pxMessageToPost),OS_OPT_POST_ALL,&ucErr); if(ucErr == OS_ERR_NONE) break; i++; OSTimeDly(5,OS_OPT_TIME_DLY,&ucErr); } //LWIP_ASSERT( "sys_mbox_post error!\n", i !=10 ); } /*---------------------------------------------------------------------------* * Routine: sys_mbox_trypost ucosiii version *---------------------------------------------------------------------------* * Description: * Try to post the "msg" to the mailbox. Returns immediately with * error if cannot. * Inputs: * sys_mbox_t pxMailBox -- Handle of mailbox * void *pxMessageToPost -- Pointer to data to post * Outputs: * err_t -- ERR_OK if message posted, else ERR_MEM * if not. *---------------------------------------------------------------------------*/ err_t sys_mbox_trypost( sys_mbox_t *pxMailBox, void *pxMessageToPost ) { OS_ERR ucErr; if(pxMessageToPost == NULL ) pxMessageToPost = (void*)&pvNullPointer; OSQPost(pxMailBox, pxMessageToPost,sizeof(pxMessageToPost),OS_OPT_POST_ALL,&ucErr); if(ucErr != OS_ERR_NONE){ return ERR_MEM; } return ERR_OK; } /*---------------------------------------------------------------------------* * Routine: sys_arch_mbox_fetch ucosiii version *---------------------------------------------------------------------------* * Description: * Blocks the thread until a message arrives in the mailbox, but does * not block the thread longer than "timeout" milliseconds (similar to * the sys_arch_sem_wait() function). The "msg" argument is a result * parameter that is set by the function (i.e., by doing "*msg = * ptr"). The "msg" parameter maybe NULL to indicate that the message * should be dropped. * * The return values are the same as for the sys_arch_sem_wait() function: * Number of milliseconds spent waiting or SYS_ARCH_TIMEOUT if there was a * timeout. * * Note that a function with a similar name, sys_mbox_fetch(), is * implemented by lwIP. * Inputs: * sys_mbox_t pxMailBox -- Handle of mailbox * void **ppvBuffer -- Pointer to pointer to msg received * u32_t ulTimeOut -- Number of milliseconds until timeout * Outputs: * u32_t -- SYS_ARCH_TIMEOUT if timeout, else number * of milliseconds until received. *---------------------------------------------------------------------------*/ u32_t sys_arch_mbox_fetch( sys_mbox_t *pxMailBox, void **ppvBuffer, u32_t ulTimeOut ) { OS_ERR ucErr; OS_MSG_SIZE msg_size; CPU_TS ucos_timeout; CPU_TS in_timeout = ulTimeOut/LWIP_ARCH_TICK_PER_MS; if(ulTimeOut && in_timeout == 0) in_timeout = 1; ucos_timeout=0; *ppvBuffer = OSQPend (pxMailBox,in_timeout,OS_OPT_PEND_BLOCKING,&msg_size, 0,&ucErr); if ( ucErr == OS_ERR_TIMEOUT ) ucos_timeout = SYS_ARCH_TIMEOUT; return ucos_timeout; } /*---------------------------------------------------------------------------* * Routine: sys_arch_mbox_tryfetch usosiii version *---------------------------------------------------------------------------* * Description: * Similar to sys_arch_mbox_fetch, but if message is not ready * immediately, we'll return with SYS_MBOX_EMPTY. On success, 0 is * returned. * Inputs: * sys_mbox_t mbox -- Handle of mailbox * void **msg -- Pointer to pointer to msg received * Outputs: * u32_t -- SYS_MBOX_EMPTY if no messages. Otherwise, * return ERR_OK. * complementary: * ucos中暂时没有相应的实现机制,暂时使用sys_arch_mbox_fetch实现 *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* * Routine: sys_mbox_valid usosiii version *---------------------------------------------------------------------------* * Description: * Check if an mbox is valid/allocated * if success return 1 * Inputs: * sys_mbox_t *pxMailBox -- Handle of mailbox * Outputs: * s32_t -- 1 if valid. Otherwise return 0 *---------------------------------------------------------------------------*/ s32_t sys_mbox_valid(sys_mbox_t *pxMailBox) { if(pxMailBox->NamePtr) return (strcmp(pxMailBox->NamePtr,"?Q"))? 1:0; else return 0; } /*---------------------------------------------------------------------------* * Routine: sys_mbox_invalid usosiii version *---------------------------------------------------------------------------* * Description: * Set an mbox invalid so that sys_mbox_valid returns 0 * Inputs: * sys_mbox_t *pxMailBox -- Handle of mailbox *---------------------------------------------------------------------------*/ void sys_mbox_set_invalid(sys_mbox_t *pxMailBox) { if(sys_mbox_valid(pxMailBox)) sys_mbox_free(pxMailBox); } /*---------------------------------------------------------------------------* * prototypes dealing with semaphore *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* * Routine: sys_sem_new ucosiii version *---------------------------------------------------------------------------* * Description: * Creates and returns a new semaphore. The "ucCount" argument specifies * the initial state of the semaphore. * NOTE: Currently this routine only creates counts of 1 or 0 * Inputs: * sys_mbox_t mbox -- Handle of mailbox * u8_t ucCount -- Initial ucCount of semaphore (1 or 0) * Outputs: * sys_sem_t -- Created semaphore or 0 if could not create. *---------------------------------------------------------------------------*/ err_t sys_sem_new( sys_sem_t *pxSemaphore, u8_t ucCount ) { OS_ERR ucErr; OSSemCreate (pxSemaphore,"LWIP Sem",ucCount,&ucErr); if(ucErr != OS_ERR_NONE ){ //LWIP_ASSERT("OSSemCreate ",ucErr == OS_ERR_NONE ); return ERR_MEM; } return ERR_OK; } /*---------------------------------------------------------------------------* * Routine: sys_arch_sem_wait *---------------------------------------------------------------------------* * Description: * Blocks the thread while waiting for the semaphore to be * signaled. If the "timeout" argument is non-zero, the thread should * only be blocked for the specified time (measured in * milliseconds). * * If the timeout argument is non-zero, the return value is the number of * milliseconds spent waiting for the semaphore to be signaled. If the * semaphore wasn't signaled within the specified time, the return value is * SYS_ARCH_TIMEOUT. If the thread didn't have to wait for the semaphore * (i.e., it was already signaled), the function may return zero. * * Notice that lwIP implements a function with a similar name, * sys_sem_wait(), that uses the sys_arch_sem_wait() function. * Inputs: * sys_sem_t sem -- Semaphore to wait on * u32_t timeout -- Number of milliseconds until timeout * Outputs: * u32_t -- Time elapsed or SYS_ARCH_TIMEOUT. *---------------------------------------------------------------------------*/ u32_t sys_arch_sem_wait( sys_sem_t *pxSemaphore, u32_t ulTimeout ) { OS_ERR ucErr; CPU_TS ucos_timeout; CPU_TS in_timeout = ulTimeout/LWIP_ARCH_TICK_PER_MS; if(ulTimeout && in_timeout == 0) in_timeout = 1; //OSSemPend (pxSemaphore,in_timeout,OS_OPT_PEND_BLOCKING,0,&ucErr); //OSSemPend (pxSemaphore,ulTimeout,OS_OPT_PEND_BLOCKING,0,&ucErr); OSSemPend (pxSemaphore,0,OS_OPT_PEND_BLOCKING,0,&ucErr); /* only when timeout! */ if(ucErr == OS_ERR_TIMEOUT) ucos_timeout = SYS_ARCH_TIMEOUT; return ucos_timeout; } /*---------------------------------------------------------------------------* * Routine: sys_mutex_new ucosiii version *---------------------------------------------------------------------------* * Description: * Creates and returns a new Mutex. * Inputs: * sys_mutex_t pxMutex -- Handle of mailbox * Outputs: * sys_mutex_t -- Created semaphore or 0 if could not create. *---------------------------------------------------------------------------*/ err_t sys_mutex_new( sys_mutex_t *pxMutex ) { OS_ERR ucErr; OSMutexCreate(pxMutex,"LWIP Mutex",&ucErr); if(ucErr != OS_ERR_NONE ){ //LWIP_ASSERT("OSMutexCreate ",ucErr == OS_ERR_NONE ); return ERR_MEM; } return ERR_OK; } /*---------------------------------------------------------------------------* * Routine: sys_mutex_lock ucosiii version *---------------------------------------------------------------------------* * Description: * Lock a mutex * Inputs: * sys_mutex_t pxMutex -- the mutex to lock *---------------------------------------------------------------------------*/ void sys_mutex_lock( sys_mutex_t *pxMutex ) { OS_ERR ucERR; OSMutexPend(pxMutex,0,OS_OPT_POST_ALL,NULL,&ucERR); } /*---------------------------------------------------------------------------* * Routine: sys_mutex_unlock ucosiii version *---------------------------------------------------------------------------* * Description: * unLock a mutex * Inputs: * sys_mutex_t pxMutex -- the mutex to unlock *---------------------------------------------------------------------------*/ void sys_mutex_unlock(sys_mutex_t *pxMutex ) { OS_ERR ucERR; OSMutexPost(pxMutex,OS_OPT_POST_NONE,&ucERR); } /* * Delete a semaphore * @param pxMutex the mutex to delete */ /* void sys_mutex_free( sys_mutex_t *pxMutex ) { OS_ERR ucERR; OSMutexDel(pxMutex,OS_OPT_DEL_ALWAYS,&ucERR); } */ /*---------------------------------------------------------------------------* * Routine: sys_sem_signal ucosiii version *---------------------------------------------------------------------------* * Description: * Signals (releases) a semaphore * Inputs: * sys_sem_t pxSemaphore -- Semaphore to signal *---------------------------------------------------------------------------*/ void sys_sem_signal( sys_sem_t *pxSemaphore ) { OS_ERR ucErr; OSSemPost(pxSemaphore,OS_OPT_POST_ALL,&ucErr); //LWIP_ASSERT("OSSemPost ",ucErr == OS_ERR_NONE ); } /*---------------------------------------------------------------------------* * Routine: sys_sem_free ucosiii version *---------------------------------------------------------------------------* * Description: * Deallocates a semaphore * Inputs: * sys_sem_t pxSemaphore -- Semaphore to free *---------------------------------------------------------------------------*/ void sys_sem_free( sys_sem_t *pxSemaphore ) { OS_ERR ucErr; OSSemDel(pxSemaphore, OS_OPT_DEL_ALWAYS, &ucErr ); //LWIP_ASSERT( "OSSemDel ", ucErr == OS_ERR_NONE ); } /*---------------------------------------------------------------------------* * Routine: sys_sem_valid usosiii version *---------------------------------------------------------------------------* * Description: * Check if an mbox is valid/allocated * if success return 1 * Inputs: * sys_mbox_t *pxSemaphore -- Handle of mailbox * Outputs: * s32_t -- 1 if valid. Otherwise return 0 *---------------------------------------------------------------------------*/ s32_t sys_sem_valid(sys_sem_t *pxSemaphore) { if(pxSemaphore->NamePtr) return (strcmp(pxSemaphore->NamePtr,"?SEM"))? 1:0; else return 0; } /*---------------------------------------------------------------------------* * Routine: sys_sem_set_invalid usosiii version *---------------------------------------------------------------------------* * Description: * Set an mbox invalid so that sys_mbox_valid returns 0 * Inputs: * sys_mbox_t *pxSemaphore -- Handle of mailbox *---------------------------------------------------------------------------*/ void sys_sem_set_invalid(sys_sem_t *pxSemaphore) { if(sys_sem_valid(pxSemaphore)) sys_sem_free(pxSemaphore); } /*---------------------------------------------------------------------------* * Routine: sys_init *---------------------------------------------------------------------------* * Description: * Initialize sys arch *---------------------------------------------------------------------------*/ void sys_init(void) { OS_ERR ucErr; memset(LwIP_task_priopity_stask,0,sizeof(LwIP_task_priopity_stask)); /* init mem used by sys_mbox_t, use ucosIII functions */ OSMemCreate(&StackMem, "LWIP TASK STK", (void*)LwIP_Task_Stk,LWIP_TASK_MAX, LWIP_STK_SIZE*sizeof(CPU_STK), &ucErr); //LWIP_ASSERT( "sys_init: failed OSMemCreate STK", ucErr == OS_ERR_NONE ); } /*暂未使能ucosiii计时功能 u32_t sys_now(void) { return xTaskGetTickCount(); } */ /*---------------------------------------------------------------------------* * Routine: sys_thread_new *---------------------------------------------------------------------------* * Description: * Starts a new thread with priority "prio" that will begin its * execution in the function "thread()". The "arg" argument will be * passed as an argument to the thread() function. The id of the new * thread is returned. Both the id and the priority are system * dependent. * Inputs: * char *name -- Name of thread * void (* thread)(void *arg) -- Pointer to function to run. * void *arg -- Argument passed into function * int stacksize -- Required stack amount in bytes * int prio -- Thread priority * Outputs: * sys_thread_t -- Pointer to per-thread timeouts. *---------------------------------------------------------------------------*/ sys_thread_t sys_thread_new( const char *name, void( *thread )( void *pvParameters ), void *arg, int stacksize, int prio ) { CPU_INT08U ubPrio = LWIP_TASK_START_PRIO; CPU_INT08U tsk_prio; CPU_STK *task_stk; OS_ERR ucErr; static u32_t i = 0; arg = arg; /*1111 //确定task优先级 if(prio){ ubPrio +=(prio-1); //确认优先权数组中有无优先级为(LWIP_TASK_START_PRIO+prio-1)的数 for(i=0; i= (LWIP_TASK_START_PRIO+LWIP_TASK_MAX) || i == LWIP_TASK_MAX){ LWIP_ASSERT( "sys_thread_new: there is no free priority", 0 ); return (-1); } } */ //确定task堆栈值 if(stacksize > LWIP_STK_SIZE || !stacksize) stacksize = LWIP_STK_SIZE; /* get Stack from pool */ // CPU_STK * task_stk = OSMemGet( &StackMem, &ucErr ); task_stk = OSMemGet( &StackMem, &ucErr ); if(ucErr != OS_ERR_NONE){ //LWIP_ASSERT( "sys_thread_new: impossible to get a stack", 0 ); return (-1); } if(i < 6){ tsk_prio = i; i++; } else return (-1); // tsk_prio = ubPrio-LWIP_TASK_START_PRIO; OSTaskCreate(&LwIP_task_TCB[tsk_prio], (CPU_CHAR *)name, (OS_TASK_PTR )thread, (void *)arg, (OS_PRIO )prio, //ubPrio, (CPU_STK *)&task_stk[0], (CPU_STK_SIZE )stacksize/10, (CPU_STK_SIZE )stacksize, (OS_MSG_QTY )0, (OS_TICK )0, (void *)0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&ucErr); return ubPrio; //return ubPrio; } /*---------------------------------------------------------------------------* * Routine: sys_arch_protect ucosiii version *---------------------------------------------------------------------------* * Description: * This optional function does a "fast" critical region protection and * returns the previous protection level. This function is only called * during very short critical regions. An embedded system which supports * ISR-based drivers might want to implement this function by disabling * interrupts. Task-based systems might want to implement this by using * a mutex or disabling tasking. This function should support recursive * calls from the same task or interrupt. In other words, * sys_arch_protect() could be called while already protected. In * that case the return value indicates that it is already protected. * * sys_arch_protect() is only required if your port is supporting an * operating system. * Outputs: * sys_prot_t -- Previous protection level (not used here) *---------------------------------------------------------------------------*/ sys_prot_t sys_arch_protect( void ) { CPU_SR cpu_sr; OS_CRITICAL_ENTER(); return (( sys_prot_t )1); } /*---------------------------------------------------------------------------* * Routine: sys_arch_unprotect ucosiii version *---------------------------------------------------------------------------* * Description: * This optional function does a "fast" set of critical region * protection to the value specified by pval. See the documentation for * sys_arch_protect() for more information. This function is only * required if your port is supporting an operating system. * Inputs: * sys_prot_t -- Previous protection level (not used here) *---------------------------------------------------------------------------*/ void sys_arch_unprotect( sys_prot_t xValue ) { CPU_SR cpu_sr; OS_CRITICAL_EXIT(); } /* * Prints an assertion messages and aborts execution. */ /* void sys_assert( const char *pcMessage ) { (void) pcMessage; for (;;) { } } */ /** * @brief MilliSecond delay function based on FreeRTOS * @param ms : Number of milliSeconds to delay * @return Nothing * Needed for some functions, do not use prior to FreeRTOS running */ void msDelay(CPU_INT32U ms) { OS_ERR ucErr; OSTimeDly(ms, OS_OPT_TIME_DLY, &ucErr); } /*-------------------------------------------------------------------------* * End of File: sys_arch.c *-------------------------------------------------------------------------*/ #endif /*-----------------------------------------------------------------------------------*/