diff options
Diffstat (limited to 'stm/main.c')
-rw-r--r-- | stm/main.c | 106 |
1 files changed, 67 insertions, 39 deletions
diff --git a/stm/main.c b/stm/main.c index 44db47f13b..d8649f5797 100644 --- a/stm/main.c +++ b/stm/main.c @@ -40,6 +40,7 @@ #include "pybwlan.h" #include "i2c.h" #include "usrsw.h" +#include "adc.h" int errno; @@ -307,7 +308,9 @@ char *strdup(const char *str) { static const char *readline_hist[READLINE_HIST_SIZE] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; void stdout_tx_str(const char *str) { - //usart_tx_str(str); // disabled because usart is a Python object and we now need specify which USART port + if (pyb_usart_global_debug != PYB_USART_NONE) { + usart_tx_str(pyb_usart_global_debug, str); + } usb_vcp_send_str(str); } @@ -322,10 +325,10 @@ int readline(vstr_t *line, const char *prompt) { if (usb_vcp_rx_any() != 0) { c = usb_vcp_rx_get(); break; - } /*else if (usart_rx_any()) { // disabled because usart is a Python object and we now need specify which USART port - c = usart_rx_char(); + } else if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) { + c = usart_rx_char(pyb_usart_global_debug); break; - }*/ + } sys_tick_delay_ms(1); if (storage_needs_flush()) { storage_flush(); @@ -388,7 +391,7 @@ void do_repl(void) { stdout_tx_str("Type \"help()\" for more information.\r\n"); vstr_t line; - vstr_init(&line); + vstr_init(&line, 32); for (;;) { vstr_reset(&line); @@ -415,10 +418,18 @@ void do_repl(void) { } mp_lexer_t *lex = mp_lexer_new_from_str_len("<stdin>", vstr_str(&line), vstr_len(&line), 0); - mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT); - mp_lexer_free(lex); - - if (pn != MP_PARSE_NODE_NULL) { + qstr parse_exc_id; + const char *parse_exc_msg; + mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT, &parse_exc_id, &parse_exc_msg); + + if (pn == MP_PARSE_NODE_NULL) { + // parse error + mp_lexer_show_error_pythonic_prefix(lex); + printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg); + mp_lexer_free(lex); + } else { + // parse okay + mp_lexer_free(lex); mp_obj_t module_fun = mp_compile(pn, true); if (module_fun != mp_const_none) { nlr_buf_t nlr; @@ -433,7 +444,7 @@ void do_repl(void) { } } else { // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); + mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR); printf("\n"); } } @@ -452,13 +463,20 @@ bool do_file(const char *filename) { return false; } - mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT); - mp_lexer_free(lex); + qstr parse_exc_id; + const char *parse_exc_msg; + mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg); if (pn == MP_PARSE_NODE_NULL) { + // parse error + mp_lexer_show_error_pythonic_prefix(lex); + printf("%s: %s\n", qstr_str(parse_exc_id), parse_exc_msg); + mp_lexer_free(lex); return false; } + mp_lexer_free(lex); + mp_obj_t module_fun = mp_compile(pn, false); if (module_fun == mp_const_none) { return false; @@ -471,7 +489,7 @@ bool do_file(const char *filename) { return true; } else { // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); + mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR); printf("\n"); return false; } @@ -653,7 +671,7 @@ typedef struct _pyb_file_obj_t { FIL fp; } pyb_file_obj_t; -void file_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { +void file_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { printf("<file %p>", self_in); } @@ -775,7 +793,9 @@ int main(void) { switch_init(); storage_init(); - //usart_init(); disabled while wi-fi is enabled; also disabled because now usart is a proper Python object + // uncomment these 2 lines if you want REPL on USART_6 (or another usart) as well as on USB VCP + //pyb_usart_global_debug = PYB_USART_6; + //usart_init(pyb_usart_global_debug, 115200); int first_soft_reset = true; @@ -808,36 +828,37 @@ soft_reset: // add some functions to the python namespace { - rt_store_name(qstr_from_str_static("help"), rt_make_function_0(pyb_help)); + rt_store_name(qstr_from_str_static("help"), rt_make_function_n(0, pyb_help)); mp_obj_t m = mp_obj_new_module(qstr_from_str_static("pyb")); - rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info)); - rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_0(pyb_sd_test)); - rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_0(pyb_stop)); - rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_0(pyb_standby)); - rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_1(pyb_source_dir)); - rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_1(pyb_main)); - rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_0(pyb_sync)); - rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_0(pyb_gc)); - rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay)); - rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_1(pyb_led)); + rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_n(0, pyb_info)); + rt_store_attr(m, qstr_from_str_static("sd_test"), rt_make_function_n(0, pyb_sd_test)); + rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_n(0, pyb_stop)); + rt_store_attr(m, qstr_from_str_static("standby"), rt_make_function_n(0, pyb_standby)); + rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_n(1, pyb_source_dir)); + rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_n(1, pyb_main)); + rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_n(0, pyb_sync)); + rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_n(0, pyb_gc)); + rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_n(1, pyb_delay)); + rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_n(1, pyb_led)); rt_store_attr(m, qstr_from_str_static("switch"), (mp_obj_t)&pyb_switch_obj); - rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_2(pyb_servo_set)); - rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_2(pyb_pwm_set)); + rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_n(2, pyb_servo_set)); + rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_n(2, pyb_pwm_set)); rt_store_attr(m, qstr_from_str_static("accel"), (mp_obj_t)&pyb_mma_read_obj); rt_store_attr(m, qstr_from_str_static("mma_read"), (mp_obj_t)&pyb_mma_read_all_obj); rt_store_attr(m, qstr_from_str_static("mma_mode"), (mp_obj_t)&pyb_mma_write_mode_obj); - rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report)); - rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read)); - rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_0(pyb_rng_get)); - rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led)); - rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo)); - rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_2(pyb_I2C)); + rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_n(1, pyb_hid_send_report)); + rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_n(0, pyb_rtc_read)); + rt_store_attr(m, qstr_from_str_static("rand"), rt_make_function_n(0, pyb_rng_get)); + rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_n(1, pyb_Led)); + rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_n(1, pyb_Servo)); + rt_store_attr(m, qstr_from_str_static("I2C"), rt_make_function_n(2, pyb_I2C)); rt_store_attr(m, qstr_from_str_static("gpio"), (mp_obj_t)&pyb_gpio_obj); - rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_2(pyb_Usart)); + rt_store_attr(m, qstr_from_str_static("Usart"), rt_make_function_n(2, pyb_Usart)); + rt_store_attr(m, qstr_from_str_static("ADC"), rt_make_function_n(1, pyb_ADC)); rt_store_name(qstr_from_str_static("pyb"), m); - rt_store_name(qstr_from_str_static("open"), rt_make_function_2(pyb_io_open)); + rt_store_name(qstr_from_str_static("open"), rt_make_function_n(2, pyb_io_open)); } // print a message to the LCD @@ -937,6 +958,9 @@ soft_reset: // USB usb_init(); + // USB host; not working! + //pyb_usbh_init(); + // MMA if (first_soft_reset) { // init and reset address to zero @@ -1064,7 +1088,9 @@ soft_reset: // nalloc=1740;6340;6836 -> 140;4600;496 bytes for lexer, parser, compiler printf("lex; al=%u\n", m_get_total_bytes_allocated()); sys_tick_delay_ms(1000); - mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT); + qstr parse_exc_id; + const char *parse_exc_msg; + mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg); mp_lexer_free(lex); if (pn != MP_PARSE_NODE_NULL) { printf("pars;al=%u\n", m_get_total_bytes_allocated()); @@ -1088,13 +1114,13 @@ soft_reset: if (nlr_push(&nlr) == 0) { mp_obj_t ret = rt_call_function_0(module_fun); printf("done! got: "); - mp_obj_print(ret); + mp_obj_print(ret, PRINT_REPR); printf("\n"); nlr_pop(); } else { // uncaught exception printf("exception: "); - mp_obj_print((mp_obj_t)nlr.ret_val); + mp_obj_print((mp_obj_t)nlr.ret_val, PRINT_REPR); printf("\n"); } @@ -1171,6 +1197,7 @@ soft_reset: goto soft_reset; } +/* now supplied by libgcc library double __aeabi_f2d(float x) { // TODO return 0.0; @@ -1180,6 +1207,7 @@ float __aeabi_d2f(double x) { // TODO return 0.0; } +*/ double sqrt(double x) { // TODO |