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 Feb 26 06:30:37 2021 +0000
Revision:
8:e9248126f512
Parent:
7:cbb5a2a2a0d2
Fix code to compliant Mbed OS 6 but still set to Mbed OS 5 as default.; Remove BufferedSerial library if switch to Mbed OS 6 before compile.

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 #include "ste_config.h"
morgandu 0:11bc39d0f367 9
morgandu 7:cbb5a2a2a0d2 10 /* If choose static IP, specify the default IP address here. */
morgandu 0:11bc39d0f367 11 #if 0
morgandu 0:11bc39d0f367 12 // private IP address for general purpose
morgandu 0:11bc39d0f367 13 #define IP_ADDRESS "192.168.1.2"
morgandu 0:11bc39d0f367 14 #define NETWORK_MASK "255.255.255.0"
morgandu 0:11bc39d0f367 15 #define GATEWAY_ADDRESS "192.168.1.1"
morgandu 0:11bc39d0f367 16 #else
morgandu 0:11bc39d0f367 17 // private IP address only for test with Windows when DHCP server doesn't exist.
morgandu 2:35f2cc8b05fa 18 // Windows set its LAN IP address to 169.254.xxx.xxx / 255.255.0.0
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 7:cbb5a2a2a0d2 24 /* Default IP configuration for Ethernet network */
morgandu 0:11bc39d0f367 25 S_NET_CONFIG net_config = {IP_STATIC_MODE, IP_ADDRESS, NETWORK_MASK, GATEWAY_ADDRESS};
morgandu 0:11bc39d0f367 26
morgandu 6:dc9f344f4bf0 27 #if defined (TARGET_NUMAKER_PFM_M487) || defined(TARGET_NUMAKER_IOT_M487)
morgandu 8:e9248126f512 28 #if MBED_MAJOR_VERSION <= 5
morgandu 5:7e7ad7c079fe 29 BufferedSerial serial_0(PB_3, PB_2, 256, 4); // UART1
morgandu 1:c53f82eb6b42 30 BufferedSerial serial_1(PA_5, PA_4, 256, 4); // UART5
morgandu 5:7e7ad7c079fe 31 //BufferedSerial serial_2(PB_13, PB_12, 256, 4); // UART0, Debug
morgandu 8:e9248126f512 32 #else
morgandu 8:e9248126f512 33 BufferedSerial serial_0(PB_3, PB_2); // UART1
morgandu 8:e9248126f512 34 BufferedSerial serial_1(PA_5, PA_4); // UART5
morgandu 8:e9248126f512 35 //BufferedSerial serial_2(PB_13, PB_12); // UART0, Debug
morgandu 8:e9248126f512 36 #endif
morgandu 0:11bc39d0f367 37
morgandu 0:11bc39d0f367 38 #elif defined (TARGET_NUMAKER_PFM_NUC472)
morgandu 8:e9248126f512 39 #if MBED_MAJOR_VERSION <= 5
morgandu 0:11bc39d0f367 40 BufferedSerial serial_0(PH_1, PH_0, 256, 4); // UART4
morgandu 0:11bc39d0f367 41 BufferedSerial serial_1(PG_2, PG_1, 256, 4); // UART0
morgandu 0:11bc39d0f367 42 BufferedSerial serial_2(PC_11, PC_10, 256, 4); // UART2
morgandu 8:e9248126f512 43 #else
morgandu 8:e9248126f512 44 BufferedSerial serial_0(PH_1, PH_0); // UART4
morgandu 8:e9248126f512 45 BufferedSerial serial_1(PG_2, PG_1); // UART0
morgandu 8:e9248126f512 46 BufferedSerial serial_2(PC_11, PC_10); // UART2
morgandu 8:e9248126f512 47 #endif
morgandu 0:11bc39d0f367 48
morgandu 7:cbb5a2a2a0d2 49 #elif defined (TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_NANO130) || defined(TARGET_NUMAKER_PFM_M2351)
morgandu 0:11bc39d0f367 50 #error The board has no Ethernet.
morgandu 0:11bc39d0f367 51
morgandu 0:11bc39d0f367 52 #else
morgandu 0:11bc39d0f367 53 #error define UART ports for your board.
morgandu 0:11bc39d0f367 54 #endif
morgandu 0:11bc39d0f367 55
morgandu 0:11bc39d0f367 56 /* Default configuration for network ports and UART ports, etc. */
morgandu 0:11bc39d0f367 57 S_PORT_CONFIG port_config[MAX_UART_PORTS] = {
morgandu 0:11bc39d0f367 58
morgandu 0:11bc39d0f367 59 #if MAX_UART_PORTS == 1
morgandu 0:11bc39d0f367 60 {NET_SERVER_MODE, NET_PORT_BASE, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}
morgandu 0:11bc39d0f367 61
morgandu 0:11bc39d0f367 62 #elif MAX_UART_PORTS == 2
morgandu 0:11bc39d0f367 63 {NET_SERVER_MODE, NET_PORT_BASE + 0, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None},
morgandu 0:11bc39d0f367 64 {NET_SERVER_MODE, NET_PORT_BASE + 1, &serial_1, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}
morgandu 0:11bc39d0f367 65
morgandu 0:11bc39d0f367 66 #elif MAX_UART_PORTS == 3
morgandu 0:11bc39d0f367 67 {NET_SERVER_MODE, NET_PORT_BASE + 0, &serial_0, DEFAULT_UART_BAUD, 8, 1, SerialBase::None},
morgandu 0:11bc39d0f367 68 {NET_SERVER_MODE, NET_PORT_BASE + 1, &serial_1, DEFAULT_UART_BAUD, 8, 1, SerialBase::None},
morgandu 0:11bc39d0f367 69 {NET_SERVER_MODE, NET_PORT_BASE + 2, &serial_2, DEFAULT_UART_BAUD, 8, 1, SerialBase::None}
morgandu 0:11bc39d0f367 70
morgandu 0:11bc39d0f367 71 #else
morgandu 0:11bc39d0f367 72 #error You have to define ports mapping table.
morgandu 0:11bc39d0f367 73 #endif
morgandu 0:11bc39d0f367 74 };
morgandu 0:11bc39d0f367 75
morgandu 0:11bc39d0f367 76 EthernetInterface eth;
morgandu 0:11bc39d0f367 77
morgandu 7:cbb5a2a2a0d2 78 #if ENABLE_WEB_CONFIG
morgandu 0:11bc39d0f367 79
morgandu 0:11bc39d0f367 80 /* Declare the SD card as storage */
morgandu 7:cbb5a2a2a0d2 81 NuSDBlockDevice *bd = new NuSDBlockDevice();
morgandu 0:11bc39d0f367 82 FATFileSystem fs("fs");
morgandu 0:11bc39d0f367 83 bool SD_Card_Mounted = FALSE;
morgandu 0:11bc39d0f367 84
morgandu 0:11bc39d0f367 85 #endif
morgandu 0:11bc39d0f367 86
morgandu 0:11bc39d0f367 87 /* --- --- */
morgandu 0:11bc39d0f367 88
morgandu 0:11bc39d0f367 89 /*
morgandu 0:11bc39d0f367 90 * Forward serial port data to ethernet, and vice versa.
morgandu 0:11bc39d0f367 91 *
morgandu 0:11bc39d0f367 92 */
morgandu 0:11bc39d0f367 93 void exchange_data(S_PORT_CONFIG *pmap, TCPSocket *psocket)
morgandu 0:11bc39d0f367 94 {
morgandu 0:11bc39d0f367 95 unsigned char n_buf[256];
morgandu 0:11bc39d0f367 96 unsigned char s_buf[256];
morgandu 0:11bc39d0f367 97 int n_len = 0, n_index = 0;
morgandu 0:11bc39d0f367 98 int s_len = 0, s_index = 0;
morgandu 1:c53f82eb6b42 99 unsigned int eth_tx_count = 0;
morgandu 0:11bc39d0f367 100
morgandu 0:11bc39d0f367 101 while(1)
morgandu 0:11bc39d0f367 102 {
morgandu 0:11bc39d0f367 103 /*** Network to Serial ***/
morgandu 0:11bc39d0f367 104
morgandu 1:c53f82eb6b42 105 if (n_len == 0)
morgandu 0:11bc39d0f367 106 {
morgandu 0:11bc39d0f367 107 // net buffer is empty, try to get new data from network.
morgandu 0:11bc39d0f367 108 n_len = psocket->recv(n_buf, sizeof(n_buf));
morgandu 1:c53f82eb6b42 109 if (n_len == 0)
morgandu 0:11bc39d0f367 110 {
morgandu 1:c53f82eb6b42 111 eth_tx_count += 3;
morgandu 1:c53f82eb6b42 112 }
morgandu 1:c53f82eb6b42 113 else if (n_len == NSAPI_ERROR_WOULD_BLOCK)
morgandu 1:c53f82eb6b42 114 {
morgandu 1:c53f82eb6b42 115 n_len = 0;
morgandu 0:11bc39d0f367 116 }
morgandu 0:11bc39d0f367 117 else if (n_len < 0)
morgandu 0:11bc39d0f367 118 {
morgandu 0:11bc39d0f367 119 printf("Socket Recv Err (%d)\r\n", n_len);
morgandu 0:11bc39d0f367 120 break;
morgandu 0:11bc39d0f367 121 }
morgandu 0:11bc39d0f367 122 }
morgandu 0:11bc39d0f367 123 else
morgandu 0:11bc39d0f367 124 {
morgandu 1:c53f82eb6b42 125 n_index += pmap->pserial->write(n_buf+n_index, n_len-n_index);
morgandu 1:c53f82eb6b42 126 if (n_index == n_len)
morgandu 0:11bc39d0f367 127 {
morgandu 1:c53f82eb6b42 128 n_len = 0;
morgandu 1:c53f82eb6b42 129 n_index = 0;
morgandu 0:11bc39d0f367 130 }
morgandu 0:11bc39d0f367 131 }
morgandu 0:11bc39d0f367 132
morgandu 0:11bc39d0f367 133 /*** Serial to Network ***/
morgandu 1:c53f82eb6b42 134
morgandu 1:c53f82eb6b42 135 // try to get more data from serial port
morgandu 8:e9248126f512 136 #if MBED_MAJOR_VERSION <= 5
morgandu 1:c53f82eb6b42 137 for(; s_index < sizeof(s_buf) && pmap->pserial->readable(); s_index++)
morgandu 1:c53f82eb6b42 138 s_buf[s_index] = pmap->pserial->getc();
morgandu 8:e9248126f512 139 #else
morgandu 8:e9248126f512 140 if (pmap->pserial->readable())
morgandu 8:e9248126f512 141 {
morgandu 8:e9248126f512 142 s_len = pmap->pserial->read(s_buf+s_index, sizeof(s_buf)-s_index);
morgandu 8:e9248126f512 143 if (s_len > 0)
morgandu 8:e9248126f512 144 s_index += s_len;
morgandu 8:e9248126f512 145 }
morgandu 8:e9248126f512 146 #endif
morgandu 0:11bc39d0f367 147
morgandu 1:c53f82eb6b42 148 if (s_index >= 240 || (s_index != 0 && ++eth_tx_count >= 5))
morgandu 0:11bc39d0f367 149 {
morgandu 1:c53f82eb6b42 150 s_len = psocket->send(s_buf, s_index);
morgandu 1:c53f82eb6b42 151
morgandu 1:c53f82eb6b42 152 if (s_len == NSAPI_ERROR_WOULD_BLOCK)
morgandu 1:c53f82eb6b42 153 {
morgandu 1:c53f82eb6b42 154 printf("Socket Send no block.\r\n");
morgandu 1:c53f82eb6b42 155 }
morgandu 1:c53f82eb6b42 156 else if (s_len < 0)
morgandu 0:11bc39d0f367 157 {
morgandu 1:c53f82eb6b42 158 printf("Socket Send Err (%d)\r\n", s_len);
morgandu 1:c53f82eb6b42 159 break;
morgandu 0:11bc39d0f367 160 }
morgandu 1:c53f82eb6b42 161 else // s_len >= s_index
morgandu 1:c53f82eb6b42 162 {
morgandu 1:c53f82eb6b42 163 unsigned int i;
morgandu 1:c53f82eb6b42 164
morgandu 1:c53f82eb6b42 165 // move remain data if existed.
morgandu 1:c53f82eb6b42 166 for(i=0; s_len < s_index; i++, s_len++)
morgandu 1:c53f82eb6b42 167 s_buf[i] = s_buf[s_len];
morgandu 1:c53f82eb6b42 168
morgandu 1:c53f82eb6b42 169 s_index = i;
morgandu 1:c53f82eb6b42 170 eth_tx_count = 0;
morgandu 1:c53f82eb6b42 171 }
morgandu 1:c53f82eb6b42 172 }
morgandu 0:11bc39d0f367 173 }
morgandu 0:11bc39d0f367 174 }
morgandu 1:c53f82eb6b42 175
morgandu 0:11bc39d0f367 176 void bridge_net_client(S_PORT_CONFIG *pmap)
morgandu 0:11bc39d0f367 177 {
morgandu 0:11bc39d0f367 178 TCPSocket socket;
morgandu 7:cbb5a2a2a0d2 179 SocketAddress server_ipaddr;
morgandu 0:11bc39d0f367 180 nsapi_error_t err;
morgandu 0:11bc39d0f367 181
morgandu 0:11bc39d0f367 182 printf("Thread %x in TCP client mode.\r\n", (unsigned int)pmap);
morgandu 0:11bc39d0f367 183
morgandu 8:e9248126f512 184 if ((err=socket.open(&eth)) != NSAPI_ERROR_OK)
morgandu 0:11bc39d0f367 185 {
morgandu 0:11bc39d0f367 186 printf("TCP socket can't open (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 187 return;
morgandu 0:11bc39d0f367 188 }
morgandu 0:11bc39d0f367 189
morgandu 0:11bc39d0f367 190 printf("Connecting server %s:%d ...\r\n", pmap->server_addr, pmap->server_port);
morgandu 0:11bc39d0f367 191 while(1)
morgandu 0:11bc39d0f367 192 {
morgandu 7:cbb5a2a2a0d2 193 server_ipaddr.set_ip_address(pmap->server_addr);
morgandu 7:cbb5a2a2a0d2 194 server_ipaddr.set_port(pmap->server_port);
morgandu 7:cbb5a2a2a0d2 195 if ((err=socket.connect(server_ipaddr)) >= 0)
morgandu 1:c53f82eb6b42 196 {
morgandu 1:c53f82eb6b42 197 printf("\r\nConnected.");
morgandu 0:11bc39d0f367 198 break;
morgandu 1:c53f82eb6b42 199 }
morgandu 0:11bc39d0f367 200 }
morgandu 0:11bc39d0f367 201
morgandu 0:11bc39d0f367 202 socket.set_timeout(1);
morgandu 0:11bc39d0f367 203 exchange_data(pmap, &socket);
morgandu 6:dc9f344f4bf0 204 socket.close();
morgandu 0:11bc39d0f367 205 }
morgandu 0:11bc39d0f367 206
morgandu 0:11bc39d0f367 207 void bridge_net_server(S_PORT_CONFIG *pmap)
morgandu 0:11bc39d0f367 208 {
morgandu 6:dc9f344f4bf0 209 TCPSocket tcp_server;
morgandu 6:dc9f344f4bf0 210 TCPSocket *client_socket;
morgandu 7:cbb5a2a2a0d2 211 SocketAddress client_ipaddr;
morgandu 0:11bc39d0f367 212 nsapi_error_t err;
morgandu 0:11bc39d0f367 213
morgandu 0:11bc39d0f367 214 printf("Thread %x in TCP server mode.\r\n", (unsigned int)pmap);
morgandu 0:11bc39d0f367 215
morgandu 8:e9248126f512 216 if ((err=tcp_server.open(&eth)) != NSAPI_ERROR_OK)
morgandu 0:11bc39d0f367 217 {
morgandu 0:11bc39d0f367 218 printf("TCP server can't open (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 219 return;
morgandu 0:11bc39d0f367 220 }
morgandu 8:e9248126f512 221 if ((err=tcp_server.bind(pmap->port)) != NSAPI_ERROR_OK)
morgandu 0:11bc39d0f367 222 {
morgandu 0:11bc39d0f367 223 printf("TCP server can't bind address and port (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 224 return;
morgandu 0:11bc39d0f367 225 }
morgandu 0:11bc39d0f367 226 if ((err=tcp_server.listen(1)) < 0)
morgandu 0:11bc39d0f367 227 {
morgandu 0:11bc39d0f367 228 printf("TCP server can't listen (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 229 return;
morgandu 0:11bc39d0f367 230 }
morgandu 0:11bc39d0f367 231
morgandu 0:11bc39d0f367 232 while(1)
morgandu 0:11bc39d0f367 233 {
morgandu 6:dc9f344f4bf0 234 client_socket = tcp_server.accept(&err);
morgandu 8:e9248126f512 235 if (err != NSAPI_ERROR_OK && err != NSAPI_ERROR_WOULD_BLOCK)
morgandu 0:11bc39d0f367 236 {
morgandu 0:11bc39d0f367 237 printf("TCP server fail to accept connection (%d)(%x).\r\n", err, (unsigned int)pmap);
morgandu 0:11bc39d0f367 238 return;
morgandu 0:11bc39d0f367 239 }
morgandu 6:dc9f344f4bf0 240 else
morgandu 6:dc9f344f4bf0 241 {
morgandu 7:cbb5a2a2a0d2 242 client_socket->getpeername(&client_ipaddr);
morgandu 7:cbb5a2a2a0d2 243 printf("Connect (%d) from %s:%d ...\r\n", pmap->port, client_ipaddr.get_ip_address(), client_ipaddr.get_port());
morgandu 6:dc9f344f4bf0 244
morgandu 6:dc9f344f4bf0 245 client_socket->set_timeout(1);
morgandu 6:dc9f344f4bf0 246 exchange_data(pmap, client_socket);
morgandu 6:dc9f344f4bf0 247 client_socket->close();
morgandu 6:dc9f344f4bf0 248 }
morgandu 0:11bc39d0f367 249 }
morgandu 0:11bc39d0f367 250 }
morgandu 0:11bc39d0f367 251
morgandu 0:11bc39d0f367 252 int main()
morgandu 0:11bc39d0f367 253 {
morgandu 7:cbb5a2a2a0d2 254 SocketAddress ip_addr;
morgandu 7:cbb5a2a2a0d2 255 SocketAddress ip_mask;
morgandu 7:cbb5a2a2a0d2 256 SocketAddress ip_gwaddr;
morgandu 7:cbb5a2a2a0d2 257
morgandu 0:11bc39d0f367 258 printf("\r\nmbed OS version is %d.\r\n", MBED_VERSION);
morgandu 0:11bc39d0f367 259 printf("Start Serial-to-Ethernet...\r\n");
morgandu 0:11bc39d0f367 260
morgandu 7:cbb5a2a2a0d2 261 #if ENABLE_WEB_CONFIG
morgandu 0:11bc39d0f367 262
morgandu 0:11bc39d0f367 263 /* Restore configuration from SD card */
morgandu 0:11bc39d0f367 264
morgandu 7:cbb5a2a2a0d2 265 printf("Mounting SD card...\r\n");
morgandu 7:cbb5a2a2a0d2 266 SD_Card_Mounted = (fs.mount(bd) == 0);
morgandu 0:11bc39d0f367 267 if (SD_Card_Mounted)
morgandu 0:11bc39d0f367 268 {
morgandu 7:cbb5a2a2a0d2 269 printf("SD card mounted. Read configuration file...\r\n");
morgandu 0:11bc39d0f367 270 FILE *fd = fopen(SER_CONFIG_FILE, "r");
morgandu 0:11bc39d0f367 271 if (fd != NULL)
morgandu 0:11bc39d0f367 272 {
morgandu 0:11bc39d0f367 273 char pBuf[sizeof(port_config)+2];
morgandu 0:11bc39d0f367 274 int len = fread(pBuf, 1, sizeof(port_config)+2, fd);
morgandu 0:11bc39d0f367 275 if (len == (sizeof(port_config)+2) && pBuf[0] == 'N' && pBuf[1] == 'T')
morgandu 0:11bc39d0f367 276 {
morgandu 0:11bc39d0f367 277 printf("Set Serial ports from config file in SD card.\r\n");
morgandu 0:11bc39d0f367 278 memcpy(port_config, pBuf+2, sizeof(port_config));
morgandu 0:11bc39d0f367 279 }
morgandu 0:11bc39d0f367 280 else
morgandu 0:11bc39d0f367 281 printf("Incorrect serial config file.\r\n");
morgandu 0:11bc39d0f367 282
morgandu 0:11bc39d0f367 283 fclose(fd);
morgandu 0:11bc39d0f367 284 }
morgandu 0:11bc39d0f367 285 else
morgandu 0:11bc39d0f367 286 printf("Can't open serial config file.\r\n");
morgandu 0:11bc39d0f367 287
morgandu 0:11bc39d0f367 288 fd = fopen(NET_CONFIG_FILE, "r");
morgandu 0:11bc39d0f367 289 if (fd != NULL)
morgandu 0:11bc39d0f367 290 {
morgandu 0:11bc39d0f367 291 char pBuf[sizeof(net_config)+2];
morgandu 0:11bc39d0f367 292 int len = fread(pBuf, 1, sizeof(net_config)+2, fd);
morgandu 0:11bc39d0f367 293 if (len == (sizeof(net_config)+2) && pBuf[0] == 'N' && pBuf[1] == 'T')
morgandu 0:11bc39d0f367 294 {
morgandu 0:11bc39d0f367 295 printf("Set network from config file in SD card.\r\n");
morgandu 0:11bc39d0f367 296 memcpy(&net_config, pBuf+2, sizeof(net_config));
morgandu 0:11bc39d0f367 297 }
morgandu 0:11bc39d0f367 298 else
morgandu 0:11bc39d0f367 299 printf("Incorrect network config file.\r\n");
morgandu 0:11bc39d0f367 300
morgandu 0:11bc39d0f367 301 fclose(fd);
morgandu 0:11bc39d0f367 302 }
morgandu 0:11bc39d0f367 303 else
morgandu 0:11bc39d0f367 304 printf("Can't open network config file.\r\n");
morgandu 0:11bc39d0f367 305 }
morgandu 0:11bc39d0f367 306 else
morgandu 0:11bc39d0f367 307 {
morgandu 0:11bc39d0f367 308 printf("Can't find SD card.\r\n");
morgandu 0:11bc39d0f367 309 }
morgandu 0:11bc39d0f367 310
morgandu 0:11bc39d0f367 311 #endif
morgandu 0:11bc39d0f367 312
morgandu 7:cbb5a2a2a0d2 313 /* Configure serial ports */
morgandu 0:11bc39d0f367 314 printf("Configure UART ports...\r\n");
morgandu 0:11bc39d0f367 315 for(int i=0; i<MAX_UART_PORTS; i++)
morgandu 0:11bc39d0f367 316 {
morgandu 8:e9248126f512 317 #if MBED_MAJOR_VERSION <= 5
morgandu 0:11bc39d0f367 318 port_config[i].pserial->baud(port_config[i].baud);
morgandu 0:11bc39d0f367 319 port_config[i].pserial->format(port_config[i].data, port_config[i].parity, port_config[i].stop);
morgandu 8:e9248126f512 320 #else
morgandu 8:e9248126f512 321 port_config[i].pserial->set_baud(port_config[i].baud);
morgandu 8:e9248126f512 322 port_config[i].pserial->set_format(port_config[i].data, port_config[i].parity, port_config[i].stop);
morgandu 8:e9248126f512 323 #endif
morgandu 0:11bc39d0f367 324 }
morgandu 0:11bc39d0f367 325
morgandu 7:cbb5a2a2a0d2 326 /* Configure network IP address */
morgandu 0:11bc39d0f367 327 if (net_config.mode == IP_STATIC_MODE)
morgandu 0:11bc39d0f367 328 {
morgandu 0:11bc39d0f367 329 printf("Start Ethernet in Static mode.\r\n");
morgandu 0:11bc39d0f367 330 eth.disconnect();
morgandu 7:cbb5a2a2a0d2 331
morgandu 7:cbb5a2a2a0d2 332 ip_addr.set_ip_address(net_config.ip);
morgandu 7:cbb5a2a2a0d2 333 ip_mask.set_ip_address(net_config.mask);
morgandu 7:cbb5a2a2a0d2 334 ip_gwaddr.set_ip_address(net_config.gateway);
morgandu 7:cbb5a2a2a0d2 335 ((NetworkInterface *)&eth)->set_network(ip_addr, ip_mask, ip_gwaddr);
morgandu 0:11bc39d0f367 336 }
morgandu 0:11bc39d0f367 337 else
morgandu 0:11bc39d0f367 338 printf("Start Ethernet in DHCP mode.\r\n");
morgandu 0:11bc39d0f367 339
morgandu 0:11bc39d0f367 340 eth.connect();
morgandu 7:cbb5a2a2a0d2 341 eth.get_ip_address(&ip_addr);
morgandu 7:cbb5a2a2a0d2 342 printf("IP Address is %s\r\n", ip_addr.get_ip_address());
morgandu 5:7e7ad7c079fe 343
morgandu 0:11bc39d0f367 344 Thread thread[MAX_UART_PORTS];
morgandu 0:11bc39d0f367 345
morgandu 7:cbb5a2a2a0d2 346 /* Folk thread for each serial port */
morgandu 0:11bc39d0f367 347 for(int i=0; i<MAX_UART_PORTS; i++)
morgandu 0:11bc39d0f367 348 {
morgandu 0:11bc39d0f367 349 if (port_config[i].mode == NET_SERVER_MODE)
morgandu 0:11bc39d0f367 350 {
morgandu 0:11bc39d0f367 351 thread[i].start(callback(bridge_net_server, &(port_config[i])));
morgandu 0:11bc39d0f367 352 }
morgandu 0:11bc39d0f367 353 else // if (port_config[i].mode == TCP_CLIENT_MODE)
morgandu 0:11bc39d0f367 354 {
morgandu 0:11bc39d0f367 355 thread[i].start(callback(bridge_net_client, &(port_config[i])));
morgandu 0:11bc39d0f367 356 }
morgandu 0:11bc39d0f367 357 }
morgandu 0:11bc39d0f367 358
morgandu 7:cbb5a2a2a0d2 359 #if ENABLE_WEB_CONFIG
morgandu 0:11bc39d0f367 360
morgandu 0:11bc39d0f367 361 /*** main thread to be a web server for configuration ***/
morgandu 0:11bc39d0f367 362 start_httpd();
morgandu 0:11bc39d0f367 363
morgandu 0:11bc39d0f367 364 #endif
morgandu 0:11bc39d0f367 365
morgandu 0:11bc39d0f367 366 while(1);
morgandu 0:11bc39d0f367 367
morgandu 0:11bc39d0f367 368 /* end of main task */
morgandu 0:11bc39d0f367 369 //eth.disconnect();
morgandu 0:11bc39d0f367 370 }