petfeeder st ver
Fork of WIZnetInterface by
Revision 30:a5c77f065ab7, committed 2017-06-09
- Comitter:
- justinkim
- Date:
- Fri Jun 09 04:44:10 2017 +0000
- Parent:
- 29:c91884bd2713
- Commit message:
- petfeeder st ver
Changed in this revision
--- a/EthernetInterface.cpp Tue Nov 17 06:35:55 2015 +0000 +++ b/EthernetInterface.cpp Fri Jun 09 04:44:10 2017 +0000 @@ -19,7 +19,7 @@ #include "EthernetInterface.h" #include "DHCPClient.h" -#if (not defined TARGET_WIZwiki_W7500) && (not defined TARGET_WIZwiki_W7500P) && (not defined TARGET_WIZwiki_W7500ECO) +//#if (not defined TARGET_WIZwiki_W7500) && (not defined TARGET_WIZwiki_W7500P) && (not defined TARGET_WIZwiki_W7500ECO) EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) : WIZnet_Chip(mosi, miso, sclk, cs, reset) { @@ -31,7 +31,7 @@ { ip_set = false; } -#endif +//#endif int EthernetInterface::init() {
--- a/EthernetInterface.h Tue Nov 17 06:35:55 2015 +0000 +++ b/EthernetInterface.h Fri Jun 09 04:44:10 2017 +0000 @@ -24,7 +24,7 @@ class EthernetInterface: public WIZnet_Chip { public: -#if (not defined TARGET_WIZwiki_W7500) && (not defined TARGET_WIZwiki_W7500P) && (not defined TARGET_WIZwiki_W7500ECO) +//#if (not defined TARGET_WIZwiki_W7500) && (not defined TARGET_WIZwiki_W7500P) && (not defined TARGET_WIZwiki_W7500ECO) /** * Constructor @@ -37,7 +37,7 @@ */ EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); EthernetInterface(SPI* spi, PinName cs, PinName reset); -#endif +//#endif /** Initialize the interface with DHCP. * Initialize the interface and configure it to use DHCP (no connection at this point).
--- a/arch/ext/W5500.cpp Tue Nov 17 06:35:55 2015 +0000 +++ b/arch/ext/W5500.cpp Fri Jun 09 04:44:10 2017 +0000 @@ -28,7 +28,7 @@ //Debug is disabled by default #if 0 #define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0); -//#define DBG(x, ...) debug("[W5500:DBG]"x"\r\n", ##__VA_ARGS__); +#define DBG(x, ...) debug("[W5500:DBG]"x"\r\n", ##__VA_ARGS__); #define WARN(x, ...) debug("[W5500:WARN]"x"\r\n", ##__VA_ARGS__); #define ERR(x, ...) debug("[W5500:ERR]"x"\r\n", ##__VA_ARGS__); #else
--- a/arch/int/W7500x_toe.cpp Tue Nov 17 06:35:55 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,558 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#include "eth_arch.h" -#if defined(TARGET_WIZwiki_W7500) || defined(TARGET_WIZwiki_W7500P) || defined(TARGET_WIZwiki_W7500ECO) - - -#include "mbed.h" -#include "mbed_debug.h" -#include "DNSClient.h" - - -/* - * MDIO via GPIO - * mdio via gpio is supported and related functions as follows. - * - mdio_init(),mdio_read(),mdio_write() - * - input_MDIO(),output_MDIO(),turnaroud_MDIO(),idle_MDIO() - * called by ethernet_link() and ethernet_set_link() - */ - -#if defined (TARGET_WIZwiki_W7500) || defined(TARGET_WIZwiki_W7500ECO) - -#define MDIO GPIO_Pin_14 -#define MDC GPIO_Pin_15 -#define GPIO_MDC GPIOB -#define PHY_ADDR_IP101G 0x07 -#define PHY_ADDR PHY_ADDR_IP101G -#define SVAL 0x2 //right shift val = 2 -#define PHYREG_CONTROL 0x0 //Control Register address (Contorl basic register) -#define PHYREG_STATUS 0x1 //Status Register address (Status basic register) -#define CNTL_DUPLEX (0x01ul<< 7) -#define CNTL_AUTONEGO (0x01ul<<11) -#define CNTL_SPEED (0x01ul<<12) -#define MDC_WAIT (1) - -#elif defined (TARGET_WIZwiki_W7500P) - -#define MDIO GPIO_Pin_15 -#define MDC GPIO_Pin_14 -#define GPIO_MDC GPIOB -#define PHY_ADDR_IP101G 0x01 -#define PHY_ADDR PHY_ADDR_IP101G -#define SVAL 0x2 //right shift val = 2 -#define PHYREG_CONTROL 0x0 //Control Register address (Contorl basic register) -#define PHYREG_STATUS 0x1 //Status Register address (Status basic register) -#define CNTL_DUPLEX (0x01ul<< 7) -#define CNTL_AUTONEGO (0x01ul<<11) -#define CNTL_SPEED (0x01ul<<12) -#define MDC_WAIT (1) - -#endif - -void mdio_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin_MDC, uint16_t GPIO_Pin_MDIO); -void mdio_write(GPIO_TypeDef* GPIOx, uint32_t PhyRegAddr, uint32_t val); -uint32_t mdio_read(GPIO_TypeDef* GPIOx, uint32_t PhyRegAddr); - -WIZnet_Chip* WIZnet_Chip::inst; - -WIZnet_Chip::WIZnet_Chip() -{ - inst = this; -} - -bool WIZnet_Chip::setmac() -{ - reg_wr_mac(SHAR, mac); - return true; -} - -// Set the IP -bool WIZnet_Chip::setip() -{ - reg_wr<uint32_t>(SIPR, ip); - reg_wr<uint32_t>(GAR, gateway); - reg_wr<uint32_t>(SUBR, netmask); - return true; -} - -bool WIZnet_Chip::setProtocol(int socket, Protocol p) -{ - if (socket < 0) { - return false; - } - sreg<uint8_t>(socket, Sn_MR, p); - return true; -} - -bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms) -{ - if (socket < 0) { - return false; - } - sreg<uint8_t>(socket, Sn_MR, TCP); - scmd(socket, OPEN); - sreg_ip(socket, Sn_DIPR, host); - sreg<uint16_t>(socket, Sn_DPORT, port); - sreg<uint16_t>(socket, Sn_PORT, new_port()); - scmd(socket, CONNECT); - Timer t; - t.reset(); - t.start(); - while(!is_connected(socket)) { - if (t.read_ms() > timeout_ms) { - return false; - } - } - return true; -} - -bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip) -{ - uint32_t addr = str_to_ip(host); - char buf[17]; - snprintf(buf, sizeof(buf), "%d.%d.%d.%d", - (uint8_t)((addr>>24)&0xff), - (uint8_t)((addr>>16)&0xff), - (uint8_t)((addr>>8)&0xff), - (uint8_t)(addr&0xff)); - if (strcmp(buf, host) == 0) { - *ip = addr; - return true; - } - DNSClient client; - if(client.lookup(host)) { - *ip = client.ip; - return true; - } - return false; -} - - -bool WIZnet_Chip::is_connected(int socket) -{ - /* - if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) { - return true; - } - */ - uint8_t tmpSn_SR; - tmpSn_SR = sreg<uint8_t>(socket, Sn_SR); - // packet sending is possible, when state is SOCK_CLOSE_WAIT. - if ((tmpSn_SR == SOCK_ESTABLISHED) || (tmpSn_SR == SOCK_CLOSE_WAIT)) { - return true; - } - return false; -} -// Reset the chip & set the buffer -void WIZnet_Chip::reset() -{ - /* S/W Reset PHY */ - mdio_write(GPIO_MDC, PHYREG_CONTROL, 0x8000); - wait_ms(10);//for S/W reset - wait_ms(10);//for MDC I/F RDY - - mdio_init(GPIO_MDC, MDC, MDIO); - - /* S/W Reset WZTOE */ - reg_wr<uint8_t>(MR, MR_RST); - // set PAD strengh and pull-up for TXD[3:0] and TXE -#ifdef __DEF_USED_IC101AG__ //For using IC+101AG - -#if defined(TARGET_WIZwiki_W7500) || defined(TARGET_WIZwiki_W7500ECO) - - *(volatile uint32_t *)(0x41003068) = 0x64; //TXD0 - *(volatile uint32_t *)(0x4100306C) = 0x64; //TXD1 - *(volatile uint32_t *)(0x41003070) = 0x64; //TXD2 - *(volatile uint32_t *)(0x41003074) = 0x64; //TXD3 - *(volatile uint32_t *)(0x41003050) = 0x64; //TXE -#endif - -#endif - - // set ticker counter - reg_wr<uint32_t>(TIC100US, (SystemCoreClock/10000)); - // write MAC address inside the WZTOE MAC address register - reg_wr_mac(SHAR, mac); - /* - * set RX and TX buffer size - * for (int socket = 0; socket < MAX_SOCK_NUM; socket++) { - * sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2); - * sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2); - * } - */ -} - - -bool WIZnet_Chip::close(int socket) -{ - if (socket < 0) { - return false; - } - // if SOCK_CLOSED, return - if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) { - return true; - } - // if SOCK_ESTABLISHED, send FIN-Packet to peer - if (sreg<uint8_t>(socket, Sn_MR) == TCP) { - scmd(socket, DISCON); - } - // close socket - scmd(socket, CLOSE); - // clear Socket Interrupt Register - sreg<uint8_t>(socket, Sn_ICR, 0xff); - return true; -} - -int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size) -{ - if (socket < 0) { - return -1; - } - Timer t; - t.reset(); - t.start(); - while(1) { - int size = sreg<uint16_t>(socket, Sn_RX_RSR); - if (size > req_size) { - return size; - } - if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { - break; - } - } - return -1; -} - -int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size) -{ - if (socket < 0) { - return -1; - } - Timer t; - t.reset(); - t.start(); - while(1) { - int size = sreg<uint16_t>(socket, Sn_TX_FSR); - if (size > req_size) { - return size; - } - if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { - break; - } - } - return -1; -} - -int WIZnet_Chip::send(int socket, const char * str, int len) -{ - if (socket < 0) { - return -1; - } - - uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR); - uint32_t sn_tx_base = W7500x_TXMEM_BASE + (uint32_t)(socket<<18); - - for(int i=0; i<len; i++) - *(volatile uint8_t *)(sn_tx_base + ((ptr+i)&0xFFFF)) = str[i]; - - sreg<uint16_t>(socket, Sn_TX_WR, ptr + len); - scmd(socket, SEND); - - uint8_t tmp_Sn_IR; - while (( (tmp_Sn_IR = sreg<uint8_t>(socket, Sn_IR)) & INT_SEND_OK) != INT_SEND_OK) { - // @Jul.10, 2014 fix contant name, and udp sendto function. - switch (sreg<uint8_t>(socket, Sn_SR)) { - case SOCK_CLOSED : - close(socket); - return 0; - //break; - case SOCK_UDP : - // ARP timeout is possible. - if ((tmp_Sn_IR & INT_TIMEOUT) == INT_TIMEOUT) { - sreg<uint8_t>(socket, Sn_ICR, INT_TIMEOUT); - return 0; - } - break; - default : - break; - } - } - - sreg<uint8_t>(socket, Sn_ICR, INT_SEND_OK); - - return len; -} - -int WIZnet_Chip::recv(int socket, char* buf, int len) -{ - if (socket < 0) { - return -1; - } - uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD); - uint32_t sn_rx_base = W7500x_RXMEM_BASE + (uint32_t)(socket<<18); - - for(int i=0; i<len; i++) - buf[i] = *(volatile uint8_t *)(sn_rx_base + ((ptr+i)&0xFFFF)); - - sreg<uint16_t>(socket, Sn_RX_RD, ptr + len); - scmd(socket, RECV); - - return len; -} - -int WIZnet_Chip::new_socket() -{ - for(int s = 0; s < MAX_SOCK_NUM; s++) { - if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) { - return s; - } - } - return -1; -} - -uint16_t WIZnet_Chip::new_port() -{ - uint16_t port = rand(); - port |= 49152; - return port; -} - -bool WIZnet_Chip::link(int wait_time_ms) -{ - Timer t; - t.reset(); - t.start(); - while(1) { - int is_link = ethernet_link(); - - if (is_link) { - return true; - } - if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { - break; - } - } - return 0; -} - -void WIZnet_Chip::set_link(PHYMode phymode) -{ - int speed = -1; - int duplex = 0; - - switch(phymode) { - case AutoNegotiate : speed = -1; duplex = 0; break; - case HalfDuplex10 : speed = 0; duplex = 0; break; - case FullDuplex10 : speed = 0; duplex = 1; break; - case HalfDuplex100 : speed = 1; duplex = 0; break; - case FullDuplex100 : speed = 1; duplex = 1; break; - } - - ethernet_set_link(speed, duplex); -} - -uint32_t str_to_ip(const char* str) -{ - uint32_t ip = 0; - char* p = (char*)str; - for(int i = 0; i < 4; i++) { - ip |= atoi(p); - p = strchr(p, '.'); - if (p == NULL) { - break; - } - ip <<= 8; - p++; - } - return ip; -} - -void printfBytes(char* str, uint8_t* buf, int len) -{ - printf("%s %d:", str, len); - for(int i = 0; i < len; i++) { - printf(" %02x", buf[i]); - } - printf("\n"); -} - -void printHex(uint8_t* buf, int len) -{ - for(int i = 0; i < len; i++) { - if ((i%16) == 0) { - printf("%p", buf+i); - } - printf(" %02x", buf[i]); - if ((i%16) == 15) { - printf("\n"); - } - } - printf("\n"); -} - -void debug_hex(uint8_t* buf, int len) -{ - for(int i = 0; i < len; i++) { - if ((i%16) == 0) { - debug("%p", buf+i); - } - debug(" %02x", buf[i]); - if ((i%16) == 15) { - debug("\n"); - } - } - debug("\n"); -} - -void WIZnet_Chip::scmd(int socket, Command cmd) -{ - sreg<uint8_t>(socket, Sn_CR, cmd); - while(sreg<uint8_t>(socket, Sn_CR)); -} - - -void mdio_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin_MDC, uint16_t GPIO_Pin_MDIO) -{ - /* Set GPIOs for MDIO and MDC */ - GPIO_InitTypeDef MDIO_InitDef; - HAL_PAD_AFConfig(PAD_PB, GPIO_Pin_MDIO, PAD_AF1); - HAL_PAD_AFConfig(PAD_PB, GPIO_Pin_MDC, PAD_AF1); - MDIO_InitDef.GPIO_Pin = GPIO_Pin_MDC | GPIO_Pin_MDIO; - MDIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; - HAL_GPIO_Init(GPIOx, &MDIO_InitDef); -} - -void output_MDIO(GPIO_TypeDef* GPIOx, uint32_t val, uint32_t n) -{ - for(val <<= (32-n); n; val<<=1, n--) - { - if(val & 0x80000000) - HAL_GPIO_SetBits(GPIOx, MDIO); - else - HAL_GPIO_ResetBits(GPIOx, MDIO); - - wait_ms(MDC_WAIT); - HAL_GPIO_SetBits(GPIOx, MDC); - wait_ms(MDC_WAIT); - HAL_GPIO_ResetBits(GPIOx, MDC); - } -} - -uint32_t input_MDIO( GPIO_TypeDef* GPIOx ) -{ - uint32_t i, val=0; - for(i=0; i<16; i++) - { - val <<=1; - HAL_GPIO_SetBits(GPIOx, MDC); - wait_ms(MDC_WAIT); - HAL_GPIO_ResetBits(GPIOx, MDC); - wait_ms(MDC_WAIT); - val |= HAL_GPIO_ReadInputDataBit(GPIOx, MDIO); - } - return (val); -} - -void turnaround_MDIO( GPIO_TypeDef* GPIOx) -{ - GPIOx->OUTENCLR = MDIO ; - HAL_GPIO_SetBits(GPIOx, MDC); - wait_ms(MDC_WAIT); - HAL_GPIO_ResetBits(GPIOx, MDC); - wait_ms(MDC_WAIT); -} - -void idle_MDIO( GPIO_TypeDef* GPIOx ) -{ - GPIOx->OUTENSET = MDIO ; - HAL_GPIO_SetBits(GPIOx,MDC); - wait_ms(MDC_WAIT); - HAL_GPIO_ResetBits(GPIOx, MDC); - wait_ms(MDC_WAIT); -} - -uint32_t mdio_read(GPIO_TypeDef* GPIOx, uint32_t PhyRegAddr) -{ - output_MDIO(GPIOx, 0xFFFFFFFF, 32); - output_MDIO(GPIOx, 0x06, 4); - output_MDIO(GPIOx, PHY_ADDR, 5); - output_MDIO(GPIOx, PhyRegAddr, 5); - turnaround_MDIO(GPIOx); - uint32_t val = input_MDIO(GPIOx ); - idle_MDIO(GPIOx); - return val; -} - -void mdio_write(GPIO_TypeDef* GPIOx, uint32_t PhyRegAddr, uint32_t val) -{ - output_MDIO(GPIOx, 0xFFFFFFFF, 32); - output_MDIO(GPIOx, 0x05, 4); - output_MDIO(GPIOx, PHY_ADDR, 5); - output_MDIO(GPIOx, PhyRegAddr, 5); - output_MDIO(GPIOx, 0x02, 2); - output_MDIO(GPIOx, val, 16); - idle_MDIO(GPIOx); -} - -int WIZnet_Chip::ethernet_link(void) { - return ((mdio_read(GPIO_MDC, PHYREG_STATUS)>>SVAL)&0x01); -} - -void WIZnet_Chip::ethernet_set_link(int speed, int duplex) { - uint32_t val=0; - if((speed < 0) || (speed > 1)) { - val = CNTL_AUTONEGO; - } else { - val = ((CNTL_SPEED&(speed<<11))|(CNTL_DUPLEX&(duplex<<7))); - } - mdio_write(GPIO_MDC, PHYREG_CONTROL, val); -} - - void WIZnet_Chip::reg_rd_mac(uint16_t addr, uint8_t* data) - { - data[0] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+3)); - data[1] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+2)); - data[2] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+1)); - data[3] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+0)); - data[4] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+7)); - data[5] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+6)); - } - - void WIZnet_Chip::reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) - { - uint8_t buf[4]={0,}; - uint32_t wr_ip = 0; - char* p = (char*)ip; - - for(int i = 0; i < 4; i++) { - wr_ip = (wr_ip<<8); - buf[i] = atoi(p); - wr_ip |= buf[i]; - p = strchr(p, '.'); - if (p == NULL) break; - p++; - } - *(volatile uint32_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)) = wr_ip; - } - - void WIZnet_Chip::sreg_ip(int socket, uint16_t addr, const char* ip) { - reg_wr_ip(addr, (uint8_t)(0x01+(socket<<2)), ip); - } - -#endif - -
--- a/arch/int/W7500x_toe.h Tue Nov 17 06:35:55 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,300 +0,0 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ -#pragma once - -#include "mbed.h" -#include "mbed_debug.h" - -#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; - -#define DEFAULT_WAIT_RESP_TIMEOUT 500 - - -#define MAX_SOCK_NUM 8 - -// Peripheral base address -#define W7500x_WZTOE_BASE (0x46000000) -#define W7500x_TXMEM_BASE (W7500x_WZTOE_BASE + 0x00020000) -#define W7500x_RXMEM_BASE (W7500x_WZTOE_BASE + 0x00030000) -// Common register -#define MR (0x2300) -#define GAR (0x6008) -#define SUBR (0x600C) -#define SHAR (0x6000) -#define SIPR (0x6010) - -// Added Common register @W7500 -#define TIC100US (0x2000) - -// Socket register -#define Sn_MR (0x0000) -#define Sn_CR (0x0010) -#define Sn_IR (0x0020) //--Sn_ISR -#define Sn_SR (0x0030) -#define Sn_PORT (0x0114) -#define Sn_DIPR (0x0124) -#define Sn_DPORT (0x0120) -#define Sn_RXBUF_SIZE (0x0200) -#define Sn_TXBUF_SIZE (0x0220) -#define Sn_TX_FSR (0x0204) -#define Sn_TX_WR (0x020C) -#define Sn_RX_RSR (0x0224) -#define Sn_RX_RD (0x0228) -// added Socket register @W7500 -#define Sn_ICR (0x0028) -enum PHYMode { - AutoNegotiate = 0, - HalfDuplex10 = 1, - FullDuplex10 = 2, - HalfDuplex100 = 3, - FullDuplex100 = 4, -}; - -//bool plink(int wait_time_ms= 3*1000); - -class WIZnet_Chip { -public: -enum Protocol { - CLOSED = 0, - TCP = 1, - UDP = 2, -}; - -enum Command { - OPEN = 0x01, - LISTEN = 0x02, - CONNECT = 0x04, - DISCON = 0x08, - CLOSE = 0x10, - SEND = 0x20, - SEND_MAC = 0x21, - SEND_KEEP = 0x22, - RECV = 0x40, - -}; - -enum Interrupt { - INT_CON = 0x01, - INT_DISCON = 0x02, - INT_RECV = 0x04, - INT_TIMEOUT = 0x08, - INT_SEND_OK = 0x10, -}; -enum Status { - SOCK_CLOSED = 0x00, - SOCK_INIT = 0x13, - SOCK_LISTEN = 0x14, - SOCK_SYNSENT = 0x15, - SOCK_ESTABLISHED = 0x17, - SOCK_CLOSE_WAIT = 0x1c, - SOCK_UDP = 0x22, -}; -enum Mode { - MR_RST = 0x80, - MR_WOL = 0x20, - MR_PB = 0x10, - MR_FARP = 0x02, -}; - - WIZnet_Chip(); - - /* - * Set MAC Address to W7500x_TOE - * - * @return true if connected, false otherwise - */ - bool setmac(); - - /* - * Connect the W7500 WZTOE to the ssid contained in the constructor. - * - * @return true if connected, false otherwise - */ - bool setip(); - - - /* - * Open a tcp connection with the specified host on the specified port - * - * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) - * @param port port - * @ returns true if successful - */ - bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); - - /* - * Set the protocol (UDP or TCP) - * - * @param p protocol - * @ returns true if successful - */ - bool setProtocol(int socket, Protocol p); - - /* - * Reset the W7500 WZTOE - */ - void reset(); - - int wait_readable(int socket, int wait_time_ms, int req_size = 0); - - int wait_writeable(int socket, int wait_time_ms, int req_size = 0); - - /* - * Check if an ethernet link is pressent or not. - * - * @returns true if successful - */ - bool link(int wait_time_ms= 3*1000); - - /* - * Sets the speed and duplex parameters of an ethernet link. - * - * @returns true if successful - */ - void set_link(PHYMode phymode); - - /* - * Check if a tcp link is active - * - * @returns true if successful - */ - bool is_connected(int socket); - - /* - * Close a tcp connection - * - * @ returns true if successful - */ - bool close(int socket); - - /* - * @param str string to be sent - * @param len string length - */ - int send(int socket, const char * str, int len); - - int recv(int socket, char* buf, int len); - - /* - * Return true if the module is using dhcp - * - * @returns true if the module is using dhcp - */ - bool isDHCP() { - return dhcp; - } - - bool gethostbyname(const char* host, uint32_t* ip); - - static WIZnet_Chip * getInstance() { - return inst; - }; - - int new_socket(); - uint16_t new_port(); - - void scmd(int socket, Command cmd); - - template<typename T> - void sreg(int socket, uint16_t addr, T data) { - reg_wr<T>(addr, (uint8_t)(0x01+(socket<<2)), data); - } - - template<typename T> - T sreg(int socket, uint16_t addr) { - return reg_rd<T>(addr, (uint8_t)(0x01+(socket<<2))); - } - - template<typename T> - void reg_wr(uint16_t addr, T data) { - return reg_wr(addr, 0x00, data); - } - - template<typename T> - void reg_wr(uint16_t addr, uint8_t cb, T data) { - uint8_t buf[sizeof(T)]; - *reinterpret_cast<T*>(buf) = data; - /* - for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian - uint8_t t = buf[i]; - buf[i] = buf[sizeof(buf)-1-i]; - buf[sizeof(buf)-1-i] = t; - } - */ - for(int i = 0; i < sizeof(buf); i++) { // Little Endian to Big Endian - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)+i) = buf[i]; - } - } - - template<typename T> - T reg_rd(uint16_t addr) { - return reg_rd<T>(addr, (uint8_t)(0x00)); - } - - template<typename T> - T reg_rd(uint16_t addr, uint8_t cb) { - uint8_t buf[sizeof(T)] = {0,}; - for(int i = 0; i < sizeof(buf); i++) { // Little Endian to Big Endian - buf[i] = *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)((cb<<16)+addr)+i); - } - /* - for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian - uint8_t t = buf[i]; - buf[i] = buf[sizeof(buf)-1-i]; - buf[sizeof(buf)-1-i] = t; - } - */ - return *reinterpret_cast<T*>(buf); - } - - void reg_rd_mac(uint16_t addr, uint8_t* data); - - void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip); - - void sreg_ip(int socket, uint16_t addr, const char* ip); - - int ethernet_link(void); - - void ethernet_set_link(int speed, int duplex); - - -protected: - uint8_t mac[6]; - uint32_t ip; - uint32_t netmask; - uint32_t gateway; - uint32_t dnsaddr; - bool dhcp; - - static WIZnet_Chip* inst; - - void reg_wr_mac(uint16_t addr, uint8_t* data) { - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+3)) = data[0] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+2)) = data[1] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+1)) = data[2] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+0)) = data[3] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+7)) = data[4] ; - *(volatile uint8_t *)(W7500x_WZTOE_BASE + (uint32_t)(addr+6)) = data[5] ; - } -}; - -extern uint32_t str_to_ip(const char* str); -extern void printfBytes(char* str, uint8_t* buf, int len); -extern void printHex(uint8_t* buf, int len); -extern void debug_hex(uint8_t* buf, int len);