Skip to content

Commit 38c8b69

Browse files
committed
ports/stm32: Add libmetal port.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent c4c8b45 commit 38c8b69

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

ports/stm32/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ endif
493493

494494
endif # MICROPY_PY_BLUETOOTH
495495

496+
ifeq ($(MICROPY_PY_OPENAMP),1)
497+
SRC_C += metal/metal_port.c
498+
endif
499+
496500
# SRC_O should be placed first to work around this LTO bug with binutils <2.35:
497501
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83967
498502
OBJ += $(addprefix $(BUILD)/, $(SRC_O))

ports/stm32/metal/metal_port.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 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 stm32 port.
27+
*/
28+
29+
#include "metal/sys.h"
30+
#include "metal/utilities.h"
31+
#include "metal/device.h"
32+
33+
struct metal_state _metal;
34+
static mp_sched_node_t rproc_notify_node;
35+
36+
int metal_sys_init(const struct metal_init_params *params) {
37+
metal_unused(params);
38+
39+
// Clear HSEM pending IRQ.
40+
HSEM_COMMON->ICR |= (uint32_t)__HAL_HSEM_SEMID_TO_MASK(METAL_HSEM_ID_MASTER);
41+
HAL_NVIC_ClearPendingIRQ(HSEM1_IRQn);
42+
43+
// Enable and configure HSEM.
44+
__HAL_RCC_HSEM_CLK_ENABLE();
45+
HAL_NVIC_SetPriority(HSEM1_IRQn, 0, 1);
46+
HAL_NVIC_EnableIRQ(HSEM1_IRQn);
47+
HAL_HSEM_ActivateNotification(__HAL_HSEM_SEMID_TO_MASK(METAL_HSEM_ID_MASTER));
48+
49+
metal_bus_register(&metal_generic_bus);
50+
return 0;
51+
}
52+
53+
void metal_sys_finish(void) {
54+
metal_bus_unregister(&metal_generic_bus);
55+
}
56+
57+
unsigned int sys_irq_save_disable(void) {
58+
return disable_irq();
59+
}
60+
61+
void sys_irq_restore_enable(unsigned int state) {
62+
enable_irq(state);
63+
}
64+
65+
void sys_irq_enable(unsigned int vector) {
66+
metal_unused(vector);
67+
}
68+
69+
void sys_irq_disable(unsigned int vector) {
70+
metal_unused(vector);
71+
}
72+
73+
void *metal_machine_io_mem_map(void *va, metal_phys_addr_t pa,
74+
size_t size, unsigned int flags) {
75+
metal_unused(pa);
76+
metal_unused(size);
77+
metal_unused(flags);
78+
return va;
79+
}
80+
81+
void metal_machine_cache_flush(void *addr, unsigned int len) {
82+
SCB_CleanDCache_by_Addr((uint32_t *)addr, len);
83+
}
84+
85+
void metal_machine_cache_invalidate(void *addr, unsigned int len) {
86+
SCB_CleanInvalidateDCache_by_Addr((uint32_t *)addr, len);
87+
}
88+
89+
int __metal_sleep_usec(unsigned int usec) {
90+
mp_hal_delay_us(usec);
91+
return 0;
92+
}
93+
94+
int metal_rproc_notify(void *priv, uint32_t id) {
95+
HAL_HSEM_FastTake(METAL_HSEM_ID_REMOTE);
96+
HAL_HSEM_Release(METAL_HSEM_ID_REMOTE, 0);
97+
return 0;
98+
}
99+
100+
void HSEM1_IRQHandler(void) {
101+
HAL_HSEM_IRQHandler();
102+
mp_sched_schedule_node(&rproc_notify_node, metal_rproc_notified);
103+
HAL_HSEM_ActivateNotification(__HAL_HSEM_SEMID_TO_MASK(METAL_HSEM_ID_MASTER));
104+
}

ports/stm32/metal/metal_port.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 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 stm32 port.
27+
*/
28+
#ifndef MICROPY_INCLUDED_STM32_METAL_PORT_H
29+
#define MICROPY_INCLUDED_STM32_METAL_PORT_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_PROCESSOR_CPU_H "metal/processor/arm/cpu.h"
39+
#define METAL_PROCESSOR_ATOMIC_H "metal/processor/arm/atomic.h"
40+
#define METAL_MAX_DEVICE_REGIONS 2
41+
42+
#define METAL_HSEM_ID_REMOTE 0
43+
#define METAL_HSEM_ID_MASTER 1
44+
45+
// Note set to 1 to enable log output.
46+
#define METAL_LOG_HANDLER_ENABLE 0
47+
48+
#define metal_cpu_yield()
49+
#define metal_sys_assert(cond) assert(cond)
50+
#define metal_generic_default_poll() MICROPY_EVENT_POLL_HOOK
51+
52+
int metal_rproc_notify(void *priv, uint32_t id);
53+
extern void metal_rproc_notified(mp_sched_node_t *node);
54+
55+
#endif // MICROPY_INCLUDED_STM32_METAL_PORT_H

0 commit comments

Comments
 (0)