summaryrefslogtreecommitdiff
path: root/esp8266
diff options
context:
space:
mode:
authorScott Shawcroft <scott@chickadee.tech>2016-11-03 15:50:59 -0700
committerScott Shawcroft <scott.shawcroft@gmail.com>2016-11-21 14:11:52 -0800
commitccbb5e84f980e252f7380d96fcc5c417a8a64523 (patch)
tree743fef26c6176aca6582b92a85829a565e60c414 /esp8266
parent93218281588b42331d83e4aaa45ac1abe4af130b (diff)
This introduces an alternative hardware API called nativeio structured around different functions that are typically accelerated by native hardware. Its not meant to reflect the structure of the hardware.
Docs are here: http://tannewt-micropython.readthedocs.io/en/microcontroller/ It differs from upstream's machine in the following ways: * Python API is identical across ports due to code structure. (Lives in shared-bindings) * Focuses on abstracting common functionality (AnalogIn) and not representing structure (ADC). * Documentation lives with code making it easy to ensure they match. * Pin is split into references (board.D13 and microcontroller.pin.PA17) and functionality (DigitalInOut). * All nativeio classes claim underlying hardware resources when inited on construction, support Context Managers (aka with statements) and have deinit methods which release the claimed hardware. * All constructors take pin references rather than peripheral ids. Its up to the implementation to find hardware or throw and exception.
Diffstat (limited to 'esp8266')
-rw-r--r--esp8266/Makefile40
-rw-r--r--esp8266/boards/feather_huzzah/pins.c48
-rw-r--r--esp8266/common-hal/board/__init__.c34
-rw-r--r--esp8266/common-hal/microcontroller/Pin.c1
-rw-r--r--esp8266/common-hal/microcontroller/__init__.c94
-rw-r--r--esp8266/common-hal/microcontroller/__init__.h51
-rw-r--r--esp8266/common-hal/microcontroller/types.h40
-rw-r--r--esp8266/common-hal/nativeio/AnalogIn.c45
-rw-r--r--esp8266/common-hal/nativeio/AnalogOut.c47
-rw-r--r--esp8266/common-hal/nativeio/DigitalInOut.c174
-rw-r--r--esp8266/common-hal/nativeio/I2C.c51
-rw-r--r--esp8266/common-hal/nativeio/PWMOut.c67
-rw-r--r--esp8266/common-hal/nativeio/SPI.c90
-rw-r--r--esp8266/common-hal/nativeio/__init__.c1
-rw-r--r--esp8266/common-hal/nativeio/types.h68
-rw-r--r--esp8266/common-hal/neopixel_write/__init__.c33
-rw-r--r--esp8266/common-hal/time/__init__.c40
-rw-r--r--esp8266/esp8266.ld3
-rw-r--r--esp8266/ets_alt_task.h8
-rw-r--r--esp8266/machine_pwm.c2
-rw-r--r--esp8266/mpconfigport.h12
21 files changed, 946 insertions, 3 deletions
diff --git a/esp8266/Makefile b/esp8266/Makefile
index b2191353a..45ab401dd 100644
--- a/esp8266/Makefile
+++ b/esp8266/Makefile
@@ -1,5 +1,15 @@
include ../py/mkenv.mk
+# Select the board to build for: if not given on the command line,
+# then default to PYBV10.
+BOARD ?= feather_huzzah
+ifeq ($(wildcard boards/$(BOARD)/.),)
+$(error Invalid BOARD specified)
+endif
+
+# If the build directory is not given, make it reflect the board name.
+BUILD ?= build-$(BOARD)
+
# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h #$(BUILD)/pins_qstr.h
@@ -93,8 +103,34 @@ SRC_C = \
fatfs_port.c \
axtls_helpers.c \
hspi.c \
+ boards/$(BOARD)/pins.c \
$(SRC_MOD)
+SRC_COMMON_HAL = \
+ microcontroller/__init__.c \
+ microcontroller/Pin.c \
+ nativeio/__init__.c \
+ nativeio/AnalogIn.c \
+ nativeio/AnalogOut.c \
+ nativeio/DigitalInOut.c \
+ nativeio/I2C.c \
+ nativeio/PWMOut.c \
+ nativeio/SPI.c \
+ neopixel_write/__init__.c \
+ time/__init__.c \
+ board/__init__.c
+
+SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
+ $(addprefix common-hal/, $(SRC_COMMON_HAL))
+
+SRC_SHARED_MODULE = \
+ bitbangio/__init__.c \
+ bitbangio/I2C.c \
+ bitbangio/SPI.c \
+
+SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
+ $(addprefix shared-module/, $(SRC_SHARED_MODULE))
+
STM_SRC_C = $(addprefix stmhal/,\
pybstdio.c \
input.c \
@@ -149,6 +185,8 @@ OBJ =
OBJ += $(PY_O)
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
+OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o))
+OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(STM_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(EXTMOD_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
@@ -156,7 +194,7 @@ OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
#OBJ += $(BUILD)/pins_$(BOARD).o
# List of sources for qstr extraction
-SRC_QSTR += $(SRC_C) $(STM_SRC_C) $(EXTMOD_SRC_C) $(DRIVERS_SRC_C)
+SRC_QSTR += $(SRC_C) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) $(STM_SRC_C) $(EXTMOD_SRC_C) $(DRIVERS_SRC_C)
# Append any auto-generated sources that are needed by sources listed in SRC_QSTR
SRC_QSTR_AUTO_DEPS +=
diff --git a/esp8266/boards/feather_huzzah/pins.c b/esp8266/boards/feather_huzzah/pins.c
new file mode 100644
index 000000000..c68555392
--- /dev/null
+++ b/esp8266/boards/feather_huzzah/pins.c
@@ -0,0 +1,48 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ *
+ * 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 "common-hal/microcontroller/__init__.h"
+
+STATIC const mp_map_elem_t board_global_dict_table[] = {
+ { MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pin_TOUT },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO16), (mp_obj_t)&pin_XPD_DCDC },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO14), (mp_obj_t)&pin_MTMS },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SCK), (mp_obj_t)&pin_MTMS },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO12), (mp_obj_t)&pin_MTDI },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_MISO), (mp_obj_t)&pin_MTDI },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO13), (mp_obj_t)&pin_MTCK },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_MOSI), (mp_obj_t)&pin_MTCK },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO15), (mp_obj_t)&pin_MTDO },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO2), (mp_obj_t)&pin_GPIO2 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO0), (mp_obj_t)&pin_GPIO0 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO4), (mp_obj_t)&pin_GPIO4 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SDA), (mp_obj_t)&pin_GPIO4 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_RX), (mp_obj_t)&pin_U0RXD },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_TX), (mp_obj_t)&pin_U0TXD },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO5), (mp_obj_t)&pin_DVDD },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SCL), (mp_obj_t)&pin_DVDD },
+};
+MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);
diff --git a/esp8266/common-hal/board/__init__.c b/esp8266/common-hal/board/__init__.c
new file mode 100644
index 000000000..3893e70f7
--- /dev/null
+++ b/esp8266/common-hal/board/__init__.c
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 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 <string.h>
+
+#include "py/runtime.h"
+#include "py/mphal.h"
+#include "common-hal/microcontroller/types.h"
+
+// Pins aren't actually defined here. They are in the board specific directory
+// such as boards/feather_huzzah/pins.c.
diff --git a/esp8266/common-hal/microcontroller/Pin.c b/esp8266/common-hal/microcontroller/Pin.c
new file mode 100644
index 000000000..2e8b72a2c
--- /dev/null
+++ b/esp8266/common-hal/microcontroller/Pin.c
@@ -0,0 +1 @@
+// Pins have no behavior.
diff --git a/esp8266/common-hal/microcontroller/__init__.c b/esp8266/common-hal/microcontroller/__init__.c
new file mode 100644
index 000000000..09ea92067
--- /dev/null
+++ b/esp8266/common-hal/microcontroller/__init__.c
@@ -0,0 +1,94 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ *
+ * 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 "common-hal/microcontroller/types.h"
+#include "shared-bindings/microcontroller/Pin.h"
+
+#include "eagle_soc.h"
+#include "osapi.h"
+#include "etshal.h"
+
+void common_hal_mcu_delay_us(uint32_t delay) {
+ os_delay_us(delay);
+}
+
+// This macro is used to simplify pin definition in boards/<board>/pins.c
+#define PIN(p_name, p_gpio_number, p_gpio_function, p_peripheral) \
+const mcu_pin_obj_t pin_## p_name = { \
+ { &mcu_pin_type }, \
+ .name = MP_QSTR_ ## p_name, \
+ .gpio_number = p_gpio_number, \
+ .gpio_function = p_gpio_function, \
+ .peripheral = p_peripheral, \
+}
+
+#define NO_GPIO 0xff
+#define SPECIAL_CASE 0xfe
+
+// Using microcontroller names from the datasheet.
+// https://cdn-shop.adafruit.com/datasheets/ESP8266_Specifications_English.pdf
+// PIN(mcu name) // function notes | module name | huzzah name
+PIN(TOUT, NO_GPIO, NO_GPIO, NO_GPIO); // adc | ADC | ADC
+PIN(XPD_DCDC, 16, SPECIAL_CASE, SPECIAL_CASE); // gpio16 | GPIO16 | GPIO16
+PIN(MTMS, 14, FUNC_GPIO14, PERIPHS_IO_MUX_MTMS_U); // gpio14 / hspi_clk / pwm2 | GPIO14 | GPIO14/SCK
+PIN(MTDI, 12, FUNC_GPIO12, PERIPHS_IO_MUX_MTDI_U); // gpio12 / hspi_miso / pwm0 | GPIO12 | GPIO12/MISO
+PIN(MTCK, 13, FUNC_GPIO13, PERIPHS_IO_MUX_MTCK_U); // gpio13 / hspi_mosi / U0cts | GPIO13 | GPIO13/MOSI
+PIN(MTDO, 15, FUNC_GPIO15, PERIPHS_IO_MUX_MTDO_U); // gpio15 / hspi_cs / u0rts / pwm1 | GPIO15 | GPIO15
+PIN(GPIO2, 2, FUNC_GPIO2, PERIPHS_IO_MUX_GPIO2_U); // U1txd | GPIO2 | GPIO2
+PIN(GPIO0, 0, FUNC_GPIO0, PERIPHS_IO_MUX_GPIO0_U); // spi_Cs2 | GPIO0 | GPIO0
+PIN(GPIO4, 4, FUNC_GPIO4, PERIPHS_IO_MUX_GPIO4_U); // pwm3 on mcu datasheet as vdd which must be wrong | GPIO4 | GPIO4/SDA
+PIN(SD_DATA_2, 9, FUNC_GPIO9, PERIPHS_IO_MUX_SD_DATA2_U); // spihd / hspihd / gpio9 | GPIO9
+PIN(SD_DATA_3, 10, FUNC_GPIO10, PERIPHS_IO_MUX_SD_DATA3_U); // spiwp / hspiwp / gpio10 | GPIO10
+PIN(SD_CMD, NO_GPIO, NO_GPIO, PERIPHS_IO_MUX_SD_CMD_U); // spi_cs0 / gpio11 | CS0
+PIN(SD_CLK, NO_GPIO, NO_GPIO, PERIPHS_IO_MUX_SD_CLK_U); // spi_clk / gpio6 | SCLK
+PIN(SD_DATA_0, NO_GPIO, NO_GPIO, PERIPHS_IO_MUX_SD_DATA0_U); // spi_miso / gpio7 | MISO
+PIN(SD_DATA_1, NO_GPIO, NO_GPIO, PERIPHS_IO_MUX_SD_DATA1_U); // spi_mosi / gpio8 / u1rxd | MOSI
+PIN(DVDD, 5, FUNC_GPIO5, PERIPHS_IO_MUX_GPIO5_U); // gpio5 | GPIO5 | GPIO5/SCL
+PIN(U0RXD, 3, FUNC_GPIO3, PERIPHS_IO_MUX_U0RXD_U); // gpio3 | RXD0 | RXD
+PIN(U0TXD, 1, FUNC_GPIO1, PERIPHS_IO_MUX_U0TXD_U); // gpio1 / spi_cs1 | TXD0 | TXD
+
+// This maps MCU pin names to pin objects.
+STATIC const mp_map_elem_t mcu_pin_global_dict_table[] = {
+ { MP_OBJ_NEW_QSTR(MP_QSTR_TOUT), (mp_obj_t)&pin_TOUT },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_XPD_DCDC), (mp_obj_t)&pin_XPD_DCDC },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_MTMS), (mp_obj_t)&pin_MTMS },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_MTDI), (mp_obj_t)&pin_MTDI },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_MTCK), (mp_obj_t)&pin_MTCK },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_MTDO), (mp_obj_t)&pin_MTDO },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO2), (mp_obj_t)&pin_GPIO2 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO0), (mp_obj_t)&pin_GPIO0 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_GPIO4), (mp_obj_t)&pin_GPIO4 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SD_DATA_2), (mp_obj_t)&pin_SD_DATA_2 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SD_DATA_3), (mp_obj_t)&pin_SD_DATA_3 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SD_CMD), (mp_obj_t)&pin_SD_CMD },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SD_CLK), (mp_obj_t)&pin_SD_CLK },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SD_DATA_0), (mp_obj_t)&pin_SD_DATA_0 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_SD_DATA_1), (mp_obj_t)&pin_SD_DATA_1 },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_DVDD), (mp_obj_t)&pin_DVDD },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_U0RXD), (mp_obj_t)&pin_U0RXD },
+ { MP_OBJ_NEW_QSTR(MP_QSTR_U0TXD), (mp_obj_t)&pin_U0TXD },
+};
+MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_global_dict_table);
diff --git a/esp8266/common-hal/microcontroller/__init__.h b/esp8266/common-hal/microcontroller/__init__.h
new file mode 100644
index 000000000..c82df49e9
--- /dev/null
+++ b/esp8266/common-hal/microcontroller/__init__.h
@@ -0,0 +1,51 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ *
+ * 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.
+ */
+
+ #ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER___INIT___H__
+ #define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER___INIT___H__
+
+#include "common-hal/microcontroller/types.h"
+
+extern const mcu_pin_obj_t pin_TOUT;
+extern const mcu_pin_obj_t pin_XPD_DCDC;
+extern const mcu_pin_obj_t pin_MTMS;
+extern const mcu_pin_obj_t pin_MTDI;
+extern const mcu_pin_obj_t pin_MTCK;
+extern const mcu_pin_obj_t pin_MTDO;
+extern const mcu_pin_obj_t pin_GPIO2;
+extern const mcu_pin_obj_t pin_GPIO0;
+extern const mcu_pin_obj_t pin_GPIO4;
+extern const mcu_pin_obj_t pin_SD_DATA_2;
+extern const mcu_pin_obj_t pin_SD_DATA_3;
+extern const mcu_pin_obj_t pin_SD_CMD;
+extern const mcu_pin_obj_t pin_SD_CLK;
+extern const mcu_pin_obj_t pin_SD_DATA_0;
+extern const mcu_pin_obj_t pin_SD_DATA_1;
+extern const mcu_pin_obj_t pin_DVDD;
+extern const mcu_pin_obj_t pin_U0RXD;
+extern const mcu_pin_obj_t pin_U0TXD;
+
+#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER___INIT___H__
diff --git a/esp8266/common-hal/microcontroller/types.h b/esp8266/common-hal/microcontroller/types.h
new file mode 100644
index 000000000..8245cd303
--- /dev/null
+++ b/esp8266/common-hal/microcontroller/types.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * 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.
+ */
+
+#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_TYPES_H__
+#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_TYPES_H__
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ qstr name;
+ uint8_t gpio_number;
+ uint8_t gpio_function;
+ uint32_t peripheral;
+} mcu_pin_obj_t;
+
+#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_TYPES_H__
diff --git a/esp8266/common-hal/nativeio/AnalogIn.c b/esp8266/common-hal/nativeio/AnalogIn.c
new file mode 100644
index 000000000..efdd2d2ac
--- /dev/null
+++ b/esp8266/common-hal/nativeio/AnalogIn.c
@@ -0,0 +1,45 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ *
+ * 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 <string.h>
+
+#include "py/nlr.h"
+#include "py/runtime.h"
+#include "py/binary.h"
+#include "py/mphal.h"
+#include "shared-bindings/nativeio/AnalogIn.h"
+
+#include "user_interface.h"
+
+void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
+ const mcu_pin_obj_t *pin) {
+}
+
+uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) {
+ // ADC is 10 bit so shift by 6 to make it 16-bit.
+ return system_adc_read() << 6;
+}
diff --git a/esp8266/common-hal/nativeio/AnalogOut.c b/esp8266/common-hal/nativeio/AnalogOut.c
new file mode 100644
index 000000000..06b1880c3
--- /dev/null
+++ b/esp8266/common-hal/nativeio/AnalogOut.c
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 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 <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "py/runtime.h"
+
+#include "shared-bindings/nativeio/AnalogOut.h"
+
+
+void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
+ const mcu_pin_obj_t *pin) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
+ "No hardware support for analog out."));
+}
+
+void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self) {
+}
+
+void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self,
+ uint16_t value) {
+}
diff --git a/esp8266/common-hal/nativeio/DigitalInOut.c b/esp8266/common-hal/nativeio/DigitalInOut.c
new file mode 100644
index 000000000..d933864c4
--- /dev/null
+++ b/esp8266/common-hal/nativeio/DigitalInOut.c
@@ -0,0 +1,174 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2013, 2014 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 <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "py/nlr.h"
+#include "py/runtime.h"
+#include "py/mphal.h"
+
+#include "shared-bindings/nativeio/DigitalInOut.h"
+
+digitalinout_result_t common_hal_nativeio_digitalinout_construct(
+ nativeio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin) {
+ self->pin = pin;
+ PIN_FUNC_SELECT(self->pin->peripheral, self->pin->gpio_function);
+ return DIGITALINOUT_OK;
+}
+
+void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self) {
+ uint32_t pin_mask = 1 << self->pin->gpio_number;
+ gpio_output_set(0x0, 0x0, 0x0, pin_mask);
+}
+
+void common_hal_nativeio_digitalinout_switch_to_input(
+ nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
+ self->output = false;
+
+ if (self->pin->gpio_number == 16) {
+ WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1);
+ WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1);
+ WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1)); // input
+ } else {
+ PIN_PULLUP_DIS(self->pin->peripheral);
+ gpio_output_set(0, 0, 0, 1 << self->pin->gpio_number);
+ }
+ common_hal_nativeio_digitalinout_set_pull(self, pull);
+}
+
+void common_hal_nativeio_digitalinout_switch_to_output(
+ nativeio_digitalinout_obj_t* self, bool value,
+ enum digitalinout_drive_mode_t drive_mode) {
+ self->output = true;
+ self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
+ if (self->pin->gpio_number == 16) {
+ WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1);
+ WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1);
+ WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1) | 1); // output
+ } else if (!self->open_drain) {
+ gpio_output_set(0, 0, 1 << self->pin->gpio_function, 0);
+ PIN_PULLUP_DIS(self->pin->peripheral);
+ }
+ common_hal_nativeio_digitalinout_set_value(self, value);
+}
+
+enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction(
+ nativeio_digitalinout_obj_t* self) {
+ return self->output? DIRECTION_OUT : DIRECTION_IN;
+}
+
+void common_hal_nativeio_digitalinout_set_value(
+ nativeio_digitalinout_obj_t* self, bool value) {
+ if (value) {
+ if (self->open_drain) {
+ // Disable output.
+ gpio_output_set(0, 0, 0, 1 << self->pin->gpio_number);
+ PIN_PULLUP_EN(self->pin->peripheral);
+ } else {
+ // Set high
+ gpio_output_set(1 << self->pin->gpio_number, 0, 0, 0);
+ }
+ } else {
+ if (self->open_drain) {
+ // Enable the output
+ PIN_PULLUP_DIS(self->pin->peripheral);
+ gpio_output_set(0, 0, 1 << self->pin->gpio_number, 0);
+ }
+ // Set low
+ gpio_output_set(0, 1 << self->pin->gpio_number, 0, 0);
+ }
+}
+
+// Register addresses taken from: https://github.com/esp8266/esp8266-wiki/wiki/gpio-registers
+volatile uint32_t* PIN_DIR = (uint32_t *) 0x6000030C;
+volatile uint32_t* PIN_OUT = (uint32_t *) 0x60000300;
+bool common_hal_nativeio_digitalinout_get_value(
+ nativeio_digitalinout_obj_t* self) {
+ if (!self->output) {
+ if (self->pin->gpio_number == 16) {
+ return READ_PERI_REG(RTC_GPIO_IN_DATA) & 1;
+ }
+ return GPIO_INPUT_GET(self->pin->gpio_number);
+ } else {
+ uint32_t pin_mask = 1 << self->pin->gpio_number;
+ if (self->open_drain && ((*PIN_DIR) & pin_mask) == 0) {
+ return true;
+ } else {
+ return ((*PIN_OUT) & pin_mask) != 0;
+ }
+ }
+}
+
+void common_hal_nativeio_digitalinout_set_drive_mode(
+ nativeio_digitalinout_obj_t* self,
+ enum digitalinout_drive_mode_t drive_mode) {
+ bool value = common_hal_nativeio_digitalinout_get_value(self);
+ self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
+ // True is implemented differently between modes so reset the value to make
+ // sure its correct for the new mode.
+ if (value) {
+ common_hal_nativeio_digitalinout_set_value(self, value);
+ }
+}
+
+enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(
+ nativeio_digitalinout_obj_t* self) {
+ if (self->open_drain) {
+ return DRIVE_MODE_OPEN_DRAIN;
+ } else {
+ return DRIVE_MODE_PUSH_PULL;
+ }
+}
+
+void common_hal_nativeio_digitalinout_set_pull(
+ nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
+ if (pull == PULL_DOWN) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
+ "ESP8266 does not support pull down."));
+ return;
+ }
+ if (self->pin->gpio_number == 16) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
+ "Pin does not support pull."));
+ return;
+ }
+ if (pull == PULL_NONE) {
+ PIN_PULLUP_DIS(self->pin->peripheral);
+ } else {
+ PIN_PULLUP_EN(self->pin->peripheral);
+ }
+}
+
+enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull(
+ nativeio_digitalinout_obj_t* self) {
+ if (self->pin->gpio_number < 16 &&
+ (READ_PERI_REG(self->pin->peripheral) & PERIPHS_IO_MUX_PULLUP) != 0) {
+ return PULL_UP;
+ }
+ return PULL_NONE;
+}
diff --git a/esp8266/common-hal/nativeio/I2C.c b/esp8266/common-hal/nativeio/I2C.c
new file mode 100644
index 000000000..2307686e2
--- /dev/null
+++ b/esp8266/common-hal/nativeio/I2C.c
@@ -0,0 +1,51 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * 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 "shared-bindings/nativeio/I2C.h"
+#include "py/nlr.h"
+
+void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
+ const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t freq) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
+ "No hardware support for I2C. Use bitbangio instead."));
+}
+
+void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self) {
+}
+
+bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr) {
+ return false;
+}
+
+bool common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t addr,
+ const uint8_t * data, size_t len, bool transmit_stop_bit) {
+ return false;
+}
+
+bool common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t addr,
+ uint8_t * data, size_t len) {
+ return false;
+}
diff --git a/esp8266/common-hal/nativeio/PWMOut.c b/esp8266/common-hal/nativeio/PWMOut.c
new file mode 100644
index 000000000..c12bc7cf2
--- /dev/null
+++ b/esp8266/common-hal/nativeio/PWMOut.c
@@ -0,0 +1,67 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 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 <stdio.h>
+#include <stdint.h>
+
+#include "esppwm.h"
+
+#include "py/runtime.h"
+#include "shared-bindings/nativeio/PWMOut.h"
+
+// Shared with pybpwm
+extern bool pwm_inited;
+
+void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty) {
+ // start the PWM subsystem if it's not already running
+ if (!pwm_inited) {
+ pwm_init();
+ pwm_inited = true;
+ }
+
+ self->channel = pwm_add(pin->gpio_number,
+ pin->peripheral,
+ pin->gpio_function);
+ if (self->channel == -1) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
+ "PWM not supported on pin %d", pin->gpio_number));
+ }
+}
+
+extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) {
+ pwm_delete(self->channel);
+ pwm_start();
+}
+
+extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty) {
+ // We get 16 bits of duty in but the underlying code is only ten bit.
+ pwm_set_duty(duty >> 6, self->channel);
+ pwm_start();
+}
+
+uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self) {
+ return pwm_get_duty(self->channel) << 6;
+}
diff --git a/esp8266/common-hal/nativeio/SPI.c b/esp8266/common-hal/nativeio/SPI.c
new file mode 100644
index 000000000..c517dd125
--- /dev/null
+++ b/esp8266/common-hal/nativeio/SPI.c
@@ -0,0 +1,90 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * 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 "esp8266/ets_alt_task.h"
+#include "esp8266/hspi.h"
+#include "shared-bindings/nativeio/SPI.h"
+#include "py/nlr.h"
+
+#include "eagle_soc.h"
+
+extern const mcu_pin_obj_t pin_MTMS;
+extern const mcu_pin_obj_t pin_MTCK;
+extern const mcu_pin_obj_t pin_MTDI;
+
+void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
+ const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
+ const mcu_pin_obj_t * miso, uint32_t baudrate) {
+ if (clock != &pin_MTMS || mosi != &pin_MTCK || miso != &pin_MTDI) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
+ "Pins not valid for SPI"));
+ }
+}
+
+void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) {
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 0);
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 0);
+ PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 0);
+}
+
+bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self,
+ const uint8_t * data, size_t len) {
+ size_t chunk_size = 1024;
+ size_t count = len / chunk_size;
+ size_t i = 0;
+ for (size_t j = 0; j < count; ++j) {
+ for (size_t k = 0; k < chunk_size; ++k) {
+ spi_tx8fast(HSPI, data[i]);
+ ++i;
+ }
+ ets_loop_iter();
+ }
+ while (i < len) {
+ spi_tx8fast(HSPI, data[i]);
+ ++i;
+ }
+ return true;
+}
+
+bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self,
+ uint8_t * data, size_t len) {
+ // Process data in chunks, let the pending tasks run in between
+ size_t chunk_size = 1024; // TODO this should depend on baudrate
+ size_t count = len / chunk_size;
+ size_t i = 0;
+ for (size_t j = 0; j < count; ++j) {
+ for (size_t k = 0; k < chunk_size; ++k) {
+ data[i] = spi_rx8(HSPI);
+ ++i;
+ }
+ ets_loop_iter();
+ }
+ while (i < len) {
+ data[i] = spi_rx8(HSPI);
+ ++i;
+ }
+ return true;
+}
diff --git a/esp8266/common-hal/nativeio/__init__.c b/esp8266/common-hal/nativeio/__init__.c
new file mode 100644
index 000000000..e0ec8acfb
--- /dev/null
+++ b/esp8266/common-hal/nativeio/__init__.c
@@ -0,0 +1 @@
+// No nativeio module functions.
diff --git a/esp8266/common-hal/nativeio/types.h b/esp8266/common-hal/nativeio/types.h
new file mode 100644
index 000000000..7cc18fd25
--- /dev/null
+++ b/esp8266/common-hal/nativeio/types.h
@@ -0,0 +1,68 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft
+ *
+ * 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.
+ */
+
+// This defines the types used to underly the standard nativeio Python objects.
+// The shared API is defined in terms of these types.
+
+#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__
+#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__
+
+#include "common-hal/microcontroller/types.h"
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ const mcu_pin_obj_t * pin;
+} nativeio_analogin_obj_t;
+
+// Not supported, throws error on construction.
+typedef struct {
+ mp_obj_base_t base;
+} nativeio_analogout_obj_t;
+
+typedef struct {
+ mp_obj_base_t base;
+ const mcu_pin_obj_t * pin;
+ bool output;
+ bool open_drain;
+} nativeio_digitalinout_obj_t;
+
+// Not supported, throws error on construction.
+typedef struct {
+ mp_obj_base_t base;
+} nativeio_i2c_obj_t;
+
+typedef struct {
+ mp_obj_base_t base;
+} nativeio_spi_obj_t;
+
+typedef struct {
+ mp_obj_base_t base;
+ int channel;
+} nativeio_pwmout_obj_t;
+
+#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__
diff --git a/esp8266/common-hal/neopixel_write/__init__.c b/esp8266/common-hal/neopixel_write/__init__.c
new file mode 100644
index 000000000..efc161256
--- /dev/null
+++ b/esp8266/common-hal/neopixel_write/__init__.c
@@ -0,0 +1,33 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ *
+ * 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 "shared-bindings/neopixel_write/__init__.h"
+
+#include "espneopixel.h"
+
+void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {
+ esp_neopixel_write(digitalinout->pin->gpio_number, pixels, numBytes, is800KHz);
+}
diff --git a/esp8266/common-hal/time/__init__.c b/esp8266/common-hal/time/__init__.c
new file mode 100644
index 000000000..993e89b1c
--- /dev/null
+++ b/esp8266/common-hal/time/__init__.c
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ *
+ * 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/mphal.h"
+
+#include "shared-bindings/time/__init__.h"
+
+#include "ets_alt_task.h"
+#include "user_interface.h"
+
+inline uint64_t common_hal_time_monotonic() {
+ return ((uint64_t)system_time_high_word << 32 | (uint64_t)system_get_time()) / 1000;
+}
+
+void common_hal_time_delay_ms(uint32_t delay) {
+ mp_hal_delay_ms(delay);
+}
diff --git a/esp8266/esp8266.ld b/esp8266/esp8266.ld
index 20b259dff..d1773cba1 100644
--- a/esp8266/esp8266.ld
+++ b/esp8266/esp8266.ld
@@ -80,6 +80,9 @@ SECTIONS
*(.irom0.literal .irom.literal .irom.text.literal .irom0.text .irom.text)
/* we put some specific text in this section */
+ *common-hal/*.o*(.literal* .text*)
+ *shared-bindings/*.o*(.literal* .text*)
+ *shared-module/*.o*(.literal* .text*)
*py/argcheck.o*(.literal* .text*)
*py/asm*.o*(.literal* .text*)
diff --git a/esp8266/ets_alt_task.h b/esp8266/ets_alt_task.h
index dba0c5fa6..d00898a78 100644
--- a/esp8266/ets_alt_task.h
+++ b/esp8266/ets_alt_task.h
@@ -1,4 +1,12 @@
+#ifndef _INCLUDED_ETS_ALT_TASK_H_
+#define _INCLUDED_ETS_ALT_TASK_H_
+
+#include <stdbool.h>
+#include <stdint.h>
+
extern int ets_loop_iter_disable;
extern uint32_t system_time_high_word;
bool ets_loop_iter(void);
+
+#endif // _INCLUDED_ETS_ALT_TASK_H_
diff --git a/esp8266/machine_pwm.c b/esp8266/machine_pwm.c
index 5d30f0965..4de7edfb9 100644
--- a/esp8266/machine_pwm.c
+++ b/esp8266/machine_pwm.c
@@ -40,7 +40,7 @@ typedef struct _pyb_pwm_obj_t {
uint8_t channel;
} pyb_pwm_obj_t;
-STATIC bool pwm_inited = false;
+bool pwm_inited = false;
/******************************************************************************/
// MicroPython bindings for PWM
diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h
index 602b3e9c8..dbfe24afd 100644
--- a/esp8266/mpconfigport.h
+++ b/esp8266/mpconfigport.h
@@ -98,6 +98,7 @@
#define MICROPY_ESP8266_APA102 (1)
#define MICROPY_ESP8266_NEOPIXEL (1)
+extern void ets_event_poll(void);
#define MICROPY_EVENT_POLL_HOOK {ets_event_poll();}
#define MICROPY_VM_HOOK_COUNT (10)
#define MICROPY_VM_HOOK_INIT static uint vm_hook_divisor = MICROPY_VM_HOOK_COUNT;
@@ -146,6 +147,11 @@ extern const struct _mp_obj_module_t uos_module;
extern const struct _mp_obj_module_t mp_module_lwip;
extern const struct _mp_obj_module_t mp_module_machine;
extern const struct _mp_obj_module_t onewire_module;
+extern const struct _mp_obj_module_t microcontroller_module;
+extern const struct _mp_obj_module_t board_module;
+extern const struct _mp_obj_module_t nativeio_module;
+extern const struct _mp_obj_module_t bitbangio_module;
+extern const struct _mp_obj_module_t time_module;
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \
@@ -157,9 +163,13 @@ extern const struct _mp_obj_module_t onewire_module;
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&onewire_module }, \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_nativeio), (mp_obj_t)&nativeio_module }, \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }, \
+ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \
#define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
- { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&utime_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&uos_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_json), (mp_obj_t)&mp_module_ujson }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_errno), (mp_obj_t)&mp_module_uerrno }, \