W5200(WIZ820io) network interface
Revision 1:803123933c5a, committed 2012-04-17
- Comitter:
- va009039
- Date:
- Tue Apr 17 12:13:15 2012 +0000
- Parent:
- 0:61831b843b44
- Child:
- 2:a8df39b4f3aa
- Commit message:
Changed in this revision
--- a/EthernetNetIf.lib Sat Apr 14 17:21:11 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/sherckuith/code/EthernetNetIf/#479ce5546098
--- a/MyNetDnsRequest.cpp Sat Apr 14 17:21:11 2012 +0000 +++ b/MyNetDnsRequest.cpp Tue Apr 17 12:13:15 2012 +0000 @@ -1,9 +1,10 @@ -// MyNetDnsRequest.cpp 2012/4/13 +// MyNetDnsRequest.cpp 2012/4/16 #include "mbed.h" #include "MyNetDnsRequest.h" #include "UDPSocket.h" #include <string> #include "dnsname.h" +#include "w5200NetIf.h" //#define DEBUG @@ -120,8 +121,13 @@ m_udp = new UDPSocket; } m_udp->setOnEvent(this, &MyNetDnsRequest::callback); - Host local(IpAddr(0,0,0,0), 1243); - Host server(IpAddr(8,8,8,8), 53); // DNS + Host local(IpAddr(0,0,0,0), 1024 + rand()&0x7fff); + IpAddr dns(8,8,8,8); + NetIf* pIf = Net::getDefaultIf(); + if (pIf) { + dns = ((w5200NetIf*)pIf)->m_dns; + } + Host server(dns, 53); // DNS m_udp->bind(local); uint8_t buf[256]; int size = query(buf, sizeof(buf), hostname);
--- a/MyNetIf.cpp Sat Apr 14 17:21:11 2012 +0000 +++ b/MyNetIf.cpp Tue Apr 17 12:13:15 2012 +0000 @@ -1,4 +1,4 @@ -// MyNetIf.cpp 2012/4/13 +// MyNetIf.cpp 2012/4/17 #include "mbed.h" #include "MyNetIf.h" #include "MyNetTcpSocket.h" @@ -8,8 +8,9 @@ //#define DEBUG #ifdef DEBUG +#include "w5100.h" #include "Utils.h" -#define PRINT_FUNC() printf("%d:%s\n", __LINE__,__PRETTY_FUNCTION__) +#define PRINT_FUNC() printf("%p %d:%s\n", this,__LINE__,__PRETTY_FUNCTION__) #else //DEBUG #define PRINT_FUNC() #endif //DEBUG @@ -49,4 +50,31 @@ void MyNetIf::poll() { PRINT_FUNC(); +#ifdef DEBUG + printf("SnMR:"); + for(int s = 0; s < MAX_SOCK_NUM; s++) { + printf(" %02x", W5100.readSnMR(s)); + } + printf("\n"); + + printf("SnIR:"); + for(int s = 0; s < MAX_SOCK_NUM; s++) { + printf(" %02x", W5100.readSnIR(s)); + } + printf("\n"); + + printf("SnSR:"); + for(int s = 0; s < MAX_SOCK_NUM; s++) { + printf(" %02x", W5100.readSnSR(s)); + } + printf("\n"); + + printf("SnPORT:"); + for(int s = 0; s < MAX_SOCK_NUM; s++) { + printf(" %d", W5100.readSnPORT(s)); + } + printf("\n"); + + wait_ms(500); +#endif //DEBUG }
--- a/MyNetIf.h Sat Apr 14 17:21:11 2012 +0000 +++ b/MyNetIf.h Tue Apr 17 12:13:15 2012 +0000 @@ -1,3 +1,4 @@ +// MyNetIf.h 2012/4/15 #ifndef MYIPNETIF_H #define MYIPNETIF_H #include "mbed.h"
--- a/MyNetTcpSocket.cpp Sat Apr 14 17:21:11 2012 +0000 +++ b/MyNetTcpSocket.cpp Tue Apr 17 12:13:15 2012 +0000 @@ -1,9 +1,11 @@ -// MyNetTcpSocket.cpp 2012/4/15 +// MyNetTcpSocket.cpp 2012/4/17 #include "mbed.h" #include "w5100.h" #include "MyNetTcpSocket.h" +#define __DEBUG +#include "dbg/dbg.h" -#define DEBUG +//#define DEBUG #ifdef DEBUG #include "Utils.h" @@ -12,18 +14,44 @@ #define PRINT_FUNC() #endif //DEBUG -MyNetTcpSocket::MyNetTcpSocket() : NetTcpSocket() { +int w5200_new_socket() +{ + for(int i = 0; i < MAX_SOCK_NUM; i++) { + if (W5100.readSnMR(i) == SnMR::CLOSE) { // 0x00 + if (W5100.readSnSR(i) != SnSR::LISTEN) { // 0x14 + return i; + } + } + } + return -1; // not found +} + +MyNetTcpSocket::MyNetTcpSocket(int socket) : NetTcpSocket(),_socket(socket),wait_accept(false) { PRINT_FUNC(); - _socket = 1; - W5100.writeSnMR(_socket, SnMR:: TCP); // set TCP mode + if (_socket == (-1)) { + _socket = w5200_new_socket(); + } +#ifdef DEBUG + printf("%p socket: %d\n", this, _socket); +#endif //DEBUG + if (_socket != (-1)) { + W5100.writeSnMR(_socket, SnMR::TCP); // set TCP mode + } } MyNetTcpSocket::~MyNetTcpSocket() { PRINT_FUNC(); + close(); + if (_socket != (-1)) { + W5100.writeSnMR(_socket, SnMR::CLOSE); + } } NetTcpSocketErr MyNetTcpSocket::bind(const Host& me) { PRINT_FUNC(); + if (_socket == (-1)) { + return NETTCPSOCKET_MEM; + } int port = me.getPort(); W5100.writeSnPORT(_socket, port); return NETTCPSOCKET_OK; @@ -31,6 +59,9 @@ NetTcpSocketErr MyNetTcpSocket::listen() { PRINT_FUNC(); + if (_socket == (-1)) { + return NETTCPSOCKET_MEM; + } W5100.execCmdSn(_socket, Sock_OPEN); // set OPEN command W5100.execCmdSn(_socket, Sock_LISTEN); // listen #ifdef DEBUG @@ -40,11 +71,15 @@ W5100.getIPAddress(ip); printf("SIPR: %d.%d.%d.%d Sn_PORT:%d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnPORT(_socket)); #endif //DEBUG + wait_accept = true; return NETTCPSOCKET_OK; } NetTcpSocketErr MyNetTcpSocket::connect(const Host& host) { PRINT_FUNC(); + if (_socket == (-1)) { + return NETTCPSOCKET_MEM; + } uint8_t ip[4]; ip[0] = host.getIp()[0]; ip[1] = host.getIp()[1]; @@ -71,12 +106,23 @@ NetTcpSocketErr MyNetTcpSocket::accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket) { PRINT_FUNC(); + if (_socket == (-1)) { + return NETTCPSOCKET_MEM; + } uint8_t ip[4]; W5100.readSnDIPR(_socket, ip); pClient->setIp(IpAddr(ip[0],ip[1],ip[2],ip[3])); int port = W5100.readSnDPORT(_socket); pClient->setPort(port); - + Host me; + me.setPort(W5100.readSnPORT(_socket)); + *ppNewNetTcpSocket = new MyNetTcpSocket(_socket); + _socket = w5200_new_socket(); + if (_socket != (-1)) { + W5100.writeSnMR(_socket, SnMR::TCP); // set TCP mode + bind(me); + listen(); + } return NETTCPSOCKET_OK; } @@ -86,6 +132,9 @@ printf("buf:%p, len=%d\n", buf, len); printHex((u8*)buf, len); #endif //DEBUG + if (_socket == (-1)) { + return NETTCPSOCKET_MEM; + } if (len > 0) { W5100.send_data_processing(_socket, (uint8_t*)buf, len); W5100.execCmdSn(_socket, Sock_SEND); @@ -95,6 +144,9 @@ int /*if < 0 : NetTcpSocketErr*/ MyNetTcpSocket::recv(char* buf, int len){ PRINT_FUNC(); + if (_socket == (-1)) { + return NETTCPSOCKET_MEM; + } int size = W5100.getRXReceivedSize(_socket); if (size > len) { size = len; @@ -116,8 +168,10 @@ } m_closed = true; cleanUp(); - W5100.execCmdSn(_socket, Sock_DISCON); - W5100.execCmdSn(_socket, Sock_CLOSE); + if (_socket != (-1)) { + W5100.execCmdSn(_socket, Sock_DISCON); + W5100.execCmdSn(_socket, Sock_CLOSE); + } return NETTCPSOCKET_OK; } @@ -125,42 +179,57 @@ PRINT_FUNC(); NetTcpSocket::flushEvents(); #ifdef DEBUG - printf("socket:%d SnMR:%02x SnIR:%02x SnSR:%02x\n", _socket, - W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket)); - uint8_t ip[4]; - W5100.readSnDIPR(_socket, ip); - printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket)); - printf("Sn_RX_RSR:%d, Sn_RX_RD:%d, Sn_RX_WR:%d\n", + printf("%p socket:%d\n", this,_socket); + if (_socket != (-1)) { + printf("SnMR:%02x SnIR:%02x SnSR:%02x\n", + W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket)); + uint8_t ip[4]; + W5100.readSnDIPR(_socket, ip); + printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket)); + printf("Sn_RX_RSR:%5d, Sn_RX_RD:%5d, Sn_RX_WR:%5d\n", W5100.readSnRX_RSR(_socket), W5100.readSnRX_RD(_socket), W5100.readSnRX_WR(_socket)); - printf("Sn_TX_FSR:%d, Sn_TX_RD:%d, Sn_TX_WR:%d\n", + printf("Sn_TX_FSR:%5d, Sn_TX_RD:%5d, Sn_TX_WR:%5d\n", W5100.readSnTX_FSR(_socket), W5100.readSnTX_RD(_socket), W5100.readSnTX_WR(_socket)); - wait_ms(500); + } + wait_ms(200); #endif //DEBUG - if (W5100.readSnSR(_socket) == 0x1c) { + if (_socket == (-1)) { + return NETTCPSOCKET_OK; + } + uint8_t Sn_SR = W5100.readSnSR(_socket); + if (wait_accept) { + if (Sn_SR == 0x17) { + queueEvent(NETTCPSOCKET_ACCEPT); + wait_accept = false; + } + } + if (Sn_SR == 0x1c) { queueEvent(NETTCPSOCKET_CONRST); } if (W5100.getRXReceivedSize(_socket) > 0) { queueEvent(NETTCPSOCKET_READABLE); } - if (W5100.readSnSR(_socket) == 0x17) { + if (Sn_SR == 0x17) { queueEvent(NETTCPSOCKET_CONNECTED); if (W5100.getTXFreeSize(_socket) > 0) { queueEvent(NETTCPSOCKET_WRITEABLE); } } + if (Sn_SR == 0x00) { + queueEvent(NETTCPSOCKET_DISCONNECTED); + } return NETTCPSOCKET_OK; } void MyNetTcpSocket::cleanUp() //Flush input buffer { PRINT_FUNC(); - while(1) { - int len = W5100.getRXReceivedSize(_socket); - if (len <= 0) { - break; - } - uint8_t temp[8]; - W5100.recv_data_processing(_socket, temp, sizeof(temp)); + if (_socket == (-1)) { + return; + } + while(W5100.getRXReceivedSize(_socket) > 0) { + uint8_t temp[1]; + W5100.recv_data_processing(_socket, temp, 1); W5100.execCmdSn(_socket, Sock_RECV); - } + } }
--- a/MyNetTcpSocket.h Sat Apr 14 17:21:11 2012 +0000 +++ b/MyNetTcpSocket.h Tue Apr 17 12:13:15 2012 +0000 @@ -1,11 +1,11 @@ -// MyNetTcpSocket.h 2012/4/14 +// MyNetTcpSocket.h 2012/4/16 #ifndef MYNETTCPSOCKET_H #define MYNETTCPSOCKET_H #include "if/net/nettcpsocket.h" class MyNetTcpSocket : public NetTcpSocket { public: - MyNetTcpSocket(); + MyNetTcpSocket(int socket = (-1)); virtual ~MyNetTcpSocket(); virtual NetTcpSocketErr bind(const Host& me); virtual NetTcpSocketErr listen(); @@ -17,6 +17,7 @@ virtual NetTcpSocketErr poll(); protected: int _socket; + bool wait_accept; private: void cleanUp(); //Flush input buffer queue<MyNetTcpSocket*> m_lpInNetTcpSocket;
--- a/MyNetUdpSocket.cpp Sat Apr 14 17:21:11 2012 +0000 +++ b/MyNetUdpSocket.cpp Tue Apr 17 12:13:15 2012 +0000 @@ -1,9 +1,9 @@ -// MyNetUdpSocket.cpp 2012/4/15 +// MyNetUdpSocket.cpp 2012/4/16 #include "mbed.h" #include "MyNetUdpSocket.h" #include "w5100.h" -#define DEBUG +//#define DEBUG #ifdef DEBUG #include "Utils.h" @@ -12,19 +12,31 @@ #define PRINT_FUNC() #endif //DEBUG -MyNetUdpSocket::MyNetUdpSocket() : NetUdpSocket() { +extern int w5200_new_socket(); // MyNetTcpSocket.cpp + +MyNetUdpSocket::MyNetUdpSocket(int socket) : NetUdpSocket(),_socket(socket) { PRINT_FUNC(); - _socket = 0; - W5100.writeSnMR(_socket, SnMR::UDP); // set UDP mode + if (_socket == (-1)) { + _socket = w5200_new_socket(); + } + if (_socket != (-1)) { + W5100.writeSnMR(_socket, SnMR::UDP); // set UDP mode + } } MyNetUdpSocket::~MyNetUdpSocket() { PRINT_FUNC(); close(); + if (_socket != (-1)) { + W5100.writeSnMR(_socket, SnMR::CLOSE); + } } NetUdpSocketErr MyNetUdpSocket::bind(const Host& me) { PRINT_FUNC(); + if (_socket == (-1)) { + return NETUDPSOCKET_MEM; + } int port = me.getPort(); W5100.writeSnPORT(_socket, port); W5100.execCmdSn( _socket, Sock_OPEN); // set OPEN command @@ -33,6 +45,9 @@ int /*if < 0 : NetUdpSocketErr*/ MyNetUdpSocket::sendto(const char* buf, int len, Host* pHost) { PRINT_FUNC(); + if (_socket == (-1)) { + return NETUDPSOCKET_MEM; + } uint8_t ip[4]; ip[0] = pHost->getIp()[0]; ip[1] = pHost->getIp()[1]; @@ -55,6 +70,9 @@ int /*if < 0 : NetUdpSocketErr*/ MyNetUdpSocket::recvfrom(char* buf, int len, Host* pHost) { PRINT_FUNC(); + if (_socket == (-1)) { + return NETUDPSOCKET_MEM; + } int size = W5100.getRXReceivedSize(_socket); if (size < 8) { return -1; @@ -82,8 +100,12 @@ if(m_closed) { return NETUDPSOCKET_OK; } + if (_socket == (-1)) { + return NETUDPSOCKET_MEM; + } m_closed = true; cleanUp(); + W5100.writeSnMR(_socket, SnMR::CLOSE); return NETUDPSOCKET_OK; } @@ -91,22 +113,27 @@ PRINT_FUNC(); NetUdpSocket::flushEvents(); #ifdef DEBUG - printf("socket:%d SnMR:%02x SnIR:%02x SnSR:%02x\n", _socket, - W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket)); - uint8_t ip[4]; - W5100.readSnDIPR(_socket, ip); - printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket)); - uint8_t mac[6]; - W5100.readSnDHAR(_socket, mac); - printf("Sn_DHAR: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - printf("Sn_RX_RSR:%d, Sn_RX_RD:%d, Sn_RX_WR:%d\n", + printf("%p socket:%d\n", this,_socket); + if (_socket != (-1)) { + printf("SnMR:%02x SnIR:%02x SnSR:%02x\n", + W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket)); + uint8_t ip[4]; + W5100.readSnDIPR(_socket, ip); + printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket)); + uint8_t mac[6]; + W5100.readSnDHAR(_socket, mac); + printf("Sn_DHAR: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + printf("Sn_RX_RSR:%5d, Sn_RX_RD:%5d, Sn_RX_WR:%5d\n", W5100.readSnRX_RSR(_socket), W5100.readSnRX_RD(_socket), W5100.readSnRX_WR(_socket)); - printf("Sn_TX_FSR:%d, Sn_TX_RD:%d, Sn_TX_WR:%d\n", + printf("Sn_TX_FSR:%5d, Sn_TX_RD:%5d, Sn_TX_WR:%5d\n", W5100.readSnTX_FSR(_socket), W5100.readSnTX_RD(_socket), W5100.readSnTX_WR(_socket)); - wait_ms(500); + } + wait_ms(200); #endif //DEBUG - if (W5100.getRXReceivedSize(_socket) > 0) { - queueEvent(NETUDPSOCKET_READABLE); + if (_socket != (-1)) { + if (W5100.getRXReceivedSize(_socket) > 0) { + queueEvent(NETUDPSOCKET_READABLE); + } } return NETUDPSOCKET_OK; } @@ -114,13 +141,12 @@ void MyNetUdpSocket::cleanUp() //Flush input buffer { PRINT_FUNC(); - while(1) { - int len = W5100.getRXReceivedSize(_socket); - if (len <= 0) { - break; - } - uint8_t temp[8]; - W5100.recv_data_processing(_socket, temp, sizeof(temp)); + if (_socket == (-1)) { + return; + } + while(W5100.getRXReceivedSize(_socket) > 0) { + uint8_t temp[1]; + W5100.recv_data_processing(_socket, temp, 1); W5100.execCmdSn(_socket, Sock_RECV); } }
--- a/MyNetUdpSocket.h Sat Apr 14 17:21:11 2012 +0000 +++ b/MyNetUdpSocket.h Tue Apr 17 12:13:15 2012 +0000 @@ -1,11 +1,11 @@ -// MyNetUdpSocket.h 2012/4/14 +// MyNetUdpSocket.h 2012/4/16 #ifndef MYNETUDPSOCKET_H #define MYNETUDPSOCKET_H #include "if/net/netudpsocket.h" class MyNetUdpSocket: public NetUdpSocket { public: - MyNetUdpSocket(); + MyNetUdpSocket(int socket = (-1)); virtual ~MyNetUdpSocket(); virtual NetUdpSocketErr bind(const Host& me); virtual int /*if < 0 : NetUdpSocketErr*/ sendto(const char* buf, int len, Host* pHost);
--- a/TinyHTTP.cpp Sat Apr 14 17:21:11 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,240 +0,0 @@ -/* - * mbed Tiny HTTP Client - * Copyright (c) 2011 Hiroshi Suga - * Released under the MIT License: http://mbed.org/license/mit - */ - -/** @file - * @brief Tiny HTTP Client - */ - -#include "mbed.h" -#include "EthernetNetIf.h" -#include "TCPSocket.h" -#include "DNSRequest.h" -#include "TinyHTTP.h" -#include <ctype.h> - - -TCPSocket *http; -volatile int tcp_ready, tcp_readable, tcp_writable; -volatile int dns_status; - -// Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) -int base64enc(const char *input, unsigned int length, char *output, int len) { - static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - unsigned int c, c1, c2, c3; - - if (len < ((((length-1)/3)+1)<<2)) return -1; - for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { - c1 = ((((unsigned char)*((unsigned char *)&input[i])))); - c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; - c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; - - c = ((c1 & 0xFC) >> 2); - output[j+0] = base64[c]; - c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); - output[j+1] = base64[c]; - c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); - output[j+2] = (length>i+1)?base64[c]:'='; - c = (c3 & 0x3F); - output[j+3] = (length>i+2)?base64[c]:'='; - } - output[(((length-1)/3)+1)<<2] = '\0'; - return 0; -} - -// Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) -int urlencode(char *str, char *buf, int len) { - static const char to_hex[] = "0123456789ABCDEF"; -// char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; - char *pstr = str, *pbuf = buf; - - if (len < (strlen(str) * 3 + 1)) return -1; - while (*pstr) { - if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') { - *pbuf++ = *pstr; - } else if (*pstr == ' ') { - *pbuf++ = '+'; - } else { - *pbuf++ = '%'; - *pbuf++ = to_hex[(*pstr >> 4) & 0x0f]; - *pbuf++ = to_hex[*pstr & 0x0f]; - } - pstr++; - } - *pbuf = '\0'; - return 0; -} - - -void isr_http (TCPSocketEvent e) { - -#ifdef DEBUG - printf("tcp(%d)\r\n", e); -#endif - switch(e) { - case TCPSOCKET_CONNECTED: - tcp_ready = 1; - break; - - case TCPSOCKET_READABLE: //Incoming data - tcp_readable = 1; - break; - - case TCPSOCKET_WRITEABLE: //We can send data - tcp_writable = 1; - break; - - case TCPSOCKET_CONTIMEOUT: - case TCPSOCKET_CONRST: - case TCPSOCKET_CONABRT: - case TCPSOCKET_ERROR: - case TCPSOCKET_DISCONNECTED: - tcp_ready = 0; - break; - } -} - -void createauth (char *user, char *pwd, char *buf, int len) { - char tmp[80]; - - strncpy(buf, "Authorization: Basic ", len); - snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); - base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf)); - strncat(buf, "\r\n", len - strlen(buf)); -} - -void isr_dns (DNSReply r) { - -#ifdef DEBUG - printf("dns(%d)\r\n", r); -#endif - if (r == DNS_FOUND) { - dns_status = 1; - } else { - dns_status = -1; - } -} - -int httpRequest (int method, Host *host, char *uri, char *head, char *body) { - TCPSocketErr err; - Timer timeout; - char buf[1500]; - int i, ret = -1; - - http = new TCPSocket; - tcp_ready = 0; - tcp_readable = 0; - tcp_writable = 0; - - http->setOnEvent(isr_http); - - // connect - if (host->getIp().isNull()) { - // resolv - DNSRequest dns; - dns_status = 0; - dns.setOnReply(isr_dns); - if (dns.resolve(host) != DNS_OK) goto exit; - timeout.reset(); - timeout.start(); - while (timeout.read_ms() < HTTP_TIMEOUT) { - if (dns_status) break; - Net::poll(); - } - timeout.stop(); - if (dns_status <= 0) goto exit; -#ifdef DEBUG - printf("%s [%d.%d.%d.%d]\r\n", host->getName(), (unsigned char)host->getIp()[0], (unsigned char)host->getIp()[1], (unsigned char)host->getIp()[2], (unsigned char)host->getIp()[3]); -#endif - } - if (! host->getPort()) { - host->setPort(HTTP_PORT); - } - err = http->connect(*host); - if (err != TCPSOCKET_OK) goto exit; - - // wait connect - timeout.reset(); - timeout.start(); - while (timeout.read_ms() < HTTP_TIMEOUT) { - if (tcp_ready) break; - Net::poll(); - } - timeout.stop(); - if (! tcp_ready) goto exit; - - // send request - if (method == METHOD_POST) { - http->send("POST ", 5); - } else { - http->send("GET ", 4); - } - http->send(uri, strlen(uri)); - http->send(" HTTP/1.1\r\nHost: ", 17); - http->send(host->getName(), strlen(host->getName())); - http->send("\r\n", 2); - http->send("Connection: close\r\n", 19); - if (head) { - http->send(head, strlen(head)); - } - if (method == METHOD_POST) { - sprintf(buf, "Content-Length: %d\r\n", strlen(body)); - http->send(buf, strlen(buf)); - } - http->send("\r\n", 2); - - // post method - if (method == METHOD_POST && body) { - http->send(body, strlen(body)); - } - - // wait responce - timeout.reset(); - timeout.start(); - while (timeout.read_ms() < HTTP_TIMEOUT) { - if (tcp_readable) break; - Net::poll(); - } - timeout.stop(); - if (! tcp_readable) goto exit; - - // recv responce - i = http->recv(buf, sizeof(buf) - 1); - buf[i] = 0; - if (i < sizeof(buf) - 1) tcp_readable = 0; - if (strncmp(buf, "HTTP/", 5) == 0) { - ret = atoi(&buf[9]); - } -#ifdef DEBUG - printf(buf); -#endif - - // recv dummy - timeout.reset(); - timeout.start(); - while (timeout.read_ms() < HTTP_TIMEOUT) { - if (tcp_readable) { - i = http->recv(buf, sizeof(buf) - 1); - buf[i] = 0; - if (i < sizeof(buf) - 1) tcp_readable = 0; -#ifdef DEBUG - printf(buf); -#endif - timeout.reset(); - } else - if (! tcp_ready) { - break; - } - Net::poll(); - } - timeout.stop(); - -exit: - http->resetOnEvent(); - http->close(); - delete http; - - return ret; -}
--- a/TinyHTTP.h Sat Apr 14 17:21:11 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,38 +0,0 @@ -/* - * mbed Tiny HTTP Client - * Copyright (c) 2011 Hiroshi Suga - * Released under the MIT License: http://mbed.org/license/mit - */ - -/** @file - * @brief Tiny HTTP Client - */ - -#ifndef TinyHTTP_h -#define TinyHTTP_h - -#define DEBUG - -#define HTTP_PORT 80 -#define HTTP_TIMEOUT 15000 // ms - -#define METHOD_GET 0 -#define METHOD_POST 1 - -/** send http request - * @param method METHOD_GET or METHOD_POST - * @param host http server - * @param uri URI - * @param head http header (CR+LF) (or NULL) - * @param body POST body (or NULL) - * @return http code, -1:failue - */ -int httpRequest (int method, Host *host, char *uri, char *head, char *body); - -void createauth (char *user, char *pwd, char *buf, int len); - -int base64enc(const char *input, unsigned int length, char *output, int len); - -int urlencode(char *str, char *buf, int len); - -#endif
--- a/Utils.cpp Sat Apr 14 17:21:11 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ - - -#include "mbed.h" -#include "Utils.h" - -void printfBytes(const char* s, const u8* data, int len) -{ - printf("%s %d:",s,len); - if (len > 256) - len = 256; - while (len-- > 0) - printf(" %02X",*data++); - printf("\n"); -} - -void printHexLine(const u8* d, int addr, int len) -{ - printf("%04X ",addr); - int i; - for (i = 0; i < len; i++) - printf("%02X ",d[i]); - for (;i < 16; i++) - printf(" "); - char s[16+1]; - memset(s,0,sizeof(s)); - for (i = 0; i < len; i++) - { - int c = d[i]; - if (c < 0x20 || c > 0x7E) - c = '.'; - s[i] = c; - } - printf("%s\n",s); -} - -void printHex(const u8* d, int len) -{ - int addr = 0; - while (len) - { - int count = len; - if (count > 16) - count = 16; - printHexLine(d+addr,addr,count); - addr += 16; - len -= count; - } -}
--- a/Utils.h Sat Apr 14 17:21:11 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned long u32; - -void DelayMS(int ms); - -void printfBytes(const char* label,const u8* data, int len); -void printHex(const u8* d, int len); - -#ifndef min -#define min(_a,_b) ((_a) < (_b) ? (_a) : (_b)) -#endif - -inline int LE16(const u8* d) -{ - return d[0] | (d[1] << 8); -} - -inline u32 BE32(const u8* d) -{ - return (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3]; -} - -inline void BE32(u32 n, u8* d) -{ - d[0] = (u8)(n >> 24); - d[1] = (u8)(n >> 16); - d[2] = (u8)(n >> 8); - d[3] = (u8)n; -} - -inline void BE16(u32 n, u8* d) -{ - d[0] = (u8)(n >> 8); - d[1] = (u8)n; -}
--- a/WIZ820ioNetIf.cpp Sat Apr 14 17:21:11 2012 +0000 +++ b/WIZ820ioNetIf.cpp Tue Apr 17 12:13:15 2012 +0000 @@ -1,7 +1,25 @@ -// WIZ820ioNetIf.cpp 2012/4/13 +// WIZ820ioNetIf.cpp 2012/4/17 #include "WIZ820ioNetIf.h" #include "w5100.h" +extern SPI* pSPI; // w5100.cpp +extern DigitalOut* pCS; // w5100.cpp + +DigitalOut* pRESET = NULL; + +void hardware_reset() { + if (!pRESET) { + pRESET = new DigitalOut(p15); + } + if (pRESET) { + pRESET->write(1); + pRESET->write(0); + wait_us(2); + pRESET->write(1); + wait_ms(150); + } +} + bool wait_linkup(int timeout = 5000) { Timer link_t; link_t.start(); @@ -14,8 +32,18 @@ return false; } +void WIZ820ioNetIf::spi(PinName mosi,PinName miso, PinName sclk) { + pSPI = new SPI(mosi,miso,sclk); +} +void WIZ820ioNetIf::cs(PinName _cs) { + pCS = new DigitalOut(_cs); +} +void WIZ820ioNetIf::reset(PinName _reset) { + pRESET = new DigitalOut(_reset); +} + int WIZ820ioNetIf::setup(int timeout_ms) { - W5100.hardware_reset(); + hardware_reset(); W5100.init(); wait_linkup(); return w5200NetIf::setup(timeout_ms);
--- a/WIZ820ioNetIf.h Sat Apr 14 17:21:11 2012 +0000 +++ b/WIZ820ioNetIf.h Tue Apr 17 12:13:15 2012 +0000 @@ -1,9 +1,25 @@ -// WIZ820ioNetIf.h 2012/4/13 +// WIZ820ioNetIf.h 2012/4/15 +/* + +WIZ820io mbed +J2P1 GND ----- p1 GND +J1P3 MOSI ---- p11 SPI(mosi) +J2P6 MISO ---- p12 SPI(miso) +J1P4 SCK ----- p13 SPI(sck) +J1P5 nSS ----- p14 +J2P5 nRESET -- p15 +J2P3 3V3D ---- p40 VOUT +J2P4 PWDN ---- GND + +*/ #ifndef WIZ820IO_NETIF_H #define WIZ820IO_NETIF_H #include "w5200NetIf.h" class WIZ820ioNetIf : public w5200NetIf { public: + void spi(PinName mosi,PinName miso, PinName sclk); + void cs(PinName _cs); + void reset(PinName _reset); int setup(int timeout_ms = 15000); }; #endif //WIZ820IO_NETIF_H
--- a/mbed.bld Sat Apr 14 17:21:11 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/737756e0b479
--- a/mymain.cpp Sat Apr 14 17:21:11 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ -#include "mbed.h" -#include "WIZ820ioNetIf.h" -#include "TinyHTTP.h" -#include <host.h> - -int main() { - WIZ820ioNetIf eth; - eth.setup(); - Host host; - host.setName("www.wiznet.co.kr"); - int status = httpRequest(METHOD_GET, &host, "/", NULL, NULL); - printf("status: %d\n", status); -}
--- a/w5100.cpp Sat Apr 14 17:21:11 2012 +0000 +++ b/w5100.cpp Tue Apr 17 12:13:15 2012 +0000 @@ -9,6 +9,7 @@ #include <stdio.h> #include <string.h> +#include "mbed.h" #include "w5100.h" #ifndef MBED #include <avr/interrupt.h> @@ -30,25 +31,25 @@ #endif #ifdef MBED -SPI spi(p11, p12, p13); // mosi, miso, sclk -DigitalOut _cs(p14); -DigitalOut _reset(p15); -inline void delay(int n) { wait_ms(n); } -inline static void initSS(){ _cs = 1; } -inline static void setSS() { _cs = 0; } -inline static void resetSS() { _cs = 1; } + +SPI* pSPI = NULL; +DigitalOut* pCS = NULL; -void W5100Class::hardware_reset() { - _reset = 1; - _reset = 0; - wait_us(2); - _reset = 1; - wait_ms(150); -} +inline void delay(int n) { wait_ms(n); } +inline static void initSS(){ pCS->write(1); } +inline static void setSS() { pCS->write(0); } +inline static void resetSS() { pCS->write(1); } + #endif //MBED void W5100Class::init(void) { + if (!pSPI) { + pSPI = new SPI(p11, p12, p13); // mosi, miso, sclk + } + if (!pCS) { + pCS = new DigitalOut(p14); + } initSS(); writeMR(1<<RST); @@ -164,16 +165,16 @@ setSS(); #ifdef W5200 - spi.write(_addr >> 8); - spi.write(_addr & 0xFF); - spi.write(0x80); - spi.write(0x01); + pSPI->write(_addr >> 8); + pSPI->write(_addr & 0xFF); + pSPI->write(0x80); + pSPI->write(0x01); #else - spi.write(0xF0); - spi.write(_addr >> 8); - spi.write(_addr & 0xFF); + pSPI->write(0xF0); + pSPI->write(_addr >> 8); + pSPI->write(_addr & 0xFF); #endif - spi.write(_data); + pSPI->write(_data); resetSS(); return 1; } @@ -183,14 +184,14 @@ #ifdef W5200 setSS(); - spi.write(_addr >> 8); - spi.write(_addr & 0xFF); - spi.write((0x80 | ((_len & 0x7F00) >> 8))); - spi.write(_len & 0x00FF); + pSPI->write(_addr >> 8); + pSPI->write(_addr & 0xFF); + pSPI->write((0x80 | ((_len & 0x7F00) >> 8))); + pSPI->write(_len & 0x00FF); for (uint16_t i=0; i<_len; i++) { - spi.write(_buf[i]); + pSPI->write(_buf[i]); } resetSS(); @@ -199,11 +200,11 @@ for (uint16_t i=0; i<_len; i++) { setSS(); - spi.write(0xF0); - spi.write(_addr >> 8); - spi.write(_addr & 0xFF); + pSPI->write(0xF0); + pSPI->write(_addr >> 8); + pSPI->write(_addr & 0xFF); _addr++; - spi.write(_buf[i]); + pSPI->write(_buf[i]); resetSS(); } #endif @@ -215,17 +216,17 @@ { setSS(); #ifdef W5200 - spi.write(_addr >> 8); - spi.write(_addr & 0xFF); - spi.write(0x00); - spi.write(0x01); + pSPI->write(_addr >> 8); + pSPI->write(_addr & 0xFF); + pSPI->write(0x00); + pSPI->write(0x01); #else - spi.write(0x0F); - spi.write(_addr >> 8); - spi.write(_addr & 0xFF); + pSPI->write(0x0F); + pSPI->write(_addr >> 8); + pSPI->write(_addr & 0xFF); #endif - uint8_t _data = spi.write(0); + uint8_t _data = pSPI->write(0); resetSS(); return _data; } @@ -234,14 +235,14 @@ { #ifdef W5200 setSS(); - spi.write(_addr >> 8); - spi.write(_addr & 0xFF); - spi.write((0x00 | ((_len & 0x7F00) >> 8))); - spi.write(_len & 0x00FF); + pSPI->write(_addr >> 8); + pSPI->write(_addr & 0xFF); + pSPI->write((0x00 | ((_len & 0x7F00) >> 8))); + pSPI->write(_len & 0x00FF); for (uint16_t i=0; i<_len; i++) { - _buf[i] = spi.write(0); + _buf[i] = pSPI->write(0); } resetSS(); @@ -251,11 +252,11 @@ for (uint16_t i=0; i<_len; i++) { setSS(); - spi.write(0x0F); - spi.write(_addr >> 8); - spi.write(_addr & 0xFF); + pSPI->write(0x0F); + pSPI->write(_addr >> 8); + pSPI->write(_addr & 0xFF); _addr++; - _buf[i] = spi.write(0); + _buf[i] = pSPI->write(0); resetSS(); } #endif
--- a/w5100.h Sat Apr 14 17:21:11 2012 +0000 +++ b/w5100.h Tue Apr 17 12:13:15 2012 +0000 @@ -143,9 +143,6 @@ class W5100Class { public: -#ifdef MBED - void hardware_reset(); -#endif //MBED void init(); /**
--- a/w5200NetIf.cpp Sat Apr 14 17:21:11 2012 +0000 +++ b/w5200NetIf.cpp Tue Apr 17 12:13:15 2012 +0000 @@ -45,6 +45,7 @@ t[2] = m_gateway[2]; t[3] = m_gateway[3]; W5100.writeGAR(t); + m_dns = IpAddr(dhcp.dnsaddr[0],dhcp.dnsaddr[1],dhcp.dnsaddr[2],dhcp.dnsaddr[3]); return err; }
--- a/w5200NetIf.h Sat Apr 14 17:21:11 2012 +0000 +++ b/w5200NetIf.h Tue Apr 17 12:13:15 2012 +0000 @@ -10,11 +10,11 @@ int IPrenew(int timeout_ms = 15000); int IPrelease(int timeout_ms = 15000); int setup(int timeout_ms = 15000); + IpAddr m_dns; private: bool m_useDhcp; IpAddr m_netmask; IpAddr m_gateway; - IpAddr m_dns; const char* m_hostname; }; #endif //W5200_NETIF_H