diff --git a/.gitignore b/.gitignore index bcb8f64..f494111 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ dkms.conf main.lst main_ext.bin main.bin +bin/ diff --git a/Makefile b/Makefile index 01726ab..1e91c25 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,8 @@ LWIP_C_FILES := LWIP_C_FILES += $(COREFILES) LWIP_C_FILES += $(CORE4FILES) LWIP_C_FILES += $(NETIFFILES) -# Add APIFILES if you are using the Netconn/Socket API -# ifeq ($(USE_API), 1) -# LWIP_C_FILES += $(APIFILES) -# endif +LWIP_C_FILES += $(HTTPFILES) + LWIP_C_FILES_WITH_PATH := $(LWIP_C_FILES) LWIP_PORT_FILES := $(wildcard $(PORT_DIR)/*.c) @@ -32,7 +30,7 @@ INCLUDE_DIRS ?= \ -I$(LWIP_DIR)/src/include \ -I$(PORT_DIR) -PROJECT_C_FILES := $(filter-out ./main.c, $(wildcard ./*.c)) +PROJECT_C_FILES := $(filter-out ./main.c ./fsdata_custom.c, $(wildcard ./*.c)) LIB_C_FILES := # Add all lwIP sources to the main build variable diff --git a/bin/makefsdata b/bin/makefsdata new file mode 100755 index 0000000..4a8499f Binary files /dev/null and b/bin/makefsdata differ diff --git a/fsdata_custom.c b/fsdata_custom.c new file mode 100644 index 0000000..197b1a7 --- /dev/null +++ b/fsdata_custom.c @@ -0,0 +1,104 @@ +#include "lwip/apps/fs.h" +#include "lwip/def.h" + + +#define file_NULL (struct fsdata_file *) NULL + + +#ifndef FS_FILE_FLAGS_HEADER_INCLUDED +#define FS_FILE_FLAGS_HEADER_INCLUDED 1 +#endif +#ifndef FS_FILE_FLAGS_HEADER_PERSISTENT +#define FS_FILE_FLAGS_HEADER_PERSISTENT 0 +#endif +/* FSDATA_FILE_ALIGNMENT: 0=off, 1=by variable, 2=by include */ +#ifndef FSDATA_FILE_ALIGNMENT +#define FSDATA_FILE_ALIGNMENT 0 +#endif +#ifndef FSDATA_ALIGN_PRE +#define FSDATA_ALIGN_PRE +#endif +#ifndef FSDATA_ALIGN_POST +#define FSDATA_ALIGN_POST +#endif +#if FSDATA_FILE_ALIGNMENT==2 +#include "fsdata_alignment.h" +#endif +#if FSDATA_FILE_ALIGNMENT==1 +static const unsigned int dummy_align__index_html = 0; +#endif +static const unsigned char FSDATA_ALIGN_PRE data__index_html[] FSDATA_ALIGN_POST = { +/* /index.html (12 chars) */ +0x2f,0x69,0x6e,0x64,0x65,0x78,0x2e,0x68,0x74,0x6d,0x6c,0x00, + +/* HTTP header */ +/* "HTTP/1.0 200 OK +" (17 bytes) */ +0x48,0x54,0x54,0x50,0x2f,0x31,0x2e,0x30,0x20,0x32,0x30,0x30,0x20,0x4f,0x4b,0x0d, +0x0a, +/* "Server: lwIP/2.2.2d (http://savannah.nongnu.org/projects/lwip) +" (64 bytes) */ +0x53,0x65,0x72,0x76,0x65,0x72,0x3a,0x20,0x6c,0x77,0x49,0x50,0x2f,0x32,0x2e,0x32, +0x2e,0x32,0x64,0x20,0x28,0x68,0x74,0x74,0x70,0x3a,0x2f,0x2f,0x73,0x61,0x76,0x61, +0x6e,0x6e,0x61,0x68,0x2e,0x6e,0x6f,0x6e,0x67,0x6e,0x75,0x2e,0x6f,0x72,0x67,0x2f, +0x70,0x72,0x6f,0x6a,0x65,0x63,0x74,0x73,0x2f,0x6c,0x77,0x69,0x70,0x29,0x0d,0x0a, + +/* "Content-Length: 549 +" (18+ bytes) */ +0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x4c,0x65,0x6e,0x67,0x74,0x68,0x3a,0x20, +0x35,0x34,0x39,0x0d,0x0a, +/* "Content-Type: text/html + +" (27 bytes) */ +0x43,0x6f,0x6e,0x74,0x65,0x6e,0x74,0x2d,0x54,0x79,0x70,0x65,0x3a,0x20,0x74,0x65, +0x78,0x74,0x2f,0x68,0x74,0x6d,0x6c,0x0d,0x0a,0x0d,0x0a, +/* raw file data (549 bytes) */ +0x3c,0x21,0x44,0x4f,0x43,0x54,0x59,0x50,0x45,0x20,0x68,0x74,0x6d,0x6c,0x3e,0x0a, +0x3c,0x68,0x74,0x6d,0x6c,0x3e,0x0a,0x20,0x20,0x20,0x20,0x3c,0x68,0x65,0x61,0x64, +0x3e,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3c,0x74,0x69,0x74,0x6c,0x65, +0x3e,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x66,0x72,0x6f,0x6d,0x20,0x43,0x48,0x33,0x32, +0x56,0x32,0x30,0x38,0x3c,0x2f,0x74,0x69,0x74,0x6c,0x65,0x3e,0x0a,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x20,0x3c,0x73,0x74,0x79,0x6c,0x65,0x3e,0x0a,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x62,0x6f,0x64,0x79,0x20,0x7b,0x0a, +0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, +0x66,0x6f,0x6e,0x74,0x2d,0x66,0x61,0x6d,0x69,0x6c,0x79,0x3a,0x20,0x73,0x61,0x6e, +0x73,0x2d,0x73,0x65,0x72,0x69,0x66,0x3b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x74,0x65,0x78,0x74,0x2d,0x61,0x6c, +0x69,0x67,0x6e,0x3a,0x20,0x63,0x65,0x6e,0x74,0x65,0x72,0x3b,0x0a,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x62,0x61,0x63, +0x6b,0x67,0x72,0x6f,0x75,0x6e,0x64,0x2d,0x63,0x6f,0x6c,0x6f,0x72,0x3a,0x20,0x23, +0x66,0x30,0x66,0x30,0x66,0x30,0x3b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x6d,0x61,0x72,0x67,0x69,0x6e,0x2d,0x74, +0x6f,0x70,0x3a,0x20,0x35,0x30,0x70,0x78,0x3b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x20,0x20,0x7d,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x68,0x31,0x20,0x7b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x63,0x6f,0x6c,0x6f,0x72,0x3a,0x20, +0x23,0x33,0x33,0x33,0x3b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, +0x20,0x20,0x7d,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, +0x70,0x20,0x7b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, +0x20,0x20,0x20,0x20,0x63,0x6f,0x6c,0x6f,0x72,0x3a,0x20,0x23,0x35,0x35,0x35,0x3b, +0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7d,0x0a,0x20, +0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3c,0x2f,0x73,0x74,0x79,0x6c,0x65,0x3e,0x0a, +0x20,0x20,0x20,0x20,0x3c,0x2f,0x68,0x65,0x61,0x64,0x3e,0x0a,0x20,0x20,0x20,0x20, +0x3c,0x62,0x6f,0x64,0x79,0x3e,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3c, +0x68,0x31,0x3e,0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,0x72,0x6c,0x64,0x3c,0x2f, +0x68,0x31,0x3e,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3c,0x70,0x3e,0x54, +0x68,0x69,0x73,0x20,0x70,0x61,0x67,0x65,0x20,0x69,0x73,0x20,0x73,0x65,0x72,0x76, +0x65,0x64,0x20,0x66,0x72,0x6f,0x6d,0x20,0x43,0x48,0x33,0x32,0x56,0x32,0x30,0x38, +0x20,0x75,0x73,0x69,0x6e,0x67,0x20,0x6c,0x77,0x49,0x50,0x2e,0x3c,0x2f,0x70,0x3e, +0x0a,0x20,0x20,0x20,0x20,0x3c,0x2f,0x62,0x6f,0x64,0x79,0x3e,0x0a,0x3c,0x2f,0x68, +0x74,0x6d,0x6c,0x3e,0x0a,}; + + + +const struct fsdata_file file__index_html[] = { { +file_NULL, +data__index_html, +data__index_html + 12, +sizeof(data__index_html) - 12, +FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT, +}}; + +#define FS_ROOT file__index_html +#define FS_NUMFILES 1 + diff --git a/main.c b/main.c index 3ce88a0..d52b38e 100644 --- a/main.c +++ b/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); - } } } \ No newline at end of file diff --git a/port/lwipopts.h b/port/lwipopts.h index 25e3421..ee4765c 100644 --- a/port/lwipopts.h +++ b/port/lwipopts.h @@ -6,7 +6,7 @@ // #define UDP_DEBUG LWIP_DBG_ON // #define IP_DEBUG LWIP_DBG_ON -#define DHCP_DEBUG LWIP_DBG_ON +// #define DHCP_DEBUG LWIP_DBG_ON #define NETIF_DEBUG LWIP_DBG_ON // #define ETHARP_DEBUG LWIP_DBG_ON @@ -17,7 +17,7 @@ // Memory options #define MEM_ALIGNMENT 4 -#define MEM_SIZE (4 * 1024) // 4KB of RAM for lwIP heap +#define MEM_SIZE (10 * 1024) // 4KB of RAM for lwIP heap // Pbuf options #define PBUF_POOL_SIZE 8 @@ -52,6 +52,14 @@ // Statistics #define LWIP_STATS 0 +#define LWIP_HTTPD 1 +// Use a read-only filesystem populated by makefsdata +// #define HTTPD_FSDATA_FILE "fsdata_custom.c" +#define LWIP_HTTPD_FS_SUPPORT 1 +#define HTTPD_USE_CUSTOM_FSDATA 1 + +#define LWIP_NETIF_HOSTNAME 1 // #define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_STATUS_CALLBACK 1 #endif /* __LWIPOPTS_H__ */ diff --git a/web_content/index.html b/web_content/index.html new file mode 100644 index 0000000..9f25643 --- /dev/null +++ b/web_content/index.html @@ -0,0 +1,24 @@ + + +
+This page is served from CH32V208 using lwIP.
+ +