標(biāo)題:
stm32通過w5500使用dns解讀IP地址 源程序
[打印本頁]
作者:
l834492956
時(shí)間:
2017-4-12 10:46
標(biāo)題:
stm32通過w5500使用dns解讀IP地址 源程序
stm32通過w5500使用dns解讀IP地址的源程序:
0.png
(67.5 KB, 下載次數(shù): 120)
下載附件
2017-4-12 15:49 上傳
0.png
(51.64 KB, 下載次數(shù): 112)
下載附件
2017-4-12 15:49 上傳
完整源碼下載:
stm32_w5500_dns.zip
(779.32 KB, 下載次數(shù): 85)
2017-4-12 10:44 上傳
點(diǎn)擊文件名下載附件
stm32_w5500_dns.zip
下載積分: 黑幣 -5
源程序預(yù)覽(主程序):
/**
******************************************************************************
* @file main.c
* $Author: 飛鴻踏雪 $
* $Revision: 17 $
* $Date:: 2014-10-25 11:16:48 +0800 #$
* @brief 主函數(shù).
******************************************************************************
* @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點(diǎn)embed-net點(diǎn)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); //注冊(cè)臨界區(qū)函數(shù)
/* Chip selection call back */
#if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注冊(cè)SPI片選信號(hào)函數(shù)
#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); //注冊(cè)讀寫函數(shù)
/* 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();//系統(tǒng)時(shí)鐘初始化
USART_Configuration();//串口1初始化
printf("\x0c");printf("\x0c");//超級(jí)終端清屏
printf("\033[1;40;32m");//設(shè)置超級(jí)終端背景為黑色,字符為綠色
printf("\r\n*******************************************************************************");
printf("\r\n************************ Copyright 2009-2014, EmbedNet ************************");
printf("\r\n*************************** http://www點(diǎn)embed-net點(diǎn)com **************************");
printf("\r\n***************************** All Rights Reserved *****************************");
printf("\r\n*******************************************************************************");
printf("\r\n");
//Config SPI
SPI_Configuration();
//滴答定時(shí)器初始化
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**********************************/
復(fù)制代碼
作者:
laxsystem01
時(shí)間:
2018-1-27 14:29
感謝,看了10分鐘就調(diào)通了,樓主威武
作者:
samxly
時(shí)間:
2018-3-2 20:11
非常需要.謝謝樓主
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1