Skip to content

Commit aeaea1b

Browse files
committed
add dhcp support, compile and test pass
1 parent 4f01577 commit aeaea1b

22 files changed

Lines changed: 476 additions & 314 deletions

example/offchip/Ethernet/stm32f103c8t6/RTE/RTE_Components.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define RTE_DEVICE_STDPERIPH_EXTI
2121
#define RTE_DEVICE_STDPERIPH_FRAMEWORK
2222
#define RTE_DEVICE_STDPERIPH_GPIO
23+
#define RTE_DEVICE_STDPERIPH_I2C
2324
#define RTE_DEVICE_STDPERIPH_IWDG
2425
#define RTE_DEVICE_STDPERIPH_RCC
2526
#define RTE_DEVICE_STDPERIPH_SPI

example/offchip/Ethernet/stm32f103c8t6/app/main.cpp

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,130 @@
22
#include "socket.h"
33
#include "TaskManager.h"
44
#include "w5500.h"
5+
#include "Dhcp.h"
6+
#include "IPAddress.h"
57

68
USART log(1,115200,false,0,3,7,1,3);//
79
uint8_t dataSend[9]={"abcdefg\n"};
810
u8 hexData[5]={0x12,0x56,0x9A,0xAB,0xEF};
911
u8 dataReceived[50];
1012

1113
uint8_t sock = 0;
12-
uint8_t mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE};
14+
uint8_t mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF};
1315
uint8_t local_ip[] = {192,168,31,236};
1416
uint8_t gateway_ip[] = {192,168,31,1};
15-
uint8_t subnetMask[] = {255,255,255,0};
17+
uint8_t configSubnetMask[] = {255,255,255,0};
1618

1719
uint16_t local_port = 5000;
1820
uint8_t server_ip[4] = {192,168,31,86};
1921
uint16_t server_port = 6000;
2022
bool flag = false;
2123

2224
uint8_t temp[6];
25+
/*
2326
int main()
2427
{
2528
log<<"system start, initilize now\n";
2629
w5500.init();
2730
w5500.setMACAddress(mac);
2831
w5500.setIPAddress(local_ip);
2932
w5500.setGatewayIp(gateway_ip);
30-
w5500.setSubnetMask(subnetMask);
33+
w5500.setSubnetMask(configSubnetMask);
34+
log<<"init complete\n";
35+
36+
w5500.getMACAddress(temp);
37+
log<<"mac:";
38+
log.PrintHex(temp,6,":")<<"\n";
39+
40+
w5500.getGatewayIp(temp);
41+
log<<"gateway ip:";
42+
log.PrintHex(temp,4,".")<<"\n";
43+
44+
w5500.getIPAddress(temp);
45+
log<<"local ip:";
46+
log.PrintHex(temp,4,".")<<"\n";
47+
48+
uint8_t ret = socket(sock,SnMR::TCP,local_port,0);
49+
if(ret == 0)
50+
{
51+
log<<"socket fail\n";
52+
}
53+
else
54+
{
55+
log<<"socket success\n";
56+
flag = true;
57+
}
58+
if(flag)
59+
{
60+
ret = connect(sock,(uint8_t*)server_ip,server_port);
61+
if(ret)
62+
{
63+
log<<"connect success\n";
64+
flag =true;
65+
while (socketStatus(sock) != SnSR::ESTABLISHED) {
66+
TaskManager::DelayMs(1);
67+
if (socketStatus(sock) == SnSR::CLOSED) {
68+
flag = false;
69+
break;
70+
}
71+
}
72+
73+
}
74+
else
75+
{
76+
log<<"connect fail\n";
77+
flag = false;
78+
}
79+
80+
}
81+
if(flag)
82+
{
83+
log<<"connect server success\n";
84+
}
85+
else
86+
{
87+
log<<"connect server fail\n";
88+
}
89+
while(1)
90+
{
91+
if(flag)
92+
{
93+
send(sock,(uint8_t*)dataSend,sizeof(dataSend));
94+
95+
}
96+
TaskManager::DelayS(2);
97+
}
98+
99+
}
100+
101+
*/
102+
103+
DhcpClass dhcp;
104+
IPAddress dnsServerAddress;
105+
106+
int main()
107+
{
108+
log<<"system start, initilize now\n";
109+
w5500.init();
110+
w5500.setMACAddress(mac);
111+
//get local ip by dhcp
112+
int result = dhcp.beginWithDHCP(mac);
113+
if(result == 1)
114+
{
115+
log<<"dhcp success\n";
116+
w5500.setIPAddress(dhcp.getLocalIp().raw_address());
117+
w5500.setGatewayIp(dhcp.getGatewayIp().raw_address());
118+
w5500.setSubnetMask(dhcp.getSubnetMask().raw_address());
119+
dnsServerAddress = dhcp.getDnsServerIp();
120+
121+
}
122+
else
123+
{
124+
log<<"dhcp fail!\n";
125+
w5500.setIPAddress(local_ip);
126+
w5500.setGatewayIp(gateway_ip);
127+
w5500.setSubnetMask(configSubnetMask);
128+
}
31129
log<<"init complete\n";
32130

33131
w5500.getMACAddress(temp);

example/offchip/Ethernet/stm32f103c8t6/demo.uvprojx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@
332332
<MiscControls></MiscControls>
333333
<Define>STM32F10X_MD,HARDWARE_USE_STM32F103DRIVERLIB</Define>
334334
<Undefine></Undefine>
335-
<IncludePath>..\..\..\..\lib\onchip\USART;..\..\..\..\lib\onchip\GPIO;..\..\..\..\lib\math;..\..\..\..\lib\onchip\interrupt;.\app;..\..\..\..\lib\onchip\taskManager;..\..\..\..\lib\onchip\SPI;..\..\..\..\lib\onchip\IWDG;..\..\..\..\lib\onchip\capture;..\..\..\..\lib\offchip\Ethernet\src;..\..\..\..\lib\offchip\LED;..\..\..\..\lib\offchip\Ethernet\src\utility</IncludePath>
335+
<IncludePath>..\..\..\..\lib\onchip\USART;..\..\..\..\lib\onchip\GPIO;..\..\..\..\lib\math;..\..\..\..\lib\onchip\interrupt;.\app;..\..\..\..\lib\onchip\taskManager;..\..\..\..\lib\onchip\SPI;..\..\..\..\lib\onchip\IWDG;..\..\..\..\lib\onchip\capture;..\..\..\..\lib\offchip\Ethernet\src;..\..\..\..\lib\offchip\LED;..\..\..\..\lib\offchip\Ethernet\src\utility;..\..\..\..\lib\onchip\delay</IncludePath>
336336
</VariousControls>
337337
</Cads>
338338
<Aads>
@@ -415,6 +415,16 @@
415415
<FileType>8</FileType>
416416
<FilePath>..\..\..\..\lib\offchip\LED\LED.cpp</FilePath>
417417
</File>
418+
<File>
419+
<FileName>I2C.cpp</FileName>
420+
<FileType>8</FileType>
421+
<FilePath>..\..\..\..\lib\onchip\I2C\I2C.cpp</FilePath>
422+
</File>
423+
<File>
424+
<FileName>Delay.cpp</FileName>
425+
<FileType>8</FileType>
426+
<FilePath>..\..\..\..\lib\onchip\delay\Delay.cpp</FilePath>
427+
</File>
418428
</Files>
419429
</Group>
420430
<Group>
@@ -450,6 +460,31 @@
450460
<FileType>8</FileType>
451461
<FilePath>..\..\..\..\lib\offchip\Ethernet\src\utility\socket.cpp</FilePath>
452462
</File>
463+
<File>
464+
<FileName>IPAddress.cpp</FileName>
465+
<FileType>8</FileType>
466+
<FilePath>..\..\..\..\lib\offchip\Ethernet\src\utility\IPAddress.cpp</FilePath>
467+
</File>
468+
<File>
469+
<FileName>EthernetUdp.cpp</FileName>
470+
<FileType>8</FileType>
471+
<FilePath>..\..\..\..\lib\offchip\Ethernet\src\EthernetUdp.cpp</FilePath>
472+
</File>
473+
<File>
474+
<FileName>Dhcp.cpp</FileName>
475+
<FileType>8</FileType>
476+
<FilePath>..\..\..\..\lib\offchip\Ethernet\src\Dhcp.cpp</FilePath>
477+
</File>
478+
<File>
479+
<FileName>Dns.cpp</FileName>
480+
<FileType>8</FileType>
481+
<FilePath>..\..\..\..\lib\offchip\Ethernet\src\Dns.cpp</FilePath>
482+
</File>
483+
<File>
484+
<FileName>Ethernet_STM.cpp</FileName>
485+
<FileType>8</FileType>
486+
<FilePath>..\..\..\..\lib\offchip\Ethernet\src\Ethernet_STM.cpp</FilePath>
487+
</File>
453488
</Files>
454489
</Group>
455490
<Group>
@@ -507,6 +542,12 @@
507542
<targetInfo name="demo"/>
508543
</targetInfos>
509544
</component>
545+
<component Cclass="Device" Cgroup="StdPeriph Drivers" Csub="I2C" Cvendor="Keil" Cversion="3.5.0" condition="STM32F1xx STDPERIPH RCC">
546+
<package name="STM32F1xx_DFP" schemaVersion="1.4.0" url="http://www.keil.com/pack/" vendor="Keil" version="2.2.0"/>
547+
<targetInfos>
548+
<targetInfo name="demo"/>
549+
</targetInfos>
550+
</component>
510551
<component Cclass="Device" Cgroup="StdPeriph Drivers" Csub="IWDG" Cvendor="Keil" Cversion="3.5.0" condition="STM32F1xx STDPERIPH">
511552
<package name="STM32F1xx_DFP" schemaVersion="1.4.0" url="http://www.keil.com/pack/" vendor="Keil" version="2.2.0"/>
512553
<targetInfos>

lib/offchip/Ethernet/src/Client.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Client.h - Base class that provides Client
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef client_h
21+
#define client_h
22+
// #include "Print.h"
23+
#include "Stream.h"
24+
#include "IPAddress.h"
25+
26+
class Client : public Stream {
27+
28+
public:
29+
virtual int connect(IPAddress ip, uint16_t port) =0;
30+
virtual int connect(const char *host, uint16_t port) =0;
31+
virtual size_t write(uint8_t) =0;
32+
virtual size_t write(const uint8_t *buf, size_t size) =0;
33+
virtual int available() = 0;
34+
virtual int read() = 0;
35+
virtual int read(uint8_t *buf, size_t size) = 0;
36+
virtual int peek() = 0;
37+
virtual void flush() = 0;
38+
virtual void stop() = 0;
39+
virtual uint8_t connected() = 0;
40+
virtual operator bool() = 0;
41+
protected:
42+
uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
43+
};
44+
45+
#endif

lib/offchip/Ethernet/src/Dhcp.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// DHCP Library v0.3 - April 25, 2009
22
// Author: Jordan Terrell - blog.jordanterrell.com
33

4-
#include "utility/w5100.h"
4+
#include "utility/w5500.h"
55

66
#include <string.h>
77
#include <stdlib.h>
88
#include "Dhcp.h"
9-
#include "Arduino.h"
109
#include "utility/util.h"
10+
#include "math.h"
1111

1212
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
1313
{
@@ -166,20 +166,20 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
166166
// siaddr: already zeroed
167167
// giaddr: already zeroed
168168

169-
//put data in W5100 transmit buffer
169+
//put data in w5500 transmit buffer
170170
_dhcpUdpSocket.write(buffer, 28);
171171

172172
memset(buffer, 0, 32); // clear local buffer
173173

174174
memcpy(buffer, _dhcpMacAddr, 6); // chaddr
175175

176-
//put data in W5100 transmit buffer
176+
//put data in w5500 transmit buffer
177177
_dhcpUdpSocket.write(buffer, 16);
178178

179179
memset(buffer, 0, 32); // clear local buffer
180180

181181
// leave zeroed out for sname && file
182-
// put in W5100 transmit buffer x 6 (192 bytes)
182+
// put in w5500 transmit buffer x 6 (192 bytes)
183183

184184
for(int i = 0; i < 6; i++) {
185185
_dhcpUdpSocket.write(buffer, 32);
@@ -211,7 +211,7 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
211211
printByte((char*)&(buffer[26]), _dhcpMacAddr[4]);
212212
printByte((char*)&(buffer[28]), _dhcpMacAddr[5]);
213213

214-
//put data in W5100 transmit buffer
214+
//put data in w5500 transmit buffer
215215
_dhcpUdpSocket.write(buffer, 30);
216216

217217
if(messageType == DHCP_REQUEST)
@@ -230,7 +230,7 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
230230
buffer[10] = _dhcpDhcpServerIp[2];
231231
buffer[11] = _dhcpDhcpServerIp[3];
232232

233-
//put data in W5100 transmit buffer
233+
//put data in w5500 transmit buffer
234234
_dhcpUdpSocket.write(buffer, 12);
235235
}
236236

@@ -244,7 +244,7 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
244244
buffer[7] = dhcpT2value;
245245
buffer[8] = endOption;
246246

247-
//put data in W5100 transmit buffer
247+
//put data in w5500 transmit buffer
248248
_dhcpUdpSocket.write(buffer, 9);
249249

250250
_dhcpUdpSocket.endPacket();

lib/offchip/Ethernet/src/Dns.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// (c) Copyright 2009-2010 MCQN Ltd.
33
// Released under Apache License, version 2.0
44

5-
#include "utility/w5100.h"
5+
#include "utility/w5500.h"
66
#include "EthernetUdp.h"
77
#include "utility/util.h"
88

99
#include "Dns.h"
1010
#include <string.h>
11-
//#include <stdlib.h>
12-
#include "Arduino.h"
11+
#include <stdlib.h>
12+
// #include "Arduino.h"
1313

1414

1515
#define SOCKET_NONE 255

lib/offchip/Ethernet/src/Dns.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define DNSClient_h
77

88
#include <EthernetUdp.h>
9+
#include "util.h"
910

1011
class DNSClient
1112
{

lib/offchip/Ethernet/src/EthernetClient.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "utility/w5100.h"
1+
#include "utility/w5500.h"
22
#include "utility/socket.h"
33

44
extern "C" {
55
#include "string.h"
66
}
77

8-
#include "Arduino.h"
8+
// #include "Arduino.h"
99

1010
#include "Ethernet_STM.h"
1111
#include "EthernetClient.h"
@@ -40,7 +40,7 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) {
4040
return 0;
4141

4242
for (int i = 0; i < MAX_SOCK_NUM; i++) {
43-
uint8_t s = W5100.readSnSR(i);
43+
uint8_t s = w5500.readSnSR(i);
4444
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
4545
_sock = i;
4646
break;
@@ -88,7 +88,7 @@ size_t EthernetClient::write(const uint8_t *buf, size_t size) {
8888

8989
int EthernetClient::available() {
9090
if (_sock != MAX_SOCK_NUM)
91-
return W5100.getRXReceivedSize(_sock);
91+
return w5500.getRXReceivedSize(_sock);
9292
return 0;
9393
}
9494

@@ -153,7 +153,7 @@ uint8_t EthernetClient::connected() {
153153

154154
uint8_t EthernetClient::status() {
155155
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
156-
return W5100.readSnSR(_sock);
156+
return w5500.readSnSR(_sock);
157157
}
158158

159159
// the next function allows us to use the client returned by

0 commit comments

Comments
 (0)