Skip to content

extmod/modopenamp: Implement MIMXRT port and log handler fixes. #14120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions extmod/libmetal/metal/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@
#define METAL_MAX_DEVICE_REGIONS 1
#endif

// generic/log.h
#if METAL_LOG_HANDLER_ENABLE
#include "py/mphal.h"
#undef metal_log
#define metal_log(level, ...) mp_printf(&mp_plat_print, __VA_ARGS__)
#endif

static inline void *__metal_allocate_memory(unsigned int size) {
return m_tracked_calloc(1, size);
}
Expand Down
26 changes: 20 additions & 6 deletions extmod/modopenamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

#if MICROPY_PY_OPENAMP

#include <stdarg.h>
#include "py/obj.h"
#include "py/nlr.h"
#include "py/runtime.h"
#include "py/mpprint.h"

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

#endif // MICROPY_PY_OPENAMP_RSC_TABLE_ENABLE

#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__)

#if MICROPY_PY_OPENAMP_REMOTEPROC
extern mp_obj_type_t openamp_remoteproc_type;
#endif
Expand Down Expand Up @@ -136,7 +136,7 @@ typedef struct _endpoint_obj_t {
static const mp_obj_type_t endpoint_type;

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

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

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

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

void openamp_metal_log_handler(enum metal_log_level level, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
mp_vprintf(&mp_plat_print, fmt, args);
va_end(args);
}

void openamp_init(void) {
if (MP_STATE_PORT(virtio_device) != NULL) {
// Already initialized.
return;
}

struct metal_device *device;
struct metal_init_params metal_params = METAL_INIT_DEFAULTS;
struct metal_init_params metal_params = { 0 };

#if METAL_LOG_HANDLER_ENABLE
// If logging is enabled, set the default log level and handler before
// calling metal_init, to allow ports to override them in metal_sys_init.
metal_params.log_level = METAL_LOG_DEBUG;
metal_params.log_handler = openamp_metal_log_handler;
#endif

// Initialize libmetal.
metal_init(&metal_params);
Expand Down
2 changes: 0 additions & 2 deletions extmod/modopenamp_remoteproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
#include "modopenamp.h"
#include "modopenamp_remoteproc.h"

#define DEBUG_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__)

#if !MICROPY_PY_OPENAMP
#error "MICROPY_PY_OPENAMP_REMOTEPROC requires MICROPY_PY_OPENAMP"
#endif
Expand Down
12 changes: 5 additions & 7 deletions extmod/modopenamp_remoteproc_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@

#if MICROPY_PY_OPENAMP_REMOTEPROC_STORE_ENABLE

#define DEBUG_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__)

// Note the initial file buffer size needs to be at least 512 to read
// enough of the elf headers on the first call to store_open(), and on
// subsequent calls to store functions, it gets reallocated if needed.
Expand All @@ -70,7 +68,7 @@ void *mp_openamp_remoteproc_store_alloc(void) {
}

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

static void openamp_remoteproc_store_close(void *store) {
DEBUG_printf("store_close()\n");
metal_log(METAL_LOG_DEBUG, "store_close()\n");
openamp_remoteproc_filestore_t *fstore = store;
mp_stream_close(fstore->file);
metal_free_memory(fstore->buf);
Expand All @@ -113,17 +111,17 @@ static int openamp_remoteproc_store_load(void *store, size_t offset, size_t size
// Note tracked allocs don't support realloc.
fstore->len = size;
fstore->buf = metal_allocate_memory(size);
DEBUG_printf("store_load() realloc to %lu\n", fstore->len);
metal_log(METAL_LOG_DEBUG, "store_load() realloc to %lu\n", fstore->len);
}
*data = fstore->buf;
DEBUG_printf("store_load(): pa 0x%lx offset %u size %u \n", (uint32_t)pa, offset, size);
metal_log(METAL_LOG_DEBUG, "store_load(): pa 0x%lx offset %u size %u \n", (uint32_t)pa, offset, size);
} else {
void *va = metal_io_phys_to_virt(io, pa);
if (va == NULL) {
return -EINVAL;
}
*data = va;
DEBUG_printf("store_load(): pa 0x%lx va 0x%p offset %u size %u \n", (uint32_t)pa, va, offset, size);
metal_log(METAL_LOG_DEBUG, "store_load(): pa 0x%lx va 0x%p offset %u size %u \n", (uint32_t)pa, va, offset, size);
}

mp_uint_t bytes = mp_stream_read_exactly(fstore->file, (void *)*data, size, &error);
Expand Down
14 changes: 13 additions & 1 deletion ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ SRC_HAL_IMX_C += \
$(MCU_DIR)/drivers/fsl_common_arm.c \
$(MCU_DIR)/drivers/fsl_anatop_ai.c \
$(MCU_DIR)/drivers/fsl_caam.c \
$(MCU_DIR)/drivers/fsl_lpadc.c
$(MCU_DIR)/drivers/fsl_lpadc.c \
$(MCU_DIR)/drivers/fsl_mu.c
else
SRC_HAL_IMX_C += \
$(MCU_DIR)/drivers/fsl_adc.c \
Expand Down Expand Up @@ -267,6 +268,17 @@ ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1)
SRC_C += mpnimbleport.c
endif

# Add mimxrt-specific implementation of libmetal (and optionally OpenAMP's rproc).
# Note: libmetal code is generated via a pre-processor so ensure that runs first.
ifeq ($(MICROPY_PY_OPENAMP),1)
SRC_C += mpmetalport.c
$(BUILD)/mpmetalport.o: $(BUILD)/openamp/metal/config.h
ifeq ($(MICROPY_PY_OPENAMP_REMOTEPROC),1)
SRC_C += mpremoteprocport.c
$(BUILD)/mpremoteprocport.o: $(BUILD)/openamp/metal/config.h
endif
endif

# Math library source files
ifeq ($(MICROPY_FLOAT_IMPL),double)
LIBM_SRC_C += $(SRC_LIB_LIBM_DBL_C)
Expand Down
2 changes: 2 additions & 0 deletions ports/mimxrt/boards/MIMXRT1170_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ MICROPY_HW_SDRAM_SIZE = 0x4000000 # 64MB
MICROPY_PY_LWIP = 1
MICROPY_PY_SSL = 1
MICROPY_SSL_MBEDTLS = 1
MICROPY_PY_OPENAMP = 1
MICROPY_PY_OPENAMP_REMOTEPROC = 1

FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py

Expand Down
4 changes: 4 additions & 0 deletions ports/mimxrt/boards/MIMXRT1176.ld
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ _gc_heap_end = ORIGIN(m_sdram) + LENGTH(m_sdram);
_gc_heap_start = ORIGIN(m_ocrm);
_gc_heap_end = ORIGIN(m_ocrm) + LENGTH(m_ocrm);
#endif

/* CM4 DTCM */
_openamp_shm_region_start = 0x20220000;
_openamp_shm_region_end = 0x20230000;
111 changes: 111 additions & 0 deletions ports/mimxrt/mpmetalport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024 Arduino SA
*
* 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.
*
* libmetal mimxrt port.
*/

#include "py/mperrno.h"
#include "py/mphal.h"

#include "fsl_common.h"
#include "fsl_mu.h"
#include CPU_HEADER_H

#include "metal/sys.h"
#include "metal/utilities.h"
#include "metal/device.h"

struct metal_state _metal;
static mp_sched_node_t rproc_notify_node;

#define IRQ_PRI_MU NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 10, 0)

int metal_sys_init(const struct metal_init_params *params) {
metal_unused(params);

// Init MU.
MU_Init(MUA);

// Configure and enable MU IRQs.
MU_ClearStatusFlags(MUA, kMU_GenInt0Flag);
NVIC_SetPriority(MUA_IRQn, IRQ_PRI_MU);
NVIC_EnableIRQ(MUA_IRQn);
MU_EnableInterrupts(MUA, kMU_GenInt0InterruptEnable);

#ifndef VIRTIO_USE_DCACHE
// If cache management is not enabled, configure the MPU to disable caching
// for the entire shared memory region.
ARM_MPU_Disable();
MPU->RBAR = ARM_MPU_RBAR(10, METAL_MPU_REGION_BASE);
// Normal type, not shareable, non-cacheable
MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 1, 0, 0, 0, 0, METAL_MPU_REGION_SIZE);
ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_HFNMIENA_Msk);
#endif

metal_bus_register(&metal_generic_bus);
return 0;
}

void metal_sys_finish(void) {
NVIC_DisableIRQ(MUA_IRQn);
metal_bus_unregister(&metal_generic_bus);
}

unsigned int sys_irq_save_disable(void) {
return disable_irq();
}

void sys_irq_restore_enable(unsigned int state) {
enable_irq(state);
}

void *metal_machine_io_mem_map(void *va, metal_phys_addr_t pa,
size_t size, unsigned int flags) {
metal_unused(pa);
metal_unused(size);
metal_unused(flags);
return va;
}

void metal_machine_cache_flush(void *addr, unsigned int len) {
SCB_CleanDCache_by_Addr(addr, len);
}

void metal_machine_cache_invalidate(void *addr, unsigned int len) {
SCB_InvalidateDCache_by_Addr(addr, len);
}

int metal_rproc_notify(void *priv, uint32_t id) {
MU_TriggerInterrupts(MUA, kMU_GenInt0InterruptTrigger);
return 0;
}

void MUA_IRQHandler(void) {
if (MU_GetStatusFlags(MUA) & kMU_GenInt0Flag) {
MU_ClearStatusFlags(MUA, kMU_GenInt0Flag);
mp_sched_schedule_node(&rproc_notify_node, openamp_remoteproc_notified);
}
__DSB();
}
73 changes: 73 additions & 0 deletions ports/mimxrt/mpmetalport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024 Arduino SA
*
* 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.
*
* libmetal mimxrt port.
*/
#ifndef MICROPY_INCLUDED_MIMXRT_MPMETALPORT_H
#define MICROPY_INCLUDED_MIMXRT_MPMETALPORT_H

#include <stdlib.h>
#include "py/mphal.h"
#include "py/runtime.h"

#define METAL_HAVE_STDATOMIC_H 0
#define METAL_HAVE_FUTEX_H 0

#define METAL_MAX_DEVICE_REGIONS 2

// Set to 1 to enable the default log handler.
#define METAL_LOG_HANDLER_ENABLE 0

#define metal_cpu_yield()

// Shared memory config
#define METAL_SHM_NAME "OPENAMP_SHM"
// Note 1K must be reserved at the start of the openamp
// shared memory region, for the shared resource table.
#define METAL_RSC_ADDR ((void *)_openamp_shm_region_start)
#define METAL_RSC_SIZE (1024)

#define METAL_SHM_ADDR ((metal_phys_addr_t)(_openamp_shm_region_start + METAL_RSC_SIZE))
#define METAL_SHM_SIZE ((size_t)(_openamp_shm_region_end - _openamp_shm_region_start - METAL_RSC_SIZE))

#define METAL_MPU_REGION_BASE ((uint32_t)_openamp_shm_region_start)
#define METAL_MPU_REGION_SIZE (ARM_MPU_REGION_SIZE_64KB)

extern const char _openamp_shm_region_start[];
extern const char _openamp_shm_region_end[];

int metal_rproc_notify(void *priv, uint32_t id);
extern void openamp_remoteproc_notified(mp_sched_node_t *node);

static inline int __metal_sleep_usec(unsigned int usec) {
mp_hal_delay_us(usec);
return 0;
}

static inline void metal_generic_default_poll(void) {
MICROPY_EVENT_POLL_HOOK
}

#endif // MICROPY_INCLUDED_MIMXRT_METAL_PORT_H
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy