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 6:d054e394fba3, committed 2014-09-21
- Comitter:
- olympux
- Date:
- Sun Sep 21 19:35:32 2014 +0000
- Parent:
- 5:a01dc14ec038
- Child:
- 7:d45bd480e90f
- Commit message:
- Tested writing and reading eeprom. Added set network configuration command (use udp broadcast address).
Changed in this revision
eeprom.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/eeprom.lib Sun Sep 21 16:24:05 2014 +0000 +++ b/eeprom.lib Sun Sep 21 19:35:32 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/olympux/code/eeprom/#0b8e4689a075 +http://mbed.org/users/olympux/code/eeprom/#30f2f413f549
--- a/main.cpp Sun Sep 21 16:24:05 2014 +0000 +++ b/main.cpp Sun Sep 21 19:35:32 2014 +0000 @@ -4,7 +4,7 @@ */ #include "mbed.h" -#include "eeprom.h" +//#include "eeprom.h" #include "EthernetInterface.h" #include "rtos.h" @@ -22,18 +22,23 @@ Serial uart(USBTX,USBRX); -/* Virtual address defined by the user: 0xFFFF value is prohibited */ -uint16_t VirtAddVarTab[NumbOfVar] = {0x5555, 0x6666, 0x7777}; - +// Variables' number +#define NumbOfVar ((uint8_t)0x0E) // 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 + }; +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 */ -#define TCP_SERVER_PORT 10000 -#define UDP_LOCAL_PORT 11000 -//#define USE_DHCP // DHCP or static - #define TCP_SERVER //#define TCP_CLIENT #define UDP_SERVER @@ -44,12 +49,14 @@ #define TCP_SERVER_RECEIVE_TIMEOUT 3000 #define UDP_SERVER_RECEIVE_TIMEOUT 200 -#ifndef USE_DHCP // for static IP setting char * IP_Addr = "192.168.0.120"; -const char * IP_Subnet = "255.255.255.0"; -const char * IP_Gateway = "192.168.0.1"; -#endif +char * IP_Subnet = "255.255.255.0"; +char * IP_Gateway = "192.168.0.1"; +char ip_addr[16], ip_subnet[16], ip_gateway[16]; // loaded from eeprom + +uint16_t tcp_server_port = 10000; // fixed +uint16_t udp_server_port = 11000; // fixed char buffer[256]; // socket buffer @@ -86,6 +93,9 @@ } +/* +* Ethernet init +*/ int ethernet_init(void) { uint8_t mac[6]; @@ -94,11 +104,7 @@ printf("Start\n"); - #ifdef USE_DHCP - int ret = eth.init(mac); //Use DHCP - #else - int ret = eth.init(mac, IP_Addr, IP_Subnet, IP_Gateway); // static - #endif + int ret = eth.init(mac, IP_Addr, IP_Subnet, IP_Gateway); // static if (!ret) { uart.printf("Initialized, MAC: %s\n", eth.getMACAddress()); @@ -119,34 +125,104 @@ } +/* +* EEPROM functions +*/ +void write_eeprom(char *buffer) { + // Unlock the Flash Program Erase controller */ + FLASH_Unlock(); + // EEPROM Init + EE_Init(); + // IP address + EE_WriteVariable(VirtAddVarTab[0], *buffer++); + EE_WriteVariable(VirtAddVarTab[1], *buffer++); + EE_WriteVariable(VirtAddVarTab[2], *buffer++); + EE_WriteVariable(VirtAddVarTab[3], *buffer++); + // IP subnet + EE_WriteVariable(VirtAddVarTab[4], *buffer++); + EE_WriteVariable(VirtAddVarTab[5], *buffer++); + EE_WriteVariable(VirtAddVarTab[6], *buffer++); + EE_WriteVariable(VirtAddVarTab[7], *buffer++); + // IP gateway + EE_WriteVariable(VirtAddVarTab[8], *buffer++); + EE_WriteVariable(VirtAddVarTab[9], *buffer++); + EE_WriteVariable(VirtAddVarTab[10], *buffer++); + EE_WriteVariable(VirtAddVarTab[11], *buffer++); + //// TCP server port + //EE_WriteVariable(VirtAddVarTab[12], *buffer++); + //// UDP server port + //EE_WriteVariable(VirtAddVarTab[13], *buffer++); + FLASH_Lock(); +} + + +void load_eeprom(void) { + // Load IP adddress + uint16_t u16ip_addr[4], u16ip_subnet[4], u16ip_gateway[4]; + //FLASH_Unlock(); + EE_Init(); + // IP address + EE_ReadVariable(VirtAddVarTab[0], &u16ip_addr[0]); + EE_ReadVariable(VirtAddVarTab[1], &u16ip_addr[1]); + EE_ReadVariable(VirtAddVarTab[2], &u16ip_addr[2]); + EE_ReadVariable(VirtAddVarTab[3], &u16ip_addr[3]); + // IP subnet + EE_ReadVariable(VirtAddVarTab[4], &u16ip_subnet[0]); + EE_ReadVariable(VirtAddVarTab[5], &u16ip_subnet[1]); + EE_ReadVariable(VirtAddVarTab[6], &u16ip_subnet[2]); + EE_ReadVariable(VirtAddVarTab[7], &u16ip_subnet[3]); + // IP gateway + EE_ReadVariable(VirtAddVarTab[8], &u16ip_gateway[0]); + EE_ReadVariable(VirtAddVarTab[9], &u16ip_gateway[1]); + EE_ReadVariable(VirtAddVarTab[10], &u16ip_gateway[2]); + EE_ReadVariable(VirtAddVarTab[11], &u16ip_gateway[3]); + //// TCP server port + //EE_ReadVariable(VirtAddVarTab[12], &tcp_server_port); + //// UDP server port + //EE_ReadVariable(VirtAddVarTab[13], &udp_server_port); + //FLASH_Lock(); + sprintf(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(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(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]); + printf("IP: %s\n", ip_addr); + printf("MASK: %s\n", ip_subnet); + printf("GW: %s\n", ip_gateway); + printf("TCP: %d\n", tcp_server_port); + printf("UDP: %d\n", udp_server_port); +} + + int main() { message_t message; + int ret; /* * configure */ uart.baud(115200); - /* - * FLASH - */ - // Unlock the Flash Program Erase controller */ - FLASH_Unlock(); - // EEPROM Init - EE_Init(); /* * UI threads */ Thread t1(uart_thread); + + /* + * FLASH + */ + load_eeprom(); + IP_Addr = ip_addr; + IP_Subnet = ip_subnet; + IP_Gateway = ip_gateway; + /* * Ethernet */ - int ret = ethernet_init(); + ret = ethernet_init(); if (ret) { printf("App halted\n"); while (true) {}; @@ -157,7 +233,7 @@ TCPSocketServer tcp_server; TCPSocketConnection tcp_client; - tcp_server.bind(TCP_SERVER_PORT); + tcp_server.bind(tcp_server_port); tcp_server.listen(); uart.printf("\nWait for new connection...\n"); tcp_server.set_blocking(false, TCP_SERVER_WAIT_CLIENT_TIMEOUT); @@ -167,7 +243,7 @@ UDPSocket udp_server; Endpoint ep_udp_client; - ret = udp_server.bind(UDP_LOCAL_PORT); + ret = udp_server.bind(udp_server_port); printf("sock.bind = %d\n", ret); udp_server.set_blocking(false, UDP_SERVER_RECEIVE_TIMEOUT); #endif @@ -225,10 +301,19 @@ //printf("Received packet from: %s\n", client.get_address()); udp_server.sendTo(ep_udp_client, buffer, n); - // Command = set new IP - if (strstr(buffer, "SetIP:")) { - IP_Addr = &buffer[6]; - printf("New IP=%s\n", IP_Addr); + // process received data + switch (n) { + // length = 14, maybe this is a command to set network configuration + // Format: 4E 4E C0 A8 00 78 FF FF FF 00 C0 A8 0 1 (NN 192.168.0.120; 255.255.255.0; 192.168.0.1) + case 14: + // check if two first chars = NN + if (strstr(buffer, "NN") != NULL) { + printf("Received new network configuration\n"); + write_eeprom(&buffer[2]); // parameters from 3rd char + } + break; + default: + break; } #endif } // network processor