#include #include "wizchip_conf.h" void wizchip_cris_enter(void) {} void wizchip_cris_exit(void) {} void wizchip_cs_select(void) {} void wizchip_cs_deselect(void) {} iodata_t wizchip_bus_readdata(uint32_t AddrSel) { return *((volatile iodata_t *)((ptrdiff_t)AddrSel)); } void wizchip_bus_writedata(uint32_t AddrSel, iodata_t wb) { *((volatile iodata_t *)((ptrdiff_t)AddrSel)) = wb; } void wizchip_bus_read_buf(uint32_t AddrSel, iodata_t* buf, int16_t len, uint8_t addrinc) { if (addrinc) addrinc = sizeof(iodata_t); for (uint16_t i = 0; i < len; i++) { *buf++ = WIZCHIP.IF.BUS._read_data(AddrSel); AddrSel += (uint32_t)addrinc; } } void wizchip_bus_write_buf(uint32_t AddrSel, iodata_t* buf, int16_t len, uint8_t addrinc) { if (addrinc) addrinc = sizeof(iodata_t); for (uint16_t i = 0; i < len; i++) { WIZCHIP.IF.BUS._write_data(AddrSel, *buf++); AddrSel += (uint32_t)addrinc; } } uint8_t wizchip_spi_readbyte(void) { return 0; } void wizchip_spi_writebyte(uint8_t wb) {} void wizchip_spi_readburst(uint8_t* pBuf, uint16_t len) { for (uint16_t i = 0; i < len; i++) *pBuf++ = WIZCHIP.IF.SPI._read_byte(); } void wizchip_spi_writeburst(uint8_t* pBuf, uint16_t len) { for (uint16_t i = 0; i < len; i++) WIZCHIP.IF.SPI._write_byte(*pBuf++); } void wizchip_qspi_read(uint8_t opcode, uint16_t addr, uint8_t* pBuf, uint16_t len) {} void wizchip_qspi_write(uint8_t opcode, uint16_t addr, uint8_t* pBuf, uint16_t len) {} _WIZCHIP WIZCHIP = { _WIZCHIP_IO_MODE_, _WIZCHIP_ID_, { wizchip_cris_enter, wizchip_cris_exit }, { wizchip_cs_select, wizchip_cs_deselect }, { { wizchip_bus_readdata, wizchip_bus_writedata } } }; static uint8_t _DNS_[4]; static uint8_t _DNS6_[16]; static ipconf_mode _IPMODE_; void reg_wizchip_cris_cbfunc(void(*cris_en)(void), void(*cris_ex)(void)) { if (!cris_en || !cris_ex) { WIZCHIP.CRIS._enter = wizchip_cris_enter; WIZCHIP.CRIS._exit = wizchip_cris_exit; } else { WIZCHIP.CRIS._enter = cris_en; WIZCHIP.CRIS._exit = cris_ex; } } void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void)) { if (!cs_sel || !cs_desel) { WIZCHIP.CS._select = wizchip_cs_select; WIZCHIP.CS._deselect = wizchip_cs_deselect; } else { WIZCHIP.CS._select = cs_sel; WIZCHIP.CS._deselect = cs_desel; } } void reg_wizchip_bus_cbfunc(iodata_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, iodata_t wb)) { while (!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_BUS_)); if (!bus_rb || !bus_wb) { WIZCHIP.IF.BUS._read_data = wizchip_bus_readdata; WIZCHIP.IF.BUS._write_data = wizchip_bus_writedata; } else { WIZCHIP.IF.BUS._read_data = bus_rb; WIZCHIP.IF.BUS._write_data = bus_wb; } } void reg_wizchip_spi_cbfunc(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb)) { while (!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_SPI_)); if (!spi_rb || !spi_wb) { WIZCHIP.IF.SPI._read_byte = wizchip_spi_readbyte; WIZCHIP.IF.SPI._write_byte = wizchip_spi_writebyte; } else { WIZCHIP.IF.SPI._read_byte = spi_rb; WIZCHIP.IF.SPI._write_byte = spi_wb; } } void reg_wizchip_spiburst_cbfunc(void (*spi_rb)(uint8_t* pBuf, uint16_t len), void (*spi_wb)(uint8_t* pBuf, uint16_t len)) { while (!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_SPI_)); if (!spi_rb || !spi_wb) { WIZCHIP.IF.SPI._read_burst = wizchip_spi_readburst; WIZCHIP.IF.SPI._write_burst = wizchip_spi_writeburst; } else { WIZCHIP.IF.SPI._read_burst = spi_rb; WIZCHIP.IF.SPI._write_burst = spi_wb; } } void reg_wizchip_qspi_cbfunc(void (*qspi_rb)(uint8_t opcode, uint16_t addr, uint8_t* pBuf, uint16_t len), void (*qspi_wb)(uint8_t opcode, uint16_t addr, uint8_t* pBuf, uint16_t len)) { while (!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_SPI_QSPI_)); if (!qspi_rb || !qspi_wb) { WIZCHIP.IF.QSPI._read_qspi = wizchip_qspi_read; WIZCHIP.IF.QSPI._write_qspi = wizchip_qspi_write; } else { WIZCHIP.IF.QSPI._read_qspi = qspi_rb; WIZCHIP.IF.QSPI._write_qspi = qspi_wb; } } 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 != 0) { 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: ((uint8_t*)arg)[0] = WIZCHIP.id[0]; ((uint8_t*)arg)[1] = WIZCHIP.id[1]; ((uint8_t*)arg)[2] = WIZCHIP.id[2]; ((uint8_t*)arg)[3] = WIZCHIP.id[3]; ((uint8_t*)arg)[4] = WIZCHIP.id[4]; ((uint8_t*)arg)[5] = WIZCHIP.id[5]; ((uint8_t*)arg)[6] = 0; 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(void) { uint8_t gw[4], sn[4], sip[4]; uint8_t 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) { int8_t i; int8_t tmp = 0; wizchip_sw_reset(); if (txsize) { tmp = 0; for (i = 0; i < _WIZCHIP_SOCK_NUM_; i++) { tmp += txsize[i]; if (tmp > 32) return -1; } for (i = 0; i < _WIZCHIP_SOCK_NUM_; i++) { setSn_TXBUF_SIZE(i, txsize[i]); } } if (rxsize) { tmp = 0; for (i = 0; i < _WIZCHIP_SOCK_NUM_; i++) { tmp += rxsize[i]; if (tmp > 32) return -1; } for (i = 0; i < _WIZCHIP_SOCK_NUM_; i++) { setSn_RXBUF_SIZE(i, rxsize[i]); } } return 0; } void wizchip_clrinterrupt(intr_kind intr) { uint8_t ir = (uint8_t)intr; uint8_t sir = (uint8_t)((uint16_t)intr >> 8); uint8_t slir = (uint8_t)((uint32_t)intr >> 16); setIRCLR(ir); for (int i = 0; i < _WIZCHIP_SOCK_NUM_; i++) { if (sir & (1 << i)) setSn_IRCLR(i, 0xFF); } setSLIRCLR(slir); } intr_kind wizchip_getinterrupt(void) { uint8_t ir = getIR(); uint8_t sir = getSIR(); uint32_t ret = sir; ret = (ret << 8) + ir; ret = (((uint32_t)getSLIR()) << 16) | ret; return (intr_kind)ret; } void wizchip_setinterruptmask(intr_kind intr) { uint8_t imr = (uint8_t)intr; uint8_t simr = (uint8_t)((uint16_t)intr >> 8); uint8_t slimr = (uint8_t)((uint32_t)intr >> 16); setIMR(imr); setSIMR(simr); setSLIMR(slimr); } intr_kind wizchip_getinterruptmask(void) { uint8_t imr = getIMR(); uint8_t simr = getSIMR(); uint32_t ret = simr; ret = (ret << 8) + imr; ret = (((uint32_t)getSLIMR()) << 16) | ret; return (intr_kind)ret; } int8_t wizphy_getphylink(void) { #if (_PHY_IO_MODE_ == _PHY_IO_MODE_PHYCR_) return (getPHYSR() & PHYSR_LNK); #elif (_PHY_IO_MODE_ == _PHY_IO_MODE_MII_) if (wiz_mdio_read(PHYRAR_BMSR) & BMSR_LINK_STATUS) return PHY_LINK_ON; return PHY_LINK_OFF; #endif } int8_t wizphy_getphypmode(void) { #if (_PHY_IO_MODE_ == _PHY_IO_MODE_PHYCR_) if (getPHYCR1() & PHYCR1_PWDN) return PHY_POWER_DOWN; #elif (_PHY_IO_MODE_ == _PHY_IO_MODE_MII_) if (wiz_mdio_read(PHYRAR_BMCR) & BMCR_PWDN) return PHY_POWER_DOWN; #endif return PHY_POWER_NORM; } void wizphy_reset(void) { #if (_PHY_IO_MODE_ == _PHY_IO_MODE_PHYCR_) uint8_t tmp = getPHYCR1() | PHYCR1_RST; PHYUNLOCK(); setPHYCR1(tmp); PHYLOCK(); #elif (_PHY_IO_MODE_ == _PHY_IO_MODE_MII_) wiz_mdio_write(PHYRAR_BMCR, wiz_mdio_read(PHYRAR_BMCR) | BMCR_RST); while (wiz_mdio_read(PHYRAR_BMCR) & BMCR_RST); #endif } void wizphy_setphyconf(wiz_PhyConf* phyconf) { #if (_PHY_IO_MODE_ == _PHY_IO_MODE_PHYCR_) uint8_t tmp = 0; if (phyconf->mode == PHY_MODE_TE) { setPHYCR1(getPHYCR1() | PHYCR1_TE); tmp = PHYCR0_AUTO; } else { setPHYCR1(getPHYCR1() & ~PHYCR1_TE); if (phyconf->mode == PHY_MODE_AUTONEGO) { tmp = PHYCR0_AUTO; } else { tmp |= 0x04; if (phyconf->speed == PHY_SPEED_10) tmp |= 0x02; if (phyconf->duplex == PHY_DUPLEX_HALF) tmp |= 0x01; } } setPHYCR0(tmp); #elif (_PHY_IO_MODE_ == _PHY_IO_MODE_MII_) 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); } #endif } void wizphy_getphyconf(wiz_PhyConf* phyconf) { #if (_PHY_IO_MODE_ == _PHY_IO_MODE_PHYCR_) uint8_t tmp = getPHYSR(); phyconf->mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & (1 << 5)) ? PHY_MODE_MANUAL : PHY_MODE_AUTONEGO); phyconf->speed = (tmp & (1 << 4)) ? PHY_SPEED_10 : PHY_SPEED_100; phyconf->duplex = (tmp & (1 << 3)) ? PHY_DUPLEX_HALF : PHY_DUPLEX_FULL; #elif (_PHY_IO_MODE_ == _PHY_IO_MODE_MII_) 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; #endif } 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) { #if (_PHY_IO_MODE_ == _PHY_IO_MODE_PHYCR_) uint8_t tmp = getPHYCR1(); if (pmode == PHY_POWER_DOWN) tmp |= PHYCR1_PWDN; else tmp &= ~PHYCR1_PWDN; setPHYCR1(tmp); #elif (_PHY_IO_MODE_ == _PHY_IO_MODE_MII_) 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); #endif } void wizchip_setnetinfo(wiz_NetInfo* pnetinfo) { setSHAR(pnetinfo->mac); setGAR(pnetinfo->gw); setSUBR(pnetinfo->sn); setSIPR(pnetinfo->ip); setGA6R(pnetinfo->gw6); setSUB6R(pnetinfo->sn6); setLLAR(pnetinfo->lla); setGUAR(pnetinfo->gua); for (uint8_t i = 0; i < 4; i++) _DNS_[i] = pnetinfo->dns[i]; for (uint8_t i = 0; i < 16; i++) _DNS6_[i] = pnetinfo->dns6[i]; _IPMODE_ = pnetinfo->ipmode; } void wizchip_getnetinfo(wiz_NetInfo* pnetinfo) { getSHAR(pnetinfo->mac); getGAR(pnetinfo->gw); getSUBR(pnetinfo->sn); getSIPR(pnetinfo->ip); getGA6R(pnetinfo->gw6); getSUB6R(pnetinfo->sn6); getLLAR(pnetinfo->lla); getGUAR(pnetinfo->gua); for (uint8_t i = 0; i < 4; i++) pnetinfo->dns[i] = _DNS_[i]; for (uint8_t i = 0; i < 16; i++) pnetinfo->dns6[i] = _DNS6_[i]; pnetinfo->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(void) { 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* nettime) { setRCR(nettime->s_retry_cnt); setRTR(nettime->s_time_100us); setSLRCR(nettime->sl_retry_cnt); setSLRTR(nettime->sl_time_100us); } void wizchip_gettimeout(wiz_NetTimeout* nettime) { nettime->s_retry_cnt = getRCR(); nettime->s_time_100us = getRTR(); nettime->sl_retry_cnt = getSLRCR(); nettime->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(void) { 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; }