Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem WIZnet_Library mbed
Fork of Nucleo_L152RE_W5500HelloWorld_ver2 by
main.cpp
- Committer:
- najgh08
- Date:
- 2016-01-18
- Revision:
- 3:fa8925fb003a
- Parent:
- 2:a2cf65b34267
- Child:
- 4:17797698535b
File content as of revision 3:fa8925fb003a:
// Nucloe_L152RE_W5500_Ethernet shield Exmaple
#include "mbed.h"
#include "WIZnetInterface.h"
#include "SDFileSystem.h"
#include "UDPSocket.h"
//#define USE_DHCP 1 // DHCP 사용할때 사용
#define TCP_PORT 5000
#define UDP_PORT 7000
const char * IP_Addr = "192.168.0.194";
const char * IP_Subnet = "255.255.255.0";
const char * IP_Gateway = "192.168.0.1";
unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x01,0x02,0x03};
const char * UDP_IP = "0.0.0.0";
SDFileSystem sd(PA_7, PA_6, PA_5, PB_5, "sd");
DigitalOut myled1(LED1);
Serial pc(USBTX, USBRX);
SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
WIZnetInterface ethernet(&spi, PB_6, PA_9);//scs(PB_6), nRESET(PA_9); // reset pin is dummy, don't affect any pin of WIZ550io
int main() {
//Set serial port baudrate speed: 115200
pc.baud(115200);
wait(10);
pc.printf("W5500 Application Started \r\n");
char buffer[128];
//SD-CARD
printf("Hello World!\n");
mkdir("/sd/mydir", 0777);
FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
if(fp == NULL) {
error("Could not open file for write\n");
}
fprintf(fp, "Hello fun SD Card World!");
fclose(fp);
printf("Goodbye World!\r\n");
//SD-CARD END
while(1)
{
#if USE_DHCP
pc.printf("DHCP ON \r\n");
int ret = ethernet.init(MAC_Addr);
#else
pc.printf("DHCP OFF \r\n");
int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
#endif
printf("SPI Initialized \r\n");
wait(1); // 1 second for stable state
printf("W5500 Networking Started \r\n");
wait(1); // 1 second for stable state
if (!ret) {
pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
ret = ethernet.connect();
if (!ret) {
pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
} else {
pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
exit(0);
}
} else {
pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
exit(0);
}
TCPSocketServer TCP_server; // TCP Socket open
UDPSocket UDP_server; // UDP Socket open
UDP_server.init();
Endpoint nist;
nist.set_address(UDP_IP, UDP_PORT);
UDP_server.bind(UDP_PORT);
pc.printf("UDP PORT %d \r\n", UDP_PORT);
TCP_server.bind(TCP_PORT);
pc.printf("TCP PORT %d \r\n", TCP_PORT);
TCP_server.listen();
pc.printf("server %s on port %d: \r\n", nist.get_address(), nist.get_port());
int m = UDP_server.receiveFrom(nist, buffer, sizeof(buffer));
if(m > 0){
UDP_server.sendTo(nist, buffer, m);
pc.printf("UDP Data Send OK\r\n");
}
pc.printf("UDP Close\r\n");
while (1) {
pc.printf("\nWait for TCP connection...\r\n");
TCPSocketConnection TCP_client;
if(TCP_server.accept(TCP_client) == 0)
{
pc.printf("Connection from: %s\r\n", TCP_client.get_address());
while (TCP_client.is_connected() == true) {
int n = TCP_client.receive(buffer, sizeof(buffer));
if(n > 0)
TCP_client.send_all(buffer, n);
if(TCP_client.is_fin_received()){
TCP_client.close();
}
}
}
pc.printf("Disconnected.\r\n");
}
}
}
