Skip to content

Commit f6f8607

Browse files
committed
ports/mimxrt: Add support for Open-AMP.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 0596ea7 commit f6f8607

File tree

6 files changed

+367
-1
lines changed

6 files changed

+367
-1
lines changed

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+
}

ports/mimxrt/mpmetalport.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
#ifndef MICROPY_INCLUDED_MIMXRT_MPMETALPORT_H
29+
#define MICROPY_INCLUDED_MIMXRT_MPMETALPORT_H
30+
31+
#include <stdlib.h>
32+
#include "py/mphal.h"
33+
#include "py/runtime.h"
34+
35+
#define METAL_HAVE_STDATOMIC_H 0
36+
#define METAL_HAVE_FUTEX_H 0
37+
38+
#define METAL_MAX_DEVICE_REGIONS 2
39+
40+
// Set to 1 to enable the default log handler.
41+
#define METAL_LOG_HANDLER_ENABLE 0
42+
43+
#define metal_cpu_yield()
44+
45+
// Shared memory config
46+
#define METAL_SHM_NAME "OPENAMP_SHM"
47+
// Note 1K must be reserved at the start of the openamp
48+
// shared memory region, for the shared resource table.
49+
#define METAL_RSC_ADDR ((void *)_openamp_shm_region_start)
50+
#define METAL_RSC_SIZE (1024)
51+
52+
#define METAL_SHM_ADDR ((metal_phys_addr_t)(_openamp_shm_region_start + METAL_RSC_SIZE))
53+
#define METAL_SHM_SIZE ((size_t)(_openamp_shm_region_end - _openamp_shm_region_start - METAL_RSC_SIZE))
54+
55+
#define METAL_MPU_REGION_BASE ((uint32_t)_openamp_shm_region_start)
56+
#define METAL_MPU_REGION_SIZE (ARM_MPU_REGION_SIZE_64KB)
57+
58+
extern const char _openamp_shm_region_start[];
59+
extern const char _openamp_shm_region_end[];
60+
61+
int metal_rproc_notify(void *priv, uint32_t id);
62+
extern void openamp_remoteproc_notified(mp_sched_node_t *node);
63+
64+
static inline int __metal_sleep_usec(unsigned int usec) {
65+
mp_hal_delay_us(usec);
66+
return 0;
67+
}
68+
69+
static inline void metal_generic_default_poll(void) {
70+
MICROPY_EVENT_POLL_HOOK
71+
}
72+
73+
#endif // MICROPY_INCLUDED_MIMXRT_METAL_PORT_H

ports/mimxrt/mpremoteprocport.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
* modremoteproc mimxrt port.
27+
*/
28+
29+
#include <stdio.h>
30+
#include <stdint.h>
31+
32+
#include "py/obj.h"
33+
#include "py/runtime.h"
34+
35+
#include "fsl_common.h"
36+
#include "fsl_mu.h"
37+
38+
#include "metal/alloc.h"
39+
#include "metal/errno.h"
40+
#include "metal/io.h"
41+
#include "metal/sys.h"
42+
#include "metal/device.h"
43+
#include "metal/utilities.h"
44+
#include "extmod/modopenamp_remoteproc.h"
45+
46+
struct remoteproc *mp_openamp_remoteproc_init(struct remoteproc *rproc,
47+
const struct remoteproc_ops *ops, void *arg) {
48+
metal_log(METAL_LOG_DEBUG, "rproc_init()\n");
49+
50+
rproc->ops = ops;
51+
rproc->state = RPROC_OFFLINE;
52+
// Allocate the image store and save it in private data.
53+
rproc->priv = mp_openamp_remoteproc_store_alloc();
54+
return rproc;
55+
}
56+
57+
void *mp_openamp_remoteproc_mmap(struct remoteproc *rproc, metal_phys_addr_t *pa,
58+
metal_phys_addr_t *da, size_t size, unsigned int attribute,
59+
struct metal_io_region **io) {
60+
metal_log(METAL_LOG_DEBUG, "rproc_mmap(): pa 0x%p da 0x%p io 0x%p size %u\n", *pa, *da, *io, size);
61+
62+
struct remoteproc_mem *mem;
63+
metal_phys_addr_t lpa = *pa;
64+
metal_phys_addr_t lda = *da;
65+
66+
if (lda == METAL_BAD_PHYS) {
67+
return NULL;
68+
}
69+
70+
if (lpa == METAL_BAD_PHYS) {
71+
lpa = lda;
72+
}
73+
74+
if (lpa >= 0x1FFE0000 && lpa < 0x1FFFFFFF) {
75+
// Map CM4 ITCM to CM7 address space.
76+
lpa = 0x20200000 | (((uint32_t)lpa) & 0x1FFFF);
77+
metal_log(METAL_LOG_DEBUG, "rproc_mmap(): remap 0x%p -> 0x%p\n", lda, lpa);
78+
} else if (lpa >= 0x20000000 && lpa < 0x20020000) {
79+
// Map CM4 DTCM to CM7 address space.
80+
lpa = 0x20220000 | (((uint32_t)lpa) & 0x1FFFF);
81+
metal_log(METAL_LOG_DEBUG, "rproc_mmap(): remap 0x%p -> 0x%p\n", lda, lpa);
82+
}
83+
84+
mem = metal_allocate_memory(sizeof(*mem));
85+
if (!mem) {
86+
return NULL;
87+
}
88+
89+
*io = metal_allocate_memory(sizeof(struct metal_io_region));
90+
if (!*io) {
91+
metal_free_memory(mem);
92+
return NULL;
93+
}
94+
95+
remoteproc_init_mem(mem, NULL, lpa, lda, size, *io);
96+
97+
metal_io_init(*io, (void *)lpa, &mem->pa, size,
98+
sizeof(metal_phys_addr_t) << 3, attribute, NULL);
99+
100+
remoteproc_add_mem(rproc, mem);
101+
*pa = lpa;
102+
*da = lda;
103+
return metal_io_phys_to_virt(*io, mem->pa);
104+
}
105+
106+
int mp_openamp_remoteproc_start(struct remoteproc *rproc) {
107+
metal_log(METAL_LOG_DEBUG, "rproc_start()\n");
108+
if (SRC->SCR & SRC_SCR_BT_RELEASE_M4_MASK) {
109+
// The CM4 core is already running.
110+
metal_log(METAL_LOG_DEBUG, "rproc_start(): CM4 core is already booted.\n");
111+
return -1;
112+
}
113+
114+
// Flush M7 cache.
115+
struct metal_list *node;
116+
metal_list_for_each(&rproc->mems, node) {
117+
struct remoteproc_mem *mem;
118+
mem = metal_container_of(node, struct remoteproc_mem, node);
119+
metal_log(METAL_LOG_DEBUG, "rproc_start() clean: pa 0x%p size %u\n", (uint32_t *)mem->pa, mem->size);
120+
SCB_CleanDCache_by_Addr((uint32_t *)mem->pa, mem->size);
121+
}
122+
123+
unsigned long offset = 0;
124+
struct metal_io_region *io = remoteproc_get_io_with_da(rproc, rproc->bootaddr, &offset);
125+
if (!io) {
126+
return -1;
127+
}
128+
129+
IOMUXC_LPSR_GPR->GPR0 = IOMUXC_LPSR_GPR_GPR0_CM4_INIT_VTOR_LOW(((uint32_t)io->virt) >> 3);
130+
(void)IOMUXC_LPSR_GPR->GPR0;
131+
IOMUXC_LPSR_GPR->GPR1 = IOMUXC_LPSR_GPR_GPR1_CM4_INIT_VTOR_HIGH(((uint32_t)io->virt) >> 16);
132+
(void)IOMUXC_LPSR_GPR->GPR1;
133+
134+
SRC->CTRL_M4CORE = SRC_CTRL_M4CORE_SW_RESET_MASK;
135+
SRC->SCR |= SRC_SCR_BT_RELEASE_M4_MASK;
136+
return 0;
137+
}
138+
139+
int mp_openamp_remoteproc_stop(struct remoteproc *rproc) {
140+
metal_log(METAL_LOG_DEBUG, "rproc_stop()\n");
141+
if (rproc->state == RPROC_RUNNING) {
142+
NVIC_SystemReset();
143+
}
144+
return 0;
145+
}
146+
147+
int mp_openamp_remoteproc_config(struct remoteproc *rproc, void *data) {
148+
metal_log(METAL_LOG_DEBUG, "rproc_config()\n");
149+
(void)rproc;
150+
return 0;
151+
}
152+
153+
void mp_openamp_remoteproc_remove(struct remoteproc *rproc) {
154+
metal_log(METAL_LOG_DEBUG, "rproc_remove()\n");
155+
(void)rproc;
156+
}
157+
158+
int mp_openamp_remoteproc_shutdown(struct remoteproc *rproc) {
159+
metal_log(METAL_LOG_DEBUG, "rproc_shutdown()\n");
160+
if (rproc->state == RPROC_RUNNING) {
161+
NVIC_SystemReset();
162+
}
163+
return 0;
164+
}

0 commit comments

Comments
 (0)
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