diff options
60 files changed, 549 insertions, 135 deletions
diff --git a/py/modcmath.c b/py/modcmath.c index 80dc0c8860..cfafdf84e8 100644 --- a/py/modcmath.c +++ b/py/modcmath.c @@ -65,7 +65,7 @@ mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) { mp_obj_get_complex(z_obj, &real, &imag); mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real*real + imag*imag, 0.25); mp_float_t theta = 0.5 * MICROPY_FLOAT_C_FUN(atan2)(imag, real); - return mp_obj_new_complex(sqrt_abs * cos(theta), sqrt_abs * sin(theta)); + return mp_obj_new_complex(sqrt_abs * MICROPY_FLOAT_C_FUN(cos)(theta), sqrt_abs * MICROPY_FLOAT_C_FUN(sin)(theta)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt); diff --git a/py/objtype.c b/py/objtype.c index 0eb09e5832..fd187147a0 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -22,10 +22,10 @@ /******************************************************************************/ // 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); +#define is_native_type(type) ((type)->make_new != instance_make_new) +STATIC mp_obj_t instance_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) { +STATIC mp_obj_t mp_obj_new_instance(mp_obj_t class, uint 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); @@ -33,7 +33,7 @@ STATIC mp_obj_t mp_obj_new_class(mp_obj_t class, uint subobjs) { return o; } -STATIC int class_count_native_bases(const mp_obj_type_t *type, const mp_obj_type_t **last_native_base) { +STATIC int instance_count_native_bases(const mp_obj_type_t *type, const mp_obj_type_t **last_native_base) { uint len; mp_obj_t *items; mp_obj_tuple_get(type->bases_tuple, &len, &items); @@ -45,7 +45,7 @@ STATIC int class_count_native_bases(const mp_obj_type_t *type, const mp_obj_type *last_native_base = items[i]; count++; } else { - count += class_count_native_bases(items[i], last_native_base); + count += instance_count_native_bases(items[i], last_native_base); } } @@ -63,7 +63,7 @@ STATIC int class_count_native_bases(const mp_obj_type_t *type, const mp_obj_type // applies to instance->subobj[0]. In most cases, we also don't need to know which 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. +// instance->subobj[0]. This case is handled via instance_count_native_bases() though. 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); @@ -130,7 +130,7 @@ STATIC void mp_obj_class_lookup(mp_obj_instance_t *o, const mp_obj_type_t *type, } } -STATIC void class_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { 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}; @@ -163,15 +163,15 @@ STATIC void class_print(void (*print)(void *env, const char *fmt, ...), void *en print(env, "<%s object at %p>", mp_obj_get_type_str(self_in), self_in); } -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 instance_make_new(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_type)); mp_obj_type_t *self = self_in; const mp_obj_type_t *native_base; - uint num_native_bases = class_count_native_bases(self, &native_base); + uint num_native_bases = instance_count_native_bases(self, &native_base); assert(num_native_bases < 2); - mp_obj_instance_t *o = mp_obj_new_class(self_in, num_native_bases); + mp_obj_instance_t *o = mp_obj_new_instance(self_in, num_native_bases); // look for __init__ function mp_obj_t init_fn[2] = {MP_OBJ_NULL}; @@ -219,7 +219,7 @@ STATIC const qstr unary_op_method_name[] = { [MP_UNARY_OP_NOT] = MP_QSTR_, // don't need to implement this, used to make sure array has full size }; -STATIC mp_obj_t class_unary_op(int op, mp_obj_t self_in) { +STATIC mp_obj_t instance_unary_op(int op, mp_obj_t self_in) { mp_obj_instance_t *self = self_in; qstr op_name = unary_op_method_name[op]; /* Still try to lookup native slot @@ -282,7 +282,7 @@ STATIC const qstr binary_op_method_name[] = { // and put the result in the dest[] array for a possible method call. // Conversion means dealing with static/class methods, callables, and values. // see http://docs.python.org/3.3/howto/descriptor.html -STATIC void class_convert_return_attr(mp_obj_t self, mp_obj_t member, mp_obj_t *dest) { +STATIC void instance_convert_return_attr(mp_obj_t self, mp_obj_t member, mp_obj_t *dest) { assert(dest[1] == NULL); if (MP_OBJ_IS_TYPE(member, &mp_type_staticmethod)) { // return just the function @@ -301,7 +301,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) { +STATIC mp_obj_t instance_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_instance_t *lhs = lhs_in; @@ -318,7 +318,7 @@ STATIC mp_obj_t class_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { } else if (member[0] != MP_OBJ_NULL) { mp_obj_t dest[3]; dest[1] = MP_OBJ_NULL; - class_convert_return_attr(lhs_in, member[0], dest); + instance_convert_return_attr(lhs_in, member[0], dest); dest[2] = rhs_in; return mp_call_method_n_kw(1, 0, dest); } else { @@ -326,7 +326,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) { +STATIC void instance_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_instance_t *self = self_in; @@ -346,13 +346,13 @@ STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } else if (MP_OBJ_IS_TYPE(member, &mp_type_property)) { // object member is a property // delegate the store to the property - // TODO should this be part of class_convert_return_attr? + // TODO should this be part of instance_convert_return_attr? const mp_obj_t *proxy = mp_obj_property_get(member); if (proxy[0] == mp_const_none) { // TODO } else { dest[0] = mp_call_function_n_kw(proxy[0], 1, 0, &self_in); - // TODO should we convert the returned value using class_convert_return_attr? + // TODO should we convert the returned value using instance_convert_return_attr? } #endif } else { @@ -360,7 +360,7 @@ STATIC void class_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { // if we don't yet have bound method (supposedly from native base), go // try to convert own attrs. if (dest[1] == MP_OBJ_NULL) { - class_convert_return_attr(self_in, member, dest); + instance_convert_return_attr(self_in, member, dest); } } return; @@ -380,7 +380,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) { +STATIC bool instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { mp_obj_instance_t *self = self_in; #if MICROPY_ENABLE_PROPERTY @@ -414,7 +414,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) { +STATIC mp_obj_t instance_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_instance_t *self = self_in; mp_obj_t member[2] = {MP_OBJ_NULL}; uint meth_args; @@ -435,7 +435,7 @@ STATIC mp_obj_t class_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { return mp_obj_subscr(self->subobj[0], index, value); } else if (member[0] != MP_OBJ_NULL) { mp_obj_t args[3] = {self_in, index, value}; - // TODO probably need to call class_convert_return_attr, and use mp_call_method_n_kw + // TODO probably need to call instance_convert_return_attr, and use mp_call_method_n_kw mp_obj_t ret = mp_call_function_n_kw(member[0], meth_args, 0, args); if (value == MP_OBJ_SENTINEL) { return ret; @@ -447,7 +447,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) { +STATIC mp_obj_t instance_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { 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); @@ -605,19 +605,19 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) mp_obj_type_t *o = m_new0(mp_obj_type_t, 1); o->base.type = &mp_type_type; o->name = name; - o->print = class_print; - o->make_new = class_make_new; - o->unary_op = class_unary_op; - o->binary_op = class_binary_op; - o->load_attr = class_load_attr; - o->store_attr = class_store_attr; - o->subscr = class_subscr; - o->call = class_call; + o->print = instance_print; + o->make_new = instance_make_new; + o->unary_op = instance_unary_op; + o->binary_op = instance_binary_op; + o->load_attr = instance_load_attr; + o->store_attr = instance_store_attr; + o->subscr = instance_subscr; + o->call = instance_call; o->bases_tuple = bases_tuple; o->locals_dict = locals_dict; const mp_obj_type_t *native_base; - uint num_native_bases = class_count_native_bases(o, &native_base); + uint num_native_bases = instance_count_native_bases(o, &native_base); if (num_native_bases > 1) { nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "multiple bases have instance lay-out conflict")); } @@ -674,7 +674,7 @@ STATIC void super_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { mp_obj_t member[2] = {MP_OBJ_NULL}; mp_obj_class_lookup(self->obj, (mp_obj_type_t*)items[i], attr, 0, member); if (member[0] != MP_OBJ_NULL) { - class_convert_return_attr(self->obj, member[0], dest); + instance_convert_return_attr(self->obj, member[0], dest); return; } } diff --git a/stmhal/.gitignore b/stmhal/.gitignore new file mode 100644 index 0000000000..61ad25738f --- /dev/null +++ b/stmhal/.gitignore @@ -0,0 +1,5 @@ +build-PYBV3/ +build-PYBV4/ +build-PYBV10/ +build-STM32F4DISC/ +build-NETDUINO_PLUS_2/ diff --git a/stmhal/Makefile b/stmhal/Makefile index 09c03d71e4..4cd33b9581 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -1,3 +1,13 @@ +# Select the board to build for: if not given on the command line, +# then default to PYBV10. +BOARD ?= PYBV10 +ifeq ($(wildcard boards/$(BOARD)/.),) +$(error Invalid BOARD specified) +endif + +# If the build directory is not given, make it reflect the board name. +BUILD ?= build-$(BOARD) + include ../py/mkenv.mk # qstr definitions (must come before including py.mk) @@ -32,10 +42,6 @@ INC += -I$(CC3K_DIR) CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 $(CFLAGS_CORTEX_M4) $(COPT) -BOARD ?= PYBV10 -ifeq ($(wildcard boards/$(BOARD)/.),) -$(error Invalid BOARD specified) -endif CFLAGS += -Iboards/$(BOARD) #Debugging/Optimization @@ -189,25 +195,21 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_FATFS:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_CC3K:.c=.o)) OBJ += $(BUILD)/pins_$(BOARD).o -all: $(BUILD)/flash.dfu +all: $(BUILD)/firmware.dfu -.PHONY: flashboard +.PHONY: deploy -flashboard: $(BUILD)/flash.dfu +deploy: $(BUILD)/firmware.dfu $(ECHO) "Writing $< to the board" $(Q)$(DFU_UTIL) -a 0 -D $< -$(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin +$(BUILD)/firmware.dfu: $(BUILD)/firmware.elf $(ECHO) "Create $@" - $(Q)$(PYTHON) $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@ - -$(BUILD)/flash0.bin: $(BUILD)/flash.elf - $(Q)$(OBJCOPY) -O binary -j .isr_vector $^ $@ - -$(BUILD)/flash1.bin: $(BUILD)/flash.elf - $(Q)$(OBJCOPY) -O binary -j .text -j .data $^ $@ + $(Q)$(OBJCOPY) -O binary -j .isr_vector $^ $(BUILD)/firmware0.bin + $(Q)$(OBJCOPY) -O binary -j .text -j .data $^ $(BUILD)/firmware1.bin + $(Q)$(PYTHON) $(DFU) -b 0x08000000:$(BUILD)/firmware0.bin -b 0x08020000:$(BUILD)/firmware1.bin $@ -$(BUILD)/flash.elf: $(OBJ) +$(BUILD)/firmware.elf: $(OBJ) $(ECHO) "LINK $@" $(Q)$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS) $(Q)$(SIZE) $@ diff --git a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h index b70b56d9ce..0e40545253 100644 --- a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h +++ b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h @@ -11,7 +11,6 @@ #define MICROPY_HW_HAS_MMA7660 (0) #define MICROPY_HW_HAS_LIS3DSH (0) #define MICROPY_HW_HAS_LCD (0) -#define MICROPY_HW_HAS_WLAN (0) #define MICROPY_HW_ENABLE_RNG (1) #define MICROPY_HW_ENABLE_RTC (0) #define MICROPY_HW_ENABLE_TIMER (1) @@ -20,6 +19,7 @@ #define MICROPY_HW_ENABLE_I2C1 (0) #define MICROPY_HW_ENABLE_SPI1 (0) #define MICROPY_HW_ENABLE_SPI3 (0) +#define MICROPY_HW_ENABLE_CC3K (0) // USRSW is pulled low. Pressing the button makes the input go high. #define MICROPY_HW_USRSW_PIN (pin_B11) diff --git a/stmhal/boards/PYBV10/mpconfigboard.h b/stmhal/boards/PYBV10/mpconfigboard.h index fc22f63341..3def531232 100644 --- a/stmhal/boards/PYBV10/mpconfigboard.h +++ b/stmhal/boards/PYBV10/mpconfigboard.h @@ -7,7 +7,6 @@ #define MICROPY_HW_HAS_MMA7660 (1) #define MICROPY_HW_HAS_LIS3DSH (0) #define MICROPY_HW_HAS_LCD (1) -#define MICROPY_HW_HAS_WLAN (1) #define MICROPY_HW_ENABLE_RNG (1) #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_TIMER (1) @@ -16,6 +15,7 @@ #define MICROPY_HW_ENABLE_I2C1 (1) #define MICROPY_HW_ENABLE_SPI1 (1) #define MICROPY_HW_ENABLE_SPI3 (0) +#define MICROPY_HW_ENABLE_CC3K (0) // USRSW has no pullup or pulldown, and pressing the switch makes the input go low #define MICROPY_HW_USRSW_PIN (pin_B3) diff --git a/stmhal/boards/PYBV3/mpconfigboard.h b/stmhal/boards/PYBV3/mpconfigboard.h index af4da0c1d6..ac0d84ca29 100644 --- a/stmhal/boards/PYBV3/mpconfigboard.h +++ b/stmhal/boards/PYBV3/mpconfigboard.h @@ -7,7 +7,6 @@ #define MICROPY_HW_HAS_MMA7660 (1) #define MICROPY_HW_HAS_LIS3DSH (0) #define MICROPY_HW_HAS_LCD (0) -#define MICROPY_HW_HAS_WLAN (0) #define MICROPY_HW_ENABLE_RNG (1) #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_TIMER (1) @@ -16,6 +15,7 @@ #define MICROPY_HW_ENABLE_I2C1 (1) #define MICROPY_HW_ENABLE_SPI1 (1) #define MICROPY_HW_ENABLE_SPI3 (0) +#define MICROPY_HW_ENABLE_CC3K (0) // USRSW has no pullup or pulldown, and pressing the switch makes the input go low #define MICROPY_HW_USRSW_PIN (pin_A13) diff --git a/stmhal/boards/PYBV4/mpconfigboard.h b/stmhal/boards/PYBV4/mpconfigboard.h index 550f1633f9..9fedb70136 100644 --- a/stmhal/boards/PYBV4/mpconfigboard.h +++ b/stmhal/boards/PYBV4/mpconfigboard.h @@ -7,7 +7,6 @@ #define MICROPY_HW_HAS_MMA7660 (1) #define MICROPY_HW_HAS_LIS3DSH (0) #define MICROPY_HW_HAS_LCD (1) -#define MICROPY_HW_HAS_WLAN (0) #define MICROPY_HW_ENABLE_RNG (1) #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_TIMER (1) @@ -16,6 +15,7 @@ #define MICROPY_HW_ENABLE_I2C1 (1) #define MICROPY_HW_ENABLE_SPI1 (1) #define MICROPY_HW_ENABLE_SPI3 (0) +#define MICROPY_HW_ENABLE_CC3K (0) // USRSW has no pullup or pulldown, and pressing the switch makes the input go low #define MICROPY_HW_USRSW_PIN (pin_B3) diff --git a/stmhal/boards/STM32F4DISC/mpconfigboard.h b/stmhal/boards/STM32F4DISC/mpconfigboard.h index c83bb162bb..e6780eacbd 100644 --- a/stmhal/boards/STM32F4DISC/mpconfigboard.h +++ b/stmhal/boards/STM32F4DISC/mpconfigboard.h @@ -7,7 +7,6 @@ #define MICROPY_HW_HAS_MMA7660 (0) #define MICROPY_HW_HAS_LIS3DSH (1) #define MICROPY_HW_HAS_LCD (0) -#define MICROPY_HW_HAS_WLAN (0) #define MICROPY_HW_ENABLE_RNG (1) #define MICROPY_HW_ENABLE_RTC (1) #define MICROPY_HW_ENABLE_TIMER (1) @@ -16,6 +15,7 @@ #define MICROPY_HW_ENABLE_I2C1 (1) #define MICROPY_HW_ENABLE_SPI1 (1) #define MICROPY_HW_ENABLE_SPI3 (0) +#define MICROPY_HW_ENABLE_CC3K (0) // USRSW is pulled low. Pressing the button makes the input go high. #define MICROPY_HW_USRSW_PIN (pin_A0) diff --git a/stmhal/cc3k/cc3000_common.c b/stmhal/cc3k/cc3000_common.c index 48eda26760..8d9bd7d03a 100644 --- a/stmhal/cc3k/cc3000_common.c +++ b/stmhal/cc3k/cc3000_common.c @@ -52,6 +52,9 @@ * *****************************************************************************/ #include <stdint.h> +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "cc3000_common.h" #include "socket.h" @@ -194,3 +197,5 @@ uint32_t STREAM_TO_UINT32_f(char * cp, uint16_t offset) //! @} // //***************************************************************************** + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/ccspi.c b/stmhal/cc3k/ccspi.c index 4785897e13..133b17d030 100644 --- a/stmhal/cc3k/ccspi.c +++ b/stmhal/cc3k/ccspi.c @@ -42,6 +42,9 @@ *****************************************************************************/ #include <stdint.h> #include <string.h> // for memset +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "ccspi.h" #include "hci.h" @@ -735,3 +738,5 @@ void cc3k_int_poll() SpiIntGPIOHandler(); } } + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/evnt_handler.c b/stmhal/cc3k/evnt_handler.c index 5a75fadcc0..cca2e796d7 100644 --- a/stmhal/cc3k/evnt_handler.c +++ b/stmhal/cc3k/evnt_handler.c @@ -52,6 +52,9 @@ //****************************************************************************** #include <stdint.h> +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "cc3000_common.h" #include "string.h" @@ -871,3 +874,5 @@ SimpleLinkWaitData(unsigned char *pBuf, unsigned char *from, //! @} // //***************************************************************************** + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/hci.c b/stmhal/cc3k/hci.c index ef77db95f9..533311f175 100644 --- a/stmhal/cc3k/hci.c +++ b/stmhal/cc3k/hci.c @@ -50,6 +50,9 @@ #include <stdint.h> #include <string.h> // for memcpy +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "cc3000_common.h" #include "hci.h" @@ -240,3 +243,5 @@ hci_patch_send(unsigned char ucOpcode, unsigned char *pucBuff, char *patch, unsi // // //***************************************************************************** + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/netapp.c b/stmhal/cc3k/netapp.c index 4b3efb24df..cdeccefc46 100644 --- a/stmhal/cc3k/netapp.c +++ b/stmhal/cc3k/netapp.c @@ -41,6 +41,9 @@ * *****************************************************************************/ #include <stdint.h> +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "netapp.h" #include "hci.h" @@ -475,3 +478,5 @@ long netapp_set_debug_level(unsigned long ulLevel) } #endif + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/nvmem.c b/stmhal/cc3k/nvmem.c index 4113620086..774759c41f 100644 --- a/stmhal/cc3k/nvmem.c +++ b/stmhal/cc3k/nvmem.c @@ -50,6 +50,9 @@ #include <stdint.h> #include <string.h> +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "nvmem.h" #include "hci.h" @@ -364,3 +367,4 @@ nvmem_create_entry(unsigned long ulFileId, unsigned long ulNewLen) // //***************************************************************************** +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/pybcc3k.c b/stmhal/cc3k/pybcc3k.c index 8712a3780b..d43f42f0ba 100644 --- a/stmhal/cc3k/pybcc3k.c +++ b/stmhal/cc3k/pybcc3k.c @@ -17,6 +17,8 @@ #include "ccdebug.h" #include "pybcc3k.h" +#if MICROPY_HW_ENABLE_CC3K + // IRQ on PA14, input, pulled up, active low // EN on PC7, output, active high // CS on PC6, output, active low @@ -165,3 +167,5 @@ uint8_t pyb_cc3000_spi_send(uint8_t val) { HAL_SPI_TransmitReceive(&SPI_HANDLE, data, data, 1, 1000); return data[0]; } + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/security.c b/stmhal/cc3k/security.c index c12aee370d..c52d7f67e6 100644 --- a/stmhal/cc3k/security.c +++ b/stmhal/cc3k/security.c @@ -41,6 +41,9 @@ //***************************************************************************** #include <stdint.h> +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "security.h" @@ -533,3 +536,5 @@ signed long aes_write_key(unsigned char *key) //! @} // //***************************************************************************** + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/socket.c b/stmhal/cc3k/socket.c index cfccbd7831..86a4549ecf 100644 --- a/stmhal/cc3k/socket.c +++ b/stmhal/cc3k/socket.c @@ -50,6 +50,9 @@ #include <stdint.h> #include <string.h> // for memcpy +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "hci.h" #include "socket.h" @@ -1188,3 +1191,5 @@ mdnsAdvertiser(unsigned short mdnsEnabled, char * deviceServiceName, unsigned sh return ret; } + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/cc3k/wlan.c b/stmhal/cc3k/wlan.c index be6b3242ce..b22796a7c8 100644 --- a/stmhal/cc3k/wlan.c +++ b/stmhal/cc3k/wlan.c @@ -49,6 +49,9 @@ //***************************************************************************** #include <stdlib.h> #include <stdint.h> +#include "mpconfigport.h" + +#if MICROPY_HW_ENABLE_CC3K #include "wlan.h" #include "hci.h" @@ -1262,3 +1265,5 @@ wlan_smart_config_process() //! @} // //***************************************************************************** + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/i2c.c b/stmhal/i2c.c index b8de855304..96222bf336 100644 --- a/stmhal/i2c.c +++ b/stmhal/i2c.c @@ -367,7 +367,7 @@ STATIC mp_obj_t pyb_i2c_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_arg } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_send_obj, 1, pyb_i2c_send); -/// \method recv(send, addr=0x00, timeout=5000) +/// \method recv(recv, addr=0x00, timeout=5000) /// /// Receive data on the bus: /// diff --git a/stmhal/main.c b/stmhal/main.c index 603bf31889..5faa70e657 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -495,13 +495,11 @@ soft_reset: vstr_free(vstr); } -#if 0 -#if MICROPY_HW_HAS_WLAN - // wifi +#if MICROPY_HW_ENABLE_CC3K + // wifi using the CC3000 driver pyb_wlan_init(); pyb_wlan_start(); #endif -#endif // enter REPL // REPL mode can change, or it can request a soft reset diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index ad55ed6324..faa1c806a4 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -19,6 +19,7 @@ #define MICROPY_ENABLE_LFN (1) #define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ #define MICROPY_MOD_SYS_STDFILES (1) +#define MICROPY_ENABLE_MOD_CMATH (1) // extra built in names to add to the global namespace extern const struct _mp_obj_fun_native_t mp_builtin_help_obj; diff --git a/stmhal/pin.c b/stmhal/pin.c index 5b0f998de6..d390926360 100644 --- a/stmhal/pin.c +++ b/stmhal/pin.c @@ -241,6 +241,7 @@ STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_debug_obj, (mp_obj_t)&pin_debug_fun_o /// - `Pin.PULL_DOWN` - enable the pull-down resistor. /// /// Returns: `None`. +// TODO allow keyword args STATIC mp_obj_t pin_obj_init(uint n_args, mp_obj_t *args) { pin_obj_t *self = args[0]; diff --git a/stmhal/pybwlan.c b/stmhal/pybwlan.c index 06309b61c0..fb214209c2 100644 --- a/stmhal/pybwlan.c +++ b/stmhal/pybwlan.c @@ -12,6 +12,8 @@ #include "obj.h" #include "runtime.h" +#if MICROPY_HW_ENABLE_CC3K + #include "cc3k/ccspi.h" #include "cc3k/hci.h" #include "cc3k/socket.h" @@ -377,3 +379,5 @@ void pyb_wlan_start(void) { printf("nvmem_read_sp_version=%d; %02x %02x\n", ret, ver[0], ver[1]); */ } + +#endif // MICROPY_HW_ENABLE_CC3K diff --git a/stmhal/rtc.c b/stmhal/rtc.c index ec3c0126b4..ea7aa789ec 100644 --- a/stmhal/rtc.c +++ b/stmhal/rtc.c @@ -260,7 +260,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(pyb_rtc_info_obj, pyb_rtc_info); /// /// `weekday` is 1-7 for Monday through Sunday. /// -/// `subseconds` is 0-59. +/// `subseconds` counts down from 255 to 0 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 68e211bcb3..6f87197bd2 100644 --- a/stmhal/servo.c +++ b/stmhal/servo.c @@ -203,6 +203,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_servo_pulse_width_obj, 1, 2, pyb_ /// \method calibration([pulse_min, pulse_max, pulse_centre, [pulse_angle_90, pulse_speed_100]]) /// Get or set the calibration of the servo timing. +// TODO should accept 1 arg, a 5-tuple of values to set STATIC mp_obj_t pyb_servo_calibration(uint n_args, const mp_obj_t *args) { pyb_servo_obj_t *self = args[0]; if (n_args == 1) { diff --git a/stmhal/spi.c b/stmhal/spi.c index c1b8e75d05..a07ebd21a9 100644 --- a/stmhal/spi.c +++ b/stmhal/spi.c @@ -198,7 +198,7 @@ STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void * } } -/// \method init(mode, baudrate=328125, *, polarity=1, phase=1, bits=8, firstbit=SPI.MSB, ti=false, crc=None) +/// \method init(mode, baudrate=328125, *, polarity=1, phase=1, bits=8, firstbit=SPI.MSB, ti=False, crc=None) /// /// Initialise the SPI bus with the given parameters: /// diff --git a/tests/pyb/accel.py b/tests/pyb/accel.py new file mode 100644 index 0000000000..13f53b33c0 --- /dev/null +++ b/tests/pyb/accel.py @@ -0,0 +1,7 @@ +accel = pyb.Accel() +print(accel) +accel.x() +accel.y() +accel.z() +accel.tilt() +accel.filtered_xyz() diff --git a/tests/pyb/accel.py.exp b/tests/pyb/accel.py.exp new file mode 100644 index 0000000000..28070be177 --- /dev/null +++ b/tests/pyb/accel.py.exp @@ -0,0 +1 @@ +<Accel> diff --git a/tests/pyb/adc.py b/tests/pyb/adc.py new file mode 100644 index 0000000000..7bed54e9f8 --- /dev/null +++ b/tests/pyb/adc.py @@ -0,0 +1,10 @@ +from pyb import ADC +from pyb import Pin + +adc = ADC('X22') +print(adc) + +adc.read() + +buf = bytearray(100) +adc.read_timed(buf, 500) diff --git a/tests/pyb/adc.py.exp b/tests/pyb/adc.py.exp new file mode 100644 index 0000000000..bbc6af7379 --- /dev/null +++ b/tests/pyb/adc.py.exp @@ -0,0 +1 @@ +<ADC on X22 channel=13> diff --git a/tests/pyb/dac.py b/tests/pyb/dac.py new file mode 100644 index 0000000000..61ab7bd6e6 --- /dev/null +++ b/tests/pyb/dac.py @@ -0,0 +1,8 @@ +dac = pyb.DAC(1) +print(dac) +dac.noise(100) +dac.triangle(100) +dac.write(0) +dac.write_timed(bytearray(10), 100, mode=pyb.DAC.NORMAL) +pyb.delay(20) +dac.write(0) diff --git a/tests/pyb/dac.py.exp b/tests/pyb/dac.py.exp new file mode 100644 index 0000000000..ae245f2e61 --- /dev/null +++ b/tests/pyb/dac.py.exp @@ -0,0 +1 @@ +<DAC> diff --git a/tests/pyb/extint.py b/tests/pyb/extint.py new file mode 100644 index 0000000000..20648995bc --- /dev/null +++ b/tests/pyb/extint.py @@ -0,0 +1,6 @@ +ext = pyb.ExtInt('X1', pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_DOWN, lambda l:print('line:', l)) +ext.disable() +ext.enable() +print(ext.line()) +ext.swint() +ext.disable() diff --git a/tests/pyb/extint.py.exp b/tests/pyb/extint.py.exp new file mode 100644 index 0000000000..28019d75c6 --- /dev/null +++ b/tests/pyb/extint.py.exp @@ -0,0 +1,2 @@ +0 +line: 0 diff --git a/tests/pyb/i2c.py b/tests/pyb/i2c.py new file mode 100644 index 0000000000..79169d0553 --- /dev/null +++ b/tests/pyb/i2c.py @@ -0,0 +1,23 @@ +from pyb import I2C + +i2c = I2C(1) +i2c2 = I2C(2) + +i2c.init(I2C.MASTER, baudrate=400000) +print(i2c.scan()) +i2c.deinit() + +# use accelerometer to test i2c bus + +accel_addr = 76 + +pyb.Accel() # this will init the bus for us + +print(i2c.scan()) +print(i2c.is_ready(accel_addr)) + +print(i2c.mem_read(1, accel_addr, 7, timeout=500)) +i2c.mem_write(0, accel_addr, 0, timeout=500) + +i2c.send(7, addr=accel_addr) +i2c.recv(1, addr=accel_addr) diff --git a/tests/pyb/i2c.py.exp b/tests/pyb/i2c.py.exp new file mode 100644 index 0000000000..c2b982f0fe --- /dev/null +++ b/tests/pyb/i2c.py.exp @@ -0,0 +1,4 @@ +[] +[76] +True +b'\x01' diff --git a/tests/pyb/led.py b/tests/pyb/led.py new file mode 100644 index 0000000000..a727c90dfa --- /dev/null +++ b/tests/pyb/led.py @@ -0,0 +1,28 @@ +from pyb import LED + +for i in range(4): + print(LED(i+1)) + +for i in range(4): + LED(i+1).on() +pyb.delay(10) +for i in range(4): + LED(i+1).off() +pyb.delay(10) +for i in range(4): + LED(i+1).toggle() +pyb.delay(10) +for i in range(4): + LED(i+1).intensity(0) + +for i in range(256): + LED(4).intensity(i) + if LED(4).intensity() != i: + print('fail', i) + pyb.delay(1) +for i in range(256): + LED(4).intensity(255 - i) + pyb.delay(1) + +for i in range(4): + LED(i+1).off() diff --git a/tests/pyb/led.py.exp b/tests/pyb/led.py.exp new file mode 100644 index 0000000000..b528706694 --- /dev/null +++ b/tests/pyb/led.py.exp @@ -0,0 +1,4 @@ +<LED 1> +<LED 2> +<LED 3> +<LED 4> diff --git a/tests/pyb/pin.py b/tests/pyb/pin.py new file mode 100644 index 0000000000..448ce53998 --- /dev/null +++ b/tests/pyb/pin.py @@ -0,0 +1,29 @@ +from pyb import Pin + +p = Pin('X1') +print(p) +print(p.name()) +print(p.pin()) +print(p.port()) + +p = Pin('X1', Pin.IN, Pin.PULL_UP) +#p = Pin('X1', Pin.IN, pull=Pin.PULL_UP) +print(p.value()) + +p.init(p.IN, p.PULL_DOWN) +#p.init(p.IN, pull=p.PULL_DOWN) +print(p.value()) + +p.init(p.OUT_PP) +p.low() +print(p.value()) +p.high() +print(p.value()) +p.value(0) +print(p.value()) +p.value(1) +print(p.value()) +p.value(False) +print(p.value()) +p.value(True) +print(p.value()) diff --git a/tests/pyb/pin.py.exp b/tests/pyb/pin.py.exp new file mode 100644 index 0000000000..4856f5b8e7 --- /dev/null +++ b/tests/pyb/pin.py.exp @@ -0,0 +1,12 @@ +<Pin A0> +A0 +0 +0 +1 +0 +0 +1 +0 +1 +0 +1 diff --git a/tests/pyb/pyb1.py b/tests/pyb/pyb1.py new file mode 100644 index 0000000000..0087ec0507 --- /dev/null +++ b/tests/pyb/pyb1.py @@ -0,0 +1,42 @@ +# basic tests of pyb module + +import pyb + +# test delay + +pyb.delay(-1) +pyb.delay(0) +pyb.delay(1) + +start = pyb.millis() +pyb.delay(17) +print((pyb.millis() - start) // 5) # should print 3 + +# test udelay + +pyb.udelay(-1) +pyb.udelay(0) +pyb.udelay(1) + +start = pyb.millis() +pyb.udelay(17000) +print((pyb.millis() - start) // 5) # should print 3 + +# other + +pyb.disable_irq() +pyb.enable_irq() + +print(pyb.freq()) + +print(pyb.have_cdc()) + +pyb.hid((0, 0, 0, 0)) # won't do anything + +pyb.rng() + +pyb.sync() + +print(len(pyb.unique_id())) + +pyb.wfi() diff --git a/tests/pyb/pyb1.py.exp b/tests/pyb/pyb1.py.exp new file mode 100644 index 0000000000..84034d683c --- /dev/null +++ b/tests/pyb/pyb1.py.exp @@ -0,0 +1,5 @@ +3 +3 +(168000000, 168000000, 42000000, 84000000) +True +12 diff --git a/tests/pyb/rtc.py b/tests/pyb/rtc.py new file mode 100644 index 0000000000..ac716b27fc --- /dev/null +++ b/tests/pyb/rtc.py @@ -0,0 +1,6 @@ +from pyb import RTC +rtc = RTC() +print(rtc) +rtc.datetime((2014, 1, 1, 1, 0, 0, 0, 0)) +pyb.delay(1000) +print(rtc.datetime()[:7]) diff --git a/tests/pyb/rtc.py.exp b/tests/pyb/rtc.py.exp new file mode 100644 index 0000000000..d1ea2d9590 --- /dev/null +++ b/tests/pyb/rtc.py.exp @@ -0,0 +1,2 @@ +<RTC> +(2014, 1, 1, 1, 0, 0, 1) diff --git a/tests/pyb/servo.py b/tests/pyb/servo.py new file mode 100644 index 0000000000..d15cafe483 --- /dev/null +++ b/tests/pyb/servo.py @@ -0,0 +1,16 @@ +from pyb import Servo + +servo = Servo(1) +print(servo) + +servo.angle(0) +servo.angle(10, 100) + +servo.speed(-10) +servo.speed(10, 100) + +servo.pulse_width(1500) +print(servo.pulse_width()) + +servo.calibration(630, 2410, 1490, 2460, 2190) +print(servo.calibration()) diff --git a/tests/pyb/servo.py.exp b/tests/pyb/servo.py.exp new file mode 100644 index 0000000000..ac6032ba5f --- /dev/null +++ b/tests/pyb/servo.py.exp @@ -0,0 +1,3 @@ +<Servo 1 at 1500us> +1500 +(630, 2410, 1490, 2460, 2190) diff --git a/tests/pyb/spi.py b/tests/pyb/spi.py new file mode 100644 index 0000000000..90dfb441a3 --- /dev/null +++ b/tests/pyb/spi.py @@ -0,0 +1,17 @@ +from pyb import SPI + +spi = SPI(1) +print(spi) + +spi = SPI(1, SPI.MASTER) +spi = SPI(1, SPI.MASTER, baudrate=500000) +spi = SPI(1, SPI.MASTER, 500000, polarity=1, phase=1, bits=8, firstbit=SPI.MSB, ti=False, crc=None) +print(spi) + +spi.init(SPI.SLAVE) +print(spi) + +spi.init(SPI.MASTER) +spi.send(1, timeout=100) +print(spi.recv(1, timeout=100)) +print(spi.send_recv(1, timeout=100)) diff --git a/tests/pyb/spi.py.exp b/tests/pyb/spi.py.exp new file mode 100644 index 0000000000..030dfe1b34 --- /dev/null +++ b/tests/pyb/spi.py.exp @@ -0,0 +1,5 @@ +SPI(1) +SPI(1, SPI.MASTER, baudrate=328125, polarity=1, phase=1, bits=8) +SPI(1, SPI.SLAVE, polarity=1, phase=1, bits=8) +b'\xff' +b'\xff' diff --git a/tests/pyb/switch.py b/tests/pyb/switch.py new file mode 100644 index 0000000000..4b74e0fd73 --- /dev/null +++ b/tests/pyb/switch.py @@ -0,0 +1,6 @@ +from pyb import Switch + +sw = pyb.Switch() +print(sw()) +sw.callback(print) +sw.callback(None) diff --git a/tests/pyb/switch.py.exp b/tests/pyb/switch.py.exp new file mode 100644 index 0000000000..bc59c12aa1 --- /dev/null +++ b/tests/pyb/switch.py.exp @@ -0,0 +1 @@ +False diff --git a/tests/pyb/timer.py b/tests/pyb/timer.py new file mode 100644 index 0000000000..ff1bda0f23 --- /dev/null +++ b/tests/pyb/timer.py @@ -0,0 +1,28 @@ +from pyb import Timer + +tim = Timer(4) +tim = Timer(4, prescaler=100, period=200) +print(tim.prescaler()) +print(tim.period()) +tim.prescaler(300) +print(tim.prescaler()) +tim.period(400) +print(tim.period()) + +tim = Timer(4, freq=1) +tim.init(freq=2000) +def f(t): + print(1) + t.callback(None) +tim.callback(f) +pyb.delay(10) + +# f3 closes over f2.y +def f2(x): + y = x + def f3(t): + print(2, y) + t.callback(None) + return f3 +tim.callback(f2(3)) +pyb.delay(10) diff --git a/tests/pyb/timer.py.exp b/tests/pyb/timer.py.exp new file mode 100644 index 0000000000..58b81e2af1 --- /dev/null +++ b/tests/pyb/timer.py.exp @@ -0,0 +1,6 @@ +100 +200 +300 +400 +1 +2 3 diff --git a/tests/pyb/uart.py b/tests/pyb/uart.py new file mode 100644 index 0000000000..6e0118d155 --- /dev/null +++ b/tests/pyb/uart.py @@ -0,0 +1,12 @@ +from pyb import UART + +uart = UART(1) +uart = UART(1, 9600) +uart = UART(1, 9600, bits=8, stop=1, parity=None) +print(uart) + +uart.init(1200) +print(uart) + +uart.any() +uart.send(1, timeout=500) diff --git a/tests/pyb/uart.py.exp b/tests/pyb/uart.py.exp new file mode 100644 index 0000000000..58ded4d848 --- /dev/null +++ b/tests/pyb/uart.py.exp @@ -0,0 +1,2 @@ +UART(1, baudrate=9600, bits=8, stop=1, parity=None) +UART(1, baudrate=1200, bits=8, stop=1, parity=None) diff --git a/tests/pyboard.py b/tests/pyboard.py new file mode 120000 index 0000000000..3a82f6a6a3 --- /dev/null +++ b/tests/pyboard.py @@ -0,0 +1 @@ +../tools/pyboard.py
\ No newline at end of file diff --git a/tests/run-tests b/tests/run-tests index 9e94026fa2..c1eee59eaa 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -3,6 +3,7 @@ import os import subprocess import sys +import argparse from glob import glob # Tests require at least CPython 3.3. If your default python3 executable @@ -15,82 +16,113 @@ else: CPYTHON3 = os.getenv('MICROPY_CPYTHON3', 'python3') MICROPYTHON = os.getenv('MICROPY_MICROPYTHON', '../unix/micropython') -# Set of tests that we shouldn't run under Travis CI -skip_travis_tests = set(['basics/memoryerror.py']) - def rm_f(fname): if os.path.exists(fname): os.remove(fname) -test_count = 0 -testcase_count = 0 -passed_count = 0 -failed_tests = [] -tests = [] - -if not sys.argv[1:]: - test_dirs = ('basics', 'float', 'import', 'io', 'misc') - tests = sorted(test_file for test_files in (glob('{}/*.py'.format(dir)) for dir in test_dirs) for test_file in test_files) -else: - tests = sys.argv[1:] - -test_on_pyboard = False -if test_on_pyboard: - import pyboard - pyb = pyboard.Pyboard('/dev/ttyACM0') - pyb.enter_raw_repl() - -running_under_travis = os.getenv('TRAVIS') == 'true' - -for test_file in tests: - if running_under_travis and test_file in skip_travis_tests: - print("skip ", test_file) - continue - - # run CPython - try: - output_expected = subprocess.check_output([CPYTHON3, '-B', test_file]) - except subprocess.CalledProcessError: - output_expected = b'CPYTHON3 CRASH' - - # run Micro Python - if test_on_pyboard: +def run_tests(pyb, tests): + test_count = 0 + testcase_count = 0 + passed_count = 0 + failed_tests = [] + + running_under_travis = os.getenv('TRAVIS') == 'true' + + # Set of tests that we shouldn't run under Travis CI + skip_travis_tests = set(['basics/memoryerror.py']) + + for test_file in tests: + if running_under_travis and test_file in skip_travis_tests: + print("skip ", test_file) + continue + + # get expected output + test_file_expected = test_file + '.exp' + if os.path.isfile(test_file_expected): + # expected output given by a file, so read that in + with open(test_file_expected, 'rb') as f: + output_expected = f.read() + else: + # run CPython to work out expeceted output + try: + output_expected = subprocess.check_output([CPYTHON3, '-B', test_file]) + except subprocess.CalledProcessError: + output_expected = b'CPYTHON3 CRASH' + + # run Micro Python + if pyb is None: + # run on PC + try: + output_mupy = subprocess.check_output([MICROPYTHON, '-X', 'emit=bytecode', test_file]) + except subprocess.CalledProcessError: + output_mupy = b'CRASH' + else: + # run on pyboard + pyb.enter_raw_repl() + try: + output_mupy = pyb.execfile(test_file).replace(b'\r\n', b'\n') + except pyboard.PyboardError: + output_mupy = b'CRASH' + + testcase_count += len(output_expected.splitlines()) + + test_basename = os.path.basename(test_file) + test_name = os.path.splitext(test_basename)[0] + filename_expected = test_basename + ".exp" + filename_mupy = test_basename + ".out" + + if output_expected == output_mupy: + print("pass ", test_file) + passed_count += 1 + rm_f(filename_expected) + rm_f(filename_mupy) + else: + with open(filename_expected, "w") as f: + f.write(str(output_expected, "ascii")) + with open(filename_mupy, "w") as f: + f.write(str(output_mupy, "ascii")) + print("FAIL ", test_file) + failed_tests.append(test_name) + + test_count += 1 + + print("{} tests performed ({} individual testcases)".format(test_count, testcase_count)) + print("{} tests passed".format(passed_count)) + + if len(failed_tests) > 0: + print("{} tests failed: {}".format(len(failed_tests), ' '.join(failed_tests))) + return False + + # all tests succeeded + return True + +def main(): + cmd_parser = argparse.ArgumentParser(description='Run tests for Micro Python.') + cmd_parser.add_argument('--pyboard', action='store_true', help='run the tests on the pyboard') + cmd_parser.add_argument('files', nargs='*', help='input test files') + args = cmd_parser.parse_args() + + if args.pyboard: + import pyboard + pyb = pyboard.Pyboard('/dev/ttyACM0') pyb.enter_raw_repl() - try: - output_mupy = pyb.execfile(test_file).replace(b'\r\n', b'\n') - except pyboard.PyboardError: - output_mupy = b'CRASH' else: - try: - output_mupy = subprocess.check_output([MICROPYTHON, '-X', 'emit=bytecode', test_file]) - except subprocess.CalledProcessError: - output_mupy = b'CRASH' - - testcase_count += len(output_expected.splitlines()) - - test_basename = os.path.basename(test_file) - test_name = os.path.splitext(test_basename)[0] - filename_expected = test_basename + ".exp" - filename_mupy = test_basename + ".out" - - if output_expected == output_mupy: - print("pass ", test_file) - passed_count += 1 - rm_f(filename_expected) - rm_f(filename_mupy) + pyb = None + + if len(args.files) == 0: + if pyb is None: + # run PC tests + test_dirs = ('basics', 'float', 'import', 'io', 'misc') + else: + # run pyboard tests + test_dirs = ('basics', 'float', 'pyb') + tests = sorted(test_file for test_files in (glob('{}/*.py'.format(dir)) for dir in test_dirs) for test_file in test_files) else: - with open(filename_expected, "w") as f: - f.write(str(output_expected, "ascii")) - with open(filename_mupy, "w") as f: - f.write(str(output_mupy, "ascii")) - print("FAIL ", test_file) - failed_tests.append(test_name) - - test_count += 1 + # tests explicitly given + tests = args.files -print("{} tests performed ({} individual testcases)".format(test_count, testcase_count)) -print("{} tests passed".format(passed_count)) + if not run_tests(pyb, tests): + sys.exit(1) -if len(failed_tests) > 0: - print("{} tests failed: {}".format(len(failed_tests), ' '.join(failed_tests))) - sys.exit(1) +if __name__ == "__main__": + main() diff --git a/tools/build-stm-latest.sh b/tools/build-stm-latest.sh index 951d8be9c2..f0d639d10f 100755 --- a/tools/build-stm-latest.sh +++ b/tools/build-stm-latest.sh @@ -24,8 +24,8 @@ git_hash="$(git rev-parse --short HEAD 2> /dev/null || echo unknown)" for board in PYBV3 PYBV10; do echo $board lower_board=$(echo $board | tr A-Z a-z) - build_dir=/tmp/stm-build-$lower_board + build_dir=/tmp/stm-build-$board make -B BOARD=$board BUILD=$build_dir || exit 1 - mv $build_dir/flash.dfu $dest_dir/$lower_board-$date-$git_hash.dfu + mv $build_dir/firmware.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 38d428282b..dce60350d0 100644 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -9,7 +9,7 @@ Example usage: import pyboard pyb = pyboard.Pyboard('/dev/ttyACM0') pyb.enter_raw_repl() - pyb.exec('pyb.Led(1).on()') + pyb.exec('pyb.LED(1).on()') pyb.exit_raw_repl() To run a script from the local machine on the board and print out the results: @@ -17,6 +17,10 @@ To run a script from the local machine on the board and print out the results: import pyboard pyboard.execfile('test.py', device='/dev/ttyACM0') +This script can also be run directly. To execute a local script, use: + + python pyboard.py test.py + """ import time @@ -157,5 +161,19 @@ def run_test(): pyb.exit_raw_repl() pyb.close() +def main(): + import argparse + cmd_parser = argparse.ArgumentParser(description='Run scripts on the pyboard.') + cmd_parser.add_argument('--device', default='/dev/ttyACM0', help='the serial device of the pyboard') + cmd_parser.add_argument('--test', action='store_true', help='run a small test suite on the pyboard') + cmd_parser.add_argument('files', nargs='*', help='input files') + args = cmd_parser.parse_args() + + if args.test: + run_test() + + for file in args.files: + execfile(file, device=args.device) + if __name__ == "__main__": - run_test() + main() diff --git a/unix/file.c b/unix/file.c index 04305cac7a..4588d96574 100644 --- a/unix/file.c +++ b/unix/file.c @@ -18,6 +18,16 @@ typedef struct _mp_obj_fdfile_t { int fd; } mp_obj_fdfile_t; +#ifdef MICROPY_CPYTHON_COMPAT +void check_fd_is_open(const mp_obj_fdfile_t *o) { + if (o->fd < 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "I/O operation on closed file")); + } +} +#else +#define check_fd_is_open(o) +#endif + STATIC const mp_obj_type_t rawfile_type; STATIC void fdfile_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { @@ -27,6 +37,7 @@ STATIC void fdfile_print(void (*print)(void *env, const char *fmt, ...), void *e STATIC machine_int_t fdfile_read(mp_obj_t o_in, void *buf, machine_uint_t size, int *errcode) { mp_obj_fdfile_t *o = o_in; + check_fd_is_open(o); machine_int_t r = read(o->fd, buf, size); if (r == -1) { *errcode = errno; @@ -36,6 +47,7 @@ STATIC machine_int_t fdfile_read(mp_obj_t o_in, void *buf, machine_uint_t size, STATIC machine_int_t fdfile_write(mp_obj_t o_in, const void *buf, machine_uint_t size, int *errcode) { mp_obj_fdfile_t *o = o_in; + check_fd_is_open(o); machine_int_t r = write(o->fd, buf, size); if (r == -1) { *errcode = errno; @@ -46,6 +58,9 @@ STATIC machine_int_t fdfile_write(mp_obj_t o_in, const void *buf, machine_uint_t STATIC mp_obj_t fdfile_close(mp_obj_t self_in) { mp_obj_fdfile_t *self = self_in; close(self->fd); +#ifdef MICROPY_CPYTHON_COMPAT + self->fd = -1; +#endif return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(fdfile_close_obj, fdfile_close); @@ -57,6 +72,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fdfile___exit___obj, 4, 4, fdfile___e STATIC mp_obj_t fdfile_fileno(mp_obj_t self_in) { mp_obj_fdfile_t *self = self_in; + check_fd_is_open(self); return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->fd); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(fdfile_fileno_obj, fdfile_fileno); |