Transfer data between UART ports and Ethernet.

Dependencies:   BufferedSerial

Transfer data between UART ports and Ethernet.

  • Support multiple UART ports.
  • Support fixed IP address and DHCP.
  • Support TCP server and TCP client modes.
  • Support a simple web server for UART and Ethernet configuration.
Committer:
morgandu
Date:
Fri May 05 07:40:19 2017 +0000
Revision:
0:11bc39d0f367
Child:
1:c53f82eb6b42
The first version.; ; Support multiple UART ports.; Support TCP server and TCP client mode.; Support static IP or DHCP.; Support fix configuration, or a web server for dynamic configuration.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
morgandu 0:11bc39d0f367 1 /*
morgandu 0:11bc39d0f367 2 * Copyright (c) 2017 Nuvoton Tecnology Corp. All rights reserved.
morgandu 0:11bc39d0f367 3 *
morgandu 0:11bc39d0f367 4 * The code is forward data from UART port to Ethernet, and vice versa.
morgandu 0:11bc39d0f367 5 *
morgandu 0:11bc39d0f367 6 *
morgandu 0:11bc39d0f367 7 */
morgandu 0:11bc39d0f367 8
morgandu 0:11bc39d0f367 9 #include "ste_config.h"
morgandu 0:11bc39d0f367 10
morgandu 0:11bc39d0f367 11 /* If define USE_STATIC_IP, specify the default IP address. */
morgandu 0:11bc39d0f367 12 #if 0
morgandu 0:11bc39d0f367 13 // private IP address for general purpose
morgandu 0:11bc39d0f367 14 #define IP_ADDRESS "192.168.1.2"
morgandu 0:11bc39d0f367 15 #define NETWORK_MASK "255.255.255.0"
morgandu 0:11bc39d0f367 16 #define GATEWAY_ADDRESS "192.168.1.1"
morgandu 0:11bc39d0f367 17 #else
morgandu 0:11bc39d0f367 18 // private IP address only for test with Windows when DHCP server doesn't exist.
morgandu 0:11bc39d0f367 19 #define IP_ADDRESS "169.254.108.2"
morgandu 0:11bc39d0f367 20 #define NETWORK_MASK "255.255.0.0"
morgandu 0:11bc39d0f367 21 #define GATEWAY_ADDRESS "169.254.108.1"
morgandu 0:11bc39d0f367 22 #endif
morgandu 0:11bc39d0f367 23
morgandu 0:11bc39d0f367 24 /* Default configuration for network */
morgandu 0:11bc39d0f367 25 S_NET_CONFIG net_config = {IP_STATIC_MODE, IP_ADDRESS, NETWORK_MASK, GATEWAY_ADDRESS};
morgandu 0:11bc39d0f367 26
morgandu 0:11bc39d0f367 27 #if defined (TARGET_NUMAKER_PFM_M487)
morgandu 0:11bc39d0f367 28 #error Not defined UART ports yet.
morgandu 0:11bc39d0f367 29
morgandu 0:11bc39d0f367 30 #elif defined (TARGET_NUMAKER_PFM_NUC472)
morgandu 0:11bc39d0f367 31 BufferedSerial serial_0(PH_1, PH_0, 256, 4); // UART4
morgandu 0:11bc39d0f367 32 BufferedSerial serial_1(PG_2, PG_1, 256, 4); // UART0
morgandu 0:11bc39d0f367 33 BufferedSerial serial_2(PC_11, PC_10, 256, 4); // UART2
morgandu 0:11bc39d0f367 34
morgandu 0:11bc39d0f367 35 #elif defined (TARGET_NUMAKER_PFM_M453)
morgandu 0:11bc39d0f367 36 #error The board has no Ethernet.
morgandu 0:11bc39d0f367 37
morgandu 0:11bc39d0f367 38 #else
morgandu 0:11bc39d0f367 39 #error define UART ports for your board.
morgandu 0:11bc39d0f367 40 #endif
morgandu 0:11bc39d0f367 41
morgandu 0:11bc39d0f367 42 /* Default configuration for network ports and UART ports, etc. */
morgandu 0:11bc39d0f367 43 S_PORT_CONFIG port_config[MAX_UART_PORTS] = {
morgandu 0:11bc39d0f367 44
morgandu 0:11bc39d0f367 45 #if MAX_UART_PORTS == 1
morgandu 0:11bc39d0f367 46 {NET_SERVER_MODE, NET_PORT_BASE, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}
morgandu 0:11bc39d0f367 47
morgandu 0:11bc39d0f367 48 #elif MAX_UART_PORTS == 2
morgandu 0:11bc39d0f367 49 {NET_SERVER_MODE, NET_PORT_BASE + 0, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None},
morgandu 0:11bc39d0f367 50 {NET_SERVER_MODE, NET_PORT_BASE + 1, &serial_1, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}
morgandu 0:11bc39d0f367 51
morgandu 0:11bc39d0f367 52 #elif MAX_UART_PORTS == 3
morgandu 0:11bc39d0f367 53 {NET_SERVER_MODE, NET_PORT_BASE + 0, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None},
morgandu 0:11bc39d0f367 54 {NET_SERVER_MODE, NET_PORT_BASE + 1, &serial_1, DEFAULT_UART_BAUD, 8, 1, SerialBase::None},
morgandu 0:11bc39d0f367 55 {NET_SERVER_MODE, NET_PORT_BASE + 2, &serial_2, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}
morgandu 0:11bc39d0f367 56
morgandu 0:11bc39d0f367 57 #else
morgandu 0:11bc39d0f367 58 #error You have to define ports mapping table.
morgandu 0:11bc39d0f367 59 #endif
morgandu 0:11bc39d0f367 60 };
morgandu 0:11bc39d0f367 61
morgandu 0:11bc39d0f367 62 /* UART port to output debug message */
morgandu 0:11bc39d0f367 63 RawSerial output(USBTX, USBRX); // UART3 on NuMaker-PFM-NUC472
morgandu 0:11bc39d0f367 64 EthernetInterface eth;
morgandu 0:11bc39d0f367 65
morgandu 0:11bc39d0f367 66 #ifdef ENABLE_WEB_CONFIG
morgandu 0:11bc39d0f367 67
morgandu 0:11bc39d0f367 68 /* Declare the SD card as storage */
morgandu 0:11bc39d0f367 69 NuSDBlockDevice bd;
morgandu 0:11bc39d0f367 70 FATFileSystem fs("fs");
morgandu 0:11bc39d0f367 71 bool SD_Card_Mounted = FALSE;
morgandu 0:11bc39d0f367 72
morgandu 0:11bc39d0f367 73 #endif
morgandu 0:11bc39d0f367 74
morgandu 0:11bc39d0f367 75 /* --- --- */
morgandu 0:11bc39d0f367 76
morgandu 0:11bc39d0f367 77 /*
morgandu 0:11bc39d0f367 78 * Forward serial port data to ethernet, and vice versa.
morgandu 0:11bc39d0f367 79 *
morgandu 0:11bc39d0f367 80 */
morgandu 0:11bc39d0f367 81 void exchange_data(S_PORT_CONFIG *pmap, TCPSocket *psocket)
morgandu 0:11bc39d0f367 82 {
morgandu 0:11bc39d0f367 83 unsigned char n_buf[256];
morgandu 0:11bc39d0f367 84 unsigned char s_buf[256];
morgandu 0:11bc39d0f367 85 int n_len = 0, n_index = 0;
morgandu 0:11bc39d0f367 86 int s_len = 0, s_index = 0;
morgandu 0:11bc39d0f367 87
morgandu 0:11bc39d0f367 88 while(1)
morgandu 0:11bc39d0f367 89 {
morgandu 0:11bc39d0f367 90 /*** Network to Serial ***/
morgandu 0:11bc39d0f367 91
morgandu 0:11bc39d0f367 92 if (n_len < 0 || n_len == n_index)
morgandu 0:11bc39d0f367 93 {
morgandu 0:11bc39d0f367 94 // net buffer is empty, try to get new data from network.
morgandu 0:11bc39d0f367 95 n_len = psocket->recv(n_buf, sizeof(n_buf));
morgandu 0:11bc39d0f367 96 if (n_len == NSAPI_ERROR_WOULD_BLOCK)
morgandu 0:11bc39d0f367 97 {
morgandu 0:11bc39d0f367 98 }
morgandu 0:11bc39d0f367 99 else if (n_len < 0)
morgandu 0:11bc39d0f367 100 {
morgandu 0:11bc39d0f367 101 printf("Socket Recv Err (%d)\r\n", n_len);
morgandu 0:11bc39d0f367 102 psocket->close();
morgandu 0:11bc39d0f367 103 break;
morgandu 0:11bc39d0f367 104 }
morgandu 0:11bc39d0f367 105 else
morgandu 0:11bc39d0f367 106 {
morgandu 0:11bc39d0f367 107 n_index = 0;
morgandu 0:11bc39d0f367 108 }
morgandu 0:11bc39d0f367 109 }
morgandu 0:11bc39d0f367 110 else
morgandu 0:11bc39d0f367 111 {
morgandu 0:11bc39d0f367 112 // send data to serial port.
morgandu 0:11bc39d0f367 113 for(;n_index < n_len && pmap->pserial->writeable(); n_index++)
morgandu 0:11bc39d0f367 114 {
morgandu 0:11bc39d0f367 115 pmap->pserial->putc(n_buf[n_index]);
morgandu 0:11bc39d0f367 116 }
morgandu 0:11bc39d0f367 117 }
morgandu 0:11bc39d0f367 118
morgandu 0:11bc39d0f367 119 /*** Serial to Network ***/
morgandu 0:11bc39d0f367 120
morgandu 0:11bc39d0f367 121 if (pmap->pserial->readable())
morgandu 0:11bc39d0f367 122 {
morgandu 0:11bc39d0f367 123 // try to get more data from serial port
morgandu 0:11bc39d0f367 124 for(s_index = 0; s_index < sizeof(s_buf) && pmap->pserial->readable(); s_index++)
morgandu 0:11bc39d0f367 125 s_buf[s_index] = pmap->pserial->getc();
morgandu 0:11bc39d0f367 126
morgandu 0:11bc39d0f367 127 // send all data to network.
morgandu 0:11bc39d0f367 128 if (s_index > 0)
morgandu 0:11bc39d0f367 129 {
morgandu 0:11bc39d0f367 130 s_len = psocket->send(s_buf, s_index);
morgandu 0:11bc39d0f367 131 if (s_len == NSAPI_ERROR_WOULD_BLOCK)
morgandu 0:11bc39d0f367 132 {
morgandu 0:11bc39d0f367 133 printf("Socket Send no block.\r\n");
morgandu 0:11bc39d0f367 134 }
morgandu 0:11bc39d0f367 135 else if (s_len < 0)
morgandu 0:11bc39d0f367 136 {
morgandu 0:11bc39d0f367 137 printf("Socket Send Err (%d)\r\n", s_len);
morgandu 0:11bc39d0f367 138 psocket->close();
morgandu 0:11bc39d0f367 139 break;
morgandu 0:11bc39d0f367 140 }
morgandu 0:11bc39d0f367 141 else if (s_len != s_index)
morgandu 0:11bc39d0f367 142 {
morgandu 0:11bc39d0f367 143 printf("Socket Send not complete.\r\n");
morgandu 0:11bc39d0f367 144 psocket->close();
morgandu 0:11bc39d0f367 145 break;
morgandu 0:11bc39d0f367 146 }
morgandu 0:11bc39d0f367 147 }
morgandu 0:11bc39d0f367 148 }
morgandu 0:11bc39d0f367 149 }
morgandu 0:11bc39d0f367 150 }
morgandu 0:11bc39d0f367 151
morgandu 0:11bc39d0f367 152 void bridge_net_client(S_PORT_CONFIG *pmap)
morgandu 0:11bc39d0f367 153 {
morgandu 0:11bc39d0f367 154 TCPSocket socket;
morgandu 0:11bc39d0f367 155 SocketAddress server_address;
morgandu 0:11bc39d0f367 156 nsapi_error_t err;
morgandu 0:11bc39d0f367 157
morgandu 0:11bc39d0f367 158 printf("Thread %x in TCP client mode.\r\n", (unsigned int)pmap);
morgandu 0:11bc39d0f367 159
morgandu 0:11bc39d0f367 160 if ((err=socket.open(&eth)) < 0)
morgandu 0:11bc39d0f367 161 {
morgandu 0:11bc39d0f367 162 printf("TCP socket can't open (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 163 return;
morgandu 0:11bc39d0f367 164 }
morgandu 0:11bc39d0f367 165
morgandu 0:11bc39d0f367 166 printf("Connecting server %s:%d ...\r\n", pmap->server_addr, pmap->server_port);
morgandu 0:11bc39d0f367 167 while(1)
morgandu 0:11bc39d0f367 168 {
morgandu 0:11bc39d0f367 169 if ((err=socket.connect(pmap->server_addr, pmap->server_port)) >= 0)
morgandu 0:11bc39d0f367 170 break;
morgandu 0:11bc39d0f367 171 }
morgandu 0:11bc39d0f367 172
morgandu 0:11bc39d0f367 173 printf("\r\nConnected.");
morgandu 0:11bc39d0f367 174
morgandu 0:11bc39d0f367 175 socket.set_timeout(1);
morgandu 0:11bc39d0f367 176 exchange_data(pmap, &socket);
morgandu 0:11bc39d0f367 177 }
morgandu 0:11bc39d0f367 178
morgandu 0:11bc39d0f367 179 void bridge_net_server(S_PORT_CONFIG *pmap)
morgandu 0:11bc39d0f367 180 {
morgandu 0:11bc39d0f367 181 TCPServer tcp_server;
morgandu 0:11bc39d0f367 182 TCPSocket client_socket;
morgandu 0:11bc39d0f367 183 SocketAddress client_address;
morgandu 0:11bc39d0f367 184 nsapi_error_t err;
morgandu 0:11bc39d0f367 185
morgandu 0:11bc39d0f367 186 printf("Thread %x in TCP server mode.\r\n", (unsigned int)pmap);
morgandu 0:11bc39d0f367 187
morgandu 0:11bc39d0f367 188 if ((err=tcp_server.open(&eth)) < 0)
morgandu 0:11bc39d0f367 189 {
morgandu 0:11bc39d0f367 190 printf("TCP server can't open (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 191 return;
morgandu 0:11bc39d0f367 192 }
morgandu 0:11bc39d0f367 193 if ((err=tcp_server.bind(eth.get_ip_address(), pmap->port)) < 0)
morgandu 0:11bc39d0f367 194 {
morgandu 0:11bc39d0f367 195 printf("TCP server can't bind address and port (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 196 return;
morgandu 0:11bc39d0f367 197 }
morgandu 0:11bc39d0f367 198 if ((err=tcp_server.listen(1)) < 0)
morgandu 0:11bc39d0f367 199 {
morgandu 0:11bc39d0f367 200 printf("TCP server can't listen (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 201 return;
morgandu 0:11bc39d0f367 202 }
morgandu 0:11bc39d0f367 203
morgandu 0:11bc39d0f367 204 client_socket.set_timeout(1);
morgandu 0:11bc39d0f367 205
morgandu 0:11bc39d0f367 206 while(1)
morgandu 0:11bc39d0f367 207 {
morgandu 0:11bc39d0f367 208 if ((err=tcp_server.accept(&client_socket, &client_address)) < 0)
morgandu 0:11bc39d0f367 209 {
morgandu 0:11bc39d0f367 210 printf("TCP server fail to accept connection (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 211 return;
morgandu 0:11bc39d0f367 212 }
morgandu 0:11bc39d0f367 213
morgandu 0:11bc39d0f367 214 printf("Connect (%d) from %s:%d ...\r\n", pmap->port, client_address.get_ip_address(), client_address.get_port());
morgandu 0:11bc39d0f367 215
morgandu 0:11bc39d0f367 216 exchange_data(pmap, &client_socket);
morgandu 0:11bc39d0f367 217 }
morgandu 0:11bc39d0f367 218 }
morgandu 0:11bc39d0f367 219
morgandu 0:11bc39d0f367 220 int main()
morgandu 0:11bc39d0f367 221 {
morgandu 0:11bc39d0f367 222 /* Set the console baud-rate */
morgandu 0:11bc39d0f367 223 output.baud(115200);
morgandu 0:11bc39d0f367 224 printf("\r\nmbed OS version is %d.\r\n", MBED_VERSION);
morgandu 0:11bc39d0f367 225 printf("Start Serial-to-Ethernet...\r\n");
morgandu 0:11bc39d0f367 226
morgandu 0:11bc39d0f367 227 #ifdef ENABLE_WEB_CONFIG
morgandu 0:11bc39d0f367 228
morgandu 0:11bc39d0f367 229 /* Restore configuration from SD card */
morgandu 0:11bc39d0f367 230
morgandu 0:11bc39d0f367 231 SD_Card_Mounted = (fs.mount(&bd) >= 0);
morgandu 0:11bc39d0f367 232 if (SD_Card_Mounted)
morgandu 0:11bc39d0f367 233 {
morgandu 0:11bc39d0f367 234 FILE *fd = fopen(SER_CONFIG_FILE, "r");
morgandu 0:11bc39d0f367 235 if (fd != NULL)
morgandu 0:11bc39d0f367 236 {
morgandu 0:11bc39d0f367 237 char pBuf[sizeof(port_config)+2];
morgandu 0:11bc39d0f367 238 int len = fread(pBuf, 1, sizeof(port_config)+2, fd);
morgandu 0:11bc39d0f367 239 if (len == (sizeof(port_config)+2) && pBuf[0] == 'N' && pBuf[1] == 'T')
morgandu 0:11bc39d0f367 240 {
morgandu 0:11bc39d0f367 241 printf("Set Serial ports from config file in SD card.\r\n");
morgandu 0:11bc39d0f367 242 memcpy(port_config, pBuf+2, sizeof(port_config));
morgandu 0:11bc39d0f367 243 }
morgandu 0:11bc39d0f367 244 else
morgandu 0:11bc39d0f367 245 printf("Incorrect serial config file.\r\n");
morgandu 0:11bc39d0f367 246
morgandu 0:11bc39d0f367 247 fclose(fd);
morgandu 0:11bc39d0f367 248 }
morgandu 0:11bc39d0f367 249 else
morgandu 0:11bc39d0f367 250 printf("Can't open serial config file.\r\n");
morgandu 0:11bc39d0f367 251
morgandu 0:11bc39d0f367 252 fd = fopen(NET_CONFIG_FILE, "r");
morgandu 0:11bc39d0f367 253 if (fd != NULL)
morgandu 0:11bc39d0f367 254 {
morgandu 0:11bc39d0f367 255 char pBuf[sizeof(net_config)+2];
morgandu 0:11bc39d0f367 256 int len = fread(pBuf, 1, sizeof(net_config)+2, fd);
morgandu 0:11bc39d0f367 257 if (len == (sizeof(net_config)+2) && pBuf[0] == 'N' && pBuf[1] == 'T')
morgandu 0:11bc39d0f367 258 {
morgandu 0:11bc39d0f367 259 printf("Set network from config file in SD card.\r\n");
morgandu 0:11bc39d0f367 260 memcpy(&net_config, pBuf+2, sizeof(net_config));
morgandu 0:11bc39d0f367 261 }
morgandu 0:11bc39d0f367 262 else
morgandu 0:11bc39d0f367 263 printf("Incorrect network config file.\r\n");
morgandu 0:11bc39d0f367 264
morgandu 0:11bc39d0f367 265 fclose(fd);
morgandu 0:11bc39d0f367 266 }
morgandu 0:11bc39d0f367 267 else
morgandu 0:11bc39d0f367 268 printf("Can't open network config file.\r\n");
morgandu 0:11bc39d0f367 269 }
morgandu 0:11bc39d0f367 270 else
morgandu 0:11bc39d0f367 271 {
morgandu 0:11bc39d0f367 272 printf("Can't find SD card.\r\n");
morgandu 0:11bc39d0f367 273 }
morgandu 0:11bc39d0f367 274
morgandu 0:11bc39d0f367 275 #endif
morgandu 0:11bc39d0f367 276
morgandu 0:11bc39d0f367 277 printf("Configure UART ports...\r\n");
morgandu 0:11bc39d0f367 278 for(int i=0; i<MAX_UART_PORTS; i++)
morgandu 0:11bc39d0f367 279 {
morgandu 0:11bc39d0f367 280 port_config[i].pserial->baud(port_config[i].baud);
morgandu 0:11bc39d0f367 281 port_config[i].pserial->format(port_config[i].data, port_config[i].parity, port_config[i].stop);
morgandu 0:11bc39d0f367 282 }
morgandu 0:11bc39d0f367 283
morgandu 0:11bc39d0f367 284 if (net_config.mode == IP_STATIC_MODE)
morgandu 0:11bc39d0f367 285 {
morgandu 0:11bc39d0f367 286 printf("Start Ethernet in Static mode.\r\n");
morgandu 0:11bc39d0f367 287 eth.disconnect();
morgandu 0:11bc39d0f367 288 ((NetworkInterface *)&eth)->set_network(net_config.ip, net_config.mask, net_config.gateway);
morgandu 0:11bc39d0f367 289 }
morgandu 0:11bc39d0f367 290 else
morgandu 0:11bc39d0f367 291 printf("Start Ethernet in DHCP mode.\r\n");
morgandu 0:11bc39d0f367 292
morgandu 0:11bc39d0f367 293 eth.connect();
morgandu 0:11bc39d0f367 294 printf("IP Address is %s\r\n", eth.get_ip_address());
morgandu 0:11bc39d0f367 295
morgandu 0:11bc39d0f367 296 Thread thread[MAX_UART_PORTS];
morgandu 0:11bc39d0f367 297
morgandu 0:11bc39d0f367 298 // Folk thread for each port
morgandu 0:11bc39d0f367 299 for(int i=0; i<MAX_UART_PORTS; i++)
morgandu 0:11bc39d0f367 300 {
morgandu 0:11bc39d0f367 301 if (port_config[i].mode == NET_SERVER_MODE)
morgandu 0:11bc39d0f367 302 {
morgandu 0:11bc39d0f367 303 thread[i].start(callback(bridge_net_server, &(port_config[i])));
morgandu 0:11bc39d0f367 304 }
morgandu 0:11bc39d0f367 305 else // if (port_config[i].mode == TCP_CLIENT_MODE)
morgandu 0:11bc39d0f367 306 {
morgandu 0:11bc39d0f367 307 thread[i].start(callback(bridge_net_client, &(port_config[i])));
morgandu 0:11bc39d0f367 308 }
morgandu 0:11bc39d0f367 309 }
morgandu 0:11bc39d0f367 310
morgandu 0:11bc39d0f367 311 #ifdef ENABLE_WEB_CONFIG
morgandu 0:11bc39d0f367 312
morgandu 0:11bc39d0f367 313 /*** main thread to be a web server for configuration ***/
morgandu 0:11bc39d0f367 314 start_httpd();
morgandu 0:11bc39d0f367 315
morgandu 0:11bc39d0f367 316 #endif
morgandu 0:11bc39d0f367 317
morgandu 0:11bc39d0f367 318 while(1);
morgandu 0:11bc39d0f367 319
morgandu 0:11bc39d0f367 320 /* end of main task */
morgandu 0:11bc39d0f367 321 //eth.disconnect();
morgandu 0:11bc39d0f367 322 }