/*****************************************************/ // W5500_config.c // brief WIZCHIP Config // version 1.0.1 // date 2013/10/21 /*****************************************************/ #include #include #include #include "w5500_spi.h" #include "w5500_config.h" #include "w5500.h" static uint8_t _DNS_[4]; // DNS server ip address static dhcp_mode _DHCP_; // DHCP mode /* FUNCTION **********************************/ int8_t ctlwizchip (ctlwizchip_type cwtype, void* arg) { uint8_t* ptmp[2] = {0,0}; uint8_t tmp; switch(cwtype) { case CW_RESET_WIZCHIP: w5500cfg_sw_reset(); break; case CW_INIT_WIZCHIP: if(arg != 0) { ptmp[0] = (uint8_t*)arg; ptmp[1] = ptmp[0] + _WIZCHIP_SOCK_NUM_; } return w5500cfg_init (ptmp[0], ptmp[1]); case CW_CLR_INTERRUPT: w5500cfg_clrinterrupt(*((intr_kind*)arg)); break; case CW_GET_INTERRUPT: *((intr_kind*)arg) = w5500cfg_getinterrupt(); break; case CW_SET_INTRMASK: w5500cfg_setinterruptmask(*((intr_kind*)arg)); break; case CW_GET_INTRMASK: *((intr_kind*)arg) = w5500cfg_getinterruptmask(); break; case CW_SET_INTRTIME: setINTLEVEL(*(uint16_t*)arg); break; case CW_GET_INTRTIME: //*(uint16_t*)arg = getINTLEVEL(); break; case CW_GET_ID: ((uint8_t*)arg)[0] = 'W'; ((uint8_t*)arg)[1] = '5'; ((uint8_t*)arg)[2] = '5'; ((uint8_t*)arg)[3] = '0'; ((uint8_t*)arg)[4] = '0'; ((uint8_t*)arg)[5] = 0; 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: return wizphy_setphypmode(*(uint8_t*)arg); 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; } /* --- ctlwizchip --- */ /* FUNCTION **********************************/ int8_t ctlnetwork (ctlnetwork_type cntype, void* arg) { switch (cntype) { case CN_SET_NETINFO: w5500cfg_setnetinfo((wiz_NetInfo*)arg); break; case CN_GET_NETINFO: w5500cfg_getnetinfo((wiz_NetInfo*)arg); break; case CN_SET_NETMODE: return w5500cfg_setnetmode(*(netmode_type*)arg); case CN_GET_NETMODE: *(netmode_type*)arg = w5500cfg_getnetmode(); break; case CN_SET_TIMEOUT: w5500cfg_settimeout((wiz_NetTimeout*)arg); break; case CN_GET_TIMEOUT: w5500cfg_gettimeout((wiz_NetTimeout*)arg); break; default: return -1; } return 0; } /* --- ctlnetwork --- */ /* FUNCTION **********************************/ void w5500cfg_sw_reset (void) { uint8_t gw[4], sn[4], sip[4]; uint8_t mac[6]; getSHAR(mac); getGAR(gw); getSUBR(sn); getSIPR(sip); setMR(MR_RST); getMR(); // for delay setSHAR(mac); setGAR(gw); setSUBR(sn); setSIPR(sip); } /* --- w5500cfg_sw_reset --- */ /* FUNCTION **********************************/ int8_t w5500cfg_init (uint8_t* txsize, uint8_t* rxsize) { int8_t i; int8_t tmp = 0; w5500cfg_sw_reset (); if (txsize) { tmp = 0; for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) { tmp += txsize[i]; if(tmp > 16) 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 > 16) return -1; } for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++) setSn_RXBUF_SIZE(i, rxsize[i]); } return 0; } /* --- w5500cfg_init --- */ /* FUNCTION **********************************/ void w5500cfg_clrinterrupt (intr_kind intr) { uint8_t ir = (uint8_t)intr; uint8_t sir = (uint8_t)((uint16_t)intr >> 8); setIR(ir); setSIR(sir); } /* --- w5500cfg_clrinterrupt --- */ /* FUNCTION **********************************/ intr_kind w5500cfg_getinterrupt (void) { uint8_t ir = 0; uint8_t sir = 0; uint16_t ret = 0; ir = getIR(); sir = getSIR(); ret = sir; ret = (ret << 8) + ir; return (intr_kind)ret; } /* --- w5500cfg_getinterrupt --- */ /* FUNCTION **********************************/ void w5500cfg_setinterruptmask (intr_kind intr) { uint8_t imr = (uint8_t)intr; uint8_t simr = (uint8_t)((uint16_t)intr >> 8); setIMR(imr); setSIMR(simr); } /* --- w5500cfg_setinterruptmask --- */ /* FUNCTION **********************************/ intr_kind w5500cfg_getinterruptmask (void) { uint8_t imr = 0; uint8_t simr = 0; uint16_t ret = 0; imr = getIMR(); simr = getSIMR(); ret = simr; ret = (ret << 8) + imr; return (intr_kind)ret; } /* --- w5500cfg_getinterruptmask --- */ /* FUNCTION **********************************/ int8_t wizphy_getphylink (void) { int8_t tmp; if (getPHYCFGR() & PHYCFGR_LNK_ON) tmp = PHY_LINK_ON; else tmp = PHY_LINK_OFF; return tmp; } /* --- wizphy_getphylink --- */ /* FUNCTION **********************************/ int8_t wizphy_getphypmode (void) { int8_t tmp = 0; if(getPHYCFGR() & PHYCFGR_OPMDC_PDOWN) tmp = PHY_POWER_DOWN; else tmp = PHY_POWER_NORM; return tmp; } /* --- wizphy_getphypmode --- */ /* FUNCTION **********************************/ void wizphy_reset (void) { uint8_t tmp = getPHYCFGR(); tmp &= PHYCFGR_RST; setPHYCFGR(tmp); tmp = getPHYCFGR(); tmp |= ~PHYCFGR_RST; setPHYCFGR(tmp); } /* --- wizphy_reset --- */ /* FUNCTION **********************************/ void wizphy_setphyconf (wiz_PhyConf* phyconf) { uint8_t tmp = 0; if (phyconf->by == PHY_CONFBY_SW) tmp |= PHYCFGR_OPMD; else tmp &= ~PHYCFGR_OPMD; if (phyconf->mode == PHY_MODE_AUTONEGO) tmp |= PHYCFGR_OPMDC_ALLA; else { if (phyconf->duplex == PHY_DUPLEX_FULL) { if(phyconf->speed == PHY_SPEED_100) tmp |= PHYCFGR_OPMDC_100F; else tmp |= PHYCFGR_OPMDC_10F; } else { if (phyconf->speed == PHY_SPEED_100) tmp |= PHYCFGR_OPMDC_100H; else tmp |= PHYCFGR_OPMDC_10H; } } setPHYCFGR(tmp); wizphy_reset (); } /* --- wizphy_setphyconf --- */ /* FUNCTION **********************************/ void wizphy_getphyconf (wiz_PhyConf* phyconf) { uint8_t tmp = 0; tmp = getPHYCFGR(); phyconf->by = (tmp & PHYCFGR_OPMD) ? PHY_CONFBY_SW : PHY_CONFBY_HW; switch(tmp & PHYCFGR_OPMDC_ALLA) { case PHYCFGR_OPMDC_ALLA: case PHYCFGR_OPMDC_100FA: phyconf->mode = PHY_MODE_AUTONEGO; break; default: phyconf->mode = PHY_MODE_MANUAL; break; } switch(tmp & PHYCFGR_OPMDC_ALLA) { case PHYCFGR_OPMDC_100FA: case PHYCFGR_OPMDC_100F: case PHYCFGR_OPMDC_100H: phyconf->speed = PHY_SPEED_100; break; default: phyconf->speed = PHY_SPEED_10; break; } switch(tmp & PHYCFGR_OPMDC_ALLA) { case PHYCFGR_OPMDC_100FA: case PHYCFGR_OPMDC_100F: case PHYCFGR_OPMDC_10F: phyconf->duplex = PHY_DUPLEX_FULL; break; default: phyconf->duplex = PHY_DUPLEX_HALF; break; } } /* --- wizphy_getphyconf --- */ /* FUNCTION **********************************/ void wizphy_getphystat (wiz_PhyConf* phyconf) { uint8_t tmp = getPHYCFGR(); phyconf->duplex = (tmp & PHYCFGR_DPX_FULL) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF; phyconf->speed = (tmp & PHYCFGR_SPD_100) ? PHY_SPEED_100 : PHY_SPEED_10; } /* --- wizphy_getphystat --- */ /* FUNCTION **********************************/ int8_t wizphy_setphypmode (uint8_t pmode) { uint8_t tmp = 0; tmp = getPHYCFGR(); if ((tmp & PHYCFGR_OPMD)== 0) return -1; tmp &= ~PHYCFGR_OPMDC_ALLA; if ( pmode == PHY_POWER_DOWN) tmp |= PHYCFGR_OPMDC_PDOWN; else tmp |= PHYCFGR_OPMDC_ALLA; setPHYCFGR(tmp); wizphy_reset(); tmp = getPHYCFGR(); if (pmode == PHY_POWER_DOWN) { if (tmp & PHYCFGR_OPMDC_PDOWN) return 0; } else { if (tmp & PHYCFGR_OPMDC_ALLA) return 0; } return -1; } /* --- wizphy_setphypmode --- */ /* FUNCTION **********************************/ void w5500cfg_setnetinfo (wiz_NetInfo* pnetinfo) { setSHAR(pnetinfo->mac); setGAR(pnetinfo->gw); setSUBR(pnetinfo->sn); setSIPR(pnetinfo->ip); _DNS_[0] = pnetinfo->dns[0]; _DNS_[1] = pnetinfo->dns[1]; _DNS_[2] = pnetinfo->dns[2]; _DNS_[3] = pnetinfo->dns[3]; _DHCP_ = pnetinfo->dhcp; } /* --- w5500cfg_setnetinfo --- */ /* FUNCTION **********************************/ void w5500cfg_getnetinfo (wiz_NetInfo* pnetinfo) { getSHAR(pnetinfo->mac); getGAR(pnetinfo->gw); getSUBR(pnetinfo->sn); getSIPR(pnetinfo->ip); pnetinfo->dns[0]= _DNS_[0]; pnetinfo->dns[1]= _DNS_[1]; pnetinfo->dns[2]= _DNS_[2]; pnetinfo->dns[3]= _DNS_[3]; pnetinfo->dhcp = _DHCP_; } /* --- w5500cfg_getnetinfo --- */ /* FUNCTION **********************************/ int8_t w5500cfg_setnetmode (netmode_type netmode) { uint8_t tmp = 0; if(netmode & ~(NM_WAKEONLAN | NM_PPPOE | NM_PINGBLOCK | NM_FORCEARP)) return -1; tmp = getMR(); tmp |= (uint8_t)netmode; setMR(tmp); return 0; } /* --- w5500cfg_setnetmode --- */ /* FUNCTION **********************************/ netmode_type w5500cfg_getnetmode (void) { return (netmode_type) getMR(); } /* --- w5500cfg_getnetmode --- */ /* FUNCTION **********************************/ void w5500cfg_settimeout (wiz_NetTimeout* nettime) { setRCR(nettime->retry_cnt); setRTR(nettime->time_100us); } /* --- w5500cfg_settimeout --- */ /* FUNCTION **********************************/ void w5500cfg_gettimeout (wiz_NetTimeout* nettime) { nettime->retry_cnt = getRCR(); nettime->time_100us = getRTR(); } /* --- w5500cfg_gettimeout --- */ /* ----------------------------------------------*/ /* -------- End of file W5500_config.c ------- */ /* ----------------------------------------------*/