Files
picomap/w6300/wizchip_conf.cpp

348 lines
10 KiB
C++
Raw Normal View History

#include <cstring>
#include "wizchip_conf.h"
static uint8_t dns_[4];
static uint8_t dns6_[16];
static ipconf_mode ipmode_;
static constexpr char WIZCHIP_ID[] = "W6300";
int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg) {
uint8_t tmp = *(uint8_t*)arg;
uint8_t* ptmp[2] = {0, 0};
switch (cwtype) {
case CW_SYS_LOCK:
if (tmp & SYS_CHIP_LOCK) CHIPLOCK();
if (tmp & SYS_NET_LOCK) NETLOCK();
if (tmp & SYS_PHY_LOCK) PHYLOCK();
break;
case CW_SYS_UNLOCK:
if (tmp & SYS_CHIP_LOCK) CHIPUNLOCK();
if (tmp & SYS_NET_LOCK) NETUNLOCK();
if (tmp & SYS_PHY_LOCK) PHYUNLOCK();
break;
case CW_GET_SYSLOCK:
*(uint8_t*)arg = getSYSR() >> 5;
break;
case CW_RESET_WIZCHIP:
wizchip_sw_reset();
break;
case CW_INIT_WIZCHIP:
if (arg) {
ptmp[0] = (uint8_t*)arg;
ptmp[1] = ptmp[0] + WIZCHIP_SOCK_NUM;
}
return wizchip_init(ptmp[0], ptmp[1]);
case CW_CLR_INTERRUPT:
wizchip_clrinterrupt(*((intr_kind*)arg));
break;
case CW_GET_INTERRUPT:
*((intr_kind*)arg) = wizchip_getinterrupt();
break;
case CW_SET_INTRMASK:
wizchip_setinterruptmask(*((intr_kind*)arg));
break;
case CW_GET_INTRMASK:
*((intr_kind*)arg) = wizchip_getinterruptmask();
break;
case CW_SET_INTRTIME:
setINTPTMR(*(uint16_t*)arg);
break;
case CW_GET_INTRTIME:
*(uint16_t*)arg = getINTPTMR();
break;
case CW_GET_ID:
memcpy(arg, WIZCHIP_ID, sizeof(WIZCHIP_ID));
break;
case CW_GET_VER:
*(uint16_t*)arg = getVER();
break;
case CW_RESET_PHY:
wizphy_reset();
break;
case CW_SET_PHYCONF:
wizphy_setphyconf((wiz_PhyConf*)arg);
break;
case CW_GET_PHYCONF:
wizphy_getphyconf((wiz_PhyConf*)arg);
break;
case CW_GET_PHYSTATUS:
break;
case CW_SET_PHYPOWMODE:
wizphy_setphypmode(*(uint8_t*)arg);
break;
case CW_GET_PHYPOWMODE:
tmp = wizphy_getphypmode();
if ((int8_t)tmp == -1) return -1;
*(uint8_t*)arg = tmp;
break;
case CW_GET_PHYLINK:
tmp = wizphy_getphylink();
if ((int8_t)tmp == -1) return -1;
*(uint8_t*)arg = tmp;
break;
default:
return -1;
}
return 0;
}
int8_t ctlnetwork(ctlnetwork_type cntype, void* arg) {
switch (cntype) {
case CN_SET_NETINFO: wizchip_setnetinfo((wiz_NetInfo*)arg); break;
case CN_GET_NETINFO: wizchip_getnetinfo((wiz_NetInfo*)arg); break;
case CN_SET_NETMODE: wizchip_setnetmode(*(netmode_type*)arg); break;
case CN_GET_NETMODE: *(netmode_type*)arg = wizchip_getnetmode(); break;
case CN_SET_TIMEOUT: wizchip_settimeout((wiz_NetTimeout*)arg); break;
case CN_GET_TIMEOUT: wizchip_gettimeout((wiz_NetTimeout*)arg); break;
case CN_SET_PREFER: setSLPSR(*(uint8_t*)arg); break;
case CN_GET_PREFER: *(uint8_t*)arg = getSLPSR(); break;
default: return -1;
}
return 0;
}
int8_t ctlnetservice(ctlnetservice_type cnstype, void* arg) {
switch (cnstype) {
case CNS_ARP: return wizchip_arp((wiz_ARP*)arg);
case CNS_PING: return wizchip_ping((wiz_PING*)arg);
case CNS_DAD: return wizchip_dad((uint8_t*)arg);
case CNS_SLAAC: return wizchip_slaac((wiz_Prefix*)arg);
case CNS_UNSOL_NA: return wizchip_unsolicited();
case CNS_GET_PREFIX: return wizchip_getprefix((wiz_Prefix*)arg);
default: return -1;
}
}
void wizchip_sw_reset() {
uint8_t gw[4], sn[4], sip[4], mac[6];
uint8_t gw6[16], sn6[16], lla[16], gua[16];
uint8_t islock = getSYSR();
CHIPUNLOCK();
getSHAR(mac); getGAR(gw); getSUBR(sn); getSIPR(sip);
getGA6R(gw6); getSUB6R(sn6); getLLAR(lla); getGUAR(gua);
setSYCR0(SYCR0_RST);
getSYCR0();
NETUNLOCK();
setSHAR(mac); setGAR(gw); setSUBR(sn); setSIPR(sip);
setGA6R(gw6); setSUB6R(sn6); setLLAR(lla); setGUAR(gua);
if (islock & SYSR_CHPL) CHIPLOCK();
if (islock & SYSR_NETL) NETLOCK();
}
int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize) {
wizchip_sw_reset();
if (txsize) {
int8_t tmp = 0;
for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) {
tmp += txsize[i];
if (tmp > 32) return -1;
}
for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) setSn_TXBUF_SIZE(i, txsize[i]);
}
if (rxsize) {
int8_t tmp = 0;
for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) {
tmp += rxsize[i];
if (tmp > 32) return -1;
}
for (int i = 0; i < WIZCHIP_SOCK_NUM; i++) setSn_RXBUF_SIZE(i, rxsize[i]);
}
return 0;
}
void wizchip_clrinterrupt(intr_kind intr) {
setIRCLR((uint8_t)intr);
uint8_t sir = (uint8_t)((uint16_t)intr >> 8);
for (int i = 0; i < WIZCHIP_SOCK_NUM; i++)
if (sir & (1 << i)) setSn_IRCLR(i, 0xFF);
setSLIRCLR((uint8_t)((uint32_t)intr >> 16));
}
intr_kind wizchip_getinterrupt() {
uint32_t ret = getSIR();
ret = (ret << 8) + getIR();
ret = (((uint32_t)getSLIR()) << 16) | ret;
return (intr_kind)ret;
}
void wizchip_setinterruptmask(intr_kind intr) {
setIMR((uint8_t)intr);
setSIMR((uint8_t)((uint16_t)intr >> 8));
setSLIMR((uint8_t)((uint32_t)intr >> 16));
}
intr_kind wizchip_getinterruptmask() {
uint32_t ret = getSIMR();
ret = (ret << 8) + getIMR();
ret = (((uint32_t)getSLIMR()) << 16) | ret;
return (intr_kind)ret;
}
int8_t wizphy_getphylink() {
if (wiz_mdio_read(PHYRAR_BMSR) & BMSR_LINK_STATUS) return PHY_LINK_ON;
return PHY_LINK_OFF;
}
int8_t wizphy_getphypmode() {
if (wiz_mdio_read(PHYRAR_BMCR) & BMCR_PWDN) return PHY_POWER_DOWN;
return PHY_POWER_NORM;
}
void wizphy_reset() {
wiz_mdio_write(PHYRAR_BMCR, wiz_mdio_read(PHYRAR_BMCR) | BMCR_RST);
while (wiz_mdio_read(PHYRAR_BMCR) & BMCR_RST);
}
void wizphy_setphyconf(wiz_PhyConf* phyconf) {
uint16_t tmp = wiz_mdio_read(PHYRAR_BMCR);
if (phyconf->mode == PHY_MODE_TE) {
setPHYCR1(getPHYCR1() | PHYCR1_TE);
setPHYCR0(PHYCR0_AUTO);
} else {
setPHYCR1(getPHYCR1() & ~PHYCR1_TE);
if (phyconf->mode == PHY_MODE_AUTONEGO) {
tmp |= BMCR_ANE;
} else {
tmp &= ~(BMCR_ANE | BMCR_DPX | BMCR_SPD);
if (phyconf->duplex == PHY_DUPLEX_FULL) tmp |= BMCR_DPX;
if (phyconf->speed == PHY_SPEED_100) tmp |= BMCR_SPD;
}
wiz_mdio_write(PHYRAR_BMCR, tmp);
}
}
void wizphy_getphyconf(wiz_PhyConf* phyconf) {
uint16_t tmp = wiz_mdio_read(PHYRAR_BMCR);
phyconf->mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & BMCR_ANE) ? PHY_MODE_AUTONEGO : PHY_MODE_MANUAL);
phyconf->duplex = (tmp & BMCR_DPX) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF;
phyconf->speed = (tmp & BMCR_SPD) ? PHY_SPEED_100 : PHY_SPEED_10;
}
void wizphy_getphystat(wiz_PhyConf* phyconf) {
uint8_t tmp = getPHYSR();
phyconf->mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & (1 << 5)) ? PHY_MODE_MANUAL : PHY_MODE_AUTONEGO);
phyconf->speed = (tmp & PHYSR_SPD) ? PHY_SPEED_10 : PHY_SPEED_100;
phyconf->duplex = (tmp & PHYSR_DPX) ? PHY_DUPLEX_HALF : PHY_DUPLEX_FULL;
}
void wizphy_setphypmode(uint8_t pmode) {
uint16_t tmp = wiz_mdio_read(PHYRAR_BMCR);
if (pmode == PHY_POWER_DOWN) tmp |= BMCR_PWDN;
else tmp &= ~BMCR_PWDN;
wiz_mdio_write(PHYRAR_BMCR, tmp);
}
void wizchip_setnetinfo(wiz_NetInfo* p) {
setSHAR(p->mac); setGAR(p->gw); setSUBR(p->sn); setSIPR(p->ip);
setGA6R(p->gw6); setSUB6R(p->sn6); setLLAR(p->lla); setGUAR(p->gua);
memcpy(dns_, p->dns, 4);
memcpy(dns6_, p->dns6, 16);
ipmode_ = p->ipmode;
}
void wizchip_getnetinfo(wiz_NetInfo* p) {
getSHAR(p->mac); getGAR(p->gw); getSUBR(p->sn); getSIPR(p->ip);
getGA6R(p->gw6); getSUB6R(p->sn6); getLLAR(p->lla); getGUAR(p->gua);
memcpy(p->dns, dns_, 4);
memcpy(p->dns6, dns6_, 16);
p->ipmode = ipmode_;
}
void wizchip_setnetmode(netmode_type netmode) {
uint32_t tmp = (uint32_t)netmode;
setNETMR((uint8_t)tmp);
setNETMR2((uint8_t)(tmp >> 8));
setNET4MR((uint8_t)(tmp >> 16));
setNET6MR((uint8_t)(tmp >> 24));
}
netmode_type wizchip_getnetmode() {
uint32_t ret = getNETMR();
ret = (ret << 8) + getNETMR2();
ret = (ret << 16) + getNET4MR();
ret = (ret << 24) + getNET6MR();
return (netmode_type)ret;
}
void wizchip_settimeout(wiz_NetTimeout* t) {
setRCR(t->s_retry_cnt); setRTR(t->s_time_100us);
setSLRCR(t->sl_retry_cnt); setSLRTR(t->sl_time_100us);
}
void wizchip_gettimeout(wiz_NetTimeout* t) {
t->s_retry_cnt = getRCR(); t->s_time_100us = getRTR();
t->sl_retry_cnt = getSLRCR(); t->sl_time_100us = getSLRTR();
}
int8_t wizchip_arp(wiz_ARP* arp) {
uint8_t tmp;
if (arp->destinfo.len == 16) { setSLDIP6R(arp->destinfo.ip); setSLCR(SLCR_ARP6); }
else { setSLDIP4R(arp->destinfo.ip); setSLCR(SLCR_ARP4); }
while (getSLCR());
while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA);
if (tmp & (SLIR_ARP4 | SLIR_ARP6)) { getSLDHAR(arp->dha); return 0; }
return -1;
}
int8_t wizchip_ping(wiz_PING* ping) {
uint8_t tmp;
setPINGIDR(ping->id); setPINGSEQR(ping->seq);
if (ping->destinfo.len == 16) { setSLDIP6R(ping->destinfo.ip); setSLCR(SLCR_PING6); }
else { setSLDIP4R(ping->destinfo.ip); setSLCR(SLCR_PING4); }
while (getSLCR());
while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA);
if (tmp & (SLIR_PING4 | SLIR_PING6)) return 0;
return -1;
}
int8_t wizchip_dad(uint8_t* ipv6) {
uint8_t tmp;
setSLDIP6R(ipv6); setSLCR(SLCR_NS);
while (getSLCR());
while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA);
if (tmp & SLIR_TOUT) return 0;
return -1;
}
int8_t wizchip_slaac(wiz_Prefix* prefix) {
uint8_t tmp;
setSLCR(SLCR_RS);
while (getSLCR());
while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA);
if (tmp & SLIR_RS) {
prefix->len = getPLR(); prefix->flag = getPFR();
prefix->valid_lifetime = getVLTR(); prefix->preferred_lifetime = getPLTR();
getPAR(prefix->prefix);
return 0;
}
return -1;
}
int8_t wizchip_unsolicited() {
uint8_t tmp;
setSLCR(SLCR_UNA);
while (getSLCR());
while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA);
if (tmp & SLIR_TOUT) return 0;
return -1;
}
int8_t wizchip_getprefix(wiz_Prefix* prefix) {
if (getSLIR() & SLIR_RA) {
prefix->len = getPLR(); prefix->flag = getPFR();
prefix->valid_lifetime = getVLTR(); prefix->preferred_lifetime = getPLTR();
getPAR(prefix->prefix);
setSLIRCLR(SLIR_RA);
}
return -1;
}