summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--py/compile.c41
-rw-r--r--py/mpconfig.h5
-rw-r--r--stmhal/Makefile3
-rw-r--r--stmhal/modstm.c120
-rw-r--r--stmhal/modstm.h1
-rw-r--r--stmhal/mpconfigport.h7
-rwxr-xr-xstmhal/pybcdc.inf4
-rw-r--r--stmhal/qstrdefsport.h16
8 files changed, 190 insertions, 7 deletions
diff --git a/py/compile.c b/py/compile.c
index ca3ee9d6d1..4738c0ae55 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -78,6 +78,19 @@ STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const cha
comp->had_error = true;
}
+STATIC const mp_map_elem_t mp_constants_table[] = {
+ // Extra constants as defined by a port
+ MICROPY_EXTRA_CONSTANTS
+};
+
+STATIC const mp_map_t mp_constants_map = {
+ .all_keys_are_qstrs = 1,
+ .table_is_fixed_array = 1,
+ .used = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
+ .alloc = sizeof(mp_constants_table) / sizeof(mp_map_elem_t),
+ .table = (mp_map_elem_t*)mp_constants_table,
+};
+
mp_parse_node_t fold_constants(mp_parse_node_t pn) {
if (MP_PARSE_NODE_IS_STRUCT(pn)) {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
@@ -168,10 +181,12 @@ mp_parse_node_t fold_constants(mp_parse_node_t pn) {
}
break;
-#if MICROPY_EMIT_CPYTHON
case PN_power:
- // can overflow; enabled only to compare with CPython
- if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
+ if (0) {
+#if MICROPY_EMIT_CPYTHON
+ } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
+ // int**x
+ // can overflow; enabled only to compare with CPython
mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
@@ -184,9 +199,27 @@ mp_parse_node_t fold_constants(mp_parse_node_t pn) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
}
}
+#endif
+ } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
+ // id.id
+ // look it up in constant table, see if it can be replaced with an integer
+ mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
+ assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
+ qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
+ qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
+ mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
+ if (elem != NULL) {
+ mp_obj_t dest[2];
+ mp_load_method_maybe(elem->value, q_attr, dest);
+ if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
+ machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
+ if (MP_PARSE_FITS_SMALL_INT(val)) {
+ pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
+ }
+ }
+ }
}
break;
-#endif
}
}
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 1f2862ab0e..b120c4bb4a 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -150,6 +150,11 @@ typedef double mp_float_t;
#define MICROPY_EXTRA_BUILTIN_MODULES
#endif
+// Additional constant definitions for the compiler - see compile.c:mp_constants_table.
+#ifndef MICROPY_EXTRA_CONSTANTS
+#define MICROPY_EXTRA_CONSTANTS
+#endif
+
/*****************************************************************************/
/* Miscellaneous settings */
diff --git a/stmhal/Makefile b/stmhal/Makefile
index 064db23c35..9789189946 100644
--- a/stmhal/Makefile
+++ b/stmhal/Makefile
@@ -77,8 +77,9 @@ SRC_C = \
pyexec.c \
help.c \
input.c \
- modpyb.c \
modos.c \
+ modpyb.c \
+ modstm.c \
modtime.c \
import.c \
lexerfatfs.c \
diff --git a/stmhal/modstm.c b/stmhal/modstm.c
new file mode 100644
index 0000000000..5e72a75bb2
--- /dev/null
+++ b/stmhal/modstm.c
@@ -0,0 +1,120 @@
+#include <stdio.h>
+#include <stdint.h>
+
+#include <stm32f4xx_hal.h>
+
+#include "nlr.h"
+#include "misc.h"
+#include "mpconfig.h"
+#include "qstr.h"
+#include "obj.h"
+#include "modstm.h"
+
+STATIC uint32_t get_read_addr(mp_obj_t addr_o, uint align) {
+ uint32_t addr = mp_obj_get_int(addr_o) & 0x7fffffff;
+ if (addr < 0x10000000) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "cannot read from address %08x", addr));
+ }
+ if ((addr & (align - 1)) != 0) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
+ }
+ return addr;
+}
+
+STATIC uint32_t get_write_addr(mp_obj_t addr_o, uint align) {
+ uint32_t addr = mp_obj_get_int(addr_o) & 0x7fffffff;
+ if (addr < 0x10000000) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "cannot write to address %08x", addr));
+ }
+ if ((addr & (align - 1)) != 0) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "address %08x is not aligned to %d bytes", addr, align));
+ }
+ return addr;
+}
+
+STATIC mp_obj_t stm_read8(mp_obj_t addr) {
+ uint32_t a = get_read_addr(addr, 1);
+ uint32_t v = *(uint8_t*)a;
+ return mp_obj_new_int(v);
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(stm_read8_obj, stm_read8);
+
+STATIC mp_obj_t stm_read16(mp_obj_t addr) {
+ uint32_t a = get_read_addr(addr, 2);
+ uint32_t v = *(uint16_t*)a;
+ return mp_obj_new_int(v);
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(stm_read16_obj, stm_read16);
+
+STATIC mp_obj_t stm_read32(mp_obj_t addr) {
+ uint32_t a = get_read_addr(addr, 4);
+ uint32_t v = *(uint32_t*)a;
+ return mp_obj_new_int(v);
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(stm_read32_obj, stm_read32);
+
+STATIC mp_obj_t stm_write8(mp_obj_t addr, mp_obj_t val) {
+ uint32_t a = get_write_addr(addr, 1);
+ uint32_t v = mp_obj_get_int(val);
+ *(uint8_t*)a = v;
+ return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(stm_write8_obj, stm_write8);
+
+STATIC mp_obj_t stm_write16(mp_obj_t addr, mp_obj_t val) {
+ uint32_t a = get_write_addr(addr, 2);
+ uint32_t v = mp_obj_get_int(val);
+ *(uint16_t*)a = v;
+ return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(stm_write16_obj, stm_write16);
+
+STATIC mp_obj_t stm_write32(mp_obj_t addr, mp_obj_t val) {
+ uint32_t a = get_write_addr(addr, 4);
+ uint32_t v = mp_obj_get_int(val);
+ *(uint32_t*)a = v;
+ return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(stm_write32_obj, stm_write32);
+
+STATIC const mp_map_elem_t stm_module_globals_table[] = {
+ { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_stm) },
+
+ { MP_OBJ_NEW_QSTR(MP_QSTR_read8), (mp_obj_t)&stm_read8_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_read16), (mp_obj_t)&stm_read16_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_read32), (mp_obj_t)&stm_read32_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_write8), (mp_obj_t)&stm_write8_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_write16), (mp_obj_t)&stm_write16_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_write32), (mp_obj_t)&stm_write32_obj },
+
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIOA), MP_OBJ_NEW_SMALL_INT(GPIOA_BASE) },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIOB), MP_OBJ_NEW_SMALL_INT(GPIOB_BASE) },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIOC), MP_OBJ_NEW_SMALL_INT(GPIOC_BASE) },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIOD), MP_OBJ_NEW_SMALL_INT(GPIOD_BASE) },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO_IDR), MP_OBJ_NEW_SMALL_INT(0x10) },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO_BSRRL), MP_OBJ_NEW_SMALL_INT(0x18) },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO_BSRRH), MP_OBJ_NEW_SMALL_INT(0x1a) },
+};
+
+STATIC const mp_obj_dict_t stm_module_globals = {
+ .base = {&mp_type_dict},
+ .map = {
+ .all_keys_are_qstrs = 1,
+ .table_is_fixed_array = 1,
+ .used = sizeof(stm_module_globals_table) / sizeof(mp_map_elem_t),
+ .alloc = sizeof(stm_module_globals_table) / sizeof(mp_map_elem_t),
+ .table = (mp_map_elem_t*)stm_module_globals_table,
+ },
+};
+
+const mp_obj_module_t stm_module = {
+ .base = { &mp_type_module },
+ .name = MP_QSTR_stm,
+ .globals = (mp_obj_dict_t*)&stm_module_globals,
+};
diff --git a/stmhal/modstm.h b/stmhal/modstm.h
new file mode 100644
index 0000000000..205cdd2a18
--- /dev/null
+++ b/stmhal/modstm.h
@@ -0,0 +1 @@
+extern const mp_obj_module_t stm_module;
diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h
index 08ba923e40..5529101425 100644
--- a/stmhal/mpconfigport.h
+++ b/stmhal/mpconfigport.h
@@ -31,12 +31,19 @@ extern const struct _mp_obj_fun_native_t mp_builtin_open_obj;
// extra built in modules to add to the list of known ones
extern const struct _mp_obj_module_t os_module;
extern const struct _mp_obj_module_t pyb_module;
+extern const struct _mp_obj_module_t stm_module;
extern const struct _mp_obj_module_t time_module;
#define MICROPY_EXTRA_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&os_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_stm), (mp_obj_t)&stm_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \
+// extra constants
+#define MICROPY_EXTRA_CONSTANTS \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_stm), (mp_obj_t)&stm_module }, \
+
// type definitions for the specific machine
#define BYTES_PER_WORD (4)
diff --git a/stmhal/pybcdc.inf b/stmhal/pybcdc.inf
index 372c313e98..eb04b65cee 100755
--- a/stmhal/pybcdc.inf
+++ b/stmhal/pybcdc.inf
@@ -77,10 +77,10 @@ ServiceBinary=%12%\usbser.sys
[SourceDisksFiles]
[SourceDisksNames]
[DeviceList]
-%DESCRIPTION%=DriverInstall, USB\VID_0483&PID_5740&MI_00
+%DESCRIPTION%=DriverInstall, USB\VID_0483&PID_5740&MI_00, USB\VID_0483&PID_5740&MI_01
[DeviceList.NTamd64]
-%DESCRIPTION%=DriverInstall, USB\VID_0483&PID_5740&MI_00
+%DESCRIPTION%=DriverInstall, USB\VID_0483&PID_5740&MI_00, USB\VID_0483&PID_5740&MI_01
;---------------------------------------------------------------------
; String Definitions
diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h
index 4eb6ea12a0..546b5b1430 100644
--- a/stmhal/qstrdefsport.h
+++ b/stmhal/qstrdefsport.h
@@ -130,3 +130,19 @@ Q(sleep)
// for input
Q(input)
+
+// for stm module
+Q(stm)
+Q(read8)
+Q(read16)
+Q(read32)
+Q(write8)
+Q(write16)
+Q(write32)
+Q(GPIOA)
+Q(GPIOB)
+Q(GPIOC)
+Q(GPIOD)
+Q(GPIO_IDR)
+Q(GPIO_BSRRL)
+Q(GPIO_BSRRH)