找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8703|回复: 10
收起左侧

STM32驱动w5500_dhcp+http TCP/IP网络集成模块的源码

  [复制链接]
ID:296725 发表于 2018-3-25 14:31 | 显示全部楼层 |阅读模式
W5500 TCP/IP网络集成模块 STM32驱动
stm32_w5500_dhcp+http

单片机源程序如下:

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "usart.h"
  4. #include "delay.h"
  5. #include "spi.h"
  6. #include "socket.h"        // Just include one header for WIZCHIP
  7. #include "Internet/DHCP/dhcp.h"
  8. #include "httpServer.h"
  9. #include "httpParser.h"
  10. #include "httpUtil.h"
  11. /* Private typedef -----------------------------------------------------------*/
  12. /* Private define ------------------------------------------------------------*/
  13. #define SOCK_DHCP                        0
  14. #define MY_MAX_DHCP_RETRY        3
  15. #define DATA_BUF_SIZE   2048
  16. /* Private macro -------------------------------------------------------------*/
  17. uint8_t gDATABUF[DATA_BUF_SIZE];
  18. // Default Network Configuration

  19. uint8_t http_tx_buf[2048];
  20. uint8_t http_rx_buf[2048];
  21. const uint8_t flash_buffer[2048];
  22. uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
  23. uint8_t socklist[] = {0,1,2,3,4,5,6 };
  24. int g_time6=0;
  25. extern char index_html[4096];

  26. wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
  27.                             .ip = {192, 168, 1, 123},
  28.                             .sn = {255,255,255,0},
  29.                             .gw = {192, 168, 1, 1},
  30.                             .dns = {0,0,0,0},
  31.                             .dhcp = NETINFO_STATIC };
  32. /* Private variables ---------------------------------------------------------*/
  33. /* Private function prototypes -----------------------------------------------*/
  34. /* Private functions ---------------------------------------------------------*/
  35. void platform_init(void);                                                                // initialize the dependent host peripheral
  36. void my_ip_assign(void);
  37. void network_init(void);
  38.                            
  39.                            
  40.                                                    
  41.                            
  42.                            
  43. /************************************
  44. * @ brief Call back for ip Conflict
  45. ************************************/
  46. void my_ip_conflict(void)
  47. {
  48.         printf("CONFLICT IP from DHCP\r\n");
  49.         //halt or reset or any...
  50.         while(1); // this example is halt.
  51. }
  52. /**
  53.   * @brief  串口打印输出
  54.   * @param  None
  55.   * @retval None
  56.   */
  57. int main(void)
  58. {
  59.         uint8_t tmp;
  60.         uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
  61.         uint8_t my_dhcp_retry = 0;
  62.         

  63.         //Host dependent peripheral initialized
  64.         platform_init();
  65.         // First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP
  66.         /* Critical section callback */
  67.         reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit);        //注册临界区函数
  68.         /* Chip selection call back */
  69. #if   _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
  70.         reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注册SPI片选信号函数
  71. #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
  72.         reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);  // CS must be tried with LOW.
  73. #else
  74.    #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
  75.       #error "Unknown _WIZCHIP_IO_MODE_"
  76.    #else
  77.       reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
  78.    #endif
  79. #endif
  80.         /* SPI Read & Write callback function */
  81.         reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte);        //注册读写函数

  82.         /* WIZCHIP SOCKET Buffer initialize */
  83.         if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
  84.                  printf("WIZCHIP Initialized fail.\r\n");
  85.                  while(1);
  86.         }

  87.         /* PHY link status check */
  88.         do{
  89.                  if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
  90.                                 printf("Unknown PHY Link stauts.\r\n");
  91.                  }
  92.         }while(tmp == PHY_LINK_OFF);

  93.     /************************************************/
  94.     /* WIZnet W5500 Code Examples :                                 */
  95.     /* Implemented using ioLibrary_BSD Socket APIs        */
  96.     /************************************************/
  97.     /* >> DHCP Client                                                                 */
  98.     /************************************************/

  99.     // must be set the default mac before DHCP started.
  100.         setSHAR(gWIZNETINFO.mac);

  101.         DHCP_init(SOCK_DHCP, gDATABUF);
  102.         // if you want defiffent action instead defalut ip assign,update, conflict,
  103.         // if cbfunc == 0, act as default.
  104.         reg_dhcp_cbfunc(my_ip_assign, my_ip_assign, my_ip_conflict);
  105.     httpServer_init(http_tx_buf,http_rx_buf,7,socklist);
  106.     sprintf(index_html,INDEX_HTML_C,"");
  107.         /* Main Loop */
  108.         while(1)
  109.         {
  110.                 switch(DHCP_run())
  111.                 {
  112.                         case DHCP_IP_ASSIGN:
  113.                         case DHCP_IP_CHANGED:
  114.                                 /* If this block empty, act with default_ip_assign & default_ip_update */
  115.                                 //
  116.                                 // This example calls my_ip_assign in the two case.
  117.                                 //
  118.                                 // Add to ...
  119.                                 //
  120.                                 break;
  121.                         case DHCP_IP_LEASED:
  122.                                 //
  123.                                 // TO DO YOUR NETWORK APPs.
  124.                                 //
  125.                                 break;
  126.                         case DHCP_FAILED:
  127.                                 /* ===== Example pseudo code =====  */
  128.                                 // The below code can be replaced your code or omitted.
  129.                                 // if omitted, retry to process DHCP
  130.                                 my_dhcp_retry++;
  131.                                 if(my_dhcp_retry > MY_MAX_DHCP_RETRY)
  132.                                 {
  133.                                         printf(">> DHCP %d Failed\r\n", my_dhcp_retry);
  134.                                         my_dhcp_retry = 0;
  135.                                         DHCP_stop();      // if restart, recall DHCP_init()
  136.                                         network_init();   // apply the default static network and print out netinfo to serial
  137.                                 }
  138.                                 break;
  139.                         default:
  140.                                 break;
  141.                 }

  142.         httpServer_run(0);
  143.         httpServer_run(1);
  144.         httpServer_run(2);
  145.         httpServer_run(3);
  146.         httpServer_run(4);
  147.         httpServer_run(5);
  148.         httpServer_run(6);

  149.         }
  150. } // end of main()




  151. /**
  152.   * @brief  Loopback Test Example Code using ioLibrary_BSD       
  153.   * @retval None
  154.   */
  155. void platform_init(void)
  156. {
  157.         SystemInit();//系统时钟初始化
  158.         USART_Configuration();//串口1初始化
  159.         //Config SPI
  160.         SPI_Configuration();
  161.         //延时初始化
  162.         delay_init();
  163. }

  164. /*******************************************************
  165. * @ brief Call back for ip assing & ip update from DHCP
  166. *******************************************************/
  167. void my_ip_assign(void)
  168. {
  169.    getIPfromDHCP(gWIZNETINFO.ip);
  170.    getGWfromDHCP(gWIZNETINFO.gw);
  171.    getSNfromDHCP(gWIZNETINFO.sn);
  172.    getDNSfromDHCP(gWIZNETINFO.dns);
  173.    gWIZNETINFO.dhcp = NETINFO_DHCP;
  174.    /* Network initialization */
  175.    network_init();      // apply from dhcp
  176.    printf("DHCP LEASED TIME : %d Sec.\r\n", getDHCPLeasetime());
  177. }

  178. /******************************************************************************
  179. * @brief  Network Init
  180. * Intialize the network information to be used in WIZCHIP
  181. *****************************************************************************/
  182. void network_init(void)
  183. {
  184.         uint8_t tmpstr[6] = {0};
  185.         wiz_NetInfo netinfo;

  186.         // Set Network information from netinfo structure
  187.         ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);

  188.         // Get Network information
  189.         ctlnetwork(CN_GET_NETINFO, (void*)&netinfo);

  190.         // Display Network Information
  191.         ctlwizchip(CW_GET_ID,(void*)tmpstr);

  192.         if(netinfo.dhcp == NETINFO_DHCP) printf("\r\n=== %s NET CONF : DHCP ===\r\n",(char*)tmpstr);
  193.         else printf("\r\n=== %s NET CONF : Static ===\r\n",(char*)tmpstr);

  194.         printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",netinfo.mac[0],netinfo.mac[1],netinfo.mac[2],
  195.                         netinfo.mac[3],netinfo.mac[4],netinfo.mac[5]);
  196.         printf("SIP: %d.%d.%d.%d\r\n", netinfo.ip[0],netinfo.ip[1],netinfo.ip[2],netinfo.ip[3]);
  197.         printf("GAR: %d.%d.%d.%d\r\n", netinfo.gw[0],netinfo.gw[1],netinfo.gw[2],netinfo.gw[3]);
  198.         printf("SUB: %d.%d.%d.%d\r\n", netinfo.sn[0],netinfo.sn[1],netinfo.sn[2],netinfo.sn[3]);
  199.         printf("DNS: %d.%d.%d.%d\r\n", netinfo.dns[0],netinfo.dns[1],netinfo.dns[2],netinfo.dns[3]);
  200.         printf("===========================\r\n");

  201. }

  202. /*********************************END OF FILE**********************************/
复制代码

所有资料51hei提供下载:
stm32_w5500_dhcp+http.rar (744.49 KB, 下载次数: 398)

评分

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

查看全部评分

回复

使用道具 举报

ID:298882 发表于 2018-3-29 14:54 | 显示全部楼层
正好是我想找的,只有这里有,下来看看学习一下
回复

使用道具 举报

ID:552154 发表于 2019-7-16 09:43 | 显示全部楼层
还是下载不了
回复

使用道具 举报

ID:580104 发表于 2019-7-16 10:20 | 显示全部楼层
没币,先留个标记,等有黑币了再下吧!
回复

使用道具 举报

ID:322573 发表于 2019-9-12 21:30 | 显示全部楼层
学习一下,不错啊
回复

使用道具 举报

ID:625716 发表于 2019-10-17 16:38 | 显示全部楼层
下载不了啥情况 楼主能发我下么  我最近正想搞这一块
回复

使用道具 举报

ID:39439 发表于 2019-10-18 08:28 | 显示全部楼层
好东西
回复

使用道具 举报

ID:402986 发表于 2020-7-21 17:53 | 显示全部楼层
一直不知道怎么通过HTTP去接收数据,感谢分享~
回复

使用道具 举报

ID:984493 发表于 2021-11-25 10:39 | 显示全部楼层
非常感谢楼主分享,也算是值得的。
回复

使用道具 举报

ID:974713 发表于 2022-2-22 15:38 | 显示全部楼层
谢谢楼主,不过不是我想要的,楼主使用的代码基本上来源于github上面,大家在github上搜索下w5500就用,我想要的是监听w5500拔插,发现重新连接上之后,重新运行dhcp获取新ip地址。
回复

使用道具 举报

ID:974713 发表于 2022-2-22 15:40 | 显示全部楼层
刚刚找到解决办法了,CW_GET_PHYLINK就是获取链路状态。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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