alif: Integrate lwIP and mbedTLS.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2024-10-14 21:48:51 +11:00
parent 411146b0ed
commit 526c7eabce
12 changed files with 320 additions and 1 deletions

View file

@ -47,6 +47,7 @@ INC += -I$(ALIF_DFP_REL_HERE)/Device/core/$(MCU_CORE)/include/
INC += -I$(ALIF_DFP_REL_HERE)/Device/$(MCU_SERIES)/$(MCU_VARIANT)/
INC += -I$(TOP)/lib/tinyusb/src
INC += -Itinyusb_port
INC += -Ilwip_inc
GEN_PIN_MKPINS = mcu/make-pins.py
GEN_PIN_PREFIX = mcu/pins_prefix.c
@ -122,6 +123,7 @@ SRC_C = \
main.c \
modalif.c \
mphalport.c \
mpnetworkport.c \
mpu.c \
mpuart.c \
msc_disk.c \
@ -135,6 +137,10 @@ SRC_C = \
vfs_rom_ioctl.c \
$(wildcard $(BOARD_DIR)/*.c)
ifeq ($(MICROPY_SSL_MBEDTLS),1)
SRC_C += mbedtls/mbedtls_port.c
endif
ifeq ($(MICROPY_FLOAT_IMPL),float)
LIBM_SRC_C += $(SRC_LIB_LIBM_C)
LIBM_SRC_C += $(SRC_LIB_LIBM_SQRT_HW_C)
@ -148,7 +154,6 @@ endif
SHARED_SRC_C += $(addprefix shared/,\
libc/string0.c \
netutils/dhcpserver.c \
netutils/netutils.c \
netutils/trace.c \
readline/readline.c \
runtime/gchelper_native.c \

View file

@ -3,3 +3,4 @@ include("$(MPY_DIR)/extmod/asyncio")
require("dht")
require("neopixel")
require("onewire")
require("bundle-networking")

View file

@ -0,0 +1,10 @@
#ifndef MICROPY_INCLUDED_ALIF_LWIP_ARCH_CC_H
#define MICROPY_INCLUDED_ALIF_LWIP_ARCH_CC_H
#include <assert.h>
#define LWIP_PLATFORM_DIAG(x)
#define LWIP_PLATFORM_ASSERT(x) { assert(1); }
#define LWIP_NO_CTYPE_H 1
#endif // MICROPY_INCLUDED_ALIF_LWIP_ARCH_CC_H

View file

@ -0,0 +1 @@
// empty

View file

@ -0,0 +1,60 @@
#ifndef MICROPY_INCLUDED_ALIF_LWIP_LWIPOPTS_H
#define MICROPY_INCLUDED_ALIF_LWIP_LWIPOPTS_H
#include <stdint.h>
// This protection is not needed, instead we execute all lwIP code at PendSV priority
#define SYS_ARCH_DECL_PROTECT(lev) do { } while (0)
#define SYS_ARCH_PROTECT(lev) do { } while (0)
#define SYS_ARCH_UNPROTECT(lev) do { } while (0)
#define NO_SYS 1
#define SYS_LIGHTWEIGHT_PROT 1
#define MEM_ALIGNMENT 4
#define LWIP_CHKSUM_ALGORITHM 3
#define LWIP_CHECKSUM_CTRL_PER_NETIF 1
#define LWIP_ARP 1
#define LWIP_ETHERNET 1
#define LWIP_RAW 1
#define LWIP_NETCONN 0
#define LWIP_SOCKET 0
#define LWIP_STATS 0
#define LWIP_NETIF_HOSTNAME 1
#define LWIP_NETIF_EXT_STATUS_CALLBACK 1
#define LWIP_LOOPIF_MULTICAST 1
#define LWIP_LOOPBACK_MAX_PBUFS 8
#define LWIP_IPV6 0
#define LWIP_DHCP 1
#define LWIP_DHCP_CHECK_LINK_UP 1
#define LWIP_DHCP_DOES_ACD_CHECK 0 // to speed DHCP up
#define LWIP_DNS 1
#define LWIP_DNS_SUPPORT_MDNS_QUERIES 1
#define LWIP_MDNS_RESPONDER 1
#define LWIP_IGMP 1
#define LWIP_NUM_NETIF_CLIENT_DATA LWIP_MDNS_RESPONDER
#define MEMP_NUM_UDP_PCB (4 + LWIP_MDNS_RESPONDER)
#define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + LWIP_MDNS_RESPONDER)
#define SO_REUSE 1
#define TCP_LISTEN_BACKLOG 1
extern uint64_t se_services_rand64(void);
#define LWIP_RAND() se_services_rand64()
#define MEM_SIZE (16 * 1024)
#define TCP_MSS (1460)
#define TCP_OVERSIZE (TCP_MSS)
#define TCP_WND (8 * TCP_MSS)
#define TCP_SND_BUF (8 * TCP_MSS)
#define TCP_SND_QUEUELEN (2 * (TCP_SND_BUF / TCP_MSS))
#define TCP_QUEUE_OOSEQ (1)
#define MEMP_NUM_TCP_SEG (2 * TCP_SND_QUEUELEN)
typedef uint32_t sys_prot_t;
#endif // MICROPY_INCLUDED_ALIF_LWIP_LWIPOPTS_H

View file

@ -30,6 +30,7 @@
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/stackctrl.h"
#include "extmod/modnetwork.h"
#include "shared/readline/readline.h"
#include "shared/runtime/gchelper.h"
#include "shared/runtime/pyexec.h"
@ -42,6 +43,11 @@
#include "se_services.h"
#include "system_tick.h"
#if MICROPY_PY_LWIP
#include "lwip/init.h"
#include "lwip/apps/mdns.h"
#endif
extern uint8_t __StackTop, __StackLimit;
extern uint8_t __GcHeapStart, __GcHeapEnd;
@ -83,6 +89,17 @@ int main(void) {
mp_stack_set_limit(&__StackTop - &__StackLimit - 1024);
gc_init(&__GcHeapStart, &__GcHeapEnd);
#if MICROPY_PY_LWIP
// lwIP doesn't allow to reinitialise itself by subsequent calls to this function
// because the system timeout list (next_timeout) is only ever reset by BSS clearing.
// So for now we only init the lwIP stack once on power-up.
lwip_init();
#if LWIP_MDNS_RESPONDER
mdns_resp_init();
#endif
mod_network_lwip_init();
#endif
for (;;) {
// Initialise MicroPython runtime.
mp_init();

View file

@ -0,0 +1,41 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024-2025 OpenMV LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H
// Time hook.
#include <time.h>
extern time_t alif_mbedtls_time(time_t *timer);
#define MBEDTLS_PLATFORM_TIME_MACRO alif_mbedtls_time
#define MBEDTLS_PLATFORM_MS_TIME_ALT undefined_and_unused
// Set MicroPython-specific options.
#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1)
// Include common mbedtls configuration.
#include "extmod/mbedtls/mbedtls_config_common.h"
#endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */

View file

@ -0,0 +1,65 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024-2025 OpenMV LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/obj.h"
#include "se_services.h"
#include "mbedtls_config_port.h"
#if defined(MBEDTLS_HAVE_TIME)
#include "shared/timeutils/timeutils.h"
#include "mbedtls/platform_time.h"
#endif
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
uint32_t val = 0;
int n = 0;
*olen = len;
while (len--) {
if (!n) {
val = se_services_rand64();
n = 4;
}
*output++ = val;
val >>= 8;
--n;
}
return 0;
}
#if defined(MBEDTLS_HAVE_TIME)
time_t alif_mbedtls_time(time_t *timer) {
// TODO implement proper RTC time
unsigned int year = 2025;
unsigned int month = 1;
unsigned int date = 1;
unsigned int hours = 12;
unsigned int minutes = 0;
unsigned int seconds = 0;
return timeutils_seconds_since_epoch(year, month, date, hours, minutes, seconds);
}
#endif

View file

@ -136,6 +136,10 @@
#define MICROPY_PY_MACHINE_SPI (1)
#define MICROPY_PY_MACHINE_SOFTSPI (1)
#define MICROPY_PY_MACHINE_TIMER (1)
#define MICROPY_PY_NETWORK (CORE_M55_HP)
#ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-alif"
#endif
#define MICROPY_VFS (1)
#define MICROPY_VFS_ROM (1)

View file

@ -38,6 +38,7 @@
#include "tusb.h"
#include "mpuart.h"
#include "pendsv.h"
#include "se_services.h"
#include "system_tick.h"
#ifndef MICROPY_HW_STDIN_BUFFER_LEN
@ -237,3 +238,32 @@ void soft_timer_schedule_at_ms(uint32_t ticks_ms) {
}
#endif
/*******************************************************************************/
// MAC address
// Generate a random locally administered MAC address (LAA)
void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]) {
uint8_t id[8];
se_services_get_unique_id(id);
buf[0] = 0x02; // LAA range
buf[1] = id[4];
buf[2] = id[3];
buf[3] = id[2];
buf[4] = id[1];
buf[5] = (id[0] << 2) | idx;
}
// A board can override this if needed
MP_WEAK void mp_hal_get_mac(int idx, uint8_t buf[6]) {
mp_hal_generate_laa_mac(idx, buf);
}
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest) {
static const char hexchr[16] = "0123456789ABCDEF";
uint8_t mac[6];
mp_hal_get_mac(idx, mac);
for (; chr_len; ++chr_off, --chr_len) {
*dest++ = hexchr[mac[chr_off >> 1] >> (4 * (1 - (chr_off & 1))) & 0xf];
}
}

View file

@ -39,6 +39,11 @@
#define MICROPY_PY_PENDSV_REENTER atomic_state = raise_irq_pri(IRQ_PRI_PENDSV);
#define MICROPY_PY_PENDSV_EXIT restore_irq_pri(atomic_state);
// Prevent the "lwIP task" from running.
#define MICROPY_PY_LWIP_ENTER MICROPY_PY_PENDSV_ENTER
#define MICROPY_PY_LWIP_REENTER MICROPY_PY_PENDSV_REENTER
#define MICROPY_PY_LWIP_EXIT MICROPY_PY_PENDSV_EXIT
// Port level Wait-for-Event macro
//
// Do not use this macro directly, include py/runtime.h and
@ -202,3 +207,17 @@ void mp_hal_pin_config(const machine_pin_obj_t *pin, uint32_t mode,
// Include all the pin definitions.
#include "genhdr/pins_board.h"
/******************************************************************************/
// Other HAL functions.
enum {
MP_HAL_MAC_WLAN0 = 0,
MP_HAL_MAC_WLAN1,
MP_HAL_MAC_BDADDR,
MP_HAL_MAC_ETH0,
};
void mp_hal_generate_laa_mac(int idx, uint8_t buf[6]);
void mp_hal_get_mac(int idx, uint8_t buf[6]);
void mp_hal_get_mac_ascii(int idx, size_t chr_off, size_t chr_len, char *dest);

View file

@ -0,0 +1,66 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024-2025 OpenMV LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/mphal.h"
#include "py/obj.h"
#if MICROPY_PY_LWIP
#include "shared/runtime/softtimer.h"
#include "lwip/timeouts.h"
// Poll lwIP every 64ms by default
#define LWIP_TICK_RATE_MS 64
// Soft timer for running lwIP in the background.
static soft_timer_entry_t mp_network_soft_timer;
u32_t sys_now(void) {
return mp_hal_ticks_ms();
}
// This is called by soft_timer and executes at PendSV level.
static void mp_network_soft_timer_callback(soft_timer_entry_t *self) {
// Run the lwIP internal updates.
sys_check_timeouts();
#if LWIP_NETIF_LOOPBACK
netif_poll_all();
#endif
}
void mod_network_lwip_init(void) {
soft_timer_static_init(
&mp_network_soft_timer,
SOFT_TIMER_MODE_PERIODIC,
LWIP_TICK_RATE_MS,
mp_network_soft_timer_callback
);
soft_timer_reinsert(&mp_network_soft_timer, LWIP_TICK_RATE_MS);
}
#endif // MICROPY_PY_LWIP