chore: httpd
This commit is contained in:
35
main.c
35
main.c
@@ -3,6 +3,7 @@
|
||||
#include "ch32fun.h"
|
||||
#include "ch32v20xhw.h"
|
||||
#include "ethernetif.h"
|
||||
#include "lwip/apps/httpd.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "lwip/init.h"
|
||||
#include "lwip/netif.h"
|
||||
@@ -73,6 +74,26 @@ void led_init(void) {
|
||||
GPIOA->CFGLR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP) << (4 * LED2_PIN);
|
||||
}
|
||||
|
||||
static void netif_status_callback(struct netif* netif) {
|
||||
if (netif_is_up(netif) && !ip_addr_isany_val(*netif_ip4_addr(netif))) {
|
||||
printf("netif UP\n");
|
||||
printf(" MAC : %02X:%02X:%02X:%02X:%02X:%02X\n", netif->hwaddr[0],
|
||||
netif->hwaddr[1], netif->hwaddr[2], netif->hwaddr[3],
|
||||
netif->hwaddr[4], netif->hwaddr[5]);
|
||||
printf(" IP : %s\n", ip4addr_ntoa(netif_ip4_addr(netif)));
|
||||
printf(" Mask : %s\n", ip4addr_ntoa(netif_ip4_netmask(netif)));
|
||||
printf(" GW : %s\n", ip4addr_ntoa(netif_ip4_gw(netif)));
|
||||
|
||||
httpd_init();
|
||||
printf("HTTP server init\n");
|
||||
GPIOA->BSHR = (1 << LED2_PIN);
|
||||
|
||||
} else {
|
||||
printf("netif DOWN or has no IP address\n");
|
||||
GPIOA->BSHR = (1 << (LED2_PIN + 16));
|
||||
}
|
||||
}
|
||||
|
||||
void lwip_stack_init(void) {
|
||||
ip_addr_t ipaddr, netmask, gw;
|
||||
|
||||
@@ -85,6 +106,8 @@ void lwip_stack_init(void) {
|
||||
netif_add(&g_netif, &ipaddr, &netmask, &gw, NULL, ðernetif_init,
|
||||
ðernet_input);
|
||||
|
||||
netif_set_status_callback(&g_netif, netif_status_callback);
|
||||
|
||||
netif_set_default(&g_netif);
|
||||
netif_set_up(&g_netif);
|
||||
|
||||
@@ -107,18 +130,16 @@ int main() {
|
||||
uint32_t last_led_toggle_time = 0;
|
||||
uint32_t last_link_poll_time = 0;
|
||||
int led_state = 0;
|
||||
int ip_address_printed = 0;
|
||||
|
||||
while (1) {
|
||||
ethernetif_input(&g_netif);
|
||||
sys_check_timeouts();
|
||||
|
||||
if (millis() - last_link_poll_time > LINK_POLL_INTERVAL_MS) {
|
||||
ethernetif_link_poll(&g_netif);
|
||||
last_link_poll_time = millis();
|
||||
}
|
||||
|
||||
sys_check_timeouts();
|
||||
|
||||
uint32_t now = millis();
|
||||
if (now - last_led_toggle_time > LED_TOGGLE_INTERVAL_MS) {
|
||||
if (led_state) {
|
||||
@@ -129,13 +150,5 @@ int main() {
|
||||
led_state = !led_state;
|
||||
last_led_toggle_time = now;
|
||||
}
|
||||
|
||||
if (!ip_address_printed && g_netif.ip_addr.addr != 0) {
|
||||
printf("IP address assigned: %s\n",
|
||||
ip4addr_ntoa(netif_ip4_addr(&g_netif)));
|
||||
ip_address_printed = 1;
|
||||
|
||||
GPIOA->BSHR = (1 << LED2_PIN);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user