Branch data Line data Source code
1 : : /* 2 : : * This file is part of the MicroPython project, http://micropython.org/ 3 : : * 4 : : * The MIT License (MIT) 5 : : * 6 : : * Copyright (c) 2013-2023 Damien P. George 7 : : * Copyright (c) 2015 Paul Sokolovsky 8 : : * 9 : : * Permission is hereby granted, free of charge, to any person obtaining a copy 10 : : * of this software and associated documentation files (the "Software"), to deal 11 : : * in the Software without restriction, including without limitation the rights 12 : : * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 : : * copies of the Software, and to permit persons to whom the Software is 14 : : * furnished to do so, subject to the following conditions: 15 : : * 16 : : * The above copyright notice and this permission notice shall be included in 17 : : * all copies or substantial portions of the Software. 18 : : * 19 : : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 : : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 : : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 : : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 : : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 : : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 : : * THE SOFTWARE. 26 : : */ 27 : : 28 : : // This file is never compiled standalone, it's included directly from 29 : : // extmod/modmachine.c via MICROPY_PY_MACHINE_INCLUDEFILE. 30 : : 31 : : #if MICROPY_PLAT_DEV_MEM 32 : : #include <errno.h> 33 : : #include <fcntl.h> 34 : : #include <sys/mman.h> 35 : : #define MICROPY_PAGE_SIZE 4096 36 : : #define MICROPY_PAGE_MASK (MICROPY_PAGE_SIZE - 1) 37 : : #endif 38 : : 39 : 12 : uintptr_t mod_machine_mem_get_addr(mp_obj_t addr_o, uint align) { 40 : 12 : uintptr_t addr = mp_obj_get_int_truncated(addr_o); 41 [ + - ]: 4 : if ((addr & (align - 1)) != 0) { 42 : 4 : mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("address %08x is not aligned to %d bytes"), addr, align); 43 : : } 44 : : #if MICROPY_PLAT_DEV_MEM 45 : : { 46 : : // Not thread-safe 47 : 0 : static int fd; 48 : 0 : static uintptr_t last_base = (uintptr_t)-1; 49 : 0 : static uintptr_t map_page; 50 [ # # ]: 0 : if (!fd) { 51 : 0 : int _fd = open("/dev/mem", O_RDWR | O_SYNC); 52 [ # # ]: 0 : if (_fd == -1) { 53 : 0 : mp_raise_OSError(errno); 54 : : } 55 : 0 : fd = _fd; 56 : : } 57 : : 58 : 0 : uintptr_t cur_base = addr & ~MICROPY_PAGE_MASK; 59 [ # # ]: 0 : if (cur_base != last_base) { 60 : 0 : map_page = (uintptr_t)mmap(NULL, MICROPY_PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, cur_base); 61 : 0 : last_base = cur_base; 62 : : } 63 : 0 : addr = map_page + (addr & MICROPY_PAGE_MASK); 64 : : } 65 : : #endif 66 : : 67 : 0 : return addr; 68 : : } 69 : : 70 : 0 : static void mp_machine_idle(void) { 71 : : #ifdef MICROPY_UNIX_MACHINE_IDLE 72 : 0 : MICROPY_UNIX_MACHINE_IDLE 73 : : #else 74 : : // Do nothing. 75 : : #endif 76 : 0 : }