summaryrefslogtreecommitdiffstatshomepage
path: root/esp8266
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-08-14 12:15:54 +1000
committerDamien George <damien.p.george@gmail.com>2017-08-14 12:15:54 +1000
commit5e76ea4affd0bd46e67b456496818803010a2d24 (patch)
tree2be81a889e765b8e24614bcd8bc80c0a4063e2f3 /esp8266
parentb4078cbbf3354229fc81ecfd26295237d808caa9 (diff)
parent1459a8d5c9b29c78da2cf5c7cf3c37ab03b34b8e (diff)
downloadmicropython-5e76ea4affd0bd46e67b456496818803010a2d24.tar.gz
micropython-5e76ea4affd0bd46e67b456496818803010a2d24.zip
Merge tag 'v1.8.2' into parse-bytecode
Thread support, ESP8266 memory improvements, btree module, improved docs This release brings multi-threading support in the form of the _thread module, which closely matches the semantics of the corresponding CPython module. There is support for GIL and non-GIL builds; without the GIL enabled one must protect concurrent access to mutable Python state at the Python level using Lock objects. Threading with the GIL is enabled in the cc3200 port on the WiPy. Threading without the GIL is enabled on the unix port. The ESP8266 port has support for frozen bytecode (for scripts in the modules/ subdirectory), as well as optimisations for reduced memory usage and decreased memory fragmentation. The ESP8266 RTC also resumes correctly after a deepsleep. An initial "btree" module has been implemented for database support (unix port only), and the documentation has been further improved, with pre-built PDF versions of the docs now available. py core: - parse: treat constants that start with underscore as private - objdict: implemented OrderedDict equality check - support to build berkeley db 1.85 and "btree" module - mpconfig.h: MP_NOINLINE is universally useful, move from unix port - makeqstrdefs.py: remove restriction that source path can't be absolute - mkrules.mk: define "lib" outside conditional block - rename __QSTR_EXTRACT flag to NO_QSTR - objtype: instance: inherit protocol vtable from a base class - mphal.h: if virtpin API is used, automagically include its header - objtype: inherit protocol vtable from base class only if it exists - add MP_STATE_THREAD to hold state specific to a given thread - add basic _thread module, with ability to start a new thread - modthread: properly cast concrete exception pointer to an object - modthread: add stack_size() function - modthread: add exit() function - modthread: implement lock object, for creating a mutex - modthread: add with-context capabilities to lock object - gc: make memory manager and garbage collector thread safe - modthread: satisfy unused-args warning - gc: fix 2 cases of concurrent access to ATB and FTB - modthread: be more careful with root pointers when creating a thread - modthread: call mp_thread_start/mp_thread_finish around threads - gc: fix GC+thread bug where ptr gets lost because it's not computed - make interning of qstrs thread safe - implement a simple global interpreter lock (GIL) - don't use gc or qstr mutex when the GIL is enabled, there is no need - modthread: make Lock objects work when GIL is enabled - mpthread.h: move GIL macros outside MICROPY_PY_THREAD block - modthread: allow to properly set the stack limit of a thread - nlrthumb: convert NLR thumb funcs from asm to C with inline-asm - nlrsetjmp: update to take into account new location of nlr_top - mpthread: include mpstate.h when defining GIL macros - gc: be sure to count last allocated block at heap end in stats - gc: calculate (and report) maximum contiguous free block size - builtinimport: disable "imported as namespace package" warning - mpconfig.h: mention MICROPY_PY_BTREE config option - objarray: split out header to allow direct access to object - rename mp_obj_type_t::stream_p to "protocol" extmod: - modbtree: initial implementation of "btree" module based on BerkeleyDB - modbtree: handle default value and error check - modbtree: implement .items() iterator - modbtree: actually implement end key support for .items() - modbtree: items(): Implement "end key inclusive" flag - modbtree: items(): Implement DESC flag - modbtree: __getitem__() should raise KeyError for non-existing key - modbtree: open(): Support "in-memory" database with filename=None - machine_pinbase: implementation of PinBase class - machine_pinbase: fix nanbox build - modlwip: store a chain of incoming pbufs, instead of only one - modbtree: implement keys(), values(), items() iterators - modbtree: cleverly implement "for key in btree:" syntax - modwebsocket: add readinto() method - modwebrepl: add readinto() method - modbtree: fix unused argument warning - modbtree: fixes for nanbox build - moduos_dupterm: reserve buffer bytearray object for dupterm - moduos_dupterm: reuse dupterm_arr_obj for write operations lib: - berkeley-db-1.xx: add Berkeley DB 1.85 as a submodule - berkeley-db-1.xx: update to upstream which builds for uPy - fatfs/option/ccsbcs: follow uPy optional features model - libm: format code to pass gcc v6.1.1 warning - libm: remove unused definition of "one" drivers: - display/ssd1306: add width arg and support 64px wide displays tests: - recursive_iternext: clang/Linux is even more stack-frugal than MacOS - bench: add testcase to compare bytes(N) vs b"\0" * N - add testcase for OrderedDict equality - add a testcase for machine.PinBase class - extmod: add "btree" module test - btree1: add testcase for iterating btree object directly - add tests for _thread module - add 3 more tests for _thread module - thread: remove need to sleep to wait for completion in some tests - thread: add tests for running GC within a thread, and heap stress - thread: rename thread_stress_XXX.py to stress_XXX.py - thread: add tests that mutate shared objects - thread: add test for concurrent interning of strings - thread: add test for concurrent mutating of user instance - thread: make stack-size test run correctly and reliable on uPy - thread: make sure that thread tests don't rely on floating point - thread: allow thread_sleep1 to run without floating point - thread: allow some tests to run on ports with not much heap - meminfo: update for addition of "max free sz" output - run-tests: If running thread tests on unix, don't run mutate ones unix port: - unbreak "minimal" target by disabling FatFs - mpconfigport_minimal.h: clearly mark where user-define config ends - deprecate support for GNU Readline (MICROPY_USE_READLINE=2) - time to build with --gc-sections - move "utime" module config to C level instead of make level - Makefile: libffi: Build with -Os - alloc: add option to use uPy's alloc-exec implementation even for libffi - alloc: make coverage build and its overzealous warnings happy - disable FatFs VFS for normal build, keep enabled for coverage - modmachine: include PinBase class - modmachine: enable time_pulse_us() function - fix Makefile to handle gc-sections linker flags on Mac OS - add basic thread support using pthreads - file: if write syscall returns because of EINTR then try again - implement garbage collection with threading - mpthreadport: use SA_SIGINFO for GC signal handler - gccollect: provide declaration of exported function - mpthreadport: suppress compiler warning about unused arguments - modtime: release the GIL when sleeping - enable btree module - Makefile: make "minimal" build be minimal again - mpconfigport_minimal.h: allow to print a string within 1KB of heap - main: error out on unknown value of suffix in -X heapsize= option - main: improve help for -X options a bit - main: when preparing sys.path, allocate exact strings on uPy heap - disable the GIL to improve performance of non-thread code windows port: - follow unix port changes regarding "utime" module - msvc: include machine_pinbase.c in build and enable umachine module stmhal port: - add board definition files for Olimex STM32-E407 - port of f4 hal commit 1d7fb82 to l4 hal (SD card modifications) - correct DMA to allow SD card on L4 MCUs - enable SD card on L4 MCUs cc3200 port: - update FreeRTOS to v9.0.0 - define our own FreeRTOS heap so it can go in a special segment - add basic threading capabilities - fix call to new exception to be _msg instead of _arg1 - use xTaskCreateStatic instead of osi_TaskCreate - mpthreadport: make mutex statically allocated - mpthreadport: properly initialise the main thread's data - gccollect: use MP_STATE_THREAD(stack_top) to get top of stack - mpthreadport: scan more root pointers from thread data - mpthreadport: move mem alloc outside the thread_mutex lock - enable the GIL - in FreeRTOSConfig.h, comment on configSUPPORT_STATIC_ALLOCATION - remove .boot section attribute from osi_TaskCreate function - start the simplelink spawn task using the static task creator - shrink the FreeRTOS heap and place TCB+stack in freed location - allow to compile bootloader with threading enabled - remove obsolete singleton heart-beat object teensy port: - update the README with OSX specific information and tips esp8266 port: - modnetwork: use struct bss_info::ssid_len for ESSID length - let RTC work correctly after deepsleep - switch floating-point arith routines to BootROM - Makefile: enable --verify option for esptool.py write_flash - use RTC to set date & time stamps for files - main.c: clear the command line history when (re)booting - enable frozen bytecode, with scripts in modules/ subdir - README: describe how to build mpy-cross - websocket_helper.py: avoid extra string allocations - websocket_helper.py: fix typo in debug output - modpybuart: allow setting baudrate and other params - explicitly collect garbage in bootstrap scripts - switch webrepl_setup to use frozen bytecode - switch webrepl to use frozen bytecode - README: promote from "highly experimental" to "experimental" - main: init recently added dupterm_arr_obj port state var - esp_mphal: call_dupterm_read: Use readinto() method - esp_mphal: properly handle dupterm EOF after switching to readinto() qemu-arm port: - disable gcc LTO option for nlrthumb.c docs: - esp8266/tutorial/network_basics: minor typo fix, ap becomes ap_if - add link to PDF version of docs in sidebar - library/network: wipy: fix literal block indentation - esp.socket: remove unused file - ure: typo fix - usocket: there must be empty line after .only:: - library/machine.Pin: mention GPIO and cross-link .value() function - machine.*: add "currentmodule:: machine" directive - pyb.*: add "currentmodule:: pyb" directive - machine.Pin: remove explicit target - machine.UART: cleanup pyboard section - machine.*: use proper class case in method headers - machine.Pin: add class designator to all constants - pyb.CAN: mark CAN.initfilterbanks() as classmethod explicitly - pyb.Pin: af_list() is a normal method, not a class method - pyb.ExtInt,pyb.Pin: mark up class methods as such - pyb.*: use proper class case in method headers - pyb.Pin: sort .af() and .af_list() methods together - builtins: enumerate all builtin functions implemented - library/index: add builtins.rst - machine.Pin: disambiguate object call method - machine*: remove explicit targets and "machine." prefixes on classes - uctypes: improve documentation - sys: detailed description of print_exception() diff from traceback module - add sphinx_selective_exclude extension suite - conf.py: active sphinx_selective_exclude extensions - rebuild docs from scratch, as required for proper only:: handling - select: add an article - sys: print_exception: Fixes/clarifications - conf.py: exclude cmath from modindex for wipy - library: fix typo in docs for usocket.listen() examples: - unix/ffi_example.py: update for current "ffi" module API - unix/ffi_example.py: remove TODO, make output more clear
Diffstat (limited to 'esp8266')
-rw-r--r--esp8266/Makefile20
-rw-r--r--esp8266/README.md14
-rw-r--r--esp8266/eagle.rom.addr.v6.ld32
-rw-r--r--esp8266/esp_mphal.c14
-rw-r--r--esp8266/fatfs_port.c41
-rw-r--r--esp8266/main.c3
-rw-r--r--esp8266/modesp.c6
-rw-r--r--esp8266/modmachine.c2
-rw-r--r--esp8266/modnetwork.c6
-rw-r--r--esp8266/modpybrtc.c6
-rw-r--r--esp8266/modpybrtc.h2
-rw-r--r--esp8266/modpybuart.c103
-rw-r--r--esp8266/modules/_boot.py (renamed from esp8266/scripts/_boot.py)3
-rw-r--r--esp8266/modules/flashbdev.py (renamed from esp8266/scripts/flashbdev.py)0
-rw-r--r--esp8266/modules/webrepl.py (renamed from esp8266/scripts/webrepl.py)0
-rw-r--r--esp8266/modules/webrepl_setup.py (renamed from esp8266/scripts/webrepl_setup.py)0
-rw-r--r--esp8266/mpconfigport.h2
-rw-r--r--esp8266/scripts/inisetup.py2
-rw-r--r--esp8266/scripts/websocket_helper.py19
-rw-r--r--esp8266/uart.c6
-rw-r--r--esp8266/uart.h1
21 files changed, 231 insertions, 51 deletions
diff --git a/esp8266/Makefile b/esp8266/Makefile
index 65093d819b..00826cfe46 100644
--- a/esp8266/Makefile
+++ b/esp8266/Makefile
@@ -8,9 +8,12 @@ MICROPY_PY_USSL = 1
# include py core make definitions
include ../py/py.mk
+MPY_CROSS = ../mpy-cross/mpy-cross
+MPY_TOOL = ../tools/mpy-tool.py
MAKE_FROZEN = ../tools/make-frozen.py
SCRIPTDIR = scripts
+FROZEN_MPY_DIR = modules
PORT ?= /dev/ttyACM0
BAUD ?= 115200
CROSS_COMPILE = xtensa-lx106-elf-
@@ -131,6 +134,9 @@ DRIVERS_SRC_C = $(addprefix drivers/,\
SRC_S = \
gchelper.s \
+FROZEN_MPY_PY_FILES := $(shell find $(FROZEN_MPY_DIR)/ -type f -name '*.py')
+FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
+
OBJ =
OBJ += $(PY_O)
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
@@ -139,6 +145,7 @@ OBJ += $(addprefix $(BUILD)/, $(STM_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
+OBJ += $(BUILD)/$(BUILD)/frozen_mpy.o
#OBJ += $(BUILD)/pins_$(BOARD).o
# List of sources for qstr extraction
@@ -163,11 +170,22 @@ $(BUILD)/frozen.c: $(wildcard $(SCRIPTDIR)/*) $(CONFVARS_FILE)
$(ECHO) "Generating $@"
$(Q)$(MAKE_FROZEN) $(SCRIPTDIR) > $@
+# to build .mpy files from .py files
+$(BUILD)/$(FROZEN_MPY_DIR)/%.mpy: $(FROZEN_MPY_DIR)/%.py
+ @$(ECHO) "MPY $<"
+ $(Q)$(MKDIR) -p $(dir $@)
+ $(Q)$(MPY_CROSS) -o $@ -s $(^:$(FROZEN_MPY_DIR)/%=%) $^
+
+# to build frozen_mpy.c from all .mpy files
+$(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h
+ @$(ECHO) "Creating $@"
+ $(Q)$(PYTHON) $(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h $(FROZEN_MPY_MPY_FILES) > $@
+
.PHONY: deploy
deploy: $(BUILD)/firmware-combined.bin
$(ECHO) "Writing $< to the board"
- $(Q)esptool.py --port $(PORT) --baud $(BAUD) write_flash --flash_size=8m 0 $<
+ $(Q)esptool.py --port $(PORT) --baud $(BAUD) write_flash --verify --flash_size=8m 0 $<
#$(Q)esptool.py --port $(PORT) --baud $(BAUD) write_flash --flash_size=8m 0 $(BUILD)/firmware.elf-0x00000.bin 0x9000 $(BUILD)/firmware.elf-0x0[1-f]000.bin
reset:
diff --git a/esp8266/README.md b/esp8266/README.md
index 0509a3799b..b9162e9585 100644
--- a/esp8266/README.md
+++ b/esp8266/README.md
@@ -1,12 +1,12 @@
MicroPython port to ESP8266
===========================
-This is a highly experimental port of MicroPython for the WiFi modules based
+This is an experimental port of MicroPython for the WiFi modules based
on Espressif ESP8266 chip.
-WARNING: The port is highly experimental and any APIs are subject to change.
+WARNING: The port is experimental and many APIs are subject to change.
-Currently implemented features include:
+Supported features include:
- REPL (Python prompt) over UART0.
- Garbage collector, exceptions.
- Unicode support.
@@ -19,6 +19,8 @@ Currently implemented features include:
- 1-Wire and WS2812 (aka Neopixel) protocols support.
- Internal filesystem using the flash.
- WebREPL over WiFi from a browser (clients at https://github.com/micropython/webrepl).
+- Modules for HTTP, MQTT, many other formats and protocols via
+ https://github.com/micropython/micropython-lib .
Work-in-progress documentation is available at
http://docs.micropython.org/en/latest/esp8266/ .
@@ -39,6 +41,12 @@ $ git submodule update --init
See the README in the repository root for more information about external
dependencies.
+The MicroPython cross-compiler must be built to pre-compile some of the
+built-in scripts to bytecode. This can be done using:
+```bash
+$ make -C mpy-cross
+```
+
Then, to build MicroPython for the ESP8266, just run:
```bash
$ cd esp8266
diff --git a/esp8266/eagle.rom.addr.v6.ld b/esp8266/eagle.rom.addr.v6.ld
index 30e238f5a5..ce071a3bb9 100644
--- a/esp8266/eagle.rom.addr.v6.ld
+++ b/esp8266/eagle.rom.addr.v6.ld
@@ -47,25 +47,25 @@ PROVIDE ( _NMIExceptionVector = 0x40000020 );
PROVIDE ( _ResetHandler = 0x400000a4 );
PROVIDE ( _ResetVector = 0x40000080 );
PROVIDE ( _UserExceptionVector = 0x40000050 );
-PROVIDE ( __adddf3 = 0x4000c538 );
-PROVIDE ( __addsf3 = 0x4000c180 );
-PROVIDE ( __divdf3 = 0x4000cb94 );
+__adddf3 = 0x4000c538;
+__addsf3 = 0x4000c180;
+__divdf3 = 0x4000cb94;
__divdi3 = 0x4000ce60;
__divsi3 = 0x4000dc88;
-PROVIDE ( __extendsfdf2 = 0x4000cdfc );
-PROVIDE ( __fixdfsi = 0x4000ccb8 );
-PROVIDE ( __fixunsdfsi = 0x4000cd00 );
-PROVIDE ( __fixunssfsi = 0x4000c4c4 );
-PROVIDE ( __floatsidf = 0x4000e2f0 );
-PROVIDE ( __floatsisf = 0x4000e2ac );
-PROVIDE ( __floatunsidf = 0x4000e2e8 );
-PROVIDE ( __floatunsisf = 0x4000e2a4 );
-PROVIDE ( __muldf3 = 0x4000c8f0 );
+__extendsfdf2 = 0x4000cdfc;
+__fixdfsi = 0x4000ccb8;
+__fixunsdfsi = 0x4000cd00;
+__fixunssfsi = 0x4000c4c4;
+__floatsidf = 0x4000e2f0;
+__floatsisf = 0x4000e2ac;
+__floatunsidf = 0x4000e2e8;
+__floatunsisf = 0x4000e2a4;
+__muldf3 = 0x4000c8f0;
__muldi3 = 0x40000650;
-PROVIDE ( __mulsf3 = 0x4000c3dc );
-PROVIDE ( __subdf3 = 0x4000c688 );
-PROVIDE ( __subsf3 = 0x4000c268 );
-PROVIDE ( __truncdfsf2 = 0x4000cd5c );
+__mulsf3 = 0x4000c3dc;
+__subdf3 = 0x4000c688;
+__subsf3 = 0x4000c268;
+__truncdfsf2 = 0x4000cd5c;
__udivdi3 = 0x4000d310;
__udivsi3 = 0x4000e21c;
__umoddi3 = 0x4000d770;
diff --git a/esp8266/esp_mphal.c b/esp8266/esp_mphal.c
index 9f090a0973..3cc4610825 100644
--- a/esp8266/esp_mphal.c
+++ b/esp8266/esp_mphal.c
@@ -161,21 +161,21 @@ static int call_dupterm_read(void) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
- mp_obj_t read_m[3];
- mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_read, read_m);
- read_m[2] = MP_OBJ_NEW_SMALL_INT(1);
- mp_obj_t res = mp_call_method_n_kw(1, 0, read_m);
+ mp_obj_t readinto_m[3];
+ mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_readinto, readinto_m);
+ readinto_m[2] = MP_STATE_PORT(dupterm_arr_obj);
+ mp_obj_t res = mp_call_method_n_kw(1, 0, readinto_m);
if (res == mp_const_none) {
nlr_pop();
return -2;
}
- mp_buffer_info_t bufinfo;
- mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ);
- if (bufinfo.len == 0) {
+ if (res == MP_OBJ_NEW_SMALL_INT(0)) {
mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL);
nlr_pop();
return -1;
}
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(MP_STATE_PORT(dupterm_arr_obj), &bufinfo, MP_BUFFER_READ);
nlr_pop();
if (*(byte*)bufinfo.buf == interrupt_char) {
mp_keyboard_interrupt();
diff --git a/esp8266/fatfs_port.c b/esp8266/fatfs_port.c
index c62ae2e7dc..b87906f789 100644
--- a/esp8266/fatfs_port.c
+++ b/esp8266/fatfs_port.c
@@ -1,6 +1,43 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014, 2016 Damien P. George
+ *
+ * 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/obj.h"
#include "lib/fatfs/ff.h"
-#include "lib/fatfs/diskio.h"
+#include "timeutils.h"
+#include "modpybrtc.h"
DWORD get_fattime(void) {
- return 0;
+
+ // TODO: Optimize division (there's no HW division support on ESP8266,
+ // so it's expensive).
+ uint32_t secs = (uint32_t)(pyb_rtc_get_us_since_2000() / 1000000);
+
+ timeutils_struct_time_t tm;
+ timeutils_seconds_since_2000_to_struct_time(secs, &tm);
+
+ return (((DWORD)(tm.tm_year - 1980) << 25) | ((DWORD)tm.tm_mon << 21) | ((DWORD)tm.tm_mday << 16) |
+ ((DWORD)tm.tm_hour << 11) | ((DWORD)tm.tm_min << 5) | ((DWORD)tm.tm_sec >> 1));
}
diff --git a/esp8266/main.c b/esp8266/main.c
index 9cc955e44c..21bb6e0e0f 100644
--- a/esp8266/main.c
+++ b/esp8266/main.c
@@ -34,6 +34,7 @@
#include "py/stackctrl.h"
#include "py/mphal.h"
#include "py/gc.h"
+#include "lib/mp-readline/readline.h"
#include "lib/utils/pyexec.h"
#include "gccollect.h"
#include "user_interface.h"
@@ -56,7 +57,9 @@ STATIC void mp_reset(void) {
#endif
MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
MP_STATE_PORT(term_obj) = MP_OBJ_NULL;
+ MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL;
pin_init0();
+ readline_init0();
#if MICROPY_MODULE_FROZEN
pyexec_frozen_module("_boot.py");
pyexec_file("boot.py");
diff --git a/esp8266/modesp.c b/esp8266/modesp.c
index 0c6cc423e1..99f063a756 100644
--- a/esp8266/modesp.c
+++ b/esp8266/modesp.c
@@ -46,6 +46,7 @@
#include "espneopixel.h"
#include "espapa102.h"
#include "modpyb.h"
+#include "modpybrtc.h"
#define MODESP_ESPCONN (0)
@@ -540,8 +541,11 @@ STATIC mp_obj_t esp_sleep_type(mp_uint_t n_args, const mp_obj_t *args) {
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_sleep_type_obj, 0, 1, esp_sleep_type);
STATIC mp_obj_t esp_deepsleep(mp_uint_t n_args, const mp_obj_t *args) {
+ uint32_t sleep_us = n_args > 0 ? mp_obj_get_int(args[0]) : 0;
+ // prepare for RTC reset at wake up
+ rtc_prepare_deepsleep(sleep_us);
system_deep_sleep_set_option(n_args > 1 ? mp_obj_get_int(args[1]) : 0);
- system_deep_sleep(n_args > 0 ? mp_obj_get_int(args[0]) : 0);
+ system_deep_sleep(sleep_us);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_deepsleep_obj, 0, 2, esp_deepsleep);
diff --git a/esp8266/modmachine.c b/esp8266/modmachine.c
index 36872c9dff..80ae7ac2a0 100644
--- a/esp8266/modmachine.c
+++ b/esp8266/modmachine.c
@@ -103,6 +103,8 @@ STATIC mp_obj_t machine_deepsleep(void) {
}
}
+ // prepare for RTC reset at wake up
+ rtc_prepare_deepsleep(sleep_us);
// put the device in a deep-sleep state
system_deep_sleep_set_option(0); // default power down mode; TODO check this
system_deep_sleep(sleep_us);
diff --git a/esp8266/modnetwork.c b/esp8266/modnetwork.c
index c0763ce0eb..7cfa3ff77a 100644
--- a/esp8266/modnetwork.c
+++ b/esp8266/modnetwork.c
@@ -142,7 +142,13 @@ STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
struct bss_info *bs;
STAILQ_FOREACH(bs, si->pbss, next) {
mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
+ #if 1
+ // struct bss_info::ssid_len is not documented in SDK API Guide,
+ // but is present in SDK headers since 1.4.0
+ t->items[0] = mp_obj_new_bytes(bs->ssid, bs->ssid_len);
+ #else
t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
+ #endif
t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid));
t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel);
t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
diff --git a/esp8266/modpybrtc.c b/esp8266/modpybrtc.c
index 484d0d82fd..9685248034 100644
--- a/esp8266/modpybrtc.c
+++ b/esp8266/modpybrtc.c
@@ -120,6 +120,12 @@ uint64_t pyb_rtc_get_us_since_2000() {
return (((uint64_t)rtc_ticks * cal) >> 12) + delta;
};
+void rtc_prepare_deepsleep(uint64_t sleep_us) {
+ // RTC time will reset at wake up. Let's be preared for this.
+ int64_t delta = pyb_rtc_get_us_since_2000() + sleep_us;
+ system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta));
+}
+
STATIC mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
if (n_args == 1) {
// Get time
diff --git a/esp8266/modpybrtc.h b/esp8266/modpybrtc.h
index b4ca780712..5b9d9fc766 100644
--- a/esp8266/modpybrtc.h
+++ b/esp8266/modpybrtc.h
@@ -30,3 +30,5 @@ extern uint64_t pyb_rtc_alarm0_expiry;
void pyb_rtc_set_us_since_2000(uint64_t nowus);
uint64_t pyb_rtc_get_us_since_2000();
+
+void rtc_prepare_deepsleep(uint64_t sleep_us);
diff --git a/esp8266/modpybuart.c b/esp8266/modpybuart.c
index 5971ffecec..f27ee4e2d2 100644
--- a/esp8266/modpybuart.c
+++ b/esp8266/modpybuart.c
@@ -36,32 +36,39 @@
#include "py/mperrno.h"
#include "modpyb.h"
-// baudrate is currently fixed to this value
-#define UART_BAUDRATE (115200)
+// UartDev is defined and initialized in rom code.
+extern UartDevice UartDev;
typedef struct _pyb_uart_obj_t {
mp_obj_base_t base;
uint8_t uart_id;
+ uint8_t bits;
+ uint8_t parity;
+ uint8_t stop;
+ uint32_t baudrate;
uint16_t timeout; // timeout waiting for first char (in ms)
uint16_t timeout_char; // timeout waiting between chars (in ms)
} pyb_uart_obj_t;
+STATIC const char *_parity_name[] = {"None", "1", "0"};
+
/******************************************************************************/
// MicroPython bindings for UART
STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
- mp_printf(print, "UART(%u, baudrate=%u, timeout=%u, timeout_char=%u)",
- self->uart_id, UART_BAUDRATE, self->timeout, self->timeout_char);
+ mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, timeout=%u, timeout_char=%u)",
+ self->uart_id, self->baudrate, self->bits, _parity_name[self->parity],
+ self->stop, self->timeout, self->timeout_char);
}
STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_timeout, ARG_timeout_char };
+ enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_timeout_char };
static const mp_arg_t allowed_args[] = {
- //{ MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 9600} },
- //{ MP_QSTR_bits, MP_ARG_INT, {.u_int = 8} },
- //{ MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = mp_const_none} },
- //{ MP_QSTR_stop, MP_ARG_INT, {.u_int = 1} },
+ { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_bits, MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+ { MP_QSTR_stop, MP_ARG_INT, {.u_int = 0} },
//{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
//{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
@@ -70,16 +77,84 @@ STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_o
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+ // set baudrate
+ if (args[ARG_baudrate].u_int > 0) {
+ self->baudrate = args[ARG_baudrate].u_int;
+ UartDev.baut_rate = self->baudrate; // Sic!
+ }
+
+ // set data bits
+ switch (args[ARG_bits].u_int) {
+ case 0:
+ break;
+ case 5:
+ UartDev.data_bits = UART_FIVE_BITS;
+ self->bits = 5;
+ break;
+ case 6:
+ UartDev.data_bits = UART_SIX_BITS;
+ self->bits = 6;
+ break;
+ case 7:
+ UartDev.data_bits = UART_SEVEN_BITS;
+ self->bits = 7;
+ break;
+ case 8:
+ UartDev.data_bits = UART_EIGHT_BITS;
+ self->bits = 8;
+ break;
+ default:
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid data bits"));
+ break;
+ }
+
+ // set parity
+ if (args[ARG_parity].u_obj != MP_OBJ_NULL) {
+ if (args[ARG_parity].u_obj == mp_const_none) {
+ UartDev.parity = UART_NONE_BITS;
+ self->parity = 0;
+ } else {
+ mp_int_t parity = mp_obj_get_int(args[ARG_parity].u_obj);
+ if (parity & 1) {
+ UartDev.parity = UART_ODD_BITS;
+ self->parity = 1;
+ } else {
+ UartDev.parity = UART_EVEN_BITS;
+ self->parity = 2;
+ }
+ }
+ }
+
+ // set stop bits
+ switch (args[ARG_stop].u_int) {
+ case 0:
+ break;
+ case 1:
+ UartDev.stop_bits = UART_ONE_STOP_BIT;
+ self->stop = 1;
+ break;
+ case 2:
+ UartDev.stop_bits = UART_TWO_STOP_BIT;
+ self->stop = 2;
+ break;
+ default:
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid stop bits"));
+ break;
+ }
+
// set timeout
self->timeout = args[ARG_timeout].u_int;
// set timeout_char
// make sure it is at least as long as a whole character (13 bits to be safe)
self->timeout_char = args[ARG_timeout_char].u_int;
- uint32_t min_timeout_char = 13000 / UART_BAUDRATE + 1;
+ uint32_t min_timeout_char = 13000 / self->baudrate + 1;
if (self->timeout_char < min_timeout_char) {
self->timeout_char = min_timeout_char;
}
+
+ // setup
+ uart_setup(self->uart_id);
}
STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
@@ -95,6 +170,12 @@ STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size
pyb_uart_obj_t *self = m_new_obj(pyb_uart_obj_t);
self->base.type = &pyb_uart_type;
self->uart_id = uart_id;
+ self->baudrate = 115200;
+ self->bits = 8;
+ self->parity = 0;
+ self->stop = 1;
+ self->timeout = 0;
+ self->timeout_char = 0;
// init the peripheral
mp_map_t kw_args;
@@ -191,6 +272,6 @@ const mp_obj_type_t pyb_uart_type = {
.make_new = pyb_uart_make_new,
.getiter = mp_identity,
.iternext = mp_stream_unbuffered_iter,
- .stream_p = &uart_stream_p,
+ .protocol = &uart_stream_p,
.locals_dict = (mp_obj_dict_t*)&pyb_uart_locals_dict,
};
diff --git a/esp8266/scripts/_boot.py b/esp8266/modules/_boot.py
index c950de6758..2cacb56760 100644
--- a/esp8266/scripts/_boot.py
+++ b/esp8266/modules/_boot.py
@@ -1,3 +1,4 @@
+import gc
import uos
from flashbdev import bdev
@@ -7,3 +8,5 @@ try:
except OSError:
import inisetup
vfs = inisetup.setup()
+
+gc.collect()
diff --git a/esp8266/scripts/flashbdev.py b/esp8266/modules/flashbdev.py
index 07ed966020..07ed966020 100644
--- a/esp8266/scripts/flashbdev.py
+++ b/esp8266/modules/flashbdev.py
diff --git a/esp8266/scripts/webrepl.py b/esp8266/modules/webrepl.py
index da3e70c595..da3e70c595 100644
--- a/esp8266/scripts/webrepl.py
+++ b/esp8266/modules/webrepl.py
diff --git a/esp8266/scripts/webrepl_setup.py b/esp8266/modules/webrepl_setup.py
index d0bf8465d5..d0bf8465d5 100644
--- a/esp8266/scripts/webrepl_setup.py
+++ b/esp8266/modules/webrepl_setup.py
diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h
index 544edc408f..90dbc00098 100644
--- a/esp8266/mpconfigport.h
+++ b/esp8266/mpconfigport.h
@@ -74,7 +74,9 @@
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
#define MICROPY_STREAMS_NON_BLOCK (1)
#define MICROPY_MODULE_FROZEN_STR (1)
+#define MICROPY_MODULE_FROZEN_MPY (1)
#define MICROPY_MODULE_FROZEN_LEXER mp_lexer_new_from_str32
+#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool
#define MICROPY_FATFS_ENABLE_LFN (1)
#define MICROPY_FATFS_RPATH (2)
diff --git a/esp8266/scripts/inisetup.py b/esp8266/scripts/inisetup.py
index 461690b162..7d8f6fa0a8 100644
--- a/esp8266/scripts/inisetup.py
+++ b/esp8266/scripts/inisetup.py
@@ -40,7 +40,9 @@ def setup():
with open("/boot.py", "w") as f:
f.write("""\
# This file is executed on every boot (including wake-boot from deepsleep)
+import gc
#import webrepl
#webrepl.start()
+gc.collect()
""")
return vfs
diff --git a/esp8266/scripts/websocket_helper.py b/esp8266/scripts/websocket_helper.py
index 22ac28592d..9c06db5023 100644
--- a/esp8266/scripts/websocket_helper.py
+++ b/esp8266/scripts/websocket_helper.py
@@ -36,21 +36,20 @@ def server_handshake(sock):
if DEBUG:
print("Sec-WebSocket-Key:", webkey, len(webkey))
- respkey = webkey + b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
- respkey = hashlib.sha1(respkey).digest()
+ d = hashlib.sha1(webkey)
+ d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
+ respkey = d.digest()
respkey = binascii.b2a_base64(respkey)[:-1]
+ if DEBUG:
+ print("respkey:", respkey)
- resp = b"""\
+ sock.send(b"""\
HTTP/1.1 101 Switching Protocols\r
Upgrade: websocket\r
Connection: Upgrade\r
-Sec-WebSocket-Accept: %s\r
-\r
-""" % respkey
-
- if DEBUG:
- print(resp)
- sock.send(resp)
+Sec-WebSocket-Accept: """)
+ sock.send(respkey)
+ sock.send("\r\n\r\n")
# Very simplified client handshake, works for MicroPython's
diff --git a/esp8266/uart.c b/esp8266/uart.c
index 1c317a1cb6..d724331c41 100644
--- a/esp8266/uart.c
+++ b/esp8266/uart.c
@@ -237,6 +237,12 @@ void ICACHE_FLASH_ATTR uart_reattach() {
uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880);
}
+void ICACHE_FLASH_ATTR uart_setup(uint8 uart) {
+ ETS_UART_INTR_DISABLE();
+ uart_config(uart);
+ ETS_UART_INTR_ENABLE();
+}
+
// Task-based UART interface
#include "py/obj.h"
diff --git a/esp8266/uart.h b/esp8266/uart.h
index 8e09beea58..21894d3fa6 100644
--- a/esp8266/uart.h
+++ b/esp8266/uart.h
@@ -96,5 +96,6 @@ int uart_rx_char(void);
void uart_tx_one_char(uint8 uart, uint8 TxChar);
void uart_flush(uint8 uart);
void uart_os_config(int uart);
+void uart_setup(uint8 uart);
#endif // _INCLUDED_UART_H_