Chau Vo / device_configuration

Dependents:   F103-Web-Server

Fork of my_eeprom_funcs by Chau Vo

Committer:
olympux
Date:
Thu Sep 01 12:22:10 2016 +0000
Revision:
21:23ae23754f0b
Parent:
20:858384cac44a
Child:
22:0e1d8a9e8f54
Configure UART parameters

Who changed what in which revision?

UserRevisionLine numberNew contents of line
olympux 1:6bdc99dd8e0a 1 #include "mbed.h"
olympux 16:700377cd9d29 2 #include "device_configuration.h"
olympux 1:6bdc99dd8e0a 3
olympux 1:6bdc99dd8e0a 4
olympux 11:250b05a5266d 5 /*
olympux 10:f37acffddef9 6 * Debug option
olympux 10:f37acffddef9 7 */
olympux 16:700377cd9d29 8 #if 0
olympux 4:944a4646b825 9 //Enable debug
olympux 4:944a4646b825 10 #include <cstdio>
olympux 16:700377cd9d29 11 #define INFO(x, ...) std::printf(""x"\r\n", ##__VA_ARGS__);
olympux 16:700377cd9d29 12 #define DBG(x, ...) std::printf("[dev_conf: DBG]"x"\r\n", ##__VA_ARGS__);
olympux 16:700377cd9d29 13 #define WARN(x, ...) std::printf("[dev_conf: WARN]"x"\r\n", ##__VA_ARGS__);
olympux 16:700377cd9d29 14 #define ERR(x, ...) std::printf("[dev_conf: ERR]"x"\r\n", ##__VA_ARGS__);
olympux 4:944a4646b825 15 #else
olympux 4:944a4646b825 16 //Disable debug
olympux 16:700377cd9d29 17 #define INFO(x, ...)
olympux 4:944a4646b825 18 #define DBG(x, ...)
olympux 4:944a4646b825 19 #define WARN(x, ...)
olympux 4:944a4646b825 20 #define ERR(x, ...)
olympux 16:700377cd9d29 21 #endif
olympux 4:944a4646b825 22
olympux 16:700377cd9d29 23
olympux 16:700377cd9d29 24 /*
olympux 16:700377cd9d29 25 * Variables for network configuration
olympux 16:700377cd9d29 26 */
olympux 16:700377cd9d29 27 // Device configuration
olympux 16:700377cd9d29 28 uint16_t u16IpAddr[4], u16IpSubnet[4], u16IpGateway[4], u16MacAddr[3]; // 16-bits variables to be compatible with eeprom functions
olympux 16:700377cd9d29 29 char strIpAddr[16], strIpSubnet[16], strIpGateway[16], strMacAddr[20]; // RPC variables, converted from 16-bits u16ip_xxx
olympux 16:700377cd9d29 30 uint16_t u16DeviceConfiguredFlag = 0; // flag indicates whether device has been configured (0xA5A5) or not
olympux 16:700377cd9d29 31 // TCP server/UDP
olympux 16:700377cd9d29 32 uint16_t u16LocalTcpServerPort = DEFAULT_LOCAL_TCP_SERVER_PORT;
olympux 18:d88314ae7979 33 uint16_t u16LocalUdpServerPort = DEFAULT_LOCAL_UDP_SERVER_PORT;
olympux 16:700377cd9d29 34 // TCP client mode, set parameters of the remote TCP server this device connects to.
olympux 16:700377cd9d29 35 // When enabled, this device will send its status to the server every transmit_time_period.
olympux 16:700377cd9d29 36 uint16_t u16RemoteTcpServerIpAddr[4]; // 16-bit variable to be compatible with eeprom functions
olympux 16:700377cd9d29 37 char strRemoteTcpServerIpAddr[16]; // RPC variable, converted from 16-bits u16server_ip_addr
olympux 16:700377cd9d29 38 uint16_t u16RemoteTcpServerPort; // 16-bit variable to be compatible with eeprom functions
olympux 16:700377cd9d29 39 uint16_t u16AutoTransmitFlag = 0, u16TransmitPeriod = DEFAULT_TRANSMIT_PERIOD; // auto transmit status, time period = 1s
olympux 18:d88314ae7979 40 // UDP client mode
olympux 18:d88314ae7979 41 uint16_t u16RemoteUdpServerIpAddr[4]; // 16-bit variable to be compatible with eeprom functions
olympux 18:d88314ae7979 42 char strRemoteUdpServerIpAddr[16]; // RPC variable, converted from 16-bits u16server_ip_addr
olympux 18:d88314ae7979 43 uint16_t u16RemoteUdpServerPort; // 16-bit variable to be compatible with eeprom functions
olympux 16:700377cd9d29 44 // enable modes
olympux 19:5671f3c25342 45 uint16_t u16EnableTcpServer = DEFAULT_ENABLE_FLAG_VALUE, u16EnableTcpClient = DEFAULT_DISABLE_FLAG_VALUE; // flags for enabling TCP server/client and UDP (UDP is always on for configuration)
olympux 19:5671f3c25342 46 uint16_t u16EnableUdpServer = DEFAULT_DISABLE_FLAG_VALUE, u16EnableUdpClient = DEFAULT_DISABLE_FLAG_VALUE;
olympux 20:858384cac44a 47 // time period between UART data package before sending through Ethernet interface
olympux 20:858384cac44a 48 uint16_t u16InterDataPeriod;
olympux 20:858384cac44a 49 // special character to signal sending data
olympux 20:858384cac44a 50 uint16_t u16SpecialChar;
olympux 21:23ae23754f0b 51 // uart
olympux 21:23ae23754f0b 52 uint16_t u16UartBaudrate, u16UartBits, u16UartStopBits, u16UartParity, u16UartHandshake;
olympux 20:858384cac44a 53
olympux 20:858384cac44a 54 // extra, not actually in EEPROM
olympux 16:700377cd9d29 55 uint8_t u8IpAddr[4]; // keep device ip address in 8-bits
olympux 16:700377cd9d29 56 uint8_t u8MacAddr[6]; // keep mac in 8-bits
olympux 16:700377cd9d29 57 uint8_t u8RemoteTcpServerIpAddr[4]; // remote TCP server ip address in 8-bits
olympux 18:d88314ae7979 58 uint8_t u8RemoteUdpServerIpAddr[4]; // remote UDP server ip address in 8-bits
olympux 4:944a4646b825 59
olympux 19:5671f3c25342 60
olympux 19:5671f3c25342 61 // erase page
olympux 19:5671f3c25342 62 void erase_device_configuration() {
olympux 19:5671f3c25342 63 // erase first_run flag
olympux 19:5671f3c25342 64 enableEEPROMWriting();
olympux 19:5671f3c25342 65 disableEEPROMWriting();
olympux 19:5671f3c25342 66 }
olympux 19:5671f3c25342 67
olympux 19:5671f3c25342 68
olympux 12:613ab276bf37 69 /*!
olympux 12:613ab276bf37 70 * Function to write module network configuration
olympux 12:613ab276bf37 71 * @param <char *buf> configuration buffer
olympux 16:700377cd9d29 72 * @note 4x16-bit IP address
olympux 16:700377cd9d29 73 * @note 4x16-bit subnet
olympux 16:700377cd9d29 74 * @note 4x16-bit gateway
olympux 16:700377cd9d29 75 * @note 3x16-bit MAC
olympux 16:700377cd9d29 76 * @note 16-bit TCP local port
olympux 16:700377cd9d29 77 * @note 16-bit UDP local port
olympux 12:613ab276bf37 78 */
olympux 18:d88314ae7979 79 void write_device_configuration(uint16_t* ip, uint16_t* subnet, uint16_t* gateway, uint16_t* mac,
olympux 18:d88314ae7979 80 uint16_t tcp_port, uint16_t udp_port,
olympux 18:d88314ae7979 81 uint16_t* remote_tcp_ip, uint16_t remote_tcp_port, uint16_t auto_transmit, uint16_t transmit_period,
olympux 18:d88314ae7979 82 uint16_t* remote_udp_ip, uint16_t remote_udp_port,
olympux 20:858384cac44a 83 uint16_t enable_tcp_server, uint16_t enable_tcp_client, uint16_t enable_udp_server, uint16_t enable_udp_client,
olympux 21:23ae23754f0b 84 uint16_t inter_data_period, uint16_t special_char,
olympux 21:23ae23754f0b 85 uint16_t uart_baudrate, uint16_t uart_bits, uint16_t uart_stopbits, uint16_t uart_parity, uint16_t uart_handshake) {
olympux 1:6bdc99dd8e0a 86 // Write network configuration
olympux 1:6bdc99dd8e0a 87 // 4-byte IP address + 4-byte subnet + 4-byte gateway + 3-byte MAC
olympux 16:700377cd9d29 88 // + local TCP server port + local UDP port
olympux 1:6bdc99dd8e0a 89
olympux 16:700377cd9d29 90 INFO("Saving network configuration for this device...");
olympux 16:700377cd9d29 91
olympux 16:700377cd9d29 92 enableEEPROMWriting();
olympux 6:241d1539914a 93
olympux 6:241d1539914a 94 // erase first_run flag
olympux 16:700377cd9d29 95 writeEEPROMHalfWord(DEVICE_CONFIGURED_FLAG_POS, DEFAULT_ENABLE_FLAG_VALUE);
olympux 6:241d1539914a 96
olympux 1:6bdc99dd8e0a 97 // IP address
olympux 16:700377cd9d29 98 writeEEPROMHalfWord(IP_ADDRESS_POS+2*0, ip[0]);
olympux 16:700377cd9d29 99 writeEEPROMHalfWord(IP_ADDRESS_POS+2*1, ip[1]);
olympux 16:700377cd9d29 100 writeEEPROMHalfWord(IP_ADDRESS_POS+2*2, ip[2]);
olympux 16:700377cd9d29 101 writeEEPROMHalfWord(IP_ADDRESS_POS+2*3, ip[3]);
olympux 1:6bdc99dd8e0a 102
olympux 1:6bdc99dd8e0a 103 // IP subnet
olympux 16:700377cd9d29 104 writeEEPROMHalfWord(IP_SUBNET_POS+2*0, subnet[0]);
olympux 16:700377cd9d29 105 writeEEPROMHalfWord(IP_SUBNET_POS+2*1, subnet[1]);
olympux 16:700377cd9d29 106 writeEEPROMHalfWord(IP_SUBNET_POS+2*2, subnet[2]);
olympux 16:700377cd9d29 107 writeEEPROMHalfWord(IP_SUBNET_POS+2*3, subnet[3]);
olympux 1:6bdc99dd8e0a 108
olympux 1:6bdc99dd8e0a 109 // IP gateway
olympux 16:700377cd9d29 110 writeEEPROMHalfWord(IP_GATEWAY_POS+2*0, gateway[0]);
olympux 16:700377cd9d29 111 writeEEPROMHalfWord(IP_GATEWAY_POS+2*1, gateway[1]);
olympux 16:700377cd9d29 112 writeEEPROMHalfWord(IP_GATEWAY_POS+2*2, gateway[2]);
olympux 16:700377cd9d29 113 writeEEPROMHalfWord(IP_GATEWAY_POS+2*3, gateway[3]);
olympux 1:6bdc99dd8e0a 114
olympux 1:6bdc99dd8e0a 115 // MAC address
olympux 16:700377cd9d29 116 writeEEPROMHalfWord(MAC_ADDRESS_POS+2*0, mac[0]);
olympux 16:700377cd9d29 117 writeEEPROMHalfWord(MAC_ADDRESS_POS+2*1, mac[1]);
olympux 16:700377cd9d29 118 writeEEPROMHalfWord(MAC_ADDRESS_POS+2*2, mac[2]);
olympux 16:700377cd9d29 119
olympux 16:700377cd9d29 120 // Ports
olympux 16:700377cd9d29 121 writeEEPROMHalfWord(LOCAL_TCP_SERVER_PORT_POS, tcp_port);
olympux 18:d88314ae7979 122 writeEEPROMHalfWord(LOCAL_UDP_SERVER_PORT_POS, udp_port);
olympux 16:700377cd9d29 123
olympux 16:700377cd9d29 124 // Remote TCP server
olympux 18:d88314ae7979 125 writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*0, remote_tcp_ip[0]);
olympux 18:d88314ae7979 126 writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*1, remote_tcp_ip[1]);
olympux 18:d88314ae7979 127 writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*2, remote_tcp_ip[2]);
olympux 18:d88314ae7979 128 writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*3, remote_tcp_ip[3]);
olympux 1:6bdc99dd8e0a 129
olympux 18:d88314ae7979 130 // Remote TCP server port
olympux 18:d88314ae7979 131 writeEEPROMHalfWord(REMOTE_TCP_SERVER_PORT_POS, remote_tcp_port);
olympux 16:700377cd9d29 132
olympux 16:700377cd9d29 133 // Auto transmit
olympux 16:700377cd9d29 134 writeEEPROMHalfWord(AUTO_TRANSMIT_FLAG_POS, auto_transmit);
olympux 16:700377cd9d29 135 writeEEPROMHalfWord(AUTO_TRANSMIT_TIME_PERIOD_POS, transmit_period);
olympux 16:700377cd9d29 136
olympux 18:d88314ae7979 137 // Remote UDP server
olympux 18:d88314ae7979 138 writeEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*0, remote_udp_ip[0]);
olympux 18:d88314ae7979 139 writeEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*1, remote_udp_ip[1]);
olympux 18:d88314ae7979 140 writeEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*2, remote_udp_ip[2]);
olympux 18:d88314ae7979 141 writeEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*3, remote_udp_ip[3]);
olympux 18:d88314ae7979 142
olympux 18:d88314ae7979 143 // Remote UDP server port
olympux 18:d88314ae7979 144 writeEEPROMHalfWord(REMOTE_UDP_SERVER_PORT_POS, remote_udp_port);
olympux 18:d88314ae7979 145
olympux 16:700377cd9d29 146 // Enable modes
olympux 16:700377cd9d29 147 writeEEPROMHalfWord(ENABLE_TCP_SERVER_POS, enable_tcp_server);
olympux 16:700377cd9d29 148 writeEEPROMHalfWord(ENABLE_TCP_CLIENT_POS, enable_tcp_client);
olympux 18:d88314ae7979 149 writeEEPROMHalfWord(ENABLE_UDP_SERVER_POS, enable_udp_server);
olympux 18:d88314ae7979 150 writeEEPROMHalfWord(ENABLE_UDP_CLIENT_POS, enable_udp_client);
olympux 16:700377cd9d29 151
olympux 20:858384cac44a 152 // Serial inter-data period
olympux 20:858384cac44a 153 writeEEPROMHalfWord(INTER_DATA_PERIOD_POS, inter_data_period);
olympux 20:858384cac44a 154
olympux 20:858384cac44a 155 // Special char
olympux 20:858384cac44a 156 writeEEPROMHalfWord(SPECIAL_CHAR_POS, special_char);
olympux 20:858384cac44a 157
olympux 21:23ae23754f0b 158 // UART
olympux 21:23ae23754f0b 159 writeEEPROMHalfWord(UART_BAUDRATE_POS, uart_baudrate);
olympux 21:23ae23754f0b 160 writeEEPROMHalfWord(UART_BITS_POS, uart_bits);
olympux 21:23ae23754f0b 161 writeEEPROMHalfWord(UART_STOPBITS_POS, uart_stopbits);
olympux 21:23ae23754f0b 162 writeEEPROMHalfWord(UART_PARITY_POS, uart_parity);
olympux 21:23ae23754f0b 163 writeEEPROMHalfWord(UART_HANDSHAKE_POS, uart_handshake);
olympux 21:23ae23754f0b 164
olympux 16:700377cd9d29 165 disableEEPROMWriting();
olympux 16:700377cd9d29 166
olympux 16:700377cd9d29 167 INFO("Successful");
olympux 1:6bdc99dd8e0a 168 }
olympux 1:6bdc99dd8e0a 169
olympux 10:f37acffddef9 170 /*!
olympux 10:f37acffddef9 171 * Function to load module network configuration
olympux 10:f37acffddef9 172 */
olympux 16:700377cd9d29 173 void read_device_configuration(void) {
olympux 16:700377cd9d29 174 mbed_mac_address((char *)u8MacAddr);
olympux 1:6bdc99dd8e0a 175
olympux 16:700377cd9d29 176 INFO("Loading network configuration...");
olympux 1:6bdc99dd8e0a 177
olympux 16:700377cd9d29 178 // check if configured
olympux 16:700377cd9d29 179 u16DeviceConfiguredFlag = readEEPROMHalfWord(DEVICE_CONFIGURED_FLAG_POS);
olympux 1:6bdc99dd8e0a 180
olympux 1:6bdc99dd8e0a 181 // if not first run, load network config
olympux 16:700377cd9d29 182 if (u16DeviceConfiguredFlag == DEFAULT_ENABLE_FLAG_VALUE) {
olympux 16:700377cd9d29 183 INFO("User settings");
olympux 1:6bdc99dd8e0a 184
olympux 1:6bdc99dd8e0a 185 // IP address
olympux 16:700377cd9d29 186 u16IpAddr[0] = readEEPROMHalfWord(IP_ADDRESS_POS+2*0);
olympux 16:700377cd9d29 187 u16IpAddr[1] = readEEPROMHalfWord(IP_ADDRESS_POS+2*1);
olympux 16:700377cd9d29 188 u16IpAddr[2] = readEEPROMHalfWord(IP_ADDRESS_POS+2*2);
olympux 16:700377cd9d29 189 u16IpAddr[3] = readEEPROMHalfWord(IP_ADDRESS_POS+2*3);
olympux 16:700377cd9d29 190 u8IpAddr[0] = (uint8_t)(u16IpAddr[0] & 0x00FF);
olympux 16:700377cd9d29 191 u8IpAddr[1] = (uint8_t)(u16IpAddr[1] & 0x00FF);
olympux 16:700377cd9d29 192 u8IpAddr[2] = (uint8_t)(u16IpAddr[2] & 0x00FF);
olympux 16:700377cd9d29 193 u8IpAddr[3] = (uint8_t)(u16IpAddr[3] & 0x00FF);
olympux 1:6bdc99dd8e0a 194
olympux 1:6bdc99dd8e0a 195 // IP subnet
olympux 16:700377cd9d29 196 u16IpSubnet[0] = readEEPROMHalfWord(IP_SUBNET_POS+2*0);
olympux 16:700377cd9d29 197 u16IpSubnet[1] = readEEPROMHalfWord(IP_SUBNET_POS+2*1);
olympux 16:700377cd9d29 198 u16IpSubnet[2] = readEEPROMHalfWord(IP_SUBNET_POS+2*2);
olympux 16:700377cd9d29 199 u16IpSubnet[3] = readEEPROMHalfWord(IP_SUBNET_POS+2*3);
olympux 1:6bdc99dd8e0a 200
olympux 1:6bdc99dd8e0a 201 // IP gateway
olympux 16:700377cd9d29 202 u16IpGateway[0] = readEEPROMHalfWord(IP_GATEWAY_POS+2*0);
olympux 16:700377cd9d29 203 u16IpGateway[1] = readEEPROMHalfWord(IP_GATEWAY_POS+2*1);
olympux 16:700377cd9d29 204 u16IpGateway[2] = readEEPROMHalfWord(IP_GATEWAY_POS+2*2);
olympux 16:700377cd9d29 205 u16IpGateway[3] = readEEPROMHalfWord(IP_GATEWAY_POS+2*3);
olympux 1:6bdc99dd8e0a 206
olympux 1:6bdc99dd8e0a 207 // MAC address
olympux 16:700377cd9d29 208 u16MacAddr[0] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*0);
olympux 16:700377cd9d29 209 u16MacAddr[1] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*1);
olympux 16:700377cd9d29 210 u16MacAddr[2] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*2);
olympux 16:700377cd9d29 211 u8MacAddr[0] = DEFAULT_MAC0; u8MacAddr[1] = DEFAULT_MAC1; u8MacAddr[2] = DEFAULT_MAC2;
olympux 16:700377cd9d29 212 u8MacAddr[3] = (uint8_t)(u16MacAddr[0] & 0x00FF);
olympux 16:700377cd9d29 213 u8MacAddr[4] = (uint8_t)(u16MacAddr[1] & 0x00FF);
olympux 16:700377cd9d29 214 u8MacAddr[5] = (uint8_t)(u16MacAddr[2] & 0x00FF);
olympux 16:700377cd9d29 215
olympux 16:700377cd9d29 216 // Ports
olympux 16:700377cd9d29 217 u16LocalTcpServerPort = readEEPROMHalfWord(LOCAL_TCP_SERVER_PORT_POS);
olympux 18:d88314ae7979 218 u16LocalUdpServerPort = readEEPROMHalfWord(LOCAL_UDP_SERVER_PORT_POS);
olympux 1:6bdc99dd8e0a 219
olympux 16:700377cd9d29 220 // Remote TCP server
olympux 16:700377cd9d29 221 u16RemoteTcpServerIpAddr[0] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*0);
olympux 16:700377cd9d29 222 u16RemoteTcpServerIpAddr[1] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*1);
olympux 16:700377cd9d29 223 u16RemoteTcpServerIpAddr[2] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*2);
olympux 16:700377cd9d29 224 u16RemoteTcpServerIpAddr[3] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*3);
olympux 16:700377cd9d29 225 u8RemoteTcpServerIpAddr[0] = (uint8_t)(u16RemoteTcpServerIpAddr[0] & 0x00FF);
olympux 16:700377cd9d29 226 u8RemoteTcpServerIpAddr[1] = (uint8_t)(u16RemoteTcpServerIpAddr[1] & 0x00FF);
olympux 16:700377cd9d29 227 u8RemoteTcpServerIpAddr[2] = (uint8_t)(u16RemoteTcpServerIpAddr[2] & 0x00FF);
olympux 16:700377cd9d29 228 u8RemoteTcpServerIpAddr[3] = (uint8_t)(u16RemoteTcpServerIpAddr[3] & 0x00FF);
olympux 16:700377cd9d29 229
olympux 18:d88314ae7979 230 // Remote TCP server port
olympux 16:700377cd9d29 231 u16RemoteTcpServerPort = readEEPROMHalfWord(REMOTE_TCP_SERVER_PORT_POS);
olympux 16:700377cd9d29 232
olympux 16:700377cd9d29 233 // Auto transmit
olympux 16:700377cd9d29 234 u16AutoTransmitFlag = readEEPROMHalfWord(AUTO_TRANSMIT_FLAG_POS);
olympux 16:700377cd9d29 235 u16TransmitPeriod = readEEPROMHalfWord(AUTO_TRANSMIT_TIME_PERIOD_POS);
olympux 16:700377cd9d29 236
olympux 18:d88314ae7979 237 // Remote UDP server
olympux 18:d88314ae7979 238 u16RemoteUdpServerIpAddr[0] = readEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*0);
olympux 18:d88314ae7979 239 u16RemoteUdpServerIpAddr[1] = readEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*1);
olympux 18:d88314ae7979 240 u16RemoteUdpServerIpAddr[2] = readEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*2);
olympux 18:d88314ae7979 241 u16RemoteUdpServerIpAddr[3] = readEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*3);
olympux 18:d88314ae7979 242 u8RemoteUdpServerIpAddr[0] = (uint8_t)(u16RemoteUdpServerIpAddr[0] & 0x00FF);
olympux 18:d88314ae7979 243 u8RemoteUdpServerIpAddr[1] = (uint8_t)(u16RemoteUdpServerIpAddr[1] & 0x00FF);
olympux 18:d88314ae7979 244 u8RemoteUdpServerIpAddr[2] = (uint8_t)(u16RemoteUdpServerIpAddr[2] & 0x00FF);
olympux 18:d88314ae7979 245 u8RemoteUdpServerIpAddr[3] = (uint8_t)(u16RemoteUdpServerIpAddr[3] & 0x00FF);
olympux 18:d88314ae7979 246
olympux 19:5671f3c25342 247 // Remote UDP server port
olympux 18:d88314ae7979 248 u16RemoteUdpServerPort = readEEPROMHalfWord(REMOTE_UDP_SERVER_PORT_POS);
olympux 18:d88314ae7979 249
olympux 16:700377cd9d29 250 // Enable modes
olympux 16:700377cd9d29 251 u16EnableTcpServer = readEEPROMHalfWord(ENABLE_TCP_SERVER_POS);
olympux 16:700377cd9d29 252 u16EnableTcpClient = readEEPROMHalfWord(ENABLE_TCP_CLIENT_POS);
olympux 18:d88314ae7979 253 u16EnableUdpServer = readEEPROMHalfWord(ENABLE_UDP_SERVER_POS);
olympux 18:d88314ae7979 254 u16EnableUdpClient = readEEPROMHalfWord(ENABLE_UDP_CLIENT_POS);
olympux 16:700377cd9d29 255
olympux 20:858384cac44a 256 // Serial inter-data period
olympux 20:858384cac44a 257 u16InterDataPeriod = readEEPROMHalfWord(INTER_DATA_PERIOD_POS);
olympux 20:858384cac44a 258 // Special character
olympux 20:858384cac44a 259 u16SpecialChar = readEEPROMHalfWord(SPECIAL_CHAR_POS);
olympux 20:858384cac44a 260
olympux 21:23ae23754f0b 261 // UART
olympux 21:23ae23754f0b 262 u16UartBaudrate = readEEPROMHalfWord(UART_BAUDRATE_POS);
olympux 21:23ae23754f0b 263 u16UartBits = readEEPROMHalfWord(UART_BITS_POS);
olympux 21:23ae23754f0b 264 u16UartStopBits = readEEPROMHalfWord(UART_STOPBITS_POS);
olympux 21:23ae23754f0b 265 u16UartParity = readEEPROMHalfWord(UART_PARITY_POS);
olympux 21:23ae23754f0b 266 u16UartHandshake = readEEPROMHalfWord(UART_HANDSHAKE_POS);
olympux 21:23ae23754f0b 267
olympux 21:23ae23754f0b 268 // convert to strings
olympux 16:700377cd9d29 269 sprintf(strIpAddr, "%d.%d.%d.%d", u8IpAddr[0], u8IpAddr[1], u8IpAddr[2], u8IpAddr[3]);
olympux 16:700377cd9d29 270 sprintf(strIpSubnet, "%d.%d.%d.%d", (uint8_t)u16IpSubnet[0], (uint8_t)u16IpSubnet[1], (uint8_t)u16IpSubnet[2], (uint8_t)u16IpSubnet[3]);
olympux 16:700377cd9d29 271 sprintf(strIpGateway, "%d.%d.%d.%d", (uint8_t)u16IpGateway[0], (uint8_t)u16IpGateway[1], (uint8_t)u16IpGateway[2], (uint8_t)u16IpGateway[3]);
olympux 16:700377cd9d29 272 sprintf(strRemoteTcpServerIpAddr, "%d.%d.%d.%d", u8RemoteTcpServerIpAddr[0], u8RemoteTcpServerIpAddr[1], u8RemoteTcpServerIpAddr[2], u8RemoteTcpServerIpAddr[3]);
olympux 18:d88314ae7979 273 sprintf(strRemoteUdpServerIpAddr, "%d.%d.%d.%d", u8RemoteUdpServerIpAddr[0], u8RemoteUdpServerIpAddr[1], u8RemoteUdpServerIpAddr[2], u8RemoteUdpServerIpAddr[3]);
olympux 17:574f90b11417 274 sprintf(strMacAddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", u8MacAddr[0], u8MacAddr[1], u8MacAddr[2], u8MacAddr[3], u8MacAddr[4], u8MacAddr[5]);
olympux 1:6bdc99dd8e0a 275 }
olympux 8:4fc4b1b5509b 276 // if ip is not configured, use default addresses
olympux 1:6bdc99dd8e0a 277 else {
olympux 16:700377cd9d29 278 INFO("No user settings, load defaults");
olympux 16:700377cd9d29 279 u8MacAddr[0] = DEFAULT_MAC0; u8MacAddr[1] = DEFAULT_MAC1; u8MacAddr[2] = DEFAULT_MAC2;
olympux 16:700377cd9d29 280 u8MacAddr[3] = DEFAULT_MAC3; u8MacAddr[4] = DEFAULT_MAC4; u8MacAddr[5] = DEFAULT_MAC5;
olympux 16:700377cd9d29 281 sprintf(strIpAddr, DEFAULT_IP_ADDRESS);
olympux 16:700377cd9d29 282 sprintf(strIpSubnet, DEFAULT_IP_SUBNET);
olympux 16:700377cd9d29 283 sprintf(strIpGateway, DEFAULT_IP_GATEWAY);
olympux 19:5671f3c25342 284 sprintf(strMacAddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", u8MacAddr[0], u8MacAddr[1], u8MacAddr[2], u8MacAddr[3], u8MacAddr[4], u8MacAddr[5]);
olympux 19:5671f3c25342 285
olympux 19:5671f3c25342 286 u16LocalTcpServerPort = DEFAULT_LOCAL_TCP_SERVER_PORT;
olympux 19:5671f3c25342 287 u16LocalUdpServerPort = DEFAULT_LOCAL_UDP_SERVER_PORT;
olympux 19:5671f3c25342 288
olympux 16:700377cd9d29 289 sprintf(strRemoteTcpServerIpAddr, DEFAULT_REMOTE_TCP_SERVER_IP);
olympux 19:5671f3c25342 290 u16RemoteTcpServerPort = DEFAULT_REMOTE_TCP_SERVER_PORT;
olympux 19:5671f3c25342 291
olympux 18:d88314ae7979 292 sprintf(strRemoteUdpServerIpAddr, DEFAULT_REMOTE_UDP_SERVER_IP);
olympux 19:5671f3c25342 293 u16RemoteUdpServerPort = DEFAULT_REMOTE_UDP_SERVER_PORT;
olympux 19:5671f3c25342 294
olympux 19:5671f3c25342 295 u16AutoTransmitFlag = DEFAULT_DISABLE_FLAG_VALUE;
olympux 19:5671f3c25342 296 u16TransmitPeriod = DEFAULT_TRANSMIT_PERIOD;
olympux 19:5671f3c25342 297
olympux 19:5671f3c25342 298 u16EnableTcpServer = DEFAULT_ENABLE_FLAG_VALUE;
olympux 19:5671f3c25342 299 u16EnableTcpClient = DEFAULT_DISABLE_FLAG_VALUE;
olympux 19:5671f3c25342 300 u16EnableUdpServer = DEFAULT_DISABLE_FLAG_VALUE;
olympux 19:5671f3c25342 301 u16EnableUdpClient = DEFAULT_DISABLE_FLAG_VALUE;
olympux 19:5671f3c25342 302
olympux 20:858384cac44a 303 u16InterDataPeriod = DEFAULT_INTER_DATA_PERIOD;
olympux 20:858384cac44a 304 u16SpecialChar = DEFAULT_SPECIAL_CHARACTER;
olympux 20:858384cac44a 305
olympux 21:23ae23754f0b 306 u16UartBaudrate = DEFAULT_UART_BAUDRATE;
olympux 21:23ae23754f0b 307 u16UartBits = DEFAULT_UART_BITS;
olympux 21:23ae23754f0b 308 u16UartStopBits = DEFAULT_UART_STOPBITS;
olympux 21:23ae23754f0b 309 u16UartParity = DEFAULT_UART_PARITY;
olympux 21:23ae23754f0b 310 u16UartHandshake = DEFAULT_UART_HANDSHAKE;
olympux 1:6bdc99dd8e0a 311 }
olympux 1:6bdc99dd8e0a 312
olympux 16:700377cd9d29 313 INFO("Successful");
olympux 16:700377cd9d29 314 INFO("IP: %s", strIpAddr);
olympux 16:700377cd9d29 315 INFO("MASK: %s", strIpSubnet);
olympux 16:700377cd9d29 316 INFO("GW: %s", strIpGateway);
olympux 16:700377cd9d29 317 INFO("TCP server local port: %d", u16LocalTcpServerPort);
olympux 18:d88314ae7979 318 INFO("UDP server local port: %d", u16LocalUdpServerPort);
olympux 18:d88314ae7979 319
olympux 18:d88314ae7979 320 INFO("Remote TCP server: %s", strRemoteTcpServerIpAddr);
olympux 18:d88314ae7979 321 INFO("Remote TCP server port: %d", u16RemoteTcpServerPort);
olympux 18:d88314ae7979 322
olympux 18:d88314ae7979 323 INFO("Remote UDP server: %s", strRemoteUdpServerIpAddr);
olympux 18:d88314ae7979 324 INFO("Remote UDP server port: %d", u16RemoteUdpServerPort);
olympux 18:d88314ae7979 325
olympux 18:d88314ae7979 326 INFO("Auto transmit: %s", (u16AutoTransmitFlag == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
olympux 18:d88314ae7979 327 INFO("Period: %d", u16TransmitPeriod);
olympux 16:700377cd9d29 328
olympux 16:700377cd9d29 329 INFO("TCP server: %s", (u16EnableTcpServer == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
olympux 16:700377cd9d29 330 INFO("TCP client: %s", (u16EnableTcpClient == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
olympux 18:d88314ae7979 331 INFO("UDP server: %s", (u16EnableUdpServer == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
olympux 18:d88314ae7979 332 INFO("UDP client: %s", (u16EnableUdpClient == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
olympux 20:858384cac44a 333
olympux 20:858384cac44a 334 INFO("Serial inter-data period: %d", u16InterDataPeriod);
olympux 20:858384cac44a 335 INFO("Special character: %.2x", u16SpecialChar);
olympux 1:6bdc99dd8e0a 336 }
olympux 1:6bdc99dd8e0a 337
olympux 19:5671f3c25342 338 void reset_default_device_configuration() {
olympux 19:5671f3c25342 339 uint16_t ip[4] = {192,168,0,120};
olympux 19:5671f3c25342 340 uint16_t subnet[4] = {255,255,255,0};
olympux 19:5671f3c25342 341 uint16_t gateway[4] = {192,168,0,1};
olympux 19:5671f3c25342 342 uint16_t mac[3] = {u8MacAddr[3],u8MacAddr[4],u8MacAddr[5]};
olympux 19:5671f3c25342 343
olympux 19:5671f3c25342 344 uint16_t tcp_port = DEFAULT_LOCAL_TCP_SERVER_PORT;
olympux 19:5671f3c25342 345 uint16_t udp_port = DEFAULT_LOCAL_UDP_SERVER_PORT;
olympux 19:5671f3c25342 346
olympux 19:5671f3c25342 347 uint16_t remote_tcp_ip[4] = {192,168,0,2};
olympux 19:5671f3c25342 348 uint16_t remote_tcp_port = DEFAULT_REMOTE_TCP_SERVER_PORT;
olympux 19:5671f3c25342 349
olympux 19:5671f3c25342 350 uint16_t remote_udp_ip[4] = {192,168,0,2};
olympux 19:5671f3c25342 351 uint16_t remote_udp_port = DEFAULT_REMOTE_UDP_SERVER_PORT;
olympux 19:5671f3c25342 352
olympux 19:5671f3c25342 353 uint16_t auto_transmit = DEFAULT_DISABLE_FLAG_VALUE;
olympux 19:5671f3c25342 354 uint16_t transmit_period = DEFAULT_TRANSMIT_PERIOD;
olympux 19:5671f3c25342 355
olympux 19:5671f3c25342 356 uint16_t enable_tcp_server = DEFAULT_ENABLE_FLAG_VALUE;
olympux 19:5671f3c25342 357 uint16_t enable_tcp_client = DEFAULT_DISABLE_FLAG_VALUE;
olympux 19:5671f3c25342 358 uint16_t enable_udp_server = DEFAULT_DISABLE_FLAG_VALUE;
olympux 19:5671f3c25342 359 uint16_t enable_udp_client = DEFAULT_DISABLE_FLAG_VALUE;
olympux 19:5671f3c25342 360
olympux 20:858384cac44a 361 uint16_t inter_data_period = DEFAULT_INTER_DATA_PERIOD;
olympux 20:858384cac44a 362 uint16_t special_char = DEFAULT_SPECIAL_CHARACTER;
olympux 20:858384cac44a 363
olympux 21:23ae23754f0b 364 uint16_t uart_baudrate = DEFAULT_UART_BAUDRATE;
olympux 21:23ae23754f0b 365 uint16_t uart_bits = DEFAULT_UART_BITS;
olympux 21:23ae23754f0b 366 uint16_t uart_stopbits = DEFAULT_UART_STOPBITS;
olympux 21:23ae23754f0b 367 uint16_t uart_parity = DEFAULT_UART_PARITY;
olympux 21:23ae23754f0b 368 uint16_t uart_handshake = DEFAULT_UART_HANDSHAKE;
olympux 21:23ae23754f0b 369
olympux 19:5671f3c25342 370 // erase the device configured flag
olympux 19:5671f3c25342 371 erase_device_configuration();
olympux 19:5671f3c25342 372
olympux 19:5671f3c25342 373 // write new one
olympux 19:5671f3c25342 374 write_device_configuration(ip, subnet, gateway, mac,
olympux 19:5671f3c25342 375 tcp_port, udp_port,
olympux 19:5671f3c25342 376 remote_tcp_ip, remote_tcp_port, auto_transmit, transmit_period,
olympux 19:5671f3c25342 377 remote_udp_ip, remote_udp_port,
olympux 20:858384cac44a 378 enable_tcp_server, enable_tcp_client, enable_udp_server, enable_udp_client,
olympux 21:23ae23754f0b 379 inter_data_period, special_char,
olympux 21:23ae23754f0b 380 uart_baudrate, uart_bits, uart_stopbits, uart_parity, uart_handshake);
olympux 1:6bdc99dd8e0a 381 }