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 * Header for Serial-To-Ethernet configuration.
morgandu 0:11bc39d0f367 5 *
morgandu 0:11bc39d0f367 6 */
morgandu 0:11bc39d0f367 7
morgandu 0:11bc39d0f367 8 #ifndef _STE_CONFIG_H
morgandu 0:11bc39d0f367 9 #define _STE_CONFIG_H
morgandu 0:11bc39d0f367 10
morgandu 0:11bc39d0f367 11 #include "mbed.h"
morgandu 0:11bc39d0f367 12 #include "EthernetInterface.h"
morgandu 0:11bc39d0f367 13 #include "FATFileSystem.h"
morgandu 0:11bc39d0f367 14 #include "NuSDBlockDevice.h"
morgandu 0:11bc39d0f367 15
morgandu 8:e9248126f512 16 #if MBED_MAJOR_VERSION <= 5
morgandu 8:e9248126f512 17 #include "BufferedSerial.h"
morgandu 8:e9248126f512 18 #endif
morgandu 8:e9248126f512 19
morgandu 7:cbb5a2a2a0d2 20 /* A simple web server for network and UART configuration
morgandu 7:cbb5a2a2a0d2 21 0: Disable the web server.
morgandu 7:cbb5a2a2a0d2 22 1: Enable the web server
morgandu 7:cbb5a2a2a0d2 23 */
morgandu 7:cbb5a2a2a0d2 24 #define ENABLE_WEB_CONFIG 1
morgandu 0:11bc39d0f367 25
morgandu 2:35f2cc8b05fa 26 /* Maximum UART ports supported.
morgandu 7:cbb5a2a2a0d2 27 Must define enough tuples of mapping table "port_config[]" in main.c
morgandu 7:cbb5a2a2a0d2 28 */
morgandu 0:11bc39d0f367 29 #define MAX_UART_PORTS 2
morgandu 0:11bc39d0f367 30
morgandu 0:11bc39d0f367 31 /* Default UART baud */
morgandu 0:11bc39d0f367 32 #define DEFAULT_UART_BAUD 115200
morgandu 0:11bc39d0f367 33
morgandu 0:11bc39d0f367 34 /* Network base port number to listen.
morgandu 2:35f2cc8b05fa 35 The base port maps to the 1st UART port,
morgandu 7:cbb5a2a2a0d2 36 the (base port + 1) maps to the 2nd UART port, etc.
morgandu 7:cbb5a2a2a0d2 37 */
morgandu 0:11bc39d0f367 38 #define NET_PORT_BASE 10001
morgandu 0:11bc39d0f367 39
morgandu 2:35f2cc8b05fa 40 /* Files in SD card to store settings via web configuration */
morgandu 0:11bc39d0f367 41 #define SER_CONFIG_FILE "/fs/STE_SER.TXT" // for serial ports
morgandu 0:11bc39d0f367 42 #define NET_CONFIG_FILE "/fs/STE_NET.TXT" // for network
morgandu 0:11bc39d0f367 43
morgandu 7:cbb5a2a2a0d2 44 /* Maximum size of server domain name or address for web configuration */
morgandu 0:11bc39d0f367 45 #define MAX_SERVER_ADDRESS_SIZE 63
morgandu 0:11bc39d0f367 46
morgandu 2:35f2cc8b05fa 47 /* Maximum size of IP address for web configuration */
morgandu 0:11bc39d0f367 48 #define MAX_IPV4_ADDRESS_SIZE 15
morgandu 0:11bc39d0f367 49
morgandu 2:35f2cc8b05fa 50
morgandu 0:11bc39d0f367 51 /* Functions and global variables declaration. */
morgandu 0:11bc39d0f367 52
morgandu 0:11bc39d0f367 53 typedef enum {
morgandu 0:11bc39d0f367 54 NET_SERVER_MODE = 0,
morgandu 0:11bc39d0f367 55 NET_CLIENT_MODE
morgandu 0:11bc39d0f367 56 } E_NetMode;
morgandu 0:11bc39d0f367 57
morgandu 0:11bc39d0f367 58 typedef enum {
morgandu 0:11bc39d0f367 59 IP_STATIC_MODE = 0,
morgandu 0:11bc39d0f367 60 IP_DHCP_MODE
morgandu 0:11bc39d0f367 61 } E_IPMode;
morgandu 0:11bc39d0f367 62
morgandu 0:11bc39d0f367 63 typedef struct {
morgandu 0:11bc39d0f367 64 E_IPMode mode;
morgandu 0:11bc39d0f367 65 char ip[MAX_IPV4_ADDRESS_SIZE+1];
morgandu 0:11bc39d0f367 66 char mask[MAX_IPV4_ADDRESS_SIZE+1];
morgandu 0:11bc39d0f367 67 char gateway[MAX_IPV4_ADDRESS_SIZE+1];
morgandu 0:11bc39d0f367 68 } S_NET_CONFIG;
morgandu 0:11bc39d0f367 69
morgandu 0:11bc39d0f367 70 typedef struct {
morgandu 0:11bc39d0f367 71 E_NetMode mode; // Network server or client mode
morgandu 7:cbb5a2a2a0d2 72 int port; // Network port number (Server mode)
morgandu 0:11bc39d0f367 73 BufferedSerial *pserial; // UART number
morgandu 0:11bc39d0f367 74 int baud; // UART baud
morgandu 0:11bc39d0f367 75 int data; // UART data bits
morgandu 0:11bc39d0f367 76 int stop; // UART stop bits
morgandu 0:11bc39d0f367 77 mbed::SerialBase::Parity parity; // UART parity bit
morgandu 0:11bc39d0f367 78 char server_addr[MAX_SERVER_ADDRESS_SIZE+1]; // Server address for TCP client mode
morgandu 0:11bc39d0f367 79 unsigned short server_port; // Server port for TCP client mode
morgandu 0:11bc39d0f367 80 } S_PORT_CONFIG;
morgandu 0:11bc39d0f367 81
morgandu 8:e9248126f512 82 //extern RawSerial output; // for debug output
morgandu 0:11bc39d0f367 83 extern EthernetInterface eth;
morgandu 0:11bc39d0f367 84 extern S_PORT_CONFIG port_config[MAX_UART_PORTS];
morgandu 0:11bc39d0f367 85 extern S_NET_CONFIG net_config;
morgandu 0:11bc39d0f367 86
morgandu 0:11bc39d0f367 87 extern bool SD_Card_Mounted;
morgandu 0:11bc39d0f367 88 void start_httpd(void);
morgandu 0:11bc39d0f367 89
morgandu 0:11bc39d0f367 90 #endif