diff options
Diffstat (limited to 'esp8266')
40 files changed, 665 insertions, 235 deletions
diff --git a/esp8266/Makefile b/esp8266/Makefile index 65a513afd..b2191353a 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -5,20 +5,19 @@ QSTR_DEFS = qstrdefsport.h #$(BUILD)/pins_qstr.h MICROPY_PY_USSL = 1 MICROPY_SSL_AXTLS = 1 +MICROPY_FATFS = 1 MICROPY_PY_BTREE = 1 +FROZEN_DIR = scripts +FROZEN_MPY_DIR = modules + # include py core make definitions include ../py/py.mk -MPY_CROSS = ../mpy-cross/mpy-cross -MPY_TOOL = ../tools/mpy-tool.py - -FROZEN_DIR = scripts -FROZEN_MPY_DIR = modules PORT ?= /dev/ttyACM0 BAUD ?= 115200 FLASH_MODE ?= qio -FLASH_SIZE ?= 8m +FLASH_SIZE ?= detect CROSS_COMPILE = xtensa-lx106-elf- ESP_SDK = $(shell $(CC) -print-sysroot)/usr @@ -44,7 +43,8 @@ CFLAGS_XTENSA = -fsingle-precision-constant -Wdouble-promotion \ CFLAGS = $(INC) -Wall -Wpointer-arith -Werror -ansi -std=gnu99 -nostdlib -DUART_OS=$(UART_OS) \ $(CFLAGS_XTENSA) $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA) -LDFLAGS = -nostdlib -T esp8266.ld -Map=$(@:.elf=.map) --cref +LDSCRIPT = esp8266.ld +LDFLAGS = -nostdlib -T $(LDSCRIPT) -Map=$(@:.elf=.map) --cref LIBS = -L$(ESP_SDK)/lib -lmain -ljson -llwip_open -lpp -lnet80211 -lwpa -lphy -lnet80211 $(LDFLAGS_MOD) LIBGCC_FILE_NAME = $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) @@ -65,6 +65,7 @@ SRC_C = \ main.c \ help.c \ esp_mphal.c \ + esp_init_data.c \ gccollect.c \ lexerstr32.c \ uart.c \ @@ -74,22 +75,21 @@ SRC_C = \ espapa102.c \ intr.c \ modpyb.c \ - modpybpin.c \ - modpybpwm.c \ - modpybrtc.c \ - modpybadc.c \ - modpybuart.c \ - modmachinewdt.c \ - modpybspi.c \ - modpybhspi.c \ + modmachine.c \ + machine_pin.c \ + machine_pwm.c \ + machine_rtc.c \ + machine_adc.c \ + machine_uart.c \ + machine_wdt.c \ + machine_spi.c \ + machine_hspi.c \ modesp.c \ modnetwork.c \ modutime.c \ moduos.c \ - modmachine.c \ modonewire.c \ ets_alt_task.c \ - $(BUILD)/frozen.c \ fatfs_port.c \ axtls_helpers.c \ hspi.c \ @@ -130,10 +130,14 @@ LIB_SRC_C = $(addprefix lib/,\ utils/pyexec.c \ utils/pyhelp.c \ utils/interrupt_char.c \ - fatfs/ff.c \ - fatfs/option/ccsbcs.c \ ) +ifeq ($(MICROPY_FATFS), 1) +LIB_SRC_C += \ + lib/fatfs/ff.c \ + lib/fatfs/option/ccsbcs.c +endif + DRIVERS_SRC_C = $(addprefix drivers/,\ dht/dht.c \ ) @@ -141,9 +145,6 @@ DRIVERS_SRC_C = $(addprefix drivers/,\ SRC_S = \ gchelper.s \ -FROZEN_MPY_PY_FILES := $(shell find -L $(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)) @@ -152,7 +153,6 @@ 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 @@ -175,23 +175,11 @@ $(BUILD)/uart.o: $(CONFVARS_FILE) FROZEN_EXTRA_DEPS = $(CONFVARS_FILE) -# 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 --verify --flash_size=$(FLASH_SIZE) --flash_mode=$(FLASH_MODE) 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: echo -e "\r\nimport machine; machine.reset()\r\n" >$(PORT) @@ -206,6 +194,9 @@ $(BUILD)/firmware.elf: $(OBJ) $(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS) $(Q)$(SIZE) $@ +512k: + $(MAKE) LDSCRIPT=esp8266_512k.ld CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_512k.h>"' MICROPY_FATFS=0 MICROPY_PY_BTREE=0 + #MAKE_PINS = boards/make-pins.py #BOARD_PINS = boards/$(BOARD)/pins.csv #AF_FILE = boards/stm32f4xx_af.csv diff --git a/esp8266/README.md b/esp8266/README.md index c998b7ad1..54591fcc1 100644 --- a/esp8266/README.md +++ b/esp8266/README.md @@ -70,13 +70,14 @@ $ make deploy ``` This will use the `esptool.py` script to download the images. You must have your ESP module in the bootloader mode, and connected to a serial port on your PC. -The default serial port is `/dev/ttyACM0`, flash mode is `qio` and flash size is `8m`. -To specify other values, use, eg: +The default serial port is `/dev/ttyACM0`, flash mode is `qio` and flash size is +`detect` (auto-detect based on Flash ID). To specify other values, use, eg (note +that flash size is in megabits): ```bash -$ make PORT=/dev/ttyUSB0 FLASH_MODE=qio FLASH_SIZE=8m deploy +$ make PORT=/dev/ttyUSB0 FLASH_MODE=qio FLASH_SIZE=32m deploy ``` -The image produced is `firmware-combined.bin`, to be flashed at 0x00000. +The image produced is `build/firmware-combined.bin`, to be flashed at 0x00000. First start ----------- diff --git a/esp8266/esp8266.ld b/esp8266/esp8266.ld index c726790d3..20b259dff 100644 --- a/esp8266/esp8266.ld +++ b/esp8266/esp8266.ld @@ -20,7 +20,7 @@ PHDRS irom0_0_phdr PT_LOAD; } -ENTRY(call_user_start) +ENTRY(firmware_start) EXTERN(_DebugExceptionVector) EXTERN(_DoubleExceptionVector) EXTERN(_KernelExceptionVector) @@ -130,20 +130,23 @@ SECTIONS *stmhal/pybstdio.o(.literal*, .text*) + build/main.o(.literal* .text*) *gccollect.o(.literal* .text*) *gchelper.o(.literal* .text*) + *help.o(.literal* .text*) *lexerstr32.o(.literal* .text*) *utils.o(.literal* .text*) *modpyb.o(.literal*, .text*) - *modpybpin.o(.literal*, .text*) - *modpybpwm.o(.literal*, .text*) - *modpybrtc.o(.literal*, .text*) - *modpybadc.o(.literal*, .text*) - *modpybuart.o(.literal*, .text*) + *machine_pin.o(.literal*, .text*) + *machine_pwm.o(.literal*, .text*) + *machine_rtc.o(.literal*, .text*) + *machine_adc.o(.literal*, .text*) + *machine_uart.o(.literal*, .text*) *modpybi2c.o(.literal*, .text*) - *modmachinewdt.o(.literal*, .text*) - *modpybspi.o(.literal*, .text*) - *modpybhspi.o(.literal*, .text*) + *modmachine.o(.literal*, .text*) + *machine_wdt.o(.literal*, .text*) + *machine_spi.o(.literal*, .text*) + *machine_hspi.o(.literal*, .text*) *hspi.o(.literal*, .text*) *modesp.o(.literal* .text*) *modnetwork.o(.literal* .text*) @@ -199,7 +202,8 @@ SECTIONS *(.entry.text) *(.init.literal) *(.init) - *(.literal .text .literal.* .text.* .stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*) + *(.literal .text .literal.* .text.* .iram0.literal .iram0.text .iram0.text.*.literal .iram0.text.*) + *(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*) *(.fini.literal) *(.fini) *(.gnu.version) diff --git a/esp8266/esp8266_512k.ld b/esp8266/esp8266_512k.ld new file mode 100644 index 000000000..781cbb985 --- /dev/null +++ b/esp8266/esp8266_512k.ld @@ -0,0 +1,303 @@ +/* GNU linker script for ESP8266 */ + +MEMORY +{ + dport0_0_seg : org = 0x3ff00000, len = 0x10 + dram0_0_seg : org = 0x3ffe8000, len = 0x14000 + iram1_0_seg : org = 0x40100000, len = 0x8000 + irom0_0_seg : org = 0x40209000, len = 0x72000 +} + +/* define the top of RAM */ +_heap_end = ORIGIN(dram0_0_seg) + LENGTH(dram0_0_seg); + +PHDRS +{ + dport0_0_phdr PT_LOAD; + dram0_0_phdr PT_LOAD; + dram0_0_bss_phdr PT_LOAD; + iram1_0_phdr PT_LOAD; + irom0_0_phdr PT_LOAD; +} + +ENTRY(firmware_start) +EXTERN(_DebugExceptionVector) +EXTERN(_DoubleExceptionVector) +EXTERN(_KernelExceptionVector) +EXTERN(_NMIExceptionVector) +EXTERN(_UserExceptionVector) + +PROVIDE(_memmap_vecbase_reset = 0x40000000); + +/* Various memory-map dependent cache attribute settings: */ +_memmap_cacheattr_wb_base = 0x00000110; +_memmap_cacheattr_wt_base = 0x00000110; +_memmap_cacheattr_bp_base = 0x00000220; +_memmap_cacheattr_unused_mask = 0xFFFFF00F; +_memmap_cacheattr_wb_trapnull = 0x2222211F; +_memmap_cacheattr_wba_trapnull = 0x2222211F; +_memmap_cacheattr_wbna_trapnull = 0x2222211F; +_memmap_cacheattr_wt_trapnull = 0x2222211F; +_memmap_cacheattr_bp_trapnull = 0x2222222F; +_memmap_cacheattr_wb_strict = 0xFFFFF11F; +_memmap_cacheattr_wt_strict = 0xFFFFF11F; +_memmap_cacheattr_bp_strict = 0xFFFFF22F; +_memmap_cacheattr_wb_allvalid = 0x22222112; +_memmap_cacheattr_wt_allvalid = 0x22222112; +_memmap_cacheattr_bp_allvalid = 0x22222222; +PROVIDE(_memmap_cacheattr_reset = _memmap_cacheattr_wb_trapnull); + +SECTIONS +{ + + .dport0.rodata : ALIGN(4) + { + _dport0_rodata_start = ABSOLUTE(.); + *(.dport0.rodata) + *(.dport.rodata) + _dport0_rodata_end = ABSOLUTE(.); + } >dport0_0_seg :dport0_0_phdr + + .dport0.literal : ALIGN(4) + { + _dport0_literal_start = ABSOLUTE(.); + *(.dport0.literal) + *(.dport.literal) + _dport0_literal_end = ABSOLUTE(.); + } >dport0_0_seg :dport0_0_phdr + + .dport0.data : ALIGN(4) + { + _dport0_data_start = ABSOLUTE(.); + *(.dport0.data) + *(.dport.data) + _dport0_data_end = ABSOLUTE(.); + } >dport0_0_seg :dport0_0_phdr + + .irom0.text : ALIGN(4) + { + _irom0_text_start = ABSOLUTE(.); + *(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text) + + /* we put some specific text in this section */ + + *py/argcheck.o*(.literal* .text*) + *py/asm*.o*(.literal* .text*) + *py/bc.o*(.literal* .text*) + *py/binary.o*(.literal* .text*) + *py/builtin*.o*(.literal* .text*) + *py/compile.o*(.literal* .text*) + *py/emit*.o*(.literal* .text*) + *py/formatfloat.o*(.literal* .text*) + *py/frozenmod.o*(.literal* .text*) + *py/gc.o*(.literal* .text*) + *py/lexer*.o*(.literal* .text*) + *py/malloc*.o*(.literal* .text*) + *py/map*.o*(.literal* .text*) + *py/mod*.o*(.literal* .text*) + *py/mpprint.o*(.literal* .text*) + *py/mpstate.o*(.literal* .text*) + *py/mpz.o*(.literal* .text*) + *py/native*.o*(.literal* .text*) + *py/nlr*.o*(.literal* .text*) + *py/obj*.o*(.literal* .text*) + *py/opmethods.o*(.literal* .text*) + *py/parse*.o*(.literal* .text*) + *py/qstr.o*(.literal* .text*) + *py/repl.o*(.literal* .text*) + *py/runtime.o*(.literal* .text*) + *py/scope.o*(.literal* .text*) + *py/sequence.o*(.literal* .text*) + *py/showbc.o*(.literal* .text*) + *py/smallint.o*(.literal* .text*) + *py/stackctrl.o*(.literal* .text*) + *py/stream.o*(.literal* .text*) + *py/unicode.o*(.literal* .text*) + *py/vm.o*(.literal* .text*) + *py/vstr.o*(.literal* .text*) + *py/warning.o*(.literal* .text*) + + *extmod/*.o*(.literal* .text*) + + *lib/fatfs/*.o*(.literal*, .text*) + */libaxtls.a:(.literal*, .text*) + *lib/berkeley-db-1.xx/*.o(.literal*, .text*) + *lib/libm/*.o*(.literal*, .text*) + *lib/mp-readline/*.o(.literal*, .text*) + *lib/netutils/*.o*(.literal*, .text*) + *lib/timeutils/*.o*(.literal*, .text*) + *lib/utils/*.o*(.literal*, .text*) + + *stmhal/pybstdio.o(.literal*, .text*) + + build/main.o(.literal* .text*) + *gccollect.o(.literal* .text*) + *gchelper.o(.literal* .text*) + *help.o(.literal* .text*) + *lexerstr32.o(.literal* .text*) + *utils.o(.literal* .text*) + *modpyb.o(.literal*, .text*) + *machine_pin.o(.literal*, .text*) + *machine_pwm.o(.literal*, .text*) + *machine_rtc.o(.literal*, .text*) + *machine_adc.o(.literal*, .text*) + *machine_uart.o(.literal*, .text*) + *modpybi2c.o(.literal*, .text*) + *modmachine.o(.literal*, .text*) + *machine_wdt.o(.literal*, .text*) + *machine_spi.o(.literal*, .text*) + *machine_hspi.o(.literal*, .text*) + *hspi.o(.literal*, .text*) + *modesp.o(.literal* .text*) + *modnetwork.o(.literal* .text*) + *moduos.o(.literal* .text*) + *modutime.o(.literal* .text*) + *modlwip.o(.literal* .text*) + *modsocket.o(.literal* .text*) + *modonewire.o(.literal* .text*) + + /* we put as much rodata as possible in this section */ + /* note that only rodata accessed as a machine word is allowed here */ + *py/qstr.o(.rodata.const_pool) + *.o(.rodata.mp_type_*) /* catches type: mp_obj_type_t */ + *.o(.rodata.*_locals_dict*) /* catches types: mp_obj_dict_t, mp_map_elem_t */ + *.o(.rodata.mp_module_*) /* catches types: mp_obj_module_t, mp_obj_dict_t, mp_map_elem_t */ + */frozen.o(.rodata.mp_frozen_sizes) /* frozen modules */ + */frozen.o(.rodata.mp_frozen_content) /* frozen modules */ + + /* for -mforce-l32 */ + build/*.o(.rodata*) + + _irom0_text_end = ABSOLUTE(.); + } >irom0_0_seg :irom0_0_phdr + + .text : ALIGN(4) + { + _stext = .; + _text_start = ABSOLUTE(.); + *(.UserEnter.text) + . = ALIGN(16); + *(.DebugExceptionVector.text) + . = ALIGN(16); + *(.NMIExceptionVector.text) + . = ALIGN(16); + *(.KernelExceptionVector.text) + LONG(0) + LONG(0) + LONG(0) + LONG(0) + . = ALIGN(16); + *(.UserExceptionVector.text) + LONG(0) + LONG(0) + LONG(0) + LONG(0) + . = ALIGN(16); + *(.DoubleExceptionVector.text) + LONG(0) + LONG(0) + LONG(0) + LONG(0) + . = ALIGN (16); + *(.entry.text) + *(.init.literal) + *(.init) + *(.literal .text .literal.* .text.* .iram0.literal .iram0.text .iram0.text.*.literal .iram0.text.*) + *(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*) + *(.fini.literal) + *(.fini) + *(.gnu.version) + _text_end = ABSOLUTE(.); + _etext = .; + } >iram1_0_seg :iram1_0_phdr + + .lit4 : ALIGN(4) + { + _lit4_start = ABSOLUTE(.); + *(*.lit4) + *(.lit4.*) + *(.gnu.linkonce.lit4.*) + _lit4_end = ABSOLUTE(.); + } >iram1_0_seg :iram1_0_phdr + + .data : ALIGN(4) + { + _data_start = ABSOLUTE(.); + *(.data) + *(.data.*) + *(.gnu.linkonce.d.*) + *(.data1) + *(.sdata) + *(.sdata.*) + *(.gnu.linkonce.s.*) + *(.sdata2) + *(.sdata2.*) + *(.gnu.linkonce.s2.*) + *(.jcr) + _data_end = ABSOLUTE(.); + } >dram0_0_seg :dram0_0_phdr + + .rodata : ALIGN(4) + { + _rodata_start = ABSOLUTE(.); + *(.sdk.version) + *(.rodata) + *(.rodata.*) + *(.gnu.linkonce.r.*) + *(.rodata1) + __XT_EXCEPTION_TABLE__ = ABSOLUTE(.); + *(.xt_except_table) + *(.gcc_except_table) + *(.gnu.linkonce.e.*) + *(.gnu.version_r) + *(.eh_frame) + /* C++ constructor and destructor tables, properly ordered: */ + KEEP (*crtbegin.o(.ctors)) + KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors)) + KEEP (*(SORT(.ctors.*))) + KEEP (*(.ctors)) + KEEP (*crtbegin.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + /* C++ exception handlers table: */ + __XT_EXCEPTION_DESCS__ = ABSOLUTE(.); + *(.xt_except_desc) + *(.gnu.linkonce.h.*) + __XT_EXCEPTION_DESCS_END__ = ABSOLUTE(.); + *(.xt_except_desc_end) + *(.dynamic) + *(.gnu.version_d) + . = ALIGN(4); /* this table MUST be 4-byte aligned */ + _bss_table_start = ABSOLUTE(.); + LONG(_bss_start) + LONG(_bss_end) + _bss_table_end = ABSOLUTE(.); + _rodata_end = ABSOLUTE(.); + } >dram0_0_seg :dram0_0_phdr + + .bss ALIGN(8) (NOLOAD) : ALIGN(4) + { + . = ALIGN (8); + _bss_start = ABSOLUTE(.); + *(.dynsbss) + *(.sbss) + *(.sbss.*) + *(.gnu.linkonce.sb.*) + *(.scommon) + *(.sbss2) + *(.sbss2.*) + *(.gnu.linkonce.sb2.*) + *(.dynbss) + *(.bss) + *(.bss.*) + *(.gnu.linkonce.b.*) + *(COMMON) + . = ALIGN (8); + _bss_end = ABSOLUTE(.); + _heap_start = ABSOLUTE(.); + } >dram0_0_seg :dram0_0_bss_phdr +} + +/* get ROM code address */ +INCLUDE "eagle.rom.addr.v6.ld" diff --git a/esp8266/esp_init_data.c b/esp8266/esp_init_data.c new file mode 100644 index 000000000..b14de573a --- /dev/null +++ b/esp8266/esp_init_data.c @@ -0,0 +1,77 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Paul Sokolovsky + * + * 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 <stdio.h> +#include "ets_sys.h" +#include "etshal.h" +#include "esp_mphal.h" +#include "user_interface.h" +#include "extmod/misc.h" + +NORETURN void call_user_start(void); +void ets_printf(const char *fmt, ...); +extern char flashchip; + +static const uint8_t default_init_data[] __attribute__((aligned(4))) = { +0x05, 0x00, 0x04, 0x02, 0x05, 0x05, 0x05, 0x02, 0x05, 0x00, 0x04, 0x05, 0x05, 0x04, 0x05, 0x05, +0x04, 0xfe, 0xfd, 0xff, 0xf0, 0xf0, 0xf0, 0xe0, 0xe0, 0xe0, 0xe1, 0x0a, 0xff, 0xff, 0xf8, 0x00, +0xf8, 0xf8, 0x52, 0x4e, 0x4a, 0x44, 0x40, 0x38, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, +0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0xe1, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x43, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +void firmware_start(void) { + // For SDK 1.5.2, either address has shifted and not mirrored in + // eagle.rom.addr.v6.ld, or extra initial member was added. + SpiFlashChip *flash = (SpiFlashChip*)(&flashchip + 4); + + char buf[128]; + SPIRead(flash->chip_size - 4 * 0x1000, buf, sizeof(buf)); + /*for (int i = 0; i < sizeof(buf); i++) { + static char hexf[] = "%x "; + ets_printf(hexf, buf[i]); + }*/ + + bool inited = false; + for (int i = 0; i < sizeof(buf); i++) { + if (buf[i] != 0xff) { + inited = true; + break; + } + } + + if (!inited) { + static char msg[] = "Writing init data\n"; + ets_printf(msg); + SPIRead((uint32_t)&default_init_data - 0x40200000, buf, sizeof(buf)); + SPIWrite(flash->chip_size - 4 * 0x1000, buf, sizeof(buf)); + } + + asm("j call_user_start"); +} diff --git a/esp8266/esp_mphal.h b/esp8266/esp_mphal.h index 7a71c0f03..1622667f9 100644 --- a/esp8266/esp_mphal.h +++ b/esp8266/esp_mphal.h @@ -75,7 +75,7 @@ void ets_event_poll(void); // C-level pin HAL #include "etshal.h" #include "gpio.h" -#include "esp8266/modpyb.h" +#include "esp8266/modmachine.h" #define mp_hal_pin_obj_t uint32_t #define mp_hal_get_pin_obj(o) mp_obj_get_pin(o) void mp_hal_pin_input(mp_hal_pin_obj_t pin); diff --git a/esp8266/espneopixel.c b/esp8266/espneopixel.c index 27c773106..6c7659186 100644 --- a/esp8266/espneopixel.c +++ b/esp8266/espneopixel.c @@ -33,7 +33,7 @@ void /*ICACHE_RAM_ATTR*/ esp_neopixel_write(uint8_t pin, uint8_t *pixels, uint32 #ifdef NEO_KHZ400 if(is800KHz) { #endif - time0 = fcpu / 2500000; // 0.4us + time0 = fcpu / 2857143; // 0.35us time1 = fcpu / 1250000; // 0.8us period = fcpu / 800000; // 1.25us per bit #ifdef NEO_KHZ400 diff --git a/esp8266/esponewire.c b/esp8266/esponewire.c index 797ec0bd2..22bb45b15 100644 --- a/esp8266/esponewire.c +++ b/esp8266/esponewire.c @@ -28,7 +28,7 @@ #include "etshal.h" #include "user_interface.h" -#include "modpyb.h" +#include "modmachine.h" #include "esponewire.h" #define TIMING_RESET1 (0) diff --git a/esp8266/etshal.h b/esp8266/etshal.h index e7326a43b..90af63ba2 100644 --- a/esp8266/etshal.h +++ b/esp8266/etshal.h @@ -24,7 +24,9 @@ extern void ets_wdt_disable(void); extern void wdt_feed(void); // Opaque structure -typedef char MD5_CTX[64]; +#ifndef MD5_CTX +typedef char MD5_CTX[88]; +#endif void MD5Init(MD5_CTX *context); void MD5Update(MD5_CTX *context, const void *data, unsigned int len); @@ -32,6 +34,12 @@ void MD5Final(unsigned char digest[16], MD5_CTX *context); // These prototypes are for recent SDKs with "malloc tracking" void *pvPortMalloc(unsigned sz, const char *fname, int line); +void *pvPortZalloc(unsigned sz, const char *fname, int line); +void *pvPortRealloc(void *p, unsigned sz, const char *fname, int line); void vPortFree(void *p, const char *fname, int line); +uint32_t SPIRead(uint32_t offset, void *buf, uint32_t len); +uint32_t SPIWrite(uint32_t offset, const void *buf, uint32_t len); +uint32_t SPIEraseSector(int sector); + #endif // _INCLUDED_ETSHAL_H_ diff --git a/esp8266/fatfs_port.c b/esp8266/fatfs_port.c index b87906f78..9c84f04e4 100644 --- a/esp8266/fatfs_port.c +++ b/esp8266/fatfs_port.c @@ -27,7 +27,7 @@ #include "py/obj.h" #include "lib/fatfs/ff.h" #include "timeutils.h" -#include "modpybrtc.h" +#include "modmachine.h" DWORD get_fattime(void) { diff --git a/esp8266/intr.c b/esp8266/intr.c index 62da4a721..456d6cb04 100644 --- a/esp8266/intr.c +++ b/esp8266/intr.c @@ -27,7 +27,7 @@ #include "etshal.h" #include "ets_alt_task.h" -#include "modpyb.h" +#include "modmachine.h" // this is in a separate file so it can go in iRAM void pin_intr_handler_iram(void *arg) { diff --git a/esp8266/modpybadc.c b/esp8266/machine_adc.c index 26b28c50b..26b28c50b 100644 --- a/esp8266/modpybadc.c +++ b/esp8266/machine_adc.c diff --git a/esp8266/modpybhspi.c b/esp8266/machine_hspi.c index 10a090269..10a090269 100644 --- a/esp8266/modpybhspi.c +++ b/esp8266/machine_hspi.c diff --git a/esp8266/modpybpin.c b/esp8266/machine_pin.c index 8916da64f..205c58aae 100644 --- a/esp8266/modpybpin.c +++ b/esp8266/machine_pin.c @@ -37,7 +37,7 @@ #include "py/runtime.h" #include "py/gc.h" #include "py/mphal.h" -#include "modpyb.h" +#include "modmachine.h" #define GET_TRIGGER(phys_port) \ GPIO_PIN_INT_TYPE_GET(GPIO_REG_READ(GPIO_PIN_ADDR(phys_port))) diff --git a/esp8266/modpybpwm.c b/esp8266/machine_pwm.c index 871e4c3dd..5d30f0965 100644 --- a/esp8266/modpybpwm.c +++ b/esp8266/machine_pwm.c @@ -31,7 +31,7 @@ #include "py/nlr.h" #include "py/runtime.h" -#include "modpyb.h" +#include "modmachine.h" typedef struct _pyb_pwm_obj_t { mp_obj_base_t base; diff --git a/esp8266/modpybrtc.c b/esp8266/machine_rtc.c index d69fc47a8..54eeea6f6 100644 --- a/esp8266/modpybrtc.c +++ b/esp8266/machine_rtc.c @@ -32,7 +32,7 @@ #include "py/runtime.h" #include "timeutils.h" #include "user_interface.h" -#include "modpyb.h" +#include "modmachine.h" typedef struct _pyb_rtc_obj_t { mp_obj_base_t base; diff --git a/esp8266/modpybspi.c b/esp8266/machine_spi.c index e97454711..e97454711 100644 --- a/esp8266/modpybspi.c +++ b/esp8266/machine_spi.c diff --git a/esp8266/modpybuart.c b/esp8266/machine_uart.c index 25320fa1e..80e10d131 100644 --- a/esp8266/modpybuart.c +++ b/esp8266/machine_uart.c @@ -34,7 +34,7 @@ #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" -#include "modpyb.h" +#include "modmachine.h" // UartDev is defined and initialized in rom code. extern UartDevice UartDev; diff --git a/esp8266/modmachinewdt.c b/esp8266/machine_wdt.c index 6dc4c0d18..6dc4c0d18 100644 --- a/esp8266/modmachinewdt.c +++ b/esp8266/machine_wdt.c diff --git a/esp8266/main.c b/esp8266/main.c index c938dcb30..a2e747d21 100644 --- a/esp8266/main.c +++ b/esp8266/main.c @@ -39,7 +39,7 @@ #include "gccollect.h" #include "user_interface.h" -STATIC char heap[28 * 1024]; +STATIC char heap[36 * 1024]; STATIC void mp_reset(void) { mp_stack_set_top((void*)0x40000000); @@ -141,7 +141,7 @@ mp_obj_t mp_builtin_open(uint 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) { +void MP_FASTCODE(nlr_jump_fail)(void *val) { printf("NLR jump failed\n"); for (;;) { } diff --git a/esp8266/modesp.c b/esp8266/modesp.c index 515319c9e..207422b67 100644 --- a/esp8266/modesp.c +++ b/esp8266/modesp.c @@ -45,8 +45,7 @@ #include "mem.h" #include "espneopixel.h" #include "espapa102.h" -#include "modpyb.h" -#include "modpybrtc.h" +#include "modmachine.h" #define MODESP_ESPCONN (0) @@ -629,6 +628,11 @@ STATIC mp_obj_t esp_flash_size(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_size_obj, esp_flash_size); +STATIC mp_obj_t esp_flash_user_start(void) { + return MP_OBJ_NEW_SMALL_INT(0x90000); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_user_start_obj, esp_flash_user_start); + STATIC mp_obj_t esp_check_fw(void) { MD5_CTX ctx; uint32_t *sz_p = (uint32_t*)0x40208ffc; @@ -706,6 +710,7 @@ STATIC const mp_map_elem_t esp_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_flash_write), (mp_obj_t)&esp_flash_write_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_flash_erase), (mp_obj_t)&esp_flash_erase_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_flash_size), (mp_obj_t)&esp_flash_size_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_flash_user_start), (mp_obj_t)&esp_flash_user_start_obj }, #if MODESP_ESPCONN { MP_OBJ_NEW_QSTR(MP_QSTR_socket), (mp_obj_t)&esp_socket_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&esp_getaddrinfo_obj }, diff --git a/esp8266/modmachine.c b/esp8266/modmachine.c index 0ab3c122b..29a72f7e4 100644 --- a/esp8266/modmachine.c +++ b/esp8266/modmachine.c @@ -33,8 +33,7 @@ #include "extmod/machine_mem.h" #include "extmod/machine_pulse.h" #include "extmod/machine_i2c.h" -#include "modpyb.h" -#include "modpybrtc.h" +#include "modmachine.h" #include "xtirq.h" #include "os_type.h" diff --git a/esp8266/modpyb.h b/esp8266/modmachine.h index 45d0bb8cf..df7953ecb 100644 --- a/esp8266/modpyb.h +++ b/esp8266/modmachine.h @@ -13,7 +13,7 @@ extern const mp_obj_type_t pyb_spi_type; extern const mp_obj_type_t pyb_hspi_type; extern const mp_obj_type_t machine_spi_type; -MP_DECLARE_CONST_FUN_OBJ(pyb_info_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_info_obj); typedef struct _pyb_pin_obj_t { mp_obj_base_t base; @@ -33,4 +33,11 @@ pyb_pin_obj_t *mp_obj_get_pin_obj(mp_obj_t pin_in); int pin_get(uint pin); void pin_set(uint pin, int value); +extern uint32_t pyb_rtc_alarm0_wake; +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); + #endif // __MICROPY_INCLUDED_ESP8266_MODPYB_H__ diff --git a/esp8266/modnetwork.c b/esp8266/modnetwork.c index 86bf27ca6..1d8a02bc9 100644 --- a/esp8266/modnetwork.c +++ b/esp8266/modnetwork.c @@ -100,17 +100,23 @@ STATIC mp_obj_t esp_connect(mp_uint_t n_args, const mp_obj_t *args) { mp_uint_t len; const char *p; - p = mp_obj_str_get_data(args[1], &len); - memcpy(config.ssid, p, len); - p = mp_obj_str_get_data(args[2], &len); - memcpy(config.password, p, len); + if (n_args > 1) { + p = mp_obj_str_get_data(args[1], &len); + memcpy(config.ssid, p, len); + if (n_args > 2) { + p = mp_obj_str_get_data(args[2], &len); + } else { + p = ""; + } + memcpy(config.password, p, len); - error_check(wifi_station_set_config(&config), "Cannot set STA config"); + error_check(wifi_station_set_config(&config), "Cannot set STA config"); + } error_check(wifi_station_connect(), "Cannot connect to AP"); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 3, 7, esp_connect); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 1, 7, esp_connect); STATIC mp_obj_t esp_disconnect(mp_obj_t self_in) { require_if(self_in, STATION_IF); @@ -295,7 +301,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs switch ((uintptr_t)kwargs->table[i].key) { case QS(MP_QSTR_mac): { mp_buffer_info_t bufinfo; - mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); + mp_get_buffer_raise(kwargs->table[i].value, &bufinfo, MP_BUFFER_READ); if (bufinfo.len != 6) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid buffer length")); @@ -336,6 +342,14 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs cfg.ap.channel = mp_obj_get_int(kwargs->table[i].value); break; } + case QS(MP_QSTR_dhcp_hostname): { + req_if = STATION_IF; + if (self->if_id == STATION_IF) { + const char *s = mp_obj_str_get_str(kwargs->table[i].value); + wifi_station_set_hostname((char*)s); + } + break; + } default: goto unknown; } @@ -389,6 +403,12 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs req_if = SOFTAP_IF; val = MP_OBJ_NEW_SMALL_INT(cfg.ap.channel); break; + case QS(MP_QSTR_dhcp_hostname): { + req_if = STATION_IF; + char* s = wifi_station_get_hostname(); + val = mp_obj_new_str(s, strlen(s), false); + break; + } default: goto unknown; } diff --git a/esp8266/modonewire.c b/esp8266/modonewire.c index 0d8958cee..1bf772240 100644 --- a/esp8266/modonewire.c +++ b/esp8266/modonewire.c @@ -29,7 +29,7 @@ #include "py/obj.h" #include "py/mphal.h" -#include "modpyb.h" +#include "modmachine.h" #include "esponewire.h" STATIC mp_obj_t onewire_timings(mp_obj_t timings_in) { diff --git a/esp8266/modpyb.c b/esp8266/modpyb.c index ba53e71b3..9fe8039bc 100644 --- a/esp8266/modpyb.c +++ b/esp8266/modpyb.c @@ -28,12 +28,12 @@ #include "py/gc.h" #include "gccollect.h" -#include "modpyb.h" +#include "modmachine.h" // The pyb module no longer exists since all functionality now appears // elsewhere, in more standard places (eg time, machine modules). The // only remaining function is pyb.info() which has been moved to the -// esp module, pending deletion/renaming/moving elsewher. +// esp module, pending deletion/renaming/moving elsewhere. STATIC mp_obj_t pyb_info(mp_uint_t n_args, const mp_obj_t *args) { // print info about memory diff --git a/esp8266/modpybrtc.h b/esp8266/modpybrtc.h deleted file mode 100644 index 5b9d9fc76..000000000 --- a/esp8266/modpybrtc.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2015 Josef Gajdusek - * - * 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. - */ - -extern uint32_t pyb_rtc_alarm0_wake; -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/modules/ds18x20.py b/esp8266/modules/ds18x20.py index eb22e2ae3..bf0609483 100644 --- a/esp8266/modules/ds18x20.py +++ b/esp8266/modules/ds18x20.py @@ -1,6 +1,8 @@ # DS18x20 temperature sensor driver for MicroPython. # MIT license; Copyright (c) 2016 Damien P. George +from micropython import const + _CONVERT = const(0x44) _RD_SCRATCH = const(0xbe) _WR_SCRATCH = const(0x4e) @@ -43,4 +45,7 @@ class DS18X20: t = buf[0] >> 1 return t - 0.25 + (buf[7] - buf[6]) / buf[7] else: - return (buf[1] << 8 | buf[0]) / 16 + t = buf[1] << 8 | buf[0] + if t & 0x8000: # sign bit set + t = -((t ^ 0xffff) + 1) + return t / 16 diff --git a/esp8266/modules/flashbdev.py b/esp8266/modules/flashbdev.py index 98b0208fb..e879895fb 100644 --- a/esp8266/modules/flashbdev.py +++ b/esp8266/modules/flashbdev.py @@ -3,7 +3,7 @@ import esp class FlashBdev: SEC_SIZE = 4096 - START_SEC = 0x90000 // SEC_SIZE + START_SEC = esp.flash_user_start() // SEC_SIZE NUM_BLK = 0x6b def __init__(self, blocks=NUM_BLK): diff --git a/esp8266/modules/onewire.py b/esp8266/modules/onewire.py index 06b216a57..83318d1a4 100644 --- a/esp8266/modules/onewire.py +++ b/esp8266/modules/onewire.py @@ -1,6 +1,7 @@ # 1-Wire driver for MicroPython on ESP8266 # MIT license; Copyright (c) 2016 Damien P. George +from micropython import const import _onewire as _ow class OneWireError(Exception): diff --git a/esp8266/modules/upip.py b/esp8266/modules/upip.py new file mode 120000 index 000000000..20d52a4ab --- /dev/null +++ b/esp8266/modules/upip.py @@ -0,0 +1 @@ +../../tools/upip.py
\ No newline at end of file diff --git a/esp8266/modules/upip_utarfile.py b/esp8266/modules/upip_utarfile.py new file mode 120000 index 000000000..149886291 --- /dev/null +++ b/esp8266/modules/upip_utarfile.py @@ -0,0 +1 @@ +../../tools/upip_utarfile.py
\ No newline at end of file diff --git a/esp8266/modules/webrepl.py b/esp8266/modules/webrepl.py index da3e70c59..5a76e9b26 100644 --- a/esp8266/modules/webrepl.py +++ b/esp8266/modules/webrepl.py @@ -31,6 +31,10 @@ def setup_conn(port, accept_handler): def accept_conn(listen_sock): global client_s cl, remote_addr = listen_sock.accept() + if uos.dupterm(): + print("\nConcurrent WebREPL connection from", remote_addr, "rejected") + cl.close() + return print("\nWebREPL connection from:", remote_addr) client_s = cl websocket_helper.server_handshake(cl) @@ -55,18 +59,16 @@ def start(port=8266, password=None): stop() if password is None: try: - import port_config - _webrepl.password(port_config.WEBREPL_PASS) + import webrepl_cfg + _webrepl.password(webrepl_cfg.PASS) setup_conn(port, accept_conn) print("Started webrepl in normal mode") except: - import webrepl_setup - setup_conn(port, webrepl_setup.handle_conn) - print("Started webrepl in setup mode") + print("WebREPL is not configured, run 'import webrepl_setup'") else: _webrepl.password(password) setup_conn(port, accept_conn) - print("Started webrepl in normal mode") + print("Started webrepl in manual override mode") def start_foreground(port=8266): diff --git a/esp8266/modules/webrepl_setup.py b/esp8266/modules/webrepl_setup.py index d0bf8465d..d91600e6e 100644 --- a/esp8266/modules/webrepl_setup.py +++ b/esp8266/modules/webrepl_setup.py @@ -1,83 +1,111 @@ import sys -import socket -import time +#import uos as os +import os +import machine -from websocket import * -import websocket_helper +RC = "./boot.py" +CONFIG = "./webrepl_cfg.py" - -def setup_server(): - s = socket.socket() - s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - ai = socket.getaddrinfo("0.0.0.0", 8266) - addr = ai[0][4] - - s.bind(addr) - s.listen(1) - return s - -def getpass(stream, prompt): - stream.write(prompt) - passwd = b"" +def input_choice(prompt, choices): while 1: - c = stream.read(1) - if c in (b"\r", b"\n"): - stream.write("\r\n") - return passwd - passwd += c - stream.write("*") - -def handle_conn(listen_sock): - cl, remote_addr = listen_sock.accept() - - print(""" - -First-time WebREPL connection has been received. WebREPL initial setup -will now start over this connection. During setup, UART REPL will be -non-responsive. After setup finishes, the board will be rebooted. In -case of error during setup, current session will continue. - -If you receive this message unexpectedly, it may mean that your WebREPL -connection is being hacked (power off board if unsure). -""") - - websocket_helper.server_handshake(cl) - ws = websocket(cl) - - ws.write("""\ -Welcome to MicroPython WebREPL!\r -\r -This is the first time you connect to WebREPL, so please set a password\r -to use for the following WebREPL sessions. Once you enter the password\r -twice, your board will reboot with WebREPL running in active mode. On\r -some boards, you may need to press reset button or reconnect power.\r -\r -""") + resp = input(prompt) + if resp in choices: + return resp +def getpass(prompt): + return input(prompt) + +def input_pass(): while 1: - passwd1 = getpass(ws, "New password: ") + passwd1 = getpass("New password: ") if len(passwd1) < 4: - ws.write("Password too short\r\n") + print("Password too short") continue elif len(passwd1) > 9: - ws.write("Password too long\r\n") + print("Password too long") continue - passwd2 = getpass(ws, "Confirm password: ") + passwd2 = getpass("Confirm password: ") if passwd1 == passwd2: - break - ws.write("Passwords do not match\r\n") - - with open("port_config.py", "w") as f: - f.write("WEBREPL_PASS = %r\n" % passwd1.decode("ascii")) + return passwd1 + print("Passwords do not match") - ws.write("Password successfully set, restarting...\r\n") - cl.close() - time.sleep(2) - import machine - machine.reset() +def exists(fname): + try: + with open(fname): + pass + return True + except OSError: + return False -def test(): - s = setup_server() - handle_conn(s) +def copy_stream(s_in, s_out): + buf = bytearray(64) + while 1: + sz = s_in.readinto(buf) + s_out.write(buf, sz) + + +def get_daemon_status(): + with open(RC) as f: + for l in f: + if "webrepl" in l: + if l.startswith("#"): + return False + return True + return None + +def add_daemon(): + with open(RC) as old_f, open(RC + ".tmp", "w") as new_f: + new_f.write("import webrepl\nwebrepl.start()\n") + copy_stream(old_f, new_f) + +def change_daemon(action): + LINES = ("import webrepl", "webrepl.start()") + with open(RC) as old_f, open(RC + ".tmp", "w") as new_f: + for l in old_f: + for patt in LINES: + if patt in l: + if action and l.startswith("#"): + l = l[1:] + elif not action and not l.startswith("#"): + l = "#" + l + new_f.write(l) + # FatFs rename() is not POSIX compliant, will raise OSError if + # dest file exists. + os.remove(RC) + os.rename(RC + ".tmp", RC) + + +def main(): + status = get_daemon_status() + + print("WebREPL daemon auto-start status:", "enabled" if status else "disabled") + print("\nWould you like to (E)nable or (D)isable it running on boot?") + print("(Empty line to quit)") + resp = input("> ").upper() + + if resp == "E": + if exists(CONFIG): + resp2 = input_choice("Would you like to change WebREPL password? (y/n) ", ("y", "n", "")) + else: + print("To enable WebREPL, you must set password for it") + resp2 = "y" + + if resp2 == "y": + passwd = input_pass() + with open(CONFIG, "w") as f: + f.write("PASS = %r\n" % passwd) + + + if resp not in ("D", "E") or (resp == "D" and not status) or (resp == "E" and status): + print("No further action required") + sys.exit() + + change_daemon(resp == "E") + + print("Changes will be activated after reboot") + resp = input_choice("Would you like to reboot now? (y/n) ", ("y", "n", "")) + if resp == "y": + machine.reset() + +main() diff --git a/esp8266/modutime.c b/esp8266/modutime.c index abfe069cc..2adb6c563 100644 --- a/esp8266/modutime.c +++ b/esp8266/modutime.c @@ -34,8 +34,7 @@ #include "py/runtime.h" #include "py/mphal.h" #include "py/smallint.h" -#include "modpyb.h" -#include "modpybrtc.h" +#include "modmachine.h" #include "timeutils.h" #include "user_interface.h" #include "extmod/utime_mphal.h" @@ -108,19 +107,20 @@ STATIC mp_obj_t time_time(void) { } MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); -STATIC const mp_map_elem_t time_module_globals_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) }, +STATIC const mp_rom_map_elem_t time_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_localtime), (mp_obj_t)&time_localtime_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mktime), (mp_obj_t)&time_mktime_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&mp_utime_sleep_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&mp_utime_sleep_ms_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&mp_utime_sleep_us_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&mp_utime_ticks_cpu_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_time_obj }, + { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, + { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, + { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index 201057f12..602b3e9c8 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -182,4 +182,6 @@ extern const struct _mp_obj_module_t onewire_module; #define MICROPY_HW_MCU_NAME "ESP8266" #define MICROPY_PY_SYS_PLATFORM "esp8266" +#define MP_FASTCODE(n) __attribute__((section(".iram0.text." #n))) n + #define _assert(expr) ((expr) ? (void)0 : __assert_func(__FILE__, __LINE__, __func__, #expr)) diff --git a/esp8266/mpconfigport_512k.h b/esp8266/mpconfigport_512k.h new file mode 100644 index 000000000..f0de6035f --- /dev/null +++ b/esp8266/mpconfigport_512k.h @@ -0,0 +1,19 @@ +#include <mpconfigport.h> + +#undef MICROPY_FSUSERMOUNT +#define MICROPY_FSUSERMOUNT (0) +#undef MICROPY_VFS_FAT +#define MICROPY_VFS_FAT (0) + +#undef MICROPY_PERSISTENT_CODE_LOAD +#define MICROPY_PERSISTENT_CODE_LOAD (0) + +#undef MICROPY_PY_IO_FILEIO +#define MICROPY_PY_IO_FILEIO (0) + +#undef MICROPY_PY_SYS_STDIO_BUFFER +#define MICROPY_PY_SYS_STDIO_BUFFER (0) +#undef MICROPY_PY_BUILTINS_SLICE_ATTRS +#define MICROPY_PY_BUILTINS_SLICE_ATTRS (0) +#undef MICROPY_PY_ALL_SPECIAL_METHODS +#define MICROPY_PY_ALL_SPECIAL_METHODS (0) diff --git a/esp8266/scripts/apa102.py b/esp8266/scripts/apa102.py index 126448cc2..41b7c0485 100644 --- a/esp8266/scripts/apa102.py +++ b/esp8266/scripts/apa102.py @@ -2,27 +2,16 @@ # MIT license; Copyright (c) 2016 Robert Foss, Daniel Busch from esp import apa102_write +from neopixel import NeoPixel -class APA102: - def __init__(self, clock_pin, data_pin, n): - self.clock_pin = clock_pin - self.data_pin = data_pin - self.n = n - self.buf = bytearray(n * 4) - - self.clock_pin.init(clock_pin.OUT) - self.data_pin.init(data_pin.OUT) - def __setitem__(self, index, val): - r, g, b, brightness = val - self.buf[index * 4] = r - self.buf[index * 4 + 1] = g - self.buf[index * 4 + 2] = b - self.buf[index * 4 + 3] = brightness +class APA102(NeoPixel): + ORDER = (0, 1, 2, 3) - def __getitem__(self, index): - i = index * 4 - return self.buf[i], self.buf[i + 1], self.buf[i + 2], self.buf[i + 3] + def __init__(self, clock_pin, data_pin, n, bpp=4): + super().__init__(data_pin, n, bpp) + self.clock_pin = clock_pin + self.clock_pin.init(clock_pin.OUT) def write(self): - apa102_write(self.clock_pin, self.data_pin, self.buf) + apa102_write(self.clock_pin, self.pin, self.buf) diff --git a/esp8266/scripts/neopixel.py b/esp8266/scripts/neopixel.py index 8aa034868..b13424d7d 100644 --- a/esp8266/scripts/neopixel.py +++ b/esp8266/scripts/neopixel.py @@ -3,29 +3,30 @@ from esp import neopixel_write + class NeoPixel: - def __init__(self, pin, n): + ORDER = (1, 0, 2, 3) + + def __init__(self, pin, n, bpp=3): self.pin = pin self.n = n - self.buf = bytearray(n * 3) + self.bpp = bpp + self.buf = bytearray(n * bpp) self.pin.init(pin.OUT) def __setitem__(self, index, val): - r, g, b = val - self.buf[index * 3] = g - self.buf[index * 3 + 1] = r - self.buf[index * 3 + 2] = b + offset = index * self.bpp + for i in range(self.bpp): + self.buf[offset + self.ORDER[i]] = val[i] def __getitem__(self, index): - i = index * 3 - return self.buf[i + 1], self.buf[i], self.buf[i + 2] + offset = index * self.bpp + return tuple(self.buf[offset + self.ORDER[i]] + for i in range(self.bpp)) def fill(self, color): - r, g, b = color - for i in range(len(self.buf) / 3): - self.buf[i * 3] = g - self.buf[i * 3 + 1] = r - self.buf[i * 3 + 2] = b + for i in range(self.n): + self[i] = color def write(self): neopixel_write(self.pin, self.buf, True) diff --git a/esp8266/scripts/port_diag.py b/esp8266/scripts/port_diag.py index f2abd8066..ef8800355 100644 --- a/esp8266/scripts/port_diag.py +++ b/esp8266/scripts/port_diag.py @@ -24,8 +24,8 @@ def main(): print("STA ifconfig:", network.WLAN(network.STA_IF).ifconfig()) print("AP ifconfig:", network.WLAN(network.AP_IF).ifconfig()) print("Free WiFi driver buffers of type:") - for i in range(5): - print("%d: %d" % (i, esp.esf_free_bufs(i))) + for i, comm in enumerate(("1,2 TX", "4 Mngmt TX(len: 0x41-0x100)", "5 Mngmt TX (len: 0-0x40)", "7", "8 RX")): + print("%d: %d (%s)" % (i, esp.esf_free_bufs(i), comm)) print("lwIP PCBs:") lwip.print_pcbs() |