summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--bare-arm/main.c2
-rw-r--r--py/argcheck.c2
-rw-r--r--py/builtin.c10
-rw-r--r--py/builtinevex.c2
-rw-r--r--py/builtinimport.c8
-rw-r--r--py/nlr.h1
-rw-r--r--py/nlrsetjmp.c1
-rw-r--r--py/obj.c2
-rw-r--r--py/obj.h11
-rw-r--r--py/objarray.c2
-rw-r--r--py/objbool.c2
-rw-r--r--py/objboundmeth.c2
-rw-r--r--py/objcell.c13
-rw-r--r--py/objclosure.c10
-rw-r--r--py/objcomplex.c2
-rw-r--r--py/objdict.c2
-rw-r--r--py/objexcept.c67
-rw-r--r--py/objfilter.c2
-rw-r--r--py/objfloat.c2
-rw-r--r--py/objfun.c27
-rw-r--r--py/objgenerator.c5
-rw-r--r--py/objgetitemiter.c2
-rw-r--r--py/objint.c2
-rw-r--r--py/objint_longlong.c2
-rw-r--r--py/objint_mpz.c2
-rw-r--r--py/objlist.c2
-rw-r--r--py/objmap.c2
-rw-r--r--py/objmodule.c2
-rw-r--r--py/objnamedtuple.c2
-rw-r--r--py/objnone.c2
-rw-r--r--py/objobject.c2
-rw-r--r--py/objproperty.c2
-rw-r--r--py/objrange.c2
-rw-r--r--py/objset.c2
-rw-r--r--py/objslice.c2
-rw-r--r--py/objstr.c2
-rw-r--r--py/objstringio.c2
-rw-r--r--py/objtuple.c2
-rw-r--r--py/objtype.c45
-rw-r--r--py/objtype.h8
-rw-r--r--py/objzip.c2
-rw-r--r--py/opmethods.c2
-rw-r--r--py/runtime.c2
-rw-r--r--py/sequence.c2
-rw-r--r--py/showbc.c2
-rw-r--r--py/stream.c2
-rw-r--r--py/vm.c2
-rw-r--r--qemu-arm/main.c2
-rw-r--r--stmhal/Makefile8
-rw-r--r--stmhal/accel.c2
-rw-r--r--stmhal/adc.c2
-rwxr-xr-xstmhal/autoflash3
-rw-r--r--stmhal/boards/PYBV3/mpconfigboard.h2
-rw-r--r--stmhal/boards/stm32f4xx-af.csv10
-rw-r--r--stmhal/cc3k/pybcc3k.c2
-rw-r--r--stmhal/dac.c52
-rw-r--r--stmhal/extint.c2
-rw-r--r--stmhal/gendoc.py2
-rw-r--r--stmhal/help.c2
-rw-r--r--stmhal/i2c.c2
-rw-r--r--stmhal/input.c2
-rw-r--r--stmhal/lcd.c2
-rw-r--r--stmhal/led.c2
-rw-r--r--stmhal/modos.c2
-rw-r--r--stmhal/modstm.c2
-rw-r--r--stmhal/modtime.c2
-rw-r--r--stmhal/pin.c4
-rw-r--r--stmhal/pybwlan.c2
-rw-r--r--stmhal/pyexec.c2
-rw-r--r--stmhal/rtc.c41
-rw-r--r--stmhal/servo.c2
-rw-r--r--stmhal/spi.c2
-rw-r--r--stmhal/timer.c66
-rw-r--r--stmhal/uart.c2
-rw-r--r--tests/basics/builtin-minmax.py15
-rw-r--r--tests/basics/subclass-native3.py18
-rw-r--r--tests/float/builtin-float-minmax.py26
-rwxr-xr-xtools/build-stm-latest.sh31
-rw-r--r--tools/pyboard.py15
-rw-r--r--unix-cpy/main.c2
-rw-r--r--unix/file.c2
-rw-r--r--unix/main.c2
-rw-r--r--unix/modffi.c2
-rw-r--r--unix/modsocket.c2
-rw-r--r--windows/alloca.h3
-rw-r--r--windows/mpconfigport.h3
87 files changed, 437 insertions, 185 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/bare-arm/main.c b/bare-arm/main.c
index 8ad9b1587..bdaded436 100644
--- a/bare-arm/main.c
+++ b/bare-arm/main.c
@@ -2,9 +2,9 @@
#include <stdio.h>
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"
diff --git a/py/argcheck.c b/py/argcheck.c
index bfaa5764f..4b849d1fe 100644
--- a/py/argcheck.c
+++ b/py/argcheck.c
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/builtin.c b/py/builtin.c
index 250a3558e..5ff40be0c 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -1,9 +1,9 @@
#include <stdio.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objstr.h"
@@ -245,7 +245,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
mp_obj_t max_obj = NULL;
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
- if (max_obj == NULL || mp_binary_op(MP_BINARY_OP_LESS, max_obj, item)) {
+ if (max_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, max_obj, item) == mp_const_true)) {
max_obj = item;
}
}
@@ -257,7 +257,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
// given many args
mp_obj_t max_obj = args[0];
for (int i = 1; i < n_args; i++) {
- if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i])) {
+ if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i]) == mp_const_true) {
max_obj = args[i];
}
}
@@ -274,7 +274,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
mp_obj_t min_obj = NULL;
mp_obj_t item;
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
- if (min_obj == NULL || mp_binary_op(MP_BINARY_OP_LESS, item, min_obj)) {
+ if (min_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, item, min_obj) == mp_const_true)) {
min_obj = item;
}
}
@@ -286,7 +286,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
// given many args
mp_obj_t min_obj = args[0];
for (int i = 1; i < n_args; i++) {
- if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj)) {
+ if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj) == mp_const_true) {
min_obj = args[i];
}
}
diff --git a/py/builtinevex.c b/py/builtinevex.c
index ae8253737..bc3adb625 100644
--- a/py/builtinevex.c
+++ b/py/builtinevex.c
@@ -1,8 +1,8 @@
#include <stdint.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "lexer.h"
#include "lexerunix.h"
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 07978e61d..f4e089b5d 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -1,16 +1,12 @@
#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"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "lexer.h"
#include "lexerunix.h"
diff --git a/py/nlr.h b/py/nlr.h
index c7b592843..757ae61b2 100644
--- a/py/nlr.h
+++ b/py/nlr.h
@@ -3,7 +3,6 @@
#include <limits.h>
#include <setjmp.h>
-#include "mpconfig.h"
typedef struct _nlr_buf_t nlr_buf_t;
struct _nlr_buf_t {
diff --git a/py/nlrsetjmp.c b/py/nlrsetjmp.c
index a97c8634b..a8756d9d8 100644
--- a/py/nlrsetjmp.c
+++ b/py/nlrsetjmp.c
@@ -1,5 +1,6 @@
#include <setjmp.h>
#include <stdio.h>
+#include "mpconfig.h"
#include "nlr.h"
#if MICROPY_NLR_SETJMP
diff --git a/py/obj.c b/py/obj.c
index 4b7251447..e1cee3c23 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -2,9 +2,9 @@
#include <stdarg.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/obj.h b/py/obj.h
index 35c98d371..0ecc2fe78 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -169,9 +169,10 @@ typedef mp_obj_t (*mp_fun_var_t)(uint n, const mp_obj_t *);
typedef mp_obj_t (*mp_fun_kw_t)(uint n, const mp_obj_t *, mp_map_t *);
typedef enum {
- PRINT_STR,
- PRINT_REPR,
- PRINT_EXC, // Special format for printing exception in unhandled exception message
+ PRINT_STR = 0,
+ PRINT_REPR = 1,
+ PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
+ PRINT_EXC_SUBCLASS = 4, // Internal flag for printing exception subclasses
} mp_print_kind_t;
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind);
@@ -424,6 +425,7 @@ mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in);
// exception
+#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
bool mp_obj_is_exception_type(mp_obj_t self_in);
bool mp_obj_is_exception_instance(mp_obj_t self_in);
bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type);
@@ -431,6 +433,7 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in);
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block);
void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values);
mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
+mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
// str
mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data);
@@ -508,6 +511,8 @@ 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);
MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj);
diff --git a/py/objarray.c b/py/objarray.c
index ea3765576..428e61972 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -1,9 +1,9 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/objbool.c b/py/objbool.c
index 56020914d..c479fd362 100644
--- a/py/objbool.c
+++ b/py/objbool.c
@@ -1,8 +1,8 @@
#include <stdlib.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/objboundmeth.c b/py/objboundmeth.c
index 55b75e42c..346118d51 100644
--- a/py/objboundmeth.c
+++ b/py/objboundmeth.c
@@ -1,8 +1,8 @@
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/objcell.c b/py/objcell.c
index 7fdf07b04..35b44e996 100644
--- a/py/objcell.c
+++ b/py/objcell.c
@@ -1,6 +1,6 @@
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
@@ -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 09371b034..2b83cab71 100644
--- a/py/objclosure.c
+++ b/py/objclosure.c
@@ -1,8 +1,8 @@
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
@@ -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/objcomplex.c b/py/objcomplex.c
index 924420964..45cff6a18 100644
--- a/py/objcomplex.c
+++ b/py/objcomplex.c
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "parsenum.h"
diff --git a/py/objdict.c b/py/objdict.c
index 0654a198e..334798c37 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -2,9 +2,9 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
diff --git a/py/objexcept.c b/py/objexcept.c
index 160cf09fd..abf56ab03 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -2,12 +2,13 @@
#include <stdarg.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
+#include "objtype.h"
#include "runtime.h"
#include "runtime0.h"
@@ -30,12 +31,17 @@ const mp_obj_exception_t mp_const_GeneratorExit_obj = {{&mp_type_GeneratorExit},
STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_exception_t *o = o_in;
- if (kind == PRINT_REPR) {
+ mp_print_kind_t k = kind & ~PRINT_EXC_SUBCLASS;
+ bool is_subclass = kind & PRINT_EXC_SUBCLASS;
+ if (!is_subclass && (k == PRINT_REPR || k == PRINT_EXC)) {
print(env, "%s", qstr_str(o->base.type->name));
- } else if (kind == PRINT_EXC) {
- print(env, "%s: ", qstr_str(o->base.type->name));
}
- if (kind == PRINT_STR || kind == PRINT_EXC) {
+
+ if (k == PRINT_EXC) {
+ print(env, ": ");
+ }
+
+ if (k == PRINT_STR || k == PRINT_EXC) {
if (o->args == NULL || o->args->len == 0) {
print(env, "");
return;
@@ -47,7 +53,7 @@ STATIC void mp_obj_exception_print(void (*print)(void *env, const char *fmt, ...
tuple_print(print, env, o->args, kind);
}
-STATIC mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
+mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
mp_obj_type_t *type = type_in;
if (n_kw != 0) {
@@ -266,38 +272,39 @@ bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type) {
return mp_binary_op(MP_BINARY_OP_EXCEPTION_MATCH, exc, (mp_obj_t)exc_type) == mp_const_true;
}
-void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
- // make sure self_in is an exception instance
- // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
- if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new) {
- mp_obj_exception_t *self = self_in;
-
- // just set the traceback to the null object
- // we don't want to call any memory management functions here
- self->traceback = MP_OBJ_NULL;
+// traceback handling functions
+
+#define GET_NATIVE_EXCEPTION(self, self_in) \
+ /* make sure self_in is an exception instance */ \
+ assert(mp_obj_is_exception_instance(self_in)); \
+ mp_obj_exception_t *self; \
+ if (mp_obj_is_native_exception_instance(self_in)) { \
+ self = self_in; \
+ } else { \
+ self = ((mp_obj_instance_t*)self_in)->subobj[0]; \
}
+
+void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
+ GET_NATIVE_EXCEPTION(self, self_in);
+ // just set the traceback to the null object
+ // we don't want to call any memory management functions here
+ self->traceback = MP_OBJ_NULL;
}
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) {
- // make sure self_in is an exception instance
- // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that)
- if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new && self_in != &mp_emergency_exception_obj) {
- mp_obj_exception_t *self = self_in;
-
- // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
- if (self->traceback == MP_OBJ_NULL) {
- self->traceback = mp_obj_new_list(0, NULL);
- }
- mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
- mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
- mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
+ GET_NATIVE_EXCEPTION(self, self_in);
+
+ // for traceback, we are just using the list object for convenience, it's not really a list of Python objects
+ if (self->traceback == MP_OBJ_NULL) {
+ self->traceback = mp_obj_new_list(0, NULL);
}
+ mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file);
+ mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line);
+ mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block);
}
void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values) {
- // make sure self_in is an exception instance
- assert(mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new);
- mp_obj_exception_t *self = self_in;
+ GET_NATIVE_EXCEPTION(self, self_in);
if (self->traceback == MP_OBJ_NULL) {
*n = 0;
diff --git a/py/objfilter.c b/py/objfilter.c
index 3eacdfc9b..37cb269ad 100644
--- a/py/objfilter.c
+++ b/py/objfilter.c
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/objfloat.c b/py/objfloat.c
index 924916057..14d861b66 100644
--- a/py/objfloat.c
+++ b/py/objfloat.c
@@ -4,9 +4,9 @@
#include <assert.h>
#include <math.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "parsenum.h"
diff --git a/py/objfun.c b/py/objfun.c
index 94f6d26d7..4690dc6c8 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -1,15 +1,11 @@
#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"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
@@ -126,12 +122,17 @@ mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var
/******************************************************************************/
/* byte code functions */
-const char *mp_obj_fun_get_name(mp_obj_fun_bc_t *o) {
- const byte *code_info = o->bytecode;
+const char *mp_obj_code_get_name(const byte *code_info) {
qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
return qstr_str(block_name);
}
+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);
+}
+
#if MICROPY_CPYTHON_COMPAT
STATIC void fun_bc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_fun_bc_t *o = o_in;
@@ -178,6 +179,8 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, ui
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) {
mp_obj_fun_bc_t *self = self_in;
+ DEBUG_printf("mp_obj_fun_prepare_simple_args: given: %d pos, %d kw, expected: %d pos (%d default)\n",
+ n_args, n_kw, self->n_pos_args, self->n_def_args);
assert(n_kw == 0);
assert(self->n_kwonly_args == 0);
@@ -190,8 +193,12 @@ bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, co
if (n_args > self->n_pos_args) {
goto arg_error;
} else {
- extra_args -= self->n_pos_args - n_args;
- n_extra_args += self->n_pos_args - n_args;
+ if (n_args >= self->n_pos_args - self->n_def_args) {
+ extra_args -= self->n_pos_args - n_args;
+ n_extra_args += self->n_pos_args - n_args;
+ } else {
+ fun_pos_args_mismatch(self, self->n_pos_args - self->n_def_args, n_args);
+ }
}
*out_args1 = args;
*out_args1_len = n_args;
diff --git a/py/objgenerator.c b/py/objgenerator.c
index 895c03cc2..d7d6dcaaf 100644
--- a/py/objgenerator.c
+++ b/py/objgenerator.c
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
@@ -68,7 +68,8 @@ typedef struct _mp_obj_gen_instance_t {
} mp_obj_gen_instance_t;
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
- print(env, "<generator object 'fun-name' at %p>", self_in);
+ mp_obj_gen_instance_t *self = self_in;
+ print(env, "<generator object '%s' at %p>", mp_obj_code_get_name(self->code_info), self_in);
}
mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
diff --git a/py/objgetitemiter.c b/py/objgetitemiter.c
index faa8321c3..15676b452 100644
--- a/py/objgetitemiter.c
+++ b/py/objgetitemiter.c
@@ -1,8 +1,8 @@
#include <stdlib.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/objint.c b/py/objint.c
index 110876e00..bec4e920f 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -3,9 +3,9 @@
#include <assert.h>
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "parsenum.h"
diff --git a/py/objint_longlong.c b/py/objint_longlong.c
index 7d71c5a69..d319e2746 100644
--- a/py/objint_longlong.c
+++ b/py/objint_longlong.c
@@ -2,9 +2,9 @@
#include <stdint.h>
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "mpz.h"
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index b94dcfee3..bfc7def35 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -3,9 +3,9 @@
#include <stdio.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "parsenumbase.h"
#include "obj.h"
diff --git a/py/objlist.c b/py/objlist.c
index 0c55f524d..d886affff 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -1,9 +1,9 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/objmap.c b/py/objmap.c
index 45e65549d..c6f8f1657 100644
--- a/py/objmap.c
+++ b/py/objmap.c
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/objmodule.c b/py/objmodule.c
index 15ccd68ac..d4a45b84a 100644
--- a/py/objmodule.c
+++ b/py/objmodule.c
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objmodule.h"
diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c
index 423998358..99a1d7bb9 100644
--- a/py/objnamedtuple.c
+++ b/py/objnamedtuple.c
@@ -1,7 +1,7 @@
#include <string.h>
-#include "misc.h"
#include "mpconfig.h"
+#include "misc.h"
#include "nlr.h"
#include "qstr.h"
#include "obj.h"
diff --git a/py/objnone.c b/py/objnone.c
index fc55132f3..49b726bdd 100644
--- a/py/objnone.c
+++ b/py/objnone.c
@@ -1,8 +1,8 @@
#include <stdlib.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/objobject.c b/py/objobject.c
index 74a4926b3..62fd0c4c9 100644
--- a/py/objobject.c
+++ b/py/objobject.c
@@ -1,8 +1,8 @@
#include <stdlib.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/objproperty.c b/py/objproperty.c
index dd4eedebd..220388df0 100644
--- a/py/objproperty.c
+++ b/py/objproperty.c
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/objrange.c b/py/objrange.c
index 8c0eadd32..acfeed7d9 100644
--- a/py/objrange.c
+++ b/py/objrange.c
@@ -1,8 +1,8 @@
#include <stdlib.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/objset.c b/py/objset.c
index db3683f60..1d10f94e8 100644
--- a/py/objset.c
+++ b/py/objset.c
@@ -2,9 +2,9 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/objslice.c b/py/objslice.c
index 924927db5..0488eb422 100644
--- a/py/objslice.c
+++ b/py/objslice.c
@@ -1,9 +1,9 @@
#include <stdlib.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/objstr.c b/py/objstr.c
index aab1a4492..fc7415fe1 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -2,9 +2,9 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/objstringio.c b/py/objstringio.c
index a3a756630..8b3dfaf61 100644
--- a/py/objstringio.c
+++ b/py/objstringio.c
@@ -1,9 +1,9 @@
#include <stdio.h>
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/objtuple.c b/py/objtuple.c
index e6b902d74..792c65193 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -1,9 +1,9 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/objtype.c b/py/objtype.c
index 56a5dd937..0eb09e583 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -3,13 +3,14 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
#include "runtime.h"
+#include "objtype.h"
#if 0 // print debugging info
#define DEBUG_PRINT (1)
@@ -19,21 +20,13 @@
#endif
/******************************************************************************/
-// class object
-// creating an instance of a class makes one of these objects
-
-typedef struct _mp_obj_class_t {
- mp_obj_base_t base;
- mp_map_t members;
- mp_obj_t subobj[];
- // TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them
-} mp_obj_class_t;
+// instance object
#define is_native_type(type) ((type)->make_new != class_make_new)
STATIC mp_obj_t class_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args);
STATIC mp_obj_t mp_obj_new_class(mp_obj_t class, uint subobjs) {
- mp_obj_class_t *o = m_new_obj_var(mp_obj_class_t, mp_obj_t, subobjs);
+ mp_obj_instance_t *o = m_new_obj_var(mp_obj_instance_t, mp_obj_t, subobjs);
o->base.type = class;
mp_map_init(&o->members, 0);
mp_seq_clear(o->subobj, 0, subobjs, sizeof(*o->subobj));
@@ -71,7 +64,7 @@ STATIC int class_count_native_bases(const mp_obj_type_t *type, const mp_obj_type
// it was - because instance->subobj[0] is of that type. The only exception is when
// object is not yet constructed, then we need to know base native type to construct
// instance->subobj[0]. This case is handled via class_count_native_bases() though.
-STATIC void mp_obj_class_lookup(mp_obj_class_t *o, const mp_obj_type_t *type, qstr attr, machine_uint_t meth_offset, mp_obj_t *dest) {
+STATIC void mp_obj_class_lookup(mp_obj_instance_t *o, const mp_obj_type_t *type, qstr attr, machine_uint_t meth_offset, mp_obj_t *dest) {
assert(dest[0] == NULL);
assert(dest[1] == NULL);
for (;;) {
@@ -138,7 +131,7 @@ STATIC void mp_obj_class_lookup(mp_obj_class_t *o, const mp_obj_type_t *type, qs
}
STATIC void class_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
- mp_obj_class_t *self = self_in;
+ mp_obj_instance_t *self = self_in;
qstr meth = (kind == PRINT_STR) ? MP_QSTR___str__ : MP_QSTR___repr__;
mp_obj_t member[2] = {MP_OBJ_NULL};
mp_obj_class_lookup(self, self->base.type, meth, offsetof(mp_obj_type_t, print), member);
@@ -148,7 +141,15 @@ STATIC void class_print(void (*print)(void *env, const char *fmt, ...), void *en
}
if (member[0] == MP_OBJ_SENTINEL) {
- mp_obj_print_helper(print, env, self->subobj[0], kind);
+ // Handle Exception subclasses specially
+ if (mp_obj_is_native_exception_instance(self->subobj[0])) {
+ if (kind != PRINT_STR) {
+ print(env, "%s", qstr_str(self->base.type->name));
+ }
+ mp_obj_print_helper(print, env, self->subobj[0], kind | PRINT_EXC_SUBCLASS);
+ } else {
+ mp_obj_print_helper(print, env, self->subobj[0], kind);
+ }
return;
}
@@ -170,7 +171,7 @@ STATIC mp_obj_t class_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const m
uint num_native_bases = class_count_native_bases(self, &native_base);
assert(num_native_bases < 2);
- mp_obj_class_t *o = mp_obj_new_class(self_in, num_native_bases);
+ mp_obj_instance_t *o = mp_obj_new_class(self_in, num_native_bases);
// look for __init__ function
mp_obj_t init_fn[2] = {MP_OBJ_NULL};
@@ -188,7 +189,7 @@ STATIC mp_obj_t class_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const m
// now call Python class __init__ function with all args
mp_obj_t init_ret;
if (n_args == 0 && n_kw == 0) {
- init_ret = mp_call_function_n_kw(init_fn[0], 1, 0, (mp_obj_t*)&o);
+ init_ret = mp_call_function_n_kw(init_fn[0], 1, 0, (mp_obj_t*)(void*)&o);
} else {
mp_obj_t *args2 = m_new(mp_obj_t, 1 + n_args + 2 * n_kw);
args2[0] = o;
@@ -219,7 +220,7 @@ STATIC const qstr unary_op_method_name[] = {
};
STATIC mp_obj_t class_unary_op(int op, mp_obj_t self_in) {
- mp_obj_class_t *self = self_in;
+ mp_obj_instance_t *self = self_in;
qstr op_name = unary_op_method_name[op];
/* Still try to lookup native slot
if (op_name == 0) {
@@ -303,7 +304,7 @@ STATIC void class_convert_return_attr(mp_obj_t self, mp_obj_t member, mp_obj_t *
STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
// Note: For ducktyping, CPython does not look in the instance members or use
// __getattr__ or __getattribute__. It only looks in the class dictionary.
- mp_obj_class_t *lhs = lhs_in;
+ mp_obj_instance_t *lhs = lhs_in;
qstr op_name = binary_op_method_name[op];
/* Still try to lookup native slot
if (op_name == 0) {
@@ -327,7 +328,7 @@ STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
// logic: look in obj members then class locals (TODO check this against CPython)
- mp_obj_class_t *self = self_in;
+ mp_obj_instance_t *self = self_in;
mp_map_elem_t *elem = mp_map_lookup(&self->members, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
if (elem != NULL) {
@@ -380,7 +381,7 @@ STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
}
STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
- mp_obj_class_t *self = self_in;
+ mp_obj_instance_t *self = self_in;
#if MICROPY_ENABLE_PROPERTY
// for property, we need to do a lookup first in the class dict
@@ -414,7 +415,7 @@ STATIC bool class_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
}
STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
- mp_obj_class_t *self = self_in;
+ mp_obj_instance_t *self = self_in;
mp_obj_t member[2] = {MP_OBJ_NULL};
uint meth_args;
if (value == MP_OBJ_NULL) {
@@ -447,7 +448,7 @@ STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
}
STATIC mp_obj_t class_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
- mp_obj_class_t *self = self_in;
+ mp_obj_instance_t *self = self_in;
mp_obj_t member[2] = {MP_OBJ_NULL};
mp_obj_class_lookup(self, self->base.type, MP_QSTR___call__, offsetof(mp_obj_type_t, call), member);
if (member[0] == MP_OBJ_NULL) {
diff --git a/py/objtype.h b/py/objtype.h
new file mode 100644
index 000000000..c3176c328
--- /dev/null
+++ b/py/objtype.h
@@ -0,0 +1,8 @@
+// instance object
+// creating an instance of a class makes one of these objects
+typedef struct _mp_obj_instance_t {
+ mp_obj_base_t base;
+ mp_map_t members;
+ mp_obj_t subobj[];
+ // TODO maybe cache __getattr__ and __setattr__ for efficient lookup of them
+} mp_obj_instance_t;
diff --git a/py/objzip.c b/py/objzip.c
index 4c7070b7c..c3bffd830 100644
--- a/py/objzip.c
+++ b/py/objzip.c
@@ -1,8 +1,8 @@
#include <stdlib.h>
#include <assert.h>
-#include "misc.h"
#include "mpconfig.h"
+#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/opmethods.c b/py/opmethods.c
index a745ec122..b029eef7c 100644
--- a/py/opmethods.c
+++ b/py/opmethods.c
@@ -1,6 +1,6 @@
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/py/runtime.c b/py/runtime.c
index b56740a02..2928de8a7 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -2,9 +2,9 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
diff --git a/py/sequence.c b/py/sequence.c
index f91bf43c7..9fce1c51a 100644
--- a/py/sequence.c
+++ b/py/sequence.c
@@ -2,9 +2,9 @@
#include <stdbool.h>
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime0.h"
diff --git a/py/showbc.c b/py/showbc.c
index e3032e8d9..b5b5ce44a 100644
--- a/py/showbc.c
+++ b/py/showbc.c
@@ -1,8 +1,8 @@
#include <stdio.h>
#include <assert.h>
-#include "misc.h"
#include "mpconfig.h"
+#include "misc.h"
#include "qstr.h"
#include "bc0.h"
diff --git a/py/stream.c b/py/stream.c
index 72e6ab509..113eaa46e 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -1,8 +1,8 @@
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "stream.h"
diff --git a/py/vm.c b/py/vm.c
index d3eb97042..f2ca91be5 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -2,9 +2,9 @@
#include <string.h>
#include <assert.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "emitglue.h"
diff --git a/qemu-arm/main.c b/qemu-arm/main.c
index 6ecc9a4b2..91c096289 100644
--- a/qemu-arm/main.c
+++ b/qemu-arm/main.c
@@ -2,9 +2,9 @@
#include <stdio.h>
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "lexer.h"
#include "parse.h"
diff --git a/stmhal/Makefile b/stmhal/Makefile
index a32d9afbb..09c03d71e 100644
--- a/stmhal/Makefile
+++ b/stmhal/Makefile
@@ -13,6 +13,8 @@ USBDEV_DIR=usbdev
FATFS_DIR=fatfs
CC3K_DIR=cc3k
DFU=../tools/dfu.py
+# may need to prefix dfu-util with sudo
+DFU_UTIL=dfu-util
CROSS_COMPILE = arm-none-eabi-
@@ -189,6 +191,12 @@ OBJ += $(BUILD)/pins_$(BOARD).o
all: $(BUILD)/flash.dfu
+.PHONY: flashboard
+
+flashboard: $(BUILD)/flash.dfu
+ $(ECHO) "Writing $< to the board"
+ $(Q)$(DFU_UTIL) -a 0 -D $<
+
$(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin
$(ECHO) "Create $@"
$(Q)$(PYTHON) $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@
diff --git a/stmhal/accel.c b/stmhal/accel.c
index d3373f1eb..64ed8b2f1 100644
--- a/stmhal/accel.c
+++ b/stmhal/accel.c
@@ -3,9 +3,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/stmhal/adc.c b/stmhal/adc.c
index 0cc2a9a90..e04571abe 100644
--- a/stmhal/adc.c
+++ b/stmhal/adc.c
@@ -2,9 +2,9 @@
#include <stm32f4xx_hal.h>
#include <string.h>
+#include "mpconfig.h"
#include "misc.h"
#include "nlr.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/stmhal/autoflash b/stmhal/autoflash
index dc28a97e2..d2240ccb5 100755
--- a/stmhal/autoflash
+++ b/stmhal/autoflash
@@ -8,6 +8,7 @@
# - run a terminal
SERIAL=/dev/ttyACM0
+DEVICE=0483:df11
while true; do
echo "waiting for DFU device..."
@@ -19,7 +20,7 @@ while true; do
done
echo "found DFU device, flashing"
- dfu-util -a 0 -D build/flash.dfu
+ dfu-util -a 0 -d $DEVICE -D build/flash.dfu
echo "waiting for DFU to exit..."
while true; do
diff --git a/stmhal/boards/PYBV3/mpconfigboard.h b/stmhal/boards/PYBV3/mpconfigboard.h
index 02ad78a64..af4da0c1d 100644
--- a/stmhal/boards/PYBV3/mpconfigboard.h
+++ b/stmhal/boards/PYBV3/mpconfigboard.h
@@ -12,7 +12,7 @@
#define MICROPY_HW_ENABLE_RTC (1)
#define MICROPY_HW_ENABLE_TIMER (1)
#define MICROPY_HW_ENABLE_SERVO (1)
-#define MICROPY_HW_ENABLE_DAC (0)
+#define MICROPY_HW_ENABLE_DAC (1)
#define MICROPY_HW_ENABLE_I2C1 (1)
#define MICROPY_HW_ENABLE_SPI1 (1)
#define MICROPY_HW_ENABLE_SPI3 (0)
diff --git a/stmhal/boards/stm32f4xx-af.csv b/stmhal/boards/stm32f4xx-af.csv
index 0a21fc87d..5746ac8cc 100644
--- a/stmhal/boards/stm32f4xx-af.csv
+++ b/stmhal/boards/stm32f4xx-af.csv
@@ -16,20 +16,20 @@ PortA,PA12,,TIM1_ETR,,,,,,USART1_RTS,,CAN1_TX,OTG_FS_DP,,,,,EVENTOUT
PortA,PA13,JTMS-SWDIO,,,,,,,,,,,,,,,EVENTOUT
PortA,PA14,JTCK-SWCLK,,,,,,,,,,,,,,,EVENTOUT
PortA,PA15,JTDI,TIM2_CH1/TIM2_ETR,,,,SPI1_NSS,SPI3_NSS/I2S3_WS,,,,,,,,,EVENTOUT
-PortB,PB0,,TIM1_CH2N,TIM3_CH3,TIM8_CH2N,,,,,,,OTG_HS_ULPI_D1,ETH_MII_RXD2,,,,EVENTOUT,AC12_IN8
+PortB,PB0,,TIM1_CH2N,TIM3_CH3,TIM8_CH2N,,,,,,,OTG_HS_ULPI_D1,ETH_MII_RXD2,,,,EVENTOUT,ADC12_IN8
PortB,PB1,,TIM1_CH3N,TIM3_CH4,TIM8_CH3N,,,,,,,OTG_HS_ULPI_D2,ETH_MII_RXD3,,,,EVENTOUT,ADC12_IN9
PortB,PB2,,,,,,,,,,,,,,,,EVENTOUT
-PortB,PB3,JTDO/TRACESWO,TIM2_CH2,,,,SPI1_SCK,SPI3_SCKI2S3_CK,,,,,,,,,EVENTOUT
+PortB,PB3,JTDO/TRACESWO,TIM2_CH2,,,,SPI1_SCK,SPI3_SCK/I2S3_CK,,,,,,,,,EVENTOUT
PortB,PB4,NJTRST,,TIM3_CH1,,,SPI1_MISO,SPI3_MISO,I2S3ext_SD,,,,,,,,EVENTOUT
PortB,PB5,,,TIM3_CH2,,I2C1_SMBA,SPI1_MOSI,SPI3_MOSI/I2S3_SD,,,CAN2_RX,OTG_HS_ULPI_D7,ETH_PPS_OUT,,DCMI_D10,,EVENTOUT
PortB,PB6,,,TIM4_CH1,,I2C1_SCL,,,USART1_TX,,CAN2_TX,,,,DCMI_D5,,EVENTOUT
PortB,PB7,,,TIM4_CH2,,I2C1_SDA,,,USART1_RX,,,,,FSMC_NL,DCMI_VSYNC,,EVENTOUT
PortB,PB8,,,TIM4_CH3,TIM10_CH1,I2C1_SCL,,,,,CAN1_RX,,ETH_MII_TXD3,SDIO_D4,DCMI_D6,,EVENTOUT
PortB,PB9,,,TIM4_CH4,TIM11_CH1,I2C1_SDA,SPI2_NSS/I2S2_WS,,,,CAN1_TX,,,SDIO_D5,DCMI_D7,,EVENTOUT
-PortB,PB10,,TIM2_CH3,,,I2C2_SCL,SPI2_SCKI2S2_CK,,USART3_TX,,,OTG_HS_ULPI_D3,ETH_MII_RX_ER,,,,EVENTOUT
+PortB,PB10,,TIM2_CH3,,,I2C2_SCL,SPI2_SCK/I2S2_CK,,USART3_TX,,,OTG_HS_ULPI_D3,ETH_MII_RX_ER,,,,EVENTOUT
PortB,PB11,,TIM2_CH4,,,I2C2_SDA,,,USART3_RX,,,OTG_HS_ULPI_D4,ETH_MII_TX_EN/ETH_RMII_TX_EN,,,,EVENTOUT
PortB,PB12,,TIM1_BKIN,,,I2C2_SMBA,SPI2_NSS/I2S2_WS,,USART3_CK,,CAN2_RX,OTG_HS_ULPI_D5,ETH_MII_TXD0/ETH_RMII_TXD0,OTG_HS_ID,,,EVENTOUT
-PortB,PB13,,TIM1_CH1N,,,,SPI2_SCKI2S2_CK,,USART3_CTS,,CAN2_TX,OTG_HS_ULPI_D6,ETH_MII_TXD1/ETH_RMII_TXD1,,,,EVENTOUT
+PortB,PB13,,TIM1_CH1N,,,,SPI2_SCK/I2S2_CK,,USART3_CTS,,CAN2_TX,OTG_HS_ULPI_D6,ETH_MII_TXD1/ETH_RMII_TXD1,,,,EVENTOUT
PortB,PB14,,TIM1_CH2N,,TIM8_CH2N,,SPI2_MISO,I2S2ext_SD,USART3_RTS,,TIM12_CH1,,,OTG_HS_DM,,,EVENTOUT
PortB,PB15,RTC_REFIN,TIM1_CH3N,,TIM8_CH3N,,SPI2_MOSI/I2S2_SD,,,,TIM12_CH2,,,OTG_HS_DP,,,EVENTOUT
PortC,PC0,,,,,,,,,,,OTG_HS_ULPI_STP,,,,,EVENTOUT,ADC123_IN10
@@ -129,7 +129,7 @@ PortH,PH13,,,,TIM8_CH1N,,,,,,CAN1_TX,,,,,,EVENTOUT
PortH,PH14,,,,TIM8_CH2N,,,,,,,,,,DCMI_D4,,EVENTOUT
PortH,PH15,,,,TIM8_CH3N,,,,,,,,,,DCMI_D11,,EVENTOUT
PortI,PI0,,,TIM5_CH4,,,SPI2_NSS/I2S2_WS,,,,,,,,DCMI_D13,,EVENTOUT
-PortI,PI1,,,,,,SPI2_SCKI2S2_CK,,,,,,,,DCMI_D8,,EVENTOUT
+PortI,PI1,,,,,,SPI2_SCK/I2S2_CK,,,,,,,,DCMI_D8,,EVENTOUT
PortI,PI2,,,,TIM8_CH4,,SPI2_MISO,I2S2ext_SD,,,,,,,DCMI_D9,,EVENTOUT
PortI,PI3,,,,TIM8_ETR,,SPI2_MOSI/I2S2_SD,,,,,,,,DCMI_D10,,EVENTOUT
PortI,PI4,,,,TIM8_BKIN,,,,,,,,,,DCMI_D5,,EVENTOUT
diff --git a/stmhal/cc3k/pybcc3k.c b/stmhal/cc3k/pybcc3k.c
index 17b205b94..8712a3780 100644
--- a/stmhal/cc3k/pybcc3k.c
+++ b/stmhal/cc3k/pybcc3k.c
@@ -2,9 +2,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/stmhal/dac.c b/stmhal/dac.c
index cd62f017f..56674c431 100644
--- a/stmhal/dac.c
+++ b/stmhal/dac.c
@@ -3,9 +3,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "parse.h"
#include "obj.h"
@@ -16,6 +16,31 @@
/// \moduleref pyb
/// \class DAC - digital to analog conversion
///
+/// The DAC is used to output analog values (a specific voltage) on pin X5 or pin X6.
+/// The voltage will be between 0 and 3.3V.
+///
+/// *This module will undergo changes to the API.*
+///
+/// Example usage:
+///
+/// from pyb import DAC
+///
+/// dac = DAC(1) # create DAC 1 on pin X5
+/// dac.write(128) # write a value to the DAC (makes X5 1.65V)
+///
+/// To output a continuous sine-wave:
+///
+/// import math
+/// from pyb import DAC
+///
+/// # create a buffer containing a sine-wave
+/// buf = bytearray(100)
+/// for i in range(len(buf)):
+/// buf[i] = 128 + 127 * math.sin(2 * math.pi * i / len(buf))
+///
+/// # output the sine-wave at 400Hz
+/// dac = DAC(1)
+/// dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
STATIC DAC_HandleTypeDef DAC_Handle;
@@ -52,6 +77,10 @@ typedef struct _pyb_dac_obj_t {
// create the dac object
// currently support either DAC1 on X5 (id = 1) or DAC2 on X6 (id = 2)
+/// \classmethod \constructor(id)
+/// Construct a new DAC object.
+///
+/// `id` can be 1 or 2: DAC 1 is on pin X5 and DAC 2 is on pin X6.
STATIC mp_obj_t pyb_dac_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, 1, 1, false);
@@ -93,6 +122,9 @@ STATIC mp_obj_t pyb_dac_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
return dac;
}
+/// \method noise(freq)
+/// Generate a pseudo-random noise signal. A new random sample is written
+/// to the DAC output at the given frequency.
STATIC mp_obj_t pyb_dac_noise(mp_obj_t self_in, mp_obj_t freq) {
pyb_dac_obj_t *self = self_in;
@@ -117,6 +149,10 @@ STATIC mp_obj_t pyb_dac_noise(mp_obj_t self_in, mp_obj_t freq) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_noise_obj, pyb_dac_noise);
+/// \method triangle(freq)
+/// Generate a triangle wave. The value on the DAC output changes at
+/// the given frequency, and the frequence of the repeating triangle wave
+/// itself is 256 (or 1024, need to check) times smaller.
STATIC mp_obj_t pyb_dac_triangle(mp_obj_t self_in, mp_obj_t freq) {
pyb_dac_obj_t *self = self_in;
@@ -141,7 +177,8 @@ STATIC mp_obj_t pyb_dac_triangle(mp_obj_t self_in, mp_obj_t freq) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_triangle_obj, pyb_dac_triangle);
-// direct access to DAC (8 bit only at the moment)
+/// \method write(value)
+/// Direct access to the DAC output (8 bit only at the moment).
STATIC mp_obj_t pyb_dac_write(mp_obj_t self_in, mp_obj_t val) {
pyb_dac_obj_t *self = self_in;
@@ -160,12 +197,15 @@ STATIC mp_obj_t pyb_dac_write(mp_obj_t self_in, mp_obj_t val) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_dac_write_obj, pyb_dac_write);
-// initiates a burst of RAM->DAC using DMA
-// input data is treated as an array of bytes (8 bit data)
-// TIM6 is used to set the frequency of the transfer
+/// \method write_timed(data, freq, *, mode=DAC.NORMAL)
+/// Initiates a burst of RAM to DAC using a DMA transfer.
+/// The input data is treated as an array of bytes (8 bit data).
+///
+/// `mode` can be `DAC.NORMAL` or `DAC.CIRCULAR`.
+///
+/// TIM6 is used to control the frequency of the transfer.
// TODO add callback argument, to call when transfer is finished
// TODO add double buffer argument
-
STATIC const mp_arg_t pyb_dac_write_timed_args[] = {
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_freq, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
diff --git a/stmhal/extint.c b/stmhal/extint.c
index 61469e0a1..90016d19a 100644
--- a/stmhal/extint.c
+++ b/stmhal/extint.c
@@ -4,9 +4,9 @@
#include <stm32f4xx_hal.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "gc.h"
#include "obj.h"
diff --git a/stmhal/gendoc.py b/stmhal/gendoc.py
index 42882ad27..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']
+ 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/help.c b/stmhal/help.c
index 975a10244..084d708e6 100644
--- a/stmhal/help.c
+++ b/stmhal/help.c
@@ -1,8 +1,8 @@
#include <stdio.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
diff --git a/stmhal/i2c.c b/stmhal/i2c.c
index ef9da496d..b8de85530 100644
--- a/stmhal/i2c.c
+++ b/stmhal/i2c.c
@@ -3,9 +3,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/stmhal/input.c b/stmhal/input.c
index 0ded89866..a45a36198 100644
--- a/stmhal/input.c
+++ b/stmhal/input.c
@@ -1,6 +1,6 @@
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "readline.h"
diff --git a/stmhal/lcd.c b/stmhal/lcd.c
index efee13d23..f121a93d7 100644
--- a/stmhal/lcd.c
+++ b/stmhal/lcd.c
@@ -2,9 +2,9 @@
#include <string.h>
#include <stm32f4xx_hal.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#if MICROPY_HW_HAS_LCD
diff --git a/stmhal/led.c b/stmhal/led.c
index 9e32c5248..264a47548 100644
--- a/stmhal/led.c
+++ b/stmhal/led.c
@@ -1,9 +1,9 @@
#include <stdio.h>
#include <stm32f4xx_hal.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/stmhal/modos.c b/stmhal/modos.c
index 33b4ff73e..076a28782 100644
--- a/stmhal/modos.c
+++ b/stmhal/modos.c
@@ -1,8 +1,8 @@
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "systick.h"
diff --git a/stmhal/modstm.c b/stmhal/modstm.c
index 078f590ec..a2b72995a 100644
--- a/stmhal/modstm.c
+++ b/stmhal/modstm.c
@@ -3,9 +3,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "modstm.h"
diff --git a/stmhal/modtime.c b/stmhal/modtime.c
index 83be2deaa..410c6391e 100644
--- a/stmhal/modtime.c
+++ b/stmhal/modtime.c
@@ -1,8 +1,8 @@
#include <stm32f4xx_hal.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "modtime.h"
diff --git a/stmhal/pin.c b/stmhal/pin.c
index 06923a492..5b0f998de 100644
--- a/stmhal/pin.c
+++ b/stmhal/pin.c
@@ -4,9 +4,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
@@ -179,7 +179,7 @@ STATIC mp_obj_t pin_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_
if (n_args >= 2) {
// pin mode given, so configure this GPIO
- mp_obj_t args2[3] = {(mp_obj_t)pin, args2[1], MP_OBJ_NULL};
+ mp_obj_t args2[3] = {(mp_obj_t)pin, args[1], MP_OBJ_NULL};
if (n_args == 3) {
args2[2] = args[2];
}
diff --git a/stmhal/pybwlan.c b/stmhal/pybwlan.c
index 72cb9bfd9..06309b61c 100644
--- a/stmhal/pybwlan.c
+++ b/stmhal/pybwlan.c
@@ -5,9 +5,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c
index 3f9482ec5..6a8929cb4 100644
--- a/stmhal/pyexec.c
+++ b/stmhal/pyexec.c
@@ -3,9 +3,9 @@
#include <stm32f4xx_hal.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "misc.h"
#include "lexer.h"
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/servo.c b/stmhal/servo.c
index 2ebe64376..68e211bcb 100644
--- a/stmhal/servo.c
+++ b/stmhal/servo.c
@@ -2,9 +2,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/stmhal/spi.c b/stmhal/spi.c
index ba550f979..c1b8e75d0 100644
--- a/stmhal/spi.c
+++ b/stmhal/spi.c
@@ -3,9 +3,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/stmhal/timer.c b/stmhal/timer.c
index 1578e9b1d..e1d55e14a 100644
--- a/stmhal/timer.c
+++ b/stmhal/timer.c
@@ -16,6 +16,38 @@
#include "timer.h"
#include "servo.h"
+/// \moduleref pyb
+/// \class Timer - periodically call a function
+///
+/// Timers can be used for a great variety of tasks. At the moment, only
+/// the simplest case is implemented: that of calling a function periodically.
+///
+/// Each timer consists of a counter that counts up at a certain rate. The rate
+/// at which it counts is the peripheral clock frequency (in Hz) divided by the
+/// timer prescaler. When the counter reaches the timer period it triggers an
+/// event, and the counter resets back to zero. By using the callback method,
+/// the timer event can call a Python function.
+///
+/// Example usage to toggle an LED at a fixed frequency:
+///
+/// tim = pyb.Timer(4) # create a timer object using timer 4
+/// tim.init(freq=2) # trigger at 2Hz
+/// tim.callback(lambda t:pyb.LED(1).toggle())
+///
+/// Further examples:
+///
+/// tim = pyb.Timer(4, freq=100) # freq in Hz
+/// tim = pyb.Timer(4, prescaler=1, period=100)
+/// tim.counter() # get counter (can also set)
+/// tim.prescaler(2) # set prescaler (can also get)
+/// tim.period(200) # set period (can also get)
+/// tim.callback(lambda t: ...) # set callback for update interrupt (t=tim instance)
+/// tim.callback(None) # clear callback
+///
+/// *Note:* Timer 3 is reserved for internal use. Timer 5 controls
+/// the servo driver, and Timer 6 is used for timed ADC/DAC reading/writing.
+/// It is recommended to use the other timers in your programs.
+
// The timers can be used by multiple drivers, and need a common point for
// the interrupts to be dispatched, so they are all collected here.
//
@@ -29,16 +61,6 @@
//
// TIM6:
// - ADC, DAC for read_timed and write_timed
-//
-// Python usage model:
-//
-// tim = pyb.Timer(4, freq=100) # freq in Hz
-// tim = pyb.Timer(4, prescaler=1, period=100)
-// tim.counter() # get counter (can also set)
-// tim.prescaler(2) # set prescaler (can also get)
-// tim.period(200) # set period (can also get)
-// tim.callback(lambda t: ...) # set callback for update interrupt (t=tim instance)
-// tim.callback(None) # clear callback
typedef struct _pyb_timer_obj_t {
mp_obj_base_t base;
@@ -173,6 +195,12 @@ STATIC void pyb_timer_print(void (*print)(void *env, const char *fmt, ...), void
}
}
+/// \method init(*, freq, prescaler, period)
+/// Initialise the timer. Initialisation must be either by frequency (in Hz)
+/// or by prescaler and period:
+///
+/// tim.init(freq=100) # set the timer to trigger at 100Hz
+/// tim.init(prescaler=100, period=300) # set the prescaler and period directly
STATIC const mp_arg_t pyb_timer_init_args[] = {
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
{ MP_QSTR_prescaler, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
@@ -257,6 +285,10 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, uint n_args, const
return mp_const_none;
}
+/// \classmethod \constructor(id, ...)
+/// Construct a new timer object of the given id. If additional
+/// arguments are given, then the timer is initialised by `init(...)`.
+/// `id` can be 1 to 14, excluding 3.
STATIC mp_obj_t pyb_timer_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, 1, MP_OBJ_FUN_ARGS_MAX, true);
@@ -308,6 +340,10 @@ STATIC mp_obj_t pyb_timer_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_a
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_timer_init_obj, 1, pyb_timer_init);
+/// \method deinit()
+/// Deinitialises the timer.
+///
+/// *This function is not yet implemented.*
STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in) {
//pyb_timer_obj_t *self = self_in;
// TODO implement me
@@ -315,6 +351,8 @@ STATIC mp_obj_t pyb_timer_deinit(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_timer_deinit_obj, pyb_timer_deinit);
+/// \method counter([value])
+/// Get or set the timer counter.
mp_obj_t pyb_timer_counter(uint n_args, const mp_obj_t *args) {
pyb_timer_obj_t *self = args[0];
if (n_args == 1) {
@@ -328,6 +366,8 @@ mp_obj_t pyb_timer_counter(uint n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_counter_obj, 1, 2, pyb_timer_counter);
+/// \method prescaler([value])
+/// Get or set the prescaler for the timer.
mp_obj_t pyb_timer_prescaler(uint n_args, const mp_obj_t *args) {
pyb_timer_obj_t *self = args[0];
if (n_args == 1) {
@@ -341,6 +381,8 @@ mp_obj_t pyb_timer_prescaler(uint n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_prescaler_obj, 1, 2, pyb_timer_prescaler);
+/// \method period([value])
+/// Get or set the period of the timer.
mp_obj_t pyb_timer_period(uint n_args, const mp_obj_t *args) {
pyb_timer_obj_t *self = args[0];
if (n_args == 1) {
@@ -354,6 +396,10 @@ mp_obj_t pyb_timer_period(uint n_args, const mp_obj_t *args) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_timer_period_obj, 1, 2, pyb_timer_period);
+/// \method callback(fun)
+/// Set the function to be called when the timer triggers.
+/// `fun` is passed 1 argument, the timer object.
+/// If `fun` is `None` then the callback will be disabled.
STATIC mp_obj_t pyb_timer_callback(mp_obj_t self_in, mp_obj_t callback) {
pyb_timer_obj_t *self = self_in;
if (callback == mp_const_none) {
diff --git a/stmhal/uart.c b/stmhal/uart.c
index 6dc60ca08..04373a884 100644
--- a/stmhal/uart.c
+++ b/stmhal/uart.c
@@ -3,9 +3,9 @@
#include "stm32f4xx_hal.h"
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/tests/basics/builtin-minmax.py b/tests/basics/builtin-minmax.py
new file mode 100644
index 000000000..8ee4bbca7
--- /dev/null
+++ b/tests/basics/builtin-minmax.py
@@ -0,0 +1,15 @@
+# test builtin min and max functions
+
+print(min(0,1))
+print(min(1,0))
+print(min(0,-1))
+print(min(-1,0))
+
+print(max(0,1))
+print(max(1,0))
+print(max(0,-1))
+print(max(-1,0))
+
+print(min([1,2,4,0,-1,2]))
+print(max([1,2,4,0,-1,2]))
+
diff --git a/tests/basics/subclass-native3.py b/tests/basics/subclass-native3.py
index 5d443cf5c..bd99ab0d6 100644
--- a/tests/basics/subclass-native3.py
+++ b/tests/basics/subclass-native3.py
@@ -3,6 +3,20 @@ class MyExc(Exception):
e = MyExc(100, "Some error")
print(e)
-# TODO: Prints native base class name
-#print(repr(e))
+print(repr(e))
print(e.args)
+
+try:
+ raise MyExc("Some error")
+except MyExc as e:
+ print("Caught exception:", repr(e))
+
+try:
+ raise MyExc("Some error2")
+except Exception as e:
+ print("Caught exception:", repr(e))
+
+try:
+ raise MyExc("Some error2")
+except:
+ print("Caught user exception")
diff --git a/tests/float/builtin-float-minmax.py b/tests/float/builtin-float-minmax.py
new file mode 100644
index 000000000..ce45a768a
--- /dev/null
+++ b/tests/float/builtin-float-minmax.py
@@ -0,0 +1,26 @@
+# test builtin min and max functions with float args
+
+print(min(0,1.0))
+print(min(1.0,0))
+print(min(0,-1.0))
+print(min(-1.0,0))
+
+print(max(0,1.0))
+print(max(1.0,0))
+print(max(0,-1.0))
+print(max(-1.0,0))
+
+print(min(1.5,-1.5))
+print(min(-1.5,1.5))
+
+print(max(1.5,-1.5))
+print(max(-1.5,1.5))
+
+print(min([1,2.9,4,0,-1,2]))
+print(max([1,2.9,4,0,-1,2]))
+
+print(min([1,2.9,4,6.5,-1,2]))
+print(max([1,2.9,4,6.5,-1,2]))
+print(min([1,2.9,4,-6.5,-1,2]))
+print(max([1,2.9,4,-6.5,-1,2]))
+
diff --git a/tools/build-stm-latest.sh b/tools/build-stm-latest.sh
new file mode 100755
index 000000000..951d8be9c
--- /dev/null
+++ b/tools/build-stm-latest.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+# check/get parameters
+if [ $# != 1 ]; then
+ echo "usage: $0 <dest-dir>"
+ exit 1
+fi
+
+dest_dir=$1
+
+# check we are in the correct directory
+if [ ! -r modpyb.c ]; then
+ echo "must be in stm directory"
+ exit 1
+fi
+
+# get the date
+date=$(date '+%Y-%m-%d')
+
+# get the git hash
+git_hash="$(git rev-parse --short HEAD 2> /dev/null || echo unknown)"
+
+# build the versions
+for board in PYBV3 PYBV10; do
+ echo $board
+ lower_board=$(echo $board | tr A-Z a-z)
+ build_dir=/tmp/stm-build-$lower_board
+ make -B BOARD=$board BUILD=$build_dir || exit 1
+ mv $build_dir/flash.dfu $dest_dir/$lower_board-$date-$git_hash.dfu
+ rm -rf $build_dir
+done
diff --git a/tools/pyboard.py b/tools/pyboard.py
index 2484a1a08..38d428282 100644
--- a/tools/pyboard.py
+++ b/tools/pyboard.py
@@ -111,18 +111,19 @@ def run_test():
pyb.exec('def apply(l, f):\r\n for item in l:\r\n f(item)\r\n')
- pyb.exec('leds=[pyb.Led(l) for l in range(1, 5)]')
+ pyb.exec('leds=[pyb.LED(l) for l in range(1, 5)]')
pyb.exec('apply(leds, lambda l:l.off())')
## USR switch test
- if True:
- for i in range(2):
- print("press USR button")
- pyb.exec('while pyb.switch(): pyb.delay(10)')
- pyb.exec('while not pyb.switch(): pyb.delay(10)')
+ pyb.exec('switch = pyb.Switch()')
+
+ for i in range(2):
+ print("press USR button")
+ pyb.exec('while switch(): pyb.delay(10)')
+ pyb.exec('while not switch(): pyb.delay(10)')
- print('USR switch passed')
+ print('USR switch passed')
## accel test
diff --git a/unix-cpy/main.c b/unix-cpy/main.c
index 6cdd72c5d..3f42398f5 100644
--- a/unix-cpy/main.c
+++ b/unix-cpy/main.c
@@ -3,9 +3,9 @@
#include <stdio.h>
#include <string.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "lexer.h"
#include "lexerunix.h"
diff --git a/unix/file.c b/unix/file.c
index 041e28e95..04305cac7 100644
--- a/unix/file.c
+++ b/unix/file.c
@@ -5,9 +5,9 @@
#include <sys/stat.h>
#include <sys/types.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/unix/main.c b/unix/main.c
index 4c86edeae..e677ff65d 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -8,9 +8,9 @@
#include <sys/types.h>
#include <errno.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "lexer.h"
#include "lexerunix.h"
diff --git a/unix/modffi.c b/unix/modffi.c
index be542625e..39551c264 100644
--- a/unix/modffi.c
+++ b/unix/modffi.c
@@ -4,9 +4,9 @@
#include <dlfcn.h>
#include <ffi.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
diff --git a/unix/modsocket.c b/unix/modsocket.c
index e52f5d92a..898d5bfe9 100644
--- a/unix/modsocket.c
+++ b/unix/modsocket.c
@@ -11,9 +11,9 @@
#include <netdb.h>
#include <errno.h>
+#include "mpconfig.h"
#include "nlr.h"
#include "misc.h"
-#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "objtuple.h"
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 },