refactor dhcp handling

This commit is contained in:
2020-11-10 14:51:40 +01:00
parent 0cbc9e2d56
commit 0bd9174e91
2 changed files with 26 additions and 12 deletions

View File

@ -1,6 +1,10 @@
#ifndef _WIZHELPER_H_
#define _WIZHELPER_H_
#include <stdbool.h>
int wizInit();
bool isNetworkAvailable();
#endif // _WIZHELPER_H_

View File

@ -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(&etherSpi, &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(&etherSpi, &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(&etherSpi, 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(&etherSpi, 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;
}
}
}
}