From 6f4cc803beddd7b9705be61c279a6cf51efde6d7 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 10 Nov 2020 14:26:01 +0100 Subject: [PATCH] refactor dhcp handling --- cube/User/Src/main2.c | 8 +++---- cube/User/Src/wizHelper.c | 49 +++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index 8fc823f..8dfe19a 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -190,11 +190,11 @@ void my_setup_2() { wizInit(); - frontendInit(); - frontendSetThreshold(240); + // frontendInit(); + // frontendSetThreshold(240); - schAdd(scheduleMBusRequest, NULL, 0, 1000); - schAdd(triggerMBusRequest, NULL, 0, 100); + // schAdd(scheduleMBusRequest, NULL, 0, 1000); + // schAdd(triggerMBusRequest, NULL, 0, 100); } void my_loop() { diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index 4f11c16..60850ca 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -78,39 +78,48 @@ static void wizDHCPUpdate() { } static void wizDHCPHandler(void *handle) { - static uint8_t lastStablePhyLink = 255; + coloredLog(LOG_RED, "wizdh"); + static uint8_t lastDhcpRes = 255; - static bool dhcpInitialized = false; + + uint8_t res = DHCP_run(); + + if (lastDhcpRes != res) { + coloredMsg(LOG_RED, "wizdh, dhcp state has changed: %d", res); + lastDhcpRes = res; + } +} + +static void wizPhyLinkHandler(void *handle) { + static uint8_t lastStablePhyLink = 255; uint8_t phyLink = 0; int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink); if (lastStablePhyLink != phyLink) { - coloredMsg(LOG_RED, "wizdh, ctlwizchip returns %d, phy link changed to %d", res, phyLink); + coloredMsg(LOG_RED, "wizplh, ctlwizchip returns %d, phy link changed to %d", res, phyLink); lastStablePhyLink = phyLink; - wiz_PhyConf wpc; - wizphy_getphyconf(&wpc); - coloredMsg(LOG_RED, "wizdh, wpc: b:%02x, m:%02x, s:%02x, d:%02x", - wpc.by, wpc.mode, wpc.speed, wpc.duplex); - - } else if (phyLink == PHY_LINK_ON) { - if (! dhcpInitialized) { + + if (phyLink == PHY_LINK_ON) { + // start DHCP handler memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE); reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL); DHCP_init(DHCP_SOCK, dhcpBuffer); - coloredMsg(LOG_RED, "wizdh, DHCP initialized"); - dhcpInitialized = true; - } + coloredMsg(LOG_RED, "wizplh, DHCP initialized"); - uint8_t res = DHCP_run(); + // run the dhcp handler the first time after 1s and then every 100ms + schAdd(wizDHCPHandler, NULL, 1000, 1000); + coloredMsg(LOG_RED, "wizplh, DHCP handler scheduled"); + } else { + // stop DHCP handler + DHCP_stop(); + coloredMsg(LOG_RED, "wizplh, DHCP stopped"); - if (lastDhcpRes != res) { - coloredMsg(LOG_RED, "wizdh, dhcp state has changed: %d", res); - lastDhcpRes = res; + schDel(wizDHCPHandler, NULL); + coloredMsg(LOG_RED, "wizplh, DHCP handler unscheduled"); } } } - int wizInit() { coloredMsg(LOG_RED, "wizI, resetting Ethernet module"); wizReset(true); @@ -155,8 +164,8 @@ int wizInit() { coloredMsg(LOG_RED, "wizI, CW_GET_ID: %d %02x %02x %02x %02x %02x %02x", res, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); - schAdd(wizDHCPHandler, NULL, 0, 1000); - coloredMsg(LOG_RED, "wizI, DHCP handler scheduled"); + schAdd(wizPhyLinkHandler, NULL, 0, 1000); + coloredMsg(LOG_RED, "wizI, PhyLink handler scheduled"); return 0; }