W55RP20-EVB-Picoを使ってみよう~C/C++編~の続きです。
今回は、DHCPにしてみます。
最初に、examples の dhcp_dns を使用して DHCP をテストしましょう。
https://github.com/WIZnet-ioNIC/WIZnet-PICO-C/tree/main/examples/dhcp_dns
ターゲットとなるDNSが表示されていれば成功です。

DHCP化
次は、固定IPからDHCPに変更してみましょう。
前回も使用した wizchip_http_server.c を使用し、以下を追加します。
wizchip_dhcp_init();
while(1) {
if (DHCP_IP_LEASED == DHCP_run())
break;
}
while (1) {
DHCP_run();
// Application code can be placed here
}
当然、このまま追加しても動作しない為、調整します。
ヘッダー
/* DHCP */
static void wizchip_dhcp_init(void);
static void wizchip_dhcp_assign(void);
static void wizchip_dhcp_conflict(void);
/* Timer */
static void repeating_timer_callback(void);
Main内
wizchip_1ms_timer_initialize(repeating_timer_callback);
network_initialize(g_net_info);
wizchip_dhcp_init();
while (1)
{
if (DHCP_IP_LEASED == DHCP_run())
break;
}
httpServer_init(g_http_send_buf, g_http_recv_buf, HTTP_SOCKET_MAX_NUM, g_http_socket_num_list);
/* Get network information */
print_network_information(g_net_info);
/* Register web page */
reg_httpServer_webContent("index.html", index_page);
/* Infinite loop */
while (1)
{
//DHCP_run();
/* Run HTTP server */
for (i = 0; i < HTTP_SOCKET_MAX_NUM; i++)
{
httpServer_run(i);
}
}
関数類は適宜追加します。
コンパイルし書き込むことでDHCPにすることができました。

固定IP&DHCP切替
純粋にDHCP部分をコメントアウトすることでDHCPと固定IPを切り替えるようにしました。
以下を追加することでDHCPが有効になります。
逆にコメントアウトすることで固定IPとなります。
//DHCP
/*
wizchip_dhcp_init();
while (1)
{
if (DHCP_IP_LEASED == DHCP_run())
break;
}
*/
プログラム
プログラムは以下になります。
https://github.com/taisirou/WIZnet-PICO-C/tree/main