RTno is communicating library and framework which allows you to make your embedded device capable of communicating with RT-middleware world. RT-middleware is a platform software to realize Robotic system. In RTM, robots are developed by constructing robotics technologies\' elements (components) named RT-component. Therefore, the RTno helps you to create your own RT-component with your mbed and arduino. To know how to use your RTno device, visit here: http://ysuga.net/robot_e/rtm_e/rtc_e/1065?lang=en To know about RT-middleware and RT-component, visit http://www.openrtm.org
Dependencies: EthernetInterface mbed-rtos
EtherTcp.cpp@6:86d72601ff54, 2013-08-29 (annotated)
- Committer:
- ysuga
- Date:
- Thu Aug 29 05:26:09 2013 +0000
- Revision:
- 6:86d72601ff54
- Parent:
- 1:f74116b37bc9
RTno updt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ysuga | 0:5f7bc45bc2e8 | 1 | #include "mbed.h" |
ysuga | 6:86d72601ff54 | 2 | #define RTNO_SUBMODULE_DEFINE |
ysuga | 6:86d72601ff54 | 3 | #include "RTno.h" |
ysuga | 6:86d72601ff54 | 4 | |
ysuga | 6:86d72601ff54 | 5 | #ifdef USE_ETHERNET_CONNECTION |
ysuga | 6:86d72601ff54 | 6 | |
ysuga | 0:5f7bc45bc2e8 | 7 | #include "EtherTcp.h" |
ysuga | 6:86d72601ff54 | 8 | #include "EthernetInterface.h" |
ysuga | 6:86d72601ff54 | 9 | |
ysuga | 0:5f7bc45bc2e8 | 10 | |
ysuga | 0:5f7bc45bc2e8 | 11 | #include "ip_addr.h" |
ysuga | 0:5f7bc45bc2e8 | 12 | /** |
ysuga | 0:5f7bc45bc2e8 | 13 | #include <../SPI/SPI.h> |
ysuga | 0:5f7bc45bc2e8 | 14 | #include <../Ethernet/Ethernet.h> |
ysuga | 0:5f7bc45bc2e8 | 15 | |
ysuga | 0:5f7bc45bc2e8 | 16 | static EthernetServer *m_pServer; |
ysuga | 0:5f7bc45bc2e8 | 17 | static EthernetClient *m_pClient; |
ysuga | 0:5f7bc45bc2e8 | 18 | */ |
ysuga | 6:86d72601ff54 | 19 | //static EthernetInterface* m_pInterface; |
ysuga | 6:86d72601ff54 | 20 | static TCPSocketServer* m_pServerSocket; |
ysuga | 6:86d72601ff54 | 21 | static TCPSocketConnection m_ClientSocket; |
ysuga | 0:5f7bc45bc2e8 | 22 | |
ysuga | 0:5f7bc45bc2e8 | 23 | |
ysuga | 0:5f7bc45bc2e8 | 24 | #define ETCP_RX_BUFFER_SIZE 128 |
ysuga | 1:f74116b37bc9 | 25 | uint8_t etcp_rx_buffer[ETCP_RX_BUFFER_SIZE]; |
ysuga | 0:5f7bc45bc2e8 | 26 | int etcp_rx_buffer_pointer_head = 0; |
ysuga | 0:5f7bc45bc2e8 | 27 | int etcp_rx_buffer_pointer_tail = 0; |
ysuga | 6:86d72601ff54 | 28 | ///Host m_Client; |
ysuga | 0:5f7bc45bc2e8 | 29 | |
ysuga | 0:5f7bc45bc2e8 | 30 | Serial *pSerial; |
ysuga | 0:5f7bc45bc2e8 | 31 | |
ysuga | 0:5f7bc45bc2e8 | 32 | /** |
ysuga | 0:5f7bc45bc2e8 | 33 | * Push data to ring buffer. |
ysuga | 0:5f7bc45bc2e8 | 34 | */ |
ysuga | 0:5f7bc45bc2e8 | 35 | int etcp_rx_buffer_push(unsigned char c) { |
ysuga | 0:5f7bc45bc2e8 | 36 | etcp_rx_buffer[etcp_rx_buffer_pointer_tail] = c; |
ysuga | 0:5f7bc45bc2e8 | 37 | etcp_rx_buffer_pointer_tail++; |
ysuga | 0:5f7bc45bc2e8 | 38 | if(etcp_rx_buffer_pointer_tail >= ETCP_RX_BUFFER_SIZE) { |
ysuga | 0:5f7bc45bc2e8 | 39 | etcp_rx_buffer_pointer_tail = 0; |
ysuga | 0:5f7bc45bc2e8 | 40 | } |
ysuga | 0:5f7bc45bc2e8 | 41 | return 0; |
ysuga | 0:5f7bc45bc2e8 | 42 | } |
ysuga | 0:5f7bc45bc2e8 | 43 | |
ysuga | 0:5f7bc45bc2e8 | 44 | /** |
ysuga | 0:5f7bc45bc2e8 | 45 | * Pop data fron ring buffer |
ysuga | 0:5f7bc45bc2e8 | 46 | */ |
ysuga | 0:5f7bc45bc2e8 | 47 | int etcp_rx_buffer_pop(unsigned char *c) { |
ysuga | 0:5f7bc45bc2e8 | 48 | *c = etcp_rx_buffer[etcp_rx_buffer_pointer_head]; |
ysuga | 0:5f7bc45bc2e8 | 49 | etcp_rx_buffer_pointer_head++; |
ysuga | 0:5f7bc45bc2e8 | 50 | if(etcp_rx_buffer_pointer_head >= ETCP_RX_BUFFER_SIZE) { |
ysuga | 0:5f7bc45bc2e8 | 51 | etcp_rx_buffer_pointer_head = 0; |
ysuga | 0:5f7bc45bc2e8 | 52 | } |
ysuga | 0:5f7bc45bc2e8 | 53 | return 0; |
ysuga | 0:5f7bc45bc2e8 | 54 | } |
ysuga | 0:5f7bc45bc2e8 | 55 | |
ysuga | 0:5f7bc45bc2e8 | 56 | int etcp_rx_buffer_get_size() { |
ysuga | 0:5f7bc45bc2e8 | 57 | int size = etcp_rx_buffer_pointer_tail - etcp_rx_buffer_pointer_head; |
ysuga | 0:5f7bc45bc2e8 | 58 | if(size < 0) { |
ysuga | 0:5f7bc45bc2e8 | 59 | size += ETCP_RX_BUFFER_SIZE; |
ysuga | 0:5f7bc45bc2e8 | 60 | } |
ysuga | 0:5f7bc45bc2e8 | 61 | return size; |
ysuga | 0:5f7bc45bc2e8 | 62 | } |
ysuga | 0:5f7bc45bc2e8 | 63 | |
ysuga | 6:86d72601ff54 | 64 | /* |
ysuga | 0:5f7bc45bc2e8 | 65 | static void EtherTcp_onClientEvent(TCPSocketEvent e) { |
ysuga | 0:5f7bc45bc2e8 | 66 | switch (e) { |
ysuga | 0:5f7bc45bc2e8 | 67 | // If the socket is readable, do stuff |
ysuga | 0:5f7bc45bc2e8 | 68 | case TCPSOCKET_READABLE: |
ysuga | 0:5f7bc45bc2e8 | 69 | while(1) { |
ysuga | 0:5f7bc45bc2e8 | 70 | char buf; |
ysuga | 0:5f7bc45bc2e8 | 71 | int ret = m_pClientSocket->recv(&buf, 1); |
ysuga | 0:5f7bc45bc2e8 | 72 | if (ret == 0) break; |
ysuga | 0:5f7bc45bc2e8 | 73 | etcp_rx_buffer_push(buf); |
ysuga | 0:5f7bc45bc2e8 | 74 | } |
ysuga | 0:5f7bc45bc2e8 | 75 | |
ysuga | 0:5f7bc45bc2e8 | 76 | break; |
ysuga | 0:5f7bc45bc2e8 | 77 | case TCPSOCKET_CONTIMEOUT: |
ysuga | 0:5f7bc45bc2e8 | 78 | case TCPSOCKET_CONRST: |
ysuga | 0:5f7bc45bc2e8 | 79 | case TCPSOCKET_CONABRT: |
ysuga | 0:5f7bc45bc2e8 | 80 | case TCPSOCKET_ERROR: |
ysuga | 0:5f7bc45bc2e8 | 81 | case TCPSOCKET_DISCONNECTED: |
ysuga | 0:5f7bc45bc2e8 | 82 | delete m_pClientSocket; |
ysuga | 0:5f7bc45bc2e8 | 83 | m_pClientSocket = NULL; |
ysuga | 0:5f7bc45bc2e8 | 84 | break; |
ysuga | 0:5f7bc45bc2e8 | 85 | } |
ysuga | 0:5f7bc45bc2e8 | 86 | } |
ysuga | 0:5f7bc45bc2e8 | 87 | |
ysuga | 0:5f7bc45bc2e8 | 88 | static void EtherTcp_onServerEvent(TCPSocketEvent e) { |
ysuga | 0:5f7bc45bc2e8 | 89 | if(e == TCPSOCKET_ACCEPT ) { |
ysuga | 0:5f7bc45bc2e8 | 90 | if ( m_pServerSocket->accept(&m_Client, &m_pClientSocket) ) { |
ysuga | 0:5f7bc45bc2e8 | 91 | return; //Error in accept, discard connection |
ysuga | 0:5f7bc45bc2e8 | 92 | } |
ysuga | 0:5f7bc45bc2e8 | 93 | |
ysuga | 0:5f7bc45bc2e8 | 94 | m_pClientSocket->setOnEvent(EtherTcp_onClientEvent); |
ysuga | 0:5f7bc45bc2e8 | 95 | } |
ysuga | 0:5f7bc45bc2e8 | 96 | } |
ysuga | 6:86d72601ff54 | 97 | */ |
ysuga | 0:5f7bc45bc2e8 | 98 | |
ysuga | 6:86d72601ff54 | 99 | void EtherTcp_init(/*const char* mac, */const char* ip, |
ysuga | 6:86d72601ff54 | 100 | const char* gateway, const char* subnet, |
ysuga | 0:5f7bc45bc2e8 | 101 | uint16_t port) |
ysuga | 0:5f7bc45bc2e8 | 102 | |
ysuga | 0:5f7bc45bc2e8 | 103 | { |
ysuga | 0:5f7bc45bc2e8 | 104 | pSerial = new Serial(USBTX, USBRX); |
ysuga | 6:86d72601ff54 | 105 | EthernetInterface::init( |
ysuga | 6:86d72601ff54 | 106 | ip, subnet, gateway); |
ysuga | 6:86d72601ff54 | 107 | //printf("Hello %d %d %d %d\r\n", ip[0], ip[1], ip[2], ip[3]); |
ysuga | 6:86d72601ff54 | 108 | //EthernetErr ethErr = m_pInterface->setup(); |
ysuga | 6:86d72601ff54 | 109 | //if (ethErr) { |
ysuga | 6:86d72601ff54 | 110 | // return; |
ysuga | 6:86d72601ff54 | 111 | // } |
ysuga | 0:5f7bc45bc2e8 | 112 | |
ysuga | 6:86d72601ff54 | 113 | m_pServerSocket = new TCPSocketServer(); |
ysuga | 6:86d72601ff54 | 114 | //m_pServerSocket->setOnEvent(EtherTcp_onServerEvent); |
ysuga | 6:86d72601ff54 | 115 | m_pServerSocket->bind(port); |
ysuga | 0:5f7bc45bc2e8 | 116 | m_pServerSocket->listen(); |
ysuga | 6:86d72601ff54 | 117 | m_pServerSocket->accept(m_ClientSocket); |
ysuga | 0:5f7bc45bc2e8 | 118 | SerialDevice_available = EtherTcp_available; |
ysuga | 0:5f7bc45bc2e8 | 119 | SerialDevice_getc = EtherTcp_getc; |
ysuga | 0:5f7bc45bc2e8 | 120 | SerialDevice_putc = EtherTcp_putc; |
ysuga | 0:5f7bc45bc2e8 | 121 | } |
ysuga | 0:5f7bc45bc2e8 | 122 | |
ysuga | 0:5f7bc45bc2e8 | 123 | uint8_t EtherTcp_available() |
ysuga | 0:5f7bc45bc2e8 | 124 | { |
ysuga | 6:86d72601ff54 | 125 | //Net::poll(); |
ysuga | 0:5f7bc45bc2e8 | 126 | return etcp_rx_buffer_get_size(); |
ysuga | 0:5f7bc45bc2e8 | 127 | } |
ysuga | 0:5f7bc45bc2e8 | 128 | |
ysuga | 0:5f7bc45bc2e8 | 129 | |
ysuga | 0:5f7bc45bc2e8 | 130 | void EtherTcp_putc(const char c) { |
ysuga | 6:86d72601ff54 | 131 | char d = c; |
ysuga | 6:86d72601ff54 | 132 | m_ClientSocket.send(&d, 1); |
ysuga | 0:5f7bc45bc2e8 | 133 | } |
ysuga | 0:5f7bc45bc2e8 | 134 | |
ysuga | 1:f74116b37bc9 | 135 | uint8_t EtherTcp_getc() |
ysuga | 0:5f7bc45bc2e8 | 136 | { |
ysuga | 1:f74116b37bc9 | 137 | uint8_t c; |
ysuga | 6:86d72601ff54 | 138 | m_ClientSocket.receive((char*)&c, 1); |
ysuga | 6:86d72601ff54 | 139 | //etcp_rx_buffer_pop(&c); |
ysuga | 0:5f7bc45bc2e8 | 140 | return c; |
ysuga | 0:5f7bc45bc2e8 | 141 | } |
ysuga | 6:86d72601ff54 | 142 | |
ysuga | 6:86d72601ff54 | 143 | #endif |