Initial Example Project

Dependencies:   SDFileSystem WIZnet_Library mbed

Fork of Nucleo_L152RE_W5500HelloWorld_ver2 by Edward ahn

Committer:
peter_h
Date:
Wed Jan 24 09:59:13 2018 +0000
Revision:
5:0a1a21449539
Parent:
4:17797698535b
Initial version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
peter_h 5:0a1a21449539 1 // Nucloe_L152RE_W5500_Ethernet Example
najgh08 2:a2cf65b34267 2
bangbh 0:412f9c1172b7 3 #include "mbed.h"
bangbh 0:412f9c1172b7 4 #include "WIZnetInterface.h"
peter_h 5:0a1a21449539 5 //#include "SDFileSystem.h"
bangbh 0:412f9c1172b7 6
peter_h 5:0a1a21449539 7 #define USE_DHCP 1 // DHCP
bangbh 0:412f9c1172b7 8
najgh08 3:fa8925fb003a 9 #define TCP_PORT 5000
najgh08 2:a2cf65b34267 10
najgh08 2:a2cf65b34267 11 const char * IP_Addr = "192.168.0.194";
bangbh 0:412f9c1172b7 12 const char * IP_Subnet = "255.255.255.0";
najgh08 2:a2cf65b34267 13 const char * IP_Gateway = "192.168.0.1";
najgh08 2:a2cf65b34267 14 unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x01,0x02,0x03};
najgh08 2:a2cf65b34267 15
najgh08 2:a2cf65b34267 16
peter_h 5:0a1a21449539 17 // SDFileSystem sd(PA_7, PA_6, PA_5, PB_5, "sd");
bangbh 0:412f9c1172b7 18
bangbh 0:412f9c1172b7 19 DigitalOut myled1(LED1);
bangbh 0:412f9c1172b7 20 Serial pc(USBTX, USBRX);
bangbh 0:412f9c1172b7 21
najgh08 2:a2cf65b34267 22 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
najgh08 4:17797698535b 23 WIZnetInterface ethernet(&spi, PB_6, PC_7);//scs(PB_6), nRESET(PA_9); // reset pin is dummy, don't affect any pin of WIZ550io
bangbh 0:412f9c1172b7 24
bangbh 0:412f9c1172b7 25 int main() {
najgh08 3:fa8925fb003a 26
bangbh 0:412f9c1172b7 27 //Set serial port baudrate speed: 115200
bangbh 0:412f9c1172b7 28 pc.baud(115200);
kzl108 1:9a6af61c5eb4 29 wait(10);
kzl108 1:9a6af61c5eb4 30 pc.printf("W5500 Application Started \r\n");
bangbh 0:412f9c1172b7 31
najgh08 3:fa8925fb003a 32 char buffer[128];
najgh08 3:fa8925fb003a 33
najgh08 2:a2cf65b34267 34 //SD-CARD
peter_h 5:0a1a21449539 35 // printf("Hello World!\n");
peter_h 5:0a1a21449539 36 // mkdir("/sd/mydir", 0777);
peter_h 5:0a1a21449539 37 // FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
peter_h 5:0a1a21449539 38 // if(fp == NULL) {
peter_h 5:0a1a21449539 39 // error("Could not open file for write\n");
peter_h 5:0a1a21449539 40 // }
peter_h 5:0a1a21449539 41 // fprintf(fp, "Hello fun SD Card World!");
peter_h 5:0a1a21449539 42 // fclose(fp);
peter_h 5:0a1a21449539 43 // printf("Goodbye World!\r\n");
najgh08 2:a2cf65b34267 44 //SD-CARD END
najgh08 2:a2cf65b34267 45
bangbh 0:412f9c1172b7 46 while(1)
bangbh 0:412f9c1172b7 47 {
bangbh 0:412f9c1172b7 48 #if USE_DHCP
najgh08 2:a2cf65b34267 49 pc.printf("DHCP ON \r\n");
bangbh 0:412f9c1172b7 50 int ret = ethernet.init(MAC_Addr);
bangbh 0:412f9c1172b7 51 #else
najgh08 2:a2cf65b34267 52 pc.printf("DHCP OFF \r\n");
bangbh 0:412f9c1172b7 53 int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
bangbh 0:412f9c1172b7 54 #endif
bangbh 0:412f9c1172b7 55
kzl108 1:9a6af61c5eb4 56
kzl108 1:9a6af61c5eb4 57 printf("SPI Initialized \r\n");
kzl108 1:9a6af61c5eb4 58 wait(1); // 1 second for stable state
kzl108 1:9a6af61c5eb4 59
kzl108 1:9a6af61c5eb4 60 printf("W5500 Networking Started \r\n");
kzl108 1:9a6af61c5eb4 61 wait(1); // 1 second for stable state
kzl108 1:9a6af61c5eb4 62
kzl108 1:9a6af61c5eb4 63
bangbh 0:412f9c1172b7 64 if (!ret) {
bangbh 0:412f9c1172b7 65 pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
bangbh 0:412f9c1172b7 66 ret = ethernet.connect();
bangbh 0:412f9c1172b7 67 if (!ret) {
bangbh 0:412f9c1172b7 68 pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
bangbh 0:412f9c1172b7 69 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
bangbh 0:412f9c1172b7 70 } else {
bangbh 0:412f9c1172b7 71 pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
bangbh 0:412f9c1172b7 72 exit(0);
bangbh 0:412f9c1172b7 73 }
bangbh 0:412f9c1172b7 74 } else {
bangbh 0:412f9c1172b7 75 pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
bangbh 0:412f9c1172b7 76 exit(0);
bangbh 0:412f9c1172b7 77 }
peter_h 5:0a1a21449539 78 TCPSocketServer server;
peter_h 5:0a1a21449539 79 server.bind(TCP_PORT);
peter_h 5:0a1a21449539 80 server.listen();
najgh08 2:a2cf65b34267 81
najgh08 2:a2cf65b34267 82 while (1) {
peter_h 5:0a1a21449539 83 pc.printf("\nWait for new connection...\r\n");
peter_h 5:0a1a21449539 84 TCPSocketConnection client;
peter_h 5:0a1a21449539 85 server.accept(client);
peter_h 5:0a1a21449539 86 client.set_blocking(false, 0); // Timeout=0.
peter_h 5:0a1a21449539 87 pc.printf("Connection from: %s\r\n", client.get_address());
peter_h 5:0a1a21449539 88 while (client.is_connected() == true) {
peter_h 5:0a1a21449539 89 int n = client.receive(buffer, sizeof(buffer));
peter_h 5:0a1a21449539 90 if(n > 0)
peter_h 5:0a1a21449539 91 client.send_all(buffer, n);
peter_h 5:0a1a21449539 92 if(client.is_fin_received())
peter_h 5:0a1a21449539 93 client.close();
bangbh 0:412f9c1172b7 94 }
bangbh 0:412f9c1172b7 95 pc.printf("Disconnected.\r\n");
bangbh 0:412f9c1172b7 96 }
bangbh 0:412f9c1172b7 97 }
bangbh 0:412f9c1172b7 98 }