Threads + Network Demo

Dependencies:   EthernetInterface mbed-rtos mbed-src

Fork of TestNetComm by Malcolm Nixon

Committer:
MalcolmNixon
Date:
Sat May 10 21:52:53 2014 +0000
Revision:
6:97573a8dce83
Parent:
5:18415c556f04
Simple demo showing a crash in EthernetInterface::init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MalcolmNixon 0:9eba2bd74769 1 #include "EthernetInterface.h"
MalcolmNixon 0:9eba2bd74769 2 #include "Thread.h"
MalcolmNixon 0:9eba2bd74769 3 #include "mbed.h"
MalcolmNixon 0:9eba2bd74769 4
MalcolmNixon 6:97573a8dce83 5 Serial pc(USBTX, USBRX);
MalcolmNixon 6:97573a8dce83 6
MalcolmNixon 5:18415c556f04 7 void led2ThreadProc(const void *param)
MalcolmNixon 3:1e86c29d1eac 8 {
MalcolmNixon 5:18415c556f04 9 // Toggle LED2
MalcolmNixon 5:18415c556f04 10 DigitalOut led2(LED2, 1);
MalcolmNixon 4:f2789e665f4e 11 for (;;)
MalcolmNixon 4:f2789e665f4e 12 {
MalcolmNixon 4:f2789e665f4e 13 led2 = !led2;
MalcolmNixon 4:f2789e665f4e 14 Thread::wait(1000);
MalcolmNixon 4:f2789e665f4e 15 }
MalcolmNixon 2:d62b28f02d45 16 }
MalcolmNixon 0:9eba2bd74769 17
MalcolmNixon 5:18415c556f04 18 void led3ThreadProc(const void *param)
MalcolmNixon 5:18415c556f04 19 {
MalcolmNixon 5:18415c556f04 20 // Toggle LED3
MalcolmNixon 5:18415c556f04 21 DigitalOut led3(LED3, 1);
MalcolmNixon 5:18415c556f04 22 for (;;)
MalcolmNixon 5:18415c556f04 23 {
MalcolmNixon 5:18415c556f04 24 led3 = !led3;
MalcolmNixon 5:18415c556f04 25 Thread::wait(2000);
MalcolmNixon 5:18415c556f04 26 }
MalcolmNixon 5:18415c556f04 27 }
MalcolmNixon 5:18415c556f04 28
MalcolmNixon 1:429f7bca62aa 29 int main(void)
MalcolmNixon 0:9eba2bd74769 30 {
MalcolmNixon 6:97573a8dce83 31 int nr;
MalcolmNixon 6:97573a8dce83 32
MalcolmNixon 6:97573a8dce83 33 // Start LED2 toggle
MalcolmNixon 6:97573a8dce83 34 pc.printf("Starting LED 2 thread\r\n");
MalcolmNixon 5:18415c556f04 35 Thread led2(led2ThreadProc);
MalcolmNixon 6:97573a8dce83 36 osDelay(1000);
MalcolmNixon 6:97573a8dce83 37
MalcolmNixon 6:97573a8dce83 38 // Start LED3 toggle
MalcolmNixon 6:97573a8dce83 39 pc.printf("Starting LED 3 thread\r\n");
MalcolmNixon 5:18415c556f04 40 Thread led3(led3ThreadProc);
MalcolmNixon 6:97573a8dce83 41 osDelay(1000);
MalcolmNixon 3:1e86c29d1eac 42
MalcolmNixon 6:97573a8dce83 43 // Initialize the network
MalcolmNixon 6:97573a8dce83 44 pc.printf("Initializing network\r\n");
MalcolmNixon 6:97573a8dce83 45 nr = EthernetInterface::init("192.168.100.5", "255.255.255.0", "0.0.0.0");
MalcolmNixon 6:97573a8dce83 46 pc.printf(" Network Init: %d\r\n", nr);
MalcolmNixon 6:97573a8dce83 47
MalcolmNixon 6:97573a8dce83 48 // Connect to the network
MalcolmNixon 6:97573a8dce83 49 pc.printf("Connecting to the network\r\n");
MalcolmNixon 6:97573a8dce83 50 nr = EthernetInterface::connect();
MalcolmNixon 6:97573a8dce83 51 pc.printf(" Connected: %d\r\n", nr);
MalcolmNixon 4:f2789e665f4e 52
MalcolmNixon 1:429f7bca62aa 53 // Construct the server
MalcolmNixon 1:429f7bca62aa 54 TCPSocketServer svr;
MalcolmNixon 1:429f7bca62aa 55 svr.bind(1234);
MalcolmNixon 1:429f7bca62aa 56 svr.listen();
MalcolmNixon 4:f2789e665f4e 57
MalcolmNixon 0:9eba2bd74769 58 for (;;)
MalcolmNixon 0:9eba2bd74769 59 {
MalcolmNixon 1:429f7bca62aa 60 // Accept the next connection
MalcolmNixon 6:97573a8dce83 61 TCPSocketConnection conn;
MalcolmNixon 4:f2789e665f4e 62 svr.accept(conn);
MalcolmNixon 4:f2789e665f4e 63
MalcolmNixon 4:f2789e665f4e 64 // Manage the connection
MalcolmNixon 4:f2789e665f4e 65 for (;;)
MalcolmNixon 1:429f7bca62aa 66 {
MalcolmNixon 4:f2789e665f4e 67 // Read the input
MalcolmNixon 4:f2789e665f4e 68 char buf[32];
MalcolmNixon 4:f2789e665f4e 69 int nr = conn.receive(buf, 32);
MalcolmNixon 4:f2789e665f4e 70 if (nr <= 0)
MalcolmNixon 1:429f7bca62aa 71 {
MalcolmNixon 4:f2789e665f4e 72 break;
MalcolmNixon 1:429f7bca62aa 73 }
MalcolmNixon 4:f2789e665f4e 74
MalcolmNixon 4:f2789e665f4e 75 // Write the response twice
MalcolmNixon 4:f2789e665f4e 76 conn.send(buf, nr);
MalcolmNixon 4:f2789e665f4e 77 conn.send(buf, nr);
MalcolmNixon 1:429f7bca62aa 78 }
MalcolmNixon 4:f2789e665f4e 79 }
MalcolmNixon 4:f2789e665f4e 80 }