stm32通过w5500使用dns解读IP地址的源程序:
完整源码下载:
stm32_w5500_dns.zip
(779.32 KB, 下载次数: 85)
源程序预览(主程序):
- /**
- ******************************************************************************
- * @file main.c
- * $Author: 飞鸿踏雪 $
- * $Revision: 17 $
- * $Date:: 2014-10-25 11:16:48 +0800 #$
- * @brief 主函数.
- ******************************************************************************
- * @attention
- *
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "usart.h"
- #include "delay.h"
- #include "spi.h"
- #include "socket.h" // Just include one header for WIZCHIP
- #include "Internet/DNS/dns.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define _MAIN_DEBUG_
- #define SOCK_DNS 0
- #define DATA_BUF_SIZE 2048
- /* Private macro -------------------------------------------------------------*/
- uint8_t gDATABUF[DATA_BUF_SIZE];
- // Default Network Configuration
- wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
- .ip = {192, 168, 1, 123},
- .sn = {255,255,255,0},
- .gw = {192, 168, 1, 1},
- .dns = {8,8,8,8},
- .dhcp = NETINFO_STATIC };
- uint8_t DNS_2nd[4] = {192, 168, 1, 1}; // Secondary DNS server IP
- uint8_t Domain_name[] = "www点embed-net点com"; // for Example domain name
- uint8_t Domain_IP[4] = {0}; // Translated IP address by DNS
- volatile uint32_t msTicks; /* counts 100ms timeTicks */
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- void platform_init(void); // initialize the dependent host peripheral
- void network_init(void);
- /*****************************************************************************
- * @brief SysTickIntHandler
- * Interrupt Service Routine for system tick counter
- *****************************************************************************/
- void SysTick_Handler(void)
- {
- msTicks++; /* increment counter necessary in Delay()*/
- // SHOULD BE Added DNS Timer Handler your 1s tick timer
- if((msTicks % 1000) == 0){
- DNS_time_handler();
- }
- }
- /**
- * @brief 串口打印输出
- * @param None
- * @retval None
- */
- int main(void)
- {
- int8_t ret;
- uint8_t tmp;
- uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
- //Host dependent peripheral initialized
- platform_init();
- // First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP
- /* Critical section callback */
- reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit); //注册临界区函数
- /* Chip selection call back */
- #if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
- reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注册SPI片选信号函数
- #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
- reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect); // CS must be tried with LOW.
- #else
- #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
- #error "Unknown _WIZCHIP_IO_MODE_"
- #else
- reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
- #endif
- #endif
- /* SPI Read & Write callback function */
- reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte); //注册读写函数
- /* WIZCHIP SOCKET Buffer initialize */
- if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
- printf("WIZCHIP Initialized fail.\r\n");
- while(1);
- }
- /* PHY link status check */
- do{
- if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
- printf("Unknown PHY Link stauts.\r\n");
- }
- }while(tmp == PHY_LINK_OFF);
- /* Network initialization */
- network_init(); // Static netinfo setting
- /************************************************/
- /* WIZnet W5500 Code Examples : */
- /* Implemented using ioLibrary_BSD Socket APIs */
- /************************************************/
- /* >> DNS Client */
- /************************************************/
- #ifdef _MAIN_DEBUG_
- printf("\r\n=== DNS Client Example ===============\r\n");
- printf("> DNS 1st : %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0], gWIZNETINFO.dns[1], gWIZNETINFO.dns[2], gWIZNETINFO.dns[3]);
- printf("> DNS 2nd : %d.%d.%d.%d\r\n", DNS_2nd[0], DNS_2nd[1], DNS_2nd[2], DNS_2nd[3]);
- printf("======================================\r\n");
- printf("> Example Domain Name : %s\r\n", Domain_name);
- #endif
- /* DNS client initialization */
- DNS_init(SOCK_DNS, gDATABUF);
- /* DNS procssing */
- if ((ret = DNS_run(gWIZNETINFO.dns, Domain_name, Domain_IP)) > 0) // try to 1st DNS
- {
- #ifdef _MAIN_DEBUG_
- printf("> 1st DNS Reponsed\r\n");
- #endif
- }
- else if ((ret != -1) && ((ret = DNS_run(DNS_2nd, Domain_name, Domain_IP))>0)) // retry to 2nd DNS
- {
- #ifdef _MAIN_DEBUG_
- printf("> 2nd DNS Reponsed\r\n");
- #endif
- }
- else if(ret == -1)
- {
- #ifdef _MAIN_DEBUG_
- printf("> MAX_DOMAIN_NAME is too small. Should be redefined it.\r\n");
- #endif
- }
- else
- {
- #ifdef _MAIN_DEBUG_
- printf("> DNS Failed\r\n");
- #endif
- }
- if(ret > 0)
- {
- #ifdef _MAIN_DEBUG_
- printf("> Translated %s to %d.%d.%d.%d\r\n",Domain_name,Domain_IP[0],Domain_IP[1],Domain_IP[2],Domain_IP[3]);
- #endif
- //
- // TO DO
- //
- }
- /* Main Loop */
- while(1)
- {
- }
- } // end of main()
- /**
- * @brief Loopback Test Example Code using ioLibrary_BSD
- * @retval None
- */
- void platform_init(void)
- {
- SystemInit();//系统时钟初始化
- USART_Configuration();//串口1初始化
- printf("\x0c");printf("\x0c");//超级终端清屏
- printf("\033[1;40;32m");//设置超级终端背景为黑色,字符为绿色
- printf("\r\n*******************************************************************************");
- printf("\r\n************************ Copyright 2009-2014, EmbedNet ************************");
- printf("\r\n*************************** http://www点embed-net点com **************************");
- printf("\r\n***************************** All Rights Reserved *****************************");
- printf("\r\n*******************************************************************************");
- printf("\r\n");
- //Config SPI
- SPI_Configuration();
- //滴答定时器初始化
- SysTick_Config(72000000 / 1000);//1ms
- }
- /******************************************************************************
- * @brief Network Init
- * Intialize the network information to be used in WIZCHIP
- *****************************************************************************/
- void network_init(void)
- {
- #ifdef _MAIN_DEBUG_
- uint8_t tmpstr[6] = {0,};
- wiz_NetInfo netinfo;
- #endif
- // Set Network information from netinfo structure
- ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
- #ifdef _MAIN_DEBUG_
- // Get Network information
- ctlnetwork(CN_GET_NETINFO, (void*)&netinfo);
- // Display Network Information
- ctlwizchip(CW_GET_ID,(void*)tmpstr);
- if(netinfo.dhcp == NETINFO_DHCP) printf("\r\n=== %s NET CONF : DHCP ===\r\n",(char*)tmpstr);
- else printf("\r\n=== %s NET CONF : Static ===\r\n",(char*)tmpstr);
- printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",netinfo.mac[0],netinfo.mac[1],netinfo.mac[2],
- netinfo.mac[3],netinfo.mac[4],netinfo.mac[5]);
- printf("SIP: %d.%d.%d.%d\r\n", netinfo.ip[0],netinfo.ip[1],netinfo.ip[2],netinfo.ip[3]);
- printf("GAR: %d.%d.%d.%d\r\n", netinfo.gw[0],netinfo.gw[1],netinfo.gw[2],netinfo.gw[3]);
- printf("SUB: %d.%d.%d.%d\r\n", netinfo.sn[0],netinfo.sn[1],netinfo.sn[2],netinfo.sn[3]);
- printf("DNS: %d.%d.%d.%d\r\n", netinfo.dns[0],netinfo.dns[1],netinfo.dns[2],netinfo.dns[3]);
- printf("===========================\r\n");
- #endif
- }
- /*********************************END OF FILE**********************************/
复制代码
|