Skip to content

Commit c3bca7d

Browse files
committed
wip
1 parent 79e962e commit c3bca7d

File tree

4 files changed

+117
-25
lines changed

4 files changed

+117
-25
lines changed

‎project/apps/he_app/boards/alif_e7_dk_ae722f80f55d5xx_rtss_he.conf‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ CONFIG_NET_RX_STACK_SIZE=4096
4242
CONFIG_NET_MGMT_EVENT_STACK_SIZE=2048
4343
CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=16
4444
CONFIG_NET_IF_MAX_IPV4_COUNT=2
45+
CONFIG_NET_SHELL=y
46+
CONFIG_NET_STATISTICS=y
47+
4548
# GPIO (required for WiFi control pins)
4649
CONFIG_GPIO=y
4750

‎project/apps/he_app/eth.cpp‎

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,23 @@
77

88
LOG_MODULE_REGISTER(eth, LOG_LEVEL_INF);
99

10-
#define ETH_STATIC_IP "169.254.0.1"
11-
#define ETH_NETMASK "255.255.0.0"
10+
#define ETH_STATIC_IP "169.254.0.1"
11+
#define ETH_NETMASK "255.255.0.0"
12+
#define ETH_THREAD_STACK_SIZE 1024
13+
#define ETH_THREAD_PRIORITY 7
14+
#define ETH_RETRY_DELAY_MS 5000
1215

1316
static struct net_if *eth_iface;
1417
static struct net_mgmt_event_callback eth_cb;
1518
static bool eth_configured;
19+
static bool eth_task_running;
20+
21+
static K_SEM_DEFINE(eth_reconfigure_sem, 0, 1);
22+
23+
static void eth_thread_entry(void *p1, void *p2, void *p3);
24+
25+
K_THREAD_STACK_DEFINE(eth_thread_stack, ETH_THREAD_STACK_SIZE);
26+
static struct k_thread eth_thread_data;
1627

1728
static void eth_event_handler(struct net_mgmt_event_callback *cb,
1829
uint32_t mgmt_event,
@@ -24,29 +35,25 @@ static void eth_event_handler(struct net_mgmt_event_callback *cb,
2435

2536
if (mgmt_event == NET_EVENT_IF_UP) {
2637
LOG_INF("Ethernet interface up");
38+
if (eth_task_running && !eth_configured) {
39+
k_sem_give(&eth_reconfigure_sem);
40+
}
2741
} else if (mgmt_event == NET_EVENT_IF_DOWN) {
2842
LOG_WRN("Ethernet interface down");
43+
eth_configured = false;
2944
}
3045
}
3146

32-
extern "C" auto eth_init() -> int
47+
static auto eth_configure() -> int
3348
{
3449
struct in_addr addr;
3550
struct in_addr netmask;
3651
struct net_if_addr *ifaddr;
3752

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;
53+
if (eth_configured) {
54+
return 0;
4255
}
4356

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-
5057
if (net_addr_pton(AF_INET, ETH_STATIC_IP, &addr) < 0) {
5158
LOG_ERR("Invalid IP address: %s", ETH_STATIC_IP);
5259
return -EINVAL;
@@ -71,7 +78,84 @@ extern "C" auto eth_init() -> int
7178
return 0;
7279
}
7380

81+
extern "C" auto eth_init() -> int
82+
{
83+
eth_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(ETHERNET));
84+
if (eth_iface == nullptr) {
85+
LOG_ERR("No Ethernet interface found");
86+
return -ENODEV;
87+
}
88+
89+
LOG_INF("Ethernet interface found: %s", net_if_get_device(eth_iface)->name);
90+
91+
net_mgmt_init_event_callback(&eth_cb, eth_event_handler,
92+
NET_EVENT_IF_UP | NET_EVENT_IF_DOWN);
93+
net_mgmt_add_event_callback(&eth_cb);
94+
95+
return 0;
96+
}
97+
7498
extern "C" auto eth_is_configured() -> bool
7599
{
76100
return eth_configured;
77101
}
102+
103+
static void eth_thread_entry(void *p1, void *p2, void *p3)
104+
{
105+
ARG_UNUSED(p1);
106+
ARG_UNUSED(p2);
107+
ARG_UNUSED(p3);
108+
109+
LOG_INF("ETH background task started");
110+
111+
while (eth_task_running) {
112+
if (!eth_configured) {
113+
LOG_INF("Configuring Ethernet interface...");
114+
int ret = eth_configure();
115+
if (ret != 0) {
116+
LOG_WRN("Configuration failed, retry in %d ms", ETH_RETRY_DELAY_MS);
117+
k_msleep(ETH_RETRY_DELAY_MS);
118+
continue;
119+
}
120+
}
121+
122+
/* Wait for interface down/up event or timeout */
123+
k_sem_take(&eth_reconfigure_sem, K_MSEC(ETH_RETRY_DELAY_MS));
124+
}
125+
126+
LOG_INF("ETH background task stopped");
127+
}
128+
129+
extern "C" auto eth_start() -> int
130+
{
131+
if (eth_iface == nullptr) {
132+
LOG_ERR("Ethernet not initialized, call eth_init() first");
133+
return -EINVAL;
134+
}
135+
136+
if (eth_task_running) {
137+
LOG_WRN("ETH task already running");
138+
return 0;
139+
}
140+
141+
eth_task_running = true;
142+
143+
k_thread_create(&eth_thread_data, eth_thread_stack,
144+
K_THREAD_STACK_SIZEOF(eth_thread_stack),
145+
eth_thread_entry, nullptr, nullptr, nullptr,
146+
ETH_THREAD_PRIORITY, 0, K_NO_WAIT);
147+
148+
k_thread_name_set(&eth_thread_data, "eth_task");
149+
150+
return 0;
151+
}
152+
153+
extern "C" auto eth_stop() -> void
154+
{
155+
if (!eth_task_running) {
156+
return;
157+
}
158+
159+
eth_task_running = false;
160+
k_sem_give(&eth_reconfigure_sem);
161+
}

‎project/apps/he_app/main.cpp‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
#include <zephyr/logging/log.h>
33
#include <zephyr/drivers/gpio.h>
44

5-
extern "C"
6-
{
7-
auto wlan_init() -> int;
8-
auto wlan_start() -> int;
9-
auto wlan_is_connected() -> bool;
5+
LOG_MODULE_REGISTER(app);
6+
7+
// Network interface functions
8+
extern "C" {
9+
auto eth_init() -> int;
10+
auto eth_start() -> int;
11+
auto wlan_init() -> int;
12+
auto wlan_start() -> int;
1013
}
1114

1215
#define GREEN_LED_NODE DT_ALIAS(led0)
@@ -19,20 +22,25 @@ auto main() -> int
1922

2023
LOG_INF("HE App starting...");
2124

22-
// Initialize Ethernet with static IP (169.254.0.2)
25+
// Initialize Ethernet and start background configuration task
2326
ret = eth_init();
2427
if (ret < 0) {
2528
LOG_ERR("Ethernet init failed: %d", ret);
29+
} else {
30+
ret = eth_start();
31+
if (ret < 0) {
32+
LOG_ERR("Ethernet background task start failed: %d", ret);
33+
}
2634
}
2735

28-
// Initialize and connect WiFi with DHCP
36+
// Initialize WiFi and start background connection task
2937
ret = wlan_init();
3038
if (ret < 0) {
3139
LOG_ERR("WiFi init failed: %d", ret);
3240
} else {
33-
ret = wlan_connect();
41+
ret = wlan_start();
3442
if (ret < 0) {
35-
LOG_ERR("WiFi connect failed: %d", ret);
43+
LOG_ERR("WiFi background task start failed: %d", ret);
3644
}
3745
}
3846

‎project/apps/he_app/wlan.cpp‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ 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-
4037
static void wifi_event_handler(struct net_mgmt_event_callback *cb,
4138
uint32_t mgmt_event,
4239
struct net_if *iface)

0 commit comments

Comments
 (0)