rs485 to ethernet
Dependencies: BufferedSerialttt
Fork of Serial-to-Ethernet by
main.cpp@1:c53f82eb6b42, 2017-09-01 (annotated)
- Committer:
- morgandu
- Date:
- Fri Sep 01 02:30:10 2017 +0000
- Revision:
- 1:c53f82eb6b42
- Parent:
- 0:11bc39d0f367
- Child:
- 2:1af7ad1f058a
* Add UART port definition for NuMaker-PFM-M487; * Improve data transfer stability
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) |
morgandu | 1:c53f82eb6b42 | 28 | BufferedSerial serial_0(PH_8, PH_9, 256, 4); // UART1 |
morgandu | 1:c53f82eb6b42 | 29 | BufferedSerial serial_1(PA_5, PA_4, 256, 4); // UART5 |
morgandu | 1:c53f82eb6b42 | 30 | //BufferedSerial serial_1(PC_12, PC_11, 256, 4); // UART0, Debug |
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 | 1:c53f82eb6b42 | 37 | #elif defined (TARGET_NUMAKER_PFM_M453) || defined(TARGET_NUMAKER_PFM_NANO130) |
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 | 1:c53f82eb6b42 | 89 | unsigned int eth_tx_count = 0; |
morgandu | 0:11bc39d0f367 | 90 | |
morgandu | 0:11bc39d0f367 | 91 | while(1) |
morgandu | 0:11bc39d0f367 | 92 | { |
morgandu | 0:11bc39d0f367 | 93 | /*** Network to Serial ***/ |
morgandu | 0:11bc39d0f367 | 94 | |
morgandu | 1:c53f82eb6b42 | 95 | if (n_len == 0) |
morgandu | 0:11bc39d0f367 | 96 | { |
morgandu | 0:11bc39d0f367 | 97 | // net buffer is empty, try to get new data from network. |
morgandu | 0:11bc39d0f367 | 98 | n_len = psocket->recv(n_buf, sizeof(n_buf)); |
morgandu | 1:c53f82eb6b42 | 99 | if (n_len == 0) |
morgandu | 0:11bc39d0f367 | 100 | { |
morgandu | 1:c53f82eb6b42 | 101 | eth_tx_count += 3; |
morgandu | 1:c53f82eb6b42 | 102 | } |
morgandu | 1:c53f82eb6b42 | 103 | else if (n_len == NSAPI_ERROR_WOULD_BLOCK) |
morgandu | 1:c53f82eb6b42 | 104 | { |
morgandu | 1:c53f82eb6b42 | 105 | n_len = 0; |
morgandu | 0:11bc39d0f367 | 106 | } |
morgandu | 0:11bc39d0f367 | 107 | else if (n_len < 0) |
morgandu | 0:11bc39d0f367 | 108 | { |
morgandu | 0:11bc39d0f367 | 109 | printf("Socket Recv Err (%d)\r\n", n_len); |
morgandu | 0:11bc39d0f367 | 110 | psocket->close(); |
morgandu | 0:11bc39d0f367 | 111 | break; |
morgandu | 0:11bc39d0f367 | 112 | } |
morgandu | 0:11bc39d0f367 | 113 | } |
morgandu | 0:11bc39d0f367 | 114 | else |
morgandu | 0:11bc39d0f367 | 115 | { |
morgandu | 1:c53f82eb6b42 | 116 | n_index += pmap->pserial->write(n_buf+n_index, n_len-n_index); |
morgandu | 1:c53f82eb6b42 | 117 | if (n_index == n_len) |
morgandu | 0:11bc39d0f367 | 118 | { |
morgandu | 1:c53f82eb6b42 | 119 | n_len = 0; |
morgandu | 1:c53f82eb6b42 | 120 | n_index = 0; |
morgandu | 0:11bc39d0f367 | 121 | } |
morgandu | 0:11bc39d0f367 | 122 | } |
morgandu | 0:11bc39d0f367 | 123 | |
morgandu | 0:11bc39d0f367 | 124 | /*** Serial to Network ***/ |
morgandu | 1:c53f82eb6b42 | 125 | |
morgandu | 1:c53f82eb6b42 | 126 | // try to get more data from serial port |
morgandu | 1:c53f82eb6b42 | 127 | for(; s_index < sizeof(s_buf) && pmap->pserial->readable(); s_index++) |
morgandu | 1:c53f82eb6b42 | 128 | s_buf[s_index] = pmap->pserial->getc(); |
morgandu | 0:11bc39d0f367 | 129 | |
morgandu | 1:c53f82eb6b42 | 130 | if (s_index >= 240 || (s_index != 0 && ++eth_tx_count >= 5)) |
morgandu | 0:11bc39d0f367 | 131 | { |
morgandu | 1:c53f82eb6b42 | 132 | s_len = psocket->send(s_buf, s_index); |
morgandu | 1:c53f82eb6b42 | 133 | |
morgandu | 1:c53f82eb6b42 | 134 | if (s_len == NSAPI_ERROR_WOULD_BLOCK) |
morgandu | 1:c53f82eb6b42 | 135 | { |
morgandu | 1:c53f82eb6b42 | 136 | printf("Socket Send no block.\r\n"); |
morgandu | 1:c53f82eb6b42 | 137 | } |
morgandu | 1:c53f82eb6b42 | 138 | else if (s_len < 0) |
morgandu | 0:11bc39d0f367 | 139 | { |
morgandu | 1:c53f82eb6b42 | 140 | printf("Socket Send Err (%d)\r\n", s_len); |
morgandu | 1:c53f82eb6b42 | 141 | psocket->close(); |
morgandu | 1:c53f82eb6b42 | 142 | break; |
morgandu | 0:11bc39d0f367 | 143 | } |
morgandu | 1:c53f82eb6b42 | 144 | else // s_len >= s_index |
morgandu | 1:c53f82eb6b42 | 145 | { |
morgandu | 1:c53f82eb6b42 | 146 | unsigned int i; |
morgandu | 1:c53f82eb6b42 | 147 | |
morgandu | 1:c53f82eb6b42 | 148 | // move remain data if existed. |
morgandu | 1:c53f82eb6b42 | 149 | for(i=0; s_len < s_index; i++, s_len++) |
morgandu | 1:c53f82eb6b42 | 150 | s_buf[i] = s_buf[s_len]; |
morgandu | 1:c53f82eb6b42 | 151 | |
morgandu | 1:c53f82eb6b42 | 152 | s_index = i; |
morgandu | 1:c53f82eb6b42 | 153 | eth_tx_count = 0; |
morgandu | 1:c53f82eb6b42 | 154 | } |
morgandu | 1:c53f82eb6b42 | 155 | } |
morgandu | 0:11bc39d0f367 | 156 | } |
morgandu | 0:11bc39d0f367 | 157 | } |
morgandu | 1:c53f82eb6b42 | 158 | |
morgandu | 0:11bc39d0f367 | 159 | void bridge_net_client(S_PORT_CONFIG *pmap) |
morgandu | 0:11bc39d0f367 | 160 | { |
morgandu | 0:11bc39d0f367 | 161 | TCPSocket socket; |
morgandu | 0:11bc39d0f367 | 162 | SocketAddress server_address; |
morgandu | 0:11bc39d0f367 | 163 | nsapi_error_t err; |
morgandu | 0:11bc39d0f367 | 164 | |
morgandu | 0:11bc39d0f367 | 165 | printf("Thread %x in TCP client mode.\r\n", (unsigned int)pmap); |
morgandu | 0:11bc39d0f367 | 166 | |
morgandu | 0:11bc39d0f367 | 167 | if ((err=socket.open(ð)) < 0) |
morgandu | 0:11bc39d0f367 | 168 | { |
morgandu | 0:11bc39d0f367 | 169 | printf("TCP socket can't open (%d)(%x).\r\n", err, (unsigned int)pmap); |
morgandu | 0:11bc39d0f367 | 170 | return; |
morgandu | 0:11bc39d0f367 | 171 | } |
morgandu | 0:11bc39d0f367 | 172 | |
morgandu | 0:11bc39d0f367 | 173 | printf("Connecting server %s:%d ...\r\n", pmap->server_addr, pmap->server_port); |
morgandu | 0:11bc39d0f367 | 174 | while(1) |
morgandu | 0:11bc39d0f367 | 175 | { |
morgandu | 0:11bc39d0f367 | 176 | if ((err=socket.connect(pmap->server_addr, pmap->server_port)) >= 0) |
morgandu | 1:c53f82eb6b42 | 177 | { |
morgandu | 1:c53f82eb6b42 | 178 | printf("\r\nConnected."); |
morgandu | 0:11bc39d0f367 | 179 | break; |
morgandu | 1:c53f82eb6b42 | 180 | } |
morgandu | 0:11bc39d0f367 | 181 | } |
morgandu | 0:11bc39d0f367 | 182 | |
morgandu | 0:11bc39d0f367 | 183 | socket.set_timeout(1); |
morgandu | 0:11bc39d0f367 | 184 | exchange_data(pmap, &socket); |
morgandu | 0:11bc39d0f367 | 185 | } |
morgandu | 0:11bc39d0f367 | 186 | |
morgandu | 0:11bc39d0f367 | 187 | void bridge_net_server(S_PORT_CONFIG *pmap) |
morgandu | 0:11bc39d0f367 | 188 | { |
morgandu | 0:11bc39d0f367 | 189 | TCPServer tcp_server; |
morgandu | 0:11bc39d0f367 | 190 | TCPSocket client_socket; |
morgandu | 0:11bc39d0f367 | 191 | SocketAddress client_address; |
morgandu | 0:11bc39d0f367 | 192 | nsapi_error_t err; |
morgandu | 0:11bc39d0f367 | 193 | |
morgandu | 0:11bc39d0f367 | 194 | printf("Thread %x in TCP server mode.\r\n", (unsigned int)pmap); |
morgandu | 0:11bc39d0f367 | 195 | |
morgandu | 0:11bc39d0f367 | 196 | if ((err=tcp_server.open(ð)) < 0) |
morgandu | 0:11bc39d0f367 | 197 | { |
morgandu | 0:11bc39d0f367 | 198 | printf("TCP server can't open (%d)(%x).\r\n", err, (unsigned int)pmap); |
morgandu | 0:11bc39d0f367 | 199 | return; |
morgandu | 0:11bc39d0f367 | 200 | } |
morgandu | 0:11bc39d0f367 | 201 | if ((err=tcp_server.bind(eth.get_ip_address(), pmap->port)) < 0) |
morgandu | 0:11bc39d0f367 | 202 | { |
morgandu | 0:11bc39d0f367 | 203 | printf("TCP server can't bind address and port (%d)(%x).\r\n", err, (unsigned int)pmap); |
morgandu | 0:11bc39d0f367 | 204 | return; |
morgandu | 0:11bc39d0f367 | 205 | } |
morgandu | 0:11bc39d0f367 | 206 | if ((err=tcp_server.listen(1)) < 0) |
morgandu | 0:11bc39d0f367 | 207 | { |
morgandu | 0:11bc39d0f367 | 208 | printf("TCP server can't listen (%d)(%x).\r\n", err, (unsigned int)pmap); |
morgandu | 0:11bc39d0f367 | 209 | return; |
morgandu | 0:11bc39d0f367 | 210 | } |
morgandu | 0:11bc39d0f367 | 211 | |
morgandu | 0:11bc39d0f367 | 212 | client_socket.set_timeout(1); |
morgandu | 0:11bc39d0f367 | 213 | |
morgandu | 0:11bc39d0f367 | 214 | while(1) |
morgandu | 0:11bc39d0f367 | 215 | { |
morgandu | 0:11bc39d0f367 | 216 | if ((err=tcp_server.accept(&client_socket, &client_address)) < 0) |
morgandu | 0:11bc39d0f367 | 217 | { |
morgandu | 0:11bc39d0f367 | 218 | printf("TCP server fail to accept connection (%d)(%x).\r\n", err, (unsigned int)pmap); |
morgandu | 0:11bc39d0f367 | 219 | return; |
morgandu | 0:11bc39d0f367 | 220 | } |
morgandu | 0:11bc39d0f367 | 221 | |
morgandu | 0:11bc39d0f367 | 222 | printf("Connect (%d) from %s:%d ...\r\n", pmap->port, client_address.get_ip_address(), client_address.get_port()); |
morgandu | 0:11bc39d0f367 | 223 | |
morgandu | 0:11bc39d0f367 | 224 | exchange_data(pmap, &client_socket); |
morgandu | 0:11bc39d0f367 | 225 | } |
morgandu | 0:11bc39d0f367 | 226 | } |
morgandu | 0:11bc39d0f367 | 227 | |
morgandu | 0:11bc39d0f367 | 228 | int main() |
morgandu | 0:11bc39d0f367 | 229 | { |
morgandu | 0:11bc39d0f367 | 230 | /* Set the console baud-rate */ |
morgandu | 0:11bc39d0f367 | 231 | output.baud(115200); |
morgandu | 0:11bc39d0f367 | 232 | printf("\r\nmbed OS version is %d.\r\n", MBED_VERSION); |
morgandu | 0:11bc39d0f367 | 233 | printf("Start Serial-to-Ethernet...\r\n"); |
morgandu | 0:11bc39d0f367 | 234 | |
morgandu | 0:11bc39d0f367 | 235 | #ifdef ENABLE_WEB_CONFIG |
morgandu | 0:11bc39d0f367 | 236 | |
morgandu | 0:11bc39d0f367 | 237 | /* Restore configuration from SD card */ |
morgandu | 0:11bc39d0f367 | 238 | |
morgandu | 0:11bc39d0f367 | 239 | SD_Card_Mounted = (fs.mount(&bd) >= 0); |
morgandu | 0:11bc39d0f367 | 240 | if (SD_Card_Mounted) |
morgandu | 0:11bc39d0f367 | 241 | { |
morgandu | 0:11bc39d0f367 | 242 | FILE *fd = fopen(SER_CONFIG_FILE, "r"); |
morgandu | 0:11bc39d0f367 | 243 | if (fd != NULL) |
morgandu | 0:11bc39d0f367 | 244 | { |
morgandu | 0:11bc39d0f367 | 245 | char pBuf[sizeof(port_config)+2]; |
morgandu | 0:11bc39d0f367 | 246 | int len = fread(pBuf, 1, sizeof(port_config)+2, fd); |
morgandu | 0:11bc39d0f367 | 247 | if (len == (sizeof(port_config)+2) && pBuf[0] == 'N' && pBuf[1] == 'T') |
morgandu | 0:11bc39d0f367 | 248 | { |
morgandu | 0:11bc39d0f367 | 249 | printf("Set Serial ports from config file in SD card.\r\n"); |
morgandu | 0:11bc39d0f367 | 250 | memcpy(port_config, pBuf+2, sizeof(port_config)); |
morgandu | 0:11bc39d0f367 | 251 | } |
morgandu | 0:11bc39d0f367 | 252 | else |
morgandu | 0:11bc39d0f367 | 253 | printf("Incorrect serial config file.\r\n"); |
morgandu | 0:11bc39d0f367 | 254 | |
morgandu | 0:11bc39d0f367 | 255 | fclose(fd); |
morgandu | 0:11bc39d0f367 | 256 | } |
morgandu | 0:11bc39d0f367 | 257 | else |
morgandu | 0:11bc39d0f367 | 258 | printf("Can't open serial config file.\r\n"); |
morgandu | 0:11bc39d0f367 | 259 | |
morgandu | 0:11bc39d0f367 | 260 | fd = fopen(NET_CONFIG_FILE, "r"); |
morgandu | 0:11bc39d0f367 | 261 | if (fd != NULL) |
morgandu | 0:11bc39d0f367 | 262 | { |
morgandu | 0:11bc39d0f367 | 263 | char pBuf[sizeof(net_config)+2]; |
morgandu | 0:11bc39d0f367 | 264 | int len = fread(pBuf, 1, sizeof(net_config)+2, fd); |
morgandu | 0:11bc39d0f367 | 265 | if (len == (sizeof(net_config)+2) && pBuf[0] == 'N' && pBuf[1] == 'T') |
morgandu | 0:11bc39d0f367 | 266 | { |
morgandu | 0:11bc39d0f367 | 267 | printf("Set network from config file in SD card.\r\n"); |
morgandu | 0:11bc39d0f367 | 268 | memcpy(&net_config, pBuf+2, sizeof(net_config)); |
morgandu | 0:11bc39d0f367 | 269 | } |
morgandu | 0:11bc39d0f367 | 270 | else |
morgandu | 0:11bc39d0f367 | 271 | printf("Incorrect network config file.\r\n"); |
morgandu | 0:11bc39d0f367 | 272 | |
morgandu | 0:11bc39d0f367 | 273 | fclose(fd); |
morgandu | 0:11bc39d0f367 | 274 | } |
morgandu | 0:11bc39d0f367 | 275 | else |
morgandu | 0:11bc39d0f367 | 276 | printf("Can't open network config file.\r\n"); |
morgandu | 0:11bc39d0f367 | 277 | } |
morgandu | 0:11bc39d0f367 | 278 | else |
morgandu | 0:11bc39d0f367 | 279 | { |
morgandu | 0:11bc39d0f367 | 280 | printf("Can't find SD card.\r\n"); |
morgandu | 0:11bc39d0f367 | 281 | } |
morgandu | 0:11bc39d0f367 | 282 | |
morgandu | 0:11bc39d0f367 | 283 | #endif |
morgandu | 0:11bc39d0f367 | 284 | |
morgandu | 0:11bc39d0f367 | 285 | printf("Configure UART ports...\r\n"); |
morgandu | 0:11bc39d0f367 | 286 | for(int i=0; i<MAX_UART_PORTS; i++) |
morgandu | 0:11bc39d0f367 | 287 | { |
morgandu | 0:11bc39d0f367 | 288 | port_config[i].pserial->baud(port_config[i].baud); |
morgandu | 0:11bc39d0f367 | 289 | port_config[i].pserial->format(port_config[i].data, port_config[i].parity, port_config[i].stop); |
morgandu | 0:11bc39d0f367 | 290 | } |
morgandu | 0:11bc39d0f367 | 291 | |
morgandu | 0:11bc39d0f367 | 292 | if (net_config.mode == IP_STATIC_MODE) |
morgandu | 0:11bc39d0f367 | 293 | { |
morgandu | 0:11bc39d0f367 | 294 | printf("Start Ethernet in Static mode.\r\n"); |
morgandu | 0:11bc39d0f367 | 295 | eth.disconnect(); |
morgandu | 0:11bc39d0f367 | 296 | ((NetworkInterface *)ð)->set_network(net_config.ip, net_config.mask, net_config.gateway); |
morgandu | 0:11bc39d0f367 | 297 | } |
morgandu | 0:11bc39d0f367 | 298 | else |
morgandu | 0:11bc39d0f367 | 299 | printf("Start Ethernet in DHCP mode.\r\n"); |
morgandu | 0:11bc39d0f367 | 300 | |
morgandu | 0:11bc39d0f367 | 301 | eth.connect(); |
morgandu | 0:11bc39d0f367 | 302 | printf("IP Address is %s\r\n", eth.get_ip_address()); |
morgandu | 0:11bc39d0f367 | 303 | |
morgandu | 0:11bc39d0f367 | 304 | Thread thread[MAX_UART_PORTS]; |
morgandu | 0:11bc39d0f367 | 305 | |
morgandu | 0:11bc39d0f367 | 306 | // Folk thread for each port |
morgandu | 0:11bc39d0f367 | 307 | for(int i=0; i<MAX_UART_PORTS; i++) |
morgandu | 0:11bc39d0f367 | 308 | { |
morgandu | 0:11bc39d0f367 | 309 | if (port_config[i].mode == NET_SERVER_MODE) |
morgandu | 0:11bc39d0f367 | 310 | { |
morgandu | 0:11bc39d0f367 | 311 | thread[i].start(callback(bridge_net_server, &(port_config[i]))); |
morgandu | 0:11bc39d0f367 | 312 | } |
morgandu | 0:11bc39d0f367 | 313 | else // if (port_config[i].mode == TCP_CLIENT_MODE) |
morgandu | 0:11bc39d0f367 | 314 | { |
morgandu | 0:11bc39d0f367 | 315 | thread[i].start(callback(bridge_net_client, &(port_config[i]))); |
morgandu | 0:11bc39d0f367 | 316 | } |
morgandu | 0:11bc39d0f367 | 317 | } |
morgandu | 0:11bc39d0f367 | 318 | |
morgandu | 0:11bc39d0f367 | 319 | #ifdef ENABLE_WEB_CONFIG |
morgandu | 0:11bc39d0f367 | 320 | |
morgandu | 0:11bc39d0f367 | 321 | /*** main thread to be a web server for configuration ***/ |
morgandu | 0:11bc39d0f367 | 322 | start_httpd(); |
morgandu | 0:11bc39d0f367 | 323 | |
morgandu | 0:11bc39d0f367 | 324 | #endif |
morgandu | 0:11bc39d0f367 | 325 | |
morgandu | 0:11bc39d0f367 | 326 | while(1); |
morgandu | 0:11bc39d0f367 | 327 | |
morgandu | 0:11bc39d0f367 | 328 | /* end of main task */ |
morgandu | 0:11bc39d0f367 | 329 | //eth.disconnect(); |
morgandu | 0:11bc39d0f367 | 330 | } |