Used with eeprom_flash to write network configuration to STM32F103 flash
Fork of my_eeprom_funcs by
device_configuration.cpp@22:0e1d8a9e8f54, 2016-09-01 (annotated)
- Committer:
- olympux
- Date:
- Thu Sep 01 14:14:26 2016 +0000
- Revision:
- 22:0e1d8a9e8f54
- Parent:
- 21:23ae23754f0b
Add DHCP configuration
Who changed what in which revision?
User | Revision | Line number | New 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 | 22:0e1d8a9e8f54 | 53 | // dhcp vs static |
olympux | 22:0e1d8a9e8f54 | 54 | uint16_t u16EnableDHCP; |
olympux | 20:858384cac44a | 55 | |
olympux | 20:858384cac44a | 56 | // extra, not actually in EEPROM |
olympux | 16:700377cd9d29 | 57 | uint8_t u8IpAddr[4]; // keep device ip address in 8-bits |
olympux | 16:700377cd9d29 | 58 | uint8_t u8MacAddr[6]; // keep mac in 8-bits |
olympux | 16:700377cd9d29 | 59 | uint8_t u8RemoteTcpServerIpAddr[4]; // remote TCP server ip address in 8-bits |
olympux | 18:d88314ae7979 | 60 | uint8_t u8RemoteUdpServerIpAddr[4]; // remote UDP server ip address in 8-bits |
olympux | 4:944a4646b825 | 61 | |
olympux | 19:5671f3c25342 | 62 | |
olympux | 19:5671f3c25342 | 63 | // erase page |
olympux | 19:5671f3c25342 | 64 | void erase_device_configuration() { |
olympux | 19:5671f3c25342 | 65 | // erase first_run flag |
olympux | 19:5671f3c25342 | 66 | enableEEPROMWriting(); |
olympux | 19:5671f3c25342 | 67 | disableEEPROMWriting(); |
olympux | 19:5671f3c25342 | 68 | } |
olympux | 19:5671f3c25342 | 69 | |
olympux | 19:5671f3c25342 | 70 | |
olympux | 12:613ab276bf37 | 71 | /*! |
olympux | 12:613ab276bf37 | 72 | * Function to write module network configuration |
olympux | 12:613ab276bf37 | 73 | * @param <char *buf> configuration buffer |
olympux | 16:700377cd9d29 | 74 | * @note 4x16-bit IP address |
olympux | 16:700377cd9d29 | 75 | * @note 4x16-bit subnet |
olympux | 16:700377cd9d29 | 76 | * @note 4x16-bit gateway |
olympux | 16:700377cd9d29 | 77 | * @note 3x16-bit MAC |
olympux | 16:700377cd9d29 | 78 | * @note 16-bit TCP local port |
olympux | 16:700377cd9d29 | 79 | * @note 16-bit UDP local port |
olympux | 12:613ab276bf37 | 80 | */ |
olympux | 18:d88314ae7979 | 81 | void write_device_configuration(uint16_t* ip, uint16_t* subnet, uint16_t* gateway, uint16_t* mac, |
olympux | 18:d88314ae7979 | 82 | uint16_t tcp_port, uint16_t udp_port, |
olympux | 18:d88314ae7979 | 83 | uint16_t* remote_tcp_ip, uint16_t remote_tcp_port, uint16_t auto_transmit, uint16_t transmit_period, |
olympux | 18:d88314ae7979 | 84 | uint16_t* remote_udp_ip, uint16_t remote_udp_port, |
olympux | 20:858384cac44a | 85 | uint16_t enable_tcp_server, uint16_t enable_tcp_client, uint16_t enable_udp_server, uint16_t enable_udp_client, |
olympux | 21:23ae23754f0b | 86 | uint16_t inter_data_period, uint16_t special_char, |
olympux | 22:0e1d8a9e8f54 | 87 | uint16_t uart_baudrate, uint16_t uart_bits, uint16_t uart_stopbits, uint16_t uart_parity, uint16_t uart_handshake, |
olympux | 22:0e1d8a9e8f54 | 88 | uint16_t enable_dhcp) { |
olympux | 1:6bdc99dd8e0a | 89 | // Write network configuration |
olympux | 1:6bdc99dd8e0a | 90 | // 4-byte IP address + 4-byte subnet + 4-byte gateway + 3-byte MAC |
olympux | 16:700377cd9d29 | 91 | // + local TCP server port + local UDP port |
olympux | 1:6bdc99dd8e0a | 92 | |
olympux | 16:700377cd9d29 | 93 | INFO("Saving network configuration for this device..."); |
olympux | 16:700377cd9d29 | 94 | |
olympux | 16:700377cd9d29 | 95 | enableEEPROMWriting(); |
olympux | 6:241d1539914a | 96 | |
olympux | 6:241d1539914a | 97 | // erase first_run flag |
olympux | 16:700377cd9d29 | 98 | writeEEPROMHalfWord(DEVICE_CONFIGURED_FLAG_POS, DEFAULT_ENABLE_FLAG_VALUE); |
olympux | 6:241d1539914a | 99 | |
olympux | 1:6bdc99dd8e0a | 100 | // IP address |
olympux | 16:700377cd9d29 | 101 | writeEEPROMHalfWord(IP_ADDRESS_POS+2*0, ip[0]); |
olympux | 16:700377cd9d29 | 102 | writeEEPROMHalfWord(IP_ADDRESS_POS+2*1, ip[1]); |
olympux | 16:700377cd9d29 | 103 | writeEEPROMHalfWord(IP_ADDRESS_POS+2*2, ip[2]); |
olympux | 16:700377cd9d29 | 104 | writeEEPROMHalfWord(IP_ADDRESS_POS+2*3, ip[3]); |
olympux | 1:6bdc99dd8e0a | 105 | |
olympux | 1:6bdc99dd8e0a | 106 | // IP subnet |
olympux | 16:700377cd9d29 | 107 | writeEEPROMHalfWord(IP_SUBNET_POS+2*0, subnet[0]); |
olympux | 16:700377cd9d29 | 108 | writeEEPROMHalfWord(IP_SUBNET_POS+2*1, subnet[1]); |
olympux | 16:700377cd9d29 | 109 | writeEEPROMHalfWord(IP_SUBNET_POS+2*2, subnet[2]); |
olympux | 16:700377cd9d29 | 110 | writeEEPROMHalfWord(IP_SUBNET_POS+2*3, subnet[3]); |
olympux | 1:6bdc99dd8e0a | 111 | |
olympux | 1:6bdc99dd8e0a | 112 | // IP gateway |
olympux | 16:700377cd9d29 | 113 | writeEEPROMHalfWord(IP_GATEWAY_POS+2*0, gateway[0]); |
olympux | 16:700377cd9d29 | 114 | writeEEPROMHalfWord(IP_GATEWAY_POS+2*1, gateway[1]); |
olympux | 16:700377cd9d29 | 115 | writeEEPROMHalfWord(IP_GATEWAY_POS+2*2, gateway[2]); |
olympux | 16:700377cd9d29 | 116 | writeEEPROMHalfWord(IP_GATEWAY_POS+2*3, gateway[3]); |
olympux | 1:6bdc99dd8e0a | 117 | |
olympux | 1:6bdc99dd8e0a | 118 | // MAC address |
olympux | 16:700377cd9d29 | 119 | writeEEPROMHalfWord(MAC_ADDRESS_POS+2*0, mac[0]); |
olympux | 16:700377cd9d29 | 120 | writeEEPROMHalfWord(MAC_ADDRESS_POS+2*1, mac[1]); |
olympux | 16:700377cd9d29 | 121 | writeEEPROMHalfWord(MAC_ADDRESS_POS+2*2, mac[2]); |
olympux | 16:700377cd9d29 | 122 | |
olympux | 16:700377cd9d29 | 123 | // Ports |
olympux | 16:700377cd9d29 | 124 | writeEEPROMHalfWord(LOCAL_TCP_SERVER_PORT_POS, tcp_port); |
olympux | 18:d88314ae7979 | 125 | writeEEPROMHalfWord(LOCAL_UDP_SERVER_PORT_POS, udp_port); |
olympux | 16:700377cd9d29 | 126 | |
olympux | 16:700377cd9d29 | 127 | // Remote TCP server |
olympux | 18:d88314ae7979 | 128 | writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*0, remote_tcp_ip[0]); |
olympux | 18:d88314ae7979 | 129 | writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*1, remote_tcp_ip[1]); |
olympux | 18:d88314ae7979 | 130 | writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*2, remote_tcp_ip[2]); |
olympux | 18:d88314ae7979 | 131 | writeEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*3, remote_tcp_ip[3]); |
olympux | 1:6bdc99dd8e0a | 132 | |
olympux | 18:d88314ae7979 | 133 | // Remote TCP server port |
olympux | 18:d88314ae7979 | 134 | writeEEPROMHalfWord(REMOTE_TCP_SERVER_PORT_POS, remote_tcp_port); |
olympux | 16:700377cd9d29 | 135 | |
olympux | 16:700377cd9d29 | 136 | // Auto transmit |
olympux | 16:700377cd9d29 | 137 | writeEEPROMHalfWord(AUTO_TRANSMIT_FLAG_POS, auto_transmit); |
olympux | 16:700377cd9d29 | 138 | writeEEPROMHalfWord(AUTO_TRANSMIT_TIME_PERIOD_POS, transmit_period); |
olympux | 16:700377cd9d29 | 139 | |
olympux | 18:d88314ae7979 | 140 | // Remote UDP server |
olympux | 18:d88314ae7979 | 141 | writeEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*0, remote_udp_ip[0]); |
olympux | 18:d88314ae7979 | 142 | writeEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*1, remote_udp_ip[1]); |
olympux | 18:d88314ae7979 | 143 | writeEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*2, remote_udp_ip[2]); |
olympux | 18:d88314ae7979 | 144 | writeEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*3, remote_udp_ip[3]); |
olympux | 18:d88314ae7979 | 145 | |
olympux | 18:d88314ae7979 | 146 | // Remote UDP server port |
olympux | 18:d88314ae7979 | 147 | writeEEPROMHalfWord(REMOTE_UDP_SERVER_PORT_POS, remote_udp_port); |
olympux | 18:d88314ae7979 | 148 | |
olympux | 16:700377cd9d29 | 149 | // Enable modes |
olympux | 16:700377cd9d29 | 150 | writeEEPROMHalfWord(ENABLE_TCP_SERVER_POS, enable_tcp_server); |
olympux | 16:700377cd9d29 | 151 | writeEEPROMHalfWord(ENABLE_TCP_CLIENT_POS, enable_tcp_client); |
olympux | 18:d88314ae7979 | 152 | writeEEPROMHalfWord(ENABLE_UDP_SERVER_POS, enable_udp_server); |
olympux | 18:d88314ae7979 | 153 | writeEEPROMHalfWord(ENABLE_UDP_CLIENT_POS, enable_udp_client); |
olympux | 16:700377cd9d29 | 154 | |
olympux | 20:858384cac44a | 155 | // Serial inter-data period |
olympux | 20:858384cac44a | 156 | writeEEPROMHalfWord(INTER_DATA_PERIOD_POS, inter_data_period); |
olympux | 20:858384cac44a | 157 | |
olympux | 20:858384cac44a | 158 | // Special char |
olympux | 20:858384cac44a | 159 | writeEEPROMHalfWord(SPECIAL_CHAR_POS, special_char); |
olympux | 20:858384cac44a | 160 | |
olympux | 21:23ae23754f0b | 161 | // UART |
olympux | 21:23ae23754f0b | 162 | writeEEPROMHalfWord(UART_BAUDRATE_POS, uart_baudrate); |
olympux | 21:23ae23754f0b | 163 | writeEEPROMHalfWord(UART_BITS_POS, uart_bits); |
olympux | 21:23ae23754f0b | 164 | writeEEPROMHalfWord(UART_STOPBITS_POS, uart_stopbits); |
olympux | 21:23ae23754f0b | 165 | writeEEPROMHalfWord(UART_PARITY_POS, uart_parity); |
olympux | 21:23ae23754f0b | 166 | writeEEPROMHalfWord(UART_HANDSHAKE_POS, uart_handshake); |
olympux | 21:23ae23754f0b | 167 | |
olympux | 22:0e1d8a9e8f54 | 168 | // DHCP vs Static |
olympux | 22:0e1d8a9e8f54 | 169 | writeEEPROMHalfWord(ENABLE_DHCP_POS, enable_dhcp); |
olympux | 22:0e1d8a9e8f54 | 170 | |
olympux | 16:700377cd9d29 | 171 | disableEEPROMWriting(); |
olympux | 16:700377cd9d29 | 172 | |
olympux | 16:700377cd9d29 | 173 | INFO("Successful"); |
olympux | 1:6bdc99dd8e0a | 174 | } |
olympux | 1:6bdc99dd8e0a | 175 | |
olympux | 10:f37acffddef9 | 176 | /*! |
olympux | 10:f37acffddef9 | 177 | * Function to load module network configuration |
olympux | 10:f37acffddef9 | 178 | */ |
olympux | 16:700377cd9d29 | 179 | void read_device_configuration(void) { |
olympux | 16:700377cd9d29 | 180 | mbed_mac_address((char *)u8MacAddr); |
olympux | 1:6bdc99dd8e0a | 181 | |
olympux | 16:700377cd9d29 | 182 | INFO("Loading network configuration..."); |
olympux | 1:6bdc99dd8e0a | 183 | |
olympux | 16:700377cd9d29 | 184 | // check if configured |
olympux | 16:700377cd9d29 | 185 | u16DeviceConfiguredFlag = readEEPROMHalfWord(DEVICE_CONFIGURED_FLAG_POS); |
olympux | 1:6bdc99dd8e0a | 186 | |
olympux | 1:6bdc99dd8e0a | 187 | // if not first run, load network config |
olympux | 16:700377cd9d29 | 188 | if (u16DeviceConfiguredFlag == DEFAULT_ENABLE_FLAG_VALUE) { |
olympux | 16:700377cd9d29 | 189 | INFO("User settings"); |
olympux | 1:6bdc99dd8e0a | 190 | |
olympux | 1:6bdc99dd8e0a | 191 | // IP address |
olympux | 16:700377cd9d29 | 192 | u16IpAddr[0] = readEEPROMHalfWord(IP_ADDRESS_POS+2*0); |
olympux | 16:700377cd9d29 | 193 | u16IpAddr[1] = readEEPROMHalfWord(IP_ADDRESS_POS+2*1); |
olympux | 16:700377cd9d29 | 194 | u16IpAddr[2] = readEEPROMHalfWord(IP_ADDRESS_POS+2*2); |
olympux | 16:700377cd9d29 | 195 | u16IpAddr[3] = readEEPROMHalfWord(IP_ADDRESS_POS+2*3); |
olympux | 16:700377cd9d29 | 196 | u8IpAddr[0] = (uint8_t)(u16IpAddr[0] & 0x00FF); |
olympux | 16:700377cd9d29 | 197 | u8IpAddr[1] = (uint8_t)(u16IpAddr[1] & 0x00FF); |
olympux | 16:700377cd9d29 | 198 | u8IpAddr[2] = (uint8_t)(u16IpAddr[2] & 0x00FF); |
olympux | 16:700377cd9d29 | 199 | u8IpAddr[3] = (uint8_t)(u16IpAddr[3] & 0x00FF); |
olympux | 1:6bdc99dd8e0a | 200 | |
olympux | 1:6bdc99dd8e0a | 201 | // IP subnet |
olympux | 16:700377cd9d29 | 202 | u16IpSubnet[0] = readEEPROMHalfWord(IP_SUBNET_POS+2*0); |
olympux | 16:700377cd9d29 | 203 | u16IpSubnet[1] = readEEPROMHalfWord(IP_SUBNET_POS+2*1); |
olympux | 16:700377cd9d29 | 204 | u16IpSubnet[2] = readEEPROMHalfWord(IP_SUBNET_POS+2*2); |
olympux | 16:700377cd9d29 | 205 | u16IpSubnet[3] = readEEPROMHalfWord(IP_SUBNET_POS+2*3); |
olympux | 1:6bdc99dd8e0a | 206 | |
olympux | 1:6bdc99dd8e0a | 207 | // IP gateway |
olympux | 16:700377cd9d29 | 208 | u16IpGateway[0] = readEEPROMHalfWord(IP_GATEWAY_POS+2*0); |
olympux | 16:700377cd9d29 | 209 | u16IpGateway[1] = readEEPROMHalfWord(IP_GATEWAY_POS+2*1); |
olympux | 16:700377cd9d29 | 210 | u16IpGateway[2] = readEEPROMHalfWord(IP_GATEWAY_POS+2*2); |
olympux | 16:700377cd9d29 | 211 | u16IpGateway[3] = readEEPROMHalfWord(IP_GATEWAY_POS+2*3); |
olympux | 1:6bdc99dd8e0a | 212 | |
olympux | 1:6bdc99dd8e0a | 213 | // MAC address |
olympux | 16:700377cd9d29 | 214 | u16MacAddr[0] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*0); |
olympux | 16:700377cd9d29 | 215 | u16MacAddr[1] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*1); |
olympux | 16:700377cd9d29 | 216 | u16MacAddr[2] = readEEPROMHalfWord(MAC_ADDRESS_POS+2*2); |
olympux | 16:700377cd9d29 | 217 | u8MacAddr[0] = DEFAULT_MAC0; u8MacAddr[1] = DEFAULT_MAC1; u8MacAddr[2] = DEFAULT_MAC2; |
olympux | 16:700377cd9d29 | 218 | u8MacAddr[3] = (uint8_t)(u16MacAddr[0] & 0x00FF); |
olympux | 16:700377cd9d29 | 219 | u8MacAddr[4] = (uint8_t)(u16MacAddr[1] & 0x00FF); |
olympux | 16:700377cd9d29 | 220 | u8MacAddr[5] = (uint8_t)(u16MacAddr[2] & 0x00FF); |
olympux | 16:700377cd9d29 | 221 | |
olympux | 16:700377cd9d29 | 222 | // Ports |
olympux | 16:700377cd9d29 | 223 | u16LocalTcpServerPort = readEEPROMHalfWord(LOCAL_TCP_SERVER_PORT_POS); |
olympux | 18:d88314ae7979 | 224 | u16LocalUdpServerPort = readEEPROMHalfWord(LOCAL_UDP_SERVER_PORT_POS); |
olympux | 1:6bdc99dd8e0a | 225 | |
olympux | 16:700377cd9d29 | 226 | // Remote TCP server |
olympux | 16:700377cd9d29 | 227 | u16RemoteTcpServerIpAddr[0] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*0); |
olympux | 16:700377cd9d29 | 228 | u16RemoteTcpServerIpAddr[1] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*1); |
olympux | 16:700377cd9d29 | 229 | u16RemoteTcpServerIpAddr[2] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*2); |
olympux | 16:700377cd9d29 | 230 | u16RemoteTcpServerIpAddr[3] = readEEPROMHalfWord(REMOTE_TCP_SERVER_IP_ADDR_POS+2*3); |
olympux | 16:700377cd9d29 | 231 | u8RemoteTcpServerIpAddr[0] = (uint8_t)(u16RemoteTcpServerIpAddr[0] & 0x00FF); |
olympux | 16:700377cd9d29 | 232 | u8RemoteTcpServerIpAddr[1] = (uint8_t)(u16RemoteTcpServerIpAddr[1] & 0x00FF); |
olympux | 16:700377cd9d29 | 233 | u8RemoteTcpServerIpAddr[2] = (uint8_t)(u16RemoteTcpServerIpAddr[2] & 0x00FF); |
olympux | 16:700377cd9d29 | 234 | u8RemoteTcpServerIpAddr[3] = (uint8_t)(u16RemoteTcpServerIpAddr[3] & 0x00FF); |
olympux | 16:700377cd9d29 | 235 | |
olympux | 18:d88314ae7979 | 236 | // Remote TCP server port |
olympux | 16:700377cd9d29 | 237 | u16RemoteTcpServerPort = readEEPROMHalfWord(REMOTE_TCP_SERVER_PORT_POS); |
olympux | 16:700377cd9d29 | 238 | |
olympux | 16:700377cd9d29 | 239 | // Auto transmit |
olympux | 16:700377cd9d29 | 240 | u16AutoTransmitFlag = readEEPROMHalfWord(AUTO_TRANSMIT_FLAG_POS); |
olympux | 16:700377cd9d29 | 241 | u16TransmitPeriod = readEEPROMHalfWord(AUTO_TRANSMIT_TIME_PERIOD_POS); |
olympux | 16:700377cd9d29 | 242 | |
olympux | 18:d88314ae7979 | 243 | // Remote UDP server |
olympux | 18:d88314ae7979 | 244 | u16RemoteUdpServerIpAddr[0] = readEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*0); |
olympux | 18:d88314ae7979 | 245 | u16RemoteUdpServerIpAddr[1] = readEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*1); |
olympux | 18:d88314ae7979 | 246 | u16RemoteUdpServerIpAddr[2] = readEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*2); |
olympux | 18:d88314ae7979 | 247 | u16RemoteUdpServerIpAddr[3] = readEEPROMHalfWord(REMOTE_UDP_SERVER_IP_ADDR_POS+2*3); |
olympux | 18:d88314ae7979 | 248 | u8RemoteUdpServerIpAddr[0] = (uint8_t)(u16RemoteUdpServerIpAddr[0] & 0x00FF); |
olympux | 18:d88314ae7979 | 249 | u8RemoteUdpServerIpAddr[1] = (uint8_t)(u16RemoteUdpServerIpAddr[1] & 0x00FF); |
olympux | 18:d88314ae7979 | 250 | u8RemoteUdpServerIpAddr[2] = (uint8_t)(u16RemoteUdpServerIpAddr[2] & 0x00FF); |
olympux | 18:d88314ae7979 | 251 | u8RemoteUdpServerIpAddr[3] = (uint8_t)(u16RemoteUdpServerIpAddr[3] & 0x00FF); |
olympux | 18:d88314ae7979 | 252 | |
olympux | 19:5671f3c25342 | 253 | // Remote UDP server port |
olympux | 18:d88314ae7979 | 254 | u16RemoteUdpServerPort = readEEPROMHalfWord(REMOTE_UDP_SERVER_PORT_POS); |
olympux | 18:d88314ae7979 | 255 | |
olympux | 16:700377cd9d29 | 256 | // Enable modes |
olympux | 16:700377cd9d29 | 257 | u16EnableTcpServer = readEEPROMHalfWord(ENABLE_TCP_SERVER_POS); |
olympux | 16:700377cd9d29 | 258 | u16EnableTcpClient = readEEPROMHalfWord(ENABLE_TCP_CLIENT_POS); |
olympux | 18:d88314ae7979 | 259 | u16EnableUdpServer = readEEPROMHalfWord(ENABLE_UDP_SERVER_POS); |
olympux | 18:d88314ae7979 | 260 | u16EnableUdpClient = readEEPROMHalfWord(ENABLE_UDP_CLIENT_POS); |
olympux | 16:700377cd9d29 | 261 | |
olympux | 20:858384cac44a | 262 | // Serial inter-data period |
olympux | 20:858384cac44a | 263 | u16InterDataPeriod = readEEPROMHalfWord(INTER_DATA_PERIOD_POS); |
olympux | 20:858384cac44a | 264 | // Special character |
olympux | 20:858384cac44a | 265 | u16SpecialChar = readEEPROMHalfWord(SPECIAL_CHAR_POS); |
olympux | 20:858384cac44a | 266 | |
olympux | 21:23ae23754f0b | 267 | // UART |
olympux | 21:23ae23754f0b | 268 | u16UartBaudrate = readEEPROMHalfWord(UART_BAUDRATE_POS); |
olympux | 21:23ae23754f0b | 269 | u16UartBits = readEEPROMHalfWord(UART_BITS_POS); |
olympux | 21:23ae23754f0b | 270 | u16UartStopBits = readEEPROMHalfWord(UART_STOPBITS_POS); |
olympux | 21:23ae23754f0b | 271 | u16UartParity = readEEPROMHalfWord(UART_PARITY_POS); |
olympux | 21:23ae23754f0b | 272 | u16UartHandshake = readEEPROMHalfWord(UART_HANDSHAKE_POS); |
olympux | 21:23ae23754f0b | 273 | |
olympux | 22:0e1d8a9e8f54 | 274 | // DHCP |
olympux | 22:0e1d8a9e8f54 | 275 | u16EnableDHCP = readEEPROMHalfWord(ENABLE_DHCP_POS); |
olympux | 22:0e1d8a9e8f54 | 276 | |
olympux | 21:23ae23754f0b | 277 | // convert to strings |
olympux | 16:700377cd9d29 | 278 | sprintf(strIpAddr, "%d.%d.%d.%d", u8IpAddr[0], u8IpAddr[1], u8IpAddr[2], u8IpAddr[3]); |
olympux | 16:700377cd9d29 | 279 | 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 | 280 | 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 | 281 | sprintf(strRemoteTcpServerIpAddr, "%d.%d.%d.%d", u8RemoteTcpServerIpAddr[0], u8RemoteTcpServerIpAddr[1], u8RemoteTcpServerIpAddr[2], u8RemoteTcpServerIpAddr[3]); |
olympux | 18:d88314ae7979 | 282 | sprintf(strRemoteUdpServerIpAddr, "%d.%d.%d.%d", u8RemoteUdpServerIpAddr[0], u8RemoteUdpServerIpAddr[1], u8RemoteUdpServerIpAddr[2], u8RemoteUdpServerIpAddr[3]); |
olympux | 17:574f90b11417 | 283 | sprintf(strMacAddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", u8MacAddr[0], u8MacAddr[1], u8MacAddr[2], u8MacAddr[3], u8MacAddr[4], u8MacAddr[5]); |
olympux | 1:6bdc99dd8e0a | 284 | } |
olympux | 8:4fc4b1b5509b | 285 | // if ip is not configured, use default addresses |
olympux | 1:6bdc99dd8e0a | 286 | else { |
olympux | 16:700377cd9d29 | 287 | INFO("No user settings, load defaults"); |
olympux | 16:700377cd9d29 | 288 | u8MacAddr[0] = DEFAULT_MAC0; u8MacAddr[1] = DEFAULT_MAC1; u8MacAddr[2] = DEFAULT_MAC2; |
olympux | 16:700377cd9d29 | 289 | u8MacAddr[3] = DEFAULT_MAC3; u8MacAddr[4] = DEFAULT_MAC4; u8MacAddr[5] = DEFAULT_MAC5; |
olympux | 16:700377cd9d29 | 290 | sprintf(strIpAddr, DEFAULT_IP_ADDRESS); |
olympux | 16:700377cd9d29 | 291 | sprintf(strIpSubnet, DEFAULT_IP_SUBNET); |
olympux | 16:700377cd9d29 | 292 | sprintf(strIpGateway, DEFAULT_IP_GATEWAY); |
olympux | 19:5671f3c25342 | 293 | sprintf(strMacAddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", u8MacAddr[0], u8MacAddr[1], u8MacAddr[2], u8MacAddr[3], u8MacAddr[4], u8MacAddr[5]); |
olympux | 19:5671f3c25342 | 294 | |
olympux | 19:5671f3c25342 | 295 | u16LocalTcpServerPort = DEFAULT_LOCAL_TCP_SERVER_PORT; |
olympux | 19:5671f3c25342 | 296 | u16LocalUdpServerPort = DEFAULT_LOCAL_UDP_SERVER_PORT; |
olympux | 19:5671f3c25342 | 297 | |
olympux | 16:700377cd9d29 | 298 | sprintf(strRemoteTcpServerIpAddr, DEFAULT_REMOTE_TCP_SERVER_IP); |
olympux | 19:5671f3c25342 | 299 | u16RemoteTcpServerPort = DEFAULT_REMOTE_TCP_SERVER_PORT; |
olympux | 19:5671f3c25342 | 300 | |
olympux | 18:d88314ae7979 | 301 | sprintf(strRemoteUdpServerIpAddr, DEFAULT_REMOTE_UDP_SERVER_IP); |
olympux | 19:5671f3c25342 | 302 | u16RemoteUdpServerPort = DEFAULT_REMOTE_UDP_SERVER_PORT; |
olympux | 19:5671f3c25342 | 303 | |
olympux | 19:5671f3c25342 | 304 | u16AutoTransmitFlag = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 305 | u16TransmitPeriod = DEFAULT_TRANSMIT_PERIOD; |
olympux | 22:0e1d8a9e8f54 | 306 | // mode |
olympux | 19:5671f3c25342 | 307 | u16EnableTcpServer = DEFAULT_ENABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 308 | u16EnableTcpClient = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 309 | u16EnableUdpServer = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 310 | u16EnableUdpClient = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 22:0e1d8a9e8f54 | 311 | // signals |
olympux | 20:858384cac44a | 312 | u16InterDataPeriod = DEFAULT_INTER_DATA_PERIOD; |
olympux | 20:858384cac44a | 313 | u16SpecialChar = DEFAULT_SPECIAL_CHARACTER; |
olympux | 22:0e1d8a9e8f54 | 314 | // uart |
olympux | 21:23ae23754f0b | 315 | u16UartBaudrate = DEFAULT_UART_BAUDRATE; |
olympux | 21:23ae23754f0b | 316 | u16UartBits = DEFAULT_UART_BITS; |
olympux | 21:23ae23754f0b | 317 | u16UartStopBits = DEFAULT_UART_STOPBITS; |
olympux | 21:23ae23754f0b | 318 | u16UartParity = DEFAULT_UART_PARITY; |
olympux | 21:23ae23754f0b | 319 | u16UartHandshake = DEFAULT_UART_HANDSHAKE; |
olympux | 22:0e1d8a9e8f54 | 320 | |
olympux | 22:0e1d8a9e8f54 | 321 | u16EnableDHCP = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 1:6bdc99dd8e0a | 322 | } |
olympux | 1:6bdc99dd8e0a | 323 | |
olympux | 16:700377cd9d29 | 324 | INFO("Successful"); |
olympux | 16:700377cd9d29 | 325 | INFO("IP: %s", strIpAddr); |
olympux | 16:700377cd9d29 | 326 | INFO("MASK: %s", strIpSubnet); |
olympux | 16:700377cd9d29 | 327 | INFO("GW: %s", strIpGateway); |
olympux | 16:700377cd9d29 | 328 | INFO("TCP server local port: %d", u16LocalTcpServerPort); |
olympux | 18:d88314ae7979 | 329 | INFO("UDP server local port: %d", u16LocalUdpServerPort); |
olympux | 18:d88314ae7979 | 330 | |
olympux | 18:d88314ae7979 | 331 | INFO("Remote TCP server: %s", strRemoteTcpServerIpAddr); |
olympux | 18:d88314ae7979 | 332 | INFO("Remote TCP server port: %d", u16RemoteTcpServerPort); |
olympux | 18:d88314ae7979 | 333 | |
olympux | 18:d88314ae7979 | 334 | INFO("Remote UDP server: %s", strRemoteUdpServerIpAddr); |
olympux | 18:d88314ae7979 | 335 | INFO("Remote UDP server port: %d", u16RemoteUdpServerPort); |
olympux | 18:d88314ae7979 | 336 | |
olympux | 18:d88314ae7979 | 337 | INFO("Auto transmit: %s", (u16AutoTransmitFlag == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable"); |
olympux | 18:d88314ae7979 | 338 | INFO("Period: %d", u16TransmitPeriod); |
olympux | 16:700377cd9d29 | 339 | |
olympux | 16:700377cd9d29 | 340 | INFO("TCP server: %s", (u16EnableTcpServer == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable"); |
olympux | 16:700377cd9d29 | 341 | INFO("TCP client: %s", (u16EnableTcpClient == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable"); |
olympux | 18:d88314ae7979 | 342 | INFO("UDP server: %s", (u16EnableUdpServer == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable"); |
olympux | 18:d88314ae7979 | 343 | INFO("UDP client: %s", (u16EnableUdpClient == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable"); |
olympux | 20:858384cac44a | 344 | |
olympux | 20:858384cac44a | 345 | INFO("Serial inter-data period: %d", u16InterDataPeriod); |
olympux | 20:858384cac44a | 346 | INFO("Special character: %.2x", u16SpecialChar); |
olympux | 22:0e1d8a9e8f54 | 347 | |
olympux | 22:0e1d8a9e8f54 | 348 | INFO("DHCP: %s", (u16EnableDHCP == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable"); |
olympux | 1:6bdc99dd8e0a | 349 | } |
olympux | 1:6bdc99dd8e0a | 350 | |
olympux | 19:5671f3c25342 | 351 | void reset_default_device_configuration() { |
olympux | 19:5671f3c25342 | 352 | uint16_t ip[4] = {192,168,0,120}; |
olympux | 19:5671f3c25342 | 353 | uint16_t subnet[4] = {255,255,255,0}; |
olympux | 19:5671f3c25342 | 354 | uint16_t gateway[4] = {192,168,0,1}; |
olympux | 19:5671f3c25342 | 355 | uint16_t mac[3] = {u8MacAddr[3],u8MacAddr[4],u8MacAddr[5]}; |
olympux | 19:5671f3c25342 | 356 | |
olympux | 19:5671f3c25342 | 357 | uint16_t tcp_port = DEFAULT_LOCAL_TCP_SERVER_PORT; |
olympux | 19:5671f3c25342 | 358 | uint16_t udp_port = DEFAULT_LOCAL_UDP_SERVER_PORT; |
olympux | 19:5671f3c25342 | 359 | |
olympux | 19:5671f3c25342 | 360 | uint16_t remote_tcp_ip[4] = {192,168,0,2}; |
olympux | 19:5671f3c25342 | 361 | uint16_t remote_tcp_port = DEFAULT_REMOTE_TCP_SERVER_PORT; |
olympux | 19:5671f3c25342 | 362 | |
olympux | 19:5671f3c25342 | 363 | uint16_t remote_udp_ip[4] = {192,168,0,2}; |
olympux | 19:5671f3c25342 | 364 | uint16_t remote_udp_port = DEFAULT_REMOTE_UDP_SERVER_PORT; |
olympux | 19:5671f3c25342 | 365 | |
olympux | 19:5671f3c25342 | 366 | uint16_t auto_transmit = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 367 | uint16_t transmit_period = DEFAULT_TRANSMIT_PERIOD; |
olympux | 19:5671f3c25342 | 368 | |
olympux | 19:5671f3c25342 | 369 | uint16_t enable_tcp_server = DEFAULT_ENABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 370 | uint16_t enable_tcp_client = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 371 | uint16_t enable_udp_server = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 372 | uint16_t enable_udp_client = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 19:5671f3c25342 | 373 | |
olympux | 20:858384cac44a | 374 | uint16_t inter_data_period = DEFAULT_INTER_DATA_PERIOD; |
olympux | 20:858384cac44a | 375 | uint16_t special_char = DEFAULT_SPECIAL_CHARACTER; |
olympux | 20:858384cac44a | 376 | |
olympux | 21:23ae23754f0b | 377 | uint16_t uart_baudrate = DEFAULT_UART_BAUDRATE; |
olympux | 21:23ae23754f0b | 378 | uint16_t uart_bits = DEFAULT_UART_BITS; |
olympux | 21:23ae23754f0b | 379 | uint16_t uart_stopbits = DEFAULT_UART_STOPBITS; |
olympux | 21:23ae23754f0b | 380 | uint16_t uart_parity = DEFAULT_UART_PARITY; |
olympux | 21:23ae23754f0b | 381 | uint16_t uart_handshake = DEFAULT_UART_HANDSHAKE; |
olympux | 21:23ae23754f0b | 382 | |
olympux | 22:0e1d8a9e8f54 | 383 | uint16_t enable_dhcp = DEFAULT_DISABLE_FLAG_VALUE; |
olympux | 22:0e1d8a9e8f54 | 384 | |
olympux | 19:5671f3c25342 | 385 | // erase the device configured flag |
olympux | 19:5671f3c25342 | 386 | erase_device_configuration(); |
olympux | 19:5671f3c25342 | 387 | |
olympux | 19:5671f3c25342 | 388 | // write new one |
olympux | 19:5671f3c25342 | 389 | write_device_configuration(ip, subnet, gateway, mac, |
olympux | 19:5671f3c25342 | 390 | tcp_port, udp_port, |
olympux | 19:5671f3c25342 | 391 | remote_tcp_ip, remote_tcp_port, auto_transmit, transmit_period, |
olympux | 19:5671f3c25342 | 392 | remote_udp_ip, remote_udp_port, |
olympux | 20:858384cac44a | 393 | enable_tcp_server, enable_tcp_client, enable_udp_server, enable_udp_client, |
olympux | 21:23ae23754f0b | 394 | inter_data_period, special_char, |
olympux | 22:0e1d8a9e8f54 | 395 | uart_baudrate, uart_bits, uart_stopbits, uart_parity, uart_handshake, |
olympux | 22:0e1d8a9e8f54 | 396 | enable_dhcp); |
olympux | 1:6bdc99dd8e0a | 397 | } |