diff --git a/cube/User/Inc/wizHelper.h b/cube/User/Inc/wizHelper.h index 1ab437b..0371ce3 100644 --- a/cube/User/Inc/wizHelper.h +++ b/cube/User/Inc/wizHelper.h @@ -1,6 +1,10 @@ #ifndef _WIZHELPER_H_ #define _WIZHELPER_H_ +#include + int wizInit(); +bool isNetworkAvailable(); + #endif // _WIZHELPER_H_ \ No newline at end of file diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index 0267fc0..e6ed663 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -22,33 +22,38 @@ static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE]; const uint8_t DHCP_SOCK = 0; +static bool networkAvailable = false; -void wiz_cs_select(void) { +bool isNetworkAvailable() { + return networkAvailable; +} + +static void wiz_cs_select(void) { HAL_GPIO_WritePin(ETHER_CS_GPIO_Port, ETHER_CS_Pin, GPIO_PIN_RESET); } -void wiz_cs_deselect(void) { +static void wiz_cs_deselect(void) { HAL_GPIO_WritePin(ETHER_CS_GPIO_Port, ETHER_CS_Pin, GPIO_PIN_SET); } -uint8_t wiz_spi_readbyte(void) { +static 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) { +static 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) { +static 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) { +static 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]); } @@ -78,8 +83,6 @@ static void wizDHCPUpdate() { } static void wizDHCPHandler(void *handle) { - coloredMsg(LOG_RED, "wizdh"); - static uint8_t lastDhcpRes = 255; uint8_t res = DHCP_run(); @@ -92,6 +95,7 @@ static void wizDHCPHandler(void *handle) { static void wizPhyLinkHandler(void *handle) { static uint8_t lastStablePhyLink = 255; + static bool dhcpInitialized = false; uint8_t phyLink = 0; int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink); @@ -109,13 +113,19 @@ static void wizPhyLinkHandler(void *handle) { // 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"); + + dhcpInitialized = true; } else { // stop DHCP handler - DHCP_stop(); - coloredMsg(LOG_RED, "wizplh, DHCP stopped"); + if (dhcpInitialized) { + DHCP_stop(); + coloredMsg(LOG_RED, "wizplh, DHCP stopped"); - schDel(wizDHCPHandler, NULL); - coloredMsg(LOG_RED, "wizplh, DHCP handler unscheduled"); + schDel(wizDHCPHandler, NULL); + coloredMsg(LOG_RED, "wizplh, DHCP handler unscheduled"); + + dhcpInitialized = false; + } } } }