找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32+W5100S以太网芯片程序与资料

[复制链接]
跳转到指定楼层
楼主
stm控制W5100S的程序


单片机源程序如下:
  1. #include "socket.h"


  2. static uint16 local_port;

  3. extern uint16 sent_ptr;


  4.        
  5. #define __MACRAW__
  6. /**
  7. @brief   This Socket function initialize the channel in perticular mode, and set the port and wait for W5200 done it.
  8. @return  1 for sucess else 0.
  9. */

  10. void setkeepalive(SOCKET s);
  11. uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag)    // 2017-07-17
  12. {
  13.    uint8 ret;
  14.    if (
  15.         ((protocol&0x0F) == Sn_MR_TCP)    ||
  16.         ((protocol&0x0F) == Sn_MR_UDP)    ||
  17.         ((protocol&0x0F) == Sn_MR_IPRAW)  ||
  18.         ((protocol&0x0F) == Sn_MR_MACRAW)
  19.       
  20.       )
  21.    {
  22.       close(s);
  23.                  
  24.                   if((protocol&0x0F)==Sn_MR_TCP)
  25.                          {
  26.          setkeepalive(s);
  27.       }
  28.       IINCHIP_WRITE(W5100S_Sn_MR(s) ,protocol | flag);
  29.       if (port != 0) {
  30.          IINCHIP_WRITE( W5100S_Sn_PORT0(s) ,(uint8)((port & 0xff00) >> 8));
  31.          IINCHIP_WRITE( W5100S_Sn_PORT1(s) ,(uint8)(port & 0x00ff));
  32.       } else {
  33.          local_port++; // if don't set the source port, set local_port number.
  34.          IINCHIP_WRITE(W5100S_Sn_PORT0(s) ,(uint8)((local_port & 0xff00) >> 8));
  35.          IINCHIP_WRITE(W5100S_Sn_PORT1(s) ,(uint8)(local_port & 0x00ff));
  36.       }
  37.       IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_OPEN); // run sockinit Sn_CR

  38.       /* wait to process the command... */
  39.       while( IINCHIP_READ(W5100S_Sn_CR(s)) )
  40.          ;
  41.       /* ------- */
  42.       ret = 1;
  43.    }
  44.    else
  45.    {
  46.       ret = 0;
  47.    }
  48.    return ret;
  49. }


  50. /**
  51. @brief   This function close the socket and parameter is "s" which represent the socket number
  52. */
  53. void close(SOCKET s)
  54. {
  55.    IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_CLOSE);   //SOCKET关闭

  56.    /* wait to process the command... */
  57.    while( IINCHIP_READ(W5100S_Sn_CR(s) ) )
  58.       ;
  59.    /* ------- */
  60.         /* all clear */
  61.    IINCHIP_WRITE( W5100S_Sn_IR(s) , 0xFF);
  62. }


  63. /**
  64. @brief   This function established  the connection for the channel in passive (server) mode. This function waits for the request from the peer.
  65. @return  1 for success else 0.
  66. */
  67. uint8 listen(SOCKET s) //设置为等待客户端发出请求模式
  68. {
  69.    uint8 ret;
  70.    if (IINCHIP_READ( W5100S_Sn_SR(s) ) == Sn_SR_INIT) //指示SOCKET打开并处于TCP模式
  71.    {
  72.       IINCHIP_WRITE(W5100S_Sn_CR(s) ,Sn_CR_LISTEN); //设置为等待客户端发出请求模式
  73.       /* wait to process the command... */
  74.                         while( IINCHIP_READ(W5100S_Sn_CR(s) ) )  //等待设置完成
  75.          ;
  76.       /* ------- */
  77.       ret = 1;
  78.    }
  79.    else
  80.    {
  81.       ret = 0;
  82.    }
  83.    return ret;
  84. }


  85. /**
  86. @brief   This function established  the connection for the channel in Active (client) mode.
  87.       This function waits for the untill the connection is established.

  88. @return  1 for success else 0.
  89. */
  90. uint8 connect(SOCKET s, uint8 * addr, uint16 port)
  91. {
  92.     uint8 ret;                                // ret定义为是否连接的标志位,ret=0  连接中断;ret=1连接成功
  93.     if                                                       
  94.         (
  95.             ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
  96.             ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
  97.             (port == 0x00)
  98.         )
  99.     {
  100.       ret = 0;                                // 如果IP地址和Port无法获取,则连接中断
  101.     }
  102.     else                                                        // 如果目的IP和Port未设置,则进行设置
  103.     {
  104.         ret = 1;
  105.         IINCHIP_WRITE( W5100S_Sn_DIPR0(s), addr[0]);
  106.         IINCHIP_WRITE( W5100S_Sn_DIPR1(s), addr[1]);
  107.         IINCHIP_WRITE( W5100S_Sn_DIPR2(s), addr[2]);
  108.         IINCHIP_WRITE( W5100S_Sn_DIPR3(s), addr[3]);
  109.         IINCHIP_WRITE( W5100S_Sn_DPORT0(s), (uint8)((port & 0xff00) >> 8));
  110.         IINCHIP_WRITE( W5100S_Sn_DPORT1(s), (uint8)(port & 0x00ff));

  111.                                 IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_CONNECT);                                                // Sn_CR数值设为0x04,并执行TCP连接请求命令

  112.         while ( IINCHIP_READ(W5100S_Sn_CR(s) ) ) ;                                                                        // MCU读取Sn_CR(s)的数值

  113.         while ( IINCHIP_READ(W5100S_Sn_SR(s)) != Sn_SR_SYNSEND )                // 此时Sn_SR(s)寄存器应该处于SOCK_SYNSENT,下面排除不在该状态的几种情况
  114.         {
  115.             if(IINCHIP_READ(W5100S_Sn_SR(s)) == Sn_SR_ESTABLISHED)        // Socket连接已经建立,正常连接
  116.             {
  117.                 break;
  118.             }
  119.             if (getSn_IR(s) & Sn_IR_TIMEOUT)                                                                // 当ARPto或TCPto超时,异常
  120.             {
  121.                 IINCHIP_WRITE(W5100S_Sn_IR(s), (Sn_IR_TIMEOUT));   // 通知MCU该中断,并清中断
  122.                 ret = 0;                                                                                                                                                // ret置0,连接中断
  123.                 break;
  124.             }
  125.         }
  126.     }

  127.    return ret;
  128. }



  129. /**
  130. @brief   This function used for disconnect the socket and parameter is "s" which represent the socket number
  131. @return  1 for success else 0.
  132. */
  133. void disconnect(SOCKET s)
  134. {
  135.    IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_DISCON);

  136.    /* wait to process the command... */
  137.    while( IINCHIP_READ(W5100S_Sn_CR(s) ) )
  138.       ;
  139.    /* ------- */
  140. }


  141. /**
  142. @brief   This function used to send the data in TCP mode
  143. @return  1 for success else 0.
  144. */
  145. uint16 send(SOCKET s, const uint8 * buf, uint16 len)
  146. {
  147.   uint8 status=0;
  148.   uint16 ret=0;
  149.   uint16 freesize=0;

  150.   if (len > getSn_TXBUF_SIZE( s)*1024)
  151.                         ret = getSn_TXBUF_SIZE( s)*1024;
  152.   else ret = len;
  153.   do
  154.   {
  155.     freesize = getSn_TX_FSR(s);
  156.     status = IINCHIP_READ(W5100S_Sn_SR(s));
  157.     if ((status != Sn_SR_ESTABLISHED) && (status != Sn_SR_CLOSE_WAIT))
  158.     {
  159.                        
  160.                         printf("status break\r\n");
  161.       ret = 0;
  162.       break;
  163.     }
  164.   }
  165.         while (freesize < ret);
  166.   send_data_processing(s, (uint8 *)buf, ret);                         
  167. ……………………

  168. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
网络调试助手:http://www.51hei.com/bbs/dpj-201792-1.html
所有资料51hei提供下载:
STM32F407通过W5100S进行网络通信.7z (5.15 MB, 下载次数: 55)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:954066 发表于 2021-7-17 16:10 | 只看该作者
这个例程怎么用呀。。
回复

使用道具 举报

板凳
ID:690040 发表于 2022-12-22 10:37 | 只看该作者
你这个比世伟的驱动好多了,世伟的里面很多bug,在判断sock端口改变那里用了太多while等待,而且一些极端情况考虑的不是很周到导致经常莫名宕机,你这个比他们的好太多。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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