找回密码
 立即注册

QQ登录

只需一步,快速开始

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

W5500实现DNS域名解析STM32源码

  [复制链接]
跳转到指定楼层
楼主
W5500 dns 域名解析


单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * $Author: 飞鸿踏雪 $
  5.   * $Revision: 17 $
  6.   * $Date:: 2014-10-25 11:16:48 +0800 #$
  7.   * @brief   主函数.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   *<h3><center>© Copyright 2009-2012, EmbedNet</center>
  12.   *<center>All Rights Reserved</center></h3>
  13.   *
  14.   ******************************************************************************
  15.   */
  16. /* Includes ------------------------------------------------------------------*/
  17. #include "main.h"
  18. #include "usart.h"
  19. #include "delay.h"
  20. #include "spi.h"
  21. #include "socket.h"        // Just include one header for WIZCHIP
  22. #include "Internet/DNS/dns.h"
  23. /* Private typedef -----------------------------------------------------------*/
  24. /* Private define ------------------------------------------------------------*/
  25. #define _MAIN_DEBUG_
  26. #define SOCK_DNS                        0
  27. #define DATA_BUF_SIZE   2048
  28. /* Private macro -------------------------------------------------------------*/
  29. uint8_t gDATABUF[DATA_BUF_SIZE];
  30. // Default Network Configuration
  31. wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
  32.                             .ip = {192, 168, 1, 123},
  33.                             .sn = {255,255,255,0},
  34.                             .gw = {192, 168, 1, 1},
  35.                             .dns = {8,8,8,8},
  36.                             .dhcp = NETINFO_STATIC };
  37. uint8_t DNS_2nd[4]    = {192, 168, 1, 1};                // Secondary DNS server IP
  38. uint8_t Domain_name[] = "www点embed-net点com";                // for Example domain name
  39. uint8_t Domain_IP[4]  = {0};                                        // Translated IP address by DNS
  40. volatile uint32_t msTicks; /* counts 100ms timeTicks */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* Private function prototypes -----------------------------------------------*/
  43. /* Private functions ---------------------------------------------------------*/
  44. void platform_init(void);                                                                // initialize the dependent host peripheral
  45. void network_init(void);

  46. /*****************************************************************************
  47. * @brief SysTickIntHandler
  48. * Interrupt Service Routine for system tick counter
  49. *****************************************************************************/
  50. void SysTick_Handler(void)
  51. {
  52.         msTicks++; /* increment counter necessary in Delay()*/
  53.         // SHOULD BE Added DNS Timer Handler your 1s tick timer
  54.         if((msTicks % 1000) == 0){
  55.                 DNS_time_handler();
  56.         }
  57. }

  58. /**
  59.   * @brief  串口打印输出
  60.   * @param  None
  61.   * @retval None
  62.   */
  63. int main(void)
  64. {
  65.    int8_t  ret;
  66.    uint8_t tmp;
  67.    uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
  68.         //Host dependent peripheral initialized
  69.         platform_init();
  70.         // First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP
  71.         /* Critical section callback */
  72.         reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit);        //注册临界区函数
  73.         /* Chip selection call back */
  74. #if   _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
  75.         reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注册SPI片选信号函数
  76. #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
  77.         reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);  // CS must be tried with LOW.
  78. #else
  79.    #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
  80.       #error "Unknown _WIZCHIP_IO_MODE_"
  81.    #else
  82.       reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
  83.    #endif
  84. #endif
  85.         /* SPI Read & Write callback function */
  86.         reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte);        //注册读写函数

  87.         /* WIZCHIP SOCKET Buffer initialize */
  88.         if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
  89.                  printf("WIZCHIP Initialized fail.\r\n");
  90.                  while(1);
  91.         }

  92.         /* PHY link status check */
  93.         do{
  94.                  if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
  95.                                 printf("Unknown PHY Link stauts.\r\n");
  96.                  }
  97.         }while(tmp == PHY_LINK_OFF);

  98.     /* Network initialization */
  99.     network_init(); // Static netinfo setting

  100.     /************************************************/
  101.     /* WIZnet W5500 Code Examples :                                 */
  102.     /* Implemented using ioLibrary_BSD Socket APIs        */
  103.     /************************************************/
  104.     /* >> DNS Client                                                                 */
  105.     /************************************************/

  106. #ifdef _MAIN_DEBUG_
  107.    printf("\r\n=== DNS Client Example ===============\r\n");
  108.    printf("> DNS 1st : %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0], gWIZNETINFO.dns[1], gWIZNETINFO.dns[2], gWIZNETINFO.dns[3]);
  109.    printf("> DNS 2nd : %d.%d.%d.%d\r\n", DNS_2nd[0], DNS_2nd[1], DNS_2nd[2], DNS_2nd[3]);
  110.    printf("======================================\r\n");
  111.    printf("> Example Domain Name : %s\r\n", Domain_name);
  112. #endif
  113.    /* DNS client initialization */
  114.    DNS_init(SOCK_DNS, gDATABUF);

  115.    /* DNS procssing */
  116.    if ((ret = DNS_run(gWIZNETINFO.dns, Domain_name, Domain_IP)) > 0) // try to 1st DNS
  117.    {
  118. #ifdef _MAIN_DEBUG_
  119.       printf("> 1st DNS Reponsed\r\n");
  120. #endif
  121.    }
  122.    else if ((ret != -1) && ((ret = DNS_run(DNS_2nd, Domain_name, Domain_IP))>0))     // retry to 2nd DNS
  123.    {
  124. #ifdef _MAIN_DEBUG_
  125.       printf("> 2nd DNS Reponsed\r\n");
  126. #endif
  127.    }
  128.    else if(ret == -1)
  129.    {
  130. #ifdef _MAIN_DEBUG_
  131.       printf("> MAX_DOMAIN_NAME is too small. Should be redefined it.\r\n");
  132. #endif
  133.    }
  134.    else
  135.    {
  136. #ifdef _MAIN_DEBUG_
  137.       printf("> DNS Failed\r\n");
  138. #endif
  139.    }

  140.    if(ret > 0)
  141.    {
  142. #ifdef _MAIN_DEBUG_
  143.       printf("> Translated %s to %d.%d.%d.%d\r\n",Domain_name,Domain_IP[0],Domain_IP[1],Domain_IP[2],Domain_IP[3]);
  144. #endif
  145.       //
  146.       // TO DO
  147.       //
  148.    }
  149.         /* Main Loop */
  150.         while(1)
  151.         {

  152.         }
  153. } // end of main()




  154. /**
  155.   * @brief  Loopback Test Example Code using ioLibrary_BSD        
  156.   * @retval None
  157.   */
  158. void platform_init(void)
  159. {
  160.         SystemInit();//系统时钟初始化
  161.         USART_Configuration();//串口1初始化
  162.         printf("\x0c");printf("\x0c");//超级终端清屏
  163.         printf("\033[1;40;32m");//设置超级终端背景为黑色,字符为绿色
  164.         printf("\r\n*******************************************************************************");
  165.         printf("\r\n************************ Copyright 2009-2014, EmbedNet ************************");
  166.         printf("\r\n*************************** www点embed-net点com **************************");
  167.         printf("\r\n***************************** All Rights Reserved *****************************");
  168.         printf("\r\n*******************************************************************************");
  169.         printf("\r\n");
  170.         //Config SPI
  171.         SPI_Configuration();
  172.         //滴答定时器初始化
  173.         SysTick_Config(72000000 / 1000);//1ms
  174. }

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

  185. ……………………

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

所有资料51hei提供下载:
stm32_w5500_dns (1).zip (779.32 KB, 下载次数: 114)



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

使用道具 举报

沙发
ID:221753 发表于 2017-7-23 13:23 | 只看该作者
谢谢                  
回复

使用道具 举报

板凳
ID:87631 发表于 2017-11-13 16:02 | 只看该作者
已下载,非常感谢
回复

使用道具 举报

地板
ID:302073 发表于 2018-4-6 23:07 来自手机 | 只看该作者
不错,非常感谢
回复

使用道具 举报

5#
ID:261070 发表于 2018-8-14 12:16 | 只看该作者
学习一下
回复

使用道具 举报

6#
ID:252705 发表于 2018-8-14 12:49 | 只看该作者
学习学习
回复

使用道具 举报

7#
ID:91469 发表于 2018-10-1 01:59 来自手机 | 只看该作者
谢谢啦!正好用到
回复

使用道具 举报

8#
ID:467503 发表于 2019-4-10 07:04 来自手机 | 只看该作者
感谢感谢
回复

使用道具 举报

9#
ID:585455 发表于 2019-8-26 13:51 | 只看该作者
感謝分享
回复

使用道具 举报

10#
ID:142699 发表于 2019-9-24 19:08 | 只看该作者
这个实现dns解析,应该需要路由支持解吧,不然自己怎么获取/.?
回复

使用道具 举报

11#
ID:476421 发表于 2019-9-25 08:18 | 只看该作者
感谢分享
回复

使用道具 举报

12#
ID:235303 发表于 2019-11-12 16:57 | 只看该作者
支持楼主
回复

使用道具 举报

13#
ID:142699 发表于 2020-4-30 16:27 | 只看该作者
建议用原厂新版的代码,优化了很多
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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