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