cancel
Showing results for 
Search instead for 
Did you mean: 

µC/OS-III transplant lwip 2.2.0, want a stable version sys_arch.c and sys_arch.h

帅气王老板
Associate

I want a stable version sys_arch.c and sys_arch.h based on µC/OS-III, LwIP only provide example for FreeRTOS.

I found follow example, please help to optimize it, thanks

 

 

#define SYS_ARCH_GLOBALS

/* lwIP includes. */
#include "lwip/debug.h"
#include "lwip/def.h"
#include "lwip/sys.h"
#include "lwip/mem.h"
#include "lwip/timeouts.h"
#include "lwip/tcpip.h"
#include "includes.h"
#include "delay.h"
#include "sys_arch.h"
#include "malloc.h"
#include <os_cfg_app.h>

const void * const pvNullPointer = (mem_ptr_t*)0xffffffff;
 
err_t sys_mbox_new( sys_mbox_t *mbox, int size)
{
	OS_ERR err;

	(*mbox)=mymalloc(SRAMIN,sizeof(TQ_DESCR));
	my_mem_set((*mbox),0,sizeof(TQ_DESCR));

	(*mbox)->pQ=mymalloc(SRAMIN,sizeof(OS_Q));
	my_mem_set((*mbox)->pQ,0,sizeof(OS_Q));

	if(*mbox&&(*mbox)->pQ)
	{
		if(size>MAX_QUEUE_ENTRIES)
			size=MAX_QUEUE_ENTRIES;

		OSQCreate((*mbox)->pQ,"lwip_mbox",size,&err); 
		LWIP_ASSERT("OSQCreate",err==OS_ERR_NONE);
		if(err==OS_ERR_NONE)
			return ERR_OK;  
		else
		{ 
			myfree(SRAMIN,(*mbox));
			return ERR_MEM;  		
		}
	}else return ERR_MEM; 			
} 

void sys_mbox_free(sys_mbox_t * mbox)
{
	OS_ERR err;
	sys_mbox_t m_box=*mbox;   
	OSQDel(m_box->pQ,OS_OPT_DEL_ALWAYS,&err);
	LWIP_ASSERT( "OSQDel ",err == OS_ERR_NONE );
	myfree(SRAMIN,m_box);
	*mbox=NULL;
}

void sys_mbox_post(sys_mbox_t *mbox,void *msg)
{
	OS_ERR err;
	OS_MSG_SIZE msg_size;
	if(msg==NULL)
		msg=(void*)&pvNullPointer;

	msg_size = (OS_MSG_SIZE)sizeof(msg);

	do {
		OSQPost((*mbox)->pQ,(void*)msg,msg_size,OS_OPT_POST_ALL,&err);
	} while(err!=OS_ERR_NONE);
}

err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
{ 
	OS_ERR err;
	OS_MSG_SIZE msg_size;
	if(msg==NULL)
		msg=(void*)&pvNullPointer; 

	msg_size = (OS_MSG_SIZE)sizeof(msg);

	OSQPost((*mbox)->pQ,(void*)msg,msg_size,OS_OPT_POST_ALL,&err);
	if(err!=OS_ERR_NONE)
		return ERR_MEM;

	return ERR_OK;
}

u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
{ 
	OS_ERR err;
	OS_MSG_SIZE msg_size;
	u32_t ucos_timeout,timeout_new;
	void *temp;
	sys_mbox_t m_box=*mbox;

	if(timeout!=0)
	{
		ucos_timeout=(timeout*OS_CFG_TICK_RATE_HZ)/1000;
		if(ucos_timeout<1)
			ucos_timeout=1;
	}
	else
		ucos_timeout = 0; 

	timeout = OSTimeGet(&err);

	temp=OSQPend(m_box->pQ,
				 (OS_TICK)ucos_timeout,
				 OS_OPT_PEND_BLOCKING,
				 &msg_size,
				 (CPU_TS *)0,
				 &err);

	if(msg!=NULL)
	{	
		if(temp==(void*)&pvNullPointer)*msg = NULL;
 		else *msg=temp;									
	}    
	if(err==OS_ERR_TIMEOUT)timeout=SYS_ARCH_TIMEOUT;
	else
	{
		LWIP_ASSERT("OSQPend ",err==OS_ERR_NONE); 
		timeout_new=OSTimeGet(&err);
		if (timeout_new>timeout) timeout_new = timeout_new - timeout;
		else timeout_new = 0xffffffff - timeout + timeout_new; 
		timeout=timeout_new*1000/OS_CFG_TICK_RATE_HZ + 1;
	}
	return timeout; 
}

u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
{
	return sys_arch_mbox_fetch(mbox,msg,1);
}

int sys_mbox_valid(sys_mbox_t *mbox)
{  
	sys_mbox_t m_box=*mbox;
	//u8_t ucErr;
	int ret;
	//OS_Q_DATA q_data;
	//memset(&q_data,0,sizeof(OS_Q_DATA));
	//ucErr=OSQQuery (m_box->pQ,&q_data);
	ret=(/*ucErr<2&&*/(m_box->pQ->MsgQ.NbrEntries<m_box->pQ->MsgQ.NbrEntriesSize))?1:0;
	return ret; 
} 

void sys_mbox_set_invalid(sys_mbox_t *mbox)
{
	*mbox=NULL;
} 

err_t sys_sem_new(sys_sem_t * sem, u8_t count)
{  
	OS_ERR err;

	(*sem)=mymalloc(SRAMIN,sizeof(OS_SEM));
	my_mem_set(*sem,0,sizeof(OS_SEM));
	LWIP_ASSERT("OSSemCreate ",*sem != NULL);

	if(*sem)
	{
		OSSemCreate(*sem,"LWIP Sem",(u16_t)count,&err);
		if(err==OS_ERR_NONE)
			return ERR_OK;
		else
			return ERR_MEM;
	}
	else
		return ERR_MEM;
} 

u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
{ 
	OS_ERR err;
	u32_t ucos_timeout, timeout_new; 
	if(timeout!=0) 
	{
		ucos_timeout = (timeout * OS_CFG_TICK_RATE_HZ) / 1000;
		if(ucos_timeout < 1)
			ucos_timeout = 1;
	}else
		ucos_timeout = 0; 

	timeout = OSTimeGet(&err);
	OSSemPend (*sem,(u16_t)ucos_timeout,OS_OPT_PEND_BLOCKING,NULL,&err);
 	if(err == OS_ERR_TIMEOUT)
		timeout=SYS_ARCH_TIMEOUT;
	else
	{     
 		timeout_new = OSTimeGet(&err);
		if (timeout_new>=timeout)
			timeout_new = timeout_new - timeout;
		else
			timeout_new = 0xffffffff - timeout + timeout_new;

		timeout = (timeout_new*1000/OS_CFG_TICK_RATE_HZ + 1);
	}
	return timeout;
}

void sys_sem_signal(sys_sem_t *sem)
{
	OS_ERR err;
	OSSemPost(*sem, OS_OPT_POST_ALL, &err);
}

void sys_sem_free(sys_sem_t *sem)
{
	OS_ERR err;
	(void)OSSemDel(*sem,OS_OPT_DEL_ALWAYS,&err);
	if(err!=OS_ERR_NONE)LWIP_ASSERT("OSSemDel ",err==OS_ERR_NONE);
	*sem = NULL;
} 

int sys_sem_valid(sys_sem_t *sem)
{
	//OS_SEM_DATA  sem_data;
	//return (OSSemQuery (*sem,&sem_data) == OS_ERR_NONE )? 1:0;              

	sys_sem_t sem_t = *sem;
	if (sem_t == NULL) {
		return 0;
	}

	if (sem_t->Type != OS_OBJ_TYPE_SEM) { 	/* Validate event block type 			   */
		return 0;
	}

	return 1;
}

void sys_sem_set_invalid(sys_sem_t *sem)
{
	*sem=NULL;
} 

void sys_init(void)
{ 
} 
extern CPU_STK * TCPIP_THREAD_TASK_STK;
extern OS_TCB tcpip_thread_tcb;
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
{
	OS_ERR err;
	CPU_SR_ALLOC();
	if(strcmp(name,TCPIP_THREAD_NAME)==0)
	{
		OS_CRITICAL_ENTER();
		OSTaskCreate((OS_TCB	* )&tcpip_thread_tcb, 	
					 (CPU_CHAR	* )name,		
					 (OS_TASK_PTR )thread, 			
					 (void		* )arg,
					 (OS_PRIO	  )prio,
					 (CPU_STK	* )&TCPIP_THREAD_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	* )&err);
		OS_CRITICAL_EXIT();
	} 
	return 0;
} 

err_t sys_mbox_trypost_fromisr(sys_mbox_t *mbox, void *msg) {
    OS_ERR err;
	sys_mbox_t m_box=*mbox;
	OSQPost(m_box->pQ, msg, 0, OS_OPT_POST_FIFO, &err);
    if (err == OS_ERR_NONE) {
        return ERR_OK;
    } else {
        return ERR_MEM;
    }
}

void sys_mutex_lock(sys_mutex_t *mutex) {
    OS_ERR err;
    OSMutexPend((OS_MUTEX *)*mutex, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
}

void sys_mutex_unlock(sys_mutex_t *mutex) {
    OS_ERR err;
    OSMutexPost((OS_MUTEX *)*mutex, OS_OPT_POST_NONE, &err);
}

err_t sys_mutex_new(sys_mutex_t *mutex) {
    OS_ERR err;
    *mutex = mymalloc(SRAMIN, sizeof(OS_MUTEX));
    if (*mutex != NULL) {
        OSMutexCreate((OS_MUTEX *)*mutex, "LwIP Mutex", &err);
        if (err == OS_ERR_NONE) {
            return ERR_OK;
        }
    }
    return ERR_MEM;
}

void tcpip_thread(void *arg) {
	OS_ERR err;
    tcpip_init(NULL, NULL);
    while (1) {
        sys_check_timeouts();
        OSTimeDlyHMSM(0, 0, 0, 100, OS_OPT_TIME_DLY, &err);
    }
}

void tcpip_thread_init(void) {
    OS_ERR err;
    OS_TCB *tcb = mymalloc(SRAMIN, sizeof(OS_TCB));
    void *stack = mymalloc(SRAMIN, TCPIP_THREAD_STACKSIZE);
    OSTaskCreate(tcb, "TCP/IP Task", tcpip_thread, NULL, TCPIP_THREAD_PRIO,
                 stack, TCPIP_THREAD_STACKSIZE/10, TCPIP_THREAD_STACKSIZE, 0, 0, NULL,
                 OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR, &err);
    if (err != OS_ERR_NONE) {
    }
}

 

 

1 REPLY 1
Passerby
Associate III