找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1698|回复: 0
打印 上一主题 下一主题
收起左侧

STM32 ENC28J60调试通过的源程序

[复制链接]
跳转到指定楼层
楼主
ID:810398 发表于 2020-8-1 22:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "main.h"

  2. static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
  3. static uint8_t myip[4] = {192,168,0,24};
  4. #define MYWWWPORT 80
  5. #define MYUDPPORT 1200
  6. #define BUFFER_SIZE 550
  7. static volatile ErrorStatus HSEStartUpStatus = SUCCESS;
  8. static vu32 TimingDelay = 0;
  9. static uint8_t buf[BUFFER_SIZE+1];
  10. static char password[]="secret";



  11. /*u16 fill_tcp_data_p(char *buf,u16 pos, u8 *progmem_s)
  12. {
  13.         char c;
  14.         // fill in tcp data at position pos
  15.         //
  16.         // with no options the data starts after the checksum + 2 more bytes (urgent ptr)
  17.         while ((c = *(progmem_s++))) {
  18.                 buf[TCP_CHECKSUM_L_P+3+pos]=c;
  19.                 pos++;
  20.         }
  21.         return(pos);
  22. }*/

  23. char strncmp ( char * s1, char * s2, char n)
  24. {
  25.   if ( !n )//n为无符号整形变量;如果n为0,则返回0
  26.   return(0);
  27.   
  28.   while (--n && *s1 && *s1 == *s2)
  29.   {
  30.     s1++;//S1指针自加1,指向下一个字符
  31.     s2++;//S2指针自加1,指向下一个字符

  32.   }
  33.   return( *s1 - *s2 );//返回比较结果
  34. }

  35. uint8_t verify_password(char *str)
  36. {
  37.         // the first characters of the received string are
  38.         // a simple password/cookie:
  39.         if (strncmp(password,str,5)==0){
  40.                 return(1);
  41.         }
  42.         return(0);
  43. }


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

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


  90. // prepare the webpage by writing the data to the tcp send buffer
  91. uint16_t print_webpage(uint8_t *buf,uint8_t on_off)
  92. {
  93.         uint16_t plen;
  94.         plen=fill_tcp_data_p(buf,0,"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
  95.         plen=fill_tcp_data_p(buf,plen,"<center><p>Output is: ");
  96.         if (on_off){
  97.                 plen=fill_tcp_data_p(buf,plen,"<font color=\"#00FF00\"> ON</font>");
  98.         }else{
  99.                 plen=fill_tcp_data_p(buf,plen,"OFF");
  100.         }
  101.         plen=fill_tcp_data_p(buf,plen," <small><a href=\".\">[refresh status]</a></small></p>\n<p><a href=\".");
  102.         if (on_off){
  103.                 plen=fill_tcp_data_p(buf,plen,"/0\">Switch off</a><p>");
  104.         }else{
  105.                 plen=fill_tcp_data_p(buf,plen,"/1\">Switch on</a><p>");
  106.         }
  107.         plen=fill_tcp_data_p(buf,plen,"</center><hr><br>version 2.17, TangYC\n");
  108.         return(plen);
  109. }

  110. /**
  111.   * @brief  Main program.
  112.   * @param  None
  113.   * @retval : None
  114.   */
  115. int main(void)
  116. {
  117.   uint16_t plen=0;
  118.   uint16_t dat_p;
  119.   int8_t cmd;
  120.   uint8_t payloadlen,cmd_pos;
  121.   char str[30];
  122.   char cmdval;
  123.   
  124.   RCC_Init();
  125.   InterruptConfig();
  126.   SysTick_Config();
  127.   SPI_Config();
  128.   GPIO_Config();
  129.   enc28j60Init(mymac);
  130.   
  131.   
  132.   /* Infinite loop */
  133. //init the ethernet/ip layer:
  134.         init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);

  135.         while(1){
  136.                 // get the next new packet:
  137.                 plen = enc28j60PacketReceive(BUFFER_SIZE, buf);

  138.                 /*plen will ne unequal to zero if there is a valid
  139.                  * packet (without crc error) */
  140.                 if(plen==0){
  141.                         continue;
  142.                 }
  143.                         
  144.                 // arp is broadcast if unknown but a host may also
  145.                 // verify the mac address by sending it to
  146.                 // a unicast address.
  147.                 if(eth_type_is_arp_and_my_ip(buf,plen)){
  148.                         make_arp_answer_from_request(buf);
  149.                         continue;
  150.                 }

  151.                 // check if ip packets are for us:
  152. ……………………

  153. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码


STM32 ENC28J60调试通过.7z

171.69 KB, 下载次数: 33, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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