|
| 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 | +} |
0 commit comments