c - STM32F103如何使用ENC28j60

标签 c embedded arm ethernet stm32

我正在研究 stm32f103,我想用 enc28j60 传输数据,但我不知道如何。我所有的示例代码都使用了 http,但我想通过以太网(enc28j60)在 pc 和 stm32 之间进行简单的传输。哪个功能有用?

我的代码:

 int main(void)
{
SystemInit();               
NVIC_Configuration();       
SPInet_Init();
simple_server();
return 0;  
}
int simple_server(void)
{
u16 plen;
u16 dat_p;
u8 i = 0;
u8 cmd_pos = 0;
u8 cmd;
u8 payloadlen = 0;
char str[30];
char cmdval;


 // Del_1ms(100);
 /*initialize enc28j60*/
 enc28j60Init(mymac);

 str[0]=(char)enc28j60getrev(); //IDºÅ

 init_ip_arp_udp_tcp(mymac, myip, mywwwport);   //½«MAC IP wwwportµØÖ·×°µ½Ö¸¶¨µØ
 //ָʾµÆ״̬:0x476 is PHLCON LEDA(ÂÌ)=links status, LEDB(ºì)=receive/transmit
 //   enc28j60PhyWrite(PHLCON, 0x7a4); 
 enc28j60PhyWrite(PHLCON, 0x476);
 enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
 // Del_1ms(20);

 //init the ethernet/ip layer:
 while (1)
 {
    // get the next new packet:
    plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
    //USART_DMASendData(USART1,buf,plen);

    /*plen will be unequal to zero if there is a valid packet (without crc error) */
    if (plen == 0)
    {
       continue;
    }
    // arp is broadcast if unknown but a host may also
    // verify the mac address by sending it to 
    // a unicast address.
    if (eth_type_is_arp_and_my_ip(buf, plen))
    {
       make_arp_answer_from_request(buf);
       //USART_DMASendText(USART1,"make_arp_answer_from_request\n");
       continue;
    }

    // check if ip packets are for us:
    if (eth_type_is_ip_and_my_ip(buf, plen) == 0)
    {
       continue;
    }


    if (buf[IP_PROTO_P] == IP_PROTO_ICMP_V &&
       buf[ICMP_TYPE_P] == ICMP_TYPE_ECHOREQUEST_V)
    {
       // a ping packet, let's send pong    
       make_echo_reply_from_request(buf, plen);
       //USART_DMASendText(USART1,"make_echo_reply_from_request\n");
       continue;
    }
    /***********************************************************************************************************/
    // tcp port www start, compare only the lower byte
    if (buf[IP_PROTO_P] == IP_PROTO_TCP_V &&
       buf[TCP_DST_PORT_H_P] == 0 &&
       buf[TCP_DST_PORT_L_P] == mywwwport)
    {
       if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V)
       {
          make_tcp_synack_from_syn(buf);
          // make_tcp_synack_from_syn does already send the syn,ack
          continue;
       }
     if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V)
     {
        init_len_info(buf); // init some data structures
        // we can possibly have no data, just ack:
        dat_p = get_tcp_data_pointer();
        if (dat_p == 0)
        {
           if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V)
           {
              // finack, answer with ack
              make_tcp_ack_from_any(buf);
           }
           // just an ack with no data, wait for next packet
           continue;
        }
        if (strncmp("GET ", (char *) &(buf[dat_p]), 4) != 0)
        {
           // head, post and other methods:
           //
           // for possible status codes see:
           // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
           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>"));
           goto SENDTCP;
        }
        if (strncmp("/ ", (char *) &(buf[dat_p + 4]), 2) == 0)
        {
           plen = fill_tcp_data_p(buf, 0, PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
           plen = fill_tcp_data_p(buf, plen, PSTR("<p>Usage: "));
           plen = fill_tcp_data(buf, plen, baseurl);
           plen = fill_tcp_data_p(buf, plen, PSTR("password</p>"));
           plen = fill_tcp_data_p(buf, plen, PSTR("ÍøÂçµØÖ·Ó¦¸ÃÊÇhttp://192.168.1.100/888</p>"));
           goto SENDTCP;
        }
        cmd = analyse_get_url((char *) &(buf[dat_p + 5]));
        // for possible status codes see:

        if (cmd ==(u8)-1)
        {
           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>"));
           goto SENDTCP;
        }
        if (cmd == 1)
        {
            LED1 = LED2 = LED3 =1;
           i = 1;
        }
        if (cmd == 0)
        {
            LED1 = LED2 = LED3 =0;
           i = 0;
        }
        // if (cmd==-2) or any other value
        // just display the status:
          plen = print_webpage(buf, (i));

  SENDTCP : 
           make_tcp_ack_from_any(buf); // send ack for http get
           make_tcp_ack_with_data(buf, plen); // send data
           continue;
        }
      }
 /***********************************************************************************************************/
    // tcp port www end
    //
    // udp start, we listen on udp port 1200=0x4B0
    if (buf[IP_PROTO_P] == IP_PROTO_UDP_V &&
       buf[UDP_DST_PORT_H_P] == 4 &&
       buf[UDP_DST_PORT_L_P] == 0xb0)
    {
       payloadlen = buf[UDP_LEN_L_P] - UDP_HEADER_LEN;
       // you must sent a string starting with v
       // e.g udpcom version 10.0.0.24
       if (verify_password((char *) &(buf[UDP_DATA_P])))
       {
          // find the first comma which indicates 
          // the start of a command:
          cmd_pos = 0;
          while (cmd_pos < payloadlen)
          {
             cmd_pos++;
             if (buf[UDP_DATA_P + cmd_pos] == ',')
             {
                cmd_pos++; // put on start of cmd
                break;
             }
          }
          // a command is one char and a value. At
          // least 3 characters long. It has an '=' on
          // position 2:
          if (cmd_pos<2 ||
             cmd_pos>payloadlen - 3 ||
             buf[UDP_DATA_P + cmd_pos + 1] != '=')
          {
             strcpy(str, "e=no_cmd");
             goto ANSWER;
          }
          // supported commands are
          // t=1 t=0 t=?
          if (buf[UDP_DATA_P + cmd_pos] == 't')
          {
             cmdval = buf[UDP_DATA_P + cmd_pos + 2];
             if (cmdval == '1')
             {
                //PORTD|= (1<<PD7);// transistor on
                //IOCLR |= (1<<26);
                //LED1ON();
                strcpy(str, "t=1");
                goto ANSWER;
             }
             else if (cmdval == '0')
             {
                //PORTD &= ~(1<<PD7);// transistor off
                //IOSET |= (1<<26);
                //LED1OFF();
              strcpy(str, "t=0");
              goto ANSWER;
           }
           else if (cmdval == '?')
           {
              /*
                                 if (IOPIN & (1<<26))
                                   {
                                      strcpy(str,"t=1");
                                      goto ANSWER;
                                   }
              */
              strcpy(str, "t=0");
              goto ANSWER;
           }
        }
        strcpy(str, "e=no_such_cmd");
        goto ANSWER;
     }
     strcpy(str, "e=invalid_pw");
     ANSWER : make_udp_reply_from_request(buf, str, strlen(str), myudpport);
    }
 }
   //        return (0);
}

最佳答案

试试 UIP

uip-uip-1-0\apps\hello-world 是一个 tcp 服务器演示

和 可以在 uip 帮助文档中找到 tcp 客户端演示

关于c - STM32F103如何使用ENC28j60,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26955544/

相关文章:

c - 如果使用这个程序,为什么第一个字母不进入数组?

c - 了解 GHS 链接器文件

无法在腻子中正确显示十六进制值

windows-phone - 为 Windows Phone 上传仅 ARM 版本是否安全?

assembly - Cortex-M0+ 链接器脚本和启动代码

exception - ARM 异步外部中止

php - 从 PHP 打包 C 结构

c - 使用指针访问 typedef 结构

c++ - 数字左边的加法

c - 由于我是嵌入式编程的新手,所以我无法理解 "%s\xC2\xB0"是什么?我知道 %s 是字符串