Skip to content

Commit 526c7ea

Browse files
committed
alif: Integrate lwIP and mbedTLS.
Signed-off-by: Damien George <[email protected]>
1 parent 411146b commit 526c7ea

File tree

12 files changed

+320
-1
lines changed

12 files changed

+320
-1
lines changed

ports/alif/alif.mk

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ INC += -I$(ALIF_DFP_REL_HERE)/Device/core/$(MCU_CORE)/include/
4747
INC += -I$(ALIF_DFP_REL_HERE)/Device/$(MCU_SERIES)/$(MCU_VARIANT)/
4848
INC += -I$(TOP)/lib/tinyusb/src
4949
INC += -Itinyusb_port
50+
INC += -Ilwip_inc
5051

5152
GEN_PIN_MKPINS = mcu/make-pins.py
5253
GEN_PIN_PREFIX = mcu/pins_prefix.c
@@ -122,6 +123,7 @@ SRC_C = \
122123
main.c \
123124
modalif.c \
124125
mphalport.c \
126+
mpnetworkport.c \
125127
mpu.c \
126128
mpuart.c \
127129
msc_disk.c \
@@ -135,6 +137,10 @@ SRC_C = \
135137
vfs_rom_ioctl.c \
136138
$(wildcard $(BOARD_DIR)/*.c)
137139

140+
ifeq ($(MICROPY_SSL_MBEDTLS),1)
141+
SRC_C += mbedtls/mbedtls_port.c
142+
endif
143+
138144
ifeq ($(MICROPY_FLOAT_IMPL),float)
139145
LIBM_SRC_C += $(SRC_LIB_LIBM_C)
140146
LIBM_SRC_C += $(SRC_LIB_LIBM_SQRT_HW_C)
@@ -148,7 +154,6 @@ endif
148154
SHARED_SRC_C += $(addprefix shared/,\
149155
libc/string0.c \
150156
netutils/dhcpserver.c \
151-
netutils/netutils.c \
152157
netutils/trace.c \
153158
readline/readline.c \
154159
runtime/gchelper_native.c \

ports/alif/boards/manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
require("dht")
44
require("neopixel")
55
require("onewire")
6+
require("bundle-networking")

ports/alif/lwip_inc/arch/cc.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef MICROPY_INCLUDED_ALIF_LWIP_ARCH_CC_H
2+
#define MICROPY_INCLUDED_ALIF_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_ALIF_LWIP_ARCH_CC_H

ports/alif/lwip_inc/arch/sys_arch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

ports/alif/lwip_inc/lwipopts.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef MICROPY_INCLUDED_ALIF_LWIP_LWIPOPTS_H
2+
#define MICROPY_INCLUDED_ALIF_LWIP_LWIPOPTS_H
3+
4+
#include <stdint.h>
5+
6+
// This protection is not needed, instead we execute all lwIP code at PendSV priority
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+
#define LWIP_NETIF_EXT_STATUS_CALLBACK 1
26+
27+
#define LWIP_LOOPIF_MULTICAST 1
28+
#define LWIP_LOOPBACK_MAX_PBUFS 8
29+
30+
#define LWIP_IPV6 0
31+
#define LWIP_DHCP 1
32+
#define LWIP_DHCP_CHECK_LINK_UP 1
33+
#define LWIP_DHCP_DOES_ACD_CHECK 0 // to speed DHCP up
34+
#define LWIP_DNS 1
35+
#define LWIP_DNS_SUPPORT_MDNS_QUERIES 1
36+
#define LWIP_MDNS_RESPONDER 1
37+
#define LWIP_IGMP 1
38+
39+
#define LWIP_NUM_NETIF_CLIENT_DATA LWIP_MDNS_RESPONDER
40+
#define MEMP_NUM_UDP_PCB (4 + LWIP_MDNS_RESPONDER)
41+
#define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + LWIP_MDNS_RESPONDER)
42+
43+
#define SO_REUSE 1
44+
#define TCP_LISTEN_BACKLOG 1
45+
46+
extern uint64_t se_services_rand64(void);
47+
#define LWIP_RAND() se_services_rand64()
48+
49+
#define MEM_SIZE (16 * 1024)
50+
#define TCP_MSS (1460)
51+
#define TCP_OVERSIZE (TCP_MSS)
52+
#define TCP_WND (8 * TCP_MSS)
53+
#define TCP_SND_BUF (8 * TCP_MSS)
54+
#define TCP_SND_QUEUELEN (2 * (TCP_SND_BUF / TCP_MSS))
55+
#define TCP_QUEUE_OOSEQ (1)
56+
#define MEMP_NUM_TCP_SEG (2 * TCP_SND_QUEUELEN)
57+
58+
typedef uint32_t sys_prot_t;
59+
60+
#endif // MICROPY_INCLUDED_ALIF_LWIP_LWIPOPTS_H

ports/alif/main.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/mperrno.h"
3131
#include "py/mphal.h"
3232
#include "py/stackctrl.h"
33+
#include "extmod/modnetwork.h"
3334
#include "shared/readline/readline.h"
3435
#include "shared/runtime/gchelper.h"
3536
#include "shared/runtime/pyexec.h"
@@ -42,6 +43,11 @@
4243
#include "se_services.h"
4344
#include "system_tick.h"
4445

46+
#if MICROPY_PY_LWIP
47+
#include "lwip/init.h"
48+
#include "lwip/apps/mdns.h"
49+
#endif
50+
4551
extern uint8_t __StackTop, __StackLimit;
4652
extern uint8_t __GcHeapStart, __GcHeapEnd;
4753

@@ -83,6 +89,17 @@ int main(void) {
8389
mp_stack_set_limit(&__StackTop - &__StackLimit - 1024);
8490
gc_init(&__GcHeapStart, &__GcHeapEnd);
8591

92+
#if MICROPY_PY_LWIP
93+
// lwIP doesn't allow to reinitialise itself by subsequent calls to this function
94+
// because the system timeout list (next_timeout) is only ever reset by BSS clearing.
95+
// So for now we only init the lwIP stack once on power-up.
96+
lwip_init();
97+
#if LWIP_MDNS_RESPONDER
98+
mdns_resp_init();
99+
#endif
100+
mod_network_lwip_init();
101+
#endif
102+
86103
for (;;) {
87104
// Initialise MicroPython runtime.
88105
mp_init();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024-2025 OpenMV LLC.
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+
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
27+
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H
28+
29+
// Time hook.
30+
#include <time.h>
31+
extern time_t alif_mbedtls_time(time_t *timer);
32+
#define MBEDTLS_PLATFORM_TIME_MACRO alif_mbedtls_time
33+
#define MBEDTLS_PLATFORM_MS_TIME_ALT undefined_and_unused
34+
35+
// Set MicroPython-specific options.
36+
#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1)
37+
38+
// Include common mbedtls configuration.
39+
#include "extmod/mbedtls/mbedtls_config_common.h"
40+
41+
#endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */

ports/alif/mbedtls/mbedtls_port.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024-2025 OpenMV LLC.
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/obj.h"
28+
#include "se_services.h"
29+
#include "mbedtls_config_port.h"
30+
31+
#if defined(MBEDTLS_HAVE_TIME)
32+
#include "shared/timeutils/timeutils.h"
33+
#include "mbedtls/platform_time.h"
34+
#endif
35+
36+
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
37+
uint32_t val = 0;
38+
int n = 0;
39+
*olen = len;
40+
while (len--) {
41+
if (!n) {
42+
val = se_services_rand64();
43+
n = 4;
44+
}
45+
*output++ = val;
46+
val >>= 8;
47+
--n;
48+
}
49+
return 0;
50+
}
51+
52+
#if defined(MBEDTLS_HAVE_TIME)
53+
54+
time_t alif_mbedtls_time(time_t *timer) {
55+
// TODO implement proper RTC time
56+
unsigned int year = 2025;
57+
unsigned int month = 1;
58+
unsigned int date = 1;
59+
unsigned int hours = 12;
60+
unsigned int minutes = 0;
61+
unsigned int seconds = 0;
62+
return timeutils_seconds_since_epoch(year, month, date, hours, minutes, seconds);
63+
}
64+
65+
#endif

ports/alif/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@
136136
#define MICROPY_PY_MACHINE_SPI (1)
137137
#define MICROPY_PY_MACHINE_SOFTSPI (1)
138138
#define MICROPY_PY_MACHINE_TIMER (1)
139+
#define MICROPY_PY_NETWORK (CORE_M55_HP)
140+
#ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT
141+
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-alif"
142+
#endif
139143
#define MICROPY_VFS (1)
140144
#define MICROPY_VFS_ROM (1)
141145

ports/alif/mphalport.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "tusb.h"
3939
#include "mpuart.h"
4040
#include "pendsv.h"
41+
#include "se_services.h"
4142
#include "system_tick.h"
4243

4344
#ifndef MICROPY_HW_STDIN_BUFFER_LEN
@@ -237,3 +238,32 @@ void soft_timer_schedule_at_ms(uint32_t ticks_ms) {
237238
}
238239

239240
#endif
241+
242+
/*******************************************************************************/
243+
// MAC address
244+
245+
// Generate a random locally administered MAC address (LAA)
246+
void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]) {
247+
uint8_t id[8];
248+
se_services_get_unique_id(id);
249+
buf[0] = 0x02; // LAA range
250+
buf[1] = id[4];
251+
buf[2] = id[3];
252+
buf[3] = id[2];
253+
buf[4] = id[1];
254+
buf[5] = (id[0] << 2) | idx;
255+
}
256+
257+
// A board can override this if needed
258+
MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
259+
mp_hal_generate_laa_mac(idx, buf);
260+
}
261+
262+
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) {
263+
static const char hexchr[16] = "0123456789ABCDEF";
264+
uint8_t mac[6];
265+
mp_hal_get_mac(idx, mac);
266+
for (; chr_len; ++chr_off, --chr_len) {
267+
*dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf];
268+
}
269+
}

0 commit comments

Comments
 (0)