Webserver only w/o any other functions, single thread. Running on STM32F013+W5500
Dependencies: NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed
Fork of F103-Serial-to-Ethernet by
Revision 12:7c152c0ca4d8, committed 2014-09-28
- Comitter:
- olympux
- Date:
- Sun Sep 28 17:35:54 2014 +0000
- Parent:
- 11:709f90a3b599
- Child:
- 13:bcf840da68fd
- Commit message:
- Added eeprom variables and functions for TCP server info when the device is in TCP client mode
Changed in this revision
--- a/eeprom.lib Sat Sep 27 10:33:49 2014 +0000 +++ b/eeprom.lib Sun Sep 28 17:35:54 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/olympux/code/eeprom/#4bf7c01c5a48 +http://mbed.org/users/olympux/code/eeprom/#535d7082fbb2
--- a/main.cpp Sat Sep 27 10:33:49 2014 +0000 +++ b/main.cpp Sun Sep 28 17:35:54 2014 +0000 @@ -4,11 +4,13 @@ */ #include "mbed.h" -//#include "eeprom.h" +#include "eeprom.h" #include "EthernetInterface.h" #include "NTPClient.h" #include "rtos.h" +#include "my_eeprom_funcs.h" + /* * Hardware defines @@ -49,32 +51,27 @@ //AnalogOut ano0(PA_8); //AnalogOut ano1(PA_15); -void update_digital_outputs(void); +void update_digital_outputs(char* buf); +void update_sending_frame(char* buf); -// eeprom -#define NumbOfVar ((uint8_t)0x80) // REMEMBER: update this variable in eeprom.h too -#define IP_ADDRESS_POS 0 -#define IP_SUBNET_POS 4 -#define IP_GATEWAY_POS 8 -#define TCP_SERVER_PORT_POS 12 -#define UDP_SERVER_PORT_POS 13 -#define FIRST_RUN_FLAG_POS 14 -#define MAC_ADDRESS_POS 15 +// EEPROM: device network configuration +//#define NumbOfVar ((uint8_t)0x80) // REMEMBER: update this variable in eeprom.h too // Virtual address defined by the user: 0xFFFF value is prohibited uint16_t VirtAddVarTab[NumbOfVar] = {0x1212, 0x1313, 0x1414, 0x1515, // IP_Addr 0x2212, 0x2313, 0x2414, 0x2515, // IP_Subnet 0x3212, 0x3313, 0x3414, 0x3515, // IP_Gateway 0x4212, // TCP server port, not used 0x5212, // UDP server port, not used - 0x8888, // 1st run? - 0x6212, 0x6313, 0x6414 // MAC + 0x8888, // 1st run? + 0x6212, 0x6313, 0x6414, // MAC + + // this section is for the TCP server that this device connects to in TCP client mode + 0x7212, 0x7313, // auto transmit status, time period + 0x8212, 0x8313,0x8414, 0x8515, // TCP server IP address + 0x9212 // TCP server port }; -extern "C" uint16_t EE_Init(void); -extern "C" uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data); -extern "C" uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data); - /* * Network configuration @@ -85,23 +82,13 @@ //#define UDP_CLIENT #define NTP -#define DEFAULT_IP_ADDRESS "192.168.0.249" -#define DEFAULT_IP_SUBNET "255.255.255.0" -#define DEFAULT_IP_GATEWAY "192.168.0.1" -#define DEFAULT_MAC0 0x00 -#define DEFAULT_MAC1 0x08 -#define DEFAULT_MAC2 0xDC -#define DEFAULT_MAC3 0x00 -#define DEFAULT_MAC4 0x00 -#define DEFAULT_MAC5 0x01 - #define TCP_SERVER_WAIT_CLIENT_TIMEOUT 200 #define TCP_SERVER_RECEIVE_TIMEOUT 3000 #define UDP_SERVER_RECEIVE_TIMEOUT 200 // for static IP setting uint8_t u8mac[6], u8ip_addr[4];// keep mac and ip address in 8-bits -char * IP_Addr; // pointers to ip_xxx[16] +char * IP_Addr; // pointers to str_ip_xxx[16] char * IP_Subnet; char * IP_Gateway; @@ -109,8 +96,20 @@ char str_ip_addr[16], str_ip_subnet[16], str_ip_gateway[16]; // for printf, converted from 16-bits u16ip_xxx uint16_t first_run = 0; // first run flag -const uint16_t tcp_server_port = 10000; // fixed -const uint16_t udp_server_port = 11000; // fixed +const uint16_t tcp_server_local_port = 10000; // fixed +const uint16_t udp_server_local_port = 11000; // fixed + +// TCP client: this section is used for the TCP server that this device connects to in TCP client mode +// this device will transmit status every transmit_time_period +uint16_t auto_transmit_flag = 0, transmit_time_period = 1000; // auto transmit status, time period = 1s +uint16_t u16server_ip_addr[4]; // directly loaded from eeprom +uint16_t u16tcp_server_port; // directly loaded from eeprom +uint8_t u8server_ip_addr[4]; // server ip address in 8-bits +char * server_ip_addr; // pointer to str_server_ip_addr +char str_server_ip_addr[16];// for printf, converted from 16-bits u16server_ip_addr + +extern void load_eeprom_network(void); + char buffer[256]; // socket buffer @@ -214,128 +213,6 @@ } -/* -* EEPROM functions -*/ -void write_eeprom(char *buffer) { - // Write network configuration - // 4-byte IP address + 4-byte subnet + 4-byte gateway + 3-byte MAC - - printf("Saving configuration\r\n"); - - // Unlock the Flash Program Erase controller */ - FLASH_Unlock(); - // EEPROM Init - EE_Init(); - // IP address - EE_WriteVariable(VirtAddVarTab[IP_ADDRESS_POS+0], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_ADDRESS_POS+1], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_ADDRESS_POS+2], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_ADDRESS_POS+3], *buffer++); - - // IP subnet - EE_WriteVariable(VirtAddVarTab[IP_SUBNET_POS+0], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_SUBNET_POS+1], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_SUBNET_POS+2], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_SUBNET_POS+3], *buffer++); - - // IP gateway - EE_WriteVariable(VirtAddVarTab[IP_GATEWAY_POS+0], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_GATEWAY_POS+1], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_GATEWAY_POS+2], *buffer++); - EE_WriteVariable(VirtAddVarTab[IP_GATEWAY_POS+3], *buffer++); - - //// TCP server port, not used - //EE_WriteVariable(VirtAddVarTab[TCP_SERVER_PORT_POS], *buffer++); - //// UDP server port, not used - //EE_WriteVariable(VirtAddVarTab[UDP_SERVER_PORT_POS], *buffer++); - - // erase first_run flag - EE_WriteVariable(VirtAddVarTab[FIRST_RUN_FLAG_POS], 0xA5A5); - - // MAC address - EE_WriteVariable(VirtAddVarTab[MAC_ADDRESS_POS+0], *buffer++); - EE_WriteVariable(VirtAddVarTab[MAC_ADDRESS_POS+1], *buffer++); - EE_WriteVariable(VirtAddVarTab[MAC_ADDRESS_POS+2], *buffer++); - FLASH_Lock(); - - printf("Success\r\n"); -} - - -void load_eeprom(void) { - mbed_mac_address((char *)u8mac); - - printf("Loading network configuration...\r\n"); - - EE_Init(); - - // check if 1st run - EE_ReadVariable(VirtAddVarTab[FIRST_RUN_FLAG_POS], &first_run); - // if not first run, load network config - if (first_run == 0xA5A5) { - printf("User settings\r\n"); - - // IP address - EE_ReadVariable(VirtAddVarTab[IP_ADDRESS_POS+0], &u16ip_addr[0]); - EE_ReadVariable(VirtAddVarTab[IP_ADDRESS_POS+1], &u16ip_addr[1]); - EE_ReadVariable(VirtAddVarTab[IP_ADDRESS_POS+2], &u16ip_addr[2]); - EE_ReadVariable(VirtAddVarTab[IP_ADDRESS_POS+3], &u16ip_addr[3]); - u8ip_addr[0] = (uint8_t)(u16ip_addr[0] & 0x00FF); - u8ip_addr[1] = (uint8_t)(u16ip_addr[1] & 0x00FF); - u8ip_addr[2] = (uint8_t)(u16ip_addr[2] & 0x00FF); - u8ip_addr[3] = (uint8_t)(u16ip_addr[3] & 0x00FF); - - // IP subnet - EE_ReadVariable(VirtAddVarTab[IP_SUBNET_POS+0], &u16ip_subnet[0]); - EE_ReadVariable(VirtAddVarTab[IP_SUBNET_POS+1], &u16ip_subnet[1]); - EE_ReadVariable(VirtAddVarTab[IP_SUBNET_POS+2], &u16ip_subnet[2]); - EE_ReadVariable(VirtAddVarTab[IP_SUBNET_POS+3], &u16ip_subnet[3]); - - // IP gateway - EE_ReadVariable(VirtAddVarTab[IP_GATEWAY_POS+0], &u16ip_gateway[0]); - EE_ReadVariable(VirtAddVarTab[IP_GATEWAY_POS+1], &u16ip_gateway[1]); - EE_ReadVariable(VirtAddVarTab[IP_GATEWAY_POS+2], &u16ip_gateway[2]); - EE_ReadVariable(VirtAddVarTab[IP_GATEWAY_POS+3], &u16ip_gateway[3]); - - //// TCP server port - //EE_ReadVariable(VirtAddVarTab[TCP_SERVER_PORT_POS], &tcp_server_port); - //// UDP server port - //EE_ReadVariable(VirtAddVarTab[UDP_SERVER_PORT_POS], &udp_server_port); - - // First run flag, already read above - - // MAC address - EE_ReadVariable(VirtAddVarTab[MAC_ADDRESS_POS+0], &u16mac_addr[0]); - EE_ReadVariable(VirtAddVarTab[MAC_ADDRESS_POS+1], &u16mac_addr[1]); - EE_ReadVariable(VirtAddVarTab[MAC_ADDRESS_POS+2], &u16mac_addr[2]); - u8mac[0] = DEFAULT_MAC0; u8mac[1] = DEFAULT_MAC1; u8mac[2] = DEFAULT_MAC2; - u8mac[3] = (uint8_t)(u16mac_addr[0] & 0x00FF); - u8mac[4] = (uint8_t)(u16mac_addr[1] & 0x00FF); - u8mac[5] = (uint8_t)(u16mac_addr[2] & 0x00FF); - //FLASH_Lock(); - - sprintf(str_ip_addr, "%d.%d.%d.%d", (uint8_t)u16ip_addr[0], (uint8_t)u16ip_addr[1], (uint8_t)u16ip_addr[2], (uint8_t)u16ip_addr[3]); - sprintf(str_ip_subnet, "%d.%d.%d.%d", (uint8_t)u16ip_subnet[0], (uint8_t)u16ip_subnet[1], (uint8_t)u16ip_subnet[2], (uint8_t)u16ip_subnet[3]); - sprintf(str_ip_gateway, "%d.%d.%d.%d", (uint8_t)u16ip_gateway[0], (uint8_t)u16ip_gateway[1], (uint8_t)u16ip_gateway[2], (uint8_t)u16ip_gateway[3]); - } - // if 1st run, use default addresses - else { - printf("No user settings, load defaults\r\n"); - u8mac[0] = DEFAULT_MAC0; u8mac[1] = DEFAULT_MAC1; u8mac[2] = DEFAULT_MAC2; - u8mac[3] = DEFAULT_MAC3; u8mac[4] = DEFAULT_MAC4; u8mac[5] = DEFAULT_MAC5; - sprintf(str_ip_addr, DEFAULT_IP_ADDRESS); - sprintf(str_ip_subnet, DEFAULT_IP_SUBNET); - sprintf(str_ip_gateway, DEFAULT_IP_GATEWAY); - } - - printf("Success\r\n"); - printf("IP: %s\r\n", str_ip_addr); - printf("MASK: %s\r\n", str_ip_subnet); - printf("GW: %s\r\n", str_ip_gateway); - printf("TCP server: %d\r\n", tcp_server_port); - printf("UDP server: %d\r\n", udp_server_port); -} @@ -359,7 +236,7 @@ /* * FLASH */ - load_eeprom(); + load_eeprom_network(); IP_Addr = str_ip_addr; IP_Subnet = str_ip_subnet; IP_Gateway = str_ip_gateway; @@ -374,12 +251,15 @@ while (true) {}; } - + +/* +* TCP/UDP setup +*/ #ifdef TCP_SERVER TCPSocketServer tcp_server; TCPSocketConnection tcp_client; - tcp_server.bind(tcp_server_port); + tcp_server.bind(tcp_server_local_port); tcp_server.listen(); printf("TCP server started...\r\n"); tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT); @@ -389,13 +269,15 @@ UDPSocket udp_server; Endpoint ep_udp_client; - ret = udp_server.bind(udp_server_port); + ret = udp_server.bind(udp_server_local_port); printf("UDP started (sock.bind = %d)\r\n", ret); udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT); #endif - // Network processor + /* + * Network processor + */ while (true) { // FOR INTERFACING #ifdef TCP_SERVER @@ -444,7 +326,10 @@ // digital outputs if (buffer[RECEIVING_PROTOCOL_EN_DO_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { printf("Update digital outputs\r\n"); - update_digital_outputs(); + char str_dout[9]; + memcpy(str_dout, &buffer[RECEIVING_PROTOCOL_DO_POS], 8); + str_dout[8] = '\0'; + update_digital_outputs(str_dout); } // analog output 0 if (buffer[RECEIVING_PROTOCOL_EN_A0O_POS] == RECEIVING_PROTOCOL_ENABLE_OUTPUT) { @@ -467,17 +352,7 @@ if (buffer[RECEIVING_PROTOCOL_COMMAND_POS] == QUERY_STATUS_COMMAND) { printf("Sent device status through TCP\r\n"); // sending protocol - memcpy(&buffer[SENDING_PROTOCOL_ID_POS], DEVICE_ID, 4); // device id - memcpy(&buffer[SENDING_PROTOCOL_MAC_POS], &u8mac, 6); - memcpy(&buffer[SENDING_PROTOCOL_IP_POS], &u8ip_addr, 4); - //memcpy(&buffer[SENDING_PROTOCOL_DI_POS], - //memcpy(&buffer[SENDING_PROTOCOL_D0_POS], - //memcpy(&buffer[SENDING_PROTOCOL_AI0_POS], - //memcpy(&buffer[SENDING_PROTOCOL_AI1_POS], - //memcpy(&buffer[SENDING_PROTOCOL_AO0_POS], - //memcpy(&buffer[SENDING_PROTOCOL_AO1_POS], - buffer[SENDING_PROTOCOL_CR_POS] = 0x0D; - buffer[SENDING_PROTOCOL_CR_POS+1] = '\0'; + update_sending_frame(buffer); tcp_client.send_all(buffer, SENDING_PROTOCOL_LENGTH); } @@ -525,13 +400,13 @@ // ask for TCP server port else if (strstr(buffer, "NNIOTP") != NULL) { char port[5]; - sprintf(port, "%5d", tcp_server_port); + sprintf(port, "%5d", tcp_server_local_port); udp_server.sendTo(ep_udp_client, port, strlen(port)); } // NNIOTP // ask for UDP server port else if (strstr(buffer, "NNIOUP") != NULL) { char port[5]; - sprintf(port, "%5d", udp_server_port); + sprintf(port, "%5d", udp_server_local_port); udp_server.sendTo(ep_udp_client, port, strlen(port)); } // NNIOUP else if (strstr(buffer, "NNIOTM") != NULL) { @@ -573,7 +448,7 @@ break; printf("Received user configuration\r\n"); - write_eeprom(&buffer[4]); // parameters from 3rd char, 15-bytes + write_eeprom_network(&buffer[4]); // parameters from 3rd char, 15-bytes break; } default: @@ -583,100 +458,53 @@ } // network processor } -void update_digital_outputs(void) { - char dat = buffer[RECEIVING_PROTOCOL_DO_POS+0]; - switch (dat) { - case DIGITAL_HIGH: - dout0 = 1; - break; - case DIGITAL_LOW: - dout0 = 0; - break; - default: - break; - } +/* +* Update digital outputs following receiving frame from TCP client +*/ +void update_digital_outputs(char* buf) { + printf("Digital outputs: %s\n", buf); - dat = buffer[RECEIVING_PROTOCOL_DO_POS+1]; - switch (dat) { - case DIGITAL_HIGH: - dout1 = 1; - break; - case DIGITAL_LOW: - dout1 = 0; - break; - default: - break; - } - - dat = buffer[RECEIVING_PROTOCOL_DO_POS+2]; - switch (dat) { - case DIGITAL_HIGH: - dout2 = 1; - break; - case DIGITAL_LOW: - dout2 = 0; - break; - default: - break; - } - - dat = buffer[RECEIVING_PROTOCOL_DO_POS+3]; - switch (dat) { - case DIGITAL_HIGH: - dout3 = 1; - break; - case DIGITAL_LOW: - dout3 = 0; - break; - default: - break; - } + dout0 = (buf[0] == DIGITAL_HIGH)? 1 : 0; + dout1 = (buf[1] == DIGITAL_HIGH)? 1 : 0; + dout2 = (buf[2] == DIGITAL_HIGH)? 1 : 0; + dout3 = (buf[3] == DIGITAL_HIGH)? 1 : 0; + dout4 = (buf[4] == DIGITAL_HIGH)? 1 : 0; + dout5 = (buf[5] == DIGITAL_HIGH)? 1 : 0; + dout6 = (buf[6] == DIGITAL_HIGH)? 1 : 0; + dout7 = (buf[7] == DIGITAL_HIGH)? 1 : 0; +} + +void update_sending_frame(char* buf) { + memcpy(&buf[SENDING_PROTOCOL_ID_POS], DEVICE_ID, 4); // device id + memcpy(&buf[SENDING_PROTOCOL_MAC_POS], &u8mac, 6); + memcpy(&buf[SENDING_PROTOCOL_IP_POS], &u8ip_addr, 4); - dat = buffer[RECEIVING_PROTOCOL_DO_POS+4]; - switch (dat) { - case DIGITAL_HIGH: - dout1 = 4; - break; - case DIGITAL_LOW: - dout1 = 4; - break; - default: - break; - } - - dat = buffer[RECEIVING_PROTOCOL_DO_POS+5]; - switch (dat) { - case DIGITAL_HIGH: - dout5 = 1; - break; - case DIGITAL_LOW: - dout5 = 0; - break; - default: - break; - } + buf[SENDING_PROTOCOL_DI_POS+0] = (din0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DI_POS+1] = (din1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DI_POS+2] = (din2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DI_POS+3] = (din3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DI_POS+4] = (din4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DI_POS+5] = (din5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DI_POS+6] = (din6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DI_POS+7] = (din7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; - dat = buffer[RECEIVING_PROTOCOL_DO_POS+6]; - switch (dat) { - case DIGITAL_HIGH: - dout6 = 1; - break; - case DIGITAL_LOW: - dout6 = 0; - break; - default: - break; - } + buf[SENDING_PROTOCOL_DO_POS+0] = (dout0 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DO_POS+1] = (dout1 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DO_POS+2] = (dout2 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DO_POS+3] = (dout3 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DO_POS+4] = (dout4 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DO_POS+5] = (dout5 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DO_POS+6] = (dout6 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; + buf[SENDING_PROTOCOL_DO_POS+7] = (dout7 == 1) ? DIGITAL_HIGH : DIGITAL_LOW; - dat = buffer[RECEIVING_PROTOCOL_DO_POS+7]; - switch (dat) { - case DIGITAL_HIGH: - dout7 = 1; - break; - case DIGITAL_LOW: - dout7 = 0; - break; - default: - break; - } + uint16_t val = ain0.read_u16(); + memcpy(&buf[SENDING_PROTOCOL_AI0_POS], &val, 2); + val = ain1.read_u16(); + memcpy(&buf[SENDING_PROTOCOL_AI1_POS], &val, 2); + val = 0; + memcpy(&buf[SENDING_PROTOCOL_AO0_POS], &val, 2); + val = 0; + memcpy(&buf[SENDING_PROTOCOL_AO1_POS], &val, 2); + buf[SENDING_PROTOCOL_CR_POS] = 0x0D; + buf[SENDING_PROTOCOL_CR_POS+1] = '\0'; } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/my_eeprom_funcs.lib Sun Sep 28 17:35:54 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/olympux/code/my_eeprom_funcs/#6bdc99dd8e0a