303 lines
9.4 KiB
C
Raw Normal View History

2021-01-09 22:01:21 +01:00
#include <wizHelper.h>
#include <stdint.h>
#include <main2.h>
#include <spi.h>
#include <stdbool.h>
#include <logger.h>
#include <PontCoopScheduler.h>
#include <utils.h>
#include <wizchip_conf.h>
#include <string.h>
#include <dhcp.h>
#include <show.h>
#include <dns.h>
2021-02-07 13:47:34 +01:00
#include <sntp.h>
2021-01-09 22:01:21 +01:00
#include <config.h>
static t_configBlock *config;
wiz_NetInfo netInfo;
#define DHCP_BUFFER_SIZE 2048
static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
#define DNS_BUFFER_SIZE MAX_DNS_BUF_SIZE
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
2021-02-07 13:47:34 +01:00
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
2021-02-07 18:35:30 +01:00
static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
2021-02-07 19:04:15 +01:00
const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
2021-02-07 13:47:34 +01:00
2021-01-09 22:01:21 +01:00
extern const uint8_t DHCP_SOCK;
extern const uint8_t DNS_SOCK;
2021-02-07 13:47:34 +01:00
extern const uint8_t SNTP_SOCK;
2021-01-09 22:01:21 +01:00
static bool networkAvailable = false;
bool isNetworkAvailable() {
return networkAvailable;
}
uint8_t* wizGetIPAddress() {
return netInfo.ip;
}
static void wiz_cs_select(void) {
HAL_GPIO_WritePin(ETHER_CS_GPIO_Port, ETHER_CS_Pin, GPIO_PIN_RESET);
}
static void wiz_cs_deselect(void) {
HAL_GPIO_WritePin(ETHER_CS_GPIO_Port, ETHER_CS_Pin, GPIO_PIN_SET);
}
static uint8_t wiz_spi_readbyte(void) {
uint8_t rbuf;
HAL_SPI_Receive(&etherSpi, &rbuf, 1, HAL_MAX_DELAY);
return rbuf;
}
static void wiz_spi_writebyte(uint8_t wb) {
HAL_SPI_Transmit(&etherSpi, &wb, 1, HAL_MAX_DELAY);
}
static void wiz_spi_readburst(uint8_t* pBuf, uint16_t len) {
HAL_SPI_Receive(&etherSpi, pBuf, len, HAL_MAX_DELAY);
}
static void wiz_spi_writeburst(uint8_t* pBuf, uint16_t len) {
HAL_SPI_Transmit(&etherSpi, pBuf, len, HAL_MAX_DELAY);
}
static void wizReset(bool b) {
HAL_GPIO_WritePin(ETHER_RES_GPIO_Port, ETHER_RES_Pin, b ? GPIO_PIN_RESET : GPIO_PIN_SET);
}
static void wizDHCPAssign() {
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizda");
2021-01-09 22:01:21 +01:00
getIPfromDHCP(netInfo.ip);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizda, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
2021-01-09 22:01:21 +01:00
getGWfromDHCP(netInfo.gw);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizda, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
2021-01-09 22:01:21 +01:00
getSNfromDHCP(netInfo.sn);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizda, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
2021-01-09 22:01:21 +01:00
getDNSfromDHCP(netInfo.dns);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizda, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
2021-01-09 22:01:21 +01:00
wizchip_setnetinfo(&netInfo);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizda, set netinfo again");
2021-01-09 22:01:21 +01:00
networkAvailable = true;
show(LED_GREEN, ON);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizda, network is available");
2021-01-09 22:01:21 +01:00
}
static void wizDHCPUpdate() {
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdu");
2021-01-09 22:01:21 +01:00
getIPfromDHCP(netInfo.ip);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdu, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
2021-01-09 22:01:21 +01:00
getGWfromDHCP(netInfo.gw);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdu, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
2021-01-09 22:01:21 +01:00
getSNfromDHCP(netInfo.sn);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdu, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
2021-01-09 22:01:21 +01:00
getDNSfromDHCP(netInfo.dns);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdu, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
2021-01-09 22:01:21 +01:00
wizchip_setnetinfo(&netInfo);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdu, netinfo updated");
2021-01-09 22:01:21 +01:00
}
static void wizDHCPHandler(void *handle) {
static uint8_t lastDhcpRes = 255;
uint8_t res = DHCP_run();
if (lastDhcpRes != res) {
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdh, dhcp state has changed: %d", res);
2021-01-09 22:01:21 +01:00
lastDhcpRes = res;
}
}
bool wizDnsQuery(char *name, uint8_t *ip) {
bool retCode = false;
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdq, querying for %s", name);
2021-01-09 22:01:21 +01:00
int8_t res = DNS_run(netInfo.dns, (uint8_t*) name, ip);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdq, DNS_run returns %d", res);
2021-01-09 22:01:21 +01:00
retCode = (res == 1);
if (retCode) {
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizdq, got address %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
2021-01-09 22:01:21 +01:00
}
return retCode;
}
2021-02-07 13:47:34 +01:00
static void wizSNTPHandler(void *handle) {
if (networkAvailable) {
coloredMsg(LOG_BLUE, "wizsh, about to call SNTP");
2021-02-07 14:14:54 +01:00
2021-02-07 13:54:45 +01:00
uint8_t ntpServer[4];
2021-02-07 14:14:54 +01:00
if (wizDnsQuery(NTP_SERVER, ntpServer)) {
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
2021-02-07 19:04:15 +01:00
bool updated = false;
2021-02-07 14:14:54 +01:00
for (uint8_t i = 0; i < 16; i++) {
2021-02-07 18:35:30 +01:00
datetime curTime;
int8_t res = SNTP_run(&curTime);
2021-02-07 19:04:15 +01:00
uint64_t receivedSeconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
2021-02-07 18:35:30 +01:00
if (res == 1) {
2021-02-07 19:04:15 +01:00
if (seconds.seconds != receivedSeconds) {
coloredMsg(LOG_BLUE, "wizsh, time deviation: %lu %lu",
seconds.seconds, receivedSeconds);
} else {
coloredMsg(LOG_BLUE, "wizsh, time still matching");
2021-02-07 18:35:30 +01:00
}
2021-02-07 19:04:15 +01:00
seconds.seconds = receivedSeconds;
2021-02-07 18:35:30 +01:00
seconds.valid = true;
2021-02-07 19:04:15 +01:00
seconds.missedUpdates = 0;
updated = true;
coloredMsg(LOG_BLUE, "wizsh, curTime: %lu", seconds.seconds);
2021-02-07 18:35:30 +01:00
break;
}
2021-02-07 13:54:45 +01:00
}
2021-02-07 19:04:15 +01:00
if (! updated) {
coloredMsg(LOG_BLUE, "wizsh, time update failed");
seconds.missedUpdates += 1;
}
2021-02-07 13:47:34 +01:00
} else {
2021-02-07 18:35:30 +01:00
seconds.missedUpdates += 1;
2021-02-07 13:54:45 +01:00
coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name");
2021-02-07 13:47:34 +01:00
}
}
}
2021-02-07 18:35:30 +01:00
static void wizSecondsHandler(void *handle) {
seconds.seconds += 1;
}
t_seconds* wizGetSeconds() {
return &seconds;
}
2021-02-07 13:47:34 +01:00
2021-01-09 22:01:21 +01:00
static void wizPhyLinkHandler(void *handle) {
// this handler is anyhow called with a 1s period, so we reuse it for the DNS timer
DNS_time_handler();
static uint8_t lastStablePhyLink = 255;
static bool dhcpInitialized = false;
2021-02-07 13:47:34 +01:00
static bool sntpInitialized = false;
2021-01-09 22:01:21 +01:00
uint8_t phyLink = 0;
int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink);
if (lastStablePhyLink != phyLink) {
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizplh, ctlwizchip returns %d, phy link changed to %d", res, phyLink);
2021-01-09 22:01:21 +01:00
lastStablePhyLink = phyLink;
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);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizplh, DHCP initialized");
2021-01-09 22:01:21 +01:00
// run the dhcp handler the first time after 1s and then every 100ms
schAdd(wizDHCPHandler, NULL, 1000, 1000);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizplh, DHCP handler scheduled");
2021-01-09 22:01:21 +01:00
dhcpInitialized = true;
2021-02-07 13:47:34 +01:00
2021-02-07 19:15:41 +01:00
schAdd(wizSNTPHandler, NULL, 0, 60*1000);
2021-02-07 13:47:34 +01:00
coloredMsg(LOG_BLUE, "wizplh, SNTP handler scheduled");
sntpInitialized = true;
2021-01-09 22:01:21 +01:00
} else {
networkAvailable = false;
show(LED_GREEN, BLINK);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizplh, network is unavailable");
2021-01-09 22:01:21 +01:00
// stop DHCP handler
if (dhcpInitialized) {
DHCP_stop();
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizplh, DHCP stopped");
2021-01-09 22:01:21 +01:00
schDel(wizDHCPHandler, NULL);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizplh, DHCP handler unscheduled");
2021-01-09 22:01:21 +01:00
dhcpInitialized = false;
}
2021-02-07 13:47:34 +01:00
if (sntpInitialized) {
schDel(wizSNTPHandler, NULL);
coloredMsg(LOG_BLUE, "wizplh, SNTP handler unscheduled");
sntpInitialized = false;
}
2021-01-09 22:01:21 +01:00
}
}
}
int wizInit() {
config = getConfig();
netInfo.dhcp = NETINFO_DHCP;
memcpy(netInfo.mac, config->macAddress, 6);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, resetting Ethernet module");
2021-01-09 22:01:21 +01:00
wizReset(true);
activeDelay(2);
wizReset(false);
activeDelay(50);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, registering callbacks");
2021-01-09 22:01:21 +01:00
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, cs funcs registed");
2021-01-09 22:01:21 +01:00
reg_wizchip_spi_cbfunc(wiz_spi_readbyte, wiz_spi_writebyte);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, spi funcs registed");
2021-01-09 22:01:21 +01:00
reg_wizchip_spiburst_cbfunc(wiz_spi_readburst, wiz_spi_writeburst);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, spi burst funcs registed");
2021-01-09 22:01:21 +01:00
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, initializing Ethernet module");
2021-01-09 22:01:21 +01:00
uint8_t bufSizes[] = { 2, 2, 2, 2, 2, 2, 2, 2 };
int8_t res = wizchip_init(bufSizes, bufSizes);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, module driver returned %d", res);
2021-01-09 22:01:21 +01:00
wizphy_reset();
activeDelay(5);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, reset phy");
2021-01-09 22:01:21 +01:00
wiz_PhyConf wpc;
wpc.mode = PHY_MODE_MANUAL;
wpc.speed = PHY_MODE_MANUAL;
wpc.duplex = PHY_DUPLEX_FULL;
wizphy_setphyconf(&wpc);
activeDelay(5);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, phy config set");
2021-01-09 22:01:21 +01:00
wizchip_setnetinfo(&netInfo);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, netinfo set to Ethernet module");
2021-01-09 22:01:21 +01:00
res = wizchip_setnetmode(NM_FORCEARP); // and not NM_PINGBLOCK
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, set netmode, result is %d", res);
2021-01-09 22:01:21 +01:00
uint8_t buf[6];
res = ctlwizchip(CW_GET_ID, (void*) buf);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, CW_GET_ID: %d %02x %02x %02x %02x %02x %02x", res, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
2021-01-09 22:01:21 +01:00
DNS_init(DNS_SOCK, dnsBuffer);
schAdd(wizPhyLinkHandler, NULL, 0, 1000);
2021-02-07 13:14:35 +01:00
coloredMsg(LOG_BLUE, "wizI, PhyLink handler scheduled");
2021-01-09 22:01:21 +01:00
2021-02-07 18:35:30 +01:00
schAdd(wizSecondsHandler, NULL, 0, 1000);
coloredMsg(LOG_BLUE, "wizI, Seconds handler scheduled");
2021-01-09 22:01:21 +01:00
return 0;
}