1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 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 #include <mk20dx128.h>
30 #include "Arduino.h"
31
32 #include "py/obj.h"
33 #include "py/gc.h"
34 #include "py/mphal.h"
35
36 #include "shared/runtime/pyexec.h"
37
38 #include "gccollect.h"
39 #include "systick.h"
40 #include "led.h"
41 #include "pin.h"
42 #include "timer.h"
43 #include "extint.h"
44 #include "usrsw.h"
45 #include "rng.h"
46 #include "uart.h"
47 #include "storage.h"
48 #include "sdcard.h"
49 #include "accel.h"
50 #include "servo.h"
51 #include "dac.h"
52 #include "usb.h"
53 #include "portmodules.h"
54 #include "modmachine.h"
55
56 /// \module pyb - functions related to the pyboard
57 ///
58 /// The `pyb` module contains specific functions related to the pyboard.
59
60 /// \function bootloader()
61 /// Activate the bootloader without BOOT* pins.
pyb_bootloader(void)62 STATIC mp_obj_t pyb_bootloader(void) {
63 printf("bootloader command not current supported\n");
64 return mp_const_none;
65 }
66 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_bootloader_obj, pyb_bootloader);
67
68 /// \function info([dump_alloc_table])
69 /// Print out lots of information about the board.
pyb_info(uint n_args,const mp_obj_t * args)70 STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
71 // get and print unique id; 96 bits
72 {
73 byte *id = (byte *)0x40048058;
74 printf("ID=%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x\n", id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11]);
75 }
76
77 // get and print clock speeds
78 printf("CPU=%u\nBUS=%u\nMEM=%u\n", F_CPU, F_BUS, F_MEM);
79
80 // to print info about memory
81 {
82 printf("_etext=%p\n", &_etext);
83 printf("_sidata=%p\n", &_sidata);
84 printf("_sdata=%p\n", &_sdata);
85 printf("_edata=%p\n", &_edata);
86 printf("_sbss=%p\n", &_sbss);
87 printf("_ebss=%p\n", &_ebss);
88 printf("_estack=%p\n", &_estack);
89 printf("_ram_start=%p\n", &_ram_start);
90 printf("_heap_start=%p\n", &_heap_start);
91 printf("_heap_end=%p\n", &_heap_end);
92 printf("_ram_end=%p\n", &_ram_end);
93 }
94
95 // qstr info
96 {
97 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
98 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
99 printf("qstr:\n n_pool=%u\n n_qstr=%u\n n_str_data_bytes=%u\n n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
100 }
101
102 // GC info
103 {
104 gc_info_t info;
105 gc_info(&info);
106 printf("GC:\n");
107 printf(" " UINT_FMT " total\n", info.total);
108 printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
109 printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
110 }
111
112 if (n_args == 1) {
113 // arg given means dump gc allocation table
114 gc_dump_alloc_table();
115 }
116
117 return mp_const_none;
118 }
119 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info);
120
121 /// \function unique_id()
122 /// Returns a string of 12 bytes (96 bits), which is the unique ID for the MCU.
pyb_unique_id(void)123 STATIC mp_obj_t pyb_unique_id(void) {
124 byte *id = (byte *)0x40048058;
125 return mp_obj_new_bytes(id, 12);
126 }
127 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
128
129 /// \function freq()
130 /// Return a tuple of clock frequencies: (SYSCLK, HCLK, PCLK1, PCLK2).
131 // TODO should also be able to set frequency via this function
pyb_freq(void)132 STATIC mp_obj_t pyb_freq(void) {
133 mp_obj_t tuple[3] = {
134 mp_obj_new_int(F_CPU),
135 mp_obj_new_int(F_BUS),
136 mp_obj_new_int(F_MEM),
137 };
138 return mp_obj_new_tuple(3, tuple);
139 }
140 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_freq_obj, pyb_freq);
141
142 /// \function sync()
143 /// Sync all file systems.
pyb_sync(void)144 STATIC mp_obj_t pyb_sync(void) {
145 printf("sync not currently implemented\n");
146 return mp_const_none;
147 }
148 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
149
150 /// \function millis()
151 /// Returns the number of milliseconds since the board was last reset.
152 ///
153 /// The result is always a MicroPython smallint (31-bit signed number), so
154 /// after 2^30 milliseconds (about 12.4 days) this will start to return
155 /// negative numbers.
pyb_millis(void)156 STATIC mp_obj_t pyb_millis(void) {
157 // We want to "cast" the 32 bit unsigned into a small-int. This means
158 // copying the MSB down 1 bit (extending the sign down), which is
159 // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
160 return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms());
161 }
162 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
163
164 /// \function elapsed_millis(start)
165 /// Returns the number of milliseconds which have elapsed since `start`.
166 ///
167 /// This function takes care of counter wrap, and always returns a positive
168 /// number. This means it can be used to measure periods upto about 12.4 days.
169 ///
170 /// Example:
171 /// start = pyb.millis()
172 /// while pyb.elapsed_millis(start) < 1000:
173 /// # Perform some operation
pyb_elapsed_millis(mp_obj_t start)174 STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
175 uint32_t startMillis = mp_obj_get_int(start);
176 uint32_t currMillis = mp_hal_ticks_ms();
177 return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x3fffffff);
178 }
179 STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
180
181 /// \function micros()
182 /// Returns the number of microseconds since the board was last reset.
183 ///
184 /// The result is always a MicroPython smallint (31-bit signed number), so
185 /// after 2^30 microseconds (about 17.8 minutes) this will start to return
186 /// negative numbers.
pyb_micros(void)187 STATIC mp_obj_t pyb_micros(void) {
188 // We want to "cast" the 32 bit unsigned into a small-int. This means
189 // copying the MSB down 1 bit (extending the sign down), which is
190 // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
191 return MP_OBJ_NEW_SMALL_INT(micros());
192 }
193 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
194
195 /// \function elapsed_micros(start)
196 /// Returns the number of microseconds which have elapsed since `start`.
197 ///
198 /// This function takes care of counter wrap, and always returns a positive
199 /// number. This means it can be used to measure periods upto about 17.8 minutes.
200 ///
201 /// Example:
202 /// start = pyb.micros()
203 /// while pyb.elapsed_micros(start) < 1000:
204 /// # Perform some operation
pyb_elapsed_micros(mp_obj_t start)205 STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
206 uint32_t startMicros = mp_obj_get_int(start);
207 uint32_t currMicros = micros();
208 return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
209 }
210 STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
211
212 /// \function delay(ms)
213 /// Delay for the given number of milliseconds.
pyb_delay(mp_obj_t ms_in)214 STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
215 mp_int_t ms = mp_obj_get_int(ms_in);
216 if (ms >= 0) {
217 mp_hal_delay_ms(ms);
218 }
219 return mp_const_none;
220 }
221 STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
222
223 /// \function udelay(us)
224 /// Delay for the given number of microseconds.
pyb_udelay(mp_obj_t usec_in)225 STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
226 mp_int_t usec = mp_obj_get_int(usec_in);
227 delayMicroseconds(usec);
228 return mp_const_none;
229 }
230 STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
231
pyb_wfi(void)232 STATIC mp_obj_t pyb_wfi(void) {
233 __WFI();
234 return mp_const_none;
235 }
236 MP_DEFINE_CONST_FUN_OBJ_0(pyb_wfi_obj, pyb_wfi);
237
pyb_stop(void)238 STATIC mp_obj_t pyb_stop(void) {
239 printf("stop not currently implemented\n");
240 return mp_const_none;
241 }
242 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_stop_obj, pyb_stop);
243
pyb_standby(void)244 STATIC mp_obj_t pyb_standby(void) {
245 printf("standby not currently implemented\n");
246 return mp_const_none;
247 }
248 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_standby_obj, pyb_standby);
249
250 /// \function have_cdc()
251 /// Return True if USB is connected as a serial device, False otherwise.
pyb_have_cdc(void)252 STATIC mp_obj_t pyb_have_cdc(void) {
253 return mp_obj_new_bool(usb_vcp_is_connected());
254 }
255 STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc);
256
257 /// \function hid((buttons, x, y, z))
258 /// Takes a 4-tuple (or list) and sends it to the USB host (the PC) to
259 /// signal a HID mouse-motion event.
pyb_hid_send_report(mp_obj_t arg)260 STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
261 #if 1
262 printf("hid_send_report not currently implemented\n");
263 #else
264 mp_obj_t *items;
265 mp_obj_get_array_fixed_n(arg, 4, &items);
266 uint8_t data[4];
267 data[0] = mp_obj_get_int(items[0]);
268 data[1] = mp_obj_get_int(items[1]);
269 data[2] = mp_obj_get_int(items[2]);
270 data[3] = mp_obj_get_int(items[3]);
271 usb_hid_send_report(data);
272 #endif
273 return mp_const_none;
274 }
275 STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
276
277 MP_DECLARE_CONST_FUN_OBJ_1(pyb_source_dir_obj); // defined in main.c
278 MP_DECLARE_CONST_FUN_OBJ_1(pyb_main_obj); // defined in main.c
279 MP_DECLARE_CONST_FUN_OBJ_1(pyb_usb_mode_obj); // defined in main.c
280
281 STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
282 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) },
283
284 { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&pyb_bootloader_obj) },
285 { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&pyb_info_obj) },
286 { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&pyb_unique_id_obj) },
287 { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&pyb_freq_obj) },
288 #if MICROPY_REPL_INFO
289 { MP_ROM_QSTR(MP_QSTR_repl_info), MP_ROM_PTR(&pyb_set_repl_info_obj) },
290 #endif
291
292 { MP_ROM_QSTR(MP_QSTR_wfi), MP_ROM_PTR(&pyb_wfi_obj) },
293 { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
294 { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
295
296 { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&pyb_stop_obj) },
297 { MP_ROM_QSTR(MP_QSTR_standby), MP_ROM_PTR(&pyb_standby_obj) },
298 { MP_ROM_QSTR(MP_QSTR_source_dir), MP_ROM_PTR(&pyb_source_dir_obj) },
299 { MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&pyb_main_obj) },
300 { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
301
302 { MP_ROM_QSTR(MP_QSTR_have_cdc), MP_ROM_PTR(&pyb_have_cdc_obj) },
303 { MP_ROM_QSTR(MP_QSTR_hid), MP_ROM_PTR(&pyb_hid_send_report_obj) },
304
305 { MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&pyb_millis_obj) },
306 { MP_ROM_QSTR(MP_QSTR_elapsed_millis), MP_ROM_PTR(&pyb_elapsed_millis_obj) },
307 { MP_ROM_QSTR(MP_QSTR_micros), MP_ROM_PTR(&pyb_micros_obj) },
308 { MP_ROM_QSTR(MP_QSTR_elapsed_micros), MP_ROM_PTR(&pyb_elapsed_micros_obj) },
309 { MP_ROM_QSTR(MP_QSTR_delay), MP_ROM_PTR(&pyb_delay_obj) },
310 { MP_ROM_QSTR(MP_QSTR_udelay), MP_ROM_PTR(&pyb_udelay_obj) },
311 { MP_ROM_QSTR(MP_QSTR_sync), MP_ROM_PTR(&pyb_sync_obj) },
312
313 { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) },
314
315 // #if MICROPY_HW_ENABLE_RNG
316 // { MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&pyb_rng_get_obj) },
317 // #endif
318
319 // #if MICROPY_HW_ENABLE_RTC
320 // { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
321 // #endif
322
323 { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) },
324 // { MP_ROM_QSTR(MP_QSTR_ExtInt), MP_ROM_PTR(&extint_type) },
325
326 #if MICROPY_HW_ENABLE_SERVO
327 { MP_ROM_QSTR(MP_QSTR_pwm), MP_ROM_PTR(&pyb_pwm_set_obj) },
328 { MP_ROM_QSTR(MP_QSTR_servo), MP_ROM_PTR(&pyb_servo_set_obj) },
329 { MP_ROM_QSTR(MP_QSTR_Servo), MP_ROM_PTR(&pyb_servo_type) },
330 #endif
331
332 #if MICROPY_HW_HAS_SWITCH
333 { MP_ROM_QSTR(MP_QSTR_Switch), MP_ROM_PTR(&pyb_switch_type) },
334 #endif
335
336 // #if MICROPY_HW_HAS_SDCARD
337 // { MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sdcard_obj) },
338 // #endif
339
340 { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pyb_led_type) },
341 // { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) },
342 // { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
343 { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
344
345 // { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
346 // { MP_ROM_QSTR(MP_QSTR_ADCAll), MP_ROM_PTR(&pyb_adc_all_type) },
347
348 // #if MICROPY_HW_ENABLE_DAC
349 // { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&pyb_dac_type) },
350 // #endif
351
352 // #if MICROPY_HW_HAS_MMA7660
353 // { MP_ROM_QSTR(MP_QSTR_Accel), MP_ROM_PTR(&pyb_accel_type) },
354 // #endif
355 };
356
357 STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
358
359 const mp_obj_module_t pyb_module = {
360 .base = { &mp_type_module },
361 .globals = (mp_obj_dict_t *)&pyb_module_globals,
362 };
363