diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index 8fc823f..c22bfe1 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 606633a..5e2630a 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include wiz_NetInfo netInfo = { @@ -15,7 +17,7 @@ wiz_NetInfo netInfo = { .dhcp = NETINFO_DHCP }; -#define DHCP_BUFFER_SIZE 1024 +#define DHCP_BUFFER_SIZE 2048 static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE]; const uint8_t DHCP_SOCK = 6; @@ -32,42 +34,29 @@ void wiz_cs_deselect(void) { uint8_t wiz_spi_readbyte(void) { uint8_t rbuf; HAL_SPI_Receive(ðerSpi, &rbuf, 1, HAL_MAX_DELAY); + coloredMsg(LOG_YELLOW, "R: %02x", rbuf); return rbuf; } void wiz_spi_writebyte(uint8_t wb) { + coloredMsg(LOG_YELLOW, "W: %02x", wb); HAL_SPI_Transmit(ðerSpi, &wb, 1, HAL_MAX_DELAY); } void wiz_spi_readburst(uint8_t* pBuf, uint16_t len) { - + HAL_SPI_Receive(ðerSpi, pBuf, len, HAL_MAX_DELAY); + coloredMsg(LOG_YELLOW, "RB: %d %02x %02x %02x", len, pBuf[0], pBuf[1], pBuf[2]); } void wiz_spi_writeburst(uint8_t* pBuf, uint16_t len) { - + HAL_SPI_Transmit(ðerSpi, pBuf, len, HAL_MAX_DELAY); + coloredMsg(LOG_YELLOW, "WB: %d %02x %02x %02x", len, pBuf[0], pBuf[1], pBuf[2]); } static void wizReset(bool b) { HAL_GPIO_WritePin(ETHER_RES_GPIO_Port, ETHER_RES_Pin, b ? GPIO_PIN_RESET : GPIO_PIN_SET); } -static void wizDHCPHandler(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); - lastStablePhyLink = phyLink; - } - if (phyLink == PHY_LINK_ON) { - static uint8_t lastRes = 255; - uint8_t res = DHCP_run(); - if (lastRes != res) { - coloredMsg(LOG_RED, "wizdh, dhcp state has changed: %d", res); - lastRes = res; - } - } -} static void wizDHCPAssign() { coloredMsg(LOG_RED, "wizda"); @@ -77,6 +66,37 @@ static void wizDHCPUpdate() { coloredMsg(LOG_RED, "wizdu"); } +static void wizDHCPHandler(void *handle) { + static uint8_t lastStablePhyLink = 255; + static uint8_t lastDhcpRes = 255; + static bool dhcpInitialized = false; + + 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); + lastStablePhyLink = phyLink; + } + if (phyLink == PHY_LINK_ON) { + + if (! dhcpInitialized) { + memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE); + reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL); + DHCP_init(DHCP_SOCK, dhcpBuffer); + coloredMsg(LOG_RED, "wizI, DHCP initialized"); + dhcpInitialized = true; + } + + uint8_t res = DHCP_run(); + + if (lastDhcpRes != res) { + coloredMsg(LOG_RED, "wizdh, dhcp state has changed: %d", res); + lastDhcpRes = res; + } + } +} + + int wizInit() { coloredMsg(LOG_RED, "wizI, resetting Ethernet module"); wizReset(true); @@ -89,7 +109,8 @@ int wizInit() { coloredMsg(LOG_GREEN, "wizI, cs funcs registed"); reg_wizchip_spi_cbfunc(wiz_spi_readbyte, wiz_spi_writebyte); coloredMsg(LOG_GREEN, "wizI, spi funcs registed"); - // reg_wizchip_spiburst_cbfunc(void (*spi_rb)(uint8_t* pBuf, uint16_t len), void (*spi_wb)(uint8_t* pBuf, uint16_t len)); + reg_wizchip_spiburst_cbfunc(wiz_spi_readburst, wiz_spi_writeburst); + coloredMsg(LOG_GREEN, "wizI, spi burst funcs registed"); coloredMsg(LOG_RED, "wizI, initializing Ethernet module"); @@ -99,12 +120,7 @@ int wizInit() { wizchip_setnetinfo(&netInfo); coloredMsg(LOG_RED, "wizI, netinfo set to Ethernet module"); - memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE); - reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL); - DHCP_init(DHCP_SOCK, dhcpBuffer); - coloredMsg(LOG_RED, "wizI, DHCP initialized"); - - schAdd(wizDHCPHandler, NULL, 0, 1000); + schAdd(wizDHCPHandler, NULL, 0, 100); coloredMsg(LOG_RED, "wizI, DHCP handler scheduled"); return 0;