blob: be64c1909fa96816ea0afdd87380a76a7c6e4468 [file] [log] [blame]
Damien George075d5972014-11-27 20:30:33 +00001/*
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 George075d5972014-11-27 20:30:33 +000027#include <stdio.h>
28
Damien Georgefe7d5422015-01-01 21:16:58 +000029#include "py/nlr.h"
30#include "py/obj.h"
31#include "py/gc.h"
Damien George731f3592015-10-30 23:03:58 +000032#include "py/mphal.h"
Damien George075d5972014-11-27 20:30:33 +000033#include "gccollect.h"
Paul Sokolovskyf1700a52015-01-18 00:29:47 +020034#include "user_interface.h"
Damien George87c62502015-02-13 22:21:44 +000035#include "modpyb.h"
Damien George075d5972014-11-27 20:30:33 +000036
37STATIC 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}
78STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info);
79
Damien George075d5972014-11-27 20:30:33 +000080STATIC mp_obj_t pyb_sync(void) {
81 //storage_flush();
82 return mp_const_none;
83}
84STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
85
86STATIC mp_obj_t pyb_millis(void) {
Paul Sokolovsky6a09e7d2015-10-29 19:35:04 +030087 return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms());
Damien George075d5972014-11-27 20:30:33 +000088}
89STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
90
91STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
92 uint32_t startMillis = mp_obj_get_int(start);
Paul Sokolovsky6a09e7d2015-10-29 19:35:04 +030093 uint32_t currMillis = mp_hal_ticks_ms();
Damien George075d5972014-11-27 20:30:33 +000094 return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff);
95}
96STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
97
98STATIC mp_obj_t pyb_micros(void) {
Josef Gajdusek800d5cd2015-05-10 17:54:09 +020099 return MP_OBJ_NEW_SMALL_INT(system_get_time());
Damien George075d5972014-11-27 20:30:33 +0000100}
101STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
102
103STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
104 uint32_t startMicros = mp_obj_get_int(start);
Josef Gajdusek800d5cd2015-05-10 17:54:09 +0200105 uint32_t currMicros = system_get_time();
Damien George075d5972014-11-27 20:30:33 +0000106 return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
107}
108STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
109
110STATIC 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 Sokolovskyebd9f552015-10-29 13:03:44 +0300113 mp_hal_delay_ms(ms);
Damien George075d5972014-11-27 20:30:33 +0000114 }
115 return mp_const_none;
116}
117STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
118
119STATIC 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 Sokolovsky5699fc92015-10-29 02:06:13 +0300122 mp_hal_delay_us(usec);
Damien George075d5972014-11-27 20:30:33 +0000123 }
124 return mp_const_none;
125}
126STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
127
Paul Sokolovskyf1700a52015-01-18 00:29:47 +0200128STATIC mp_obj_t pyb_hard_reset(void) {
129 system_restart();
130 return mp_const_none;
131}
132STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_hard_reset_obj, pyb_hard_reset);
133
Josef Gajdusek286ced42015-05-15 12:00:39 +0200134STATIC 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}
138STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
139
Damien George075d5972014-11-27 20:30:33 +0000140STATIC 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 George075d5972014-11-27 20:30:33 +0000144
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 Sokolovskyf1700a52015-01-18 00:29:47 +0200152 { MP_OBJ_NEW_QSTR(MP_QSTR_hard_reset), (mp_obj_t)&pyb_hard_reset_obj },
Damien George87c62502015-02-13 22:21:44 +0000153
154 { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pyb_pin_type },
Josef Gajdusek25a8a422015-05-18 18:35:56 +0200155 { MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type },
Damien Georgede8b5852015-06-22 23:03:17 +0100156 { MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_type },
Damien George075d5972014-11-27 20:30:33 +0000157};
158
Damien George3b603f22014-11-29 14:39:27 +0000159STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
Damien George075d5972014-11-27 20:30:33 +0000160
161const 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};