Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 1ce7389

Browse files
authored
Add LAN8720 support for STM32F4 and STM32F7
1 parent 61c7cb5 commit 1ce7389

File tree

18 files changed

+1982
-0
lines changed

18 files changed

+1982
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/****************************************************************************************************************************
2+
AdvancedWebServer_LAN8720.h - Dead simple web-server for Ethernet shields
3+
4+
For STM32 with built-in Ethernet LAN8742A (Nucleo-144, DISCOVERY, etc) or W5x00/ENC28J60 shield/module
5+
6+
EthernetWebServer_STM32 is a library for the STM32 running Ethernet WebServer
7+
8+
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9+
Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer_STM32
10+
Licensed under MIT license
11+
12+
Copyright (c) 2015, Majenko Technologies
13+
All rights reserved.
14+
15+
Redistribution and use in source and binary forms, with or without modification,
16+
are permitted provided that the following conditions are met:
17+
18+
Redistributions of source code must retain the above copyright notice, this
19+
list of conditions and the following disclaimer.
20+
21+
Redistributions in binary form must reproduce the above copyright notice, this
22+
list of conditions and the following disclaimer in the documentation and/or
23+
other materials provided with the distribution.
24+
25+
Neither the name of Majenko Technologies nor the names of its
26+
contributors may be used to endorse or promote products derived from
27+
this software without specific prior written permission.
28+
29+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
33+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
36+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39+
*****************************************************************************************************************************/
40+
/*
41+
Currently support
42+
1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as :
43+
- Nucleo-144 (F429ZI, F767ZI)
44+
- Discovery (STM32F746G-DISCOVERY)
45+
- STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet,
46+
- See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1)
47+
2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false)
48+
3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields
49+
*/
50+
51+
#include "defines.h"
52+
53+
EthernetWebServer server(80);
54+
55+
const int led = 13;
56+
57+
void handleRoot()
58+
{
59+
digitalWrite(led, 1);
60+
61+
#define BUFFER_SIZE 400
62+
63+
char temp[BUFFER_SIZE];
64+
int sec = millis() / 1000;
65+
int min = sec / 60;
66+
int hr = min / 60;
67+
int day = hr / 24;
68+
69+
hr = hr % 24;
70+
71+
snprintf(temp, BUFFER_SIZE - 1,
72+
"<html>\
73+
<head>\
74+
<meta http-equiv='refresh' content='5'/>\
75+
<title>AdvancedWebServer %s</title>\
76+
<style>\
77+
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
78+
</style>\
79+
</head>\
80+
<body>\
81+
<h2>Hi from EthernetWebServer!</h2>\
82+
<h3>on %s</h3>\
83+
<p>Uptime: %d d %02d:%02d:%02d</p>\
84+
<img src=\"/test.svg\" />\
85+
</body>\
86+
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
87+
88+
server.send(200, "text/html", temp);
89+
90+
digitalWrite(led, 0);
91+
}
92+
93+
void handleNotFound()
94+
{
95+
digitalWrite(led, 1);
96+
String message = "File Not Found\n\n";
97+
message += "URI: ";
98+
message += server.uri();
99+
message += "\nMethod: ";
100+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
101+
message += "\nArguments: ";
102+
message += server.args();
103+
message += "\n";
104+
105+
for (uint8_t i = 0; i < server.args(); i++)
106+
{
107+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
108+
}
109+
110+
server.send(404, "text/plain", message);
111+
digitalWrite(led, 0);
112+
}
113+
114+
void drawGraph()
115+
{
116+
String out;
117+
out.reserve(3000);
118+
char temp[70];
119+
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n";
120+
out += "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
121+
out += "<g stroke=\"black\">\n";
122+
int y = rand() % 130;
123+
124+
for (int x = 10; x < 300; x += 10)
125+
{
126+
int y2 = rand() % 130;
127+
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
128+
out += temp;
129+
y = y2;
130+
}
131+
out += "</g>\n</svg>\n";
132+
133+
server.send(200, "image/svg+xml", out);
134+
}
135+
136+
void setup(void)
137+
{
138+
pinMode(led, OUTPUT);
139+
digitalWrite(led, 0);
140+
141+
Serial.begin(115200);
142+
Serial.println("\nStart AdvancedWebServer_LAN8720 on " + String(BOARD_NAME) + ", using " + String(SHIELD_TYPE));
143+
Serial.println(ETHERNET_WEBSERVER_STM32_VERSION);
144+
145+
ET_LOGWARN3(F("Board :"), BOARD_NAME, F(", setCsPin:"), USE_THIS_SS_PIN);
146+
147+
ET_LOGWARN(F("Default SPI pinout:"));
148+
ET_LOGWARN1(F("MOSI:"), MOSI);
149+
ET_LOGWARN1(F("MISO:"), MISO);
150+
ET_LOGWARN1(F("SCK:"), SCK);
151+
ET_LOGWARN1(F("SS:"), SS);
152+
ET_LOGWARN(F("========================="));
153+
154+
#if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET)
155+
// For other boards, to change if necessary
156+
#if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
157+
// Must use library patch for Ethernet, Ethernet2, EthernetLarge libraries
158+
Ethernet.init (USE_THIS_SS_PIN);
159+
160+
#elif USE_ETHERNET3
161+
// Use MAX_SOCK_NUM = 4 for 4K, 2 for 8K, 1 for 16K RX/TX buffer
162+
#ifndef ETHERNET3_MAX_SOCK_NUM
163+
#define ETHERNET3_MAX_SOCK_NUM 4
164+
#endif
165+
166+
Ethernet.setCsPin (USE_THIS_SS_PIN);
167+
Ethernet.init (ETHERNET3_MAX_SOCK_NUM);
168+
169+
#elif USE_CUSTOM_ETHERNET
170+
// You have to add initialization for your Custom Ethernet here
171+
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
172+
//Ethernet.init(USE_THIS_SS_PIN);
173+
174+
#endif //( ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC )
175+
#endif
176+
177+
// start the ethernet connection and the server:
178+
// Use DHCP dynamic IP and random mac
179+
uint16_t index = millis() % NUMBER_OF_MAC;
180+
// Use Static IP
181+
//Ethernet.begin(mac[index], ip);
182+
Ethernet.begin(mac[index]);
183+
184+
server.on("/", handleRoot);
185+
server.on("/test.svg", drawGraph);
186+
server.on("/inline", []()
187+
{
188+
server.send(200, "text/plain", "This works as well");
189+
});
190+
191+
server.onNotFound(handleNotFound);
192+
server.begin();
193+
194+
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
195+
Serial.println(Ethernet.localIP());
196+
}
197+
198+
void loop(void)
199+
{
200+
server.handleClient();
201+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/****************************************************************************************************************************
2+
defines.h
3+
4+
For STM32 with built-in Ethernet LAN8742A (Nucleo-144, DISCOVERY, etc) or W5x00/ENC28J60 shield/module
5+
6+
EthernetWebServer_STM32 is a library for the STM32 running Ethernet WebServer
7+
8+
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
9+
Built by Khoi Hoang https://github.com/khoih-prog/EthernetWebServer_STM32
10+
Licensed under MIT license
11+
***************************************************************************************************************************************/
12+
13+
#ifndef defines_h
14+
#define defines_h
15+
16+
#if !( defined(STM32F4) || defined(STM32F7) )
17+
#error This code is designed to run on STM32F4 and STM32F7 platform! Please check your Tools->Board setting.
18+
#endif
19+
20+
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
21+
22+
// Debug Level from 0 to 4
23+
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 2
24+
25+
#define USING_LAN8720 true
26+
27+
// If USE_BUILTIN_ETHERNET == false and USE_UIP_ETHERNET == false =>
28+
// either use W5x00 with EthernetXYZ library
29+
// or ENC28J60 with EthernetENC library
30+
#define USE_BUILTIN_ETHERNET true
31+
//#define USE_BUILTIN_ETHERNET false
32+
33+
//#define USE_UIP_ETHERNET true
34+
#define USE_UIP_ETHERNET false
35+
36+
// To override the default CS/SS pin. Don't use unless you know exactly which pin to use
37+
// You can define here or customize for each board at same place with BOARD_TYPE
38+
//#define USE_THIS_SS_PIN 22 //21 //5 //4 //2 //15
39+
// Default pin 10 to SS/CS. To change according to your board, if necessary
40+
#define USE_THIS_SS_PIN 10
41+
42+
#if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET)
43+
// Only one if the following to be true
44+
#define USE_ETHERNET false //true
45+
#define USE_ETHERNET2 false //true
46+
#define USE_ETHERNET3 false //true
47+
#define USE_ETHERNET_LARGE false
48+
#define USE_ETHERNET_ESP8266 false //true
49+
#define USE_ETHERNET_ENC true
50+
#define USE_CUSTOM_ETHERNET false
51+
#endif
52+
53+
#if ( USE_ETHERNET2 || USE_ETHERNET3 || USE_ETHERNET_LARGE || USE_ETHERNET_ESP8266 || USE_ETHERNET_ENC )
54+
#ifdef USE_CUSTOM_ETHERNET
55+
#undef USE_CUSTOM_ETHERNET
56+
#endif
57+
#define USE_CUSTOM_ETHERNET false //true
58+
#endif
59+
60+
#if (USE_BUILTIN_ETHERNET)
61+
#if USING_LAN8720
62+
#warning Using LAN8720A Ethernet & STM32Ethernet lib
63+
#define SHIELD_TYPE "LAN8720 Ethernet & STM32Ethernet Library"
64+
#else
65+
#warning Using LAN8742A Ethernet & STM32Ethernet lib
66+
#define SHIELD_TYPE "LAN8742A Ethernet & STM32Ethernet Library"
67+
#endif
68+
#elif (USE_UIP_ETHERNET)
69+
#warning Using ENC28J60 & UIPEthernet lib
70+
#define SHIELD_TYPE "ENC28J60 & UIPEthernet Library"
71+
#elif USE_ETHERNET3
72+
#include "Ethernet3.h"
73+
#warning Using W5x00 & Ethernet3 lib
74+
#define SHIELD_TYPE "W5x00 & Ethernet3 Library"
75+
#elif USE_ETHERNET2
76+
#include "Ethernet2.h"
77+
#warning Using W5x00 & Ethernet2 lib
78+
#define SHIELD_TYPE "W5x00 & Ethernet2 Library"
79+
#elif USE_ETHERNET_LARGE
80+
#include "EthernetLarge.h"
81+
#warning Using W5x00 & EthernetLarge lib
82+
#define SHIELD_TYPE "W5x00 & EthernetLarge Library"
83+
#elif USE_ETHERNET_ESP8266
84+
#include "Ethernet_ESP8266.h"
85+
#warning Using W5x00 & Ethernet_ESP8266 lib
86+
#define SHIELD_TYPE "W5x00 & Ethernet_ESP8266 Library"
87+
#elif USE_ETHERNET_ENC
88+
#include "EthernetENC.h"
89+
#warning Using ENC28J60 & EthernetENC lib
90+
#define SHIELD_TYPE "ENC28J60 & EthernetENC Library"
91+
#elif USE_CUSTOM_ETHERNET
92+
//#include "Ethernet_XYZ.h"
93+
#include "EthernetENC.h"
94+
#warning Using Custom Ethernet library. You must include a library and initialize.
95+
#define SHIELD_TYPE "Custom Ethernet & Ethernet_XYZ Library"
96+
#else
97+
#define USE_ETHERNET true
98+
#include "Ethernet.h"
99+
#warning Using Ethernet lib
100+
#define SHIELD_TYPE "W5x00 & Ethernet Library"
101+
#endif
102+
103+
#if defined(STM32F0)
104+
#warning STM32F0 board selected
105+
#define BOARD_TYPE "STM32F0"
106+
#elif defined(STM32F1)
107+
#warning STM32F1 board selected
108+
#define BOARD_TYPE "STM32F1"
109+
#elif defined(STM32F2)
110+
#warning STM32F2 board selected
111+
#define BOARD_TYPE "STM32F2"
112+
#elif defined(STM32F3)
113+
#warning STM32F3 board selected
114+
#define BOARD_TYPE "STM32F3"
115+
#elif defined(STM32F4)
116+
#warning STM32F4 board selected
117+
#define BOARD_TYPE "STM32F4"
118+
#elif defined(STM32F7)
119+
#warning STM32F7 board selected
120+
#define BOARD_TYPE "STM32F7"
121+
#elif defined(STM32L0)
122+
#warning STM32L0 board selected
123+
#define BOARD_TYPE "STM32L0"
124+
#elif defined(STM32L1)
125+
#warning STM32L1 board selected
126+
#define BOARD_TYPE "STM32L1"
127+
#elif defined(STM32L4)
128+
#warning STM32L4 board selected
129+
#define BOARD_TYPE "STM32L4"
130+
#elif defined(STM32H7)
131+
#warning STM32H7 board selected
132+
#define BOARD_TYPE "STM32H7"
133+
#elif defined(STM32G0)
134+
#warning STM32G0 board selected
135+
#define BOARD_TYPE "STM32G0"
136+
#elif defined(STM32G4)
137+
#warning STM32G4 board selected
138+
#define BOARD_TYPE "STM32G4"
139+
#elif defined(STM32WB)
140+
#warning STM32WB board selected
141+
#define BOARD_TYPE "STM32WB"
142+
#elif defined(STM32MP1)
143+
#warning STM32MP1 board selected
144+
#define BOARD_TYPE "STM32MP1"
145+
#else
146+
#warning STM32 unknown board selected
147+
#define BOARD_TYPE "STM32 Unknown"
148+
#endif
149+
150+
#ifndef BOARD_NAME
151+
#define BOARD_NAME BOARD_TYPE
152+
#endif
153+
154+
#include <EthernetWebServer_STM32.h>
155+
156+
// Enter a MAC address and IP address for your controller below.
157+
#define NUMBER_OF_MAC 20
158+
159+
byte mac[][NUMBER_OF_MAC] =
160+
{
161+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 },
162+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 },
163+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 },
164+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 },
165+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 },
166+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 },
167+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 },
168+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 },
169+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 },
170+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A },
171+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B },
172+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C },
173+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D },
174+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E },
175+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F },
176+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 },
177+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 },
178+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 },
179+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 },
180+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 },
181+
};
182+
// Select the IP address according to your local network
183+
IPAddress ip(192, 168, 2, 232);
184+
185+
#endif //defines_h
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#define HAL_ETH_MODULE_ENABLED
2+
3+
#if LAN8742A_PHY_ADDRESS
4+
#undef LAN8742A_PHY_ADDRESS
5+
#define LAN8742A_PHY_ADDRESS 0x01U
6+
#endif

0 commit comments

Comments
 (0)