Skip to content

Commit ea8b8ef

Browse files
authored
Merge branch 'micropython:master' into master
2 parents fbdee94 + 53d0050 commit ea8b8ef

25 files changed

+673
-45
lines changed

extmod/libmetal/metal/config.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@
5757
#define METAL_MAX_DEVICE_REGIONS 1
5858
#endif
5959

60-
// generic/log.h
61-
#if METAL_LOG_HANDLER_ENABLE
62-
#include "py/mphal.h"
63-
#undef metal_log
64-
#define metal_log(level, ...) mp_printf(&mp_plat_print, __VA_ARGS__)
65-
#endif
66-
6760
static inline void *__metal_allocate_memory(unsigned int size) {
6861
return m_tracked_calloc(1, size);
6962
}

extmod/modopenamp.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828

2929
#if MICROPY_PY_OPENAMP
3030

31+
#include <stdarg.h>
3132
#include "py/obj.h"
3233
#include "py/nlr.h"
3334
#include "py/runtime.h"
35+
#include "py/mpprint.h"
3436

3537
#include "metal/sys.h"
3638
#include "metal/alloc.h"
@@ -75,8 +77,6 @@ static const char openamp_trace_buf[128];
7577

7678
#endif // MICROPY_PY_OPENAMP_RSC_TABLE_ENABLE
7779

78-
#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__)
79-
8080
#if MICROPY_PY_OPENAMP_REMOTEPROC
8181
extern mp_obj_type_t openamp_remoteproc_type;
8282
#endif
@@ -136,7 +136,7 @@ typedef struct _endpoint_obj_t {
136136
static const mp_obj_type_t endpoint_type;
137137

138138
static int endpoint_recv_callback(struct rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv) {
139-
debug_printf("endpoint_recv_callback() message received src: %lu msg len: %d\n", src, len);
139+
metal_log(METAL_LOG_DEBUG, "endpoint_recv_callback() message received src: %lu msg len: %d\n", src, len);
140140
endpoint_obj_t *self = metal_container_of(ept, endpoint_obj_t, ep);
141141
if (self->callback != mp_const_none) {
142142
mp_call_function_2(self->callback, mp_obj_new_int(src), mp_obj_new_bytearray_by_ref(len, data));
@@ -174,7 +174,7 @@ static mp_obj_t endpoint_send(uint n_args, const mp_obj_t *pos_args, mp_map_t *k
174174

175175
mp_buffer_info_t rbuf;
176176
mp_get_buffer_raise(pos_args[1], &rbuf, MP_BUFFER_READ);
177-
debug_printf("endpoint_send() msg len: %d\n", rbuf.len);
177+
metal_log(METAL_LOG_DEBUG, "endpoint_send() msg len: %d\n", rbuf.len);
178178

179179
int bytes = 0;
180180
mp_int_t timeout = args[ARG_timeout].u_int;
@@ -258,7 +258,7 @@ void openamp_remoteproc_notified(mp_sched_node_t *node) {
258258
}
259259

260260
static void openamp_ns_callback(struct rpmsg_device *rdev, const char *name, uint32_t dest) {
261-
debug_printf("rpmsg_new_service_callback() new service request name: %s dest %lu\n", name, dest);
261+
metal_log(METAL_LOG_DEBUG, "rpmsg_new_service_callback() new service request name: %s dest %lu\n", name, dest);
262262
// The remote processor advertises its presence to the host by sending
263263
// the Name Service (NS) announcement containing the name of the channel.
264264
virtio_dev_obj_t *virtio_device = metal_container_of(rdev, virtio_dev_obj_t, rvdev);
@@ -315,14 +315,28 @@ static mp_obj_t openamp_new_service_callback(mp_obj_t ns_callback) {
315315
}
316316
static MP_DEFINE_CONST_FUN_OBJ_1(openamp_new_service_callback_obj, openamp_new_service_callback);
317317

318+
void openamp_metal_log_handler(enum metal_log_level level, const char *fmt, ...) {
319+
va_list args;
320+
va_start(args, fmt);
321+
mp_vprintf(&mp_plat_print, fmt, args);
322+
va_end(args);
323+
}
324+
318325
void openamp_init(void) {
319326
if (MP_STATE_PORT(virtio_device) != NULL) {
320327
// Already initialized.
321328
return;
322329
}
323330

324331
struct metal_device *device;
325-
struct metal_init_params metal_params = METAL_INIT_DEFAULTS;
332+
struct metal_init_params metal_params = { 0 };
333+
334+
#if METAL_LOG_HANDLER_ENABLE
335+
// If logging is enabled, set the default log level and handler before
336+
// calling metal_init, to allow ports to override them in metal_sys_init.
337+
metal_params.log_level = METAL_LOG_DEBUG;
338+
metal_params.log_handler = openamp_metal_log_handler;
339+
#endif
326340

327341
// Initialize libmetal.
328342
metal_init(&metal_params);

extmod/modopenamp_remoteproc.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
#include "modopenamp.h"
4747
#include "modopenamp_remoteproc.h"
4848

49-
#define DEBUG_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__)
50-
5149
#if !MICROPY_PY_OPENAMP
5250
#error "MICROPY_PY_OPENAMP_REMOTEPROC requires MICROPY_PY_OPENAMP"
5351
#endif

extmod/modopenamp_remoteproc_store.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747

4848
#if MICROPY_PY_OPENAMP_REMOTEPROC_STORE_ENABLE
4949

50-
#define DEBUG_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__)
51-
5250
// Note the initial file buffer size needs to be at least 512 to read
5351
// enough of the elf headers on the first call to store_open(), and on
5452
// subsequent calls to store functions, it gets reallocated if needed.
@@ -70,7 +68,7 @@ void *mp_openamp_remoteproc_store_alloc(void) {
7068
}
7169

7270
static int openamp_remoteproc_store_open(void *store, const char *path, const void **image_data) {
73-
DEBUG_printf("store_open(): %s\n", path);
71+
metal_log(METAL_LOG_DEBUG, "store_open(): %s\n", path);
7472
mp_obj_t args[2] = {
7573
mp_obj_new_str(path, strlen(path)),
7674
MP_OBJ_NEW_QSTR(MP_QSTR_rb),
@@ -89,7 +87,7 @@ static int openamp_remoteproc_store_open(void *store, const char *path, const vo
8987
}
9088

9189
static void openamp_remoteproc_store_close(void *store) {
92-
DEBUG_printf("store_close()\n");
90+
metal_log(METAL_LOG_DEBUG, "store_close()\n");
9391
openamp_remoteproc_filestore_t *fstore = store;
9492
mp_stream_close(fstore->file);
9593
metal_free_memory(fstore->buf);
@@ -113,17 +111,17 @@ static int openamp_remoteproc_store_load(void *store, size_t offset, size_t size
113111
// Note tracked allocs don't support realloc.
114112
fstore->len = size;
115113
fstore->buf = metal_allocate_memory(size);
116-
DEBUG_printf("store_load() realloc to %lu\n", fstore->len);
114+
metal_log(METAL_LOG_DEBUG, "store_load() realloc to %lu\n", fstore->len);
117115
}
118116
*data = fstore->buf;
119-
DEBUG_printf("store_load(): pa 0x%lx offset %u size %u \n", (uint32_t)pa, offset, size);
117+
metal_log(METAL_LOG_DEBUG, "store_load(): pa 0x%lx offset %u size %u \n", (uint32_t)pa, offset, size);
120118
} else {
121119
void *va = metal_io_phys_to_virt(io, pa);
122120
if (va == NULL) {
123121
return -EINVAL;
124122
}
125123
*data = va;
126-
DEBUG_printf("store_load(): pa 0x%lx va 0x%p offset %u size %u \n", (uint32_t)pa, va, offset, size);
124+
metal_log(METAL_LOG_DEBUG, "store_load(): pa 0x%lx va 0x%p offset %u size %u \n", (uint32_t)pa, va, offset, size);
127125
}
128126

129127
mp_uint_t bytes = mp_stream_read_exactly(fstore->file, (void *)*data, size, &error);

extmod/modtls_axtls.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#define PROTOCOL_TLS_CLIENT (0)
4040
#define PROTOCOL_TLS_SERVER (1)
4141

42+
#define CERT_NONE (0)
43+
4244
// This corresponds to an SSLContext object.
4345
typedef struct _mp_obj_ssl_context_t {
4446
mp_obj_base_t base;
@@ -155,6 +157,25 @@ static mp_obj_t ssl_context_make_new(const mp_obj_type_t *type_in, size_t n_args
155157
return MP_OBJ_FROM_PTR(self);
156158
}
157159

160+
static void ssl_context_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
161+
if (dest[0] == MP_OBJ_NULL) {
162+
// Load attribute.
163+
if (attr == MP_QSTR_verify_mode) {
164+
// CERT_NONE is the only supported verify_mode value.
165+
dest[0] = MP_OBJ_NEW_SMALL_INT(CERT_NONE);
166+
} else {
167+
// Continue lookup in locals_dict.
168+
dest[1] = MP_OBJ_SENTINEL;
169+
}
170+
} else if (dest[1] != MP_OBJ_NULL) {
171+
// Store attribute.
172+
if (attr == MP_QSTR_verify_mode) {
173+
// CERT_NONE is the only supported verify_mode value, so no need to store anything.
174+
dest[0] = MP_OBJ_NULL;
175+
}
176+
}
177+
}
178+
158179
static void ssl_context_load_key(mp_obj_ssl_context_t *self, mp_obj_t key_obj, mp_obj_t cert_obj) {
159180
self->key = key_obj;
160181
self->cert = cert_obj;
@@ -199,6 +220,7 @@ static MP_DEFINE_CONST_OBJ_TYPE(
199220
MP_QSTR_SSLContext,
200221
MP_TYPE_FLAG_NONE,
201222
make_new, ssl_context_make_new,
223+
attr, ssl_context_attr,
202224
locals_dict, &ssl_context_locals_dict
203225
);
204226

@@ -429,6 +451,7 @@ static const mp_rom_map_elem_t mp_module_tls_globals_table[] = {
429451
// Constants.
430452
{ MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLS_CLIENT), MP_ROM_INT(PROTOCOL_TLS_CLIENT) },
431453
{ MP_ROM_QSTR(MP_QSTR_PROTOCOL_TLS_SERVER), MP_ROM_INT(PROTOCOL_TLS_SERVER) },
454+
{ MP_ROM_QSTR(MP_QSTR_CERT_NONE), MP_ROM_INT(CERT_NONE) },
432455
};
433456
static MP_DEFINE_CONST_DICT(mp_module_tls_globals, mp_module_tls_globals_table);
434457

ports/mimxrt/Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ SRC_HAL_IMX_C += \
172172
$(MCU_DIR)/drivers/fsl_common_arm.c \
173173
$(MCU_DIR)/drivers/fsl_anatop_ai.c \
174174
$(MCU_DIR)/drivers/fsl_caam.c \
175-
$(MCU_DIR)/drivers/fsl_lpadc.c
175+
$(MCU_DIR)/drivers/fsl_lpadc.c \
176+
$(MCU_DIR)/drivers/fsl_mu.c
176177
else
177178
SRC_HAL_IMX_C += \
178179
$(MCU_DIR)/drivers/fsl_adc.c \
@@ -267,6 +268,17 @@ ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1)
267268
SRC_C += mpnimbleport.c
268269
endif
269270

271+
# Add mimxrt-specific implementation of libmetal (and optionally OpenAMP's rproc).
272+
# Note: libmetal code is generated via a pre-processor so ensure that runs first.
273+
ifeq ($(MICROPY_PY_OPENAMP),1)
274+
SRC_C += mpmetalport.c
275+
$(BUILD)/mpmetalport.o: $(BUILD)/openamp/metal/config.h
276+
ifeq ($(MICROPY_PY_OPENAMP_REMOTEPROC),1)
277+
SRC_C += mpremoteprocport.c
278+
$(BUILD)/mpremoteprocport.o: $(BUILD)/openamp/metal/config.h
279+
endif
280+
endif
281+
270282
# Math library source files
271283
ifeq ($(MICROPY_FLOAT_IMPL),double)
272284
LIBM_SRC_C += $(SRC_LIB_LIBM_DBL_C)

ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ MICROPY_HW_SDRAM_SIZE = 0x4000000 # 64MB
1313
MICROPY_PY_LWIP = 1
1414
MICROPY_PY_SSL = 1
1515
MICROPY_SSL_MBEDTLS = 1
16+
MICROPY_PY_OPENAMP = 1
17+
MICROPY_PY_OPENAMP_REMOTEPROC = 1
1618

1719
FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py
1820

ports/mimxrt/boards/MIMXRT1176.ld

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ _gc_heap_end = ORIGIN(m_sdram) + LENGTH(m_sdram);
5757
_gc_heap_start = ORIGIN(m_ocrm);
5858
_gc_heap_end = ORIGIN(m_ocrm) + LENGTH(m_ocrm);
5959
#endif
60+
61+
/* CM4 DTCM */
62+
_openamp_shm_region_start = 0x20220000;
63+
_openamp_shm_region_end = 0x20230000;

ports/mimxrt/mpmetalport.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 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+
* libmetal mimxrt port.
27+
*/
28+
29+
#include "py/mperrno.h"
30+
#include "py/mphal.h"
31+
32+
#include "fsl_common.h"
33+
#include "fsl_mu.h"
34+
#include CPU_HEADER_H
35+
36+
#include "metal/sys.h"
37+
#include "metal/utilities.h"
38+
#include "metal/device.h"
39+
40+
struct metal_state _metal;
41+
static mp_sched_node_t rproc_notify_node;
42+
43+
#define IRQ_PRI_MU NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 10, 0)
44+
45+
int metal_sys_init(const struct metal_init_params *params) {
46+
metal_unused(params);
47+
48+
// Init MU.
49+
MU_Init(MUA);
50+
51+
// Configure and enable MU IRQs.
52+
MU_ClearStatusFlags(MUA, kMU_GenInt0Flag);
53+
NVIC_SetPriority(MUA_IRQn, IRQ_PRI_MU);
54+
NVIC_EnableIRQ(MUA_IRQn);
55+
MU_EnableInterrupts(MUA, kMU_GenInt0InterruptEnable);
56+
57+
#ifndef VIRTIO_USE_DCACHE
58+
// If cache management is not enabled, configure the MPU to disable caching
59+
// for the entire shared memory region.
60+
ARM_MPU_Disable();
61+
MPU->RBAR = ARM_MPU_RBAR(10, METAL_MPU_REGION_BASE);
62+
// Normal type, not shareable, non-cacheable
63+
MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 1, 0, 0, 0, 0, METAL_MPU_REGION_SIZE);
64+
ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_HFNMIENA_Msk);
65+
#endif
66+
67+
metal_bus_register(&metal_generic_bus);
68+
return 0;
69+
}
70+
71+
void metal_sys_finish(void) {
72+
NVIC_DisableIRQ(MUA_IRQn);
73+
metal_bus_unregister(&metal_generic_bus);
74+
}
75+
76+
unsigned int sys_irq_save_disable(void) {
77+
return disable_irq();
78+
}
79+
80+
void sys_irq_restore_enable(unsigned int state) {
81+
enable_irq(state);
82+
}
83+
84+
void *metal_machine_io_mem_map(void *va, metal_phys_addr_t pa,
85+
size_t size, unsigned int flags) {
86+
metal_unused(pa);
87+
metal_unused(size);
88+
metal_unused(flags);
89+
return va;
90+
}
91+
92+
void metal_machine_cache_flush(void *addr, unsigned int len) {
93+
SCB_CleanDCache_by_Addr(addr, len);
94+
}
95+
96+
void metal_machine_cache_invalidate(void *addr, unsigned int len) {
97+
SCB_InvalidateDCache_by_Addr(addr, len);
98+
}
99+
100+
int metal_rproc_notify(void *priv, uint32_t id) {
101+
MU_TriggerInterrupts(MUA, kMU_GenInt0InterruptTrigger);
102+
return 0;
103+
}
104+
105+
void MUA_IRQHandler(void) {
106+
if (MU_GetStatusFlags(MUA) & kMU_GenInt0Flag) {
107+
MU_ClearStatusFlags(MUA, kMU_GenInt0Flag);
108+
mp_sched_schedule_node(&rproc_notify_node, openamp_remoteproc_notified);
109+
}
110+
__DSB();
111+
}

0 commit comments

Comments
 (0)