圣源电子制作

 找回密码
 立即注册
查看: 14259|回复: 1
打印 上一主题 下一主题

ENC28J60 网络模块-单片机网络模块-原理图-C语言-C51-AVR-STM32-LPC程序

[复制链接]
跳转到指定楼层
楼主
发表于 2011-10-31 21:56:00 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式





原理图



提供的程序  C51  AVR  LPC  STM32

C51程序
  1. #include "uip.h"
  2. #include "uip_arp.h"
  3. //#include "rtl8019as.h"
  4. #include "httpd.h"
  5. //#include "telnet.h"
  6. #include "mcu_uart.h"
  7. #include "enc28j60.h"

  8. #define BUF ((struct uip_eth_hdr *)&uip_buf[0])

  9. #ifndef NULL
  10. #define NULL (void *)0
  11. #endif /* NULL */

  12. /*-----------------------------------------------------------------------------------*/
  13. int
  14. main(void)
  15. {
  16.         idata u8_t i, arptimer;
  17.         idata u16_t j;
  18.         init_uart();
  19.         printu("starting......\r\n");
  20.         /* Initialize the device driver. */
  21.         //  rtl8019as_init();
  22.         dev_init();
  23.         uip_arp_init();
  24.         /* Initialize the uIP TCP/IP stack. */
  25.         uip_init();
  26.         printu("11111111111111111111111\r\n");
  27.         /* Initialize the HTTP server. */
  28.         httpd_init();
  29.        
  30.         arptimer = 0;
  31.         printu("222222222222222222222222222\r\n");
  32.   while(1) {
  33.     /* Let the tapdev network device driver read an entire IP packet
  34.        into the uip_buf. If it must wait for more than 0.5 seconds, it
  35.        will return with the return value 0. If so, we know that it is
  36.        time to call upon the uip_periodic(). Otherwise, the tapdev has
  37.        received an IP packet that is to be processed by uIP. */
  38.     uip_len = dev_poll();
  39.         for(j=0;j<500;j++);
  40. /*
  41.         if(uip_len > 0)
  42.         {
  43.                 printuf("--------------- uip_len = 0x%x", uip_len);
  44.                 printuf("%x ----------\r\n", uip_len);
  45.                 for(i=0;i<uip_len;i++)
  46.                 {
  47.                         printuf("%x ", uip_buf[i]);
  48.                         if((i+1)%16==0) printu("\r\n");                       
  49.                 }
  50.                 printu("\r\n");                       
  51.         }
  52. */
  53.     if(uip_len == 0) {
  54.       for(i = 0; i < UIP_CONNS; i++) {
  55.         uip_periodic(i);
  56.         /* If the above function invocation resulted in data that
  57.            should be sent out on the network, the global variable
  58.            uip_len is set to a value > 0. */
  59.         if(uip_len > 0) {
  60.           uip_arp_out();
  61.           dev_send();
  62.         }
  63.       }

  64. #if UIP_UDP
  65.       for(i = 0; i < UIP_UDP_CONNS; i++) {
  66.         uip_udp_periodic(i);
  67.         /* If the above function invocation resulted in data that
  68.            should be sent out on the network, the global variable
  69.            uip_len is set to a value > 0. */
  70.         if(uip_len > 0) {
  71.           uip_arp_out();
  72.           dev_send();
  73.         }
  74.       }
  75. #endif /* UIP_UDP */
  76.       
  77.       /* Call the ARP timer function every 10 seconds. */
  78.       if(++arptimer == 20) {       
  79.         uip_arp_timer();
  80.         arptimer = 0;
  81.       }
  82.       
  83.     } else {
  84.       if(BUF->type == htons(UIP_ETHTYPE_IP)) {
  85.         uip_arp_ipin();
  86.         uip_input();
  87.         /* If the above function invocation resulted in data that
  88.            should be sent out on the network, the global variable
  89.            uip_len is set to a value > 0. */
  90.         if(uip_len > 0) {
  91.           uip_arp_out();
  92.           dev_send();
  93.         }
  94.       } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
  95.         uip_arp_arpin();
  96.         /* If the above function invocation resulted in data that
  97.            should be sent out on the network, the global variable
  98.            uip_len is set to a value > 0. */       
  99.         if(uip_len > 0) {       
  100.           dev_send();
  101.         }
  102.       }
  103.     }
  104.    
  105.   }
  106.   return 0;
  107. }
  108. /*-----------------------------------------------------------------------------------*/
  109. void
  110. uip_log(char *m)
  111. {
  112. //  printf("uIP log message: %s\n", m);
  113. }
  114. /*-----------------------------------------------------------------------------------*/

复制代码

AVR程序
  1. /*********************************************
  2. * vim:sw=8:ts=8:si:et
  3. * To use the above modeline in vim you must have "set modeline" in your .vimrc
  4. * Author: Guido Socher
  5. * Copyright: GPL V2
  6. * See http://www.gnu.org/licenses/gpl.html
  7. *
  8. * Ethernet remote device and sensor
  9. * UDP and HTTP interface
  10.         url looks like this http://baseurl/password/command
  11.         or http://baseurl/password/
  12. *
  13. * Chip type           : Atmega88 or Atmega168 with ENC28J60
  14. * Note: there is a version number in the text. Search for tuxgraphics
  15. *********************************************/
  16. #include <avr/io.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <avr/pgmspace.h>
  20. #include "ip_arp_udp_tcp.h"
  21. #include "enc28j60.h"
  22. #include "timeout.h"
  23. #include "avr_compat.h"
  24. #include "net.h"

  25. // MAC 地址
  26. static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
  27. // IP 地址
  28. static uint8_t myip[4] = {192,168,1,88};
  29. // TCP WWW 服务监听端口
  30. #define MYWWWPORT 80
  31. // UDP 端口
  32. #define MYUDPPORT 1200

  33. #define BUFFER_SIZE 450
  34. static uint8_t buf[BUFFER_SIZE+1];


  35. // 下面是一个简单的密码验证
  36. // 字符串 password 中存放密码
  37. // 密码只能是 a-z,0-9,下划线,不大于9个字节,仅验证前5个字节
  38. static char password[]="lcsoft"; // must not be longer than 9 char

  39. // 验证密码
  40. uint8_t verify_password(char *str)
  41. {
  42.         // the first characters of the received string are
  43.         // a simple password/cookie:
  44.         if (strncmp(password,str,5)==0){
  45.                 return(1);
  46.         }
  47.         return(0);
  48. }

  49. // takes a string of the form password/commandNumber and analyse it
  50. // return values: -1 invalid password, otherwise command number
  51. //                -2 no command given but password valid
  52. //                -3 valid password, no command and no trailing "/"
  53. int8_t analyse_get_url(char *str)
  54. {
  55.         uint8_t loop=1;
  56.         uint8_t i=0;
  57.         while(loop){
  58.                 if(password[i]){
  59.                         if(*str==password[i]){
  60.                                 str++;
  61.                                 i++;
  62.                         }else{
  63.                                 return(-1);
  64.                         }
  65.                 }else{
  66.                         // end of password
  67.                         loop=0;
  68.                 }
  69.         }
  70.         // is is now one char after the password
  71.         if (*str == '/'){
  72.                 str++;
  73.         }else{
  74.                 return(-3);
  75.         }
  76.         // check the first char, garbage after this is ignored (including a slash)
  77.         if (*str < 0x3a && *str > 0x2f){
  78.                 // is a ASCII number, return it
  79.                 return(*str-0x30);
  80.         }
  81.         return(-2);
  82. }

  83. // answer HTTP/1.0 301 Moved Permanently\r\nLocation: password/\r\n\r\n
  84. // to redirect to the url ending in a slash
  85. uint16_t moved_perm(uint8_t *buf)
  86. {
  87.         uint16_t plen;
  88.         plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 301 Moved Permanently\r\nLocation: "));
  89.         plen=fill_tcp_data(buf,plen,password);
  90.         plen=fill_tcp_data_p(buf,plen,PSTR("/\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"));
  91.         plen=fill_tcp_data_p(buf,plen,PSTR("<h1>301 Moved Permanently</h1>\n"));
  92.         plen=fill_tcp_data_p(buf,plen,PSTR("add a trailing slash to the url\n"));
  93.         return(plen);
  94. }


  95. // 填充 Web Page
  96. uint16_t print_webpage(uint8_t *buf,uint8_t on_off)
  97. {
  98.         uint16_t plen;
  99.         plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"));
  100.         plen=fill_tcp_data_p(buf,plen,PSTR("<h1 align=center>LCSOT测试页面</h1><hr>"));
  101.                                 plen=fill_tcp_data_p(buf,plen,PSTR("<center><p>当前状态: "));
  102.         if (on_off){
  103.                 plen=fill_tcp_data_p(buf,plen,PSTR("<font color="#00FF00"> 打开</font>"));
  104.         }else{
  105.                 plen=fill_tcp_data_p(buf,plen,PSTR("关闭"));
  106.         }
  107.         plen=fill_tcp_data_p(buf,plen,PSTR(" <small><a href=".">[刷新]</a></small></p>\n<p><a href="."));
  108.         if (on_off){
  109.                 plen=fill_tcp_data_p(buf,plen,PSTR("/0">关闭</a><p>"));
  110.         }else{
  111.                 plen=fill_tcp_data_p(buf,plen,PSTR("/1">打开</a><p>"));
  112.         }
  113.         plen=fill_tcp_data_p(buf,plen,PSTR("</center><hr><p align=center>详细信息请访问:<a href="http://www.lcsoft.net">http://www.lcsoft.net</a></p>"));

  114.         return(plen);
  115. }


  116. int main(void){

  117.         uint16_t plen;
  118.         uint16_t dat_p;
  119.         uint8_t i=0;
  120.         uint8_t cmd_pos=0;
  121.         int8_t cmd;
  122.         uint8_t payloadlen=0;
  123.         char str[30];
  124.         char cmdval;
  125.         
  126.         delay_ms(1);

  127.         /* enable PD2/INT0, as input */
  128.         DDRD&= ~(1<<DDD2);

  129.         /*initialize enc28j60*/
  130.         enc28j60Init(mymac);
  131.         enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
  132.         delay_ms(10);
  133.         
  134.         // LED
  135.         /* enable PB1, LED as output */
  136.         DDRC|= (1<<DDC0);

  137.         /* set output to Vcc, LED off */
  138.         PORTC|= (1<<PC0);

  139.         // the transistor on PD7
  140.         DDRD|= (1<<DDD7);
  141.         PORTD &= ~(1<<PD7);// transistor off
  142.         
  143.         /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
  144.         // LEDB=yellow LEDA=green
  145.         //
  146.         // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
  147.         // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
  148.         enc28j60PhyWrite(PHLCON,0x476);
  149.         delay_ms(20);
  150.         
  151.         /* set output to GND, red LED on */
  152.         PORTC &= ~(1<<PC0);
  153.         i=1;

  154.         //init the ethernet/ip layer:
  155.         init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);

  156.         while(1){
  157.                 // get the next new packet:
  158.                 plen = enc28j60PacketReceive(BUFFER_SIZE, buf);

  159.                 /*plen will ne unequal to zero if there is a valid
  160.                  * packet (without crc error) */
  161.                 if(plen==0){
  162.                         continue;
  163.                 }
  164.                         
  165.                 // arp is broadcast if unknown but a host may also
  166.                 // verify the mac address by sending it to
  167.                 // a unicast address.
  168.                 if(eth_type_is_arp_and_my_ip(buf,plen)){
  169.                         make_arp_answer_from_request(buf);
  170.                         continue;
  171.                 }

  172.                 // check if ip packets are for us:
  173.                 if(eth_type_is_ip_and_my_ip(buf,plen)==0){
  174.                         continue;
  175.                 }
  176.                 // led----------
  177.                 if (i){
  178.                         /* set output to Vcc, LED off */
  179.                         PORTC|= (1<<PC0);
  180.                         i=0;
  181.                 }else{
  182.                         /* set output to GND, LED on */
  183.                         PORTC &= ~(1<<PC0);
  184.                         i=1;
  185.                 }
  186.                
  187.                 if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
  188.                         // a ping packet, let's send pong
  189.                         make_echo_reply_from_request(buf,plen);
  190.                         continue;
  191.                 }
  192.                 // tcp port www start, compare only the lower byte
  193.                 if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==MYWWWPORT){
  194.                         if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
  195.                                 make_tcp_synack_from_syn(buf);
  196.                                 // make_tcp_synack_from_syn does already send the syn,ack
  197.                                 continue;
  198.                         }
  199.                         if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
  200.                                 init_len_info(buf); // init some data structures
  201.                                 // we can possibly have no data, just ack:
  202.                                 dat_p=get_tcp_data_pointer();
  203.                                 if (dat_p==0){
  204.                                         if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
  205.                                                 // finack, answer with ack
  206.                                                 make_tcp_ack_from_any(buf);
  207.                                         }
  208.                                         // just an ack with no data, wait for next packet
  209.                                         continue;
  210.                                 }
  211.                                 if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
  212.                                         // head, post and other methods:
  213.                                         //
  214.                                         // for possible status codes see:
  215.                                         // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  216.                                         plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
  217.                                         goto SENDTCP;
  218.                                 }
  219.                                 if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
  220.                                         plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
  221.                                         plen=fill_tcp_data_p(buf,plen,PSTR("<p>Usage: http://host_or_ip/password</p>\n"));
  222.                                         goto SENDTCP;
  223.                                 }
  224.                                 cmd=analyse_get_url((char *)&(buf[dat_p+5]));
  225.                                 // for possible status codes see:
  226.                                 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
  227.                                 if (cmd==-1){
  228.                                         plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
  229.                                         goto SENDTCP;
  230.                                 }
  231.                                 if (cmd==1){
  232.                                         PORTD|= (1<<PD7);// transistor on
  233.                                 }
  234.                                 if (cmd==0){
  235.                                         PORTD &= ~(1<<PD7);// transistor off
  236.                                 }
  237.                                 if (cmd==-3){
  238.                                         // redirect to add a trailing slash
  239.                                         plen=moved_perm(buf);
  240.                                         goto SENDTCP;
  241.                                 }
  242.                                 // if (cmd==-2) or any other value
  243.                                 // just display the status:
  244.                                 plen=print_webpage(buf,(PORTD & (1<<PD7)));
  245.                                 //
  246. SENDTCP:
  247.                                 make_tcp_ack_from_any(buf); // send ack for http get
  248.                                 make_tcp_ack_with_data(buf,plen); // send data
  249.                                 continue;
  250.                         }

  251.                 }
  252.                 // TCP WWW 部分结束

  253.                 // UDP 处理,监听端口 1200 = 0x4B0
  254.                 if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==4&&buf[UDP_DST_PORT_L_P]==0xb0){
  255.                         payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
  256.                         // you must sent a string starting with v
  257.                         // e.g udpcom version 10.0.0.24
  258.                         if (verify_password((char *)&(buf[UDP_DATA_P]))){
  259.                                 // find the first comma which indicates
  260.                                 // the start of a command:
  261.                                 cmd_pos=0;
  262.                                 while(cmd_pos<payloadlen){
  263.                                         cmd_pos++;
  264.                                         if (buf[UDP_DATA_P+cmd_pos]==','){
  265.                                                 cmd_pos++; // put on start of cmd
  266.                                                 break;
  267.                                         }
  268.                                 }
  269.                                 // a command is one char and a value. At
  270.                                 // least 3 characters long. It has an '=' on
  271.                                 // position 2:
  272.                                 if (cmd_pos<2 || cmd_pos>payloadlen-3 || buf[UDP_DATA_P+cmd_pos+1]!='='){
  273.                                         strcpy(str,"e=no_cmd");
  274.                                         goto ANSWER;
  275.                                 }
  276.                                 // supported commands are
  277.                                 // t=1 t=0 t=?
  278.                                 if (buf[UDP_DATA_P+cmd_pos]=='t'){
  279.                                         cmdval=buf[UDP_DATA_P+cmd_pos+2];
  280.                                         if(cmdval=='1'){
  281.                                                 PORTD|= (1<<PD7);// transistor on
  282.                                                 strcpy(str,"t=1");
  283.                                                 goto ANSWER;
  284.                                         }else if(cmdval=='0'){
  285.                                                 PORTD &= ~(1<<PD7);// transistor off
  286.                                                 strcpy(str,"t=0");
  287.                                                 goto ANSWER;
  288.                                         }else if(cmdval=='?'){
  289.                                                 if (PORTD & (1<<PD7)){
  290.                                                         strcpy(str,"t=1");
  291.                                                         goto ANSWER;
  292.                                                 }
  293.                                                 strcpy(str,"t=0");
  294.                                                 goto ANSWER;
  295.                                         }
  296.                                 }
  297.                                 strcpy(str,"e=no_such_cmd");
  298.                                 goto ANSWER;
  299.                         }
  300.                         strcpy(str,"e=invalid_pw");
  301. ANSWER:
  302.                         make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT);
  303.                 }
  304.         }
  305.         return (0);
  306. }
复制代码
LPC C程序
  1. /*
  2. * LPC2103开发板测试程序
  3. *
  4. * 用途:5110液晶+ENC28J60网络模块测试程序
  5. *
  6. */

  7. #include <stdio.h>
  8. #include <LPC2103.h>
  9. #include <string.h>

  10. #include "ip_arp_udp_tcp.h"
  11. #include "enc28j60.h"
  12. #include "net.h"
  13. #include "PCD5544.h"

  14. #define LED  ( 1 << 17 )        //P0.17控制LED

  15. #define PSTR(s) s
  16. extern void delay_ms(unsigned char ms);

  17. static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
  18. static uint8_t myip[4] = {192,168,1,88};

  19. #define MYWWWPORT 80
  20. #define MYUDPPORT 1200
  21. #define BUFFER_SIZE 1500
  22. static uint8_t buf[BUFFER_SIZE+1];

  23. static char password[]="lcsoft";

  24. uint8_t verify_password(char *str)
  25. {
  26.     if (strncmp(password,str,5)==0){
  27.             return(1);
  28.     }
  29.     return(0);
  30. }

  31. int8_t analyse_get_url(char *str)
  32. {
  33.     uint8_t loop=1;
  34.     uint8_t i=0;
  35.     while(loop){
  36.         if(password[i]){
  37.             if(*str==password[i]){
  38.                 str++;
  39.                 i++;
  40.             }else{
  41.                 return(-1);
  42.             }
  43.         }else{
  44.             loop=0;
  45.         }
  46.     }
  47.     if (*str == '/'){
  48.         str++;
  49.     }else{
  50.         return(-3);
  51.     }

  52.     if (*str < 0x3a && *str > 0x2f){
  53.         return(*str-0x30);
  54.     }
  55.     return(-2);
  56. }

  57. uint16_t moved_perm(uint8_t *buf)
  58. {
  59.     uint16_t plen;
  60.     plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 301 Moved Permanently\r\nLocation: "));
  61.     plen=fill_tcp_data(buf,plen,password);
  62.     plen=fill_tcp_data_p(buf,plen,PSTR("/\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"));
  63.     plen=fill_tcp_data_p(buf,plen,PSTR("<h1>301 Moved Permanently</h1>\n"));
  64.     plen=fill_tcp_data_p(buf,plen,PSTR("add a trailing slash to the url\n"));
  65.     return(plen);
  66. }

  67. uint16_t print_webpage(uint8_t *buf,uint8_t on_off)
  68. {
  69.     uint16_t plen;
  70.     plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"));
  71.         plen=fill_tcp_data_p(buf,plen,PSTR("<h1 align=center>LCSOFT测试页面</h1><hr>"));
  72.     plen=fill_tcp_data_p(buf,plen,PSTR("<center><p>当前状态: "));
  73.     if (on_off){
  74.         plen=fill_tcp_data_p(buf,plen,PSTR("<font color="#00FF00">开启</font>"));
  75.     }else{
  76.         plen=fill_tcp_data_p(buf,plen,PSTR("关闭"));
  77.     }
  78.     plen=fill_tcp_data_p(buf,plen,PSTR(" <small><a href=".">[刷新]</a></small></p>\n<p><a href="."));
  79.     if (on_off){
  80.         plen=fill_tcp_data_p(buf,plen,PSTR("/0">关闭</a><p>"));
  81.     }else{
  82.         plen=fill_tcp_data_p(buf,plen,PSTR("/1">开启</a><p>"));
  83.     }
  84.     plen=fill_tcp_data_p(buf,plen,PSTR("</center><hr><br><p align=center>更多信息请访问 <a href="http://www.lcsoft.net" target=blank>http://www.lcsoft.net\n"));
  85.     return(plen);
  86. }

  87. int main()
  88. {
  89.     uint16_t plen;
  90.     uint16_t dat_p;
  91.     uint8_t i=0;
  92.     uint8_t cmd_pos=0;
  93.     int8_t cmd;
  94.     uint8_t payloadlen=0;
  95.     char str[30];
  96.     char cmdval;
  97.    
  98.     enc28j60Init(mymac);
  99.     enc28j60clkout(2);
  100.     delay_ms(10);     

  101.         IODIR |= LED;
  102.         IOSET = LED;

  103.     enc28j60PhyWrite(PHLCON,0xD76);
  104.     delay_ms(20);

  105.         IOCLR = LED;
  106.     i=1;

  107.     init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);

  108.         LCD_Init();
  109.         LCD_Cls();
  110.         LCD_GotoXY(0,0);
  111.         LCD_PrintStr("5110+ENC28J60");

  112.         LCD_GotoXY(0,1);
  113.         LCD_PrintStr("Lcsoft(C) 2010");
  114.        
  115.         LCD_GotoXY(0,2);
  116.         LCD_PrintStr("--------------");

  117.         LCD_GotoXY(0,5);
  118.         LCD_PrintStr("--------------");

  119.         for(;;)
  120.         {
  121.         plen = enc28j60PacketReceive(BUFFER_SIZE, buf);

  122.         if(plen==0){
  123.             continue;
  124.         }

  125.         if(eth_type_is_arp_and_my_ip(buf,plen)){
  126.                         static int icmpcount = 0;

  127.                         char sip[15];
  128.                         char sicmp[14];

  129.                         sprintf(sip,"%u.%u.%u.%u",buf[ETH_ARP_SRC_IP_P],buf[ETH_ARP_SRC_IP_P+1],buf[ETH_ARP_SRC_IP_P+2],buf[ETH_ARP_SRC_IP_P+3]);
  130.                         LCD_GotoXY(0,3);
  131.                         LCD_PrintStr((unsigned char*)sip);

  132.                         sprintf(sicmp,"Packets:%d",++icmpcount);
  133.                         LCD_GotoXY(0,4);
  134.                         LCD_PrintStr((unsigned char*)sicmp);

  135.             make_arp_answer_from_request(buf);

  136.             continue;
  137.         }

  138.         if(eth_type_is_ip_and_my_ip(buf,plen)==0){
  139.             continue;
  140.         }

  141.         if (i){
  142.                         IOSET = LED;
  143.             i=0;
  144.         }else{
  145.                         IOCLR = LED;
  146.             i=1;
  147.         }
  148.         
  149.         if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){
  150.                         static int icmpcount = 0;

  151.                         char sip[15];
  152.                         char sicmp[14];

  153.                         sprintf(sip,"%u.%u.%u.%u",buf[IP_SRC_P],buf[IP_SRC_P+1],buf[IP_SRC_P+2],buf[IP_SRC_P+3]);
  154.                         LCD_GotoXY(0,3);
  155.                         LCD_PrintStr((unsigned char*)sip);

  156.                         sprintf(sicmp,"Packets:%d",++icmpcount);
  157.                         LCD_GotoXY(0,4);
  158.                         LCD_PrintStr((unsigned char*)sicmp);

  159.             make_echo_reply_from_request(buf,plen);
  160.             continue;
  161.         }

  162.         if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==MYWWWPORT){
  163.             if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){
  164.                 make_tcp_synack_from_syn(buf);
  165.                 continue;
  166.             }
  167.             if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){
  168.                 init_len_info(buf);
  169.                 dat_p=get_tcp_data_pointer();
  170.                 if (dat_p==0){
  171.                     if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){
  172.                             make_tcp_ack_from_any(buf);
  173.                     }
  174.                     continue;
  175.                 }
  176.                 if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
  177.                     plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));
  178.                     goto SENDTCP;
  179.                 }
  180.                 if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
  181.                     plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
  182.                     plen=fill_tcp_data_p(buf,plen,PSTR("<p>Usage: http://host_or_ip/password</p>\n"));
  183.                     goto SENDTCP;
  184.                 }
  185.                 cmd=analyse_get_url((char *)&(buf[dat_p+5]));

  186.                 if (cmd==-1){
  187.                     plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
  188.                     goto SENDTCP;
  189.                 }
  190.                 if (cmd==1){
  191.                                         IOCLR = LED;
  192.                                         i = 1;
  193.                                         LCD_GotoXY(0,4);
  194.                                         LCD_PrintStr("STATUS:ON ");
  195.                 }
  196.                 if (cmd==0){
  197.                                         IOSET = LED;
  198.                                         i = 0;
  199.                                         LCD_GotoXY(0,4);
  200.                                         LCD_PrintStr("STATUS:OFF");
  201.                 }
  202.                 if (cmd==-3){
  203.                     plen=moved_perm(buf);
  204.                     goto SENDTCP;
  205.                 }

  206.                                 plen=print_webpage(buf,(i));
  207. SENDTCP:
  208.                 make_tcp_ack_from_any(buf);
  209.                 make_tcp_ack_with_data(buf,plen);
  210.                 continue;
  211.             }
  212.         }

  213.         if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==4&&buf[UDP_DST_PORT_L_P]==0xb0){
  214.             payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
  215.             if (verify_password((char *)&(buf[UDP_DATA_P]))){
  216.                 cmd_pos=0;
  217.                 while(cmd_pos<payloadlen){
  218.                     cmd_pos++;
  219.                     if (buf[UDP_DATA_P+cmd_pos]==','){
  220.                         cmd_pos++;
  221.                         break;
  222.                     }
  223.                 }

  224.                 if (cmd_pos<2 || cmd_pos>payloadlen-3 || buf[UDP_DATA_P+cmd_pos+1]!='='){
  225.                     strcpy(str,"e=no_cmd");
  226.                     goto ANSWER;
  227.                 }

  228.                 if (buf[UDP_DATA_P+cmd_pos]=='t'){
  229.                     cmdval=buf[UDP_DATA_P+cmd_pos+2];
  230.                     if(cmdval=='1'){
  231.                                                 IOCLR = LED;
  232.                         strcpy(str,"t=1");
  233.                         goto ANSWER;
  234.                     }else if(cmdval=='0'){
  235.                                                 IOSET = LED;
  236.                         strcpy(str,"t=0");
  237.                         goto ANSWER;
  238.                     }else if(cmdval=='?'){
  239.                                                 if (IOPIN & LED){
  240.                             strcpy(str,"t=1");
  241.                             goto ANSWER;
  242.                         }
  243.                         strcpy(str,"t=0");
  244.                         goto ANSWER;
  245.                     }
  246.                 }
  247.                 strcpy(str,"e=no_such_cmd");
  248.                 goto ANSWER;
  249.             }
  250.             strcpy(str,"e=invalid_pw");
  251. ANSWER:
  252.             make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT);
  253.         }
  254.         }
  255. }
复制代码

程序:
STM32.zip (474.7 KB, 下载次数: 60)
LPC.zip (152.74 KB, 下载次数: 27)
AVR.zip (68.38 KB, 下载次数: 24)
51.zip (327.35 KB, 下载次数: 65)
原理图:
ENC28J60.pdf (177.96 KB, 下载次数: 690)

相关资料:



相关资料

回复

使用道具 举报

沙发
发表于 2011-11-1 22:46:19 | 只看该作者
51要怎么接电路图?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|联系我们|闽公网安备 35012102000020号|闽ICP备11020110号-1|圣源电子

GMT+8, 2024-9-20 05:31 , Processed in 0.048632 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表