W5200(WIZ820io) network interface

Committer:
va009039
Date:
Thu Apr 19 11:14:43 2012 +0000
Revision:
2:a8df39b4f3aa
Parent:
1:803123933c5a

        

Who changed what in which revision?

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