WIZnet Developer Forum

W5500 stm32 Can't Get IP Address From DHCP

Hi Everybody!
I am trying send data to server from stm32 with wiznet w5500. static IP’s everything is ok. but DHCP request is fail. my modem doesn’t give up. i tried many example code but dosn’t work. Can you help me?

Sorry for my bad english.

My example code is here.


oid W5500_Select(void) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
}

void W5500_Unselect(void) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_SET);
}

void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
HAL_SPI_Receive(&hspi1, buff, len, HAL_MAX_DELAY);
}

void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
HAL_SPI_Transmit(&hspi1, buff, len, HAL_MAX_DELAY);
}

uint8_t W5500_ReadByte(void) {
uint8_t byte;
W5500_ReadBuff(&byte, sizeof(byte));
return byte;
}

void W5500_WriteByte(uint8_t byte) {
W5500_WriteBuff(&byte, sizeof(byte));
}

volatile bool ip_assigned = false;

void Callback_IPAssigned(void) {
//UART_Printf(“Callback: IP assigned! Leased time: %d sec\r\n”, getDHCPLeasetime());
ip_assigned = true;
}

void Callback_IPConflict(void) {
//UART_Printf(“Callback: IP conflict!\r\n”);
}

uint8_t dhcp_buffer[1024];

uint8_t dns_buffer[1024];
void init() {

reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);

// UART_Printf(“Calling wizchip_init()…\r\n”);
uint8_t rx_tx_buff_sizes = {2, 2, 2, 2, 2, 2, 2, 2};
wizchip_init(rx_tx_buff_sizes, rx_tx_buff_sizes);

// UART_Printf(“Calling DHCP_init()…\r\n”);
wiz_NetInfo net_info = {
.mac = { 0xEA, 0x11, 0x22, 0x33, 0x44, 0xEA },
.ip = {192, 168, 1, 56},
.sn = {255, 255, 255, 0},
.gw = {192, 168, 1, 1},
.dns = {0, 0, 0, 0},
.dhcp = NETINFO_DHCP
};
// set MAC address before using DHCP
setSHAR(net_info.mac);
DHCP_init(DHCP_SOCKET, dhcp_buffer);

// UART_Printf(“Registering DHCP callbacks…\r\n”);
reg_dhcp_cbfunc(
Callback_IPAssigned,
Callback_IPAssigned,
Callback_IPConflict
);

// UART_Printf(“Calling DHCP_run()…\r\n”);
// actually should be called in a loop, e.g. by timer
uint32_t ctr = 10000;
while((!ip_assigned) && (ctr > 0)) {
DHCP_run();
ctr–;
}
if(!ip_assigned) {
// UART_Printf("\r\nIP was not assigned :(\r\n");
return;
}

getIPfromDHCP(net_info.ip);
getGWfromDHCP(net_info.gw);
getSNfromDHCP(net_info.sn);

uint8_t dns[4];
getDNSfromDHCP(dns);

// UART_Printf(“Calling wizchip_setnetinfo()…\r\n”);
wizchip_setnetinfo(&net_info);

// UART_Printf(“Calling DNS_init()…\r\n”);
DNS_init(DNS_SOCKET, dns_buffer);

uint8_t addr[4];
{
    char domain_name[] = "eax.me";
   // UART_Printf("Resolving domain name \"%s\"...\r\n", domain_name);
    int8_t res = DNS_run(dns, (uint8_t*)&domain_name, addr);
    if(res != 1) {
       // UART_Printf("DNS_run() failed, res = %d", res);
        return;
    }
 //   UART_Printf("Result: %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
}

// UART_Printf(“Creating socket…\r\n”);
uint8_t http_socket = HTTP_SOCKET;
uint8_t code = socket(http_socket, Sn_MR_TCP, 10888, 0);
if(code != http_socket) {
// UART_Printf(“socket() failed, code = %d\r\n”, code);
return;
}

// UART_Printf(“Socket created, connecting…\r\n”);
code = connect(http_socket, addr, 80);
if(code != SOCK_OK) {

    close(http_socket);
    return;
}


{
    char req[] = "GET / HTTP/1.0\r\nHost: eax.me\r\n\r\n";
    uint16_t len = sizeof(req) - 1;
    uint8_t* buff = (uint8_t*)&req;
    while(len > 0) {

        int32_t nbytes = send(http_socket, buff, len);
        if(nbytes <= 0) {

            close(http_socket);
            return;
        }

        len -= nbytes;
    }
}


{
    char buff[32];
    for(;;) {
        int32_t nbytes = recv(http_socket, (uint8_t*)&buff, sizeof(buff)-1);
        if(nbytes == SOCKERR_SOCKSTATUS) {

            break;
        }

        if(nbytes <= 0) {

            break;
        }

        buff[nbytes] = '\0';

    }
}


close(http_socket);

}

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.