Skip to content

Commit 62e650f

Browse files
iabdalkaderdpgeorge
authored andcommitted
renesas-ra: Add networking support using lwIP.
Signed-off-by: iabdalkader <[email protected]>
1 parent 142e8b7 commit 62e650f

File tree

7 files changed

+206
-1
lines changed

7 files changed

+206
-1
lines changed

ports/renesas-ra/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ INC += -I$(TOP)/$(HAL_DIR)/ra/fsp/src/bsp/cmsis/Device/RENESAS/Include
7272
INC += -I$(TOP)/lib/tinyusb/hw
7373
INC += -I$(TOP)/lib/tinyusb/src
7474
INC += -I$(TOP)/shared/tinyusb
75-
#INC += -Ilwip_inc
75+
INC += -Ilwip_inc
7676
ifeq ($(CMSIS_MCU),$(filter $(CMSIS_MCU),RA4M1 RA4W1 RA6M1 RA6M2 RA6M5))
7777
INC += -Ira
7878
endif
@@ -305,6 +305,7 @@ SRC_C += \
305305
ra_it.c \
306306
rng.c \
307307
mphalport.c \
308+
mpnetworkport.c \
308309
mpthreadport.c \
309310
irq.c \
310311
pendsv.c \

ports/renesas-ra/lwip_inc/arch/cc.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef MICROPY_INCLUDED_RP2_LWIP_ARCH_CC_H
2+
#define MICROPY_INCLUDED_RP2_LWIP_ARCH_CC_H
3+
4+
#include <assert.h>
5+
#define LWIP_PLATFORM_DIAG(x)
6+
#define LWIP_PLATFORM_ASSERT(x) { assert(1); }
7+
8+
#define LWIP_NO_CTYPE_H 1
9+
10+
#endif // MICROPY_INCLUDED_RP2_LWIP_ARCH_CC_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

ports/renesas-ra/lwip_inc/lwipopts.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef MICROPY_INCLUDED_RA_LWIP_LWIPOPTS_H
2+
#define MICROPY_INCLUDED_RA_LWIP_LWIPOPTS_H
3+
4+
#include <stdint.h>
5+
6+
// This protection is not needed, instead protect lwIP code with flags
7+
#define SYS_ARCH_DECL_PROTECT(lev) do { } while (0)
8+
#define SYS_ARCH_PROTECT(lev) do { } while (0)
9+
#define SYS_ARCH_UNPROTECT(lev) do { } while (0)
10+
11+
#define NO_SYS 1
12+
#define SYS_LIGHTWEIGHT_PROT 1
13+
#define MEM_ALIGNMENT 4
14+
15+
#define LWIP_CHKSUM_ALGORITHM 3
16+
#define LWIP_CHECKSUM_CTRL_PER_NETIF 1
17+
18+
#define LWIP_ARP 1
19+
#define LWIP_ETHERNET 1
20+
#define LWIP_RAW 1
21+
#define LWIP_NETCONN 0
22+
#define LWIP_SOCKET 0
23+
#define LWIP_STATS 0
24+
#define LWIP_NETIF_HOSTNAME 1
25+
26+
#define LWIP_IPV6 0
27+
#define LWIP_DHCP 1
28+
#define LWIP_DHCP_CHECK_LINK_UP 1
29+
#define DHCP_DOES_ARP_CHECK 0 // to speed DHCP up
30+
#define LWIP_DNS 1
31+
#define LWIP_DNS_SUPPORT_MDNS_QUERIES 1
32+
#define LWIP_MDNS_RESPONDER 1
33+
#define LWIP_IGMP 1
34+
35+
#define LWIP_NUM_NETIF_CLIENT_DATA LWIP_MDNS_RESPONDER
36+
#define MEMP_NUM_UDP_PCB (4 + LWIP_MDNS_RESPONDER)
37+
#define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + LWIP_MDNS_RESPONDER)
38+
39+
#define SO_REUSE 1
40+
#define TCP_LISTEN_BACKLOG 1
41+
42+
extern uint32_t rng_read(void);
43+
#define LWIP_RAND() rng_read()
44+
45+
// lwip takes 26700 bytes
46+
#define MEM_SIZE (8000)
47+
#define TCP_MSS (800)
48+
#define TCP_WND (8 * TCP_MSS)
49+
#define TCP_SND_BUF (8 * TCP_MSS)
50+
#define MEMP_NUM_TCP_SEG (32)
51+
52+
typedef uint32_t sys_prot_t;
53+
54+
#endif // MICROPY_INCLUDED_RA_LWIP_LWIPOPTS_H

ports/renesas-ra/main.c

+26
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,15 @@
6363
#include "rtc.h"
6464
#include "storage.h"
6565
#include "tusb.h"
66+
#if MICROPY_PY_LWIP
67+
#include "lwip/init.h"
68+
#include "lwip/apps/mdns.h"
69+
#endif
6670
#if MICROPY_PY_BLUETOOTH
6771
#include "mpbthciport.h"
6872
#include "extmod/modbluetooth.h"
6973
#endif
74+
#include "extmod/modnetwork.h"
7075

7176
#define RA_EARLY_PRINT 1 /* for enabling mp_print in boardctrl. */
7277

@@ -274,6 +279,16 @@ int main(void) {
274279

275280
MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP(&state);
276281

282+
#if MICROPY_PY_LWIP
283+
// lwIP doesn't allow to reinitialise itself by subsequent calls to this function
284+
// because the system timeout list (next_timeout) is only ever reset by BSS clearing.
285+
// So for now we only init the lwIP stack once on power-up.
286+
lwip_init();
287+
#if LWIP_MDNS_RESPONDER
288+
mdns_resp_init();
289+
#endif
290+
#endif
291+
277292
soft_reset:
278293

279294
MICROPY_BOARD_TOP_SOFT_RESET_LOOP(&state);
@@ -320,6 +335,14 @@ int main(void) {
320335
machine_i2s_init0();
321336
#endif
322337

338+
#if MICROPY_PY_NETWORK
339+
mod_network_init();
340+
#endif
341+
342+
#if MICROPY_PY_LWIP
343+
mod_network_lwip_init();
344+
#endif
345+
323346
// Initialise the local flash filesystem.
324347
// Create it if needed, mount in on /flash, and set it as current dir.
325348
bool mounted_flash = false;
@@ -393,6 +416,9 @@ int main(void) {
393416
#if MICROPY_PY_BLUETOOTH
394417
mp_bluetooth_deinit();
395418
#endif
419+
#if MICROPY_PY_NETWORK
420+
mod_network_deinit();
421+
#endif
396422
soft_timer_deinit();
397423
timer_deinit();
398424
uart_deinit_all();

ports/renesas-ra/mpconfigport.h

+39
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
#define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1)
127127
#define MICROPY_PY_TIME_TIME_TIME_NS (1)
128128
#define MICROPY_PY_TIME_INCLUDEFILE "ports/renesas-ra/modtime.c"
129+
#define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP)
129130
#ifndef MICROPY_PY_MACHINE
130131
#define MICROPY_PY_MACHINE (1)
131132
#ifndef MICROPY_PY_MACHINE_BITSTREAM
@@ -168,6 +169,25 @@
168169
#define MICROPY_FATFS_MAX_SS (FLASH_SECTOR_SIZE)
169170
#endif
170171

172+
// By default networking should include sockets, ssl, websockets, webrepl, dupterm.
173+
#if MICROPY_PY_NETWORK
174+
#ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT
175+
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-ra"
176+
#endif
177+
#ifndef MICROPY_PY_USOCKET
178+
#define MICROPY_PY_USOCKET (1)
179+
#endif
180+
#ifndef MICROPY_PY_USSL
181+
#define MICROPY_PY_USSL (1)
182+
#endif
183+
#ifndef MICROPY_PY_UWEBSOCKET
184+
#define MICROPY_PY_UWEBSOCKET (1)
185+
#endif
186+
#ifndef MICROPY_PY_WEBREPL
187+
#define MICROPY_PY_WEBREPL (1)
188+
#endif
189+
#endif
190+
171191
#if MICROPY_PY_MACHINE
172192
#define MACHINE_BUILTIN_MODULE_CONSTANTS \
173193
{ MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) },
@@ -181,6 +201,21 @@
181201

182202
#define MP_STATE_PORT MP_STATE_VM
183203

204+
#if MICROPY_PY_NETWORK_ESP_HOSTED
205+
extern const struct _mp_obj_type_t mod_network_esp_hosted_type;
206+
#define MICROPY_HW_NIC_ESP_HOSTED { MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&mod_network_esp_hosted_type) },
207+
#else
208+
#define MICROPY_HW_NIC_ESP_HOSTED
209+
#endif
210+
211+
#ifndef MICROPY_BOARD_NETWORK_INTERFACES
212+
#define MICROPY_BOARD_NETWORK_INTERFACES
213+
#endif
214+
215+
#define MICROPY_PORT_NETWORK_INTERFACES \
216+
MICROPY_HW_NIC_ESP_HOSTED \
217+
MICROPY_BOARD_NETWORK_INTERFACES \
218+
184219
// Miscellaneous settings
185220

186221
#ifndef MICROPY_HW_USB_VID
@@ -269,6 +304,10 @@ static inline mp_uint_t disable_irq(void) {
269304
#define MICROPY_THREAD_YIELD()
270305
#endif
271306

307+
#define MICROPY_PY_LWIP_ENTER
308+
#define MICROPY_PY_LWIP_REENTER
309+
#define MICROPY_PY_LWIP_EXIT
310+
272311
#ifndef MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
273312
#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1)
274313
#endif

ports/renesas-ra/mpnetworkport.c

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Arduino SA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "shared/runtime/softtimer.h"
30+
31+
#if MICROPY_PY_LWIP
32+
33+
#include "lwip/timeouts.h"
34+
35+
static mp_sched_node_t network_poll_node;
36+
static soft_timer_entry_t network_timer;
37+
38+
u32_t sys_now(void) {
39+
return mp_hal_ticks_ms();
40+
}
41+
42+
static void network_poll(mp_sched_node_t *node) {
43+
// Run the lwIP internal updates
44+
sys_check_timeouts();
45+
46+
#if MICROPY_PY_NETWORK_ESP_HOSTED
47+
extern int esp_hosted_wifi_poll(void);
48+
// Poll the NIC for incoming data
49+
if (esp_hosted_wifi_poll() == -1) {
50+
soft_timer_remove(&network_timer);
51+
}
52+
#endif
53+
}
54+
55+
void mod_network_poll_events(void) {
56+
mp_sched_schedule_node(&network_poll_node, network_poll);
57+
}
58+
59+
static void network_timer_callback(soft_timer_entry_t *self) {
60+
mod_network_poll_events();
61+
}
62+
63+
void mod_network_lwip_init(void) {
64+
static bool timer_started = false;
65+
if (timer_started) {
66+
soft_timer_remove(&network_timer);
67+
timer_started = false;
68+
}
69+
// Start poll timer.
70+
soft_timer_static_init(&network_timer, SOFT_TIMER_MODE_PERIODIC, 128, network_timer_callback);
71+
soft_timer_reinsert(&network_timer, 128);
72+
timer_started = true;
73+
}
74+
#endif // MICROPY_PY_LWIP

0 commit comments

Comments
 (0)