| 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 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 | #include <stdio.h> |
| 29 | #include <stdint.h> |
| 30 | |
| 31 | #include "py/runtime.h" |
| 32 | #include "py/obj.h" |
| 33 | |
| 34 | #include "extmod/machine_mem.h" |
| 35 | #include "extmod/machine_pinbase.h" |
| 36 | #include "extmod/machine_signal.h" |
| 37 | #include "extmod/machine_pulse.h" |
| 38 | |
| 39 | #if MICROPY_PLAT_DEV_MEM |
| 40 | #include <errno.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <sys/mman.h> |
| 43 | #define MICROPY_PAGE_SIZE 4096 |
| 44 | #define MICROPY_PAGE_MASK (MICROPY_PAGE_SIZE - 1) |
| 45 | #endif |
| 46 | |
| 47 | #if MICROPY_PY_MACHINE |
| 48 | |
| 49 | uintptr_t mod_machine_mem_get_addr(mp_obj_t addr_o, uint align) { |
| 50 | uintptr_t addr = mp_obj_get_int_truncated(addr_o); |
| 51 | if ((addr & (align - 1)) != 0) { |
| 52 | mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("address %08x is not aligned to %d bytes" ), addr, align); |
| 53 | } |
| 54 | #if MICROPY_PLAT_DEV_MEM |
| 55 | { |
| 56 | // Not thread-safe |
| 57 | static int fd; |
| 58 | static uintptr_t last_base = (uintptr_t)-1; |
| 59 | static uintptr_t map_page; |
| 60 | if (!fd) { |
| 61 | int _fd = open("/dev/mem" , O_RDWR | O_SYNC); |
| 62 | if (_fd == -1) { |
| 63 | mp_raise_OSError(errno); |
| 64 | } |
| 65 | fd = _fd; |
| 66 | } |
| 67 | |
| 68 | uintptr_t cur_base = addr & ~MICROPY_PAGE_MASK; |
| 69 | if (cur_base != last_base) { |
| 70 | map_page = (uintptr_t)mmap(NULL, MICROPY_PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, cur_base); |
| 71 | last_base = cur_base; |
| 72 | } |
| 73 | addr = map_page + (addr & MICROPY_PAGE_MASK); |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | return addr; |
| 78 | } |
| 79 | |
| 80 | #ifdef MICROPY_UNIX_MACHINE_IDLE |
| 81 | STATIC mp_obj_t machine_idle(void) { |
| 82 | MICROPY_UNIX_MACHINE_IDLE |
| 83 | return mp_const_none; |
| 84 | } |
| 85 | MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle); |
| 86 | #endif |
| 87 | |
| 88 | STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { |
| 89 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, |
| 90 | |
| 91 | { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, |
| 92 | { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, |
| 93 | { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, |
| 94 | |
| 95 | #ifdef MICROPY_UNIX_MACHINE_IDLE |
| 96 | { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, |
| 97 | #endif |
| 98 | |
| 99 | { MP_ROM_QSTR(MP_QSTR_PinBase), MP_ROM_PTR(&machine_pinbase_type) }, |
| 100 | { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, |
| 101 | #if MICROPY_PY_MACHINE_PULSE |
| 102 | { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, |
| 103 | #endif |
| 104 | }; |
| 105 | |
| 106 | STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); |
| 107 | |
| 108 | const mp_obj_module_t mp_module_machine = { |
| 109 | .base = { &mp_type_module }, |
| 110 | .globals = (mp_obj_dict_t *)&machine_module_globals, |
| 111 | }; |
| 112 | |
| 113 | #endif // MICROPY_PY_MACHINE |
| 114 | |