Nucleo_L152RE_W5500HelloWorld

Dependencies:   SDFileSystem WIZnet_Library mbed

Fork of Nucleo_F103RB_W5500HelloWorld by WIZnet

Committer:
najgh08
Date:
Tue Jan 19 00:28:08 2016 +0000
Revision:
3:c39d3606b9e1
Parent:
2:a2cf65b34267
Reset pin change PA_9(D8) -> PC_7(D9)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
najgh08 2:a2cf65b34267 1 // Nucloe_L152RE_W5500_Ethernet shield Exmaple
najgh08 2:a2cf65b34267 2
bangbh 0:412f9c1172b7 3 #include "mbed.h"
bangbh 0:412f9c1172b7 4 #include "WIZnetInterface.h"
najgh08 2:a2cf65b34267 5 #include "SDFileSystem.h"
najgh08 2:a2cf65b34267 6 #include "UDPSocket.h"
bangbh 0:412f9c1172b7 7
najgh08 2:a2cf65b34267 8 //#define USE_DHCP 1 // DHCP 사용할때 사용
najgh08 2:a2cf65b34267 9 //#define USE_UDP 1
najgh08 2:a2cf65b34267 10 #define USE_TCP 1
bangbh 0:412f9c1172b7 11
najgh08 2:a2cf65b34267 12 #define TCP_PORT 5000
najgh08 2:a2cf65b34267 13 #define UDP_PORT 7000
najgh08 2:a2cf65b34267 14
najgh08 2:a2cf65b34267 15 const char * IP_Addr = "192.168.0.194";
bangbh 0:412f9c1172b7 16 const char * IP_Subnet = "255.255.255.0";
najgh08 2:a2cf65b34267 17 const char * IP_Gateway = "192.168.0.1";
najgh08 2:a2cf65b34267 18 unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x01,0x02,0x03};
najgh08 2:a2cf65b34267 19
najgh08 2:a2cf65b34267 20 const char * UDP_IP = "0.0.0.0";
najgh08 2:a2cf65b34267 21
najgh08 2:a2cf65b34267 22 SDFileSystem sd(PA_7, PA_6, PA_5, PB_5, "sd");
bangbh 0:412f9c1172b7 23
bangbh 0:412f9c1172b7 24 DigitalOut myled1(LED1);
bangbh 0:412f9c1172b7 25 Serial pc(USBTX, USBRX);
bangbh 0:412f9c1172b7 26
najgh08 2:a2cf65b34267 27 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
najgh08 3:c39d3606b9e1 28 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 29
bangbh 0:412f9c1172b7 30 int main() {
najgh08 2:a2cf65b34267 31
bangbh 0:412f9c1172b7 32 //Set serial port baudrate speed: 115200
bangbh 0:412f9c1172b7 33 pc.baud(115200);
kzl108 1:9a6af61c5eb4 34 wait(10);
kzl108 1:9a6af61c5eb4 35 pc.printf("W5500 Application Started \r\n");
bangbh 0:412f9c1172b7 36
bangbh 0:412f9c1172b7 37 char buffer[256];
bangbh 0:412f9c1172b7 38
najgh08 2:a2cf65b34267 39 //SD-CARD
najgh08 2:a2cf65b34267 40 printf("Hello World!\n");
najgh08 2:a2cf65b34267 41 mkdir("/sd/mydir", 0777);
najgh08 2:a2cf65b34267 42 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
najgh08 2:a2cf65b34267 43 if(fp == NULL) {
najgh08 2:a2cf65b34267 44 error("Could not open file for write\n");
najgh08 2:a2cf65b34267 45 }
najgh08 2:a2cf65b34267 46 fprintf(fp, "Hello fun SD Card World!");
najgh08 2:a2cf65b34267 47 fclose(fp);
najgh08 2:a2cf65b34267 48 printf("Goodbye World!\n");
najgh08 2:a2cf65b34267 49 //SD-CARD END
najgh08 2:a2cf65b34267 50
bangbh 0:412f9c1172b7 51 while(1)
bangbh 0:412f9c1172b7 52 {
bangbh 0:412f9c1172b7 53 #if USE_DHCP
najgh08 2:a2cf65b34267 54 pc.printf("DHCP ON \r\n");
bangbh 0:412f9c1172b7 55 int ret = ethernet.init(MAC_Addr);
bangbh 0:412f9c1172b7 56 #else
najgh08 2:a2cf65b34267 57 pc.printf("DHCP OFF \r\n");
bangbh 0:412f9c1172b7 58 int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
bangbh 0:412f9c1172b7 59 #endif
bangbh 0:412f9c1172b7 60
kzl108 1:9a6af61c5eb4 61
kzl108 1:9a6af61c5eb4 62 printf("SPI Initialized \r\n");
kzl108 1:9a6af61c5eb4 63 wait(1); // 1 second for stable state
kzl108 1:9a6af61c5eb4 64
kzl108 1:9a6af61c5eb4 65 printf("W5500 Networking Started \r\n");
kzl108 1:9a6af61c5eb4 66 wait(1); // 1 second for stable state
kzl108 1:9a6af61c5eb4 67
kzl108 1:9a6af61c5eb4 68
bangbh 0:412f9c1172b7 69 if (!ret) {
bangbh 0:412f9c1172b7 70 pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
bangbh 0:412f9c1172b7 71 ret = ethernet.connect();
bangbh 0:412f9c1172b7 72 if (!ret) {
bangbh 0:412f9c1172b7 73 pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
bangbh 0:412f9c1172b7 74 ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
bangbh 0:412f9c1172b7 75 } else {
bangbh 0:412f9c1172b7 76 pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
bangbh 0:412f9c1172b7 77 exit(0);
bangbh 0:412f9c1172b7 78 }
bangbh 0:412f9c1172b7 79 } else {
bangbh 0:412f9c1172b7 80 pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
bangbh 0:412f9c1172b7 81 exit(0);
bangbh 0:412f9c1172b7 82 }
najgh08 2:a2cf65b34267 83 #if USE_UDP
najgh08 2:a2cf65b34267 84 UDPSocket UDP_server;
najgh08 2:a2cf65b34267 85 UDP_server.init();
najgh08 2:a2cf65b34267 86 Endpoint nist;
bangbh 0:412f9c1172b7 87
najgh08 2:a2cf65b34267 88 nist.set_address(UDP_IP, UDP_PORT);
najgh08 2:a2cf65b34267 89 UDP_server.bind(UDP_PORT);
najgh08 2:a2cf65b34267 90 pc.printf("UDP PORT %d \r\n", UDP_PORT);
najgh08 2:a2cf65b34267 91 pc.printf("\nWait for message...\r\n");
najgh08 2:a2cf65b34267 92
najgh08 2:a2cf65b34267 93 while (1) {
najgh08 2:a2cf65b34267 94 int n = UDP_server.receiveFrom(nist, buffer, sizeof(buffer));
najgh08 2:a2cf65b34267 95 if(n > 0){
najgh08 2:a2cf65b34267 96 UDP_server.sendTo(nist, buffer, n);
najgh08 2:a2cf65b34267 97 }
najgh08 2:a2cf65b34267 98 }
najgh08 2:a2cf65b34267 99 #endif
najgh08 2:a2cf65b34267 100 #if USE_TCP
bangbh 0:412f9c1172b7 101 TCPSocketServer server;
najgh08 2:a2cf65b34267 102 server.bind(TCP_PORT);
najgh08 2:a2cf65b34267 103 pc.printf("TCP PORT %d \r\n", TCP_PORT);
bangbh 0:412f9c1172b7 104 server.listen();
bangbh 0:412f9c1172b7 105
bangbh 0:412f9c1172b7 106 while (1) {
bangbh 0:412f9c1172b7 107 pc.printf("\nWait for new connection...\r\n");
bangbh 0:412f9c1172b7 108 TCPSocketConnection client;
bangbh 0:412f9c1172b7 109 server.accept(client);
najgh08 2:a2cf65b34267 110 //client.set_blocking(false, 0); // Timeout=0.
bangbh 0:412f9c1172b7 111 pc.printf("Connection from: %s\r\n", client.get_address());
bangbh 0:412f9c1172b7 112 while (client.is_connected() == true) {
bangbh 0:412f9c1172b7 113 int n = client.receive(buffer, sizeof(buffer));
bangbh 0:412f9c1172b7 114 if(n > 0)
bangbh 0:412f9c1172b7 115 client.send_all(buffer, n);
bangbh 0:412f9c1172b7 116 if(client.is_fin_received())
bangbh 0:412f9c1172b7 117 client.close();
bangbh 0:412f9c1172b7 118 }
bangbh 0:412f9c1172b7 119 pc.printf("Disconnected.\r\n");
bangbh 0:412f9c1172b7 120 }
najgh08 2:a2cf65b34267 121 #endif
bangbh 0:412f9c1172b7 122 }
bangbh 0:412f9c1172b7 123 }