blob: 0240395dc7aba85ebfd32c5ed7f847acca85222f [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
32#include "misc.h"
33#include "mpconfig.h"
34
35#include "teensy_hal.h"
36
37#include "qstr.h"
38#include "obj.h"
39#include "gc.h"
40#include "gccollect.h"
41#include "systick.h"
42#include "pybstdio.h"
43#include "pyexec.h"
44#include "led.h"
45#include "pin.h"
46//#include "timer.h"
47#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"
62
63/// \module pyb - functions related to the pyboard
64///
65/// The `pyb` module contains specific functions related to the pyboard.
66
67/// \function bootloader()
68/// Activate the bootloader without BOOT* pins.
69STATIC mp_obj_t pyb_bootloader(void) {
70 printf("bootloader command not current supported\n");
71 return mp_const_none;
72}
73STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_bootloader_obj, pyb_bootloader);
74
75/// \function info([dump_alloc_table])
76/// Print out lots of information about the board.
77STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) {
78 // get and print unique id; 96 bits
79 {
80 byte *id = (byte*)0x40048058;
81 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]);
82 }
83
84 // get and print clock speeds
85 printf("CPU=%u\nBUS=%u\nMEM=%u\n", F_CPU, F_BUS, F_MEM);
86
87 // to print info about memory
88 {
89 printf("_etext=%p\n", &_etext);
90 printf("_sidata=%p\n", &_sidata);
91 printf("_sdata=%p\n", &_sdata);
92 printf("_edata=%p\n", &_edata);
93 printf("_sbss=%p\n", &_sbss);
94 printf("_ebss=%p\n", &_ebss);
95 printf("_estack=%p\n", &_estack);
96 printf("_ram_start=%p\n", &_ram_start);
97 printf("_heap_start=%p\n", &_heap_start);
98 printf("_heap_end=%p\n", &_heap_end);
99 printf("_ram_end=%p\n", &_ram_end);
100 }
101
102 // qstr info
103 {
104 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
105 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
106 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);
107 }
108
109 // GC info
110 {
111 gc_info_t info;
112 gc_info(&info);
113 printf("GC:\n");
114 printf(" " UINT_FMT " total\n", info.total);
115 printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
116 printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
117 }
118
119 if (n_args == 1) {
120 // arg given means dump gc allocation table
121 gc_dump_alloc_table();
122 }
123
124 return mp_const_none;
125}
126STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj, 0, 1, pyb_info);
127
128/// \function unique_id()
129/// Returns a string of 12 bytes (96 bits), which is the unique ID for the MCU.
130STATIC mp_obj_t pyb_unique_id(void) {
131 byte *id = (byte*)0x40048058;
132 return mp_obj_new_bytes(id, 12);
133}
134STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_unique_id_obj, pyb_unique_id);
135
136/// \function freq()
137/// Return a tuple of clock frequencies: (SYSCLK, HCLK, PCLK1, PCLK2).
138// TODO should also be able to set frequency via this function
139STATIC mp_obj_t pyb_freq(void) {
140 mp_obj_t tuple[3] = {
141 mp_obj_new_int(F_CPU),
142 mp_obj_new_int(F_BUS),
143 mp_obj_new_int(F_MEM),
144 };
145 return mp_obj_new_tuple(3, tuple);
146}
147STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_freq_obj, pyb_freq);
148
149/// \function sync()
150/// Sync all file systems.
151STATIC mp_obj_t pyb_sync(void) {
152 printf("sync not currently implemented\n");
153 return mp_const_none;
154}
155STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync);
156
157/// \function millis()
158/// Returns the number of milliseconds since the board was last reset.
159STATIC mp_obj_t pyb_millis(void) {
160 return mp_obj_new_int(HAL_GetTick());
161}
162STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
163
164/// \function delay(ms)
165/// Delay for the given number of milliseconds.
166STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
167 machine_int_t ms = mp_obj_get_int(ms_in);
168 if (ms >= 0) {
169 HAL_Delay(ms);
170 }
171 return mp_const_none;
172}
173STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
174
175/// \function udelay(us)
176/// Delay for the given number of microseconds.
177STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) {
178 machine_int_t usec = mp_obj_get_int(usec_in);
179 delayMicroseconds(usec);
180 return mp_const_none;
181}
182STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay);
183
184/// \function wfi()
185/// Wait for an interrupt.
186/// This executies a `wfi` instruction which reduces power consumption
187/// of the MCU until an interrupt occurs, at which point execution continues.
188STATIC mp_obj_t pyb_wfi(void) {
189 __WFI();
190 return mp_const_none;
191}
192MP_DEFINE_CONST_FUN_OBJ_0(pyb_wfi_obj, pyb_wfi);
193
194/// \function disable_irq()
195/// Disable interrupt requests.
196STATIC mp_obj_t pyb_disable_irq(void) {
197 __disable_irq();
198 return mp_const_none;
199}
200MP_DEFINE_CONST_FUN_OBJ_0(pyb_disable_irq_obj, pyb_disable_irq);
201
202/// \function enable_irq()
203/// Enable interrupt requests.
204STATIC mp_obj_t pyb_enable_irq(void) {
205 __enable_irq();
206 return mp_const_none;
207}
208MP_DEFINE_CONST_FUN_OBJ_0(pyb_enable_irq_obj, pyb_enable_irq);
209
210STATIC mp_obj_t pyb_stop(void) {
211 printf("stop not currently implemented\n");
212 return mp_const_none;
213}
214
215MP_DEFINE_CONST_FUN_OBJ_0(pyb_stop_obj, pyb_stop);
216
217STATIC mp_obj_t pyb_standby(void) {
218 printf("standby not currently implemented\n");
219 return mp_const_none;
220}
221
222MP_DEFINE_CONST_FUN_OBJ_0(pyb_standby_obj, pyb_standby);
223
224/// \function have_cdc()
225/// Return True if USB is connected as a serial device, False otherwise.
226STATIC mp_obj_t pyb_have_cdc(void ) {
227 return MP_BOOL(usb_vcp_is_connected());
228}
229STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc);
230
231/// \function hid((buttons, x, y, z))
232/// Takes a 4-tuple (or list) and sends it to the USB host (the PC) to
233/// signal a HID mouse-motion event.
234STATIC mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
235#if 1
236 printf("hid_send_report not currently implemented\n");
237#else
238 mp_obj_t *items;
239 mp_obj_get_array_fixed_n(arg, 4, &items);
240 uint8_t data[4];
241 data[0] = mp_obj_get_int(items[0]);
242 data[1] = mp_obj_get_int(items[1]);
243 data[2] = mp_obj_get_int(items[2]);
244 data[3] = mp_obj_get_int(items[3]);
245 usb_hid_send_report(data);
246#endif
247 return mp_const_none;
248}
249STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj, pyb_hid_send_report);
250
251MP_DECLARE_CONST_FUN_OBJ(pyb_source_dir_obj); // defined in main.c
252MP_DECLARE_CONST_FUN_OBJ(pyb_main_obj); // defined in main.c
253MP_DECLARE_CONST_FUN_OBJ(pyb_usb_mode_obj); // defined in main.c
254
255STATIC const mp_map_elem_t pyb_module_globals_table[] = {
256 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_pyb) },
257
258 { MP_OBJ_NEW_QSTR(MP_QSTR_bootloader), (mp_obj_t)&pyb_bootloader_obj },
259 { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj },
260 { MP_OBJ_NEW_QSTR(MP_QSTR_unique_id), (mp_obj_t)&pyb_unique_id_obj },
261 { MP_OBJ_NEW_QSTR(MP_QSTR_freq), (mp_obj_t)&pyb_freq_obj },
262 { MP_OBJ_NEW_QSTR(MP_QSTR_repl_info), (mp_obj_t)&pyb_set_repl_info_obj },
263
264 { MP_OBJ_NEW_QSTR(MP_QSTR_wfi), (mp_obj_t)&pyb_wfi_obj },
265 { MP_OBJ_NEW_QSTR(MP_QSTR_disable_irq), (mp_obj_t)&pyb_disable_irq_obj },
266 { MP_OBJ_NEW_QSTR(MP_QSTR_enable_irq), (mp_obj_t)&pyb_enable_irq_obj },
267
268 { MP_OBJ_NEW_QSTR(MP_QSTR_stop), (mp_obj_t)&pyb_stop_obj },
269 { MP_OBJ_NEW_QSTR(MP_QSTR_standby), (mp_obj_t)&pyb_standby_obj },
270 { MP_OBJ_NEW_QSTR(MP_QSTR_source_dir), (mp_obj_t)&pyb_source_dir_obj },
271 { MP_OBJ_NEW_QSTR(MP_QSTR_main), (mp_obj_t)&pyb_main_obj },
272 { MP_OBJ_NEW_QSTR(MP_QSTR_usb_mode), (mp_obj_t)&pyb_usb_mode_obj },
273
274 { MP_OBJ_NEW_QSTR(MP_QSTR_have_cdc), (mp_obj_t)&pyb_have_cdc_obj },
275 { MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj },
276
277 { MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj },
278 { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj },
279 { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj },
280 { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj },
281
282// { MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type },
283
284//#if MICROPY_HW_ENABLE_RNG
285// { MP_OBJ_NEW_QSTR(MP_QSTR_rng), (mp_obj_t)&pyb_rng_get_obj },
286//#endif
287
288//#if MICROPY_HW_ENABLE_RTC
289// { MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_type },
290//#endif
291
292 { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pin_type },
293// { MP_OBJ_NEW_QSTR(MP_QSTR_ExtInt), (mp_obj_t)&extint_type },
294
295#if MICROPY_HW_ENABLE_SERVO
296 { MP_OBJ_NEW_QSTR(MP_QSTR_pwm), (mp_obj_t)&pyb_pwm_set_obj },
297 { MP_OBJ_NEW_QSTR(MP_QSTR_servo), (mp_obj_t)&pyb_servo_set_obj },
298 { MP_OBJ_NEW_QSTR(MP_QSTR_Servo), (mp_obj_t)&pyb_servo_type },
299#endif
300
301#if MICROPY_HW_HAS_SWITCH
302 { MP_OBJ_NEW_QSTR(MP_QSTR_Switch), (mp_obj_t)&pyb_switch_type },
303#endif
304
305//#if MICROPY_HW_HAS_SDCARD
306// { MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj },
307//#endif
308
309 { MP_OBJ_NEW_QSTR(MP_QSTR_LED), (mp_obj_t)&pyb_led_type },
310// { MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&pyb_i2c_type },
311// { MP_OBJ_NEW_QSTR(MP_QSTR_SPI), (mp_obj_t)&pyb_spi_type },
312 { MP_OBJ_NEW_QSTR(MP_QSTR_UART), (mp_obj_t)&pyb_uart_type },
313
314// { MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type },
315// { MP_OBJ_NEW_QSTR(MP_QSTR_ADCAll), (mp_obj_t)&pyb_adc_all_type },
316
317//#if MICROPY_HW_ENABLE_DAC
318// { MP_OBJ_NEW_QSTR(MP_QSTR_DAC), (mp_obj_t)&pyb_dac_type },
319//#endif
320
321//#if MICROPY_HW_HAS_MMA7660
322// { MP_OBJ_NEW_QSTR(MP_QSTR_Accel), (mp_obj_t)&pyb_accel_type },
323//#endif
324};
325
326STATIC const mp_obj_dict_t pyb_module_globals = {
327 .base = {&mp_type_dict},
328 .map = {
329 .all_keys_are_qstrs = 1,
330 .table_is_fixed_array = 1,
331 .used = ARRAY_SIZE(pyb_module_globals_table),
332 .alloc = ARRAY_SIZE(pyb_module_globals_table),
333 .table = (mp_map_elem_t*)pyb_module_globals_table,
334 },
335};
336
337const mp_obj_module_t pyb_module = {
338 .base = { &mp_type_module },
339 .name = MP_QSTR_pyb,
340 .globals = (mp_obj_dict_t*)&pyb_module_globals,
341};