Skip to content

Commit edefcec

Browse files
committed
eth0 & wlan0 ping ok
1 parent dfd97bc commit edefcec

File tree

3 files changed

+103
-12
lines changed

3 files changed

+103
-12
lines changed

‎project/apps/he_app/eth.cpp‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <zephyr/kernel.h>
2+
#include <zephyr/logging/log.h>
3+
#include <zephyr/net/net_if.h>
4+
#include <zephyr/net/net_core.h>
5+
#include <zephyr/net/net_mgmt.h>
6+
#include <zephyr/net/ethernet.h>
7+
8+
LOG_MODULE_REGISTER(eth, LOG_LEVEL_INF);
9+
10+
#define ETH_STATIC_IP "169.254.0.1"
11+
#define ETH_NETMASK "255.255.0.0"
12+
13+
static struct net_if *eth_iface;
14+
static struct net_mgmt_event_callback eth_cb;
15+
static bool eth_configured;
16+
17+
static void eth_event_handler(struct net_mgmt_event_callback *cb,
18+
uint32_t mgmt_event,
19+
struct net_if *iface)
20+
{
21+
if (iface != eth_iface) {
22+
return;
23+
}
24+
25+
if (mgmt_event == NET_EVENT_IF_UP) {
26+
LOG_INF("Ethernet interface up");
27+
} else if (mgmt_event == NET_EVENT_IF_DOWN) {
28+
LOG_WRN("Ethernet interface down");
29+
}
30+
}
31+
32+
extern "C" auto eth_init() -> int
33+
{
34+
struct in_addr addr;
35+
struct in_addr netmask;
36+
struct net_if_addr *ifaddr;
37+
38+
eth_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(ETHERNET));
39+
if (eth_iface == nullptr) {
40+
LOG_ERR("No Ethernet interface found");
41+
return -ENODEV;
42+
}
43+
44+
LOG_INF("Ethernet interface found: %s", net_if_get_device(eth_iface)->name);
45+
46+
net_mgmt_init_event_callback(&eth_cb, eth_event_handler,
47+
NET_EVENT_IF_UP | NET_EVENT_IF_DOWN);
48+
net_mgmt_add_event_callback(&eth_cb);
49+
50+
if (net_addr_pton(AF_INET, ETH_STATIC_IP, &addr) < 0) {
51+
LOG_ERR("Invalid IP address: %s", ETH_STATIC_IP);
52+
return -EINVAL;
53+
}
54+
55+
if (net_addr_pton(AF_INET, ETH_NETMASK, &netmask) < 0) {
56+
LOG_ERR("Invalid netmask: %s", ETH_NETMASK);
57+
return -EINVAL;
58+
}
59+
60+
ifaddr = net_if_ipv4_addr_add(eth_iface, &addr, NET_ADDR_MANUAL, 0);
61+
if (ifaddr == nullptr) {
62+
LOG_ERR("Failed to add IP address");
63+
return -ENOMEM;
64+
}
65+
66+
net_if_ipv4_set_netmask_by_addr(eth_iface, &addr, &netmask);
67+
68+
eth_configured = true;
69+
LOG_INF("Ethernet configured with IP %s/%s", ETH_STATIC_IP, ETH_NETMASK);
70+
71+
return 0;
72+
}
73+
74+
extern "C" auto eth_is_configured() -> bool
75+
{
76+
return eth_configured;
77+
}

‎project/apps/he_app/main.cpp‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,28 @@ extern "C"
1313

1414
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(GREEN_LED_NODE, gpios);
1515

16-
LOG_MODULE_REGISTER(app);
17-
1816
auto main() -> int
1917
{
2018
int ret;
2119

2220
LOG_INF("HE App starting...");
2321

24-
// Initialize WiFi and start background connection task
25-
// ret = wlan_init();
26-
// if (ret < 0) {
27-
// LOG_ERR("WiFi init failed: %d", ret);
28-
// } else {
29-
// ret = wlan_start();
30-
// if (ret < 0) {
31-
// LOG_ERR("WiFi background task start failed: %d", ret);
32-
// }
33-
// }
22+
// Initialize Ethernet with static IP (169.254.0.2)
23+
ret = eth_init();
24+
if (ret < 0) {
25+
LOG_ERR("Ethernet init failed: %d", ret);
26+
}
27+
28+
// Initialize and connect WiFi with DHCP
29+
ret = wlan_init();
30+
if (ret < 0) {
31+
LOG_ERR("WiFi init failed: %d", ret);
32+
} else {
33+
ret = wlan_connect();
34+
if (ret < 0) {
35+
LOG_ERR("WiFi connect failed: %d", ret);
36+
}
37+
}
3438

3539
// LED setup
3640
if (!gpio_is_ready_dt(&led))
@@ -46,6 +50,8 @@ auto main() -> int
4650
return 0;
4751
}
4852

53+
LOG_INF("Entering main loop");
54+
4955
while (true)
5056
{
5157
ret = gpio_pin_toggle_dt(&led);

‎project/apps/he_app/wlan.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ static void wlan_thread_entry(void *p1, void *p2, void *p3);
3434
K_THREAD_STACK_DEFINE(wlan_thread_stack, WLAN_THREAD_STACK_SIZE);
3535
static struct k_thread wlan_thread_data;
3636

37+
static bool wifi_connected;
38+
static bool dhcp_bound;
39+
3740
static void wifi_event_handler(struct net_mgmt_event_callback *cb,
3841
uint32_t mgmt_event,
3942
struct net_if *iface)
@@ -164,6 +167,11 @@ extern "C" auto wlan_connect() -> int
164167
};
165168

166169
LOG_INF("Connecting to WiFi: %s (WPA2-PSK)", WIFI_SSID);
170+
.security = WIFI_SECURITY_TYPE_PSK_SHA256, // -k 2
171+
.mfp = WIFI_MFP_OPTIONAL,
172+
};
173+
174+
LOG_INF("Connecting to WiFi: %s", WIFI_SSID);
167175

168176
while (retries-- > 0) {
169177
ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, wifi_iface,

0 commit comments

Comments
 (0)