163 lines
5.0 KiB
C
Raw Normal View History

#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-09 11:20:00 +01:00
wiz_NetInfo netInfo = {
2020-11-08 15:56:14 +01:00
.mac = { 0xa0, 0x57, 0x62, 0x01, 0x02, 0x03 },
.dhcp = NETINFO_DHCP
};
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-09 19:47:39 +01:00
const uint8_t DHCP_SOCK = 0;
2020-11-09 11:20:00 +01:00
2020-11-08 16:22:25 +01:00
void wiz_cs_select(void) {
HAL_GPIO_WritePin(ETHER_CS_GPIO_Port, ETHER_CS_Pin, GPIO_PIN_RESET);
}
2020-11-08 16:22:25 +01:00
void wiz_cs_deselect(void) {
HAL_GPIO_WritePin(ETHER_CS_GPIO_Port, ETHER_CS_Pin, GPIO_PIN_SET);
}
2020-11-08 16:22:25 +01:00
uint8_t wiz_spi_readbyte(void) {
uint8_t rbuf;
HAL_SPI_Receive(&etherSpi, &rbuf, 1, HAL_MAX_DELAY);
2020-11-09 14:47:37 +01:00
// coloredMsg(LOG_YELLOW, "R: %02x", rbuf);
return rbuf;
}
2020-11-08 16:22:25 +01:00
void wiz_spi_writebyte(uint8_t wb) {
2020-11-09 14:47:37 +01:00
// coloredMsg(LOG_YELLOW, "W: %02x", wb);
HAL_SPI_Transmit(&etherSpi, &wb, 1, HAL_MAX_DELAY);
}
2020-11-08 16:22:25 +01:00
void wiz_spi_readburst(uint8_t* pBuf, uint16_t len) {
2020-11-09 14:37:19 +01:00
HAL_SPI_Receive(&etherSpi, pBuf, len, HAL_MAX_DELAY);
2020-11-09 14:47:37 +01:00
// coloredMsg(LOG_YELLOW, "RB: %d %02x %02x %02x", len, pBuf[0], pBuf[1], pBuf[2]);
}
2020-11-08 16:22:25 +01:00
void wiz_spi_writeburst(uint8_t* pBuf, uint16_t len) {
2020-11-09 14:37:19 +01:00
HAL_SPI_Transmit(&etherSpi, pBuf, len, HAL_MAX_DELAY);
2020-11-09 14:47:37 +01:00
// coloredMsg(LOG_YELLOW, "WB: %d %02x %02x %02x", len, pBuf[0], pBuf[1], pBuf[2]);
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() {
coloredMsg(LOG_GREEN, "wizda");
getIPfromDHCP(netInfo.ip);
coloredMsg(LOG_GREEN, "wizda, IP: %d.%d.%d.%d", netInfo.ip[0], netInfo.ip[1], netInfo.ip[2], netInfo.ip[3]);
getGWfromDHCP(netInfo.gw);
coloredMsg(LOG_GREEN, "wizda, GW: %d.%d.%d.%d", netInfo.gw[0], netInfo.gw[1], netInfo.gw[2], netInfo.gw[3]);
getSNfromDHCP(netInfo.sn);
coloredMsg(LOG_GREEN, "wizda, SN: %d.%d.%d.%d", netInfo.sn[0], netInfo.sn[1], netInfo.sn[2], netInfo.sn[3]);
getDNSfromDHCP(netInfo.dns);
coloredMsg(LOG_GREEN, "wizda, DNS: %d.%d.%d.%d", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
wizchip_setnetinfo(&netInfo);
coloredMsg(LOG_RED, "wizda, set netinfo again");
2020-11-09 14:37:19 +01:00
}
static void wizDHCPUpdate() {
coloredMsg(LOG_RED, "wizdu");
}
2020-11-08 15:56:14 +01:00
static void wizDHCPHandler(void *handle) {
2020-11-08 16:55:27 +01:00
static uint8_t lastStablePhyLink = 255;
2020-11-09 14:37:19 +01:00
static uint8_t lastDhcpRes = 255;
static bool dhcpInitialized = false;
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) {
coloredMsg(LOG_RED, "wizdh, ctlwizchip returns %d, phy link changed to %d", res, phyLink);
lastStablePhyLink = phyLink;
2020-11-09 19:47:39 +01:00
wiz_PhyConf wpc;
wizphy_getphyconf(&wpc);
coloredMsg(LOG_RED, "wizdh, wpc: b:%02x, m:%02x, s:%02x, d:%02x",
wpc.by, wpc.mode, wpc.speed, wpc.duplex);
} else if (phyLink == PHY_LINK_ON) {
2020-11-09 14:37:19 +01:00
if (! dhcpInitialized) {
memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE);
reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL);
DHCP_init(DHCP_SOCK, dhcpBuffer);
2020-11-09 19:47:39 +01:00
coloredMsg(LOG_RED, "wizdh, DHCP initialized");
2020-11-09 14:37:19 +01:00
dhcpInitialized = true;
}
2020-11-09 11:20:00 +01:00
uint8_t res = DHCP_run();
2020-11-09 14:37:19 +01:00
if (lastDhcpRes != res) {
2020-11-09 11:20:00 +01:00
coloredMsg(LOG_RED, "wizdh, dhcp state has changed: %d", res);
2020-11-09 14:37:19 +01:00
lastDhcpRes = res;
2020-11-09 11:20:00 +01:00
}
}
}
2020-11-08 15:56:14 +01:00
int wizInit() {
coloredMsg(LOG_RED, "wizI, resetting Ethernet module");
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-08 16:14:02 +01:00
coloredMsg(LOG_RED, "wizI, registering callbacks");
2020-11-08 16:22:25 +01:00
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
coloredMsg(LOG_GREEN, "wizI, cs funcs registed");
2020-11-08 16:51:53 +01:00
reg_wizchip_spi_cbfunc(wiz_spi_readbyte, wiz_spi_writebyte);
coloredMsg(LOG_GREEN, "wizI, spi funcs registed");
2020-11-09 14:37:19 +01:00
reg_wizchip_spiburst_cbfunc(wiz_spi_readburst, wiz_spi_writeburst);
coloredMsg(LOG_GREEN, "wizI, spi burst funcs registed");
2020-11-08 16:14:02 +01:00
2020-11-08 15:56:14 +01:00
coloredMsg(LOG_RED, "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-08 16:51:53 +01:00
coloredMsg(LOG_RED, "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);
coloredMsg(LOG_RED, "wizI, reset phy");
wiz_PhyConf wpc;
wpc.mode = PHY_MODE_MANUAL;
wpc.speed = PHY_MODE_MANUAL;
wpc.duplex = PHY_DUPLEX_FULL;
wizphy_setphyconf(&wpc);
activeDelay(5);
coloredMsg(LOG_RED, "wizI, phy config set");
2020-11-09 11:20:00 +01:00
wizchip_setnetinfo(&netInfo);
2020-11-08 16:51:53 +01:00
coloredMsg(LOG_RED, "wizI, netinfo set to Ethernet module");
2020-11-08 15:56:14 +01:00
res = wizchip_setnetmode(NM_FORCEARP); // and not NM_PINGBLOCK
coloredMsg(LOG_RED, "wizI, set netmode, result is %d", res);
2020-11-09 14:46:09 +01:00
uint8_t buf[6];
res = ctlwizchip(CW_GET_ID, (void*) buf);
coloredMsg(LOG_RED, "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 19:47:39 +01:00
schAdd(wizDHCPHandler, NULL, 0, 1000);
coloredMsg(LOG_RED, "wizI, DHCP handler scheduled");
2020-11-08 15:56:14 +01:00
return 0;
2020-11-08 16:03:13 +01:00
}