⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.c

📁 ucos 在VC上的移植,已成功调试,大家请
💻 C
字号:
/*
 * Copyright (c) 2001,2002 Florian Schulze.
 * 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. Neither the name of the authors nor the names of the contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``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 AUTHORS OR CONTRIBUTORS 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.
 *
 * test.c - This file is part of lwIPtest
 *
 */

#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <conio.h>
 
#include "lwip/debug.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/sys.h"

#include "lwip/stats.h"

#include "lwip/tcpip.h"

#include "arch/sys_arch.h"

#include "netif/loopif.h"

#include "arch/perf.h"

#include "httpd.h"
//#include "ftpd.h"
//#include "fs.h"

#define  TASK_STK_SIZE    128
#define  TASK_START_PRIO    5
#define  TASK_START_PRIO1   6
#define  TASK_START_PRIO2   4
/*
*********************************************************************************************************
*                                                VARIABLES
*********************************************************************************************************
*/

OS_STK        AppStartTaskStk[TASK_STK_SIZE];
OS_STK        AppStartTaskStk1[TASK_STK_SIZE];
OS_STK        AppStartTaskStk2[4096];
/*
*********************************************************************************************************
*                                            FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  void  AppStartTask(void *p_arg);
static  void  AppStartTask1(void *p_arg);
static void lwip_init_task(void * pParam);
#if OS_VIEW_MODULE > 0
static  void  AppTerminalRx(INT8U rx_data);
#endif

/*
*********************************************************************************************************
*                                                _tmain()
*
* Description : This is the standard entry point for C++ WIN32 code.  
* Arguments   : none
*********************************************************************************************************
*/

void main(int argc, char *argv[])
{
	INT8U  err;


#if 0
    BSP_IntDisAll();                       /* For an embedded target, disable all interrupts until we are ready to accept them */
#endif

    OSInit();                              /* Initialize "uC/OS-II, The Real-Time Kernel"                                      */

    OSTaskCreateExt(AppStartTask,
                    (void *)0,
                    (OS_STK *)&AppStartTaskStk[TASK_STK_SIZE-1],
                    TASK_START_PRIO,
                    TASK_START_PRIO,
                    (OS_STK *)&AppStartTaskStk[0],
                    TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);

	OSTaskCreateExt(AppStartTask1,
                    (void *)0,
                    (OS_STK *)&AppStartTaskStk1[TASK_STK_SIZE-1],
                    TASK_START_PRIO1,
                    TASK_START_PRIO1,
                    (OS_STK *)&AppStartTaskStk1[0],
                    TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
		OSTaskCreateExt(lwip_init_task,
                    (void *)0,
                    (OS_STK *)&AppStartTaskStk2[TASK_STK_SIZE-1],
                    TASK_START_PRIO2,
                    TASK_START_PRIO2,
                    (OS_STK *)&AppStartTaskStk2[0],
                    TASK_STK_SIZE,
                    (void *)0,
                    OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
 
    OSStart();                             /* Start multitasking (i.e. give control to uC/OS-II)                               */
}
/*$PAGE*/
/*
*********************************************************************************************************
*                                          STARTUP TASK
*
* Description : This is an example of a startup task.  As mentioned in the book's text, you MUST
*               initialize the ticker only once multitasking has started.
* Arguments   : p_arg   is the argument passed to 'AppStartTask()' by 'OSTaskCreate()'.
* Notes       : 1) The first line of code is used to prevent a compiler warning because 'p_arg' is not
*                  used.  The compiler should not generate any code for this statement.
*               2) Interrupts are enabled once the task start because the I-bit of the CCR register was
*                  set to 0 by 'OSTaskCreate()'.
*********************************************************************************************************
*/

void  AppStartTask (void *p_arg)
{
    p_arg = p_arg;

#if 0
    BSP_Init();                                  /* For embedded targets, initialize BSP functions                             */
#endif


#if OS_TASK_STAT_EN > 0
    OSStatInit();                                /* Determine CPU capacity                                                     */
#endif
    
    while (TRUE)                                 /* Task body, always written as an infinite loop.                             */
	{       		
		OS_Printf("Delay 1 second and print\n");  /* your code here. Create more tasks, etc.                                    */
        OSTimeDlyHMSM(0, 0, 1, 0);       
    }
}
void  AppStartTask1 (void *p_arg)
{
    p_arg = p_arg;

#if 0
    BSP_Init();                                  /* For embedded targets, initialize BSP functions                             */
#endif


#if OS_TASK_STAT_EN > 0
    OSStatInit();                                /* Determine CPU capacity                                                     */
#endif
    
    while (TRUE)                                 /* Task body, always written as an infinite loop.                             */
	{       		
		OS_Printf("Delay 2 second and print\n");  /* your code here. Create more tasks, etc.                                    */
        OSTimeDlyHMSM(0, 0, 1, 0);       
    }
}


void ethernetif_init(struct netif *netif);
int init_adapter(int adapter_num,unsigned char * ethaddr_ptr);
void shutdown_adapter(void);
void update_adapter(void);

struct netif netif;

int dbg_printf(const char *fmt, ...)
{
	va_list v;
	int r;

	va_start(v, fmt);
	r = vfprintf(stderr,fmt, v);
	va_end(v);
	return r;
}

static err_t netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
	if (err == ERR_OK && p != NULL)
	{
		tcp_recved(pcb, p->tot_len);
		pbuf_free(p);
	}
	else
		pbuf_free(p);

	if (err == ERR_OK && p == NULL)
	{
		tcp_arg(pcb, NULL);
		tcp_sent(pcb, NULL);
		tcp_recv(pcb, NULL);
		tcp_close(pcb);
	}

	return ERR_OK;
}

static err_t netio_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
	tcp_arg(pcb, NULL);
	tcp_sent(pcb, NULL);
	tcp_recv(pcb, netio_recv);
	return ERR_OK;
}

void netio_init(void)
{
	struct tcp_pcb *pcb;

	pcb = tcp_new();
	tcp_bind(pcb, IP_ADDR_ANY, 18767);
	pcb = tcp_listen(pcb);
	tcp_accept(pcb, netio_accept);
}

void lwip_init_task(void * pParam)
{
  struct ip_addr ipaddr, netmask, gw;
  sys_sem_t sem;
  	int last_time;
	int timer1;
	int timer2;
	int done;
//yangye 2003-1-23
//move hardware init here!
 #ifdef PERF
	perf_init("/tmp/lwip.perf");
#endif /* PERF */
#ifdef STATS
	stats_init();
#endif /* STATS */
	sys_init();
	mem_init();
	memp_init();
	pbuf_init();


	
	IP4_ADDR(&gw, 192,168,0,1);
	IP4_ADDR(&ipaddr, 192,168,0,200);
	IP4_ADDR(&netmask, 255,255,255,0);
	
	if (init_adapter(0,&netif.hwaddr) != 0)
		return;

	netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init,
		ip_input));
	netif_set_up(&netif);
 

	tcp_init();
	udp_init();
	ip_init();

	httpd_init();
	netio_init();
	//ftpd_init();

	last_time=clock();
	timer1=0;
	timer2=0;
	done=0;
	
  printf("Applications started.\n");
  
//we can add tcpip application here !
//yangye 2003-1-27
//notice :must use sys_thread_new() to add lwip tasks !!!
//OSTaskCreate(udpecho_thread, (void *)0x1111, &LwipStk1[TASK_STK_SIZE - 1], 6);
//  httpd_init();
 

  /* Block for ever. */
  while (!done)
	{
		int cur_time;
		int time_diff;
 OSTimeDly(20);
	/*	cur_time=clock();
		time_diff=cur_time-last_time;
		if (time_diff>0)
		{
			last_time=cur_time;
			timer1+=time_diff;
			timer2+=time_diff;
		}

		if (timer1>10)
		{
			tcp_fasttmr();
			timer1=0;
		}

		if (timer2>45)
		{
			tcp_slowtmr();
			timer2=0;
		//	done=_kbhit();
		}
*/
		update_adapter();
	}
  	shutdown_adapter();
  printf(" never goes here, should not appear!");
}


void main_loop()
{
	struct ip_addr ipaddr, netmask, gw;
	int last_time;
	int timer1;
	int timer2;
	int done;
	
	IP4_ADDR(&gw, 192,168,0,1);
	IP4_ADDR(&ipaddr, 192,168,0,200);
	IP4_ADDR(&netmask, 255,255,255,0);
	
	if (init_adapter(0,&netif.hwaddr) != 0)
		return;

	netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init,
		ip_input));
	netif_set_up(&netif);
 

	tcp_init();
	udp_init();
	ip_init();

	httpd_init();
	netio_init();
	//ftpd_init();

	last_time=clock();
	timer1=0;
	timer2=0;
	done=0;
	
	while (!done)
	{
		int cur_time;
		int time_diff;

		cur_time=clock();
		time_diff=cur_time-last_time;
		if (time_diff>0)
		{
			last_time=cur_time;
			timer1+=time_diff;
			timer2+=time_diff;
		}

		if (timer1>10)
		{
			tcp_fasttmr();
			timer1=0;
		}

		if (timer2>45)
		{
			tcp_slowtmr();
			timer2=0;
			done=_kbhit();
		}

		update_adapter();
	}

	shutdown_adapter();
}
 
void bcopy(const void *src, void *dest, int len)
{
  memcpy(dest,src,len);
}

void bzero(void *data, int n)
{
  memset(data,0,n);
}

int main_task(void)
{ 
	setvbuf(stdout,NULL,_IONBF,0);
#ifdef PERF
	perf_init("/tmp/lwip.perf");
#endif  
#ifdef STATS
	stats_init();
#endif 
	sys_init();
	mem_init();
	memp_init();
	pbuf_init();

	//tcpdump_init();

	printf("System initialized.\n");

	main_loop();
 
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -