Skip to content

Commit 296033d

Browse files
Deomid Ryabkovcesantabot
authored andcommitted
STM32 Ethernet support
AWS MQTT connection works Also fixes #1 (because why not). PUBLISHED_FROM=7b0fa8894f2127335d075f2a97fcd37b932006b2
1 parent bbaa4e8 commit 296033d

File tree

11 files changed

+1127
-9
lines changed

11 files changed

+1127
-9
lines changed

mos.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ description: Ethernet support
33
type: lib
44
version: 1.0
55

6-
platforms: [ esp32 ]
6+
platforms: [ esp32, stm32 ]
7+
8+
sources:
9+
- src
710

811
config_schema:
912
- ["eth", "o", {title: "Ethernet settings"}]
1013
- ["eth.enable", "b", false, {title: "Enable Ethernet interface"}]
14+
- ["eth.phy_addr", "i", 0, {title: "(R)MII PHY address"}]
15+
- ["eth.ip", "s", {title: "Static IP Address"}]
16+
- ["eth.netmask", "s", {title: "Static Netmask"}]
17+
- ["eth.gw", "s", {title: "Static Default Gateway"}]
1118

1219
tags:
1320
- c

mos_esp32.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ cdefs:
77
# MGOS_ETH_PHY_TLK110: 1
88

99
config_schema:
10-
- ["eth.phy_addr", "i", 0, {title: "RMII PHY address"}]
1110
- ["eth.mdc_gpio", "i", 23, {title: "GPIO to use for RMII MDC signal"}]
1211
- ["eth.mdio_gpio", "i", 18, {title: "GPIO to use for RMII MDIO signal"}]

mos_stm32.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sources:
2+
- src/stm32
3+
4+
config_schema:
5+
# OUI = 12:34:56 happens to be a valid Locally Administered Address prefix. Win!
6+
- ["eth.mac", "s", "12:34:56:??:??:??", {title: "MAC address. ?? are replaced with bits from chip's unique ID."}]
7+
- ["eth.speed", "s", "auto", {title: "Speed and duplex selection: auto, 10HD, 10FD, 100HD, 100FD."}]
8+
- ["eth.mtu", "i", 1500, {title: "Interface MTU"}]
9+
10+
cdefs:
11+
STM32_ETH_PHY: STM32_ETH_PHY_LAN8742A

src/esp32/esp32_eth.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "eth_phy/phy_tlk110.h"
1111
#include "tcpip_adapter.h"
1212

13+
#include "lwip/ip_addr.h"
14+
15+
#include "mgos_eth.h"
1316
#include "mgos_net.h"
1417
#include "mgos_net_hal.h"
1518
#include "mgos_sys_config.h"
@@ -21,7 +24,18 @@ static void eth_config_pins(void) {
2124
}
2225

2326
bool mgos_ethernet_init(void) {
24-
if (!mgos_sys_config_get_eth_enable()) return true;
27+
bool res = false;
28+
29+
if (!mgos_sys_config_get_eth_enable()) {
30+
res = true;
31+
goto clean;
32+
}
33+
34+
tcpip_adapter_ip_info_t static_ip;
35+
if (!mgos_eth_get_static_ip_config(&static_ip.ip, &static_ip.netmask,
36+
&static_ip.gw)) {
37+
goto clean;
38+
}
2539

2640
eth_config_t config;
2741
const char *phy_model;
@@ -40,17 +54,37 @@ bool mgos_ethernet_init(void) {
4054
config.gpio_config = eth_config_pins;
4155
config.tcpip_input = tcpip_adapter_eth_input;
4256

43-
LOG(LL_INFO,
44-
("Eth init: %s PHY @ %d", phy_model, mgos_sys_config_get_eth_phy_addr()));
4557
esp_err_t ret = esp_eth_init(&config);
46-
if (ret == ESP_OK) {
47-
esp_eth_enable();
48-
} else {
58+
if (ret != ESP_OK) {
4959
LOG(LL_ERROR, ("Ethernet init failed: %d", ret));
5060
return false;
5161
}
5262

53-
return true;
63+
uint8_t mac[6];
64+
esp_eth_get_mac(mac);
65+
bool is_dhcp = ip4_addr_isany_val(static_ip.ip);
66+
67+
LOG(LL_INFO,
68+
("ETH: MAC %02x:%02x:%02x:%02x:%02x:%02x; PHY: %s @ %d%s", mac[0], mac[1],
69+
mac[2], mac[3], mac[4], mac[5], phy_model,
70+
mgos_sys_config_get_eth_phy_addr(), (is_dhcp ? "; IP: DHCP" : "")));
71+
if (!is_dhcp) {
72+
char ips[16], nms[16], gws[16];
73+
ip4addr_ntoa_r(&static_ip.ip, ips, sizeof(ips));
74+
ip4addr_ntoa_r(&static_ip.netmask, nms, sizeof(nms));
75+
ip4addr_ntoa_r(&static_ip.gw, gws, sizeof(gws));
76+
LOG(LL_INFO, ("ETH: IP %s/%s, GW %s", ips, nms, gws));
77+
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
78+
if (tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &static_ip) != ESP_OK) {
79+
LOG(LL_ERROR, ("ETH: Failed to set ip info"));
80+
goto clean;
81+
}
82+
}
83+
84+
res = (esp_eth_enable() == ESP_OK);
85+
86+
clean:
87+
return res;
5488
}
5589

5690
bool mgos_eth_dev_get_ip_info(int if_instance,

src/mgos_eth.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2014-2018 Cesanta Software Limited
3+
* All rights reserved
4+
*/
5+
6+
#include "mgos_eth.h"
7+
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
#include "lwip/ip_addr.h"
12+
13+
#include "mgos_sys_config.h"
14+
15+
const char *mgos_eth_speed_str(enum mgos_eth_speed speed) {
16+
switch (speed) {
17+
case MGOS_ETH_SPEED_AUTO:
18+
return "auto";
19+
case MGOS_ETH_SPEED_10M:
20+
return "10";
21+
case MGOS_ETH_SPEED_100M:
22+
return "100";
23+
}
24+
return "";
25+
}
26+
27+
const char *mgos_eth_duplex_str(enum mgos_eth_duplex duplex) {
28+
switch (duplex) {
29+
case MGOS_ETH_DUPLEX_AUTO:
30+
return "auto";
31+
case MGOS_ETH_DUPLEX_HALF:
32+
return "half";
33+
case MGOS_ETH_DUPLEX_FULL:
34+
return "full";
35+
}
36+
return "";
37+
}
38+
39+
bool mgos_eth_phy_opts_from_str(const char *str,
40+
struct mgos_eth_phy_opts *opts) {
41+
if (str == NULL) return false;
42+
43+
memset(opts, 0, sizeof(*opts));
44+
45+
opts->autoneg_on = (strcmp(str, "auto") == 0);
46+
47+
if (opts->autoneg_on) {
48+
return true;
49+
}
50+
51+
size_t len = strlen(str);
52+
53+
char *end = NULL;
54+
switch (strtol(str, &end, 10)) {
55+
case 10:
56+
opts->speed = MGOS_ETH_SPEED_10M;
57+
break;
58+
case 100:
59+
opts->speed = MGOS_ETH_SPEED_100M;
60+
break;
61+
default:
62+
return false;
63+
}
64+
65+
if (len - (end - str) != 2) return false;
66+
if (strcmp(end, "FD")) {
67+
opts->duplex = MGOS_ETH_DUPLEX_FULL;
68+
} else if (strcmp(end, "HD")) {
69+
opts->duplex = MGOS_ETH_DUPLEX_HALF;
70+
} else {
71+
return false;
72+
}
73+
74+
return true;
75+
}
76+
77+
bool mgos_eth_get_static_ip_config(ip4_addr_t *ip, ip4_addr_t *netmask,
78+
ip4_addr_t *gw) {
79+
bool res = false;
80+
memset(ip, 0, sizeof(*ip));
81+
memset(netmask, 0, sizeof(*netmask));
82+
memset(gw, 0, sizeof(*gw));
83+
84+
if (mgos_sys_config_get_eth_ip() == NULL) {
85+
res = true;
86+
goto clean;
87+
}
88+
89+
if (!ip4addr_aton(mgos_sys_config_get_eth_ip(), ip)) {
90+
LOG(LL_ERROR, ("Invalid eth.ip!"));
91+
goto clean;
92+
}
93+
if (mgos_sys_config_get_eth_netmask() == NULL ||
94+
!ip4addr_aton(mgos_sys_config_get_eth_netmask(), netmask)) {
95+
LOG(LL_ERROR, ("Invalid eth.netmask!"));
96+
goto clean;
97+
}
98+
if (mgos_sys_config_get_eth_gw() != NULL &&
99+
!ip4addr_aton(mgos_sys_config_get_eth_gw(), gw)) {
100+
LOG(LL_ERROR, ("Invalid eth.gw!"));
101+
goto clean;
102+
}
103+
104+
res = true;
105+
106+
clean:
107+
return res;
108+
}

src/mgos_eth.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2014-2018 Cesanta Software Limited
3+
* All rights reserved
4+
*/
5+
6+
#ifndef CS_MOS_LIBS_ETHERNET_SRC_MGOS_ETH_H_
7+
#define CS_MOS_LIBS_ETHERNET_SRC_MGOS_ETH_H_
8+
9+
#include <stdbool.h>
10+
#include <stdint.h>
11+
12+
#include "lwip/ip_addr.h"
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
enum mgos_eth_speed {
19+
MGOS_ETH_SPEED_AUTO = 0,
20+
MGOS_ETH_SPEED_10M,
21+
MGOS_ETH_SPEED_100M,
22+
};
23+
24+
enum mgos_eth_duplex {
25+
MGOS_ETH_DUPLEX_AUTO = 0,
26+
MGOS_ETH_DUPLEX_HALF,
27+
MGOS_ETH_DUPLEX_FULL,
28+
};
29+
30+
struct mgos_eth_phy_opts {
31+
enum mgos_eth_speed speed;
32+
enum mgos_eth_duplex duplex;
33+
bool autoneg_on;
34+
};
35+
36+
struct mgos_eth_opts {
37+
int phy_addr;
38+
struct mgos_eth_phy_opts phy_opts;
39+
uint8_t mac[6];
40+
int mtu;
41+
};
42+
43+
const char *mgos_eth_speed_str(enum mgos_eth_speed speed);
44+
const char *mgos_eth_duplex_str(enum mgos_eth_duplex duplex);
45+
bool mgos_eth_phy_opts_from_str(const char *str,
46+
struct mgos_eth_phy_opts *opts);
47+
bool mgos_eth_get_static_ip_config(ip4_addr_t *ip, ip4_addr_t *netmask,
48+
ip4_addr_t *gw);
49+
50+
#ifdef __cplusplus
51+
}
52+
#endif
53+
#endif /* CS_MOS_LIBS_ETHERNET_SRC_MGOS_ETH_H_ */

0 commit comments

Comments
 (0)