2020-11-07 22:37:33 +01:00
|
|
|
#include <wizHelper.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <main2.h>
|
|
|
|
#include <spi.h>
|
2020-11-08 15:56:14 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <logger.h>
|
|
|
|
#include <PontCoopScheduler.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#include <wizchip_conf.h>
|
2020-11-09 14:37:19 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <dhcp.h>
|
2020-11-17 15:04:34 +01:00
|
|
|
#include <show.h>
|
2020-11-19 20:03:37 +01:00
|
|
|
#include <oled.h>
|
2020-11-27 12:28:56 +01:00
|
|
|
#include <dns.h>
|
2020-11-27 13:32:12 +01:00
|
|
|
#include <config.h>
|
2020-11-07 22:37:33 +01:00
|
|
|
|
2020-11-27 13:32:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
static t_configBlock *config;
|
|
|
|
|
|
|
|
wiz_NetInfo netInfo;
|
|
|
|
|
2020-11-09 14:37:19 +01:00
|
|
|
#define DHCP_BUFFER_SIZE 2048
|
2020-11-09 11:20:00 +01:00
|
|
|
static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
|
|
|
|
|
2020-11-27 12:28:56 +01:00
|
|
|
#define DNS_BUFFER_SIZE MAX_DNS_BUF_SIZE
|
|
|
|
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
|
|
|
|
|
2020-11-10 15:56:07 +01:00
|
|
|
extern const uint8_t DHCP_SOCK;
|
2020-11-27 12:28:56 +01:00
|
|
|
extern const uint8_t DNS_SOCK;
|
2020-11-09 11:20:00 +01:00
|
|
|
|
2020-11-10 14:51:40 +01:00
|
|
|
static bool networkAvailable = false;
|
2020-11-07 22:37:33 +01:00
|
|
|
|
2020-11-10 14:51:40 +01:00
|
|
|
bool isNetworkAvailable() {
|
|
|
|
return networkAvailable;
|
|
|
|
}
|
|
|
|
|
2020-11-20 12:20:30 +01:00
|
|
|
uint8_t* wizGetIPAddress() {
|
|
|
|
return netInfo.ip;
|
|
|
|
}
|
2020-11-10 14:51:40 +01:00
|
|
|
static void wiz_cs_select(void) {
|
2020-11-07 22:37:33 +01:00
|
|
|
HAL_GPIO_WritePin(ETHER_CS_GPIO_Port, ETHER_CS_Pin, GPIO_PIN_RESET);
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:51:40 +01:00
|
|
|
static void wiz_cs_deselect(void) {
|
2020-11-07 22:37:33 +01:00
|
|
|
HAL_GPIO_WritePin(ETHER_CS_GPIO_Port, ETHER_CS_Pin, GPIO_PIN_SET);
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:51:40 +01:00
|
|
|
static uint8_t wiz_spi_readbyte(void) {
|
2020-11-07 22:37:33 +01:00
|
|
|
uint8_t rbuf;
|
|
|
|
HAL_SPI_Receive(ðerSpi, &rbuf, 1, HAL_MAX_DELAY);
|
|
|
|
return rbuf;
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:51:40 +01:00
|
|
|
static void wiz_spi_writebyte(uint8_t wb) {
|
2020-11-07 22:37:33 +01:00
|
|
|
HAL_SPI_Transmit(ðerSpi, &wb, 1, HAL_MAX_DELAY);
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:51:40 +01:00
|
|
|
static void wiz_spi_readburst(uint8_t* pBuf, uint16_t len) {
|
2020-11-09 14:37:19 +01:00
|
|
|
HAL_SPI_Receive(ðerSpi, pBuf, len, HAL_MAX_DELAY);
|
2020-11-07 22:37:33 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:51:40 +01:00
|
|
|
static void wiz_spi_writeburst(uint8_t* pBuf, uint16_t len) {
|
2020-11-09 14:37:19 +01:00
|
|
|
HAL_SPI_Transmit(ðerSpi, pBuf, len, HAL_MAX_DELAY);
|
2020-11-08 15:56:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wizReset(bool b) {
|
|
|
|
HAL_GPIO_WritePin(ETHER_RES_GPIO_Port, ETHER_RES_Pin, b ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:37:19 +01:00
|
|
|
|
|
|
|
static void wizDHCPAssign() {
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizda");
|
2020-11-09 20:10:12 +01:00
|
|
|
getIPfromDHCP(netInfo.ip);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizda, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
|
2020-11-09 20:10:12 +01:00
|
|
|
getGWfromDHCP(netInfo.gw);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizda, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
|
2020-11-09 20:10:12 +01:00
|
|
|
getSNfromDHCP(netInfo.sn);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizda, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
|
2020-11-09 20:10:12 +01:00
|
|
|
getDNSfromDHCP(netInfo.dns);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizda, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
|
2020-11-09 20:10:12 +01:00
|
|
|
|
|
|
|
wizchip_setnetinfo(&netInfo);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizda, set netinfo again");
|
2020-11-10 15:10:19 +01:00
|
|
|
|
|
|
|
networkAvailable = true;
|
2020-11-17 15:04:34 +01:00
|
|
|
show(LED_GREEN, ON);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizda, network is available");
|
2020-11-19 20:03:37 +01:00
|
|
|
|
2020-11-20 12:20:30 +01:00
|
|
|
oledPrintf(OLED_SCREEN0, "Addr:%d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
|
2020-11-09 14:37:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wizDHCPUpdate() {
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizdu");
|
2020-11-10 15:10:19 +01:00
|
|
|
getIPfromDHCP(netInfo.ip);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizdu, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
|
2020-11-10 15:10:19 +01:00
|
|
|
getGWfromDHCP(netInfo.gw);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizdu, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
|
2020-11-10 15:10:19 +01:00
|
|
|
getSNfromDHCP(netInfo.sn);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizdu, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
|
2020-11-10 15:10:19 +01:00
|
|
|
getDNSfromDHCP(netInfo.dns);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizdu, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
|
2020-11-10 15:10:19 +01:00
|
|
|
|
|
|
|
wizchip_setnetinfo(&netInfo);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizdu, netinfo updated");
|
2020-11-09 14:37:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 15:56:14 +01:00
|
|
|
static void wizDHCPHandler(void *handle) {
|
2020-11-09 14:37:19 +01:00
|
|
|
static uint8_t lastDhcpRes = 255;
|
2020-11-10 14:26:01 +01:00
|
|
|
|
|
|
|
uint8_t res = DHCP_run();
|
|
|
|
|
|
|
|
if (lastDhcpRes != res) {
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizdh, dhcp state has changed: %d", res);
|
2020-11-10 14:26:01 +01:00
|
|
|
lastDhcpRes = res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 12:28:56 +01:00
|
|
|
bool wizDnsQuery(char *name, uint8_t *ip) {
|
|
|
|
bool retCode = false;
|
|
|
|
coloredMsg(LOG_BLUE, false, "wizdq, querying for %s", name);
|
|
|
|
int8_t res = DNS_run(netInfo.dns, (uint8_t*) name, ip);
|
|
|
|
coloredMsg(LOG_BLUE, false, "wizdq, DNS_run returns %d", res);
|
|
|
|
retCode = (res == 1);
|
|
|
|
if (retCode) {
|
|
|
|
coloredMsg(LOG_BLUE, false, "wizdq, got address %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
|
|
|
}
|
|
|
|
return retCode;
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:26:01 +01:00
|
|
|
static void wizPhyLinkHandler(void *handle) {
|
2020-11-27 12:28:56 +01:00
|
|
|
// this handler is anyhow called with a 1s period, so we reuse it for the DNS timer
|
|
|
|
DNS_time_handler();
|
|
|
|
|
2020-11-10 14:26:01 +01:00
|
|
|
static uint8_t lastStablePhyLink = 255;
|
2020-11-10 14:51:40 +01:00
|
|
|
static bool dhcpInitialized = false;
|
2020-11-09 14:37:19 +01:00
|
|
|
|
2020-11-08 15:56:14 +01:00
|
|
|
uint8_t phyLink = 0;
|
|
|
|
int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink);
|
2020-11-08 16:55:27 +01:00
|
|
|
if (lastStablePhyLink != phyLink) {
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizplh, ctlwizchip returns %d, phy link changed to %d", res, phyLink);
|
2020-11-08 16:55:27 +01:00
|
|
|
lastStablePhyLink = phyLink;
|
2020-11-10 14:26:01 +01:00
|
|
|
|
|
|
|
if (phyLink == PHY_LINK_ON) {
|
2020-11-20 12:20:30 +01:00
|
|
|
oledPrint(OLED_SCREEN0, "Link available");
|
2020-11-10 14:26:01 +01:00
|
|
|
// start DHCP handler
|
2020-11-09 14:37:19 +01:00
|
|
|
memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE);
|
|
|
|
reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL);
|
|
|
|
DHCP_init(DHCP_SOCK, dhcpBuffer);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizplh, DHCP initialized");
|
2020-11-10 14:26:01 +01:00
|
|
|
|
|
|
|
// run the dhcp handler the first time after 1s and then every 100ms
|
|
|
|
schAdd(wizDHCPHandler, NULL, 1000, 1000);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizplh, DHCP handler scheduled");
|
2020-11-10 14:51:40 +01:00
|
|
|
|
|
|
|
dhcpInitialized = true;
|
2020-11-10 14:26:01 +01:00
|
|
|
} else {
|
2020-11-20 12:20:30 +01:00
|
|
|
oledPrint(OLED_SCREEN0, "Link lost");
|
2020-11-19 20:03:37 +01:00
|
|
|
|
2020-11-10 15:10:19 +01:00
|
|
|
networkAvailable = false;
|
2020-11-17 15:04:34 +01:00
|
|
|
show(LED_GREEN, BLINK);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizplh, network is unavailable");
|
2020-11-10 15:10:19 +01:00
|
|
|
|
2020-11-10 14:26:01 +01:00
|
|
|
// stop DHCP handler
|
2020-11-10 14:51:40 +01:00
|
|
|
if (dhcpInitialized) {
|
|
|
|
DHCP_stop();
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizplh, DHCP stopped");
|
2020-11-10 14:51:40 +01:00
|
|
|
|
|
|
|
schDel(wizDHCPHandler, NULL);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizplh, DHCP handler unscheduled");
|
2020-11-10 14:26:01 +01:00
|
|
|
|
2020-11-10 14:51:40 +01:00
|
|
|
dhcpInitialized = false;
|
|
|
|
}
|
2020-11-09 11:20:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 12:28:56 +01:00
|
|
|
|
2020-11-08 15:56:14 +01:00
|
|
|
int wizInit() {
|
2020-11-27 13:32:12 +01:00
|
|
|
config = getConfig();
|
|
|
|
netInfo.dhcp = NETINFO_DHCP;
|
|
|
|
memcpy(netInfo.mac, config->macAddress, 6);
|
|
|
|
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, resetting Ethernet module");
|
2020-11-08 15:56:14 +01:00
|
|
|
wizReset(true);
|
|
|
|
activeDelay(2);
|
|
|
|
wizReset(false);
|
2020-11-09 19:47:39 +01:00
|
|
|
activeDelay(50);
|
2020-11-08 15:56:14 +01:00
|
|
|
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, registering callbacks");
|
2020-11-08 16:22:25 +01:00
|
|
|
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, cs funcs registed");
|
2020-11-08 16:51:53 +01:00
|
|
|
reg_wizchip_spi_cbfunc(wiz_spi_readbyte, wiz_spi_writebyte);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, spi funcs registed");
|
2020-11-09 14:37:19 +01:00
|
|
|
reg_wizchip_spiburst_cbfunc(wiz_spi_readburst, wiz_spi_writeburst);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, spi burst funcs registed");
|
2020-11-08 16:14:02 +01:00
|
|
|
|
|
|
|
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, initializing Ethernet module");
|
2020-11-09 19:47:39 +01:00
|
|
|
uint8_t bufSizes[] = { 2, 2, 2, 2, 2, 2, 2, 2 };
|
|
|
|
int8_t res = wizchip_init(bufSizes, bufSizes);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, module driver returned %d", res);
|
2020-11-08 15:56:14 +01:00
|
|
|
|
2020-11-09 19:47:39 +01:00
|
|
|
wizphy_reset();
|
|
|
|
activeDelay(5);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, reset phy");
|
2020-11-09 19:47:39 +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);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, phy config set");
|
2020-11-09 19:47:39 +01:00
|
|
|
|
2020-11-09 11:20:00 +01:00
|
|
|
wizchip_setnetinfo(&netInfo);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, netinfo set to Ethernet module");
|
2020-11-08 15:56:14 +01:00
|
|
|
|
2020-11-09 20:10:12 +01:00
|
|
|
res = wizchip_setnetmode(NM_FORCEARP); // and not NM_PINGBLOCK
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, set netmode, result is %d", res);
|
2020-11-09 20:10:12 +01:00
|
|
|
|
2020-11-09 14:46:09 +01:00
|
|
|
uint8_t buf[6];
|
|
|
|
res = ctlwizchip(CW_GET_ID, (void*) buf);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, CW_GET_ID: %d %02x %02x %02x %02x %02x %02x", res, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
|
2020-11-09 14:46:09 +01:00
|
|
|
|
2020-11-27 12:28:56 +01:00
|
|
|
DNS_init(DNS_SOCK, dnsBuffer);
|
2020-11-09 14:46:09 +01:00
|
|
|
|
2020-11-10 14:26:01 +01:00
|
|
|
schAdd(wizPhyLinkHandler, NULL, 0, 1000);
|
2020-11-17 12:01:15 +01:00
|
|
|
coloredMsg(LOG_BLUE, false, "wizI, PhyLink handler scheduled");
|
2020-11-08 15:56:14 +01:00
|
|
|
|
|
|
|
return 0;
|
2020-11-08 16:03:13 +01:00
|
|
|
}
|