diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | py/builtinimport.c | 6 | ||||
-rw-r--r-- | py/makeqstrdata.py | 34 | ||||
-rw-r--r-- | py/obj.h | 1 | ||||
-rw-r--r-- | py/objcell.c | 11 | ||||
-rw-r--r-- | py/objclosure.c | 8 | ||||
-rw-r--r-- | py/objfun.c | 11 | ||||
-rw-r--r-- | stmhal/bufhelper.c | 2 | ||||
-rw-r--r-- | stmhal/bufhelper.h | 2 | ||||
-rw-r--r-- | stmhal/gccollect.c | 6 | ||||
-rw-r--r-- | stmhal/gendoc.py | 2 | ||||
-rw-r--r-- | stmhal/input.c | 2 | ||||
-rw-r--r-- | stmhal/modos.c | 1 | ||||
-rw-r--r-- | stmhal/modpyb.c | 26 | ||||
-rw-r--r-- | stmhal/mpconfigport.h | 10 | ||||
-rw-r--r-- | stmhal/pyexec.c | 6 | ||||
-rw-r--r-- | stmhal/rtc.c | 41 | ||||
-rw-r--r-- | stmhal/timer.c | 2 | ||||
-rw-r--r-- | tools/pyboard.py | 1 | ||||
-rw-r--r-- | windows/alloca.h | 3 | ||||
-rw-r--r-- | windows/mpconfigport.h | 3 |
21 files changed, 98 insertions, 81 deletions
diff --git a/.gitignore b/.gitignore index ce2e45af9..4c0191f69 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.map *.hex *.dis +*.exe # Packages ############ diff --git a/py/builtinimport.c b/py/builtinimport.c index 323b63ef6..f4e089b5d 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -1,12 +1,8 @@ #include <stdint.h> -#include <stdlib.h> #include <stdio.h> #include <string.h> #include <assert.h> -#ifdef __MINGW32__ -// For alloca() -#include <malloc.h> -#endif +#include <alloca.h> #include "mpconfig.h" #include "nlr.h" diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index 34413f0be..2ec5a1fb6 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -29,43 +29,15 @@ def compute_hash(qstr): hash = (hash * 33) ^ ord(char) return hash & 0xffff -# given a list of (name,regex) pairs, find the first one that matches the given line -def re_match_first(regexs, line): - for name, regex in regexs: - match = re.match(regex, line) - if match: - return name, match - return None, None - -# regexs to recognise lines that the CPP emits -# use a list so that matching order is honoured -cpp_regexs = [ - ('qstr', r'Q\((.+)\)$'), - ('cdecl', r'(typedef|extern) [A-Za-z0-9_* ]+;$') -] - def do_work(infiles): # read the qstrs in from the input files qstrs = {} for infile in infiles: with open(infile, 'rt') as f: - line_number = 0 for line in f: - line_number += 1 - line = line.strip() - - # ignore blank lines, comments and preprocessor directives - if len(line) == 0 or line.startswith('//') or line.startswith('#'): - continue - - # work out what kind of line it is - match_kind, match = re_match_first(cpp_regexs, line) - if match_kind is None: - # unknown line format - print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line), file=sys.stderr) - return False - elif match_kind != 'qstr': - # not a line with a qstr + # is this a QSTR line? + match = re.match(r'^Q\((.+)\)$', line.strip()) + if not match: continue # get the qstr value @@ -511,6 +511,7 @@ typedef struct _mp_obj_fun_native_t { // need this so we can define const object bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args, uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2); +const char *mp_obj_fun_get_name(mp_obj_t fun); const char *mp_obj_code_get_name(const byte *code_info); mp_obj_t mp_identity(mp_obj_t self); diff --git a/py/objcell.c b/py/objcell.c index 488a73797..35b44e996 100644 --- a/py/objcell.c +++ b/py/objcell.c @@ -20,14 +20,13 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) { self->obj = obj; } -#if 0 +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED STATIC void cell_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_cell_t *o = o_in; - print(env, "<cell "); + print(env, "<cell %p ", o->obj); if (o->obj == MP_OBJ_NULL) { print(env, "(nil)"); } else { - //print(env, "%p", o->obj); mp_obj_print_helper(print, env, o->obj, PRINT_REPR); } print(env, ">"); @@ -36,8 +35,10 @@ STATIC void cell_print(void (*print)(void *env, const char *fmt, ...), void *env const mp_obj_type_t cell_type = { { &mp_type_type }, - .name = MP_QSTR_, // should never need to print cell type - //.print = cell_print, + .name = MP_QSTR_, // cell representation is just value in < > +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED + .print = cell_print, +#endif }; mp_obj_t mp_obj_new_cell(mp_obj_t obj) { diff --git a/py/objclosure.c b/py/objclosure.c index ca7c537f0..2b83cab71 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -38,10 +38,10 @@ mp_obj_t closure_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t * } } -#if 0 +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED STATIC void closure_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_closure_t *o = o_in; - print(env, "<closure %p, n_closed=%u ", o, o->n_closed); + print(env, "<closure %s at %p, n_closed=%u ", mp_obj_fun_get_name(o->fun), o, o->n_closed); for (int i = 0; i < o->n_closed; i++) { if (o->closed[i] == MP_OBJ_NULL) { print(env, "(nil)"); @@ -57,7 +57,9 @@ STATIC void closure_print(void (*print)(void *env, const char *fmt, ...), void * const mp_obj_type_t closure_type = { { &mp_type_type }, .name = MP_QSTR_closure, - //.print = closure_print, +#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED + .print = closure_print, +#endif .call = closure_call, }; diff --git a/py/objfun.c b/py/objfun.c index e0c25771e..4690dc6c8 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -1,11 +1,7 @@ #include <stdbool.h> -#include <stdlib.h> #include <string.h> #include <assert.h> -#ifdef __MINGW32__ -// For alloca() -#include <malloc.h> -#endif +#include <alloca.h> #include "mpconfig.h" #include "nlr.h" @@ -131,8 +127,9 @@ const char *mp_obj_code_get_name(const byte *code_info) { return qstr_str(block_name); } -const char *mp_obj_fun_get_name(mp_obj_fun_bc_t *o) { - const byte *code_info = o->bytecode; +const char *mp_obj_fun_get_name(mp_obj_t fun_in) { + mp_obj_fun_bc_t *fun = fun_in; + const byte *code_info = fun->bytecode; return mp_obj_code_get_name(code_info); } diff --git a/stmhal/bufhelper.c b/stmhal/bufhelper.c index 5612a048d..c5dcd6c1d 100644 --- a/stmhal/bufhelper.c +++ b/stmhal/bufhelper.c @@ -4,7 +4,7 @@ #include "obj.h" #include "bufhelper.h" -void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, uint8_t *tmp_data) { +void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, byte *tmp_data) { if (MP_OBJ_IS_INT(o)) { tmp_data[0] = mp_obj_get_int(o); bufinfo->buf = tmp_data; diff --git a/stmhal/bufhelper.h b/stmhal/bufhelper.h index 9b58e817c..53ab7ab3e 100644 --- a/stmhal/bufhelper.h +++ b/stmhal/bufhelper.h @@ -1,2 +1,2 @@ -void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, uint8_t *tmp_data); +void pyb_buf_get_for_send(mp_obj_t o, mp_buffer_info_t *bufinfo, byte *tmp_data); mp_obj_t pyb_buf_get_for_recv(mp_obj_t o, mp_buffer_info_t *bufinfo); diff --git a/stmhal/gccollect.c b/stmhal/gccollect.c index d40460d70..8be531416 100644 --- a/stmhal/gccollect.c +++ b/stmhal/gccollect.c @@ -41,9 +41,9 @@ void gc_collect(void) { gc_info_t info; gc_info(&info); printf("GC@%lu %lums\n", start, ticks); - printf(" %lu total\n", info.total); - printf(" %lu : %lu\n", info.used, info.free); - printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block); + printf(" " UINT_FMT " total\n", info.total); + printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free); + printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block); } } diff --git a/stmhal/gendoc.py b/stmhal/gendoc.py index e52ca5d8a..0122579ab 100644 --- a/stmhal/gendoc.py +++ b/stmhal/gendoc.py @@ -343,7 +343,7 @@ def main(): args = cmd_parser.parse_args() if len(args.files) == 0: - args.files = ['modpyb.c', 'accel.c', 'adc.c', 'dac.c', 'extint.c', 'i2c.c', 'led.c', 'pin.c', 'rng.c', 'servo.c', 'spi.c', 'uart.c', 'usrsw.c', 'timer.c'] + args.files = ['modpyb.c', 'accel.c', 'adc.c', 'dac.c', 'extint.c', 'i2c.c', 'led.c', 'pin.c', 'rng.c', 'servo.c', 'spi.c', 'uart.c', 'usrsw.c', 'timer.c', 'rtc.c'] doc = Doc() for file in args.files: diff --git a/stmhal/input.c b/stmhal/input.c index a45a36198..6e98ea960 100644 --- a/stmhal/input.c +++ b/stmhal/input.c @@ -1,3 +1,5 @@ +#include <stdint.h> + #include "mpconfig.h" #include "nlr.h" #include "misc.h" diff --git a/stmhal/modos.c b/stmhal/modos.c index 076a28782..3594cbc3a 100644 --- a/stmhal/modos.c +++ b/stmhal/modos.c @@ -1,3 +1,4 @@ +#include <stdint.h> #include <string.h> #include "mpconfig.h" diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c index 2f53bb523..1ffff1d17 100644 --- a/stmhal/modpyb.c +++ b/stmhal/modpyb.c @@ -82,9 +82,9 @@ STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) { gc_info_t info; gc_info(&info); printf("GC:\n"); - printf(" %lu total\n", info.total); - printf(" %lu : %lu\n", info.used, info.free); - printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block); + printf(" " UINT_FMT " total\n", info.total); + printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free); + printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block); } // free space on flash @@ -143,22 +143,26 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis); /// \function delay(ms) /// Delay for the given number of milliseconds. -STATIC mp_obj_t pyb_delay(mp_obj_t count) { - HAL_Delay(mp_obj_get_int(count)); +STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) { + machine_int_t ms = mp_obj_get_int(ms_in); + if (ms >= 0) { + HAL_Delay(ms); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay); /// \function udelay(us) /// Delay for the given number of microseconds. -STATIC mp_obj_t pyb_udelay(mp_obj_t usec) { - uint32_t count = 0; - const uint32_t utime = (168 * mp_obj_get_int(usec) / 5); - for (;;) { - if (++count > utime) { - return mp_const_none; +STATIC mp_obj_t pyb_udelay(mp_obj_t usec_in) { + machine_int_t usec = mp_obj_get_int(usec_in); + if (usec > 0) { + uint32_t count = 0; + const uint32_t utime = (168 * usec / 4); + while (++count <= utime) { } } + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_udelay_obj, pyb_udelay); diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 24ab20b16..ad55ed632 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -1,5 +1,3 @@ -#include <stdint.h> - // options to control how Micro Python is built #define MICROPY_EMIT_THUMB (1) @@ -51,11 +49,11 @@ extern const struct _mp_obj_module_t time_module; #define BYTES_PER_WORD (4) -#define UINT_FMT "%lu" -#define INT_FMT "%ld" +#define UINT_FMT "%u" +#define INT_FMT "%d" -typedef int32_t machine_int_t; // must be pointer size -typedef uint32_t machine_uint_t; // must be pointer size +typedef int machine_int_t; // must be pointer size +typedef unsigned int machine_uint_t; // must be pointer size typedef void *machine_ptr_t; // must be of pointer size typedef const void *machine_const_ptr_t; // must be of pointer size diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c index 6a8929cb4..294020cec 100644 --- a/stmhal/pyexec.c +++ b/stmhal/pyexec.c @@ -84,9 +84,9 @@ bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bo gc_info_t info; gc_info(&info); printf("GC:\n"); - printf(" %lu total\n", info.total); - printf(" %lu : %lu\n", info.used, info.free); - printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block); + printf(" " UINT_FMT " total\n", info.total); + printf(" " UINT_FMT " : " UINT_FMT "\n", info.used, info.free); + printf(" 1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block); } } diff --git a/stmhal/rtc.c b/stmhal/rtc.c index 79e524cab..ec3c0126b 100644 --- a/stmhal/rtc.c +++ b/stmhal/rtc.c @@ -9,6 +9,18 @@ #include "runtime.h" #include "rtc.h" +/// \moduleref pyb +/// \class RTC - real time clock +/// +/// The RTC is and independent clock that keeps track of the date +/// and time. +/// +/// Example usage: +/// +/// rtc = pyb.RTC() +/// rtc.datetime((2014, 5, 1, 4, 13, 0, 0, 0)) +/// print(rtc.datetime()) + RTC_HandleTypeDef RTCHandle; // rtc_info indicates various things about RTC startup @@ -159,13 +171,13 @@ void rtc_init(void) { // fresh reset; configure RTC Calendar RTC_CalendarConfig(); } else { - // RTC was previously set, so leave it alon + // RTC was previously set, so leave it alone if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) { - // power on reset occured + // power on reset occurred rtc_info |= 0x10000; } if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) { - // external reset occured + // external reset occurred rtc_info |= 0x20000; } // Clear source Reset Flag @@ -213,6 +225,8 @@ typedef struct _pyb_rtc_obj_t { STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}}; +/// \classmethod \constructor() +/// Create an RTC object. STATIC mp_obj_t pyb_rtc_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { // check arguments mp_arg_check_num(n_args, n_kw, 0, 0, false); @@ -221,11 +235,32 @@ STATIC mp_obj_t pyb_rtc_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const return (mp_obj_t)&pyb_rtc_obj; } +/// \method info() +/// Get information about the startup time and reset source. +/// +/// - The lower 0xffff are the number of milliseconds the RTC took to +/// start up. +/// - Bit 0x10000 is set if a power-on reset occurred. +/// - Bit 0x20000 is set if an external reset occurred mp_obj_t pyb_rtc_info(mp_obj_t self_in) { return mp_obj_new_int(rtc_info); } MP_DEFINE_CONST_FUN_OBJ_1(pyb_rtc_info_obj, pyb_rtc_info); +/// \method datetime([datetimetuple]) +/// Get or set the date and time of the RTC. +/// +/// With no arguments, this method returns an 8-tuple with the current +/// date and time. With 1 argument (being an 8-tuple) it sets the date +/// and time. +/// +/// The 8-tuple has the following format: +/// +/// (year, month, day, weekday, hours, minutes, seconds, subseconds) +/// +/// `weekday` is 1-7 for Monday through Sunday. +/// +/// `subseconds` is 0-59. mp_obj_t pyb_rtc_datetime(uint n_args, const mp_obj_t *args) { if (n_args == 1) { // get date and time diff --git a/stmhal/timer.c b/stmhal/timer.c index e1d55e14a..e92a9988f 100644 --- a/stmhal/timer.c +++ b/stmhal/timer.c @@ -467,7 +467,7 @@ void timer_irq_handler(uint tim_id) { // Uncaught exception; disable the callback so it doesn't run again. tim->callback = mp_const_none; __HAL_TIM_DISABLE_IT(&tim->tim, TIM_IT_UPDATE); - printf("Uncaught exception in Timer(%lu) interrupt handler\n", tim->tim_id); + printf("Uncaught exception in Timer(" UINT_FMT ") interrupt handler\n", tim->tim_id); mp_obj_print_exception((mp_obj_t)nlr.ret_val); } gc_unlock(); diff --git a/tools/pyboard.py b/tools/pyboard.py index 2f77ba37c..38d428282 100644 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -50,6 +50,7 @@ class Pyboard: return data def enter_raw_repl(self): + self.serial.write(b'\r\x03') # ctrl-C: interrupt any running program self.serial.write(b'\r\x01') # ctrl-A: enter raw REPL self.serial.write(b'\x04') # ctrl-D: soft reset data = self.read_until(1, b'to exit\r\n>') diff --git a/windows/alloca.h b/windows/alloca.h new file mode 100644 index 000000000..1cf772266 --- /dev/null +++ b/windows/alloca.h @@ -0,0 +1,3 @@ +// Compatibility header to workaround lack of native alloca.h in some +// Windows toolchains. +#include <malloc.h> diff --git a/windows/mpconfigport.h b/windows/mpconfigport.h index 993fef9d5..80d8ac89b 100644 --- a/windows/mpconfigport.h +++ b/windows/mpconfigport.h @@ -12,6 +12,7 @@ #define MICROPY_DEBUG_PRINTERS (1) #define MICROPY_ENABLE_REPL_HELPERS (1) #define MICROPY_ENABLE_LEXER_UNIX (1) +#define MICROPY_MOD_SYS_STDFILES (1) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) @@ -33,3 +34,5 @@ typedef void *machine_ptr_t; // must be of pointer size typedef const void *machine_const_ptr_t; // must be of pointer size extern const struct _mp_obj_fun_native_t mp_builtin_open_obj; +#define MICROPY_EXTRA_BUILTINS \ + { MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, |