WIZ820io(W5200) network interface、EthernetNetIf compatible.

/media/uploads/va009039/wiz820ionetif.jpg

example

#include "WIZ820ioNetIf.h"
#include "HTTPClient.h"
#include "HTTPServer.h"

#if defined(TARGET_KL25Z)
WIZ820ioNetIf eth(PTD2,PTD3,PTD1,PTD0,PTD5);
#endif
HTTPClient http;
HTTPStream stream;

void callback(HTTPResult r){
    printf("callback %d %s\n", r, HTTPClient::ResultStr(r));
}

int main() {
    int err = eth.setup();
    if (err < 0) {
        printf("setup error %d\n", err);
        exit(-1);
    }    

    HTTPServer svr;
    svr.addHandler<SimpleHandler>("/");
    svr.bind(80);

    const char* uri = "http://va009039-mbed.appspot.com/kl25z/";
    http.get(uri, &stream, callback);
    uint8_t buf[256];
    int total = 0;
    stream.readNext(buf, sizeof(buf));
    while(1) {
        if(stream.readable()) {
            int len = stream.readLen();
            total += len;
            printf("%d %d\n", total, len);
            stream.readNext(buf, sizeof(buf));
        }
        Net::poll();
    }
}
Committer:
va009039
Date:
Fri Mar 22 11:51:24 2013 +0000
Revision:
0:bdeec5f86894
Child:
1:22b9052d864d
[mbed] converted /w5200NetIf/w5200NetIf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:bdeec5f86894 1 // MyNetUdpSocket.cpp 2012/4/16
va009039 0:bdeec5f86894 2 #include "mbed.h"
va009039 0:bdeec5f86894 3 #include "MyNetUdpSocket.h"
va009039 0:bdeec5f86894 4 #include "w5100.h"
va009039 0:bdeec5f86894 5
va009039 0:bdeec5f86894 6 //#define DEBUG
va009039 0:bdeec5f86894 7
va009039 0:bdeec5f86894 8 #ifdef DEBUG
va009039 0:bdeec5f86894 9 #include "Utils.h"
va009039 0:bdeec5f86894 10 #define PRINT_FUNC() printf("%p %d:%s\n", this,__LINE__,__PRETTY_FUNCTION__)
va009039 0:bdeec5f86894 11 #else //DEBUG
va009039 0:bdeec5f86894 12 #define PRINT_FUNC()
va009039 0:bdeec5f86894 13 #endif //DEBUG
va009039 0:bdeec5f86894 14
va009039 0:bdeec5f86894 15 extern int w5200_new_socket(); // MyNetTcpSocket.cpp
va009039 0:bdeec5f86894 16
va009039 0:bdeec5f86894 17 MyNetUdpSocket::MyNetUdpSocket(int socket) : NetUdpSocket(),_socket(socket) {
va009039 0:bdeec5f86894 18 PRINT_FUNC();
va009039 0:bdeec5f86894 19 if (_socket == (-1)) {
va009039 0:bdeec5f86894 20 _socket = w5200_new_socket();
va009039 0:bdeec5f86894 21 }
va009039 0:bdeec5f86894 22 if (_socket != (-1)) {
va009039 0:bdeec5f86894 23 W5100.writeSnMR(_socket, SnMR::UDP); // set UDP mode
va009039 0:bdeec5f86894 24 }
va009039 0:bdeec5f86894 25 }
va009039 0:bdeec5f86894 26
va009039 0:bdeec5f86894 27 MyNetUdpSocket::~MyNetUdpSocket() {
va009039 0:bdeec5f86894 28 PRINT_FUNC();
va009039 0:bdeec5f86894 29 close();
va009039 0:bdeec5f86894 30 if (_socket != (-1)) {
va009039 0:bdeec5f86894 31 W5100.writeSnMR(_socket, SnMR::CLOSE);
va009039 0:bdeec5f86894 32 }
va009039 0:bdeec5f86894 33 }
va009039 0:bdeec5f86894 34
va009039 0:bdeec5f86894 35 NetUdpSocketErr MyNetUdpSocket::bind(const Host& me) {
va009039 0:bdeec5f86894 36 PRINT_FUNC();
va009039 0:bdeec5f86894 37 if (_socket == (-1)) {
va009039 0:bdeec5f86894 38 return NETUDPSOCKET_MEM;
va009039 0:bdeec5f86894 39 }
va009039 0:bdeec5f86894 40 int port = me.getPort();
va009039 0:bdeec5f86894 41 W5100.writeSnPORT(_socket, port);
va009039 0:bdeec5f86894 42 W5100.execCmdSn( _socket, Sock_OPEN); // set OPEN command
va009039 0:bdeec5f86894 43 return NETUDPSOCKET_OK;
va009039 0:bdeec5f86894 44 }
va009039 0:bdeec5f86894 45
va009039 0:bdeec5f86894 46 int /*if < 0 : NetUdpSocketErr*/ MyNetUdpSocket::sendto(const char* buf, int len, Host* pHost) {
va009039 0:bdeec5f86894 47 PRINT_FUNC();
va009039 0:bdeec5f86894 48 if (_socket == (-1)) {
va009039 0:bdeec5f86894 49 return NETUDPSOCKET_MEM;
va009039 0:bdeec5f86894 50 }
va009039 0:bdeec5f86894 51 uint8_t ip[4];
va009039 0:bdeec5f86894 52 ip[0] = pHost->getIp()[0];
va009039 0:bdeec5f86894 53 ip[1] = pHost->getIp()[1];
va009039 0:bdeec5f86894 54 ip[2] = pHost->getIp()[2];
va009039 0:bdeec5f86894 55 ip[3] = pHost->getIp()[3];
va009039 0:bdeec5f86894 56 int port = pHost->getPort();
va009039 0:bdeec5f86894 57 W5100.writeSnDIPR(_socket, ip);
va009039 0:bdeec5f86894 58 W5100.writeSnDPORT(_socket, port);
va009039 0:bdeec5f86894 59 W5100.send_data_processing(_socket, (uint8_t*)buf, len);
va009039 0:bdeec5f86894 60 W5100.execCmdSn(_socket, Sock_SEND);
va009039 0:bdeec5f86894 61 #ifdef DEBUG
va009039 0:bdeec5f86894 62 printHex((u8*)buf, len);
va009039 0:bdeec5f86894 63 W5100.getIPAddress(ip);
va009039 0:bdeec5f86894 64 printf("SIPR: %d.%d.%d.%d Sn_PORT:%d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnPORT(_socket));
va009039 0:bdeec5f86894 65 W5100.readSnDIPR(_socket, ip);
va009039 0:bdeec5f86894 66 printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT:%d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket));
va009039 0:bdeec5f86894 67 #endif //DEBUG
va009039 0:bdeec5f86894 68 return len;
va009039 0:bdeec5f86894 69 }
va009039 0:bdeec5f86894 70
va009039 0:bdeec5f86894 71 int /*if < 0 : NetUdpSocketErr*/ MyNetUdpSocket::recvfrom(char* buf, int len, Host* pHost) {
va009039 0:bdeec5f86894 72 PRINT_FUNC();
va009039 0:bdeec5f86894 73 if (_socket == (-1)) {
va009039 0:bdeec5f86894 74 return NETUDPSOCKET_MEM;
va009039 0:bdeec5f86894 75 }
va009039 0:bdeec5f86894 76 int size = W5100.getRXReceivedSize(_socket);
va009039 0:bdeec5f86894 77 if (size < 8) {
va009039 0:bdeec5f86894 78 return -1;
va009039 0:bdeec5f86894 79 }
va009039 0:bdeec5f86894 80 uint8_t info[8];
va009039 0:bdeec5f86894 81 W5100.recv_data_processing(_socket, info, 8);
va009039 0:bdeec5f86894 82 W5100.execCmdSn(_socket, Sock_RECV);
va009039 0:bdeec5f86894 83 pHost->setIp(IpAddr(info[0],info[1],info[2],info[3]));
va009039 0:bdeec5f86894 84 pHost->setPort(info[4]<<8|info[5]);
va009039 0:bdeec5f86894 85 size -= 8;
va009039 0:bdeec5f86894 86 if (size > len) {
va009039 0:bdeec5f86894 87 size = len;
va009039 0:bdeec5f86894 88 }
va009039 0:bdeec5f86894 89 W5100.recv_data_processing(_socket, (uint8_t*)buf, size);
va009039 0:bdeec5f86894 90 W5100.execCmdSn(_socket, Sock_RECV);
va009039 0:bdeec5f86894 91 #ifdef DEBUG
va009039 0:bdeec5f86894 92 printfBytes("UDP PACKET-INFO", (u8*)info, 8);
va009039 0:bdeec5f86894 93 printHex((u8*)buf, size);
va009039 0:bdeec5f86894 94 #endif //DEBUG
va009039 0:bdeec5f86894 95 return size;
va009039 0:bdeec5f86894 96 }
va009039 0:bdeec5f86894 97
va009039 0:bdeec5f86894 98 NetUdpSocketErr MyNetUdpSocket::close() {
va009039 0:bdeec5f86894 99 PRINT_FUNC();
va009039 0:bdeec5f86894 100 if(m_closed) {
va009039 0:bdeec5f86894 101 return NETUDPSOCKET_OK;
va009039 0:bdeec5f86894 102 }
va009039 0:bdeec5f86894 103 if (_socket == (-1)) {
va009039 0:bdeec5f86894 104 return NETUDPSOCKET_MEM;
va009039 0:bdeec5f86894 105 }
va009039 0:bdeec5f86894 106 m_closed = true;
va009039 0:bdeec5f86894 107 cleanUp();
va009039 0:bdeec5f86894 108 W5100.writeSnMR(_socket, SnMR::CLOSE);
va009039 0:bdeec5f86894 109 return NETUDPSOCKET_OK;
va009039 0:bdeec5f86894 110 }
va009039 0:bdeec5f86894 111
va009039 0:bdeec5f86894 112 NetUdpSocketErr MyNetUdpSocket::poll() {
va009039 0:bdeec5f86894 113 PRINT_FUNC();
va009039 0:bdeec5f86894 114 NetUdpSocket::flushEvents();
va009039 0:bdeec5f86894 115 #ifdef DEBUG
va009039 0:bdeec5f86894 116 printf("%p socket:%d\n", this,_socket);
va009039 0:bdeec5f86894 117 if (_socket != (-1)) {
va009039 0:bdeec5f86894 118 printf("SnMR:%02x SnIR:%02x SnSR:%02x\n",
va009039 0:bdeec5f86894 119 W5100.readSnMR(_socket), W5100.readSnIR(_socket), W5100.readSnSR(_socket));
va009039 0:bdeec5f86894 120 uint8_t ip[4];
va009039 0:bdeec5f86894 121 W5100.readSnDIPR(_socket, ip);
va009039 0:bdeec5f86894 122 printf("Sn_DIPR: %d.%d.%d.%d Sn_DPORT: %d\n", ip[0], ip[1], ip[2], ip[3], W5100.readSnDPORT(_socket));
va009039 0:bdeec5f86894 123 uint8_t mac[6];
va009039 0:bdeec5f86894 124 W5100.readSnDHAR(_socket, mac);
va009039 0:bdeec5f86894 125 printf("Sn_DHAR: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
va009039 0:bdeec5f86894 126 printf("Sn_RX_RSR:%5d, Sn_RX_RD:%5d, Sn_RX_WR:%5d\n",
va009039 0:bdeec5f86894 127 W5100.readSnRX_RSR(_socket), W5100.readSnRX_RD(_socket), W5100.readSnRX_WR(_socket));
va009039 0:bdeec5f86894 128 printf("Sn_TX_FSR:%5d, Sn_TX_RD:%5d, Sn_TX_WR:%5d\n",
va009039 0:bdeec5f86894 129 W5100.readSnTX_FSR(_socket), W5100.readSnTX_RD(_socket), W5100.readSnTX_WR(_socket));
va009039 0:bdeec5f86894 130 }
va009039 0:bdeec5f86894 131 wait_ms(200);
va009039 0:bdeec5f86894 132 #endif //DEBUG
va009039 0:bdeec5f86894 133 if (_socket != (-1)) {
va009039 0:bdeec5f86894 134 if (W5100.getRXReceivedSize(_socket) > 0) {
va009039 0:bdeec5f86894 135 queueEvent(NETUDPSOCKET_READABLE);
va009039 0:bdeec5f86894 136 }
va009039 0:bdeec5f86894 137 }
va009039 0:bdeec5f86894 138 return NETUDPSOCKET_OK;
va009039 0:bdeec5f86894 139 }
va009039 0:bdeec5f86894 140
va009039 0:bdeec5f86894 141 void MyNetUdpSocket::cleanUp() //Flush input buffer
va009039 0:bdeec5f86894 142 {
va009039 0:bdeec5f86894 143 PRINT_FUNC();
va009039 0:bdeec5f86894 144 if (_socket == (-1)) {
va009039 0:bdeec5f86894 145 return;
va009039 0:bdeec5f86894 146 }
va009039 0:bdeec5f86894 147 while(W5100.getRXReceivedSize(_socket) > 0) {
va009039 0:bdeec5f86894 148 uint8_t temp[1];
va009039 0:bdeec5f86894 149 W5100.recv_data_processing(_socket, temp, 1);
va009039 0:bdeec5f86894 150 W5100.execCmdSn(_socket, Sock_RECV);
va009039 0:bdeec5f86894 151 }
va009039 0:bdeec5f86894 152 }