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 2:18f10e7209f4, committed 2014-09-20
- Comitter:
- olympux
- Date:
- Sat Sep 20 21:33:04 2014 +0000
- Parent:
- 1:fc6d2a8a4eb3
- Child:
- 3:972ed747474c
- Commit message:
- use rtos queue
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Sep 20 20:48:35 2014 +0000 +++ b/main.cpp Sat Sep 20 21:33:04 2014 +0000 @@ -1,32 +1,17 @@ /* * -* Test program for W5500 mbed Library +* Alarm and Monitoring application */ #include "mbed.h" #include "EthernetInterface.h" #include "rtos.h" -#define ECHO_SERVER_PORT 5000 -/** -* Setting DHCP or static -*/ -//#define USE_DHCP - -/** -* Setting the platform to test +/* +* Hardware defines */ -//#define LPC -#define ST_NUCLEO -//#define FRDM_KL25Z -//#define Seeeduino_Arch - -#ifdef LPC -// LPC1768 & LPC11U24 -SPI spi(p5, p6, p7); // mosi, miso, sclk -WIZnetInterface eth(&spi, p8, p9); // spi, cs, reset -#endif +#define ST_NUCLEO // hardware pin mapping #ifdef ST_NUCLEO // ST Nucleo @@ -34,20 +19,14 @@ EthernetInterface eth(&spi, PC_8, PC_9); // spi, cs, reset #endif -#ifdef FRDM_KL25Z -// Freescale FRDM KL25Z -SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk -WIZnetInterface eth(&spi, PTD0, PTA20); // spi, cs, reset -#endif +Serial uart(USBTX,USBRX); + -#ifdef Seeeduino_Arch -// Seeedstudio Arch -SPI spi(P1_22, P1_21, P1_20); // mosi, miso, sclk -WIZnetInterface eth(&spi, P0_2, P0_0); // spi, cs, reset - Serial pc(P1_13, P1_14); // tx, rx -#else - Serial pc(USBTX,USBRX); -#endif +/* +* Network configuration +*/ +#define TCP_SERVER_PORT 10000 +//#define USE_DHCP // DHCP or static #ifndef USE_DHCP // for static IP setting @@ -57,27 +36,48 @@ #endif + +/* +* RTOS +*/ +struct message_t { + int len; + char *msg; +}; +Queue<message_t, 16> queue; + + + +/* +* Threads +*/ void uart_thread(void const *args) { - int i = 0; + message_t *p_message; while (true) { - printf("%d", i++); - Thread::wait(2000); + osEvent evt = queue.get(); + if (evt.status == osEventMessage) { + p_message = (message_t*)evt.value.p; + uart.printf("len=%d\n",p_message->len); + uart.printf("msg=%s\n",p_message->msg); + } } } int main() { + Thread t1(uart_thread); + //Thread t2(tcp_server_thread); + uint8_t mac[6]; - Thread thread(uart_thread); mbed_mac_address((char *)mac); // using the MAC address in LPC11U24 or LPC1178 mac[0] = 0x00; mac[1] = 0x08; mac[2] = 0xDC; mac[3] = 0x00; mac[4] = 0x00; mac[5] = 0x00; // you can alo use WIZ550io's MAC address by enabling "USE_WIZ550IO_MAC" in wiznet.h - pc.printf("Start\n"); + uart.printf("Start\n"); #ifdef USE_DHCP int ret = eth.init(mac); //Use DHCP #else @@ -85,37 +85,45 @@ #endif if (!ret) { - pc.printf("Initialized, MAC: %s\n", eth.getMACAddress()); + uart.printf("Initialized, MAC: %s\n", eth.getMACAddress()); } else { - pc.printf("Error eth.init() - ret = %d\n", ret); + uart.printf("Error eth.init() - ret = %d\n", ret); return -1; } ret = eth.connect(); if (!ret) { - pc.printf("IP: %s, MASK: %s, GW: %s\n", + uart.printf("IP: %s, MASK: %s, GW: %s\n", eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); } else { - pc.printf("Error eth.connect() - ret = %d\n", ret); + uart.printf("Error eth.connect() - ret = %d\n", ret); return -1; } TCPSocketServer server; - server.bind(ECHO_SERVER_PORT); + server.bind(TCP_SERVER_PORT); server.listen(); while (true) { - pc.printf("\nWait for new connection...\n"); + uart.printf("\nWait for new connection...\n"); TCPSocketConnection client; server.accept(client); client.set_blocking(false, 10000); // Timeout after (10)s - pc.printf("Connection from: %s\n", client.get_address()); + uart.printf("Connection from: %s\n", client.get_address()); char buffer[256]; + message_t message; while (true) { int n = client.receive(buffer, sizeof(buffer)); if (n <= 0) break; - + + // send to uart + buffer[n] = '\0'; + message.len = n; + message.msg = buffer; + queue.put(&message); + + // echo to tcp client client.send_all(buffer, n); if (n <= 0) break; }