2012年4月8日日曜日

mbedでWIZ820io(W5200)を使ってみる(2)

W5200のMACRAWモードのプログラムを修正してみた。
データシートのPACKET-INFOとCRCの説明とは違うようだが問題なく動いている。

// W5200_ethernet.cpp 2012/4/7
#include "mbed.h"
#include "w5100.h"
#include "Utils.h"

#define DEBUG 1

extern W5100Class W5100; // w5100.cpp

class MyEthernet {
    int _socket;
public:
    MyEthernet() {
        _socket = 0;
        W5100.writeSnMR( _socket, 0x40|SnMR::MACRAW); // S0_MR_MACRAW(0x04) and MAC filter
        W5100.execCmdSn( _socket, Sock_OPEN);
    }
    int receive() {
        int size = W5100.getRXReceivedSize(_socket);
        if (size <= 2) {
            return 0;
        }
        uint8_t info[2];
        W5100.recv_data_processing(_socket, info, 2, 1); //peek
        int size2 = (int)info[0]<<8|info[1];
        if (size >= size2) {
            return size2-2;
        }
        return 0;
    }

    int read(char* data, int size) {
        uint8_t info[2];
        W5100.recv_data_processing(_socket, info, 2);
        W5100.execCmdSn( _socket, Sock_RECV);
        int size3 = (int)info[0]<<8|info[1];
        size3 -= 2;
        W5100.recv_data_processing(_socket, (uint8_t*)data, size3);
        W5100.execCmdSn(_socket, Sock_RECV);
        return size3;
    }
}; 

int ethernet_test1() {
    MyEthernet eth;
    char buf[0x600];
    while(1) {
        int size = eth.receive();
        if(size > 0) {
            printf("WIZ820io ethernet packet size: %d\n", size);
            eth.read(buf, sizeof(buf));
            printf("Destination:  %02X:%02X:%02X:%02X:%02X:%02X\n",
                    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
            printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\n",
                    buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
        }
        printHex((u8*)buf, size);
        wait(1);
        printf("S0_RX_RSR:%d, S0_RX_RD:%d, S0_RX_WR:%d\n",
            W5100.readSnRX_RSR(0), W5100.readSnRX_RD(0), W5100.readSnRX_WR(0));
    }
}
--

0 件のコメント: