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 | |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | |
Damien George | fe7d542 | 2015-01-01 21:16:58 +0000 | [diff] [blame] | 29 | #include "py/nlr.h" |
| 30 | #include "py/obj.h" |
| 31 | #include "py/gc.h" |
Damien George | 731f359 | 2015-10-30 23:03:58 +0000 | [diff] [blame] | 32 | #include "py/mphal.h" |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 33 | #include "gccollect.h" |
Paul Sokolovsky | f1700a5 | 2015-01-18 00:29:47 +0200 | [diff] [blame] | 34 | #include "user_interface.h" |
Damien George | 87c6250 | 2015-02-13 22:21:44 +0000 | [diff] [blame] | 35 | #include "modpyb.h" |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 36 | |
| 37 | STATIC mp_obj_t pyb_info(mp_uint_t n_args, const mp_obj_t *args) { |
| 38 | // print info about memory |
| 39 | { |
| 40 | printf("_text_start=%p\n", &_text_start); |
| 41 | printf("_text_end=%p\n", &_text_end); |
| 42 | printf("_irom0_text_start=%p\n", &_irom0_text_start); |
| 43 | printf("_irom0_text_end=%p\n", &_irom0_text_end); |
| 44 | printf("_data_start=%p\n", &_data_start); |
| 45 | printf("_data_end=%p\n", &_data_end); |
| 46 | printf("_rodata_start=%p\n", &_rodata_start); |
| 47 | printf("_rodata_end=%p\n", &_rodata_end); |
| 48 | printf("_bss_start=%p\n", &_bss_start); |
| 49 | printf("_bss_end=%p\n", &_bss_end); |
| 50 | printf("_heap_start=%p\n", &_heap_start); |
| 51 | printf("_heap_end=%p\n", &_heap_end); |
| 52 | } |
| 53 | |
| 54 | // qstr info |
| 55 | { |
| 56 | mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes; |
| 57 | qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); |
| 58 | 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); |
| 59 | } |
| 60 | |
| 61 | // GC info |
| 62 | { |
| 63 | gc_info_t info; |
| 64 | gc_info(&info); |
| 65 | printf("GC:\n"); |
| 66 | printf(" " UINT_FMT " total\n", info.total); |
| 67 | printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free); |
| 68 | printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block); |
| 69 | } |
| 70 | |
| 71 | if (n_args == 1) { |
| 72 | // arg given means dump gc allocation table |
| 73 | gc_dump_alloc_table(); |
| 74 | } |
| 75 | |
| 76 | return mp_const_none; |
| 77 | } |
| 78 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info); |
| 79 | |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 80 | STATIC mp_obj_t pyb_sync(void) { |
| 81 | //storage_flush(); |
| 82 | return mp_const_none; |
| 83 | } |
| 84 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync); |
| 85 | |
| 86 | STATIC mp_obj_t pyb_millis(void) { |
Paul Sokolovsky | 6a09e7d | 2015-10-29 19:35:04 +0300 | [diff] [blame] | 87 | return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms()); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 88 | } |
| 89 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis); |
| 90 | |
| 91 | STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) { |
| 92 | uint32_t startMillis = mp_obj_get_int(start); |
Paul Sokolovsky | 6a09e7d | 2015-10-29 19:35:04 +0300 | [diff] [blame] | 93 | uint32_t currMillis = mp_hal_ticks_ms(); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 94 | return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff); |
| 95 | } |
| 96 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis); |
| 97 | |
| 98 | STATIC mp_obj_t pyb_micros(void) { |
Josef Gajdusek | 800d5cd | 2015-05-10 17:54:09 +0200 | [diff] [blame] | 99 | return MP_OBJ_NEW_SMALL_INT(system_get_time()); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 100 | } |
| 101 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros); |
| 102 | |
| 103 | STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) { |
| 104 | uint32_t startMicros = mp_obj_get_int(start); |
Josef Gajdusek | 800d5cd | 2015-05-10 17:54:09 +0200 | [diff] [blame] | 105 | uint32_t currMicros = system_get_time(); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 106 | return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff); |
| 107 | } |
| 108 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros); |
| 109 | |
| 110 | STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) { |
| 111 | mp_int_t ms = mp_obj_get_int(ms_in); |
| 112 | if (ms >= 0) { |
Paul Sokolovsky | ebd9f55 | 2015-10-29 13:03:44 +0300 | [diff] [blame] | 113 | mp_hal_delay_ms(ms); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 114 | } |
| 115 | return mp_const_none; |
| 116 | } |
| 117 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay); |
| 118 | |
| 119 | STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) { |
| 120 | mp_int_t usec = mp_obj_get_int(usec_in); |
| 121 | if (usec >= 0) { |
Paul Sokolovsky | 5699fc9 | 2015-10-29 02:06:13 +0300 | [diff] [blame] | 122 | mp_hal_delay_us(usec); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 123 | } |
| 124 | return mp_const_none; |
| 125 | } |
| 126 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay); |
| 127 | |
Paul Sokolovsky | f1700a5 | 2015-01-18 00:29:47 +0200 | [diff] [blame] | 128 | STATIC mp_obj_t pyb_hard_reset(void) { |
| 129 | system_restart(); |
| 130 | return mp_const_none; |
| 131 | } |
| 132 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_hard_reset_obj, pyb_hard_reset); |
| 133 | |
Josef Gajdusek | 286ced4 | 2015-05-15 12:00:39 +0200 | [diff] [blame] | 134 | STATIC mp_obj_t pyb_unique_id(void) { |
| 135 | uint32_t id = system_get_chip_id(); |
| 136 | return mp_obj_new_bytes((byte *)&id, sizeof(id)); |
| 137 | } |
| 138 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id); |
| 139 | |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 140 | STATIC const mp_map_elem_t pyb_module_globals_table[] = { |
| 141 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) }, |
| 142 | |
| 143 | { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj }, |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 144 | |
| 145 | { MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj }, |
| 146 | { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_millis), (mp_obj_t)&pyb_elapsed_millis_obj }, |
| 147 | { MP_OBJ_NEW_QSTR(MP_QSTR_micros), (mp_obj_t)&pyb_micros_obj }, |
| 148 | { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_micros), (mp_obj_t)&pyb_elapsed_micros_obj }, |
| 149 | { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj }, |
| 150 | { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj }, |
| 151 | { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj }, |
Paul Sokolovsky | f1700a5 | 2015-01-18 00:29:47 +0200 | [diff] [blame] | 152 | { MP_OBJ_NEW_QSTR(MP_QSTR_hard_reset), (mp_obj_t)&pyb_hard_reset_obj }, |
Damien George | 87c6250 | 2015-02-13 22:21:44 +0000 | [diff] [blame] | 153 | |
| 154 | { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pyb_pin_type }, |
Josef Gajdusek | 25a8a42 | 2015-05-18 18:35:56 +0200 | [diff] [blame] | 155 | { MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type }, |
Damien George | de8b585 | 2015-06-22 23:03:17 +0100 | [diff] [blame] | 156 | { MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_type }, |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Damien George | 3b603f2 | 2014-11-29 14:39:27 +0000 | [diff] [blame] | 159 | STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table); |
Damien George | 075d597 | 2014-11-27 20:30:33 +0000 | [diff] [blame] | 160 | |
| 161 | const mp_obj_module_t pyb_module = { |
| 162 | .base = { &mp_type_module }, |
| 163 | .name = MP_QSTR_pyb, |
| 164 | .globals = (mp_obj_dict_t*)&pyb_module_globals, |
| 165 | }; |