blob: 5cf39e924f089f1f9e37026b84c706cd9d83c7a9 [file] [log] [blame]
Damien4b6e85c2013-10-21 09:56:56 +01001#include <stdio.h>
Damiened656052013-10-13 00:42:20 +01002#include <stm32f4xx.h>
3#include <stm32f4xx_rcc.h>
Damien00ff04f2013-10-19 14:40:54 +01004#include <stm32f4xx_gpio.h>
5#include <stm_misc.h>
Damiened656052013-10-13 00:42:20 +01006#include "std.h"
7
Damien995b8aa2013-10-18 23:44:05 +01008#include "misc.h"
Damien3f69aca2013-10-21 23:46:04 +01009#include "mpyconfig.h"
10#include "gc.h"
Damien00ff04f2013-10-19 14:40:54 +010011#include "systick.h"
Damien995b8aa2013-10-18 23:44:05 +010012#include "led.h"
Damien00ff04f2013-10-19 14:40:54 +010013#include "lcd.h"
Damien995b8aa2013-10-18 23:44:05 +010014#include "storage.h"
Damienafe12bc2013-10-19 18:13:48 +010015#include "mma.h"
Damienfb42ec12013-10-19 15:37:09 +010016#include "usb.h"
Damienfa2162b2013-10-20 17:42:00 +010017#include "ff.h"
Damiened656052013-10-13 00:42:20 +010018
Damien3f69aca2013-10-21 23:46:04 +010019extern uint32_t _heap_start;
20
Damien4a175e12013-10-17 22:50:21 +010021static void impl02_c_version() {
Damiened656052013-10-13 00:42:20 +010022 int x = 0;
23 while (x < 400) {
24 int y = 0;
25 while (y < 400) {
26 volatile int z = 0;
27 while (z < 400) {
28 z = z + 1;
29 }
30 y = y + 1;
31 }
32 x = x + 1;
33 }
34}
35
Damiened656052013-10-13 00:42:20 +010036#define PYB_USRSW_PORT (GPIOA)
Damien00ff04f2013-10-19 14:40:54 +010037#define PYB_USRSW_PIN (GPIO_Pin_13)
Damiened656052013-10-13 00:42:20 +010038
39void sw_init() {
40 // make it an input with pull-up
Damien00ff04f2013-10-19 14:40:54 +010041 GPIO_InitTypeDef GPIO_InitStructure;
42 GPIO_InitStructure.GPIO_Pin = PYB_USRSW_PIN;
43 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
44 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
45 GPIO_Init(PYB_USRSW_PORT, &GPIO_InitStructure);
Damiened656052013-10-13 00:42:20 +010046}
47
48int sw_get() {
Damien00ff04f2013-10-19 14:40:54 +010049 if (PYB_USRSW_PORT->IDR & PYB_USRSW_PIN) {
Damiened656052013-10-13 00:42:20 +010050 // pulled high, so switch is not pressed
51 return 0;
52 } else {
53 // pulled low, so switch is pressed
54 return 1;
55 }
56}
57
Damiened656052013-10-13 00:42:20 +010058void __fatal_error(const char *msg) {
59 lcd_print_strn("\nFATAL ERROR:\n", 14);
60 lcd_print_strn(msg, strlen(msg));
61
62 for (;;) {
Damien995b8aa2013-10-18 23:44:05 +010063 led_state(PYB_LED_R1, 1);
64 led_state(PYB_LED_R2, 0);
Damien00ff04f2013-10-19 14:40:54 +010065 sys_tick_delay_ms(150);
Damien995b8aa2013-10-18 23:44:05 +010066 led_state(PYB_LED_R1, 0);
67 led_state(PYB_LED_R2, 1);
Damien00ff04f2013-10-19 14:40:54 +010068 sys_tick_delay_ms(150);
Damiened656052013-10-13 00:42:20 +010069 }
70}
71
Damienfa2162b2013-10-20 17:42:00 +010072#include "nlr.h"
Damiened656052013-10-13 00:42:20 +010073#include "misc.h"
74#include "lexer.h"
Damienfa2162b2013-10-20 17:42:00 +010075#include "lexerstm.h"
Damiened656052013-10-13 00:42:20 +010076#include "mpyconfig.h"
77#include "parse.h"
78#include "compile.h"
79#include "runtime.h"
Damiene9f1e502013-10-22 23:09:25 +010080#include "repl.h"
81
82py_obj_t pyb_source_dir(py_obj_t source_dir) {
83 return py_const_none;
84}
85
86py_obj_t pyb_main(py_obj_t main) {
87 return py_const_none;
88}
Damiened656052013-10-13 00:42:20 +010089
Damiened656052013-10-13 00:42:20 +010090py_obj_t pyb_delay(py_obj_t count) {
Damien00ff04f2013-10-19 14:40:54 +010091 sys_tick_delay_ms(rt_get_int(count));
Damiened656052013-10-13 00:42:20 +010092 return py_const_none;
93}
94
95py_obj_t pyb_led(py_obj_t state) {
Damien995b8aa2013-10-18 23:44:05 +010096 led_state(PYB_LED_G1, rt_is_true(state));
Damiened656052013-10-13 00:42:20 +010097 return state;
98}
99
100py_obj_t pyb_sw() {
101 if (sw_get()) {
102 return py_const_true;
103 } else {
104 return py_const_false;
105 }
106}
Damiened656052013-10-13 00:42:20 +0100107
Damiened656052013-10-13 00:42:20 +0100108FATFS fatfs0;
109
Damien4a175e12013-10-17 22:50:21 +0100110/*
Damien152568b2013-10-16 00:46:39 +0100111void g(uint i) {
112 printf("g:%d\n", i);
113 if (i & 1) {
114 nlr_jump((void*)(42 + i));
115 }
116}
117void f() {
118 nlr_buf_t nlr;
119 int i;
120 for (i = 0; i < 4; i++) {
121 printf("f:loop:%d:%p\n", i, &nlr);
122 if (nlr_push(&nlr) == 0) {
123 // normal
124 //printf("a:%p:%p %p %p %u\n", &nlr, nlr.ip, nlr.sp, nlr.prev, nlr.ret_val);
125 g(i);
126 printf("f:lp:%d:nrm\n", i);
127 nlr_pop();
128 } else {
129 // nlr
130 //printf("b:%p:%p %p %p %u\n", &nlr, nlr.ip, nlr.sp, nlr.prev, nlr.ret_val);
131 printf("f:lp:%d:nlr:%d\n", i, (int)nlr.ret_val);
132 }
133 }
134}
135void nlr_test() {
136 f(1);
137}
Damien4a175e12013-10-17 22:50:21 +0100138*/
139
Damien00ff04f2013-10-19 14:40:54 +0100140void fatality() {
141 led_state(PYB_LED_R1, 1);
142 led_state(PYB_LED_G1, 1);
143 led_state(PYB_LED_R2, 1);
144 led_state(PYB_LED_G2, 1);
145}
Damiened656052013-10-13 00:42:20 +0100146
Damienafe12bc2013-10-19 18:13:48 +0100147static const char fresh_boot_py[] =
Damien00ff04f2013-10-19 14:40:54 +0100148"# boot.py -- run on boot-up\n"
149"# can run arbitrary Python, but best to keep it minimal\n"
150"\n"
151"pyb.source_dir('/src')\n"
152"pyb.main('main.py')\n"
153"#pyb.usb_usr('VCP')\n"
154"#pyb.usb_msd(True, 'dual partition')\n"
155"#pyb.flush_cache(False)\n"
156"#pyb.error_log('error.txt')\n"
157;
Damiened656052013-10-13 00:42:20 +0100158
Damien00ff04f2013-10-19 14:40:54 +0100159// get lots of info about the board
Damiene9f1e502013-10-22 23:09:25 +0100160static py_obj_t pyb_info() {
Damiened656052013-10-13 00:42:20 +0100161 // get and print clock speeds
162 // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
Damiened656052013-10-13 00:42:20 +0100163 {
164 RCC_ClocksTypeDef rcc_clocks;
165 RCC_GetClocksFreq(&rcc_clocks);
Damien00ff04f2013-10-19 14:40:54 +0100166 printf("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n", rcc_clocks.SYSCLK_Frequency, rcc_clocks.HCLK_Frequency, rcc_clocks.PCLK1_Frequency, rcc_clocks.PCLK2_Frequency);
Damien4a175e12013-10-17 22:50:21 +0100167 }
168
Damien995b8aa2013-10-18 23:44:05 +0100169 // to print info about memory
Damien00ff04f2013-10-19 14:40:54 +0100170 {
Damien4a175e12013-10-17 22:50:21 +0100171 extern void *_sidata;
172 extern void *_sdata;
173 extern void *_edata;
174 extern void *_sbss;
175 extern void *_ebss;
176 extern void *_estack;
177 extern void *_etext;
Damien00ff04f2013-10-19 14:40:54 +0100178 printf("_sidata=%p\n", &_sidata);
179 printf("_sdata=%p\n", &_sdata);
180 printf("_edata=%p\n", &_edata);
181 printf("_sbss=%p\n", &_sbss);
182 printf("_ebss=%p\n", &_ebss);
183 printf("_estack=%p\n", &_estack);
184 printf("_etext=%p\n", &_etext);
185 printf("_heap_start=%p\n", &_heap_start);
186 }
187
188 // free space on flash
189 {
190 DWORD nclst;
191 FATFS *fatfs;
192 f_getfree("0:", &nclst, &fatfs);
193 printf("free=%u\n", (uint)(nclst * fatfs->csize * 512));
194 }
Damien00ff04f2013-10-19 14:40:54 +0100195
Damiene9f1e502013-10-22 23:09:25 +0100196 return py_const_none;
Damien4b6e85c2013-10-21 09:56:56 +0100197}
198
Damien0c5827f2013-10-22 21:13:36 +0100199/*
200void gc_print_info() {
201 gc_info_t info;
202 gc_info(&info);
203 printf("! %lu total\n", info.total);
204 printf("! %lu : %lu\n", info.used, info.free);
205 printf("! 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
206}
207*/
208
Damiene9f1e502013-10-22 23:09:25 +0100209int readline(vstr_t *line, const char *prompt) {
210 usb_vcp_send_str(prompt);
211 int len = vstr_len(line);
212 for (;;) {
213 while (usb_vcp_rx_any() == 0) {
214 sys_tick_delay_ms(10);
215 }
216 char c = usb_vcp_rx_get();
217 if (c == 4 && vstr_len(line) == len) {
218 return 0;
219 } else if (c == '\r') {
220 usb_vcp_send_str("\r\n");
221 return 1;
222 } else if (c == 127) {
223 if (vstr_len(line) > len) {
224 vstr_cut_tail(line, 1);
225 usb_vcp_send_str("\b \b");
226 }
227 } else if (32 <= c && c <= 126) {
228 vstr_add_char(line, c);
229 usb_vcp_send_strn(&c, 1);
230 }
231 sys_tick_delay_ms(100);
232 }
233}
234
Damien4b6e85c2013-10-21 09:56:56 +0100235void do_repl() {
Damiene9f1e502013-10-22 23:09:25 +0100236 usb_vcp_send_str("Micro Python 0.5; STM32F405RG; PYBv2\r\n");
237 usb_vcp_send_str("Type \"help\" for more information.\r\n");
238
239 vstr_t line;
240 vstr_init(&line);
Damien4b6e85c2013-10-21 09:56:56 +0100241
242 for (;;) {
Damiene9f1e502013-10-22 23:09:25 +0100243 vstr_reset(&line);
244 int ret = readline(&line, ">>> ");
245 if (ret == 0) {
Damien4b6e85c2013-10-21 09:56:56 +0100246 // EOF
Damiene9f1e502013-10-22 23:09:25 +0100247 break;
Damien4b6e85c2013-10-21 09:56:56 +0100248 }
Damiene9f1e502013-10-22 23:09:25 +0100249
250 if (vstr_len(&line) == 0) {
251 continue;
252 }
253
254 if (py_repl_is_compound_stmt(vstr_str(&line))) {
Damien4b6e85c2013-10-21 09:56:56 +0100255 for (;;) {
Damiene9f1e502013-10-22 23:09:25 +0100256 vstr_add_char(&line, '\n');
257 int len = vstr_len(&line);
258 int ret = readline(&line, "... ");
259 if (ret == 0 || vstr_len(&line) == len) {
260 // done entering compound statement
Damien4b6e85c2013-10-21 09:56:56 +0100261 break;
262 }
Damien4b6e85c2013-10-21 09:56:56 +0100263 }
264 }
Damien4b6e85c2013-10-21 09:56:56 +0100265
266 py_lexer_str_buf_t sb;
Damiene9f1e502013-10-22 23:09:25 +0100267 py_lexer_t *lex = py_lexer_new_from_str_len("<stdin>", vstr_str(&line), vstr_len(&line), false, &sb);
Damien4b6e85c2013-10-21 09:56:56 +0100268 py_parse_node_t pn = py_parse(lex, PY_PARSE_SINGLE_INPUT);
269 py_lexer_free(lex);
270
271 if (pn != PY_PARSE_NODE_NULL) {
272 bool comp_ok = py_compile(pn, true);
273 if (comp_ok) {
274 py_obj_t module_fun = rt_make_function_from_id(1);
275 if (module_fun != py_const_none) {
276 nlr_buf_t nlr;
277 if (nlr_push(&nlr) == 0) {
278 rt_call_function_0(module_fun);
279 nlr_pop();
280 } else {
281 // uncaught exception
282 py_obj_print((py_obj_t)nlr.ret_val);
283 printf("\n");
284 }
285 }
286 }
287 }
288 }
Damiene9f1e502013-10-22 23:09:25 +0100289
290 usb_vcp_send_str("\r\nMicro Python REPL finished\r\n");
Damien4b6e85c2013-10-21 09:56:56 +0100291}
292
Damien0c5827f2013-10-22 21:13:36 +0100293#define RAM_START (0x20000000) // fixed for chip
294#define HEAP_END (0x2001c000) // tunable
295#define RAM_END (0x20020000) // fixed for chip
296
297void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end);
298
Damien3f69aca2013-10-21 23:46:04 +0100299void gc_collect() {
Damien0c5827f2013-10-22 21:13:36 +0100300 uint32_t start = sys_tick_counter;
Damien3f69aca2013-10-21 23:46:04 +0100301 gc_collect_start();
Damien0c5827f2013-10-22 21:13:36 +0100302 gc_collect_root((void**)RAM_START, (((uint32_t)&_heap_start) - RAM_START) / 4);
303 machine_uint_t regs[10];
304 gc_helper_get_regs_and_clean_stack(regs, HEAP_END);
305 gc_collect_root((void**)HEAP_END, (RAM_END - HEAP_END) / 4); // will trace regs since they now live in this function on the stack
Damien3f69aca2013-10-21 23:46:04 +0100306 gc_collect_end();
Damien0c5827f2013-10-22 21:13:36 +0100307 uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
308 gc_info_t info;
309 gc_info(&info);
310 printf("GC@%lu %lums\n", start, ticks);
311 printf(" %lu total\n", info.total);
312 printf(" %lu : %lu\n", info.used, info.free);
313 printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
314}
315
Damiene9f1e502013-10-22 23:09:25 +0100316py_obj_t pyb_gc() {
Damien0c5827f2013-10-22 21:13:36 +0100317 gc_collect();
318 return py_const_none;
Damien3f69aca2013-10-21 23:46:04 +0100319}
320
Damien00ff04f2013-10-19 14:40:54 +0100321int main() {
322 // TODO disable JTAG
323
Damienafe12bc2013-10-19 18:13:48 +0100324 // set interrupt priority config to use all 4 bits for pre-empting
325 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
326
327 // enable the CCM RAM and the GPIO's
328 RCC->AHB1ENR |= RCC_AHB1ENR_CCMDATARAMEN | RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN;
329
Damien00ff04f2013-10-19 14:40:54 +0100330 // basic sub-system init
331 sys_tick_init();
Damien00ff04f2013-10-19 14:40:54 +0100332 led_init();
333
334 // turn on LED to indicate bootup
335 led_state(PYB_LED_G1, 1);
336
337 // more sub-system init
338 sw_init();
Damien00ff04f2013-10-19 14:40:54 +0100339 storage_init();
340
Damiene9f1e502013-10-22 23:09:25 +0100341soft_reset:
342
343 // LCD init
344 lcd_init();
345
Damien3f69aca2013-10-21 23:46:04 +0100346 // GC init
Damien0c5827f2013-10-22 21:13:36 +0100347 gc_init(&_heap_start, (void*)HEAP_END);
Damien3f69aca2013-10-21 23:46:04 +0100348
Damiene9f1e502013-10-22 23:09:25 +0100349 // Micro Python init
Damienafe12bc2013-10-19 18:13:48 +0100350 qstr_init();
351 rt_init();
Damien00ff04f2013-10-19 14:40:54 +0100352
Damienf48cf672013-10-21 10:42:06 +0100353 // add some functions to the python namespace
Damiene9f1e502013-10-22 23:09:25 +0100354 {
355 py_obj_t m = py_module_new();
356 rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info));
357 rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_1(pyb_source_dir));
358 rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_1(pyb_main));
359 rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_0(pyb_gc));
360 rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay));
361 rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_1(pyb_led));
362 rt_store_attr(m, qstr_from_str_static("sw"), rt_make_function_0(pyb_sw));
363 rt_store_name(qstr_from_str_static("pyb"), m);
364 }
Damienf48cf672013-10-21 10:42:06 +0100365
Damiene9f1e502013-10-22 23:09:25 +0100366 // print a message to the LCD
367 lcd_print_str(" micro py board\n");
Damien00ff04f2013-10-19 14:40:54 +0100368
369 // local filesystem init
370 {
371 // try to mount the flash
372 FRESULT res = f_mount(&fatfs0, "0:", 1);
373 if (res == FR_OK) {
374 // mount sucessful
375 } else if (res == FR_NO_FILESYSTEM) {
376 // no filesystem, so create a fresh one
377
378 // LED on to indicate creation of LFS
379 led_state(PYB_LED_R2, 1);
380 uint32_t stc = sys_tick_counter;
381
382 res = f_mkfs("0:", 0, 0);
383 if (res == FR_OK) {
384 // success creating fresh LFS
385 } else {
386 __fatal_error("could not create LFS");
387 }
388
Damienafe12bc2013-10-19 18:13:48 +0100389 // keep LED on for at least 200ms
390 sys_tick_wait_at_least(stc, 200);
Damien00ff04f2013-10-19 14:40:54 +0100391 led_state(PYB_LED_R2, 0);
392 } else {
393 __fatal_error("could not access LFS");
Damien4a175e12013-10-17 22:50:21 +0100394 }
Damien00ff04f2013-10-19 14:40:54 +0100395 }
396
397 // make sure we have a /boot.py
398 {
399 FILINFO fno;
400 FRESULT res = f_stat("0:/boot.py", &fno);
401 if (res == FR_OK) {
402 if (fno.fattrib & AM_DIR) {
403 // exists as a directory
404 // TODO handle this case
405 // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
406 } else {
407 // exists as a file, good!
408 }
409 } else {
410 // doesn't exist, create fresh file
411
412 // LED on to indicate creation of boot.py
413 led_state(PYB_LED_R2, 1);
414 uint32_t stc = sys_tick_counter;
415
416 FIL fp;
417 f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
418 UINT n;
Damiene9f1e502013-10-22 23:09:25 +0100419 f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
Damien00ff04f2013-10-19 14:40:54 +0100420 // TODO check we could write n bytes
421 f_close(&fp);
422
Damienafe12bc2013-10-19 18:13:48 +0100423 // keep LED on for at least 200ms
424 sys_tick_wait_at_least(stc, 200);
Damien00ff04f2013-10-19 14:40:54 +0100425 led_state(PYB_LED_R2, 0);
426 }
427 }
428
Damiene9f1e502013-10-22 23:09:25 +0100429 // USB
430 usb_init();
431
Damien00ff04f2013-10-19 14:40:54 +0100432 // run /boot.py
Damiene9f1e502013-10-22 23:09:25 +0100433 if (1) {
Damien0c5827f2013-10-22 21:13:36 +0100434 py_lexer_file_buf_t fb;
435 py_lexer_t *lex = py_lexer_new_from_file("0:/boot.py", &fb);
436 py_parse_node_t pn = py_parse(lex, PY_PARSE_FILE_INPUT);
437 py_lexer_free(lex);
438
439 if (pn != PY_PARSE_NODE_NULL) {
440 bool comp_ok = py_compile(pn, true);
441 if (comp_ok) {
442 py_obj_t module_fun = rt_make_function_from_id(1);
443 if (module_fun != py_const_none) {
444 nlr_buf_t nlr;
445 if (nlr_push(&nlr) == 0) {
446 rt_call_function_0(module_fun);
447 nlr_pop();
448 } else {
449 // uncaught exception
450 py_obj_print((py_obj_t)nlr.ret_val);
451 printf("\n");
452 }
453 }
454 }
455 }
Damien00ff04f2013-10-19 14:40:54 +0100456 }
457
458 // turn boot-up LED off
459 led_state(PYB_LED_G1, 0);
460
Damien00ff04f2013-10-19 14:40:54 +0100461 //printf("init;al=%u\n", m_get_total_bytes_allocated()); // 1600, due to qstr_init
462 //sys_tick_delay_ms(1000);
463
Damiened656052013-10-13 00:42:20 +0100464 // Python!
Damien4b6e85c2013-10-21 09:56:56 +0100465 if (0) {
Damiened656052013-10-13 00:42:20 +0100466 //const char *pysrc = "def f():\n x=x+1\nprint(42)\n";
467 const char *pysrc =
468 // impl01.py
469 /*
470 "x = 0\n"
471 "while x < 400:\n"
472 " y = 0\n"
473 " while y < 400:\n"
474 " z = 0\n"
475 " while z < 400:\n"
476 " z = z + 1\n"
477 " y = y + 1\n"
478 " x = x + 1\n";
479 */
480 // impl02.py
Damien152568b2013-10-16 00:46:39 +0100481 /*
Damiened656052013-10-13 00:42:20 +0100482 "#@micropython.native\n"
483 "def f():\n"
484 " x = 0\n"
485 " while x < 400:\n"
486 " y = 0\n"
487 " while y < 400:\n"
488 " z = 0\n"
489 " while z < 400:\n"
490 " z = z + 1\n"
491 " y = y + 1\n"
492 " x = x + 1\n"
493 "f()\n";
Damien152568b2013-10-16 00:46:39 +0100494 */
Damien0c5827f2013-10-22 21:13:36 +0100495 /*
Damiened656052013-10-13 00:42:20 +0100496 "print('in python!')\n"
497 "x = 0\n"
498 "while x < 4:\n"
499 " pyb_led(True)\n"
500 " pyb_delay(201)\n"
501 " pyb_led(False)\n"
502 " pyb_delay(201)\n"
Damienfa2162b2013-10-20 17:42:00 +0100503 " x += 1\n"
Damiened656052013-10-13 00:42:20 +0100504 "print('press me!')\n"
505 "while True:\n"
506 " pyb_led(pyb_sw())\n";
Damien0c5827f2013-10-22 21:13:36 +0100507 */
Damiened656052013-10-13 00:42:20 +0100508 /*
509 // impl16.py
510 "@micropython.asm_thumb\n"
511 "def delay(r0):\n"
512 " b(loop_entry)\n"
513 " label(loop1)\n"
514 " movw(r1, 55999)\n"
515 " label(loop2)\n"
516 " subs(r1, r1, 1)\n"
517 " cmp(r1, 0)\n"
518 " bgt(loop2)\n"
519 " subs(r0, r0, 1)\n"
520 " label(loop_entry)\n"
521 " cmp(r0, 0)\n"
522 " bgt(loop1)\n"
523 "print('in python!')\n"
524 "@micropython.native\n"
525 "def flash(n):\n"
526 " x = 0\n"
527 " while x < n:\n"
528 " pyb_led(True)\n"
529 " delay(249)\n"
530 " pyb_led(False)\n"
531 " delay(249)\n"
532 " x = x + 1\n"
533 "flash(20)\n";
534 */
Damien152568b2013-10-16 00:46:39 +0100535 // impl18.py
536 /*
537 "# basic exceptions\n"
538 "x = 1\n"
539 "try:\n"
540 " x.a()\n"
541 "except:\n"
542 " print(x)\n";
543 */
544 // impl19.py
545 "# for loop\n"
546 "def f():\n"
547 " for x in range(400):\n"
548 " for y in range(400):\n"
549 " for z in range(400):\n"
550 " pass\n"
551 "f()\n";
Damiened656052013-10-13 00:42:20 +0100552
Damienfa2162b2013-10-20 17:42:00 +0100553 py_lexer_str_buf_t py_lexer_str_buf;
554 py_lexer_t *lex = py_lexer_new_from_str_len("<stdin>", pysrc, strlen(pysrc), false, &py_lexer_str_buf);
Damiened656052013-10-13 00:42:20 +0100555
Damienfa2162b2013-10-20 17:42:00 +0100556 // nalloc=1740;6340;6836 -> 140;4600;496 bytes for lexer, parser, compiler
557 printf("lex; al=%u\n", m_get_total_bytes_allocated());
558 sys_tick_delay_ms(1000);
559 py_parse_node_t pn = py_parse(lex, PY_PARSE_FILE_INPUT);
560 py_lexer_free(lex);
561 if (pn != PY_PARSE_NODE_NULL) {
Damiened656052013-10-13 00:42:20 +0100562 printf("pars;al=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100563 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100564 //parse_node_show(pn, 0);
Damienfa2162b2013-10-20 17:42:00 +0100565 bool comp_ok = py_compile(pn, false);
Damiened656052013-10-13 00:42:20 +0100566 printf("comp;al=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100567 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100568
Damienfa2162b2013-10-20 17:42:00 +0100569 if (!comp_ok) {
570 printf("compile error\n");
571 } else {
Damiened656052013-10-13 00:42:20 +0100572 // execute it!
573
Damiened656052013-10-13 00:42:20 +0100574 py_obj_t module_fun = rt_make_function_from_id(1);
575
Damien152568b2013-10-16 00:46:39 +0100576 // flash once
Damien995b8aa2013-10-18 23:44:05 +0100577 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100578 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100579 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100580
Damien152568b2013-10-16 00:46:39 +0100581 nlr_buf_t nlr;
582 if (nlr_push(&nlr) == 0) {
583 py_obj_t ret = rt_call_function_0(module_fun);
584 printf("done! got: ");
585 py_obj_print(ret);
586 printf("\n");
587 nlr_pop();
588 } else {
589 // uncaught exception
590 printf("exception: ");
591 py_obj_print((py_obj_t)nlr.ret_val);
592 printf("\n");
593 }
594
595 // flash once
Damien995b8aa2013-10-18 23:44:05 +0100596 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100597 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100598 led_state(PYB_LED_G1, 0);
Damien152568b2013-10-16 00:46:39 +0100599
Damien00ff04f2013-10-19 14:40:54 +0100600 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100601 printf("nalloc=%u\n", m_get_total_bytes_allocated());
Damien00ff04f2013-10-19 14:40:54 +0100602 sys_tick_delay_ms(1000);
Damiened656052013-10-13 00:42:20 +0100603 }
604 }
605 }
Damiened656052013-10-13 00:42:20 +0100606
Damien4b6e85c2013-10-21 09:56:56 +0100607 do_repl();
608
Damiened656052013-10-13 00:42:20 +0100609 // benchmark C version of impl02.py
610 if (0) {
Damien995b8aa2013-10-18 23:44:05 +0100611 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100612 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100613 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100614 impl02_c_version();
Damien995b8aa2013-10-18 23:44:05 +0100615 led_state(PYB_LED_G1, 1);
Damien00ff04f2013-10-19 14:40:54 +0100616 sys_tick_delay_ms(100);
Damien995b8aa2013-10-18 23:44:05 +0100617 led_state(PYB_LED_G1, 0);
Damiened656052013-10-13 00:42:20 +0100618 }
619
620 // MMA testing
621 if (0) {
622 printf("1");
623 mma_init();
624 printf("2");
625 mma_start(0x4c, 1);
626 printf("3");
627 mma_send_byte(0);
628 printf("4");
629 mma_stop();
630 printf("5");
631 mma_start(0x4c, 1);
632 printf("6");
633 mma_send_byte(0);
634 printf("7");
635 mma_restart(0x4c, 0);
636 for (int i = 0; i <= 0xa; i++) {
637 int data;
638 if (i == 0xa) {
639 data = mma_read_nack();
640 } else {
641 data = mma_read_ack();
642 }
643 printf(" %02x", data);
644 }
645 printf("\n");
646
647 mma_start(0x4c, 1);
648 mma_send_byte(7); // mode
649 mma_send_byte(1); // active mode
650 mma_stop();
651
652 for (;;) {
Damien00ff04f2013-10-19 14:40:54 +0100653 sys_tick_delay_ms(500);
Damiened656052013-10-13 00:42:20 +0100654
655 mma_start(0x4c, 1);
656 mma_send_byte(0);
657 mma_restart(0x4c, 0);
658 for (int i = 0; i <= 3; i++) {
659 int data;
660 if (i == 3) {
661 data = mma_read_nack();
662 printf(" %02x\n", data);
663 } else {
664 data = mma_read_ack() & 0x3f;
665 if (data & 0x20) {
666 data |= 0xc0;
667 }
668 printf(" % 2d", data);
669 }
670 }
671 }
672 }
673
Damiened656052013-10-13 00:42:20 +0100674 // SD card testing
675 if (0) {
676 //sdio_init();
677 }
678
Damiene9f1e502013-10-22 23:09:25 +0100679 goto soft_reset;
Damiened656052013-10-13 00:42:20 +0100680}