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