blob: a9daef9c7e5a69794fa4e1f3e01f83832f95b708 [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
80STATIC mp_obj_t pyb_freq(mp_uint_t n_args, const mp_obj_t *args) {
81 if (n_args == 0) {
82 // get
Josef Gajdusek492fd5c2015-05-15 09:56:41 +020083 return mp_obj_new_int(system_get_cpu_freq() * 1000000);
Damien George075d5972014-11-27 20:30:33 +000084 } else {
85 // set
Josef Gajdusek492fd5c2015-05-15 09:56:41 +020086 mp_int_t freq = mp_obj_get_int(args[0]) / 1000000;
87 if (freq != 80 && freq != 160) {
88 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
89 "frequency can only be either 80Mhz or 160MHz"));
90 }
91 system_update_cpu_freq(freq);
92 return mp_const_none;
Damien George075d5972014-11-27 20:30:33 +000093 }
94}
95STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_freq_obj, 0, 1, pyb_freq);
96
97STATIC mp_obj_t pyb_sync(void) {
98 //storage_flush();
99 return mp_const_none;
100}
101STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
102
103STATIC mp_obj_t pyb_millis(void) {
Paul Sokolovsky6a09e7d2015-10-29 19:35:04 +0300104 return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms());
Damien George075d5972014-11-27 20:30:33 +0000105}
106STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
107
108STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
109 uint32_t startMillis = mp_obj_get_int(start);
Paul Sokolovsky6a09e7d2015-10-29 19:35:04 +0300110 uint32_t currMillis = mp_hal_ticks_ms();
Damien George075d5972014-11-27 20:30:33 +0000111 return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff);
112}
113STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
114
115STATIC mp_obj_t pyb_micros(void) {
Josef Gajdusek800d5cd2015-05-10 17:54:09 +0200116 return MP_OBJ_NEW_SMALL_INT(system_get_time());
Damien George075d5972014-11-27 20:30:33 +0000117}
118STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
119
120STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
121 uint32_t startMicros = mp_obj_get_int(start);
Josef Gajdusek800d5cd2015-05-10 17:54:09 +0200122 uint32_t currMicros = system_get_time();
Damien George075d5972014-11-27 20:30:33 +0000123 return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
124}
125STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
126
127STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
128 mp_int_t ms = mp_obj_get_int(ms_in);
129 if (ms >= 0) {
Paul Sokolovskyebd9f552015-10-29 13:03:44 +0300130 mp_hal_delay_ms(ms);
Damien George075d5972014-11-27 20:30:33 +0000131 }
132 return mp_const_none;
133}
134STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
135
136STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
137 mp_int_t usec = mp_obj_get_int(usec_in);
138 if (usec >= 0) {
Paul Sokolovsky5699fc92015-10-29 02:06:13 +0300139 mp_hal_delay_us(usec);
Damien George075d5972014-11-27 20:30:33 +0000140 }
141 return mp_const_none;
142}
143STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
144
Paul Sokolovskyf1700a52015-01-18 00:29:47 +0200145STATIC mp_obj_t pyb_hard_reset(void) {
146 system_restart();
147 return mp_const_none;
148}
149STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_hard_reset_obj, pyb_hard_reset);
150
Josef Gajdusek286ced42015-05-15 12:00:39 +0200151STATIC mp_obj_t pyb_unique_id(void) {
152 uint32_t id = system_get_chip_id();
153 return mp_obj_new_bytes((byte *)&id, sizeof(id));
154}
155STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
156
Damien George075d5972014-11-27 20:30:33 +0000157STATIC const mp_map_elem_t pyb_module_globals_table[] = {
158 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) },
159
160 { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj },
161 { MP_OBJ_NEW_QSTR(MP_QSTR_freq), (mp_obj_t)&pyb_freq_obj },
162
163 { MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj },
164 { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_millis), (mp_obj_t)&pyb_elapsed_millis_obj },
165 { MP_OBJ_NEW_QSTR(MP_QSTR_micros), (mp_obj_t)&pyb_micros_obj },
166 { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_micros), (mp_obj_t)&pyb_elapsed_micros_obj },
167 { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj },
168 { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj },
169 { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj },
Paul Sokolovskyf1700a52015-01-18 00:29:47 +0200170 { MP_OBJ_NEW_QSTR(MP_QSTR_hard_reset), (mp_obj_t)&pyb_hard_reset_obj },
Damien George87c62502015-02-13 22:21:44 +0000171
172 { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pyb_pin_type },
Josef Gajdusek25a8a422015-05-18 18:35:56 +0200173 { MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type },
Damien Georgede8b5852015-06-22 23:03:17 +0100174 { MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_type },
Damien George075d5972014-11-27 20:30:33 +0000175};
176
Damien George3b603f22014-11-29 14:39:27 +0000177STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
Damien George075d5972014-11-27 20:30:33 +0000178
179const mp_obj_module_t pyb_module = {
180 .base = { &mp_type_module },
181 .name = MP_QSTR_pyb,
182 .globals = (mp_obj_dict_t*)&pyb_module_globals,
183};