From 58722103e10fa53e502febf8684c741171416179 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 20 Jan 2017 23:58:42 +0300 Subject: zephyr/help: Update n_args param type to size_t. --- zephyr/help.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/help.c b/zephyr/help.c index e574adf47..e40a11781 100644 --- a/zephyr/help.c +++ b/zephyr/help.c @@ -41,7 +41,7 @@ STATIC const char *help_text = "For further help on a specific object, type help(obj)\n" ; -STATIC mp_obj_t builtin_help(uint n_args, const mp_obj_t *args) { +STATIC mp_obj_t builtin_help(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { // print a general help message printf("%s", help_text); -- cgit v1.2.3 From f1f2a3cebf607f920cfa44f6aeffe847844edb02 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 21 Jan 2017 00:26:03 +0300 Subject: zephyr: Remove deprecated .mdef file. --- zephyr/Makefile.zephyr | 1 - zephyr/main.c | 4 ++-- zephyr/prj.conf | 1 + zephyr/prj.mdef | 5 ----- 4 files changed, 3 insertions(+), 8 deletions(-) delete mode 100644 zephyr/prj.mdef (limited to 'zephyr') diff --git a/zephyr/Makefile.zephyr b/zephyr/Makefile.zephyr index ad905a4db..214767bea 100644 --- a/zephyr/Makefile.zephyr +++ b/zephyr/Makefile.zephyr @@ -18,7 +18,6 @@ KERNEL_TYPE = micro # BOARD must be passed on command line from main Makefile #BOARD = CONF_FILE = prj.conf -MDEF_FILE = prj.mdef #export SOURCE_DIR = $(ZEPHYR_BASE)/samples/static_lib/hello_world export LDFLAGS_zephyr += -L$(CURDIR) diff --git a/zephyr/main.c b/zephyr/main.c index 2c7f3fc47..d812f9609 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -64,8 +64,8 @@ int real_main(void) { int stack_dummy; stack_top = (char*)&stack_dummy; mp_stack_set_top(stack_top); - // Should be set to stack size in prj.mdef minus fuzz factor - mp_stack_set_limit(3584); + // Make MicroPython's stack limit somewhat smaller than full stack available + mp_stack_set_limit(CONFIG_MAIN_STACK_SIZE - 512); soft_reset: #if MICROPY_ENABLE_GC diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 0b0827d25..5d6b353ba 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -3,3 +3,4 @@ CONFIG_CONSOLE_HANDLER=y CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS=y CONFIG_NEWLIB_LIBC=y CONFIG_FLOAT=y +CONFIG_MAIN_STACK_SIZE=4096 diff --git a/zephyr/prj.mdef b/zephyr/prj.mdef deleted file mode 100644 index 5a7312f89..000000000 --- a/zephyr/prj.mdef +++ /dev/null @@ -1,5 +0,0 @@ -% Application : MicroPython - -% TASK NAME PRIO ENTRY STACK GROUPS -% ================================== - TASK MAIN 7 main 4096 [EXE] -- cgit v1.2.3 From 1459f8142919352b5615751566d443e0c319bc8e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 21 Jan 2017 16:13:32 +0300 Subject: zephyr: Add separate Zephyr config for "minimal" build. In anticipation of enabling more features in the default build. Also, fix compilation of minimal build. --- zephyr/Makefile | 2 +- zephyr/mpconfigport_minimal.h | 1 + zephyr/prj_minimal.conf | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 zephyr/prj_minimal.conf (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index 9ddf12125..1d247af1a 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -84,7 +84,7 @@ $(Z_SYSGEN_H): -$(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) minimal: - $(MAKE) BOARD=$(BOARD) CFLAGS_EXTRA='-DMP_CONFIGFILE=""' FROZEN_DIR= + $(MAKE) BOARD=$(BOARD) CONF_FILE=prj_minimal.conf CFLAGS_EXTRA='-DMP_CONFIGFILE=""' FROZEN_DIR= # Clean Zephyr things too clean: z_clean diff --git a/zephyr/mpconfigport_minimal.h b/zephyr/mpconfigport_minimal.h index 4ecc03afb..459b0816c 100644 --- a/zephyr/mpconfigport_minimal.h +++ b/zephyr/mpconfigport_minimal.h @@ -37,6 +37,7 @@ #define MICROPY_ENABLE_GC (1) #define MICROPY_HELPER_REPL (1) #define MICROPY_REPL_AUTO_INDENT (1) +#define MICROPY_KBD_EXCEPTION (1) #define MICROPY_CPYTHON_COMPAT (0) #define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_ATTRTUPLE (0) diff --git a/zephyr/prj_minimal.conf b/zephyr/prj_minimal.conf new file mode 100644 index 000000000..5d6b353ba --- /dev/null +++ b/zephyr/prj_minimal.conf @@ -0,0 +1,6 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_CONSOLE_HANDLER=y +CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS=y +CONFIG_NEWLIB_LIBC=y +CONFIG_FLOAT=y +CONFIG_MAIN_STACK_SIZE=4096 -- cgit v1.2.3 From 5298472fee1a83878e4670c1f8c885aefc087765 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 21 Jan 2017 17:16:35 +0300 Subject: zephyr: Enable SLIP networking for the default build. This makes MicroPython app running in QEMU be pingable from the host (by following QEMU networking setup instructions, https://www.zephyrproject.org/doc/samples/net/qemu_setup.html). --- zephyr/main.c | 16 ++++++++++++++++ zephyr/prj.conf | 13 +++++++++++++ 2 files changed, 29 insertions(+) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index d812f9609..6d7fca802 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -28,6 +28,12 @@ #include #include +#include +#ifdef CONFIG_NETWORKING +#include +#include +#endif + #include "py/nlr.h" #include "py/compile.h" #include "py/runtime.h" @@ -60,6 +66,14 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) { static char *stack_top; static char heap[MICROPY_HEAP_SIZE]; +void init_zephyr(void) { + #ifdef CONFIG_NET_IPV4 + // TODO: Make address configurable + static struct in_addr in4addr_my = { { { 192, 0, 2, 1 } } }; + net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my, NET_ADDR_MANUAL, 0); + #endif +} + int real_main(void) { int stack_dummy; stack_top = (char*)&stack_dummy; @@ -67,6 +81,8 @@ int real_main(void) { // Make MicroPython's stack limit somewhat smaller than full stack available mp_stack_set_limit(CONFIG_MAIN_STACK_SIZE - 512); + init_zephyr(); + soft_reset: #if MICROPY_ENABLE_GC gc_init(heap, heap + sizeof(heap)); diff --git a/zephyr/prj.conf b/zephyr/prj.conf index 5d6b353ba..a2fcb64d9 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -4,3 +4,16 @@ CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS=y CONFIG_NEWLIB_LIBC=y CONFIG_FLOAT=y CONFIG_MAIN_STACK_SIZE=4096 + +# Networking config +CONFIG_NETWORKING=y +CONFIG_NET_IPV4=y +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_NET_NBUF_RX_COUNT=4 + +# Networking drivers +# SLIP driver for QEMU +CONFIG_NET_SLIP_TAP=y + +# BOARD-specific config (qemu_x86) +CONFIG_RAM_SIZE=256 -- cgit v1.2.3 From 77e0cee49ec182b95cefc7a00a1c96b5db278cb2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 22 Jan 2017 10:31:02 +1100 Subject: zephyr: Convert to use builtin help function. --- zephyr/Makefile | 1 - zephyr/help.c | 20 ++------------------ zephyr/mpconfigport.h | 3 ++- 3 files changed, 4 insertions(+), 20 deletions(-) (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index 1d247af1a..ae707ff75 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -44,7 +44,6 @@ SRC_C = main.c \ lib/utils/printf.c \ lib/utils/pyexec.c \ lib/utils/interrupt_char.c \ - lib/utils/pyhelp.c \ lib/mp-readline/readline.c \ $(SRC_MOD) diff --git a/zephyr/help.c b/zephyr/help.c index e40a11781..0c7f27940 100644 --- a/zephyr/help.c +++ b/zephyr/help.c @@ -24,11 +24,9 @@ * THE SOFTWARE. */ -#include +#include "py/builtin.h" -#include "lib/utils/pyhelp.h" - -STATIC const char *help_text = +const char *zephyr_help_text = "Welcome to MicroPython!\n" "\n" "Control commands:\n" @@ -40,17 +38,3 @@ STATIC const char *help_text = "\n" "For further help on a specific object, type help(obj)\n" ; - -STATIC mp_obj_t builtin_help(size_t n_args, const mp_obj_t *args) { - if (n_args == 0) { - // print a general help message - printf("%s", help_text); - - } else { - // try to print something sensible about the given object - pyhelp_print_obj(args[0]); - } - - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, builtin_help); diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index 26fb1410f..2cd5ffcec 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -48,6 +48,8 @@ #define MICROPY_PY_BUILTINS_RANGE_ATTRS (0) #define MICROPY_PY_BUILTINS_REVERSED (0) #define MICROPY_PY_BUILTINS_SET (0) +#define MICROPY_PY_BUILTINS_HELP (1) +#define MICROPY_PY_BUILTINS_HELP_TEXT zephyr_help_text #define MICROPY_PY_ARRAY (0) #define MICROPY_PY_COLLECTIONS (0) #define MICROPY_PY_CMATH (0) @@ -114,5 +116,4 @@ extern const struct _mp_obj_module_t mp_module_time; // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ - { MP_OBJ_NEW_QSTR(MP_QSTR_help), (mp_obj_t)&mp_builtin_help_obj }, \ -- cgit v1.2.3 From 40e72e4a5cc59fa005259b9e85393a13a32c0a12 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 27 Jan 2017 21:28:03 +0300 Subject: zephyr: Make sure that correct Zephyr config is used for "minimal" build. Overriding CONF_FILE in "minimal" target itself is too late due to include- pinned $(Z_EXPORTS) target. --- zephyr/Makefile | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index ae707ff75..7f35eefb5 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -7,7 +7,12 @@ # BOARD ?= qemu_x86 +ifeq ($(MAKECMDGOALS), minimal) +# For minimal, CONF_FILE must be overriden early due to $(Z_EXPORTS) target +CONF_FILE = prj_minimal.conf +else CONF_FILE = prj.conf +endif # Zephyr 1.5.0 #OUTDIR_PREFIX = # Zephyr 1.6.0 -- cgit v1.2.3 From 1df1642b706eb6d39f93936e70df22cbdfb0ec36 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 27 Jan 2017 23:42:11 +0300 Subject: zephyr: Allow to have per-board Zephyr config fragments. To enable options which may be incompatible with other boards, etc. --- zephyr/Makefile | 7 ++++++- zephyr/prj.conf | 19 ------------------- zephyr/prj_base.conf | 12 ++++++++++++ zephyr/prj_qemu_x86.conf | 6 ++++++ 4 files changed, 24 insertions(+), 20 deletions(-) delete mode 100644 zephyr/prj.conf create mode 100644 zephyr/prj_base.conf create mode 100644 zephyr/prj_qemu_x86.conf (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index 7f35eefb5..4c28e4da8 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -62,7 +62,7 @@ CFLAGS = $(KBUILD_CFLAGS) $(NOSTDINC_FLAGS) $(ZEPHYRINCLUDE) \ include ../py/mkrules.mk -$(Z_EXPORTS): +$(Z_EXPORTS): $(CONF_FILE) $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) initconfig outputexports GENERIC_TARGETS = all zephyr qemu qemugdb flash debug @@ -95,3 +95,8 @@ clean: z_clean z_clean: $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) clean + +.PHONY: prj.conf +prj.conf: prj_base.conf + cat $< >$@ + if [ -f prj_$(BOARD).conf ]; then cat prj_$(BOARD).conf >>$@; fi diff --git a/zephyr/prj.conf b/zephyr/prj.conf deleted file mode 100644 index a2fcb64d9..000000000 --- a/zephyr/prj.conf +++ /dev/null @@ -1,19 +0,0 @@ -CONFIG_STDOUT_CONSOLE=y -CONFIG_CONSOLE_HANDLER=y -CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS=y -CONFIG_NEWLIB_LIBC=y -CONFIG_FLOAT=y -CONFIG_MAIN_STACK_SIZE=4096 - -# Networking config -CONFIG_NETWORKING=y -CONFIG_NET_IPV4=y -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_NET_NBUF_RX_COUNT=4 - -# Networking drivers -# SLIP driver for QEMU -CONFIG_NET_SLIP_TAP=y - -# BOARD-specific config (qemu_x86) -CONFIG_RAM_SIZE=256 diff --git a/zephyr/prj_base.conf b/zephyr/prj_base.conf new file mode 100644 index 000000000..43aedd8fe --- /dev/null +++ b/zephyr/prj_base.conf @@ -0,0 +1,12 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_CONSOLE_HANDLER=y +CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS=y +CONFIG_NEWLIB_LIBC=y +CONFIG_FLOAT=y +CONFIG_MAIN_STACK_SIZE=4096 + +# Networking config +CONFIG_NETWORKING=y +CONFIG_NET_IPV4=y +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_NET_NBUF_RX_COUNT=4 diff --git a/zephyr/prj_qemu_x86.conf b/zephyr/prj_qemu_x86.conf new file mode 100644 index 000000000..cb90834ac --- /dev/null +++ b/zephyr/prj_qemu_x86.conf @@ -0,0 +1,6 @@ +# Networking drivers +# SLIP driver for QEMU +CONFIG_NET_SLIP_TAP=y + +# Default RAM easily overflows with uPy and networking +CONFIG_RAM_SIZE=256 -- cgit v1.2.3 From 300ecac336ba48451dc76016f46e826ddf6582f4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 27 Jan 2017 23:48:42 +0300 Subject: zephyr/prj_frdm_k64f.conf: Add, enable Ethernet support. --- zephyr/prj_frdm_k64f.conf | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 zephyr/prj_frdm_k64f.conf (limited to 'zephyr') diff --git a/zephyr/prj_frdm_k64f.conf b/zephyr/prj_frdm_k64f.conf new file mode 100644 index 000000000..611d6bc0a --- /dev/null +++ b/zephyr/prj_frdm_k64f.conf @@ -0,0 +1,2 @@ +# Networking drivers +CONFIG_NET_L2_ETHERNET=y -- cgit v1.2.3 From d335904666c43f0cfe518de41245b969cfca81af Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 30 Jan 2017 21:27:29 +0300 Subject: zephyr/Makefile.zephyr: Support and default to networked (SLIP) QEMU. Also works for non-networked builds (like minimal). --- zephyr/Makefile.zephyr | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'zephyr') diff --git a/zephyr/Makefile.zephyr b/zephyr/Makefile.zephyr index 214767bea..16f0a9452 100644 --- a/zephyr/Makefile.zephyr +++ b/zephyr/Makefile.zephyr @@ -18,9 +18,13 @@ KERNEL_TYPE = micro # BOARD must be passed on command line from main Makefile #BOARD = CONF_FILE = prj.conf +QEMU_NET = 1 #export SOURCE_DIR = $(ZEPHYR_BASE)/samples/static_lib/hello_world export LDFLAGS_zephyr += -L$(CURDIR) export ALL_LIBS += micropython include ${ZEPHYR_BASE}/Makefile.inc +ifeq ($(QEMU_NET), 1) +include ${ZEPHYR_BASE}/samples/net/common/Makefile.ipstack +endif -- cgit v1.2.3 From 00bd14539823a40cf98cabd826b0585f37732689 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 1 Feb 2017 21:23:29 +0300 Subject: zephyr/README: Describe many gotchas of networked builds. --- zephyr/Makefile | 3 +++ zephyr/README.md | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index 4c28e4da8..3779765fb 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -90,6 +90,9 @@ $(Z_SYSGEN_H): minimal: $(MAKE) BOARD=$(BOARD) CONF_FILE=prj_minimal.conf CFLAGS_EXTRA='-DMP_CONFIGFILE=""' FROZEN_DIR= +qemu-minimal: + $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=prj_minimal.conf QEMU_NET=0 run + # Clean Zephyr things too clean: z_clean diff --git a/zephyr/README.md b/zephyr/README.md index 30f668bb4..65729df5b 100644 --- a/zephyr/README.md +++ b/zephyr/README.md @@ -49,8 +49,20 @@ qemu_cortex_m3): make qemu +With the default configuration, networking is now enabled, so you need to +follow instructions in https://wiki.zephyrproject.org/view/Networking-with-Qemu +to setup host side of TAP/SLIP networking. If you get error like: + + could not connect serial device to character backend 'unix:/tmp/slip.sock' + +it's a sign that you didn't followed instructions above. If you would like +to just run it quickly without extra setup, see "minimal" build below. + For deploying/flashing a firmware on a real board, follow Zephyr -documentation for a given board. +documentation for a given board (mind again that networking is enabled +for the build, so you should be aware of known issues for a particular +board; for example, frdm_k64f requires Ethernet cable connected to both +board and host or it will hang/crash on startup). Quick example @@ -93,3 +105,8 @@ default over time. To make a minimal build: make BOARD= minimal + +To run a minimal build in QEMU without requiring TAP networking setup +run the following after you built image with the previous command: + + make BOARD= qemu-minimal -- cgit v1.2.3 From 64916436b2b6ded6770cd58d3cb4f5382c2f7d1e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 14 Feb 2017 16:23:54 +0300 Subject: zephyr: Enable IPv6 networking in addition to IPv4. --- zephyr/main.c | 9 +++++++-- zephyr/prj_base.conf | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index 6d7fca802..a11a6dbda 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -67,11 +67,16 @@ static char *stack_top; static char heap[MICROPY_HEAP_SIZE]; void init_zephyr(void) { + // TODO: Make addresses configurable #ifdef CONFIG_NET_IPV4 - // TODO: Make address configurable - static struct in_addr in4addr_my = { { { 192, 0, 2, 1 } } }; + static struct in_addr in4addr_my = {{{192, 0, 2, 1}}}; net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my, NET_ADDR_MANUAL, 0); #endif + #ifdef CONFIG_NET_IPV6 + // 2001:db8::1 + static struct in6_addr in6addr_my = {{{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}}; + net_if_ipv6_addr_add(net_if_get_default(), &in6addr_my, NET_ADDR_MANUAL, 0); + #endif } int real_main(void) { diff --git a/zephyr/prj_base.conf b/zephyr/prj_base.conf index 43aedd8fe..295963023 100644 --- a/zephyr/prj_base.conf +++ b/zephyr/prj_base.conf @@ -8,5 +8,6 @@ CONFIG_MAIN_STACK_SIZE=4096 # Networking config CONFIG_NETWORKING=y CONFIG_NET_IPV4=y +CONFIG_NET_IPV6=y CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_NET_NBUF_RX_COUNT=4 +CONFIG_NET_NBUF_RX_COUNT=5 -- cgit v1.2.3 From ce2703599f844b73f3eab10b96197a01bc01ccf2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 14 Feb 2017 17:01:26 +0300 Subject: zephyr: Add qemu_cortex_m3 config fragment. Should work for QEMU networking with soon-to-merged upstream patch. --- zephyr/prj_qemu_cortex_m3.conf | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 zephyr/prj_qemu_cortex_m3.conf (limited to 'zephyr') diff --git a/zephyr/prj_qemu_cortex_m3.conf b/zephyr/prj_qemu_cortex_m3.conf new file mode 100644 index 000000000..09614c362 --- /dev/null +++ b/zephyr/prj_qemu_cortex_m3.conf @@ -0,0 +1,3 @@ +# Networking drivers +# SLIP driver for QEMU +CONFIG_NET_SLIP_TAP=y -- cgit v1.2.3 From 453f98914e66b65f7335fb6d24269130c51777be Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 14 Feb 2017 23:30:55 +0300 Subject: zephyr/main: Don't unconditionally dump stats on each GC. This was a debug output for initial porting, breaks tests. --- zephyr/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index a11a6dbda..d6980ad29 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -126,7 +126,7 @@ void gc_collect(void) { gc_collect_start(); gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); gc_collect_end(); - gc_dump_info(); + //gc_dump_info(); } mp_lexer_t *mp_lexer_new_from_file(const char *filename) { -- cgit v1.2.3 From 3d739eb398e44c3151bb999fcb3427b5184b4f58 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 17 Feb 2017 22:06:52 +0300 Subject: zephyr/README: Network startup issues with frdm_k64f resolved. But leave a generic warning that users should be aware of Zephyr's limitations/issues for a board they use. --- zephyr/README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'zephyr') diff --git a/zephyr/README.md b/zephyr/README.md index 65729df5b..6e9e5d849 100644 --- a/zephyr/README.md +++ b/zephyr/README.md @@ -59,10 +59,12 @@ it's a sign that you didn't followed instructions above. If you would like to just run it quickly without extra setup, see "minimal" build below. For deploying/flashing a firmware on a real board, follow Zephyr -documentation for a given board (mind again that networking is enabled -for the build, so you should be aware of known issues for a particular -board; for example, frdm_k64f requires Ethernet cable connected to both -board and host or it will hang/crash on startup). +documentation for a given board, including known issues for that board +(if any). (Mind again that networking is enabled for the default build, +so you should know if there're any special requirements in that regard, +cf. for example QEMU networking requirements above; real hardware boards +generally should not have any special requirements, unless there're known +issues). Quick example -- cgit v1.2.3 From a64a0276b353cf13043d6892ac74d50c251719aa Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 8 Mar 2017 08:53:10 +0100 Subject: zephyr/modzephyr: Add a module for Zephyr-specific things. Mostly intended to ease experimentation, no particular plans for APIs so far (far less their stability), is_preempt_thread() provided is mostly an example. --- zephyr/Makefile | 1 + zephyr/modzephyr.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ zephyr/mpconfigport.h | 9 +++++++++ 3 files changed, 61 insertions(+) create mode 100644 zephyr/modzephyr.c (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index 3779765fb..d020ab5ce 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -42,6 +42,7 @@ INC += -I$(ZEPHYR_BASE)/net/ip/contiki/os SRC_C = main.c \ help.c \ modutime.c \ + modzephyr.c \ modmachine.c \ machine_pin.c \ uart_core.c \ diff --git a/zephyr/modzephyr.c b/zephyr/modzephyr.c new file mode 100644 index 000000000..1edfa7f3b --- /dev/null +++ b/zephyr/modzephyr.c @@ -0,0 +1,51 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Linaro Limited + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mpconfig.h" +#if MICROPY_PY_ZEPHYR + +#include + +#include "py/runtime.h" + +STATIC mp_obj_t mod_is_preempt_thread(void) { + return mp_obj_new_bool(k_is_preempt_thread()); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_is_preempt_thread_obj, mod_is_preempt_thread); + +STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zephy) }, + { MP_ROM_QSTR(MP_QSTR_is_preempt_thread), MP_ROM_PTR(&mod_is_preempt_thread_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table); + +const mp_obj_module_t mp_module_zephyr = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&mp_module_time_globals, +}; + +#endif // MICROPY_PY_ZEPHYR diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index 2cd5ffcec..e1e85df9c 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -60,6 +60,7 @@ #define MICROPY_PY_STRUCT (0) #define MICROPY_PY_UTIME (1) #define MICROPY_PY_UTIME_MP_HAL (1) +#define MICROPY_PY_ZEPHYR (1) #define MICROPY_PY_SYS_MODULES (0) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_LONGLONG) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) @@ -100,6 +101,7 @@ typedef long mp_off_t; extern const struct _mp_obj_module_t mp_module_machine; extern const struct _mp_obj_module_t mp_module_time; +extern const struct _mp_obj_module_t mp_module_zephyr; #if MICROPY_PY_UTIME #define MICROPY_PY_UTIME_DEF { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_time) }, @@ -107,9 +109,16 @@ extern const struct _mp_obj_module_t mp_module_time; #define MICROPY_PY_UTIME_DEF #endif +#if MICROPY_PY_ZEPHYR +#define MICROPY_PY_ZEPHYR_DEF { MP_ROM_QSTR(MP_QSTR_zephyr), MP_ROM_PTR(&mp_module_zephyr) }, +#else +#define MICROPY_PY_ZEPHYR_DEF +#endif + #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \ MICROPY_PY_UTIME_DEF \ + MICROPY_PY_ZEPHYR_DEF \ #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_module_time) }, \ -- cgit v1.2.3 From e8d4527996261f69e0d6898f963f0be9647cdbf4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 9 Mar 2017 10:17:30 +0100 Subject: zephyr/modzephyr: Fix typo in identifier. --- zephyr/modzephyr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/modzephyr.c b/zephyr/modzephyr.c index 1edfa7f3b..4bac5c970 100644 --- a/zephyr/modzephyr.c +++ b/zephyr/modzephyr.c @@ -37,7 +37,7 @@ STATIC mp_obj_t mod_is_preempt_thread(void) { STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_is_preempt_thread_obj, mod_is_preempt_thread); STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zephy) }, + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zephyr) }, { MP_ROM_QSTR(MP_QSTR_is_preempt_thread), MP_ROM_PTR(&mod_is_preempt_thread_obj) }, }; -- cgit v1.2.3 From 736a8a8ac7abfeab5edb8dfd73ecf0b2c0282e61 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 12 Mar 2017 22:28:45 +0300 Subject: zephyr: Make sure that generated prj.conf is updated only on content changes. This is a typical problem with make: we want to trigger rebuilds only if file actually changed, not if its timestamp changed. In this case, it's aggravated by the fact that prj.conf depends on the value of BOARD variable, so we need to do some tricks anyway. We still don't try to detect if just BOARD changed, just try to generate new prj.conf.tmp every time (quick), but do actual replacement of prj.conf only if its content changed. --- zephyr/Makefile | 3 +-- zephyr/makeprj.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 zephyr/makeprj.py (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index d020ab5ce..7d4d6674e 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -102,5 +102,4 @@ z_clean: .PHONY: prj.conf prj.conf: prj_base.conf - cat $< >$@ - if [ -f prj_$(BOARD).conf ]; then cat prj_$(BOARD).conf >>$@; fi + $(PYTHON) makeprj.py prj_base.conf prj_$(BOARD).conf $@ diff --git a/zephyr/makeprj.py b/zephyr/makeprj.py new file mode 100644 index 000000000..239c877cd --- /dev/null +++ b/zephyr/makeprj.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import sys +import os +import hashlib + + +def hash_file(fname): + if not os.path.exists(fname): + return b"" + hasher = hashlib.md5() + with open(fname, "rb") as f: + hasher.update(f.read()) + return hasher.digest() + + +old_digest = hash_file(sys.argv[3]) + +with open(sys.argv[3] + ".tmp", "wb") as f: + f.write(open(sys.argv[1], "rb").read()) + if os.path.exists(sys.argv[2]): + f.write(open(sys.argv[2], "rb").read()) + +new_digest = hash_file(sys.argv[3] + ".tmp") + +if new_digest != old_digest: + print("Replacing") + os.rename(sys.argv[3] + ".tmp", sys.argv[3]) +else: + os.remove(sys.argv[3] + ".tmp") -- cgit v1.2.3 From 40e541063faf15bd8965f2d65d5a5be08fdef851 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 12 Mar 2017 23:41:19 +0300 Subject: zephyr: Move "minimal" configuration building to a separate wrapper script. Minimal config can be now build with: ./make-minimal BOARD=... This is required because of Makefile.exports magic, which in its turn depends on PROJ_CONF to be set correctly at the beginning of Makefile parsing at all times. Instead of adding more and more workarounds for that, it's better to just move minimal support to a separate wrapper. Also, remove Zephyr 1.5 era cruft from Makefile, and add support for Zephyr's "run" target which supercedes older "qemu" target in upstream. --- zephyr/Makefile | 17 ++--------------- zephyr/README.md | 12 ++++++------ zephyr/make-minimal | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 21 deletions(-) create mode 100755 zephyr/make-minimal (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index 7d4d6674e..7a0d05298 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -5,17 +5,10 @@ # recursively Makefile.zephyr to build complete application binary # using Zephyr build system. # +# To build a "minimal" configuration, use "make-minimal" wrapper. BOARD ?= qemu_x86 -ifeq ($(MAKECMDGOALS), minimal) -# For minimal, CONF_FILE must be overriden early due to $(Z_EXPORTS) target -CONF_FILE = prj_minimal.conf -else CONF_FILE = prj.conf -endif -# Zephyr 1.5.0 -#OUTDIR_PREFIX = -# Zephyr 1.6.0 OUTDIR_PREFIX = $(BOARD) # Default heap size is 16KB, which is on conservative side, to let @@ -66,7 +59,7 @@ include ../py/mkrules.mk $(Z_EXPORTS): $(CONF_FILE) $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) initconfig outputexports -GENERIC_TARGETS = all zephyr qemu qemugdb flash debug +GENERIC_TARGETS = all zephyr run qemu qemugdb flash debug KCONFIG_TARGETS = \ initconfig config nconfig menuconfig xconfig gconfig \ oldconfig silentoldconfig defconfig savedefconfig \ @@ -88,12 +81,6 @@ $(Z_SYSGEN_H): rm -f $(LIBMICROPYTHON) -$(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) -minimal: - $(MAKE) BOARD=$(BOARD) CONF_FILE=prj_minimal.conf CFLAGS_EXTRA='-DMP_CONFIGFILE=""' FROZEN_DIR= - -qemu-minimal: - $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=prj_minimal.conf QEMU_NET=0 run - # Clean Zephyr things too clean: z_clean diff --git a/zephyr/README.md b/zephyr/README.md index 6e9e5d849..4fbf3d401 100644 --- a/zephyr/README.md +++ b/zephyr/README.md @@ -98,17 +98,17 @@ below 128KB, as long as Zephyr project is committed to maintain stable minimal size of their kernel (which they appear to be). Note that at such size, there is no support for any Zephyr features beyond REPL over UART, and only very minimal set of builtin Python modules. Thus, this build -is more suitable for code size control and quick demonstrations even on +is more suitable for code size control and quick demonstrations on smaller systems. It's also suitable for careful enabling of features one -by one to achieve needed functionality and code size. This is in contrast -to the "default" build, which may get more and more features enabled by -default over time. +by one to achieve needed functionality and code size. This is in a +contrast to the "default" build, which may get more and more features +enabled over time. To make a minimal build: - make BOARD= minimal + ./make-minimal BOARD= To run a minimal build in QEMU without requiring TAP networking setup run the following after you built image with the previous command: - make BOARD= qemu-minimal + ./make-minimal BOARD= qemu diff --git a/zephyr/make-minimal b/zephyr/make-minimal new file mode 100755 index 000000000..1fc143e4d --- /dev/null +++ b/zephyr/make-minimal @@ -0,0 +1,16 @@ +#!/bin/sh +# +# This is a wrapper for make to build a "minimal" Zephyr port. +# It should be run just like make (i.e. extra vars can be passed on the +# command line, etc.), e.g.: +# +# ./make-minimal BOARD=qemu_cortex_m3 +# ./make-minimal BOARD=qemu_cortex_m3 run +# + +make \ + CONF_FILE=prj_minimal.conf \ + CFLAGS_EXTRA='-DMP_CONFIGFILE=""' \ + FROZEN_DIR= \ + QEMU_NET=0 \ + "$@" -- cgit v1.2.3 From f5aac7d33fa04c1709123f8b5667476e0d32c1ff Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 13 Mar 2017 00:43:36 +0300 Subject: zephyr/main: nlr_jump_fail: Fix noreturn warning. --- zephyr/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index d6980ad29..61de6e168 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -142,7 +142,8 @@ mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) } MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); -void nlr_jump_fail(void *val) { +NORETURN void nlr_jump_fail(void *val) { + while (1); } void NORETURN __fatal_error(const char *msg) { -- cgit v1.2.3 From 9c9674a32599b8f9268fb479fc1cb3d62a5ec2f0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 14 Mar 2017 00:19:35 +0300 Subject: zephyr/main: Remove unused __fatal_error(). --- zephyr/main.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index 61de6e168..25bd88f76 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -146,10 +146,6 @@ NORETURN void nlr_jump_fail(void *val) { while (1); } -void NORETURN __fatal_error(const char *msg) { - while (1); -} - #ifndef NDEBUG void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); -- cgit v1.2.3 From 180045bce9282c4a658284e01348f57841e39cdd Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 14 Mar 2017 11:26:43 +1100 Subject: zephyr/main: Move lexer constructor to within NLR handler block. And raise an exception when mp_lexer_new_from_file is called. --- zephyr/main.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index 25bd88f76..1f8589a6c 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -44,14 +44,9 @@ #include "lib/mp-readline/readline.h" void do_str(const char *src, mp_parse_input_kind_t input_kind) { - mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); - if (lex == NULL) { - printf("MemoryError: lexer could not allocate memory\n"); - return; - } - nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { + mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); qstr source_name = lex->source_name; mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true); @@ -130,7 +125,7 @@ void gc_collect(void) { } mp_lexer_t *mp_lexer_new_from_file(const char *filename) { - return NULL; + mp_raise_OSError(ENOENT); } mp_import_stat_t mp_import_stat(const char *path) { -- cgit v1.2.3 From 99866a00a0172f3aba1e49c9c15f0afcf166a3e8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 26 Mar 2017 00:32:51 +0300 Subject: py/nlrx86: Better check for Zephyr (requires 1.7). --- py/nlrx86.c | 3 +-- zephyr/mpconfigport.h | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'zephyr') diff --git a/py/nlrx86.c b/py/nlrx86.c index 07ba72628..58aaa1a57 100644 --- a/py/nlrx86.c +++ b/py/nlrx86.c @@ -49,10 +49,9 @@ unsigned int nlr_push(nlr_buf_t *nlr) { __asm volatile ( // Check for Zephyr, which uses a different calling convention // by default. - // TODO: Better check for Zephyr. // TODE: Better support for various x86 calling conventions // (unfortunately, __attribute__((naked)) is not supported on x86). - #ifndef CONFIG_SOC_IA32 + #ifndef __ZEPHYR__ "pop %ebp \n" // undo function's prelude #endif "mov 4(%esp), %edx \n" // load nlr_buf diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index e1e85df9c..ff8a593a4 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -27,6 +27,8 @@ // Include Zephyr's autoconf.h, which should be made first by Zephyr makefiles #include "autoconf.h" +// Included here to get basic Zephyr environment (macros, etc.) +#include // Usually passed from Makefile #ifndef MICROPY_HEAP_SIZE -- cgit v1.2.3 From 220df8544b9ed1792fc35fd5b4782bc6f6910a18 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 27 Mar 2017 00:41:58 +0300 Subject: zephyr/zephyr_getchar: Use native k_sem instead of legacy nano_sem. --- zephyr/src/zephyr_getchar.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/src/zephyr_getchar.c b/zephyr/src/zephyr_getchar.c index 95912f3df..246d86a2e 100644 --- a/zephyr/src/zephyr_getchar.c +++ b/zephyr/src/zephyr_getchar.c @@ -23,7 +23,7 @@ extern int mp_interrupt_char; void mp_keyboard_interrupt(void); -static struct nano_sem uart_sem; +static struct k_sem uart_sem; #define UART_BUFSIZE 256 static uint8_t uart_ringbuf[UART_BUFSIZE]; static uint8_t i_get, i_put; -- cgit v1.2.3 From dbb2aea428fe2f9855784fc2fd04f7817b81360d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 27 Mar 2017 17:06:20 +0300 Subject: zephyr/prj_base.conf: Disable legacy kernel compatibility. This keeps dependency on mdef, sysgen and other stuff which complicates build integration. --- zephyr/prj_base.conf | 2 ++ 1 file changed, 2 insertions(+) (limited to 'zephyr') diff --git a/zephyr/prj_base.conf b/zephyr/prj_base.conf index 295963023..b0f7ae0d8 100644 --- a/zephyr/prj_base.conf +++ b/zephyr/prj_base.conf @@ -1,3 +1,5 @@ +CONFIG_LEGACY_KERNEL=n + CONFIG_STDOUT_CONSOLE=y CONFIG_CONSOLE_HANDLER=y CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS=y -- cgit v1.2.3 From e93c4ca18101e97ab5d427b97d4e0b6a72e31deb Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 29 Mar 2017 00:46:06 +0300 Subject: zephyr/prj_base.conf: Enable TCP (and UDP explicitly). --- zephyr/prj_base.conf | 2 ++ 1 file changed, 2 insertions(+) (limited to 'zephyr') diff --git a/zephyr/prj_base.conf b/zephyr/prj_base.conf index b0f7ae0d8..9a12241bc 100644 --- a/zephyr/prj_base.conf +++ b/zephyr/prj_base.conf @@ -11,5 +11,7 @@ CONFIG_MAIN_STACK_SIZE=4096 CONFIG_NETWORKING=y CONFIG_NET_IPV4=y CONFIG_NET_IPV6=y +CONFIG_NET_UDP=y +CONFIG_NET_TCP=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_NET_NBUF_RX_COUNT=5 -- cgit v1.2.3 From 29dd92c82a6ea07f8d4f71c5c6d18c45ccefde39 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 30 Mar 2017 00:08:27 +0300 Subject: zephyr/Makefile: Rework to use modern, official build integration. Build happens in 3 stages: 1. Zephyr config header and make vars are generated from prj.conf. 2. libmicropython is built using them. 3. Zephyr is built and final link happens. --- py/mkrules.mk | 7 +++++++ zephyr/Makefile | 11 ++++------- 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'zephyr') diff --git a/py/mkrules.mk b/py/mkrules.mk index bc6389144..2ba57e0ee 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -142,8 +142,15 @@ clean-prog: endif LIBMICROPYTHON = libmicropython.a + +# We can execute extra commands after library creation using +# LIBMICROPYTHON_EXTRA_CMD. This may be needed e.g. to integrate +# with 3rd-party projects which don't have proper dependency +# tracking. Then LIBMICROPYTHON_EXTRA_CMD can e.g. touch some +# other file to cause needed effect, e.g. relinking with new lib. lib $(LIBMICROPYTHON): $(OBJ) $(AR) rcs $(LIBMICROPYTHON) $^ + $(LIBMICROPYTHON_EXTRA_CMD) clean: $(RM) -rf $(BUILD) $(CLEAN_EXTRA) diff --git a/zephyr/Makefile b/zephyr/Makefile index 7a0d05298..c6d6eb325 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -18,7 +18,6 @@ MICROPY_HEAP_SIZE = 16384 FROZEN_DIR = scripts # Zephyr (generated) config files - must be defined before include below -Z_SYSGEN_H = outdir/$(OUTDIR_PREFIX)/misc/generated/sysgen/sysgen.h Z_EXPORTS = outdir/$(OUTDIR_PREFIX)/Makefile.export include $(Z_EXPORTS) @@ -71,15 +70,13 @@ $(GENERIC_TARGETS): $(LIBMICROPYTHON) $(CLEAN_TARGETS): clean $(GENERIC_TARGETS) $(KCONFIG_TARGETS) $(CLEAN_TARGETS): - $(RM) -f outdir/$(OUTDIR_PREFIX)/zephyr.lnk $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) $@ -$(LIBMICROPYTHON): $(Z_SYSGEN_H) -build/genhdr/qstr.i.last: $(Z_SYSGEN_H) +$(LIBMICROPYTHON): | $(Z_EXPORTS) +build/genhdr/qstr.i.last: | $(Z_EXPORTS) -$(Z_SYSGEN_H): - rm -f $(LIBMICROPYTHON) - -$(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) +# If we recreate libmicropython, also cause zephyr.bin relink +LIBMICROPYTHON_EXTRA_CMD = -$(RM) -f outdir/$(OUTDIR_PREFIX)/zephyr.lnk # Clean Zephyr things too clean: z_clean -- cgit v1.2.3 From df48f4645d603db99c1c9df6f72052866ba032b6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 30 Mar 2017 15:15:39 +0300 Subject: zephyr/Makefile: Add workaround (fix?) for broken builds for DTS targets. --- zephyr/Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index c6d6eb325..c3781322a 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -56,7 +56,12 @@ CFLAGS = $(KBUILD_CFLAGS) $(NOSTDINC_FLAGS) $(ZEPHYRINCLUDE) \ include ../py/mkrules.mk $(Z_EXPORTS): $(CONF_FILE) - $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) initconfig outputexports + # We use single target here ($(Z_EXPORTS)) for simplicity, but actually + # number of things get generated here: 'initconfig' generates C header for + # Kconfig configuration, 'outputexports' generates make environment with CC, + # etc., and 'lib' generates other headers which may be included by zephyr.h, + # e.g. DTS-related. + $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) initconfig outputexports lib GENERIC_TARGETS = all zephyr run qemu qemugdb flash debug KCONFIG_TARGETS = \ -- cgit v1.2.3 From 344bfbb71c0cc12a69d06958cfcd583a14a1a360 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 30 Mar 2017 22:17:18 +0300 Subject: zephyr: Fix NLR segfault in minimal build. Requires inclusion of zephyr.h to properly detect that we're building for Zephyr. --- zephyr/mpconfigport_minimal.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'zephyr') diff --git a/zephyr/mpconfigport_minimal.h b/zephyr/mpconfigport_minimal.h index 459b0816c..04383f6e3 100644 --- a/zephyr/mpconfigport_minimal.h +++ b/zephyr/mpconfigport_minimal.h @@ -27,6 +27,8 @@ // Include Zephyr's autoconf.h, which should be made first by Zephyr makefiles #include "autoconf.h" +// Included here to get basic Zephyr environment (macros, etc.) +#include // Usually passed from Makefile #ifndef MICROPY_HEAP_SIZE -- cgit v1.2.3 From d1015f0e0d8bcb297017555f8bd12486704ae4fc Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 31 Mar 2017 00:04:31 +0300 Subject: zephyr/modusocket: Initial version of usocket module for Zephyr. So far, socket creation and closure is implemented. --- zephyr/modusocket.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 zephyr/modusocket.c (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c new file mode 100644 index 000000000..0ce19c41e --- /dev/null +++ b/zephyr/modusocket.c @@ -0,0 +1,135 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Linaro Limited + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/mpconfig.h" +#ifdef MICROPY_PY_USOCKET + +#include "py/runtime.h" + +#include +#include +#include + +typedef struct _socket_obj_t { + mp_obj_base_t base; + struct net_context *ctx; +} socket_obj_t; + +STATIC const mp_obj_type_t socket_type; + +// Helper functions + +#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); } + +// Methods + +STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + socket_obj_t *self = self_in; + if (self->ctx == NULL) { + mp_printf(print, ""); + } else { + struct net_context *ctx = self->ctx; + mp_printf(print, "", ctx, net_context_get_type(ctx)); + } +} + +STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 4, false); + + socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); + socket->base.type = type; + + int family = AF_INET; + int socktype = SOCK_STREAM; + int proto = -1; + + if (n_args >= 1) { + family = mp_obj_get_int(args[0]); + if (n_args >= 2) { + socktype = mp_obj_get_int(args[1]); + if (n_args >= 3) { + proto = mp_obj_get_int(args[2]); + } + } + } + + if (proto == -1) { + proto = IPPROTO_TCP; + if (socktype != SOCK_STREAM) { + proto = IPPROTO_UDP; + } + } + + RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx)); + + return socket; +} + +STATIC mp_obj_t socket_close(mp_obj_t self_in) { + socket_obj_t *socket = self_in; + if (socket->ctx != NULL) { + RAISE_ERRNO(net_context_put(socket->ctx)); + socket->ctx = NULL; + } + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close); + +STATIC const mp_map_elem_t socket_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj }, +}; +STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); + +STATIC const mp_obj_type_t socket_type = { + { &mp_type_type }, + .name = MP_QSTR_socket, + .print = socket_print, + .make_new = socket_make_new, + //.protocol = &socket_stream_p, + .locals_dict = (mp_obj_t)&socket_locals_dict, +}; + +STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) }, + // objects + { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&socket_type }, + // class constants + { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET), MP_OBJ_NEW_SMALL_INT(AF_INET) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_AF_INET6), MP_OBJ_NEW_SMALL_INT(AF_INET6) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SOCK_STREAM) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SOCK_DGRAM) }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table); + +const mp_obj_module_t mp_module_usocket = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&mp_module_usocket_globals, +}; + +#endif // MICROPY_PY_USOCKET -- cgit v1.2.3 From 6cc1a7a214e573cf3efc3c53f3ecba6c65ea2d11 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 31 Mar 2017 00:06:31 +0300 Subject: zephyr: Integrate modusocket into build. --- zephyr/Makefile | 1 + zephyr/mpconfigport.h | 8 ++++++++ 2 files changed, 9 insertions(+) (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index c3781322a..b78b4be95 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -33,6 +33,7 @@ INC += -I$(ZEPHYR_BASE)/net/ip/contiki/os SRC_C = main.c \ help.c \ + modusocket.c \ modutime.c \ modzephyr.c \ modmachine.c \ diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index ff8a593a4..e0586dcfc 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -60,6 +60,11 @@ #define MICROPY_PY_MACHINE (1) #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_PY_STRUCT (0) +#ifdef CONFIG_NETWORKING +// If we have networking, we likely want errno comfort +#define MICROPY_PY_UERRNO (1) +#define MICROPY_PY_USOCKET (1) +#endif #define MICROPY_PY_UTIME (1) #define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_PY_ZEPHYR (1) @@ -103,6 +108,7 @@ typedef long mp_off_t; extern const struct _mp_obj_module_t mp_module_machine; extern const struct _mp_obj_module_t mp_module_time; +extern const struct _mp_obj_module_t mp_module_usocket; extern const struct _mp_obj_module_t mp_module_zephyr; #if MICROPY_PY_UTIME @@ -119,11 +125,13 @@ extern const struct _mp_obj_module_t mp_module_zephyr; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \ MICROPY_PY_UTIME_DEF \ MICROPY_PY_ZEPHYR_DEF \ #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_module_time) }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_module_usocket) }, \ // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ -- cgit v1.2.3 From 88582e33ccbf6b5d5a2f6a9a592d35139688efa4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 31 Mar 2017 09:10:30 +0300 Subject: zephyr/modusocket: Implement bind() and connect(). --- zephyr/modusocket.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 0ce19c41e..109b8943c 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -44,6 +44,24 @@ STATIC const mp_obj_type_t socket_type; #define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); } +STATIC void socket_check_closed(socket_obj_t *socket) { + if (socket->ctx == NULL) { + // already closed + mp_raise_OSError(EBADF); + } +} + +STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct sockaddr *sockaddr) { + // We employ the fact that port and address offsets are the same for IPv4 & IPv6 + struct sockaddr_in *sockaddr_in = (struct sockaddr_in*)sockaddr; + + mp_obj_t *addr_items; + mp_obj_get_array_fixed_n(addr_in, 2, &addr_items); + sockaddr_in->sin_family = net_context_get_family(socket->ctx); + RAISE_ERRNO(net_addr_pton(sockaddr_in->sin_family, mp_obj_str_get_str(addr_items[0]), &sockaddr_in->sin_addr)); + sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1])); +} + // Methods STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { @@ -88,6 +106,30 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t return socket; } +STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) { + socket_obj_t *socket = self_in; + socket_check_closed(socket); + + struct sockaddr sockaddr; + parse_inet_addr(socket, addr_in, &sockaddr); + + RAISE_ERRNO(net_context_bind(socket->ctx, &sockaddr, sizeof(sockaddr))); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind); + +STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { + socket_obj_t *socket = self_in; + socket_check_closed(socket); + + struct sockaddr sockaddr; + parse_inet_addr(socket, addr_in, &sockaddr); + + RAISE_ERRNO(net_context_connect(socket->ctx, &sockaddr, sizeof(sockaddr), NULL, K_FOREVER, NULL)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect); + STATIC mp_obj_t socket_close(mp_obj_t self_in) { socket_obj_t *socket = self_in; if (socket->ctx != NULL) { @@ -101,6 +143,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close); STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___del__), (mp_obj_t)&socket_close_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj }, }; STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); -- cgit v1.2.3 From 64d00511e11eed6e37134e5b43acac8a33226ca0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 31 Mar 2017 23:13:12 +0300 Subject: zephyr/modusocket: Implement send(). --- zephyr/modusocket.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 109b8943c..ea084368c 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -130,6 +130,25 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect); +STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { + socket_obj_t *socket = self_in; + socket_check_closed(socket); + + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); + + struct net_buf *send_buf = net_nbuf_get_tx(socket->ctx, K_FOREVER); + // TODO: Probably should limit how much data we send in one call still + if (!net_nbuf_append(send_buf, bufinfo.len, bufinfo.buf, K_FOREVER)) { + mp_raise_OSError(ENOSPC); + } + + RAISE_ERRNO(net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL)); + + return mp_obj_new_int_from_uint(bufinfo.len); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send); + STATIC mp_obj_t socket_close(mp_obj_t self_in) { socket_obj_t *socket = self_in; if (socket->ctx != NULL) { @@ -145,6 +164,7 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj }, }; STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); -- cgit v1.2.3 From 4c307bfba1e5ffa3cec94bd63ba19ba6c322883b Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 1 Apr 2017 11:39:38 +1100 Subject: all: Move BYTES_PER_WORD definition from ports to py/mpconfig.h It can still be overwritten by a port in mpconfigport.h but for almost all cases one can use the provided default. --- bare-arm/mpconfigport.h | 2 -- cc3200/mpconfigport.h | 1 - esp8266/mpconfigport.h | 2 -- examples/embedding/mpconfigport_minimal.h | 2 -- minimal/mpconfigport.h | 2 -- mpy-cross/mpconfigport.h | 2 -- pic16bit/mpconfigport.h | 1 - py/mpconfig.h | 5 +++++ qemu-arm/mpconfigport.h | 2 -- stmhal/mpconfigport.h | 2 -- teensy/mpconfigport.h | 2 -- unix/mpconfigport.h | 2 -- unix/mpconfigport_minimal.h | 2 -- windows/mpconfigport.h | 2 -- zephyr/mpconfigport.h | 2 -- zephyr/mpconfigport_minimal.h | 2 -- 16 files changed, 5 insertions(+), 28 deletions(-) (limited to 'zephyr') diff --git a/bare-arm/mpconfigport.h b/bare-arm/mpconfigport.h index 912fae66f..97e866bdb 100644 --- a/bare-arm/mpconfigport.h +++ b/bare-arm/mpconfigport.h @@ -46,8 +46,6 @@ // type definitions for the specific machine -#define BYTES_PER_WORD (4) - #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1)) #define UINT_FMT "%lu" diff --git a/cc3200/mpconfigport.h b/cc3200/mpconfigport.h index a8dc0cfeb..4d19900ae 100644 --- a/cc3200/mpconfigport.h +++ b/cc3200/mpconfigport.h @@ -200,7 +200,6 @@ extern const struct _mp_obj_module_t mp_module_ussl; // type definitions for the specific machine -#define BYTES_PER_WORD (4) #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1)) #define MP_SSIZE_MAX (0x7FFFFFFF) diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index 07bd48f67..d40eaba91 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -118,8 +118,6 @@ // type definitions for the specific machine -#define BYTES_PER_WORD (4) - #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p))) #define MP_SSIZE_MAX (0x7fffffff) diff --git a/examples/embedding/mpconfigport_minimal.h b/examples/embedding/mpconfigport_minimal.h index 48d200858..d16827fa3 100644 --- a/examples/embedding/mpconfigport_minimal.h +++ b/examples/embedding/mpconfigport_minimal.h @@ -119,8 +119,6 @@ typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size #endif -#define BYTES_PER_WORD sizeof(mp_int_t) - // Cannot include , as it may lead to symbol name clashes #if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) typedef long long mp_off_t; diff --git a/minimal/mpconfigport.h b/minimal/mpconfigport.h index e08943860..47fc98429 100644 --- a/minimal/mpconfigport.h +++ b/minimal/mpconfigport.h @@ -57,8 +57,6 @@ // type definitions for the specific machine -#define BYTES_PER_WORD (4) - #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1)) // This port is intended to be 32-bit, but unfortunately, int32_t for diff --git a/mpy-cross/mpconfigport.h b/mpy-cross/mpconfigport.h index 0f7651098..383de1439 100644 --- a/mpy-cross/mpconfigport.h +++ b/mpy-cross/mpconfigport.h @@ -100,8 +100,6 @@ typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size #endif -#define BYTES_PER_WORD sizeof(mp_int_t) - // Cannot include , as it may lead to symbol name clashes #if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) typedef long long mp_off_t; diff --git a/pic16bit/mpconfigport.h b/pic16bit/mpconfigport.h index a0371a8ae..e4113956b 100644 --- a/pic16bit/mpconfigport.h +++ b/pic16bit/mpconfigport.h @@ -68,7 +68,6 @@ // type definitions for the specific machine #define MP_ENDIANNESS_LITTLE (1) -#define BYTES_PER_WORD (2) #define MPZ_DIG_SIZE (8) // The xc16 compiler doesn't seem to respect alignment (!!) so we diff --git a/py/mpconfig.h b/py/mpconfig.h index 54cf0f3d3..9c89fc18a 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -1105,6 +1105,11 @@ typedef double mp_float_t; #define STATIC static #endif +// Number of bytes in a word +#ifndef BYTES_PER_WORD +#define BYTES_PER_WORD (sizeof(mp_uint_t)) +#endif + #define BITS_PER_BYTE (8) #define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD) // mp_int_t value with most significant bit set diff --git a/qemu-arm/mpconfigport.h b/qemu-arm/mpconfigport.h index f642c6428..974d3520b 100644 --- a/qemu-arm/mpconfigport.h +++ b/qemu-arm/mpconfigport.h @@ -39,8 +39,6 @@ // type definitions for the specific machine -#define BYTES_PER_WORD (4) - #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1)) #define MP_SSIZE_MAX (0x7fffffff) diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 339360bae..3f3683199 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -264,8 +264,6 @@ extern const struct _mp_obj_module_t mp_module_network; // type definitions for the specific machine -#define BYTES_PER_WORD (4) - #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1)) #define MP_SSIZE_MAX (0x7fffffff) diff --git a/teensy/mpconfigport.h b/teensy/mpconfigport.h index 07f60c43f..8c4022046 100644 --- a/teensy/mpconfigport.h +++ b/teensy/mpconfigport.h @@ -54,8 +54,6 @@ extern const struct _mp_obj_module_t time_module; // type definitions for the specific machine -#define BYTES_PER_WORD (4) - #define UINT_FMT "%u" #define INT_FMT "%d" diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index a85884594..7ecfc28c0 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -233,8 +233,6 @@ typedef unsigned int mp_uint_t; // must be pointer size #endif #endif -#define BYTES_PER_WORD sizeof(mp_int_t) - // Cannot include , as it may lead to symbol name clashes #if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) typedef long long mp_off_t; diff --git a/unix/mpconfigport_minimal.h b/unix/mpconfigport_minimal.h index 65e35c8d9..788c8519d 100644 --- a/unix/mpconfigport_minimal.h +++ b/unix/mpconfigport_minimal.h @@ -127,8 +127,6 @@ typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size #endif -#define BYTES_PER_WORD sizeof(mp_int_t) - // Cannot include , as it may lead to symbol name clashes #if _FILE_OFFSET_BITS == 64 && !defined(__LP64__) typedef long long mp_off_t; diff --git a/windows/mpconfigport.h b/windows/mpconfigport.h index 6156ad39e..844e2618f 100644 --- a/windows/mpconfigport.h +++ b/windows/mpconfigport.h @@ -128,8 +128,6 @@ typedef int mp_int_t; // must be pointer size typedef unsigned int mp_uint_t; // must be pointer size #endif -#define BYTES_PER_WORD sizeof(mp_int_t) - // Just assume Windows is little-endian - mingw32 gcc doesn't // define standard endianness macros. #define MP_ENDIANNESS_LITTLE (1) diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index e0586dcfc..0949d1374 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -99,8 +99,6 @@ typedef void *machine_ptr_t; // must be of pointer size typedef const void *machine_const_ptr_t; // must be of pointer size typedef long mp_off_t; -#define BYTES_PER_WORD (sizeof(mp_int_t)) - #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ diff --git a/zephyr/mpconfigport_minimal.h b/zephyr/mpconfigport_minimal.h index 04383f6e3..772335c0a 100644 --- a/zephyr/mpconfigport_minimal.h +++ b/zephyr/mpconfigport_minimal.h @@ -85,8 +85,6 @@ typedef void *machine_ptr_t; // must be of pointer size typedef const void *machine_const_ptr_t; // must be of pointer size typedef long mp_off_t; -#define BYTES_PER_WORD (sizeof(mp_int_t)) - #define MP_STATE_PORT MP_STATE_VM #define MICROPY_PORT_ROOT_POINTERS \ -- cgit v1.2.3 From 3df65e9bae3e3c5298a2691e177335bb5efeb5ed Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 1 Apr 2017 09:20:07 +0300 Subject: zephyr/modusocket: Implement recv() for UDP sockets. The foundation of recv() support is per-socket queue of incoming packets, implemented using Zephyr FIFO object. This patch implements just recv() for UDP, because TCP recv() requires much more fine-grained control of network fragments and handling other issues, like EOF condition, etc. --- zephyr/modusocket.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index ea084368c..68a66aa26 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -29,13 +29,21 @@ #include "py/runtime.h" +#include #include #include #include +#if 0 // print debugging info +#define DEBUG_printf printf +#else // don't print debugging info +#define DEBUG_printf(...) (void)0 +#endif + typedef struct _socket_obj_t { mp_obj_base_t base; struct net_context *ctx; + struct k_fifo recv_q; } socket_obj_t; STATIC const mp_obj_type_t socket_type; @@ -62,6 +70,44 @@ STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct socka sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1])); } +// Copy data from Zephyr net_buf chain into linear buffer. +// We don't use net_nbuf_read(), because it's weird (e.g., we'd like to +// free processed data fragment ASAP, while net_nbuf_read() holds onto +// the whole fragment chain to do its deeds, and that's minor comparing +// to the fact that it copies data byte by byte). +static char *net_buf_gather(struct net_buf *buf, char *to, unsigned max_len) { + struct net_buf *tmp = buf->frags; + unsigned header_len = net_nbuf_appdata(buf) - tmp->data; + net_buf_pull(tmp, header_len); + + while (tmp && max_len) { + unsigned len = tmp->len; + if (len > max_len) { + len = max_len; + } + memcpy(to, tmp->data, len); + to += len; + max_len -= len; + tmp = net_buf_frag_del(buf, tmp); + } + + return to; +} + +// Callback for incoming packets. +static void sock_received_cb(struct net_context *context, struct net_buf *net_buf, int status, void *user_data) { + socket_obj_t *socket = (socket_obj_t*)user_data; + DEBUG_printf("recv cb: context: %p, status: %d, buf: %p", context, status, net_buf); + if (net_buf) { + DEBUG_printf(" (sz=%d, l=%d), token: %p", net_buf->size, net_buf->len, net_nbuf_token(net_buf)); + } + DEBUG_printf("\n"); + + // net_buf->frags will be overwritten by fifo, so save it + net_nbuf_set_token(net_buf, net_buf->frags); + k_fifo_put(&socket->recv_q, net_buf); +} + // Methods STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { @@ -79,6 +125,7 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); socket->base.type = type; + k_fifo_init(&socket->recv_q); int family = AF_INET; int socktype = SOCK_STREAM; @@ -114,6 +161,8 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) { parse_inet_addr(socket, addr_in, &sockaddr); RAISE_ERRNO(net_context_bind(socket->ctx, &sockaddr, sizeof(sockaddr))); + DEBUG_printf("Setting recv cb after bind\n"); + RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind); @@ -126,6 +175,8 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { parse_inet_addr(socket, addr_in, &sockaddr); RAISE_ERRNO(net_context_connect(socket->ctx, &sockaddr, sizeof(sockaddr), NULL, K_FOREVER, NULL)); + DEBUG_printf("Setting recv cb after connect()\n"); + RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect); @@ -149,6 +200,41 @@ STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send); +STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { + socket_obj_t *socket = self_in; + socket_check_closed(socket); + + enum net_sock_type sock_type = net_context_get_type(socket->ctx); + mp_int_t max_len = mp_obj_get_int(len_in); + unsigned recv_len; + vstr_t vstr; + + if (sock_type == SOCK_DGRAM) { + + struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER); + // Restore ->frags overwritten by fifo + net_buf->frags = net_nbuf_token(net_buf); + + recv_len = net_nbuf_appdatalen(net_buf); + DEBUG_printf("recv: net_buf=%p, appdatalen: %d\n", net_buf, recv_len); + + if (recv_len > max_len) { + recv_len = max_len; + } + + vstr_init_len(&vstr, recv_len); + net_buf_gather(net_buf, vstr.buf, recv_len); + net_nbuf_unref(net_buf); + + } else { + mp_not_implemented(""); + } + + mp_obj_t ret = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); + return ret; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv); + STATIC mp_obj_t socket_close(mp_obj_t self_in) { socket_obj_t *socket = self_in; if (socket->ctx != NULL) { @@ -165,6 +251,7 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj }, }; STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); -- cgit v1.2.3 From ca81c3ab0b1373928eb8a867077657eead0ed73d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 2 Apr 2017 16:27:09 +0300 Subject: zephyr/modusocket: Implement recv() for TCP sockets. Short read approach is taken - at most, the remaining data in the current fragment will be returned. --- zephyr/modusocket.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 68a66aa26..45991d42e 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -44,6 +44,13 @@ typedef struct _socket_obj_t { mp_obj_base_t base; struct net_context *ctx; struct k_fifo recv_q; + struct net_buf *cur_buf; + + #define STATE_NEW 0 + #define STATE_CONNECTING 1 + #define STATE_CONNECTED 2 + #define STATE_PEER_CLOSED 3 + int8_t state; } socket_obj_t; STATIC const mp_obj_type_t socket_type; @@ -103,6 +110,19 @@ static void sock_received_cb(struct net_context *context, struct net_buf *net_bu } DEBUG_printf("\n"); + // if net_buf == NULL, EOF + if (net_buf == NULL) { + // TODO: k_fifo accessor for this? + struct net_buf *last_buf = (struct net_buf*)sys_slist_peek_tail(&socket->recv_q.data_q); + // We abuse "buf_sent" flag to store EOF flag + net_nbuf_set_buf_sent(last_buf, true); + DEBUG_printf("Set EOF flag on %p\n", last_buf); + return; + } + + // Make sure that "EOF flag" is not set + net_nbuf_set_buf_sent(net_buf, false); + // net_buf->frags will be overwritten by fifo, so save it net_nbuf_set_token(net_buf, net_buf->frags); k_fifo_put(&socket->recv_q, net_buf); @@ -126,6 +146,8 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); socket->base.type = type; k_fifo_init(&socket->recv_q); + socket->cur_buf = NULL; + socket->state = STATE_NEW; int family = AF_INET; int socktype = SOCK_STREAM; @@ -226,6 +248,61 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { net_buf_gather(net_buf, vstr.buf, recv_len); net_nbuf_unref(net_buf); + } else if (sock_type == SOCK_STREAM) { + + do { + if (socket->state == STATE_PEER_CLOSED) { + return mp_const_empty_bytes; + } + + unsigned header_len = 0; + if (socket->cur_buf == NULL) { + DEBUG_printf("TCP recv: no cur_buf, getting\n"); + struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER); + // Restore ->frags overwritten by fifo + net_buf->frags = net_nbuf_token(net_buf); + + header_len = net_nbuf_appdata(net_buf) - net_buf->frags->data; + DEBUG_printf("TCP recv: new cur_buf: %p, hdr_len: %u\n", net_buf, header_len); + socket->cur_buf = net_buf; + } + + struct net_buf *frag = socket->cur_buf->frags; + if (frag == NULL) { + printf("net_buf has empty fragments on start!\n"); + assert(0); + } + + net_buf_pull(frag, header_len); + unsigned frag_len = frag->len; + recv_len = frag_len; + if (recv_len > max_len) { + recv_len = max_len; + } + DEBUG_printf("%d data bytes in head frag, going to read %d\n", frag_len, recv_len); + + vstr_init_len(&vstr, recv_len); + memcpy(vstr.buf, frag->data, recv_len); + + if (recv_len != frag_len) { + net_buf_pull(frag, recv_len); + } else { + frag = net_buf_frag_del(socket->cur_buf, frag); + if (frag == NULL) { + DEBUG_printf("Finished processing net_buf %p\n", socket->cur_buf); + // If "buf_sent" flag was set, it's last packet and we reached EOF + if (net_nbuf_buf_sent(socket->cur_buf)) { + socket->state = STATE_PEER_CLOSED; + } + net_nbuf_unref(socket->cur_buf); + socket->cur_buf = NULL; + } + } + // Keep repeating while we're getting empty fragments + // Zephyr IP stack appears to feed empty net_buf's with empty + // frags for various TCP control packets. + } while (recv_len == 0); + } else { mp_not_implemented(""); } -- cgit v1.2.3 From 4c392243ae947545553904d6436e7db462207446 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 3 Apr 2017 12:07:56 +0300 Subject: zephyr/prj_base.conf: Add config for net_buf logging. Disabled by default. --- zephyr/prj_base.conf | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'zephyr') diff --git a/zephyr/prj_base.conf b/zephyr/prj_base.conf index 9a12241bc..fc51ccec6 100644 --- a/zephyr/prj_base.conf +++ b/zephyr/prj_base.conf @@ -15,3 +15,9 @@ CONFIG_NET_UDP=y CONFIG_NET_TCP=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_NET_NBUF_RX_COUNT=5 + +# Uncomment to enable "INFO" level net_buf logging +#CONFIG_NET_LOG=y +#CONFIG_NET_DEBUG_NET_BUF=y +# Change to 4 for "DEBUG" level +#CONFIG_SYS_LOG_NET_LEVEL=3 -- cgit v1.2.3 From 6e99a8c94e1c217f33e7116d60817ba1028d395e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 4 Apr 2017 06:19:23 +0300 Subject: zephyr/modusocket: Be sure to use MP_OBJ_FROM_PTR. --- zephyr/modusocket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 45991d42e..025cb0c92 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -172,7 +172,7 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx)); - return socket; + return MP_OBJ_FROM_PTR(socket); } STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) { -- cgit v1.2.3 From 2908c3ca4168f963958e4a97e54800667ff34ccf Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 4 Apr 2017 06:28:14 +0300 Subject: zephyr/modusocket: Factor out socket_new() function. It will be reused e.g. for accept() implementation. --- zephyr/modusocket.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 025cb0c92..155ed69b8 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -128,6 +128,15 @@ static void sock_received_cb(struct net_context *context, struct net_buf *net_bu k_fifo_put(&socket->recv_q, net_buf); } +socket_obj_t *socket_new(void) { + socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); + socket->base.type = (mp_obj_t)&socket_type; + k_fifo_init(&socket->recv_q); + socket->cur_buf = NULL; + socket->state = STATE_NEW; + return socket; +} + // Methods STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { @@ -143,11 +152,7 @@ STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kin STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 0, 4, false); - socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); - socket->base.type = type; - k_fifo_init(&socket->recv_q); - socket->cur_buf = NULL; - socket->state = STATE_NEW; + socket_obj_t *socket = socket_new(); int family = AF_INET; int socktype = SOCK_STREAM; -- cgit v1.2.3 From 58168c8e6bcf3619d281a8c877ef20e5efe049d2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 4 Apr 2017 15:36:03 +0300 Subject: zephyr/zephyr_getchar: Explicitly yield to other threads on char availability. Without this, if there's a large chunk of data coming from hardware (e.g. clipboard paste, or fed programmatically from the other side of the console), there's a behavior of initial mass fill-in of the buffer without any consumption, which starts much later and doesn't catch up with further filling, leading to buffer overflow. --- zephyr/src/zephyr_getchar.c | 1 + 1 file changed, 1 insertion(+) (limited to 'zephyr') diff --git a/zephyr/src/zephyr_getchar.c b/zephyr/src/zephyr_getchar.c index 246d86a2e..52b3394d0 100644 --- a/zephyr/src/zephyr_getchar.c +++ b/zephyr/src/zephyr_getchar.c @@ -44,6 +44,7 @@ static int console_irq_input_hook(uint8_t ch) } //printk("%x\n", ch); k_sem_give(&uart_sem); + k_yield(); return 1; } -- cgit v1.2.3 From a9e6f08adb6dbe84ee59e9545895aa9dfc5ade17 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Apr 2017 00:37:35 +0300 Subject: zephyr/Makefile: Add "test" target, runs testsuite in QEMU. --- zephyr/Makefile | 3 +++ 1 file changed, 3 insertions(+) (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index b78b4be95..057a88862 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -93,3 +93,6 @@ z_clean: .PHONY: prj.conf prj.conf: prj_base.conf $(PYTHON) makeprj.py prj_base.conf prj_$(BOARD).conf $@ + +test: + cd ../tests && ./run-tests --target minimal --device "execpty:make -C ../zephyr run BOARD=$(BOARD) QEMU_PTY=1" -- cgit v1.2.3 From faf333c04f3dddb9ca1a6b7fc5ff74eabcfe51b0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 5 Apr 2017 13:34:37 +0300 Subject: zephyr/modusocket: Factor out "extended k_fifo API". Internal structure of k_fifo changed between 1.7 and 1.8, so we need to abstract it away. This adds more functions than currently used, for future work. --- zephyr/modusocket.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 155ed69b8..0bcbbce60 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -31,6 +31,8 @@ #include #include +// Zephyr's generated version header +#include #include #include @@ -55,6 +57,36 @@ typedef struct _socket_obj_t { STATIC const mp_obj_type_t socket_type; +// k_fifo extended API + +static inline void *_k_fifo_peek_head(struct k_fifo *fifo) +{ +#if KERNEL_VERSION_NUMBER < 0x010763 /* 1.7.99 */ + return sys_slist_peek_head(&fifo->data_q); +#else + return sys_slist_peek_head(&fifo->_queue.data_q); +#endif +} + +static inline void *_k_fifo_peek_tail(struct k_fifo *fifo) +{ +#if KERNEL_VERSION_NUMBER < 0x010763 /* 1.7.99 */ + return sys_slist_peek_tail(&fifo->data_q); +#else + return sys_slist_peek_tail(&fifo->_queue.data_q); +#endif +} + +static inline void _k_fifo_wait_non_empty(struct k_fifo *fifo, int32_t timeout) +{ + struct k_poll_event events[] = { + K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE, K_POLL_MODE_NOTIFY_ONLY, fifo), + }; + + k_poll(events, MP_ARRAY_SIZE(events), timeout); + DEBUG_printf("poll res: %d\n", events[0].state); +} + // Helper functions #define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); } @@ -112,8 +144,7 @@ static void sock_received_cb(struct net_context *context, struct net_buf *net_bu // if net_buf == NULL, EOF if (net_buf == NULL) { - // TODO: k_fifo accessor for this? - struct net_buf *last_buf = (struct net_buf*)sys_slist_peek_tail(&socket->recv_q.data_q); + struct net_buf *last_buf = _k_fifo_peek_tail(&socket->recv_q); // We abuse "buf_sent" flag to store EOF flag net_nbuf_set_buf_sent(last_buf, true); DEBUG_printf("Set EOF flag on %p\n", last_buf); -- cgit v1.2.3 From 1da840464759f73efbc42238a91733f436991eaf Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 6 Apr 2017 10:11:04 +0300 Subject: modusocket: Handle a case when recv_q is empty when EOF is signaled. In this case, we can mark socket as closed directly. --- zephyr/modusocket.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 0bcbbce60..a5ed5c1b0 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -145,9 +145,14 @@ static void sock_received_cb(struct net_context *context, struct net_buf *net_bu // if net_buf == NULL, EOF if (net_buf == NULL) { struct net_buf *last_buf = _k_fifo_peek_tail(&socket->recv_q); - // We abuse "buf_sent" flag to store EOF flag - net_nbuf_set_buf_sent(last_buf, true); - DEBUG_printf("Set EOF flag on %p\n", last_buf); + if (last_buf == NULL) { + socket->state = STATE_PEER_CLOSED; + DEBUG_printf("Marked socket %p as peer-closed\n", socket); + } else { + // We abuse "buf_sent" flag to store EOF flag + net_nbuf_set_buf_sent(last_buf, true); + DEBUG_printf("Set EOF flag on %p\n", last_buf); + } return; } -- cgit v1.2.3 From f1c0676a70c771abb6e319a0ec2708740a2bf222 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 7 Apr 2017 16:47:10 +0300 Subject: zephyr/modusocket: Implement listen(). --- zephyr/modusocket.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index a5ed5c1b0..6dd257266 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -244,6 +244,16 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect); +STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) { + socket_obj_t *socket = self_in; + socket_check_closed(socket); + + mp_int_t backlog = mp_obj_get_int(backlog_in); + RAISE_ERRNO(net_context_listen(socket->ctx, backlog)); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen); + STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { socket_obj_t *socket = self_in; socket_check_closed(socket); @@ -368,6 +378,7 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&socket_close_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj }, }; -- cgit v1.2.3 From 96166ec16526e8cbb3a18df478da68fe5d8208ee Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 8 Apr 2017 00:30:17 +0300 Subject: zephyr/modusocket: socket_bind: Don't set recv callback on STREAM sockets. For stream sockets, next exected operation is listen(). --- zephyr/modusocket.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 6dd257266..7898f5576 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -224,8 +224,13 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) { parse_inet_addr(socket, addr_in, &sockaddr); RAISE_ERRNO(net_context_bind(socket->ctx, &sockaddr, sizeof(sockaddr))); - DEBUG_printf("Setting recv cb after bind\n"); - RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket)); + // For DGRAM socket, we expect to receive packets after call to bind(), + // but for STREAM socket, next expected operation is listen(), which + // doesn't work if recv callback is set. + if (net_context_get_type(socket->ctx) == SOCK_DGRAM) { + DEBUG_printf("Setting recv cb after bind\n"); + RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket)); + } return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind); -- cgit v1.2.3 From 8ef469f7cab0e820ec8bc4f175eecf81a404da4f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 8 Apr 2017 00:33:09 +0300 Subject: zephyr/modusocket: Implement accept(). --- zephyr/modusocket.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 7898f5576..36ccdbc3b 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -45,7 +45,10 @@ typedef struct _socket_obj_t { mp_obj_base_t base; struct net_context *ctx; - struct k_fifo recv_q; + union { + struct k_fifo recv_q; + struct k_fifo accept_q; + }; struct net_buf *cur_buf; #define STATE_NEW 0 @@ -164,6 +167,15 @@ static void sock_received_cb(struct net_context *context, struct net_buf *net_bu k_fifo_put(&socket->recv_q, net_buf); } +// Callback for incoming connections. +static void sock_accepted_cb(struct net_context *new_ctx, struct sockaddr *addr, socklen_t addrlen, int status, void *user_data) { + socket_obj_t *socket = (socket_obj_t*)user_data; + DEBUG_printf("accept cb: context: %p, status: %d, new ctx: %p\n", socket->ctx, status, new_ctx); + DEBUG_printf("new_ctx ref_cnt: %d\n", new_ctx->refcount); + + k_fifo_put(&socket->accept_q, new_ctx); +} + socket_obj_t *socket_new(void) { socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); socket->base.type = (mp_obj_t)&socket_type; @@ -255,10 +267,33 @@ STATIC mp_obj_t socket_listen(mp_obj_t self_in, mp_obj_t backlog_in) { mp_int_t backlog = mp_obj_get_int(backlog_in); RAISE_ERRNO(net_context_listen(socket->ctx, backlog)); + RAISE_ERRNO(net_context_accept(socket->ctx, sock_accepted_cb, K_NO_WAIT, socket)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_listen_obj, socket_listen); +STATIC mp_obj_t socket_accept(mp_obj_t self_in) { + socket_obj_t *socket = self_in; + socket_check_closed(socket); + + struct net_context *ctx = k_fifo_get(&socket->accept_q, K_FOREVER); + // Was overwritten by fifo + ctx->refcount = 1; + + socket_obj_t *socket2 = socket_new(); + socket2->ctx = ctx; + DEBUG_printf("Setting recv cb after accept()\n"); + RAISE_ERRNO(net_context_recv(ctx, sock_received_cb, K_NO_WAIT, socket2)); + + mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL); + client->items[0] = MP_OBJ_FROM_PTR(socket2); + // TODO + client->items[1] = mp_const_none; + + return MP_OBJ_FROM_PTR(client); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept); + STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { socket_obj_t *socket = self_in; socket_check_closed(socket); @@ -384,6 +419,7 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_bind), (mp_obj_t)&socket_bind_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_connect), (mp_obj_t)&socket_connect_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_listen), (mp_obj_t)&socket_listen_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj }, }; -- cgit v1.2.3 From cf70f9a474a40a4b263bce8a63ef5bdf342673c8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 8 Apr 2017 00:38:51 +0300 Subject: zephyr/mpconfigport.h: Fix build if usocket module is disabled. --- zephyr/mpconfigport.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'zephyr') diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index 0949d1374..936783451 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -109,6 +109,14 @@ extern const struct _mp_obj_module_t mp_module_time; extern const struct _mp_obj_module_t mp_module_usocket; extern const struct _mp_obj_module_t mp_module_zephyr; +#if MICROPY_PY_USOCKET +#define MICROPY_PY_USOCKET_DEF { MP_ROM_QSTR(MP_QSTR_usocket), MP_ROM_PTR(&mp_module_usocket) }, +#define MICROPY_PY_USOCKET_WEAK_DEF { MP_OBJ_NEW_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_module_usocket) }, +#else +#define MICROPY_PY_USOCKET_DEF +#define MICROPY_PY_USOCKET_WEAK_DEF +#endif + #if MICROPY_PY_UTIME #define MICROPY_PY_UTIME_DEF { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_time) }, #else @@ -123,13 +131,13 @@ extern const struct _mp_obj_module_t mp_module_zephyr; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \ + MICROPY_PY_USOCKET_DEF \ MICROPY_PY_UTIME_DEF \ MICROPY_PY_ZEPHYR_DEF \ #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_module_time) }, \ - { MP_OBJ_NEW_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_module_usocket) }, \ + MICROPY_PY_USOCKET_WEAK_DEF \ // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ -- cgit v1.2.3 From e05cb4183a6ab06123eaa9eac91d6d134ebc083b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 8 Apr 2017 00:50:19 +0300 Subject: zephyr/modmachine: Add Signal class. --- zephyr/modmachine.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modmachine.c b/zephyr/modmachine.c index 6194a95da..4013815d1 100644 --- a/zephyr/modmachine.c +++ b/zephyr/modmachine.c @@ -32,6 +32,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "extmod/machine_mem.h" +#include "extmod/machine_signal.h" #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" #include "modmachine.h" @@ -56,6 +57,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, + { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, // reset causes /*{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(REASON_DEFAULT_RST) },*/ -- cgit v1.2.3 From 0a88b44248a7fafb01e98019e4b79547772ea206 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 8 Apr 2017 14:27:36 +0300 Subject: zephyr/machine_pin: Implement pin protocol for machine.Signal support. --- zephyr/machine_pin.c | 27 +++++++++++++++++++++++++-- zephyr/mpconfigport.h | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) (limited to 'zephyr') diff --git a/zephyr/machine_pin.c b/zephyr/machine_pin.c index 049255e9f..5bf273909 100644 --- a/zephyr/machine_pin.c +++ b/zephyr/machine_pin.c @@ -81,7 +81,7 @@ STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, mp_uint_t n } // constructor(drv_name, pin, ...) -STATIC mp_obj_t machine_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); // get the wanted port @@ -154,6 +154,24 @@ STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high); +STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { + (void)errcode; + machine_pin_obj_t *self = self_in; + + switch (request) { + case MP_PIN_READ: { + uint32_t pin_val; + gpio_pin_read(self->port, self->pin, &pin_val); + return pin_val; + } + case MP_PIN_WRITE: { + gpio_pin_write(self->port, self->pin, arg); + return 0; + } + } + return -1; +} + STATIC const mp_map_elem_t machine_pin_locals_dict_table[] = { // instance methods { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&machine_pin_init_obj }, @@ -170,11 +188,16 @@ STATIC const mp_map_elem_t machine_pin_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); +STATIC const mp_pin_p_t machine_pin_pin_p = { + .ioctl = machine_pin_ioctl, +}; + const mp_obj_type_t machine_pin_type = { { &mp_type_type }, .name = MP_QSTR_Pin, .print = machine_pin_print, - .make_new = machine_pin_make_new, + .make_new = mp_pin_make_new, .call = machine_pin_call, + .protocol = &machine_pin_pin_p, .locals_dict = (mp_obj_t)&machine_pin_locals_dict, }; diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index 936783451..2c57ede4d 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -58,6 +58,7 @@ #define MICROPY_PY_IO (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_PY_STRUCT (0) #ifdef CONFIG_NETWORKING -- cgit v1.2.3 From 81d302b8f8a9a6b1c3547b4d00fc938e4ff730b5 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 11 Apr 2017 15:24:33 +0300 Subject: zephyr/modusocket: Call net_nbuf_print_frags() in recv callback if DEBUG > 1. --- zephyr/modusocket.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 36ccdbc3b..0eb6e9dac 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -36,7 +36,8 @@ #include #include -#if 0 // print debugging info +#define DEBUG 0 +#if DEBUG // print debugging info #define DEBUG_printf printf #else // don't print debugging info #define DEBUG_printf(...) (void)0 @@ -144,6 +145,9 @@ static void sock_received_cb(struct net_context *context, struct net_buf *net_bu DEBUG_printf(" (sz=%d, l=%d), token: %p", net_buf->size, net_buf->len, net_nbuf_token(net_buf)); } DEBUG_printf("\n"); + #if DEBUG > 1 + net_nbuf_print_frags(net_buf); + #endif // if net_buf == NULL, EOF if (net_buf == NULL) { -- cgit v1.2.3 From 84e17063c3ab0feb25b0777dac752e6b333b2a4d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 13 Apr 2017 22:19:16 +0300 Subject: zephyr/modusocket: Strip packet header right in the receive callback. Instead of complicating recv() implementation. --- zephyr/modusocket.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 0eb6e9dac..1baabdba6 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -120,8 +120,6 @@ STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct socka // to the fact that it copies data byte by byte). static char *net_buf_gather(struct net_buf *buf, char *to, unsigned max_len) { struct net_buf *tmp = buf->frags; - unsigned header_len = net_nbuf_appdata(buf) - tmp->data; - net_buf_pull(tmp, header_len); while (tmp && max_len) { unsigned len = tmp->len; @@ -166,6 +164,10 @@ static void sock_received_cb(struct net_context *context, struct net_buf *net_bu // Make sure that "EOF flag" is not set net_nbuf_set_buf_sent(net_buf, false); + // We don't care about packet header, so get rid of it asap + unsigned header_len = net_nbuf_appdata(net_buf) - net_buf->frags->data; + net_buf_pull(net_buf->frags, header_len); + // net_buf->frags will be overwritten by fifo, so save it net_nbuf_set_token(net_buf, net_buf->frags); k_fifo_put(&socket->recv_q, net_buf); @@ -350,15 +352,13 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { return mp_const_empty_bytes; } - unsigned header_len = 0; if (socket->cur_buf == NULL) { DEBUG_printf("TCP recv: no cur_buf, getting\n"); struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER); // Restore ->frags overwritten by fifo net_buf->frags = net_nbuf_token(net_buf); - header_len = net_nbuf_appdata(net_buf) - net_buf->frags->data; - DEBUG_printf("TCP recv: new cur_buf: %p, hdr_len: %u\n", net_buf, header_len); + DEBUG_printf("TCP recv: new cur_buf: %p\n", net_buf); socket->cur_buf = net_buf; } @@ -368,7 +368,6 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { assert(0); } - net_buf_pull(frag, header_len); unsigned frag_len = frag->len; recv_len = frag_len; if (recv_len > max_len) { -- cgit v1.2.3 From 209eaec599b8b849dbef45fdb2d8f7138b8b7745 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 14 Apr 2017 19:46:27 +0300 Subject: socket_send: Don't send more than MTU allows. As Zephyr currently doesn't handle MTU itself (ZEP-1998), limit amount of data we send on our side. Also, if we get unsuccessful result from net_nbuf_append(), calculate how much data it has added still. This works around ZEP-1984. --- zephyr/modusocket.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 1baabdba6..00e0bc789 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -308,14 +308,22 @@ STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); struct net_buf *send_buf = net_nbuf_get_tx(socket->ctx, K_FOREVER); - // TODO: Probably should limit how much data we send in one call still - if (!net_nbuf_append(send_buf, bufinfo.len, bufinfo.buf, K_FOREVER)) { - mp_raise_OSError(ENOSPC); + + unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx)); + // Arbitrary value to account for protocol headers + len -= 64; + if (len > bufinfo.len) { + len = bufinfo.len; + } + + if (!net_nbuf_append(send_buf, len, bufinfo.buf, K_FOREVER)) { + len = net_buf_frags_len(send_buf); + //mp_raise_OSError(ENOSPC); } RAISE_ERRNO(net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL)); - return mp_obj_new_int_from_uint(bufinfo.len); + return mp_obj_new_int_from_uint(len); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send); -- cgit v1.2.3 From 58467709972724ac94f6488fefe6d066bfa1589e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 19 Apr 2017 13:27:51 +0300 Subject: zephyr/modmachine: Implement machine.reset(). --- zephyr/modmachine.c | 6 +++++- zephyr/prj_base.conf | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/modmachine.c b/zephyr/modmachine.c index 4013815d1..5909c37d6 100644 --- a/zephyr/modmachine.c +++ b/zephyr/modmachine.c @@ -28,6 +28,7 @@ #include #include +#include #include "py/obj.h" #include "py/runtime.h" @@ -40,7 +41,8 @@ #if MICROPY_PY_MACHINE STATIC mp_obj_t machine_reset(void) { - printf("Warning: %s is not implemented\n", __func__); + sys_reboot(SYS_REBOOT_COLD); + // Won't get here, Zephyr has infiniloop on its side return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); @@ -53,7 +55,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, + #ifdef CONFIG_REBOOT { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, diff --git a/zephyr/prj_base.conf b/zephyr/prj_base.conf index fc51ccec6..1a0b40711 100644 --- a/zephyr/prj_base.conf +++ b/zephyr/prj_base.conf @@ -1,4 +1,5 @@ CONFIG_LEGACY_KERNEL=n +CONFIG_REBOOT=y CONFIG_STDOUT_CONSOLE=y CONFIG_CONSOLE_HANDLER=y -- cgit v1.2.3 From 5e66f2b751274d5fd95eff2510544e99a11e814c Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 22 Apr 2017 18:52:13 +0300 Subject: zephyr/main: Configure IPv4 netmask and gateway to allow Internet access. --- zephyr/main.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index 1f8589a6c..4c083e4c1 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -66,6 +66,10 @@ void init_zephyr(void) { #ifdef CONFIG_NET_IPV4 static struct in_addr in4addr_my = {{{192, 0, 2, 1}}}; net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my, NET_ADDR_MANUAL, 0); + static struct in_addr in4netmask_my = {{{255, 255, 255, 0}}}; + net_if_ipv4_set_netmask(net_if_get_default(), &in4netmask_my); + static struct in_addr in4gw_my = {{{192, 0, 2, 2}}}; + net_if_ipv4_set_gw(net_if_get_default(), &in4gw_my); #endif #ifdef CONFIG_NET_IPV6 // 2001:db8::1 -- cgit v1.2.3 From 083cd21a741e4537f743db19871cc605e31aed96 Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Wed, 22 Mar 2017 19:06:34 +0530 Subject: zephyr: Add 96b_carbon configuration. As there's no networking support in mainline yet, networking is disabled, because otherwise the board hangs on startup. --- zephyr/prj_96b_carbon.conf | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 zephyr/prj_96b_carbon.conf (limited to 'zephyr') diff --git a/zephyr/prj_96b_carbon.conf b/zephyr/prj_96b_carbon.conf new file mode 100644 index 000000000..3e41e2532 --- /dev/null +++ b/zephyr/prj_96b_carbon.conf @@ -0,0 +1,2 @@ +# TODO: Enable networking +CONFIG_NETWORKING=n -- cgit v1.2.3 From 0e177e06493368cbb5cfc5013804da247fb4c2e5 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 26 Apr 2017 01:05:54 +0300 Subject: zephyr/modusocket: Refactor send() into stream write() method. --- zephyr/modusocket.c | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 00e0bc789..bd51e6f5d 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -28,6 +28,7 @@ #ifdef MICROPY_PY_USOCKET #include "py/runtime.h" +#include "py/stream.h" #include #include @@ -300,29 +301,44 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept); -STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { +STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { socket_obj_t *socket = self_in; - socket_check_closed(socket); - - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); + if (socket->ctx == NULL) { + // already closed + *errcode = EBADF; + return MP_STREAM_ERROR; + } struct net_buf *send_buf = net_nbuf_get_tx(socket->ctx, K_FOREVER); unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx)); // Arbitrary value to account for protocol headers len -= 64; - if (len > bufinfo.len) { - len = bufinfo.len; + if (len > size) { + len = size; } - if (!net_nbuf_append(send_buf, len, bufinfo.buf, K_FOREVER)) { + if (!net_nbuf_append(send_buf, len, buf, K_FOREVER)) { len = net_buf_frags_len(send_buf); - //mp_raise_OSError(ENOSPC); } - RAISE_ERRNO(net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL)); + int err = net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL); + if (err < 0) { + *errcode = -err; + return MP_STREAM_ERROR; + } + return len; +} + +STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); + int err = 0; + mp_uint_t len = sock_write(self_in, bufinfo.buf, bufinfo.len, &err); + if (len == MP_STREAM_ERROR) { + mp_raise_OSError(err); + } return mp_obj_new_int_from_uint(len); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send); @@ -436,12 +452,18 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); +STATIC const mp_stream_p_t socket_stream_p = { + //.read = sock_read, + .write = sock_write, + //.ioctl = sock_ioctl, +}; + STATIC const mp_obj_type_t socket_type = { { &mp_type_type }, .name = MP_QSTR_socket, .print = socket_print, .make_new = socket_make_new, - //.protocol = &socket_stream_p, + .protocol = &socket_stream_p, .locals_dict = (mp_obj_t)&socket_locals_dict, }; -- cgit v1.2.3 From 63068875c5b3bb754af4fd5f8006f6d9c0435f7d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 26 Apr 2017 01:06:42 +0300 Subject: zephyr/modusocket: Enable stream write() method. --- zephyr/modusocket.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index bd51e6f5d..e154fdfea 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -449,6 +449,8 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, }; STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); -- cgit v1.2.3 From ef55be159c581e76bfc661fefd48f001b51a7007 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 26 Apr 2017 08:42:19 +0300 Subject: zephyr/modusocket: Refactor recv() into stream read() method. --- zephyr/modusocket.c | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index e154fdfea..50a5c0314 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -343,14 +343,16 @@ STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send); -STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { +STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *errcode) { socket_obj_t *socket = self_in; - socket_check_closed(socket); + if (socket->ctx == NULL) { + // already closed + *errcode = EBADF; + return MP_STREAM_ERROR; + } enum net_sock_type sock_type = net_context_get_type(socket->ctx); - mp_int_t max_len = mp_obj_get_int(len_in); unsigned recv_len; - vstr_t vstr; if (sock_type == SOCK_DGRAM) { @@ -365,15 +367,14 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { recv_len = max_len; } - vstr_init_len(&vstr, recv_len); - net_buf_gather(net_buf, vstr.buf, recv_len); + net_buf_gather(net_buf, buf, recv_len); net_nbuf_unref(net_buf); } else if (sock_type == SOCK_STREAM) { do { if (socket->state == STATE_PEER_CLOSED) { - return mp_const_empty_bytes; + return 0; } if (socket->cur_buf == NULL) { @@ -399,8 +400,7 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { } DEBUG_printf("%d data bytes in head frag, going to read %d\n", frag_len, recv_len); - vstr_init_len(&vstr, recv_len); - memcpy(vstr.buf, frag->data, recv_len); + memcpy(buf, frag->data, recv_len); if (recv_len != frag_len) { net_buf_pull(frag, recv_len); @@ -420,13 +420,32 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { // Zephyr IP stack appears to feed empty net_buf's with empty // frags for various TCP control packets. } while (recv_len == 0); + } - } else { - mp_not_implemented(""); + return recv_len; +} + +STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { + mp_int_t max_len = mp_obj_get_int(len_in); + vstr_t vstr; + // +1 to accommodate for trailing \0 + vstr_init_len(&vstr, max_len + 1); + + int err; + mp_uint_t len = sock_read(self_in, vstr.buf, max_len, &err); + + if (len == MP_STREAM_ERROR) { + vstr_clear(&vstr); + mp_raise_OSError(err); + } + + if (len == 0) { + vstr_clear(&vstr); + return mp_const_empty_bytes; } - mp_obj_t ret = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); - return ret; + vstr.len = len; + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv); @@ -455,7 +474,7 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); STATIC const mp_stream_p_t socket_stream_p = { - //.read = sock_read, + .read = sock_read, .write = sock_write, //.ioctl = sock_ioctl, }; -- cgit v1.2.3 From 1fe0f678f86507781989e8db5c005df602968e13 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 26 Apr 2017 08:42:48 +0300 Subject: zephyr/modusocket: Add read/readline/readinto stream methods. --- zephyr/modusocket.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 50a5c0314..2fa04a64a 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -469,6 +469,9 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, }; STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); -- cgit v1.2.3 From 868453d3d83ea8d3ed161bd20f2f6cb59a396562 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 26 Apr 2017 09:06:58 +0300 Subject: zephyr/modusocket: sock_read: Check socket status only at the start of packet. Otherwise, if we already have a packet in progress, finish it first, before check "peer closed" status. --- zephyr/modusocket.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 2fa04a64a..5700cc4bd 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -373,11 +373,12 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int * } else if (sock_type == SOCK_STREAM) { do { - if (socket->state == STATE_PEER_CLOSED) { - return 0; - } if (socket->cur_buf == NULL) { + if (socket->state == STATE_PEER_CLOSED) { + return 0; + } + DEBUG_printf("TCP recv: no cur_buf, getting\n"); struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER); // Restore ->frags overwritten by fifo -- cgit v1.2.3 From 31bbcd448cb8fd61ee3fa23b93d03c5ae41427d8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 27 Apr 2017 14:57:49 +0300 Subject: zephyr/modusocket: Add dummy makefile() implementation. --- zephyr/modusocket.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 5700cc4bd..92d45a0fe 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -450,6 +450,12 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv); +STATIC mp_obj_t socket_makefile(size_t n_args, const mp_obj_t *args) { + (void)n_args; + return args[0]; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_makefile); + STATIC mp_obj_t socket_close(mp_obj_t self_in) { socket_obj_t *socket = self_in; if (socket->ctx != NULL) { @@ -474,6 +480,7 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&socket_makefile_obj }, }; STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); -- cgit v1.2.3 From 674da04e2e4aa54822652b8e3e24261d8d4cd95d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 2 May 2017 01:12:02 +0300 Subject: zephyr/Makefile: Add debugserver Zephyr target. --- zephyr/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/Makefile b/zephyr/Makefile index 057a88862..9e8cb31d0 100644 --- a/zephyr/Makefile +++ b/zephyr/Makefile @@ -64,7 +64,7 @@ $(Z_EXPORTS): $(CONF_FILE) # e.g. DTS-related. $(MAKE) -f Makefile.zephyr BOARD=$(BOARD) CONF_FILE=$(CONF_FILE) initconfig outputexports lib -GENERIC_TARGETS = all zephyr run qemu qemugdb flash debug +GENERIC_TARGETS = all zephyr run qemu qemugdb flash debug debugserver KCONFIG_TARGETS = \ initconfig config nconfig menuconfig xconfig gconfig \ oldconfig silentoldconfig defconfig savedefconfig \ -- cgit v1.2.3 From b5159a91498006c4ad8ff55465437a9fe18899a5 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 2 May 2017 23:33:16 +0300 Subject: zephyr/mpconfigport.h: Enable line number information for scripts. --- zephyr/mpconfigport.h | 1 + 1 file changed, 1 insertion(+) (limited to 'zephyr') diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index 2c57ede4d..98a033be4 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -35,6 +35,7 @@ #define MICROPY_HEAP_SIZE (16 * 1024) #endif +#define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_STACK_CHECK (1) #define MICROPY_ENABLE_GC (1) #define MICROPY_HELPER_REPL (1) -- cgit v1.2.3 From 4c2fa83f2afa1dc9522755261c8a6969857a2031 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 2 May 2017 23:35:13 +0300 Subject: zephyr/main: Remove superfluous include. --- zephyr/main.c | 1 - 1 file changed, 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index 4c083e4c1..a67ad3352 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -31,7 +31,6 @@ #include #ifdef CONFIG_NETWORKING #include -#include #endif #include "py/nlr.h" -- cgit v1.2.3 From 1659c0645d27bba297813889fc7d2c118706c74f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 12 May 2017 21:54:32 +0300 Subject: zephyr/modusocket: Add dummy setsockopt() implementation. --- zephyr/modusocket.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 92d45a0fe..ad3a7e23b 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -450,6 +450,13 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv); +STATIC mp_obj_t socket_setsockopt(size_t n_args, const mp_obj_t *args) { + (void)n_args; // always 4 + mp_warning("setsockopt() not implemented"); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_setsockopt_obj, 4, 4, socket_setsockopt); + STATIC mp_obj_t socket_makefile(size_t n_args, const mp_obj_t *args) { (void)n_args; return args[0]; @@ -475,6 +482,7 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_accept), (mp_obj_t)&socket_accept_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&socket_send_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&socket_recv_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_setsockopt), (mp_obj_t)&socket_setsockopt_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, -- cgit v1.2.3 From 50d7ed325af28317c4ee8d771f93e8602fac1e19 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 12 May 2017 21:55:42 +0300 Subject: zephyr/modusocket: Add SOL_SOCKET and SO_REUSEADDR constants. --- zephyr/modusocket.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index ad3a7e23b..0cacd7179 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -517,6 +517,9 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_STREAM), MP_OBJ_NEW_SMALL_INT(SOCK_STREAM) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SOCK_DGRAM), MP_OBJ_NEW_SMALL_INT(SOCK_DGRAM) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_SOL_SOCKET), MP_OBJ_NEW_SMALL_INT(1) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_SO_REUSEADDR), MP_OBJ_NEW_SMALL_INT(2) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table); -- cgit v1.2.3 From d57c6564dc270f52e131162f682c706f44696a16 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 12 May 2017 21:58:51 +0300 Subject: zephyr/prj_qemu_x86.conf: Bump RAM size to 320K. --- zephyr/prj_qemu_x86.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/prj_qemu_x86.conf b/zephyr/prj_qemu_x86.conf index cb90834ac..ef60cfec9 100644 --- a/zephyr/prj_qemu_x86.conf +++ b/zephyr/prj_qemu_x86.conf @@ -3,4 +3,4 @@ CONFIG_NET_SLIP_TAP=y # Default RAM easily overflows with uPy and networking -CONFIG_RAM_SIZE=256 +CONFIG_RAM_SIZE=320 -- cgit v1.2.3 From 5383a50072dcd9827e601add56d20b86b9dd9e27 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 12 May 2017 22:05:07 +0300 Subject: zephyr/README: Update to require Zephyr 1.8. There're a lot of changes and fixes in 1.8 regarding IP stack, incompatible with previous Zephyr versions, so supporting them doesn't make sense. This is the last commit which should build with Zephyr 1.7. --- zephyr/README.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'zephyr') diff --git a/zephyr/README.md b/zephyr/README.md index 4fbf3d401..018b4ce71 100644 --- a/zephyr/README.md +++ b/zephyr/README.md @@ -1,20 +1,19 @@ MicroPython port to Zephyr RTOS =============================== -This is an initial port of MicroPython to Zephyr RTOS +This is an work-in-progress port of MicroPython to Zephyr RTOS (http://zephyrproject.org). -The port integrates well with Zephyr build system, using the latest -features which will be available in 1.6.0, and thus requires Zephyr -master to build against. All boards supported by Zephyr (with standard -level of feature support, like UART console) should work with -MicroPython (but not all were tested). +This port requires Zephyr version 1.8 or higher. All boards supported +by Zephyr (with standard level of features support, like UART console) +should work with MicroPython (but not all were tested). Features supported at this time: * REPL (interactive prompt) over Zephyr UART console. * `utime` module for time measurements and delays. * `machine.Pin` class for GPIO control. +* `usocket` module for networking (IPv4/IPv6). * "Frozen modules" support to allow to bundle Python modules together with firmware. Including complete applications, including with run-on-boot capability. @@ -97,10 +96,10 @@ MicroPython is committed to maintain minimal binary size for Zephyr port below 128KB, as long as Zephyr project is committed to maintain stable minimal size of their kernel (which they appear to be). Note that at such size, there is no support for any Zephyr features beyond REPL over UART, -and only very minimal set of builtin Python modules. Thus, this build -is more suitable for code size control and quick demonstrations on -smaller systems. It's also suitable for careful enabling of features one -by one to achieve needed functionality and code size. This is in a +and only very minimal set of builtin Python modules is available. Thus, +this build is more suitable for code size control and quick demonstrations +on smaller systems. It's also suitable for careful enabling of features +one by one to achieve needed functionality and code size. This is in the contrast to the "default" build, which may get more and more features enabled over time. -- cgit v1.2.3 From c022c9a2f0ea3a707b6993daaffe2bcddd520da2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 13 May 2017 15:22:22 +0300 Subject: zephyr/modusocket: Wrap pkt_get_info() call. The most important info it returns are numbers of free buffers in different pools (rx/tx packet headers, data fragments). --- zephyr/modusocket.c | 14 ++++++++++++++ zephyr/prj_base.conf | 3 +++ 2 files changed, 17 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 0cacd7179..c48606b76 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -507,6 +507,18 @@ STATIC const mp_obj_type_t socket_type = { .locals_dict = (mp_obj_t)&socket_locals_dict, }; +STATIC mp_obj_t nbuf_get_info(void) { + struct net_buf_pool *rx, *tx, *rx_data, *tx_data; + net_nbuf_get_info(&rx, &tx, &rx_data, &tx_data); + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL)); + t->items[0] = MP_OBJ_NEW_SMALL_INT(rx->avail_count); + t->items[1] = MP_OBJ_NEW_SMALL_INT(tx->avail_count); + t->items[2] = MP_OBJ_NEW_SMALL_INT(rx_data->avail_count); + t->items[3] = MP_OBJ_NEW_SMALL_INT(tx_data->avail_count); + return MP_OBJ_FROM_PTR(t); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(nbuf_get_info_obj, nbuf_get_info); + STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) }, // objects @@ -520,6 +532,8 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_SOL_SOCKET), MP_OBJ_NEW_SMALL_INT(1) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SO_REUSEADDR), MP_OBJ_NEW_SMALL_INT(2) }, + + { MP_OBJ_NEW_QSTR(MP_QSTR_nbuf_get_info), (mp_obj_t)&nbuf_get_info_obj }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table); diff --git a/zephyr/prj_base.conf b/zephyr/prj_base.conf index 1a0b40711..97a78fa77 100644 --- a/zephyr/prj_base.conf +++ b/zephyr/prj_base.conf @@ -17,6 +17,9 @@ CONFIG_NET_TCP=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_NET_NBUF_RX_COUNT=5 +# Required for usocket.pkt_get_info() +CONFIG_NET_BUF_POOL_USAGE=y + # Uncomment to enable "INFO" level net_buf logging #CONFIG_NET_LOG=y #CONFIG_NET_DEBUG_NET_BUF=y -- cgit v1.2.3 From 39d3335606e912dee16ee29cd481427f884dbf18 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 13 May 2017 15:47:47 +0300 Subject: zephyr/modusocket: Update for net_pkt refactor. --- zephyr/modusocket.c | 113 +++++++++++++++++++++++++--------------------------- 1 file changed, 54 insertions(+), 59 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index c48606b76..eec4fa219 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -35,7 +35,7 @@ // Zephyr's generated version header #include #include -#include +#include #define DEBUG 0 #if DEBUG // print debugging info @@ -51,7 +51,7 @@ typedef struct _socket_obj_t { struct k_fifo recv_q; struct k_fifo accept_q; }; - struct net_buf *cur_buf; + struct net_pkt *cur_pkt; #define STATE_NEW 0 #define STATE_CONNECTING 1 @@ -115,12 +115,12 @@ STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct socka } // Copy data from Zephyr net_buf chain into linear buffer. -// We don't use net_nbuf_read(), because it's weird (e.g., we'd like to -// free processed data fragment ASAP, while net_nbuf_read() holds onto +// We don't use net_pkt_read(), because it's weird (e.g., we'd like to +// free processed data fragment ASAP, while net_pkt_read() holds onto // the whole fragment chain to do its deeds, and that's minor comparing // to the fact that it copies data byte by byte). -static char *net_buf_gather(struct net_buf *buf, char *to, unsigned max_len) { - struct net_buf *tmp = buf->frags; +static char *net_pkt_gather(struct net_pkt *pkt, char *to, unsigned max_len) { + struct net_buf *tmp = pkt->frags; while (tmp && max_len) { unsigned len = tmp->len; @@ -130,48 +130,46 @@ static char *net_buf_gather(struct net_buf *buf, char *to, unsigned max_len) { memcpy(to, tmp->data, len); to += len; max_len -= len; - tmp = net_buf_frag_del(buf, tmp); + tmp = net_pkt_frag_del(pkt, NULL, tmp); } return to; } // Callback for incoming packets. -static void sock_received_cb(struct net_context *context, struct net_buf *net_buf, int status, void *user_data) { +static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, int status, void *user_data) { socket_obj_t *socket = (socket_obj_t*)user_data; - DEBUG_printf("recv cb: context: %p, status: %d, buf: %p", context, status, net_buf); - if (net_buf) { - DEBUG_printf(" (sz=%d, l=%d), token: %p", net_buf->size, net_buf->len, net_nbuf_token(net_buf)); + DEBUG_printf("recv cb: context: %p, status: %d, pkt: %p", context, status, pkt); + if (pkt) { + DEBUG_printf(" (appdatalen=%d), token: %p", pkt->appdatalen, net_pkt_token(pkt)); } DEBUG_printf("\n"); #if DEBUG > 1 - net_nbuf_print_frags(net_buf); + net_pkt_print_frags(pkt); #endif // if net_buf == NULL, EOF - if (net_buf == NULL) { - struct net_buf *last_buf = _k_fifo_peek_tail(&socket->recv_q); - if (last_buf == NULL) { + if (pkt == NULL) { + struct net_pkt *last_pkt = _k_fifo_peek_tail(&socket->recv_q); + if (last_pkt == NULL) { socket->state = STATE_PEER_CLOSED; DEBUG_printf("Marked socket %p as peer-closed\n", socket); } else { // We abuse "buf_sent" flag to store EOF flag - net_nbuf_set_buf_sent(last_buf, true); - DEBUG_printf("Set EOF flag on %p\n", last_buf); + net_pkt_set_sent(last_pkt, true); + DEBUG_printf("Set EOF flag on %p\n", last_pkt); } return; } // Make sure that "EOF flag" is not set - net_nbuf_set_buf_sent(net_buf, false); + net_pkt_set_sent(pkt, false); // We don't care about packet header, so get rid of it asap - unsigned header_len = net_nbuf_appdata(net_buf) - net_buf->frags->data; - net_buf_pull(net_buf->frags, header_len); + unsigned header_len = net_pkt_appdata(pkt) - pkt->frags->data; + net_buf_pull(pkt->frags, header_len); - // net_buf->frags will be overwritten by fifo, so save it - net_nbuf_set_token(net_buf, net_buf->frags); - k_fifo_put(&socket->recv_q, net_buf); + k_fifo_put(&socket->recv_q, pkt); } // Callback for incoming connections. @@ -187,7 +185,7 @@ socket_obj_t *socket_new(void) { socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); socket->base.type = (mp_obj_t)&socket_type; k_fifo_init(&socket->recv_q); - socket->cur_buf = NULL; + socket->cur_pkt = NULL; socket->state = STATE_NEW; return socket; } @@ -309,7 +307,7 @@ STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, i return MP_STREAM_ERROR; } - struct net_buf *send_buf = net_nbuf_get_tx(socket->ctx, K_FOREVER); + struct net_pkt *send_pkt = net_pkt_get_tx(socket->ctx, K_FOREVER); unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx)); // Arbitrary value to account for protocol headers @@ -318,11 +316,11 @@ STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, i len = size; } - if (!net_nbuf_append(send_buf, len, buf, K_FOREVER)) { - len = net_buf_frags_len(send_buf); + if (!net_pkt_append(send_pkt, len, buf, K_FOREVER)) { + len = net_pkt_get_len(send_pkt); } - int err = net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL); + int err = net_context_send(send_pkt, /*cb*/NULL, K_FOREVER, NULL, NULL); if (err < 0) { *errcode = -err; return MP_STREAM_ERROR; @@ -356,41 +354,37 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int * if (sock_type == SOCK_DGRAM) { - struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER); - // Restore ->frags overwritten by fifo - net_buf->frags = net_nbuf_token(net_buf); + struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER); - recv_len = net_nbuf_appdatalen(net_buf); - DEBUG_printf("recv: net_buf=%p, appdatalen: %d\n", net_buf, recv_len); + recv_len = net_pkt_appdatalen(pkt); + DEBUG_printf("recv: pkt=%p, appdatalen: %d\n", pkt, recv_len); if (recv_len > max_len) { recv_len = max_len; } - net_buf_gather(net_buf, buf, recv_len); - net_nbuf_unref(net_buf); + net_pkt_gather(pkt, buf, recv_len); + net_pkt_unref(pkt); } else if (sock_type == SOCK_STREAM) { do { - if (socket->cur_buf == NULL) { + if (socket->cur_pkt == NULL) { if (socket->state == STATE_PEER_CLOSED) { return 0; } - DEBUG_printf("TCP recv: no cur_buf, getting\n"); - struct net_buf *net_buf = k_fifo_get(&socket->recv_q, K_FOREVER); - // Restore ->frags overwritten by fifo - net_buf->frags = net_nbuf_token(net_buf); + DEBUG_printf("TCP recv: no cur_pkt, getting\n"); + struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER); - DEBUG_printf("TCP recv: new cur_buf: %p\n", net_buf); - socket->cur_buf = net_buf; + DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt); + socket->cur_pkt = pkt; } - struct net_buf *frag = socket->cur_buf->frags; + struct net_buf *frag = socket->cur_pkt->frags; if (frag == NULL) { - printf("net_buf has empty fragments on start!\n"); + printf("net_pkt has empty fragments on start!\n"); assert(0); } @@ -406,20 +400,20 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int * if (recv_len != frag_len) { net_buf_pull(frag, recv_len); } else { - frag = net_buf_frag_del(socket->cur_buf, frag); + frag = net_pkt_frag_del(socket->cur_pkt, NULL, frag); if (frag == NULL) { - DEBUG_printf("Finished processing net_buf %p\n", socket->cur_buf); - // If "buf_sent" flag was set, it's last packet and we reached EOF - if (net_nbuf_buf_sent(socket->cur_buf)) { + DEBUG_printf("Finished processing pkt %p\n", socket->cur_pkt); + // If "sent" flag was set, it's last packet and we reached EOF + if (net_pkt_sent(socket->cur_pkt)) { socket->state = STATE_PEER_CLOSED; } - net_nbuf_unref(socket->cur_buf); - socket->cur_buf = NULL; + net_pkt_unref(socket->cur_pkt); + socket->cur_pkt = NULL; } } // Keep repeating while we're getting empty fragments - // Zephyr IP stack appears to feed empty net_buf's with empty - // frags for various TCP control packets. + // Zephyr IP stack appears to have fed empty net_buf's with empty + // frags for various TCP control packets - in previous versions. } while (recv_len == 0); } @@ -507,17 +501,18 @@ STATIC const mp_obj_type_t socket_type = { .locals_dict = (mp_obj_t)&socket_locals_dict, }; -STATIC mp_obj_t nbuf_get_info(void) { - struct net_buf_pool *rx, *tx, *rx_data, *tx_data; - net_nbuf_get_info(&rx, &tx, &rx_data, &tx_data); +STATIC mp_obj_t pkt_get_info(void) { + struct k_mem_slab *rx, *tx; + struct net_buf_pool *rx_data, *tx_data; + net_pkt_get_info(&rx, &tx, &rx_data, &tx_data); mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(4, NULL)); - t->items[0] = MP_OBJ_NEW_SMALL_INT(rx->avail_count); - t->items[1] = MP_OBJ_NEW_SMALL_INT(tx->avail_count); + t->items[0] = MP_OBJ_NEW_SMALL_INT(k_mem_slab_num_free_get(rx)); + t->items[1] = MP_OBJ_NEW_SMALL_INT(k_mem_slab_num_free_get(tx)); t->items[2] = MP_OBJ_NEW_SMALL_INT(rx_data->avail_count); t->items[3] = MP_OBJ_NEW_SMALL_INT(tx_data->avail_count); return MP_OBJ_FROM_PTR(t); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(nbuf_get_info_obj, nbuf_get_info); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(pkt_get_info_obj, pkt_get_info); STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_usocket) }, @@ -533,7 +528,7 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_SOL_SOCKET), MP_OBJ_NEW_SMALL_INT(1) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SO_REUSEADDR), MP_OBJ_NEW_SMALL_INT(2) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_nbuf_get_info), (mp_obj_t)&nbuf_get_info_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_pkt_get_info), (mp_obj_t)&pkt_get_info_obj }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_usocket_globals, mp_module_usocket_globals_table); -- cgit v1.2.3 From 69f0b4ad5bac70d3be5cc674d6e0a5f188c353bb Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 13 May 2017 16:05:10 +0300 Subject: zephyr/modusocket: Switch to net_pkt_append() returning length. Requires patch in review. --- zephyr/modusocket.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index eec4fa219..d40b4887c 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -316,9 +316,9 @@ STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, i len = size; } - if (!net_pkt_append(send_pkt, len, buf, K_FOREVER)) { - len = net_pkt_get_len(send_pkt); - } + // TODO: Return value of 0 is a hard case (as we wait forever, should + // not happen). + len = net_pkt_append(send_pkt, len, buf, K_FOREVER); int err = net_context_send(send_pkt, /*cb*/NULL, K_FOREVER, NULL, NULL); if (err < 0) { -- cgit v1.2.3 From 86c4544ef9af169d590048d499edfd3e255e16d6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 13 May 2017 16:22:14 +0300 Subject: zephyr/modusocket: If there're no packets in recv_q, cancel waiter. This solves a case when socker_read() has blocked on fifo, and then peer closed event arrives. --- zephyr/modusocket.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index d40b4887c..daaa15748 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -153,6 +153,7 @@ static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, i struct net_pkt *last_pkt = _k_fifo_peek_tail(&socket->recv_q); if (last_pkt == NULL) { socket->state = STATE_PEER_CLOSED; + k_fifo_cancel_wait(&socket->recv_q); DEBUG_printf("Marked socket %p as peer-closed\n", socket); } else { // We abuse "buf_sent" flag to store EOF flag @@ -378,6 +379,11 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int * DEBUG_printf("TCP recv: no cur_pkt, getting\n"); struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER); + if (pkt == NULL) { + DEBUG_printf("TCP recv: NULL return from fifo\n"); + continue; + } + DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt); socket->cur_pkt = pkt; } -- cgit v1.2.3 From 0c57979ce28712422d996fb5291de85e8f7f3be8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 13 May 2017 16:42:35 +0300 Subject: zephyr/modusocket: Implement getaddrinfo(). --- zephyr/modusocket.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ zephyr/prj_base.conf | 6 ++++ 2 files changed, 92 insertions(+) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index daaa15748..aa638d413 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -36,6 +36,7 @@ #include #include #include +#include #define DEBUG 0 #if DEBUG // print debugging info @@ -114,6 +115,27 @@ STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct socka sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1])); } +STATIC mp_obj_t format_inet_addr(struct sockaddr *addr, mp_obj_t port) { + // We employ the fact that port and address offsets are the same for IPv4 & IPv6 + struct sockaddr_in6 *sockaddr_in6 = (struct sockaddr_in6*)addr; + char buf[40]; + net_addr_ntop(addr->family, &sockaddr_in6->sin6_addr, buf, sizeof(buf)); + mp_obj_tuple_t *tuple = mp_obj_new_tuple(addr->family == AF_INET ? 2 : 4, NULL); + + tuple->items[0] = mp_obj_new_str(buf, strlen(buf), false); + // We employ the fact that port offset is the same for IPv4 & IPv6 + // not filled in + //tuple->items[1] = mp_obj_new_int(ntohs(((struct sockaddr_in*)addr)->sin_port)); + tuple->items[1] = port; + + if (addr->family == AF_INET6) { + tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0); // flow_info + tuple->items[3] = MP_OBJ_NEW_SMALL_INT(sockaddr_in6->sin6_scope_id); + } + + return MP_OBJ_FROM_PTR(tuple); +} + // Copy data from Zephyr net_buf chain into linear buffer. // We don't use net_pkt_read(), because it's weird (e.g., we'd like to // free processed data fragment ASAP, while net_pkt_read() holds onto @@ -507,6 +529,69 @@ STATIC const mp_obj_type_t socket_type = { .locals_dict = (mp_obj_t)&socket_locals_dict, }; +// +// getaddrinfo() implementation +// + +typedef struct _getaddrinfo_state_t { + mp_obj_t result; + struct k_sem sem; + mp_obj_t port; +} getaddrinfo_state_t; + +void dns_resolve_cb(enum dns_resolve_status status, struct dns_addrinfo *info, void *user_data) { + getaddrinfo_state_t *state = user_data; + + if (info == NULL) { + k_sem_give(&state->sem); + return; + } + + mp_obj_tuple_t *tuple = mp_obj_new_tuple(5, NULL); + tuple->items[0] = MP_OBJ_NEW_SMALL_INT(info->ai_family); + // info->ai_socktype not filled + tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCK_STREAM); + // info->ai_protocol not filled + tuple->items[2] = MP_OBJ_NEW_SMALL_INT(IPPROTO_TCP); + tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_); + tuple->items[4] = format_inet_addr(&info->ai_addr, state->port); + mp_obj_list_append(state->result, MP_OBJ_FROM_PTR(tuple)); +} + +STATIC mp_obj_t mod_getaddrinfo(size_t n_args, const mp_obj_t *args) { + mp_obj_t host_in = args[0], port_in = args[1]; + const char *host = mp_obj_str_get_str(host_in); + mp_int_t family = 0; + if (n_args > 2) { + family = mp_obj_get_int(args[2]); + } + + getaddrinfo_state_t state; + // Just validate that it's int + (void)mp_obj_get_int(port_in); + state.port = port_in; + state.result = mp_obj_new_list(0, NULL); + k_sem_init(&state.sem, 0, UINT_MAX); + + int status; + for (int i = 2; i--;) { + int type = (family != AF_INET6 ? DNS_QUERY_TYPE_A : DNS_QUERY_TYPE_AAAA); + status = dns_get_addr_info(host, type, NULL, dns_resolve_cb, &state, 3000); + if (status < 0) { + mp_raise_OSError(status); + } + k_sem_take(&state.sem, K_FOREVER); + if (family != 0) { + break; + } + family = AF_INET6; + } + + return state.result; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_getaddrinfo_obj, 2, 3, mod_getaddrinfo); + + STATIC mp_obj_t pkt_get_info(void) { struct k_mem_slab *rx, *tx; struct net_buf_pool *rx_data, *tx_data; @@ -534,6 +619,7 @@ STATIC const mp_map_elem_t mp_module_usocket_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_SOL_SOCKET), MP_OBJ_NEW_SMALL_INT(1) }, { MP_OBJ_NEW_QSTR(MP_QSTR_SO_REUSEADDR), MP_OBJ_NEW_SMALL_INT(2) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&mod_getaddrinfo_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_pkt_get_info), (mp_obj_t)&pkt_get_info_obj }, }; diff --git a/zephyr/prj_base.conf b/zephyr/prj_base.conf index 97a78fa77..4346f20bf 100644 --- a/zephyr/prj_base.conf +++ b/zephyr/prj_base.conf @@ -17,6 +17,12 @@ CONFIG_NET_TCP=y CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_NET_NBUF_RX_COUNT=5 +# DNS +CONFIG_DNS_RESOLVER=y +CONFIG_DNS_RESOLVER_ADDITIONAL_QUERIES=2 +CONFIG_DNS_SERVER_IP_ADDRESSES=y +CONFIG_DNS_SERVER1="192.0.2.2" + # Required for usocket.pkt_get_info() CONFIG_NET_BUF_POOL_USAGE=y -- cgit v1.2.3 From a3008e447c2fb6ec564f5d0232b0b8aad4980cc4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 16 May 2017 13:43:06 +0300 Subject: zephyr/modusocket: First step to switch to alternative FIFO processing. Here we wait for non-empty FIFO, and then directly access/drop its head element. --- zephyr/modusocket.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index aa638d413..37dfab638 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -399,12 +399,14 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int * } DEBUG_printf("TCP recv: no cur_pkt, getting\n"); - struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER); - + _k_fifo_wait_non_empty(&socket->recv_q, K_FOREVER); + struct net_pkt *pkt = _k_fifo_peek_head(&socket->recv_q); if (pkt == NULL) { DEBUG_printf("TCP recv: NULL return from fifo\n"); continue; } + // Drop head packet from queue + k_fifo_get(&socket->recv_q, K_NO_WAIT); DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt); socket->cur_pkt = pkt; -- cgit v1.2.3 From aa9ce283c2b1877a73e20dace4062484d3d5f7fa Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 16 May 2017 13:46:54 +0300 Subject: zephyr/modusocket: Get rid of cur_pkt object member. Instead, just peek a packet at the head of the queue and work with it. --- zephyr/modusocket.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 37dfab638..c4e6529b8 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -52,7 +52,6 @@ typedef struct _socket_obj_t { struct k_fifo recv_q; struct k_fifo accept_q; }; - struct net_pkt *cur_pkt; #define STATE_NEW 0 #define STATE_CONNECTING 1 @@ -208,7 +207,6 @@ socket_obj_t *socket_new(void) { socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t); socket->base.type = (mp_obj_t)&socket_type; k_fifo_init(&socket->recv_q); - socket->cur_pkt = NULL; socket->state = STATE_NEW; return socket; } @@ -393,26 +391,20 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int * do { - if (socket->cur_pkt == NULL) { - if (socket->state == STATE_PEER_CLOSED) { - return 0; - } - - DEBUG_printf("TCP recv: no cur_pkt, getting\n"); - _k_fifo_wait_non_empty(&socket->recv_q, K_FOREVER); - struct net_pkt *pkt = _k_fifo_peek_head(&socket->recv_q); - if (pkt == NULL) { - DEBUG_printf("TCP recv: NULL return from fifo\n"); - continue; - } - // Drop head packet from queue - k_fifo_get(&socket->recv_q, K_NO_WAIT); + if (socket->state == STATE_PEER_CLOSED) { + return 0; + } - DEBUG_printf("TCP recv: new cur_pkt: %p\n", pkt); - socket->cur_pkt = pkt; + _k_fifo_wait_non_empty(&socket->recv_q, K_FOREVER); + struct net_pkt *pkt = _k_fifo_peek_head(&socket->recv_q); + if (pkt == NULL) { + DEBUG_printf("TCP recv: NULL return from fifo\n"); + continue; } - struct net_buf *frag = socket->cur_pkt->frags; + DEBUG_printf("TCP recv: cur_pkt: %p\n", pkt); + + struct net_buf *frag = pkt->frags; if (frag == NULL) { printf("net_pkt has empty fragments on start!\n"); assert(0); @@ -430,15 +422,17 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int * if (recv_len != frag_len) { net_buf_pull(frag, recv_len); } else { - frag = net_pkt_frag_del(socket->cur_pkt, NULL, frag); + frag = net_pkt_frag_del(pkt, NULL, frag); if (frag == NULL) { - DEBUG_printf("Finished processing pkt %p\n", socket->cur_pkt); + DEBUG_printf("Finished processing pkt %p\n", pkt); + // Drop head packet from queue + k_fifo_get(&socket->recv_q, K_NO_WAIT); + // If "sent" flag was set, it's last packet and we reached EOF - if (net_pkt_sent(socket->cur_pkt)) { + if (net_pkt_sent(pkt)) { socket->state = STATE_PEER_CLOSED; } - net_pkt_unref(socket->cur_pkt); - socket->cur_pkt = NULL; + net_pkt_unref(pkt); } } // Keep repeating while we're getting empty fragments -- cgit v1.2.3 From 5f648ff60e0e7ff4eaa1add0091962f02256235f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 17 May 2017 00:17:53 +0300 Subject: zephyr/main: Check default netif before applying operations to it. If no network driver is enabled (e.g., it doesn't exist for a particular board), it will be NULL. --- zephyr/main.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'zephyr') diff --git a/zephyr/main.c b/zephyr/main.c index a67ad3352..a4301629f 100644 --- a/zephyr/main.c +++ b/zephyr/main.c @@ -62,6 +62,13 @@ static char heap[MICROPY_HEAP_SIZE]; void init_zephyr(void) { // TODO: Make addresses configurable + #ifdef CONFIG_NETWORKING + if (net_if_get_default() == NULL) { + // If there's no default networking interface, + // there's nothing to configure. + return; + } + #endif #ifdef CONFIG_NET_IPV4 static struct in_addr in4addr_my = {{{192, 0, 2, 1}}}; net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my, NET_ADDR_MANUAL, 0); -- cgit v1.2.3 From 0912334f54f5cdf1d7784032e64e23495d854bed Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 17 May 2017 00:21:22 +0300 Subject: zephyr/modusocket: getaddrinfo: Use RAISE_ERRNO for proper error decoding. --- zephyr/modusocket.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index c4e6529b8..fbc630136 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -572,10 +572,7 @@ STATIC mp_obj_t mod_getaddrinfo(size_t n_args, const mp_obj_t *args) { int status; for (int i = 2; i--;) { int type = (family != AF_INET6 ? DNS_QUERY_TYPE_A : DNS_QUERY_TYPE_AAAA); - status = dns_get_addr_info(host, type, NULL, dns_resolve_cb, &state, 3000); - if (status < 0) { - mp_raise_OSError(status); - } + RAISE_ERRNO(dns_get_addr_info(host, type, NULL, dns_resolve_cb, &state, 3000)); k_sem_take(&state.sem, K_FOREVER); if (family != 0) { break; -- cgit v1.2.3 From bcf31a39087bd7727e15105363063980120fd528 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 16 Apr 2017 11:14:48 +0300 Subject: esp8266, stmhal, zephyr: Rename machine.Pin high/low methods to on/off. For consistent Pin/Signal class hierarchy. With it, Signal is a proper (while still ducktyped) subclass of a Pin, and any (direct) usage of Pin can be replace with Signal. As stmhal's class is reused both as machine.Pin and legacy pyb.Pin, high/low methods actually retained there. --- esp8266/machine_pin.c | 14 ++++++-------- stmhal/pin.c | 19 +++++++++---------- zephyr/machine_pin.c | 14 ++++++-------- 3 files changed, 21 insertions(+), 26 deletions(-) (limited to 'zephyr') diff --git a/esp8266/machine_pin.c b/esp8266/machine_pin.c index 1a263601b..385551578 100644 --- a/esp8266/machine_pin.c +++ b/esp8266/machine_pin.c @@ -336,21 +336,19 @@ STATIC mp_obj_t pyb_pin_value(mp_uint_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_pin_value_obj, 1, 2, pyb_pin_value); -// pin.low() -STATIC mp_obj_t pyb_pin_low(mp_obj_t self_in) { +STATIC mp_obj_t pyb_pin_off(mp_obj_t self_in) { pyb_pin_obj_t *self = self_in; pin_set(self->phys_port, 0); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_low_obj, pyb_pin_low); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_off_obj, pyb_pin_off); -// pin.high() -STATIC mp_obj_t pyb_pin_high(mp_obj_t self_in) { +STATIC mp_obj_t pyb_pin_on(mp_obj_t self_in) { pyb_pin_obj_t *self = self_in; pin_set(self->phys_port, 1); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_high_obj, pyb_pin_high); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_on_obj, pyb_pin_on); // pin.irq(handler=None, trigger=IRQ_FALLING|IRQ_RISING, hard=False) STATIC mp_obj_t pyb_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -410,8 +408,8 @@ STATIC const mp_map_elem_t pyb_pin_locals_dict_table[] = { // instance methods { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_pin_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&pyb_pin_value_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&pyb_pin_low_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&pyb_pin_high_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_off), (mp_obj_t)&pyb_pin_off_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_on), (mp_obj_t)&pyb_pin_on_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_irq), (mp_obj_t)&pyb_pin_irq_obj }, // class constants diff --git a/stmhal/pin.c b/stmhal/pin.c index b8702dae2..db7596900 100644 --- a/stmhal/pin.c +++ b/stmhal/pin.c @@ -400,23 +400,19 @@ STATIC mp_obj_t pin_value(mp_uint_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pin_value_obj, 1, 2, pin_value); -/// \method low() -/// Set the pin to a low logic level. -STATIC mp_obj_t pin_low(mp_obj_t self_in) { +STATIC mp_obj_t pin_off(mp_obj_t self_in) { pin_obj_t *self = self_in; mp_hal_pin_low(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_low_obj, pin_low); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_off_obj, pin_off); -/// \method high() -/// Set the pin to a high logic level. -STATIC mp_obj_t pin_high(mp_obj_t self_in) { +STATIC mp_obj_t pin_on(mp_obj_t self_in) { pin_obj_t *self = self_in; mp_hal_pin_high(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_high_obj, pin_high); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_on_obj, pin_on); /// \method name() /// Get the pin name. @@ -500,8 +496,11 @@ STATIC const mp_rom_map_elem_t pin_locals_dict_table[] = { // instance methods { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pin_init_obj) }, { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&pin_value_obj) }, - { MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&pin_low_obj) }, - { MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&pin_high_obj) }, + { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&pin_off_obj) }, + { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&pin_on_obj) }, + // Legacy names as used by pyb.Pin + { MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&pin_off_obj) }, + { MP_ROM_QSTR(MP_QSTR_high), MP_ROM_PTR(&pin_on_obj) }, { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&pin_name_obj) }, { MP_ROM_QSTR(MP_QSTR_names), MP_ROM_PTR(&pin_names_obj) }, { MP_ROM_QSTR(MP_QSTR_af_list), MP_ROM_PTR(&pin_af_list_obj) }, diff --git a/zephyr/machine_pin.c b/zephyr/machine_pin.c index 5bf273909..f011f51fd 100644 --- a/zephyr/machine_pin.c +++ b/zephyr/machine_pin.c @@ -138,21 +138,19 @@ STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value); -// pin.low() -STATIC mp_obj_t machine_pin_low(mp_obj_t self_in) { +STATIC mp_obj_t machine_pin_off(mp_obj_t self_in) { machine_pin_obj_t *self = self_in; (void)gpio_pin_write(self->port, self->pin, 0); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_off_obj, machine_pin_off); -// pin.high() -STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { +STATIC mp_obj_t machine_pin_on(mp_obj_t self_in) { machine_pin_obj_t *self = self_in; (void)gpio_pin_write(self->port, self->pin, 1); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_on_obj, machine_pin_on); STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { (void)errcode; @@ -176,8 +174,8 @@ STATIC const mp_map_elem_t machine_pin_locals_dict_table[] = { // instance methods { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&machine_pin_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&machine_pin_value_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_low), (mp_obj_t)&machine_pin_low_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_high), (mp_obj_t)&machine_pin_high_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_off), (mp_obj_t)&machine_pin_off_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_on), (mp_obj_t)&machine_pin_on_obj }, // class constants { MP_OBJ_NEW_QSTR(MP_QSTR_IN), MP_OBJ_NEW_SMALL_INT(GPIO_DIR_IN) }, -- cgit v1.2.3 From 982e67690216a04d82c9cfcb07e8c5e3dbc4d964 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 24 May 2017 19:54:26 +0300 Subject: zephyr/modusocket: getaddrinfo: Raise OSError on resolution timeout, etc. --- zephyr/modusocket.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index fbc630136..80339a2bd 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -533,12 +533,18 @@ typedef struct _getaddrinfo_state_t { mp_obj_t result; struct k_sem sem; mp_obj_t port; + int status; } getaddrinfo_state_t; void dns_resolve_cb(enum dns_resolve_status status, struct dns_addrinfo *info, void *user_data) { getaddrinfo_state_t *state = user_data; + DEBUG_printf("dns status: %d\n", status); if (info == NULL) { + if (status == DNS_EAI_ALLDONE) { + status = 0; + } + state->status = status; k_sem_give(&state->sem); return; } @@ -569,7 +575,6 @@ STATIC mp_obj_t mod_getaddrinfo(size_t n_args, const mp_obj_t *args) { state.result = mp_obj_new_list(0, NULL); k_sem_init(&state.sem, 0, UINT_MAX); - int status; for (int i = 2; i--;) { int type = (family != AF_INET6 ? DNS_QUERY_TYPE_A : DNS_QUERY_TYPE_AAAA); RAISE_ERRNO(dns_get_addr_info(host, type, NULL, dns_resolve_cb, &state, 3000)); @@ -580,6 +585,12 @@ STATIC mp_obj_t mod_getaddrinfo(size_t n_args, const mp_obj_t *args) { family = AF_INET6; } + // Raise error only if there's nothing to return, otherwise + // it may be IPv4 vs IPv6 differences. + if (state.status != 0 && mp_obj_len(state.result) == 0) { + mp_raise_OSError(state.status); + } + return state.result; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_getaddrinfo_obj, 2, 3, mod_getaddrinfo); -- cgit v1.2.3 From 011684bd4f92c545cb792b582d7556ab394e2cf0 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 25 May 2017 11:41:02 +0300 Subject: zephyr/modusocket: Use DEBUG_PRINT macro name as other modules do. Indeed, just "DEBUG" is too generic. --- zephyr/modusocket.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 80339a2bd..0a54ea576 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -38,8 +38,8 @@ #include #include -#define DEBUG 0 -#if DEBUG // print debugging info +#define DEBUG_PRINT 0 +#if DEBUG_PRINT // print debugging info #define DEBUG_printf printf #else // don't print debugging info #define DEBUG_printf(...) (void)0 @@ -165,7 +165,7 @@ static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, i DEBUG_printf(" (appdatalen=%d), token: %p", pkt->appdatalen, net_pkt_token(pkt)); } DEBUG_printf("\n"); - #if DEBUG > 1 + #if DEBUG_PRINT > 1 net_pkt_print_frags(pkt); #endif -- cgit v1.2.3 From ca16c3821053e5bf2b87aeb10007f73f31dc1eac Mon Sep 17 00:00:00 2001 From: Ville Skyttä Date: Mon, 29 May 2017 10:08:14 +0300 Subject: various: Spelling fixes --- cc3200/README.md | 2 +- docs/library/btree.rst | 2 +- docs/library/machine.SD.rst | 2 +- docs/library/machine.UART.rst | 2 +- docs/library/uhashlib.rst | 4 ++-- docs/library/utime.rst | 4 ++-- docs/sphinx_selective_exclude/README.md | 2 +- docs/sphinx_selective_exclude/modindex_exclude.py | 2 +- esp8266/README.md | 2 +- esp8266/machine_rtc.c | 2 +- examples/conwaylife.py | 4 ++-- examples/embedding/Makefile.upylib | 2 +- examples/embedding/README.md | 2 +- extmod/modlwip.c | 4 ++-- extmod/modwebsocket.c | 2 +- lib/timeutils/timeutils.c | 2 +- lib/utils/stdout_helpers.c | 2 +- py/asmthumb.c | 2 +- py/builtinimport.c | 4 ++-- py/compile.c | 4 ++-- py/misc.h | 2 +- py/mkenv.mk | 2 +- py/mpconfig.h | 2 +- py/obj.c | 2 +- py/objstr.c | 4 ++-- py/py.mk | 2 +- py/ringbuf.h | 2 +- py/stream.c | 2 +- py/vm.c | 4 ++-- qemu-arm/README.md | 2 +- tests/basics/namedtuple1.py | 2 +- tests/basics/try_reraise2.py | 2 +- tests/pyb/can.py | 2 +- tests/thread/stress_aes.py | 2 +- tests/wipy/uart.py | 2 +- tools/insert-usb-ids.py | 2 +- tools/pyboard.py | 2 +- unix/Makefile | 2 +- unix/modsocket.c | 2 +- windows/windows_mphal.c | 2 +- zephyr/modutime.c | 2 +- 41 files changed, 49 insertions(+), 49 deletions(-) (limited to 'zephyr') diff --git a/cc3200/README.md b/cc3200/README.md index 753fd450a..53cad3ba0 100644 --- a/cc3200/README.md +++ b/cc3200/README.md @@ -138,7 +138,7 @@ If `WIPY_IP`, `WIPY_USER` or `WIPY_PWD` are omitted the default values (the ones ## Regarding old revisions of the CC3200-LAUNCHXL First silicon (pre-release) revisions of the CC3200 had issues with the ram blocks, and MicroPython cannot run -there. Make sure to use a **v4.1 (or higer) LAUNCHXL board** when trying this port, otherwise it won't work. +there. Make sure to use a **v4.1 (or higher) LAUNCHXL board** when trying this port, otherwise it won't work. ### Note regarding FileZilla diff --git a/docs/library/btree.rst b/docs/library/btree.rst index aebcbc160..bd7890586 100644 --- a/docs/library/btree.rst +++ b/docs/library/btree.rst @@ -69,7 +69,7 @@ Functions Open a database from a random-access `stream` (like an open file). All other parameters are optional and keyword-only, and allow to tweak advanced - paramters of the database operation (most users will not need them): + parameters of the database operation (most users will not need them): * `flags` - Currently unused. * `cachesize` - Suggested maximum memory cache size in bytes. For a diff --git a/docs/library/machine.SD.rst b/docs/library/machine.SD.rst index 0eb024602..608e95831 100644 --- a/docs/library/machine.SD.rst +++ b/docs/library/machine.SD.rst @@ -34,7 +34,7 @@ Methods .. method:: SD.init(id=0, pins=('GP10', 'GP11', 'GP15')) - Enable the SD card. In order to initalize the card, give it a 3-tuple: + Enable the SD card. In order to initialize the card, give it a 3-tuple: ``(clk_pin, cmd_pin, dat0_pin)``. .. method:: SD.deinit() diff --git a/docs/library/machine.UART.rst b/docs/library/machine.UART.rst index f9c8efef7..64ff28e1a 100644 --- a/docs/library/machine.UART.rst +++ b/docs/library/machine.UART.rst @@ -16,7 +16,7 @@ UART objects can be created and initialised using:: uart = UART(1, 9600) # init with given baudrate uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters -Supported paramters differ on a board: +Supported parameters differ on a board: Pyboard: Bits can be 7, 8 or 9. Stop can be 1 or 2. With `parity=None`, only 8 and 9 bits are supported. With parity enabled, only 7 and 8 bits diff --git a/docs/library/uhashlib.rst b/docs/library/uhashlib.rst index cd0216dae..6b9a764ba 100644 --- a/docs/library/uhashlib.rst +++ b/docs/library/uhashlib.rst @@ -15,11 +15,11 @@ be implemented: * SHA1 - A previous generation algorithm. Not recommended for new usages, but SHA1 is a part of number of Internet standards and existing - applications, so boards targetting network connectivity and + applications, so boards targeting network connectivity and interoperatiability will try to provide this. * MD5 - A legacy algorithm, not considered cryptographically secure. Only - selected boards, targetting interoperatibility with legacy applications, + selected boards, targeting interoperatibility with legacy applications, will offer this. Constructors diff --git a/docs/library/utime.rst b/docs/library/utime.rst index 871f6c678..f3a067cde 100644 --- a/docs/library/utime.rst +++ b/docs/library/utime.rst @@ -146,8 +146,8 @@ Functions too distant inbetween, see below). The function returns **signed** value in the range [``-TICKS_PERIOD/2`` .. ``TICKS_PERIOD/2-1``] (that's a typical range definition for two's-complement signed binary integers). If the result is negative, it means that - ``ticks1`` occured earlier in time than ``ticks2``. Otherwise, it means that - ``ticks1`` occured after ``ticks2``. This holds ``only`` if ``ticks1`` and ``ticks2`` + ``ticks1`` occurred earlier in time than ``ticks2``. Otherwise, it means that + ``ticks1`` occurred after ``ticks2``. This holds ``only`` if ``ticks1`` and ``ticks2`` are apart from each other for no more than ``TICKS_PERIOD/2-1`` ticks. If that does not hold, incorrect result will be returned. Specifically, if two tick values are apart for ``TICKS_PERIOD/2-1`` ticks, that value will be returned by the function. diff --git a/docs/sphinx_selective_exclude/README.md b/docs/sphinx_selective_exclude/README.md index cc9725c21..dab140739 100644 --- a/docs/sphinx_selective_exclude/README.md +++ b/docs/sphinx_selective_exclude/README.md @@ -66,7 +66,7 @@ index for PDF, just the same as for HTML. search_auto_exclude ------------------- -Even if you exclude soem documents from toctree:: using only:: +Even if you exclude some documents from toctree:: using only:: directive, they will be indexed for full-text search, so user may find them and get confused. This plugin follows very simple idea that if you didn't include some documents in the toctree, then diff --git a/docs/sphinx_selective_exclude/modindex_exclude.py b/docs/sphinx_selective_exclude/modindex_exclude.py index 18b49cc80..bf8db795e 100644 --- a/docs/sphinx_selective_exclude/modindex_exclude.py +++ b/docs/sphinx_selective_exclude/modindex_exclude.py @@ -2,7 +2,7 @@ # This is a Sphinx documentation tool extension which allows to # exclude some Python modules from the generated indexes. Modules # are excluded both from "modindex" and "genindex" index tables -# (in the latter case, all members of a module are exlcuded). +# (in the latter case, all members of a module are excluded). # To control exclusion, set "modindex_exclude" variable in Sphinx # conf.py to the list of modules to exclude. Note: these should be # modules (as defined by py:module directive, not just raw filenames). diff --git a/esp8266/README.md b/esp8266/README.md index 897bb4737..d717d26fe 100644 --- a/esp8266/README.md +++ b/esp8266/README.md @@ -100,7 +100,7 @@ programming). __WiFi__ -Initally, the device configures itself as a WiFi access point (AP). +Initially, the device configures itself as a WiFi access point (AP). - ESSID: MicroPython-xxxxxx (x’s are replaced with part of the MAC address). - Password: micropythoN (note the upper-case N). - IP address of the board: 192.168.4.1. diff --git a/esp8266/machine_rtc.c b/esp8266/machine_rtc.c index 019b705ba..b17bcb261 100644 --- a/esp8266/machine_rtc.c +++ b/esp8266/machine_rtc.c @@ -93,7 +93,7 @@ void pyb_rtc_set_us_since_2000(uint64_t nowus) { int64_t delta = nowus - (((uint64_t)rtc_last_ticks * cal) >> 12); // As the calibration value jitters quite a bit, to make the - // clock at least somewhat practially usable, we need to store it + // clock at least somewhat practically usable, we need to store it system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal)); system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta)); }; diff --git a/examples/conwaylife.py b/examples/conwaylife.py index f99796175..323f42e85 100644 --- a/examples/conwaylife.py +++ b/examples/conwaylife.py @@ -8,7 +8,7 @@ lcd.light(1) def conway_step(): for x in range(128): # loop over x coordinates for y in range(32): # loop over y coordinates - # count number of neigbours + # count number of neighbours num_neighbours = (lcd.get(x - 1, y - 1) + lcd.get(x, y - 1) + lcd.get(x + 1, y - 1) + @@ -25,7 +25,7 @@ def conway_step(): if self and not (2 <= num_neighbours <= 3): lcd.pixel(x, y, 0) # not enough, or too many neighbours: cell dies elif not self and num_neighbours == 3: - lcd.pixel(x, y, 1) # exactly 3 neigbours around an empty cell: cell is born + lcd.pixel(x, y, 1) # exactly 3 neighbours around an empty cell: cell is born # randomise the start def conway_rand(): diff --git a/examples/embedding/Makefile.upylib b/examples/embedding/Makefile.upylib index 873c0fd34..4663ad30a 100644 --- a/examples/embedding/Makefile.upylib +++ b/examples/embedding/Makefile.upylib @@ -170,7 +170,7 @@ SRC_QSTR_AUTO_DEPS += include $(MPTOP)/py/mkrules.mk # Value of configure's --host= option (required for cross-compilation). -# Deduce it from CROSS_COMPILE by default, but can be overriden. +# Deduce it from CROSS_COMPILE by default, but can be overridden. ifneq ($(CROSS_COMPILE),) CROSS_COMPILE_HOST = --host=$(patsubst %-,%,$(CROSS_COMPILE)) else diff --git a/examples/embedding/README.md b/examples/embedding/README.md index 989ce1fc8..804dfede6 100644 --- a/examples/embedding/README.md +++ b/examples/embedding/README.md @@ -18,7 +18,7 @@ Building the example is as simple as running: It's worth to trace what's happening behind the scenes though: 1. As a first step, a MicroPython library is built. This is handled by a -seperate makefile, Makefile.upylib. It is more or less complex, but the +separate makefile, Makefile.upylib. It is more or less complex, but the good news is that you won't need to change anything in it, just use it as is, the main Makefile shows how. What may require editing though is a MicroPython configuration file. MicroPython is highly configurable, so diff --git a/extmod/modlwip.c b/extmod/modlwip.c index c72849cf9..47669cb3a 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -373,7 +373,7 @@ STATIC err_t _lwip_tcp_recv(void *arg, struct tcp_pcb *tcpb, struct pbuf *p, err } /*******************************************************************************/ -// Functions for socket send/recieve operations. Socket send/recv and friends call +// Functions for socket send/receive operations. Socket send/recv and friends call // these to do the work. // Helper function for send/sendto to handle UDP packets. @@ -805,7 +805,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { mp_raise_OSError(MP_EINPROGRESS); } } - // Register our recieve callback. + // Register our receive callback. tcp_recv(socket->pcb.tcp, _lwip_tcp_recv); socket->state = STATE_CONNECTING; err = tcp_connect(socket->pcb.tcp, &dest, port, _lwip_tcp_connected); diff --git a/extmod/modwebsocket.c b/extmod/modwebsocket.c index 8200ea708..9e17d6a6d 100644 --- a/extmod/modwebsocket.c +++ b/extmod/modwebsocket.c @@ -132,7 +132,7 @@ STATIC mp_uint_t websocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int self->buf_pos = 0; self->to_recv = to_recv; - self->msg_sz = sz; // May be overriden by FRAME_OPT + self->msg_sz = sz; // May be overridden by FRAME_OPT if (to_recv != 0) { self->state = FRAME_OPT; } else { diff --git a/lib/timeutils/timeutils.c b/lib/timeutils/timeutils.c index 0af39a295..06915f25a 100644 --- a/lib/timeutils/timeutils.c +++ b/lib/timeutils/timeutils.c @@ -165,7 +165,7 @@ mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday, // // tm_tomorrow = list(time.localtime()) // tm_tomorrow[2] += 1 # Adds 1 to mday - // tomorrow = time.mktime(tm_tommorrow) + // tomorrow = time.mktime(tm_tomorrow) // // And not have to worry about all the weird overflows. // diff --git a/lib/utils/stdout_helpers.c b/lib/utils/stdout_helpers.c index 5f7a17d32..3de119757 100644 --- a/lib/utils/stdout_helpers.c +++ b/lib/utils/stdout_helpers.c @@ -9,7 +9,7 @@ * implementation below can be used. */ -// Send "cooked" string of given length, where every occurance of +// Send "cooked" string of given length, where every occurrence of // LF character is replaced with CR LF. void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { while (len--) { diff --git a/py/asmthumb.c b/py/asmthumb.c index 749c1e405..7e92e4de4 100644 --- a/py/asmthumb.c +++ b/py/asmthumb.c @@ -52,7 +52,7 @@ void asm_thumb_end_pass(asm_thumb_t *as) { #if defined(MCU_SERIES_F7) if (as->base.pass == MP_ASM_PASS_EMIT) { - // flush D-cache, so the code emited is stored in memory + // flush D-cache, so the code emitted is stored in memory SCB_CleanDCache_by_Addr((uint32_t*)as->base.code_base, as->base.code_size); // invalidate I-cache SCB_InvalidateICache(); diff --git a/py/builtinimport.c b/py/builtinimport.c index d01ebbe73..6994fc48f 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -271,7 +271,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { if (level != 0) { // What we want to do here is to take name of current module, // chop trailing components, and concatenate with passed-in - // module name, thus resolving relative import name into absolue. + // module name, thus resolving relative import name into absolute. // This even appears to be correct per // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name // "Relative imports use a module's __name__ attribute to determine that @@ -441,7 +441,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { #if MICROPY_CPYTHON_COMPAT // Store module as "__main__" in the dictionary of loaded modules (returned by sys.modules). mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)), MP_OBJ_NEW_QSTR(MP_QSTR___main__), module_obj); - // Store real name in "__main__" attribute. Choosen semi-randonly, to reuse existing qstr's. + // Store real name in "__main__" attribute. Chosen semi-randonly, to reuse existing qstr's. mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___main__), MP_OBJ_NEW_QSTR(mod_name)); #endif } diff --git a/py/compile.c b/py/compile.c index 8533e0528..3b6a264d6 100644 --- a/py/compile.c +++ b/py/compile.c @@ -939,7 +939,7 @@ STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) { } } } else { - // some arbitrary statment that we can't delete (eg del 1) + // some arbitrary statement that we can't delete (eg del 1) goto cannot_delete; } @@ -1090,7 +1090,7 @@ STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) { STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) { mp_parse_node_t pn_import_source = pns->nodes[0]; - // extract the preceeding .'s (if any) for a relative import, to compute the import level + // extract the preceding .'s (if any) for a relative import, to compute the import level uint import_level = 0; do { mp_parse_node_t pn_rel; diff --git a/py/misc.h b/py/misc.h index 146b9a8e4..caa5945bf 100644 --- a/py/misc.h +++ b/py/misc.h @@ -197,7 +197,7 @@ int DEBUG_printf(const char *fmt, ...); extern mp_uint_t mp_verbose_flag; // This is useful for unicode handling. Some CPU archs has -// special instructions for efficient implentation of this +// special instructions for efficient implementation of this // function (e.g. CLZ on ARM). // NOTE: this function is unused at the moment #ifndef count_lead_ones diff --git a/py/mkenv.mk b/py/mkenv.mk index eb1e44fef..b167b2533 100644 --- a/py/mkenv.mk +++ b/py/mkenv.mk @@ -32,7 +32,7 @@ ifeq ($(BUILD_VERBOSE),0) $(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.) endif -# default settings; can be overriden in main Makefile +# default settings; can be overridden in main Makefile PY_SRC ?= $(TOP)/py BUILD ?= build diff --git a/py/mpconfig.h b/py/mpconfig.h index a61d431e5..78e346d73 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -32,7 +32,7 @@ // mpconfigport.h is a file containing configuration settings for a // particular port. mpconfigport.h is actually a default name for -// such config, and it can be overriden using MP_CONFIGFILE preprocessor +// such config, and it can be overridden using MP_CONFIGFILE preprocessor // define (you can do that by passing CFLAGS_EXTRA='-DMP_CONFIGFILE=""' // argument to make when using standard MicroPython makefiles). // This is useful to have more than one config per port, for example, diff --git a/py/obj.c b/py/obj.c index 98ffa930b..493945a22 100644 --- a/py/obj.c +++ b/py/obj.c @@ -401,7 +401,7 @@ mp_obj_t mp_obj_id(mp_obj_t o_in) { return MP_OBJ_NEW_SMALL_INT(id); } else { // If that didn't work, well, let's return long int, just as - // a (big) positve value, so it will never clash with the range + // a (big) positive value, so it will never clash with the range // of small int returned in previous case. return mp_obj_new_int_from_uint((mp_uint_t)id); } diff --git a/py/objstr.c b/py/objstr.c index 70de0a693..a1e223572 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -798,7 +798,7 @@ STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) { } assert(last_good_char_pos >= first_good_char_pos); - //+1 to accomodate the last character + //+1 to accommodate the last character size_t stripped_len = last_good_char_pos - first_good_char_pos + 1; if (stripped_len == orig_str_len) { // If nothing was stripped, don't bother to dup original string @@ -1811,7 +1811,7 @@ STATIC mp_obj_t str_islower(mp_obj_t self_in) { } #if MICROPY_CPYTHON_COMPAT -// These methods are superfluous in the presense of str() and bytes() +// These methods are superfluous in the presence of str() and bytes() // constructors. // TODO: should accept kwargs too STATIC mp_obj_t bytes_decode(size_t n_args, const mp_obj_t *args) { diff --git a/py/py.mk b/py/py.mk index 5ff1fd6a6..70891677d 100644 --- a/py/py.mk +++ b/py/py.mk @@ -25,7 +25,7 @@ ifeq ($(MICROPY_SSL_AXTLS),1) CFLAGS_MOD += -DMICROPY_SSL_AXTLS=1 -I../lib/axtls/ssl -I../lib/axtls/crypto -I../lib/axtls/config LDFLAGS_MOD += -Lbuild -laxtls else ifeq ($(MICROPY_SSL_MBEDTLS),1) -# Can be overriden by ports which have "builtin" mbedTLS +# Can be overridden by ports which have "builtin" mbedTLS MICROPY_SSL_MBEDTLS_INCLUDE ?= ../lib/mbedtls/include CFLAGS_MOD += -DMICROPY_SSL_MBEDTLS=1 -I$(MICROPY_SSL_MBEDTLS_INCLUDE) LDFLAGS_MOD += -L../lib/mbedtls/library -lmbedx509 -lmbedtls -lmbedcrypto diff --git a/py/ringbuf.h b/py/ringbuf.h index 5662594f7..5e108afad 100644 --- a/py/ringbuf.h +++ b/py/ringbuf.h @@ -33,7 +33,7 @@ typedef struct _ringbuf_t { uint16_t iput; } ringbuf_t; -// Static initalization: +// Static initialization: // byte buf_array[N]; // ringbuf_t buf = {buf_array, sizeof(buf_array)}; diff --git a/py/stream.c b/py/stream.c index c915110e0..d3fc767bb 100644 --- a/py/stream.c +++ b/py/stream.c @@ -51,7 +51,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in); #define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes) // Returns error condition in *errcode, if non-zero, return value is number of bytes written -// before error condition occured. If *errcode == 0, returns total bytes written (which will +// before error condition occurred. If *errcode == 0, returns total bytes written (which will // be equal to input size). mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode, byte flags) { byte *buf = buf_; diff --git a/py/vm.c b/py/vm.c index 5094e3e45..ad3d9e29c 100644 --- a/py/vm.c +++ b/py/vm.c @@ -947,7 +947,7 @@ unwind_jump:; DECODE_UINT; // unum & 0xff == n_positional // (unum >> 8) & 0xff == n_keyword - // We have folowing stack layout here: + // We have following stack layout here: // fun arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 2; #if MICROPY_STACKLESS @@ -1018,7 +1018,7 @@ unwind_jump:; DECODE_UINT; // unum & 0xff == n_positional // (unum >> 8) & 0xff == n_keyword - // We have folowing stack layout here: + // We have following stack layout here: // fun self arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 3; #if MICROPY_STACKLESS diff --git a/qemu-arm/README.md b/qemu-arm/README.md index 329ae4d92..0cf93c7d5 100644 --- a/qemu-arm/README.md +++ b/qemu-arm/README.md @@ -4,7 +4,7 @@ provided by QEMU (http://qemu.org). The purposes of this port are to enable: 1. Continuous integration - - run tests agains architecture-specific parts of code base + - run tests against architecture-specific parts of code base 2. Experimentation - simulation & prototyping of anything that has architecture-specific code diff --git a/tests/basics/namedtuple1.py b/tests/basics/namedtuple1.py index 132dcf96b..70372f7ca 100644 --- a/tests/basics/namedtuple1.py +++ b/tests/basics/namedtuple1.py @@ -76,7 +76,7 @@ T4 = namedtuple("TupTuple", ("foo", "bar")) t = T4(1, 2) print(t.foo, t.bar) -# Try single string with comma field seperator +# Try single string with comma field separator # Not implemented so far #T2 = namedtuple("TupComma", "foo,bar") #t = T2(1, 2) diff --git a/tests/basics/try_reraise2.py b/tests/basics/try_reraise2.py index d9434397c..5648d2467 100644 --- a/tests/basics/try_reraise2.py +++ b/tests/basics/try_reraise2.py @@ -1,4 +1,4 @@ -# Reraise not the latest occured exception +# Reraise not the latest occurred exception def f(): try: raise ValueError("val", 3) diff --git a/tests/pyb/can.py b/tests/pyb/can.py index 617eb7ccc..7f2d070ec 100644 --- a/tests/pyb/can.py +++ b/tests/pyb/can.py @@ -158,7 +158,7 @@ print(can.recv(1)) del can -# Testing asyncronous send +# Testing asynchronous send can = CAN(1, CAN.LOOPBACK) can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0)) diff --git a/tests/thread/stress_aes.py b/tests/thread/stress_aes.py index ecc963c92..df75e616c 100644 --- a/tests/thread/stress_aes.py +++ b/tests/thread/stress_aes.py @@ -8,7 +8,7 @@ # # The AES code comes first (code originates from a C version authored by D.P.George) # and then the test harness at the bottom. It can be tuned to be more/less -# agressive by changing the amount of data to encrypt, the number of loops and +# aggressive by changing the amount of data to encrypt, the number of loops and # the number of threads. # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd diff --git a/tests/wipy/uart.py b/tests/wipy/uart.py index a3a1c14e8..8e794015d 100644 --- a/tests/wipy/uart.py +++ b/tests/wipy/uart.py @@ -95,7 +95,7 @@ print(uart1.read() == None) print(uart1.write(b'123') == 3) print(uart0.read() == b'123') -# no pin assignemnt +# no pin assignment uart0 = UART(0, 1000000, pins=(None, None)) print(uart0.write(b'123456789') == 9) print(uart1.read() == None) diff --git a/tools/insert-usb-ids.py b/tools/insert-usb-ids.py index 420db34c5..cdccd3be9 100644 --- a/tools/insert-usb-ids.py +++ b/tools/insert-usb-ids.py @@ -1,4 +1,4 @@ -# Reads the USB VID and PID from the file specifed by sys.arg[1] and then +# Reads the USB VID and PID from the file specified by sys.argv[1] and then # inserts those values into the template file specified by sys.argv[2], # printing the result to stdout diff --git a/tools/pyboard.py b/tools/pyboard.py index 5eac030bd..921ffc52d 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -69,7 +69,7 @@ class TelnetToSerial: self.tn.write(bytes(password, 'ascii') + b"\r\n") if b'for more information.' in self.tn.read_until(b'Type "help()" for more information.', timeout=read_timeout): - # login succesful + # login successful from collections import deque self.fifo = deque() return diff --git a/unix/Makefile b/unix/Makefile index 837ddf2b7..006bce0ef 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -262,7 +262,7 @@ coverage_test: coverage gcov -o build-coverage/extmod ../extmod/*.c # Value of configure's --host= option (required for cross-compilation). -# Deduce it from CROSS_COMPILE by default, but can be overriden. +# Deduce it from CROSS_COMPILE by default, but can be overridden. ifneq ($(CROSS_COMPILE),) CROSS_COMPILE_HOST = --host=$(patsubst %-,%,$(CROSS_COMPILE)) else diff --git a/unix/modsocket.c b/unix/modsocket.c index 9ca04b88b..c7be6461e 100644 --- a/unix/modsocket.c +++ b/unix/modsocket.c @@ -58,7 +58,7 @@ from socket_more_funcs2 import * ------------------- I.e. this module should stay lean, and more functions (if needed) - should be add to seperate modules (C or Python level). + should be add to separate modules (C or Python level). */ #define MICROPY_SOCKET_EXTRA (0) diff --git a/windows/windows_mphal.c b/windows/windows_mphal.c index 1dd3105d8..a73140e54 100644 --- a/windows/windows_mphal.c +++ b/windows/windows_mphal.c @@ -72,7 +72,7 @@ void mp_hal_stdio_mode_orig(void) { // Previous versions of the mp_hal code would install a handler whenever Ctrl-C input is // allowed and remove the handler again when it is not. That is not necessary though (1), // and it might introduce problems (2) because console notifications are delivered to the -// application in a seperate thread. +// application in a separate thread. // (1) mp_hal_set_interrupt_char effectively enables/disables processing of Ctrl-C via the // ENABLE_PROCESSED_INPUT flag so in raw mode console_sighandler won't be called. // (2) if mp_hal_set_interrupt_char would remove the handler while Ctrl-C was issued earlier, diff --git a/zephyr/modutime.c b/zephyr/modutime.c index 378068bb3..0c268046a 100644 --- a/zephyr/modutime.c +++ b/zephyr/modutime.c @@ -36,7 +36,7 @@ #include "extmod/utime_mphal.h" STATIC mp_obj_t mod_time_time(void) { - /* The absense of FP support is deliberate. The Zephyr port uses + /* The absence of FP support is deliberate. The Zephyr port uses * single precision floats so the fraction component will start to * lose precision on devices with a long uptime. */ -- cgit v1.2.3 From 43f58386e0686943c053d529a1e5f39ba5d7e3ad Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 3 Jun 2017 16:10:12 +0300 Subject: zephyr/modusocket: getaddrinfo: Fix mp_obj_len() usage. Return value is mp_obj_t, so needs to be accessed using MP_OBJ_SMALL_INT_VALUE(). --- zephyr/modusocket.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'zephyr') diff --git a/zephyr/modusocket.c b/zephyr/modusocket.c index 0a54ea576..cec0eec7c 100644 --- a/zephyr/modusocket.c +++ b/zephyr/modusocket.c @@ -587,7 +587,8 @@ STATIC mp_obj_t mod_getaddrinfo(size_t n_args, const mp_obj_t *args) { // Raise error only if there's nothing to return, otherwise // it may be IPv4 vs IPv6 differences. - if (state.status != 0 && mp_obj_len(state.result) == 0) { + mp_int_t len = MP_OBJ_SMALL_INT_VALUE(mp_obj_len(state.result)); + if (state.status != 0 && len == 0) { mp_raise_OSError(state.status); } -- cgit v1.2.3 From 4352b944d2fe0919b34774bec1a6ea440649a1df Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 8 Jun 2017 17:58:13 +0300 Subject: zephyr: Define MICROPY_PY_SYS_PLATFORM (to "zephyr"). --- zephyr/mpconfigport.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'zephyr') diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index 98a033be4..2f2526779 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -80,6 +80,8 @@ #define MICROPY_COMP_CONST (0) #define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0) +#define MICROPY_PY_SYS_PLATFORM "zephyr" + #ifdef CONFIG_BOARD #define MICROPY_HW_BOARD_NAME "zephyr-" CONFIG_BOARD #else -- cgit v1.2.3 From 0161939ed1d99382296fee3827916d1fa58d3c20 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 10 Jun 2017 19:40:31 +0300 Subject: zephyr/machine_pin: Use native Zephyr types for Zephyr API calls. In 1.8, Zephyr made a controversial change of dumping C99 stdint types and switching to its own types. --- zephyr/machine_pin.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'zephyr') diff --git a/zephyr/machine_pin.c b/zephyr/machine_pin.c index f011f51fd..38971399c 100644 --- a/zephyr/machine_pin.c +++ b/zephyr/machine_pin.c @@ -117,7 +117,7 @@ STATIC mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, c mp_arg_check_num(n_args, n_kw, 0, 1, false); machine_pin_obj_t *self = self_in; if (n_args == 0) { - uint32_t pin_val; + u32_t pin_val; (void)gpio_pin_read(self->port, self->pin, &pin_val); return MP_OBJ_NEW_SMALL_INT(pin_val); } else { @@ -158,7 +158,7 @@ STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_ switch (request) { case MP_PIN_READ: { - uint32_t pin_val; + u32_t pin_val; gpio_pin_read(self->port, self->pin, &pin_val); return pin_val; } -- cgit v1.2.3