blob: c4eb35d57b356b737346d7fee347ec39e04e28d5 [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 George075d5972014-11-27 20:30:33 +000032#include "gccollect.h"
33#include "pyexec.h"
Damien George075d5972014-11-27 20:30:33 +000034#include MICROPY_HAL_H
Paul Sokolovskyf1700a52015-01-18 00:29:47 +020035#include "user_interface.h"
Damien George87c62502015-02-13 22:21:44 +000036#include "modpyb.h"
Damien George075d5972014-11-27 20:30:33 +000037
38STATIC mp_obj_t pyb_info(mp_uint_t n_args, const mp_obj_t *args) {
39 // print info about memory
40 {
41 printf("_text_start=%p\n", &_text_start);
42 printf("_text_end=%p\n", &_text_end);
43 printf("_irom0_text_start=%p\n", &_irom0_text_start);
44 printf("_irom0_text_end=%p\n", &_irom0_text_end);
45 printf("_data_start=%p\n", &_data_start);
46 printf("_data_end=%p\n", &_data_end);
47 printf("_rodata_start=%p\n", &_rodata_start);
48 printf("_rodata_end=%p\n", &_rodata_end);
49 printf("_bss_start=%p\n", &_bss_start);
50 printf("_bss_end=%p\n", &_bss_end);
51 printf("_heap_start=%p\n", &_heap_start);
52 printf("_heap_end=%p\n", &_heap_end);
53 }
54
55 // qstr info
56 {
57 mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
58 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
59 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);
60 }
61
62 // GC info
63 {
64 gc_info_t info;
65 gc_info(&info);
66 printf("GC:\n");
67 printf(" " UINT_FMT " total\n", info.total);
68 printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
69 printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
70 }
71
72 if (n_args == 1) {
73 // arg given means dump gc allocation table
74 gc_dump_alloc_table();
75 }
76
77 return mp_const_none;
78}
79STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info);
80
81STATIC mp_obj_t pyb_freq(mp_uint_t n_args, const mp_obj_t *args) {
82 if (n_args == 0) {
83 // get
84 return mp_obj_new_int(mp_hal_get_cpu_freq());
85 } else {
86 // set
87 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "can't change freq"));
88 }
89}
90STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_freq_obj, 0, 1, pyb_freq);
91
92STATIC mp_obj_t pyb_sync(void) {
93 //storage_flush();
94 return mp_const_none;
95}
96STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
97
98STATIC mp_obj_t pyb_millis(void) {
99 return MP_OBJ_NEW_SMALL_INT(HAL_GetTick());
100}
101STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
102
103STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
104 uint32_t startMillis = mp_obj_get_int(start);
105 uint32_t currMillis = HAL_GetTick();
106 return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff);
107}
108STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
109
110STATIC mp_obj_t pyb_micros(void) {
111 return MP_OBJ_NEW_SMALL_INT(0);
112}
113STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
114
115STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
116 uint32_t startMicros = mp_obj_get_int(start);
117 uint32_t currMicros = 0;
118 return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
119}
120STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
121
122STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
123 mp_int_t ms = mp_obj_get_int(ms_in);
124 if (ms >= 0) {
125 HAL_Delay(ms);
126 }
127 return mp_const_none;
128}
129STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
130
131STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
132 mp_int_t usec = mp_obj_get_int(usec_in);
133 if (usec >= 0) {
134 mp_hal_udelay(usec);
135 }
136 return mp_const_none;
137}
138STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
139
Paul Sokolovskyf1700a52015-01-18 00:29:47 +0200140STATIC mp_obj_t pyb_hard_reset(void) {
141 system_restart();
142 return mp_const_none;
143}
144STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_hard_reset_obj, pyb_hard_reset);
145
Damien George075d5972014-11-27 20:30:33 +0000146STATIC const mp_map_elem_t pyb_module_globals_table[] = {
147 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) },
148
149 { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj },
150 { MP_OBJ_NEW_QSTR(MP_QSTR_freq), (mp_obj_t)&pyb_freq_obj },
151
152 { MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj },
153 { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_millis), (mp_obj_t)&pyb_elapsed_millis_obj },
154 { MP_OBJ_NEW_QSTR(MP_QSTR_micros), (mp_obj_t)&pyb_micros_obj },
155 { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_micros), (mp_obj_t)&pyb_elapsed_micros_obj },
156 { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj },
157 { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj },
158 { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj },
Paul Sokolovskyf1700a52015-01-18 00:29:47 +0200159 { MP_OBJ_NEW_QSTR(MP_QSTR_hard_reset), (mp_obj_t)&pyb_hard_reset_obj },
Damien George87c62502015-02-13 22:21:44 +0000160
161 { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pyb_pin_type },
Damien George075d5972014-11-27 20:30:33 +0000162};
163
Damien George3b603f22014-11-29 14:39:27 +0000164STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
Damien George075d5972014-11-27 20:30:33 +0000165
166const mp_obj_module_t pyb_module = {
167 .base = { &mp_type_module },
168 .name = MP_QSTR_pyb,
169 .globals = (mp_obj_dict_t*)&pyb_module_globals,
170};