summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorLucian Copeland <hierophect@gmail.com>2020-09-09 13:24:30 -0400
committerLucian Copeland <hierophect@gmail.com>2020-09-09 13:24:30 -0400
commitbe199983c155118717b56a405e546b3bb79c49ea (patch)
treef8d8fa2aae3b451df7463e6d7b5f1711bf43a57d /py
parent4733a672265f97a83353d005160f8e24ed8755b8 (diff)
parent66cf6c46c005cf67181e2ba58ecadc619344687d (diff)
Merge remote-tracking branch 'upstream/main' into esp32-displayio-fix
Diffstat (limited to 'py')
-rw-r--r--py/builtinimport.c2
-rw-r--r--py/circuitpy_defns.mk24
-rw-r--r--py/circuitpy_mpconfig.h1
-rw-r--r--py/circuitpy_mpconfig.mk4
-rw-r--r--py/misc.h2
-rw-r--r--py/mkrules.mk5
-rw-r--r--py/mpz.h3
-rw-r--r--py/obj.c2
-rw-r--r--py/obj.h25
-rw-r--r--py/objint.c25
-rw-r--r--py/objint.h1
-rw-r--r--py/objint_longlong.c11
-rw-r--r--py/objint_mpz.c6
-rw-r--r--py/py.mk2
14 files changed, 106 insertions, 7 deletions
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 597819f55..47ffab519 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -488,7 +488,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
// afterwards.
gc_collect();
}
- if (outer_module_obj != MP_OBJ_NULL) {
+ if (outer_module_obj != MP_OBJ_NULL && VERIFY_PTR(outer_module_obj) ) {
qstr s = qstr_from_strn(mod_str + last, i - last);
mp_store_attr(outer_module_obj, s, module_obj);
// The above store can cause a dictionary rehash and new allocation. So,
diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk
index ab7a3af60..36524b2fb 100644
--- a/py/circuitpy_defns.mk
+++ b/py/circuitpy_defns.mk
@@ -338,6 +338,15 @@ SRC_COMMON_HAL_ALL = \
watchdog/WatchDogTimer.c \
watchdog/__init__.c \
+ifeq ($(CIRCUITPY_BLEIO_HCI),1)
+# Helper code for _bleio HCI.
+SRC_C += \
+ common-hal/_bleio/att.c \
+ common-hal/_bleio/hci.c \
+
+endif
+
+
SRC_COMMON_HAL = $(filter $(SRC_PATTERNS), $(SRC_COMMON_HAL_ALL))
# These don't have corresponding files in each port but are still located in
@@ -439,7 +448,7 @@ SRC_SHARED_MODULE_ALL = \
SRC_SHARED_MODULE = $(filter $(SRC_PATTERNS), $(SRC_SHARED_MODULE_ALL))
# Use the native touchio if requested. This flag is set conditionally in, say, mpconfigport.h.
-# The presence of common-hal/touchio/* # does not imply it's available for all chips in a port,
+# The presence of common-hal/touchio/* does not imply it's available for all chips in a port,
# so there is an explicit flag. For example, SAMD21 touchio is native, but SAMD51 is not.
ifeq ($(CIRCUITPY_TOUCHIO_USE_NATIVE),1)
SRC_COMMON_HAL_ALL += \
@@ -450,6 +459,14 @@ SRC_SHARED_MODULE_ALL += \
touchio/TouchIn.c \
touchio/__init__.c
endif
+
+# If supporting _bleio via HCI, make devices/ble_hci/common-hal/_bleio be includable,
+# and use C source files in devices/ble_hci/common-hal.
+ifeq ($(CIRCUITPY_BLEIO_HCI),1)
+INC += -I$(TOP)/devices/ble_hci
+DEVICES_MODULES += $(TOP)/devices/ble_hci
+endif
+
ifeq ($(CIRCUITPY_AUDIOMP3),1)
SRC_MOD += $(addprefix lib/mp3/src/, \
bitstream.c \
@@ -483,6 +500,11 @@ $(filter $(SRC_PATTERNS), \
displayio/display_core.c \
)
+SRC_COMMON_HAL_INTERNAL = \
+$(filter $(SRC_PATTERNS), \
+ _bleio/ \
+)
+
ifeq ($(INTERNAL_LIBM),1)
SRC_LIBM = \
$(addprefix lib/,\
diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h
index 76b91defa..d144f0ba8 100644
--- a/py/circuitpy_mpconfig.h
+++ b/py/circuitpy_mpconfig.h
@@ -185,6 +185,7 @@ typedef long mp_off_t;
// Turning off FULL_BUILD removes some functionality to reduce flash size on tiny SAMD21s
#define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (CIRCUITPY_FULL_BUILD)
#define MICROPY_CPYTHON_COMPAT (CIRCUITPY_FULL_BUILD)
+#define MICROPY_PY_BUILTINS_POW3 (CIRCUITPY_FULL_BUILD)
#define MICROPY_COMP_FSTRING_LITERAL (MICROPY_CPYTHON_COMPAT)
#define MICROPY_MODULE_WEAK_LINKS (CIRCUITPY_FULL_BUILD)
#define MICROPY_PY_ALL_SPECIAL_METHODS (CIRCUITPY_FULL_BUILD)
diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk
index 379c3850e..90e8b5fde 100644
--- a/py/circuitpy_mpconfig.mk
+++ b/py/circuitpy_mpconfig.mk
@@ -80,6 +80,10 @@ CFLAGS += -DCIRCUITPY_BITBANGIO=$(CIRCUITPY_BITBANGIO)
CIRCUITPY_BLEIO ?= 0
CFLAGS += -DCIRCUITPY_BLEIO=$(CIRCUITPY_BLEIO)
+# _bleio can be supported on most any board via HCI
+CIRCUITPY_BLEIO_HCI ?= 0
+CFLAGS += -DCIRCUITPY_BLEIO_HCI=$(CIRCUITPY_BLEIO_HCI)
+
CIRCUITPY_BOARD ?= 1
CFLAGS += -DCIRCUITPY_BOARD=$(CIRCUITPY_BOARD)
diff --git a/py/misc.h b/py/misc.h
index 944c0a716..6ed256e70 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -120,6 +120,8 @@ size_t m_get_peak_bytes_allocated(void);
// align ptr to the nearest multiple of "alignment"
#define MP_ALIGN(ptr, alignment) (void*)(((uintptr_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
+#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
+
/** unichar / UTF-8 *********************************************/
#if MICROPY_PY_BUILTINS_STR_UNICODE
diff --git a/py/mkrules.mk b/py/mkrules.mk
index 13a73b90e..0b3ecc0e1 100644
--- a/py/mkrules.mk
+++ b/py/mkrules.mk
@@ -42,7 +42,7 @@ $(Q)$(CC) $(CFLAGS) -c -MD -o $@ $<
$(RM) -f $(@:.o=.d)
endef
-vpath %.c . $(TOP) $(USER_C_MODULES)
+vpath %.c . $(TOP) $(USER_C_MODULES) $(DEVICES_MODULES)
$(BUILD)/%.o: %.c
$(call compile_c)
@@ -56,8 +56,7 @@ $(BUILD)/%.o: %.c
QSTR_GEN_EXTRA_CFLAGS += -I$(BUILD)/tmp
-vpath %.c . $(TOP) $(USER_C_MODULES)
-
+vpath %.c . $(TOP) $(USER_C_MODULES) $(DEVICES_MODULES)
$(BUILD)/%.pp: %.c
$(STEPECHO) "PreProcess $<"
$(Q)$(CC) $(CFLAGS) -E -Wp,-C,-dD,-dI -o $@ $<
diff --git a/py/mpz.h b/py/mpz.h
index e412f5cce..ade04a4f7 100644
--- a/py/mpz.h
+++ b/py/mpz.h
@@ -135,6 +135,9 @@ void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs);
static inline size_t mpz_max_num_bits(const mpz_t *z) { return z->len * MPZ_DIG_SIZE; }
+static inline size_t mpz_num_bits(const mpz_t *z) {
+ size_t last_bits = (8 * (sizeof(long) - sizeof(mpz_dig_t))) - __builtin_clzl(z->dig[z->len-1]);
+ return z->len * MPZ_DIG_SIZE + last_bits; }
mp_int_t mpz_hash(const mpz_t *z);
bool mpz_as_int_checked(const mpz_t *z, mp_int_t *value);
bool mpz_as_uint_checked(const mpz_t *z, mp_uint_t *value);
diff --git a/py/obj.c b/py/obj.c
index 0cff2f1a4..9dc0cf474 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -67,6 +67,8 @@ void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
#ifdef RUN_BACKGROUND_TASKS
RUN_BACKGROUND_TASKS;
#endif
+ mp_handle_pending();
+
#ifndef NDEBUG
if (o_in == MP_OBJ_NULL) {
mp_print_str(print, "(nil)");
diff --git a/py/obj.h b/py/obj.h
index e9d867f77..943e1a389 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -303,7 +303,7 @@ typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
(mp_obj_t)&mp_const_none_obj, \
(mp_obj_t)&mp_const_none_obj}, }
-// These macros are used to define constant map/dict objects
+// These macros are used to define constant or mutable map/dict objects
// You can put "static" in front of the definition to make it local
#define MP_DEFINE_CONST_MAP(map_name, table_name) \
@@ -329,6 +329,29 @@ typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
}, \
}
+#define MP_DEFINE_MUTABLE_MAP(map_name, table_name) \
+ mp_map_t map_name = { \
+ .all_keys_are_qstrs = 1, \
+ .is_fixed = 1, \
+ .is_ordered = 1, \
+ .used = MP_ARRAY_SIZE(table_name), \
+ .alloc = MP_ARRAY_SIZE(table_name), \
+ .table = table_name, \
+ }
+
+#define MP_DEFINE_MUTABLE_DICT(dict_name, table_name) \
+ mp_obj_dict_t dict_name = { \
+ .base = {&mp_type_dict}, \
+ .map = { \
+ .all_keys_are_qstrs = 1, \
+ .is_fixed = 1, \
+ .is_ordered = 1, \
+ .used = MP_ARRAY_SIZE(table_name), \
+ .alloc = MP_ARRAY_SIZE(table_name), \
+ .table = table_name, \
+ }, \
+ }
+
// These macros are used to declare and define constant staticmethond and classmethod objects
// You can put "static" in front of the definitions to make them local
diff --git a/py/objint.c b/py/objint.c
index b12bb3995..5a33ccbc0 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -457,6 +457,28 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp
return MP_OBJ_NULL; // op not supported
}
+#if MICROPY_CPYTHON_COMPAT
+STATIC mp_obj_t int_bit_length(mp_obj_t self_in) {
+ #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+ if (!MP_OBJ_IS_SMALL_INT(self_in)) {
+ return mp_obj_int_bit_length_impl(self_in);
+ }
+ else
+ #endif
+ {
+ mp_int_t int_val = MP_OBJ_SMALL_INT_VALUE(self_in);
+ mp_uint_t value =
+ (int_val == 0) ? 0 :
+ (int_val == MP_SMALL_INT_MIN) ? 8 * sizeof(mp_int_t) :
+ (int_val < 0) ? 8 * sizeof(long) - __builtin_clzl(-int_val) :
+ 8 * sizeof(long) - __builtin_clzl(int_val);
+ return mp_obj_new_int_from_uint(value);
+ }
+
+}
+MP_DEFINE_CONST_FUN_OBJ_1(int_bit_length_obj, int_bit_length);
+#endif
+
// this is a classmethod
STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) {
// TODO: Support signed param (assumes signed=False at the moment)
@@ -537,6 +559,9 @@ STATIC mp_obj_t int_to_bytes(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(int_to_bytes_obj, 3, int_to_bytes);
STATIC const mp_rom_map_elem_t int_locals_dict_table[] = {
+#if MICROPY_CPYTHON_COMPAT
+ { MP_ROM_QSTR(MP_QSTR_bit_length), MP_ROM_PTR(&int_bit_length_obj) },
+#endif
{ MP_ROM_QSTR(MP_QSTR_from_bytes), MP_ROM_PTR(&int_from_bytes_obj) },
{ MP_ROM_QSTR(MP_QSTR_to_bytes), MP_ROM_PTR(&int_to_bytes_obj) },
};
diff --git a/py/objint.h b/py/objint.h
index bba9ff50a..68997ced2 100644
--- a/py/objint.h
+++ b/py/objint.h
@@ -60,6 +60,7 @@ void mp_obj_int_buffer_overflow_check(mp_obj_t self_in, size_t nbytes, bool is_s
void mp_small_int_buffer_overflow_check(mp_int_t val, size_t nbytes, bool is_signed);
mp_int_t mp_obj_int_hash(mp_obj_t self_in);
+mp_obj_t mp_obj_int_bit_length_impl(mp_obj_t self_in);
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf);
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf);
int mp_obj_int_sign(mp_obj_t self_in);
diff --git a/py/objint_longlong.c b/py/objint_longlong.c
index 189049630..0a65098c7 100644
--- a/py/objint_longlong.c
+++ b/py/objint_longlong.c
@@ -45,6 +45,17 @@
const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
#endif
+mp_obj_t mp_obj_int_bit_length_impl(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
+ mp_obj_int_t *self = self_in;
+ long long val = self->val;
+ return MP_OBJ_NEW_SMALL_INT(
+ (val == 0) ? 0 :
+ (val == MP_SMALL_INT_MIN) ? 8 * sizeof(long long) :
+ (val < 0) ? 8 * sizeof(long long) - __builtin_clzll(-val) :
+ 8 * sizeof(long long) - __builtin_clzll(val));
+}
+
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
int delta = 1;
if (!big_endian) {
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 90060114e..d32fdfbe8 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -107,6 +107,12 @@ char *mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size,
return str;
}
+mp_obj_t mp_obj_int_bit_length_impl(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
+ mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
+ return MP_OBJ_NEW_SMALL_INT(mpz_num_bits(&self->mpz));
+}
+
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
mp_obj_int_t *o = mp_obj_int_new_mpz();
mpz_set_from_bytes(&o->mpz, big_endian, len, buf);
diff --git a/py/py.mk b/py/py.mk
index c5073d0f0..49fb0acaf 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -19,7 +19,7 @@ endif
QSTR_GLOBAL_DEPENDENCIES += $(PY_SRC)/mpconfig.h mpconfigport.h
# some code is performance bottleneck and compiled with other optimization options
-CSUPEROPT = -O3
+_CSUPEROPT = -O3
# this sets the config file for FatFs
CFLAGS_MOD += -DFFCONF_H=\"lib/oofatfs/ffconf.h\"