Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2014 Damien P. George |
| 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 | |
| 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | #include "mpconfig.h" |
| 31 | #include "misc.h" |
| 32 | #include "nlr.h" |
| 33 | #include "qstr.h" |
| 34 | #include "obj.h" |
| 35 | #include "gc.h" |
| 36 | #include "gccollect.h" |
| 37 | #include "pyexec.h" |
| 38 | #include "pybstdio.h" |
| 39 | #include MICROPY_HAL_H |
| 40 | |
| 41 | STATIC mp_obj_t pyb_info(mp_uint_t n_args, const mp_obj_t *args) { |
| 42 | // print info about memory |
| 43 | { |
| 44 | printf("_text_start=%p\n", &_text_start); |
| 45 | printf("_text_end=%p\n", &_text_end); |
| 46 | printf("_irom0_text_start=%p\n", &_irom0_text_start); |
| 47 | printf("_irom0_text_end=%p\n", &_irom0_text_end); |
| 48 | printf("_data_start=%p\n", &_data_start); |
| 49 | printf("_data_end=%p\n", &_data_end); |
| 50 | printf("_rodata_start=%p\n", &_rodata_start); |
| 51 | printf("_rodata_end=%p\n", &_rodata_end); |
| 52 | printf("_bss_start=%p\n", &_bss_start); |
| 53 | printf("_bss_end=%p\n", &_bss_end); |
| 54 | printf("_heap_start=%p\n", &_heap_start); |
| 55 | printf("_heap_end=%p\n", &_heap_end); |
| 56 | } |
| 57 | |
| 58 | // qstr info |
| 59 | { |
| 60 | mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes; |
| 61 | qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); |
| 62 | printf("qstr:\n n_pool=" UINT_FMT "\n n_qstr=" UINT_FMT "\n n_str_data_bytes=" UINT_FMT "\n n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes); |
| 63 | } |
| 64 | |
| 65 | // GC info |
| 66 | { |
| 67 | gc_info_t info; |
| 68 | gc_info(&info); |
| 69 | printf("GC:\n"); |
| 70 | printf(" " UINT_FMT " total\n", info.total); |
| 71 | printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free); |
| 72 | printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block); |
| 73 | } |
| 74 | |
| 75 | if (n_args == 1) { |
| 76 | // arg given means dump gc allocation table |
| 77 | gc_dump_alloc_table(); |
| 78 | } |
| 79 | |
| 80 | return mp_const_none; |
| 81 | } |
| 82 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info); |
| 83 | |
| 84 | STATIC mp_obj_t pyb_freq(mp_uint_t n_args, const mp_obj_t *args) { |
| 85 | if (n_args == 0) { |
| 86 | // get |
| 87 | return mp_obj_new_int(mp_hal_get_cpu_freq()); |
| 88 | } else { |
| 89 | // set |
| 90 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "can't change freq")); |
| 91 | } |
| 92 | } |
| 93 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_freq_obj, 0, 1, pyb_freq); |
| 94 | |
| 95 | STATIC mp_obj_t pyb_sync(void) { |
| 96 | //storage_flush(); |
| 97 | return mp_const_none; |
| 98 | } |
| 99 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync); |
| 100 | |
| 101 | STATIC mp_obj_t pyb_millis(void) { |
| 102 | return MP_OBJ_NEW_SMALL_INT(HAL_GetTick()); |
| 103 | } |
| 104 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis); |
| 105 | |
| 106 | STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) { |
| 107 | uint32_t startMillis = mp_obj_get_int(start); |
| 108 | uint32_t currMillis = HAL_GetTick(); |
| 109 | return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff); |
| 110 | } |
| 111 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis); |
| 112 | |
| 113 | STATIC mp_obj_t pyb_micros(void) { |
| 114 | return MP_OBJ_NEW_SMALL_INT(0); |
| 115 | } |
| 116 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros); |
| 117 | |
| 118 | STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) { |
| 119 | uint32_t startMicros = mp_obj_get_int(start); |
| 120 | uint32_t currMicros = 0; |
| 121 | return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff); |
| 122 | } |
| 123 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros); |
| 124 | |
| 125 | STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) { |
| 126 | mp_int_t ms = mp_obj_get_int(ms_in); |
| 127 | if (ms >= 0) { |
| 128 | HAL_Delay(ms); |
| 129 | } |
| 130 | return mp_const_none; |
| 131 | } |
| 132 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay); |
| 133 | |
| 134 | STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) { |
| 135 | mp_int_t usec = mp_obj_get_int(usec_in); |
| 136 | if (usec >= 0) { |
| 137 | mp_hal_udelay(usec); |
| 138 | } |
| 139 | return mp_const_none; |
| 140 | } |
| 141 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay); |
| 142 | |
| 143 | STATIC const mp_map_elem_t pyb_module_globals_table[] = { |
| 144 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) }, |
| 145 | |
| 146 | { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj }, |
| 147 | { MP_OBJ_NEW_QSTR(MP_QSTR_freq), (mp_obj_t)&pyb_freq_obj }, |
| 148 | |
| 149 | { MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj }, |
| 150 | { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_millis), (mp_obj_t)&pyb_elapsed_millis_obj }, |
| 151 | { MP_OBJ_NEW_QSTR(MP_QSTR_micros), (mp_obj_t)&pyb_micros_obj }, |
| 152 | { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_micros), (mp_obj_t)&pyb_elapsed_micros_obj }, |
| 153 | { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj }, |
| 154 | { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj }, |
| 155 | { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj }, |
| 156 | }; |
| 157 | |
Damien George | 3b603f2 | 2014-11-29 14:39:27 +0000 | [diff] [blame^] | 158 | STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 159 | |
| 160 | const mp_obj_module_t pyb_module = { |
| 161 | .base = { &mp_type_module }, |
| 162 | .name = MP_QSTR_pyb, |
| 163 | .globals = (mp_obj_dict_t*)&pyb_module_globals, |
| 164 | }; |