From f28f8ba56809fd56e777eabceae21127f95b89ed Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 10 Apr 2017 13:32:19 -0700 Subject: Split up nativeio. This was done to allow greatly granularity when deciding what functionality is built into each board's build. For example, this way pulseio can be omitted to allow for something else such as touchio. --- esp8266/Makefile | 28 +-- esp8266/common-hal/analogio/AnalogIn.c | 60 ++++++ esp8266/common-hal/analogio/AnalogIn.h | 39 ++++ esp8266/common-hal/analogio/AnalogOut.c | 46 +++++ esp8266/common-hal/analogio/AnalogOut.h | 37 ++++ esp8266/common-hal/analogio/__init__.c | 1 + esp8266/common-hal/busio/I2C.h | 33 ++++ esp8266/common-hal/busio/OneWire.h | 33 ++++ esp8266/common-hal/busio/SPI.c | 176 +++++++++++++++++ esp8266/common-hal/busio/SPI.h | 39 ++++ esp8266/common-hal/busio/UART.c | 120 ++++++++++++ esp8266/common-hal/busio/UART.h | 38 ++++ esp8266/common-hal/busio/__init__.c | 1 + esp8266/common-hal/digitalio/DigitalInOut.c | 176 +++++++++++++++++ esp8266/common-hal/digitalio/DigitalInOut.h | 40 ++++ esp8266/common-hal/digitalio/__init__.c | 1 + esp8266/common-hal/nativeio/AnalogIn.c | 60 ------ esp8266/common-hal/nativeio/AnalogOut.c | 47 ----- esp8266/common-hal/nativeio/DigitalInOut.c | 176 ----------------- esp8266/common-hal/nativeio/DigitalInOut.h | 39 ---- esp8266/common-hal/nativeio/PWMOut.c | 118 ----------- esp8266/common-hal/nativeio/PWMOut.h | 32 --- esp8266/common-hal/nativeio/PulseIn.c | 69 ------- esp8266/common-hal/nativeio/PulseOut.c | 42 ---- esp8266/common-hal/nativeio/SPI.c | 176 ----------------- esp8266/common-hal/nativeio/TouchIn.c | 46 ----- esp8266/common-hal/nativeio/UART.c | 120 ------------ esp8266/common-hal/nativeio/__init__.c | 1 - esp8266/common-hal/nativeio/types.h | 78 -------- esp8266/common-hal/neopixel_write/__init__.c | 2 +- esp8266/common-hal/pulseio/PWMOut.c | 118 +++++++++++ esp8266/common-hal/pulseio/PWMOut.h | 40 ++++ esp8266/common-hal/pulseio/PulseIn.c | 69 +++++++ esp8266/common-hal/pulseio/PulseIn.h | 38 ++++ esp8266/common-hal/pulseio/PulseOut.c | 42 ++++ esp8266/common-hal/pulseio/PulseOut.h | 38 ++++ esp8266/common-hal/pulseio/__init__.c | 1 + esp8266/esp_mphal.c | 2 +- esp8266/espuart.c | 281 +++++++++++++++++++++++++++ esp8266/espuart.h | 103 ++++++++++ esp8266/machine_uart.c | 2 +- esp8266/main.c | 2 +- esp8266/modesp.c | 2 +- esp8266/mpconfigport.h | 10 +- esp8266/uart.c | 281 --------------------------- esp8266/uart.h | 103 ---------- 46 files changed, 1598 insertions(+), 1408 deletions(-) create mode 100644 esp8266/common-hal/analogio/AnalogIn.c create mode 100644 esp8266/common-hal/analogio/AnalogIn.h create mode 100644 esp8266/common-hal/analogio/AnalogOut.c create mode 100644 esp8266/common-hal/analogio/AnalogOut.h create mode 100644 esp8266/common-hal/analogio/__init__.c create mode 100644 esp8266/common-hal/busio/I2C.h create mode 100644 esp8266/common-hal/busio/OneWire.h create mode 100644 esp8266/common-hal/busio/SPI.c create mode 100644 esp8266/common-hal/busio/SPI.h create mode 100644 esp8266/common-hal/busio/UART.c create mode 100644 esp8266/common-hal/busio/UART.h create mode 100644 esp8266/common-hal/busio/__init__.c create mode 100644 esp8266/common-hal/digitalio/DigitalInOut.c create mode 100644 esp8266/common-hal/digitalio/DigitalInOut.h create mode 100644 esp8266/common-hal/digitalio/__init__.c delete mode 100644 esp8266/common-hal/nativeio/AnalogIn.c delete mode 100644 esp8266/common-hal/nativeio/AnalogOut.c delete mode 100644 esp8266/common-hal/nativeio/DigitalInOut.c delete mode 100644 esp8266/common-hal/nativeio/DigitalInOut.h delete mode 100644 esp8266/common-hal/nativeio/PWMOut.c delete mode 100644 esp8266/common-hal/nativeio/PWMOut.h delete mode 100644 esp8266/common-hal/nativeio/PulseIn.c delete mode 100644 esp8266/common-hal/nativeio/PulseOut.c delete mode 100644 esp8266/common-hal/nativeio/SPI.c delete mode 100644 esp8266/common-hal/nativeio/TouchIn.c delete mode 100644 esp8266/common-hal/nativeio/UART.c delete mode 100644 esp8266/common-hal/nativeio/__init__.c delete mode 100644 esp8266/common-hal/nativeio/types.h create mode 100644 esp8266/common-hal/pulseio/PWMOut.c create mode 100644 esp8266/common-hal/pulseio/PWMOut.h create mode 100644 esp8266/common-hal/pulseio/PulseIn.c create mode 100644 esp8266/common-hal/pulseio/PulseIn.h create mode 100644 esp8266/common-hal/pulseio/PulseOut.c create mode 100644 esp8266/common-hal/pulseio/PulseOut.h create mode 100644 esp8266/common-hal/pulseio/__init__.c create mode 100644 esp8266/espuart.c create mode 100644 esp8266/espuart.h delete mode 100644 esp8266/uart.c delete mode 100644 esp8266/uart.h (limited to 'esp8266') diff --git a/esp8266/Makefile b/esp8266/Makefile index eac6eddc5..93e424112 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -79,7 +79,7 @@ SRC_C = \ esp_init_data.c \ gccollect.c \ lexerstr32.c \ - uart.c \ + espuart.c \ esppwm.c \ esponewire.c \ espneopixel.c \ @@ -109,16 +109,18 @@ SRC_C = \ SRC_COMMON_HAL = \ microcontroller/__init__.c \ microcontroller/Pin.c \ - nativeio/__init__.c \ - nativeio/AnalogIn.c \ - nativeio/AnalogOut.c \ - nativeio/DigitalInOut.c \ - nativeio/PulseIn.c \ - nativeio/PulseOut.c \ - nativeio/PWMOut.c \ - nativeio/SPI.c \ - nativeio/TouchIn.c \ - nativeio/UART.c \ + analogio/__init__.c \ + analogio/AnalogIn.c \ + analogio/AnalogOut.c \ + digitalio/__init__.c \ + digitalio/DigitalInOut.c \ + pulseio/__init__.c \ + pulseio/PulseIn.c \ + pulseio/PulseOut.c \ + pulseio/PWMOut.c \ + busio/__init__.c \ + busio/SPI.c \ + busio/UART.c \ neopixel_write/__init__.c \ time/__init__.c \ board/__init__.c @@ -131,8 +133,8 @@ SRC_SHARED_MODULE = \ bitbangio/I2C.c \ bitbangio/OneWire.c \ bitbangio/SPI.c \ - nativeio/I2C.c \ - nativeio/OneWire.c \ + busio/I2C.c \ + busio/OneWire.c \ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ $(addprefix shared-module/, $(SRC_SHARED_MODULE)) diff --git a/esp8266/common-hal/analogio/AnalogIn.c b/esp8266/common-hal/analogio/AnalogIn.c new file mode 100644 index 000000000..0f43a0879 --- /dev/null +++ b/esp8266/common-hal/analogio/AnalogIn.c @@ -0,0 +1,60 @@ +/* + * 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 +#include + +#include "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.h" +#include "py/mphal.h" +#include "common-hal/microcontroller/__init__.h" +#include "shared-bindings/analogio/AnalogIn.h" + +#include "user_interface.h" + +volatile bool adc_in_use __attribute__((aligned(4))) = false; + +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, + const mcu_pin_obj_t *pin) { + if (pin != &pin_TOUT) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q does not have ADC capabilities", pin->name)); + } + adc_in_use = true; +} + +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self) { + adc_in_use = false; +} + +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { + // ADC is 10 bit so shift by 6 to make it 16-bit. + return system_adc_read() << 6; +} + +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { + return 1.0f; +} diff --git a/esp8266/common-hal/analogio/AnalogIn.h b/esp8266/common-hal/analogio/AnalogIn.h new file mode 100644 index 000000000..a4edd233d --- /dev/null +++ b/esp8266/common-hal/analogio/AnalogIn.h @@ -0,0 +1,39 @@ +/* + * 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_ESP8266_COMMON_HAL_ANALOGIO_ANALOGIN_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGIN_H__ + +#include "common-hal/microcontroller/types.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; +} analogio_analogin_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGIN_H__ diff --git a/esp8266/common-hal/analogio/AnalogOut.c b/esp8266/common-hal/analogio/AnalogOut.c new file mode 100644 index 000000000..30dc44830 --- /dev/null +++ b/esp8266/common-hal/analogio/AnalogOut.c @@ -0,0 +1,46 @@ +/* + * 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 +#include +#include + +#include "py/runtime.h" + +#include "shared-bindings/analogio/AnalogOut.h" + +void common_hal_analogio_analogout_construct(analogio_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_analogio_analogout_deinit(analogio_analogout_obj_t *self) { +} + +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, + uint16_t value) { +} diff --git a/esp8266/common-hal/analogio/AnalogOut.h b/esp8266/common-hal/analogio/AnalogOut.h new file mode 100644 index 000000000..146a6571c --- /dev/null +++ b/esp8266/common-hal/analogio/AnalogOut.h @@ -0,0 +1,37 @@ +/* + * 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_ESP8266_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ + +#include "py/obj.h" + +// Not supported, throws error on construction. +typedef struct { + mp_obj_base_t base; +} analogio_analogout_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGOUT_H__ diff --git a/esp8266/common-hal/analogio/__init__.c b/esp8266/common-hal/analogio/__init__.c new file mode 100644 index 000000000..eea58c77d --- /dev/null +++ b/esp8266/common-hal/analogio/__init__.c @@ -0,0 +1 @@ +// No analogio module functions. diff --git a/esp8266/common-hal/busio/I2C.h b/esp8266/common-hal/busio/I2C.h new file mode 100644 index 000000000..5877e6c31 --- /dev/null +++ b/esp8266/common-hal/busio/I2C.h @@ -0,0 +1,33 @@ +/* + * 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_ESP8266_COMMON_HAL_BUSIO_I2C_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_I2C_H__ + +// Use the bitbang wrapper for I2C +#include "shared-module/busio/I2C.h" + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_I2C_H__ diff --git a/esp8266/common-hal/busio/OneWire.h b/esp8266/common-hal/busio/OneWire.h new file mode 100644 index 000000000..3c52ced38 --- /dev/null +++ b/esp8266/common-hal/busio/OneWire.h @@ -0,0 +1,33 @@ +/* + * 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_ESP8266_COMMON_HAL_BUSIO_ONEWIRE_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_ONEWIRE_H__ + +// Use the bitbang wrapper for OneWire +#include "shared-module/busio/OneWire.h" + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_ONEWIRE_H__ diff --git a/esp8266/common-hal/busio/SPI.c b/esp8266/common-hal/busio/SPI.c new file mode 100644 index 000000000..9ffb0294d --- /dev/null +++ b/esp8266/common-hal/busio/SPI.c @@ -0,0 +1,176 @@ +/* + * 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/microcontroller/__init__.h" +#include "common-hal/busio/SPI.h" +#include "py/nlr.h" + +#include "eagle_soc.h" +#include "c_types.h" +#include "gpio.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_busio_spi_construct(busio_spi_obj_t *self, + const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi, + const mcu_pin_obj_t * miso) { + if (clock != &pin_MTMS || !((mosi == &pin_MTCK && miso == MP_OBJ_TO_PTR(mp_const_none)) || + (mosi == MP_OBJ_TO_PTR(mp_const_none) && miso == &pin_MTDI))) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, + "Pins not valid for SPI")); + } + + uint32_t clock_div_flag = 0; + if (SPI_CLK_USE_DIV) { + clock_div_flag = 0x0001; + } + + // Set bit 9 if 80MHz sysclock required + WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105 | (clock_div_flag<<9)); + // GPIO12 is HSPI MISO pin (Master Data In) + if (miso == &pin_MTDI) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2); + } + // GPIO13 is HSPI MOSI pin (Master Data Out) + if (mosi == &pin_MTCK) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2); + } + // GPIO14 is HSPI CLK pin (Clock) + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2); + + spi_clock(HSPI, SPI_CLK_PREDIV, SPI_CLK_CNTDIV); + spi_tx_byte_order(HSPI, SPI_BYTE_ORDER_HIGH_TO_LOW); + spi_rx_byte_order(HSPI, SPI_BYTE_ORDER_HIGH_TO_LOW); + + SET_PERI_REG_MASK(SPI_USER(HSPI), SPI_CS_SETUP|SPI_CS_HOLD); + CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE); +} + +void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 0); + PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDI_U); + + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 0); + PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTCK_U); + + PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 0); + PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTMS_U); + + // Turn off outputs 12 - 14. + gpio_output_set(0x0, 0x0, 0x0, 0x7 << 12); +} + +bool common_hal_busio_spi_configure(busio_spi_obj_t *self, + uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { + if (bits != 8) { + return false; + } + if (baudrate == 80000000L) { + // Special case for full speed. + spi_init_gpio(HSPI, SPI_CLK_80MHZ_NODIV); + spi_clock(HSPI, 0, 0); + } else if (baudrate > 40000000L) { + return false; + } else { + uint32_t divider = 40000000L / baudrate; + uint16_t prediv = MIN(divider, SPI_CLKDIV_PRE + 1); + uint16_t cntdiv = (divider / prediv) * 2; // cntdiv has to be even + if (cntdiv > SPI_CLKCNT_N + 1 || cntdiv == 0 || prediv == 0) { + return false; + } + spi_init_gpio(HSPI, SPI_CLK_USE_DIV); + spi_clock(HSPI, prediv, cntdiv); + } + spi_mode(HSPI, phase, polarity); + return true; +} + +bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) { + bool success = false; + common_hal_mcu_disable_interrupts(); + if (!self->locked) { + self->locked = true; + success = true; + } + common_hal_mcu_enable_interrupts(); + return success; +} + +bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) { + return self->locked; +} + +void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { + self->locked = false; +} + +bool common_hal_busio_spi_write(busio_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; + } + while (spi_busy(HSPI)) {}; // Wait for SPI to finish the last byte. + return true; +} + +bool common_hal_busio_spi_read(busio_spi_obj_t *self, + uint8_t * data, size_t len, uint8_t write_value) { + // 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; + uint32_t long_write_value = ((uint32_t) write_value) << 24 | + write_value << 16 | + write_value << 8 | + write_value; + for (size_t j = 0; j < count; ++j) { + for (size_t k = 0; k < chunk_size; ++k) { + data[i] = spi_transaction(HSPI, 0, 0, 0, 0, 0, 0, 8, long_write_value); + ++i; + } + ets_loop_iter(); + } + while (i < len) { + data[i] = spi_transaction(HSPI, 0, 0, 0, 0, 0, 0, 8, long_write_value); + ++i; + } + return true; +} diff --git a/esp8266/common-hal/busio/SPI.h b/esp8266/common-hal/busio/SPI.h new file mode 100644 index 000000000..baff10b70 --- /dev/null +++ b/esp8266/common-hal/busio/SPI.h @@ -0,0 +1,39 @@ +/* + * 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_ESP8266_COMMON_HAL_BUSIO_SPI_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_SPI_H__ + +#include "common-hal/microcontroller/types.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + bool locked; +} busio_spi_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_SPI_H__ diff --git a/esp8266/common-hal/busio/UART.c b/esp8266/common-hal/busio/UART.c new file mode 100644 index 000000000..b2a1d69b8 --- /dev/null +++ b/esp8266/common-hal/busio/UART.c @@ -0,0 +1,120 @@ +/* + * 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 "common-hal/microcontroller/__init__.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/busio/UART.h" + +#include "ets_sys.h" +#include "espuart.h" + +#include "py/nlr.h" + +// UartDev is defined and initialized in rom code. +extern UartDevice UartDev; + +void common_hal_busio_uart_construct(busio_uart_obj_t *self, + const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, + uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout, + uint8_t receiver_buffer_size) { + if (rx != NULL || tx != &pin_GPIO2) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Only tx supported on UART1 (GPIO2).")); + } + // set baudrate + UartDev.baut_rate = baudrate; + + // set data bits + switch (bits) { + case 5: + UartDev.data_bits = UART_FIVE_BITS; + break; + case 6: + UartDev.data_bits = UART_SIX_BITS; + break; + case 7: + UartDev.data_bits = UART_SEVEN_BITS; + break; + case 8: + UartDev.data_bits = UART_EIGHT_BITS; + break; + default: + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid data bits")); + break; + } + + if (parity == PARITY_NONE) { + UartDev.parity = UART_NONE_BITS; + UartDev.exist_parity = UART_STICK_PARITY_DIS; + } else { + UartDev.exist_parity = UART_STICK_PARITY_EN; + if (parity == PARITY_ODD) { + UartDev.parity = UART_ODD_BITS; + } else { + UartDev.parity = UART_EVEN_BITS; + } + } + + switch (stop) { + case 1: + UartDev.stop_bits = UART_ONE_STOP_BIT; + break; + case 2: + UartDev.stop_bits = UART_TWO_STOP_BIT; + break; + default: + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid stop bits")); + break; + } + + uart_setup(UART1); +} + +void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { + PIN_FUNC_SELECT(FUNC_U1TXD_BK, 0); +} + +size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { + return 0; +} + +// Write characters. +size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { + // write the data + for (size_t i = 0; i < len; ++i) { + uart_tx_one_char(UART1, *data++); + } + + // return number of bytes written + return len; +} + +uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) { + return 0; +} + +bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) { + return true; +} diff --git a/esp8266/common-hal/busio/UART.h b/esp8266/common-hal/busio/UART.h new file mode 100644 index 000000000..a87e7c9b2 --- /dev/null +++ b/esp8266/common-hal/busio/UART.h @@ -0,0 +1,38 @@ +/* + * 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_ESP8266_COMMON_HAL_BUSIO_UART_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_UART_H__ + +#include "common-hal/microcontroller/types.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} busio_uart_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_UART_H__ diff --git a/esp8266/common-hal/busio/__init__.c b/esp8266/common-hal/busio/__init__.c new file mode 100644 index 000000000..41761b674 --- /dev/null +++ b/esp8266/common-hal/busio/__init__.c @@ -0,0 +1 @@ +// No busio module functions. diff --git a/esp8266/common-hal/digitalio/DigitalInOut.c b/esp8266/common-hal/digitalio/DigitalInOut.c new file mode 100644 index 000000000..356d0f3b8 --- /dev/null +++ b/esp8266/common-hal/digitalio/DigitalInOut.c @@ -0,0 +1,176 @@ +/* + * 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 +#include +#include + +#include "py/nlr.h" +#include "py/runtime.h" +#include "py/mphal.h" + +#include "shared-bindings/digitalio/DigitalInOut.h" + +digitalinout_result_t common_hal_digitalio_digitalinout_construct( + digitalio_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_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) { + if (self->pin->gpio_number < 16) { + uint32_t pin_mask = 1 << self->pin->gpio_number; + gpio_output_set(0x0, 0x0, 0x0, pin_mask); + PIN_FUNC_SELECT(self->pin->peripheral, 0); + PIN_PULLUP_DIS(self->pin->peripheral); + } +} + +void common_hal_digitalio_digitalinout_switch_to_input( + digitalio_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_digitalio_digitalinout_set_pull(self, pull); +} + +void common_hal_digitalio_digitalinout_switch_to_output( + digitalio_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_number, 0); + PIN_PULLUP_DIS(self->pin->peripheral); + } + common_hal_digitalio_digitalinout_set_value(self, value); +} + +enum digitalinout_direction_t common_hal_digitalio_digitalinout_get_direction( + digitalio_digitalinout_obj_t* self) { + return self->output? DIRECTION_OUT : DIRECTION_IN; +} + +void common_hal_digitalio_digitalinout_set_value( + digitalio_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); + } else { + // Set high + gpio_output_set(1 << self->pin->gpio_number, 0, 0, 0); + } + } else { + if (self->open_drain) { + // Enable the output + 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_digitalio_digitalinout_get_value( + digitalio_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_digitalio_digitalinout_set_drive_mode( + digitalio_digitalinout_obj_t* self, + enum digitalinout_drive_mode_t drive_mode) { + bool value = common_hal_digitalio_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_digitalio_digitalinout_set_value(self, value); + } +} + +enum digitalinout_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( + digitalio_digitalinout_obj_t* self) { + if (self->open_drain) { + return DRIVE_MODE_OPEN_DRAIN; + } else { + return DRIVE_MODE_PUSH_PULL; + } +} + +void common_hal_digitalio_digitalinout_set_pull( + digitalio_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_digitalio_digitalinout_get_pull( + digitalio_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/digitalio/DigitalInOut.h b/esp8266/common-hal/digitalio/DigitalInOut.h new file mode 100644 index 000000000..94de1bca5 --- /dev/null +++ b/esp8266/common-hal/digitalio/DigitalInOut.h @@ -0,0 +1,40 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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_DIGITALIO_DIGITALINOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__ + +#include "common-hal/microcontroller/types.h" +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t * pin; + bool output; + bool open_drain; +} digitalio_digitalinout_obj_t; + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__ diff --git a/esp8266/common-hal/digitalio/__init__.c b/esp8266/common-hal/digitalio/__init__.c new file mode 100644 index 000000000..20fad4595 --- /dev/null +++ b/esp8266/common-hal/digitalio/__init__.c @@ -0,0 +1 @@ +// No digitalio module functions. diff --git a/esp8266/common-hal/nativeio/AnalogIn.c b/esp8266/common-hal/nativeio/AnalogIn.c deleted file mode 100644 index 57ff607a3..000000000 --- a/esp8266/common-hal/nativeio/AnalogIn.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 -#include - -#include "py/nlr.h" -#include "py/runtime.h" -#include "py/binary.h" -#include "py/mphal.h" -#include "common-hal/microcontroller/__init__.h" -#include "shared-bindings/nativeio/AnalogIn.h" - -#include "user_interface.h" - -volatile bool adc_in_use __attribute__((aligned(4))) = false; - -void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, - const mcu_pin_obj_t *pin) { - if (pin != &pin_TOUT) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q does not have ADC capabilities", pin->name)); - } - adc_in_use = true; -} - -void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self) { - adc_in_use = false; -} - -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; -} - -float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) { - return 1.0f; -} diff --git a/esp8266/common-hal/nativeio/AnalogOut.c b/esp8266/common-hal/nativeio/AnalogOut.c deleted file mode 100644 index 06b1880c3..000000000 --- a/esp8266/common-hal/nativeio/AnalogOut.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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 -#include -#include - -#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 deleted file mode 100644 index e6a28ed75..000000000 --- a/esp8266/common-hal/nativeio/DigitalInOut.c +++ /dev/null @@ -1,176 +0,0 @@ -/* - * 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 -#include -#include - -#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) { - if (self->pin->gpio_number < 16) { - uint32_t pin_mask = 1 << self->pin->gpio_number; - gpio_output_set(0x0, 0x0, 0x0, pin_mask); - PIN_FUNC_SELECT(self->pin->peripheral, 0); - PIN_PULLUP_DIS(self->pin->peripheral); - } -} - -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_number, 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); - } else { - // Set high - gpio_output_set(1 << self->pin->gpio_number, 0, 0, 0); - } - } else { - if (self->open_drain) { - // Enable the output - 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/DigitalInOut.h b/esp8266/common-hal/nativeio/DigitalInOut.h deleted file mode 100644 index 6325b978b..000000000 --- a/esp8266/common-hal/nativeio/DigitalInOut.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 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_NATIVEIO_DIGITALINOUT_H__ -#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t * pin; - bool output; - bool open_drain; -} nativeio_digitalinout_obj_t; - -#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__ diff --git a/esp8266/common-hal/nativeio/PWMOut.c b/esp8266/common-hal/nativeio/PWMOut.c deleted file mode 100644 index 234246d15..000000000 --- a/esp8266/common-hal/nativeio/PWMOut.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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 -#include - -#include "esppwm.h" - -#include "py/runtime.h" -#include "shared-bindings/nativeio/PWMOut.h" - -#include "eagle_soc.h" -#include "c_types.h" -#include "gpio.h" - -#define PWM_FREQ_MAX 1000 - -// Shared with pybpwm -extern bool pwm_inited; -bool first_channel_variable; - -void pwmout_reset(void) { - first_channel_variable = false; -} - -void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, - bool variable_frequency) { - if (frequency > PWM_FREQ_MAX) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, - "Maximum PWM frequency is %dhz.", PWM_FREQ_MAX)); - } else if (frequency < 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Minimum PWM frequency is 1hz.")); - } - - // start the PWM subsystem if it's not already running - if (!pwm_inited) { - pwm_init(); - pwm_inited = true; - pwm_set_freq(frequency, 0); - first_channel_variable = variable_frequency; - } else if (first_channel_variable || pwm_get_freq(0) != frequency) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, - "Multiple PWM frequencies not supported. PWM already set to %dhz.", pwm_get_freq(0))); - } - - self->channel = pwm_add(pin->gpio_number, - pin->peripheral, - pin->gpio_function); - self->pin = pin; - 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(); - if (self->pin->gpio_number < 16) { - uint32_t pin_mask = 1 << self->pin->gpio_number; - gpio_output_set(0x0, 0x0, 0x0, pin_mask); - PIN_FUNC_SELECT(self->pin->peripheral, 0); - PIN_PULLUP_DIS(self->pin->peripheral); - } -} - -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; -} - -void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint32_t frequency) { - if (frequency > PWM_FREQ_MAX) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, - "Maximum PWM frequency is %dhz.", PWM_FREQ_MAX)); - } else if (frequency < 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "Minimum PWM frequency is 1hz.")); - } - pwm_set_freq(frequency, 0); -} - -uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self) { - return pwm_get_freq(0); -} - -bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self) { - return first_channel_variable; -} diff --git a/esp8266/common-hal/nativeio/PWMOut.h b/esp8266/common-hal/nativeio/PWMOut.h deleted file mode 100644 index 9406b4953..000000000 --- a/esp8266/common-hal/nativeio/PWMOut.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 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_NATIVEIO_PWMOUT_H__ -#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_PWMOUT_H__ - -void pwmout_reset(void); - -#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_PWMOUT_H__ diff --git a/esp8266/common-hal/nativeio/PulseIn.c b/esp8266/common-hal/nativeio/PulseIn.c deleted file mode 100644 index c4bd5a77c..000000000 --- a/esp8266/common-hal/nativeio/PulseIn.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 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 - -#include "mpconfigport.h" -#include "py/gc.h" -#include "py/runtime.h" -#include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/PulseIn.h" - -void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self, - const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { - mp_raise_NotImplementedError(""); -} - -void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) { - -} - -void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) { -} - -void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self, - uint16_t trigger_duration) { -} - -void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) { -} - -uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) { - return 0; -} - -uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) { - return 0; -} - -uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) { - return 0; -} - -uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self, - int16_t index) { - return 0; -} diff --git a/esp8266/common-hal/nativeio/PulseOut.c b/esp8266/common-hal/nativeio/PulseOut.c deleted file mode 100644 index d5d1e6366..000000000 --- a/esp8266/common-hal/nativeio/PulseOut.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2017 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 - -#include "py/runtime.h" -#include "shared-bindings/nativeio/PulseOut.h" - -void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self, - const nativeio_pwmout_obj_t* carrier) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut.")); -} - -void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) { -} - -void common_hal_nativeio_pulseout_send(nativeio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { -} diff --git a/esp8266/common-hal/nativeio/SPI.c b/esp8266/common-hal/nativeio/SPI.c deleted file mode 100644 index 427c0d33c..000000000 --- a/esp8266/common-hal/nativeio/SPI.c +++ /dev/null @@ -1,176 +0,0 @@ -/* - * 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/microcontroller/__init__.h" -#include "shared-bindings/nativeio/SPI.h" -#include "py/nlr.h" - -#include "eagle_soc.h" -#include "c_types.h" -#include "gpio.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) { - if (clock != &pin_MTMS || !((mosi == &pin_MTCK && miso == MP_OBJ_TO_PTR(mp_const_none)) || - (mosi == MP_OBJ_TO_PTR(mp_const_none) && miso == &pin_MTDI))) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, - "Pins not valid for SPI")); - } - - uint32_t clock_div_flag = 0; - if (SPI_CLK_USE_DIV) { - clock_div_flag = 0x0001; - } - - // Set bit 9 if 80MHz sysclock required - WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105 | (clock_div_flag<<9)); - // GPIO12 is HSPI MISO pin (Master Data In) - if (miso == &pin_MTDI) { - PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2); - } - // GPIO13 is HSPI MOSI pin (Master Data Out) - if (mosi == &pin_MTCK) { - PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2); - } - // GPIO14 is HSPI CLK pin (Clock) - PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2); - - spi_clock(HSPI, SPI_CLK_PREDIV, SPI_CLK_CNTDIV); - spi_tx_byte_order(HSPI, SPI_BYTE_ORDER_HIGH_TO_LOW); - spi_rx_byte_order(HSPI, SPI_BYTE_ORDER_HIGH_TO_LOW); - - SET_PERI_REG_MASK(SPI_USER(HSPI), SPI_CS_SETUP|SPI_CS_HOLD); - CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE); -} - -void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) { - PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 0); - PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDI_U); - - PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 0); - PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTCK_U); - - PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 0); - PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTMS_U); - - // Turn off outputs 12 - 14. - gpio_output_set(0x0, 0x0, 0x0, 0x7 << 12); -} - -bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self, - uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { - if (bits != 8) { - return false; - } - if (baudrate == 80000000L) { - // Special case for full speed. - spi_init_gpio(HSPI, SPI_CLK_80MHZ_NODIV); - spi_clock(HSPI, 0, 0); - } else if (baudrate > 40000000L) { - return false; - } else { - uint32_t divider = 40000000L / baudrate; - uint16_t prediv = MIN(divider, SPI_CLKDIV_PRE + 1); - uint16_t cntdiv = (divider / prediv) * 2; // cntdiv has to be even - if (cntdiv > SPI_CLKCNT_N + 1 || cntdiv == 0 || prediv == 0) { - return false; - } - spi_init_gpio(HSPI, SPI_CLK_USE_DIV); - spi_clock(HSPI, prediv, cntdiv); - } - spi_mode(HSPI, phase, polarity); - return true; -} - -bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self) { - bool success = false; - common_hal_mcu_disable_interrupts(); - if (!self->locked) { - self->locked = true; - success = true; - } - common_hal_mcu_enable_interrupts(); - return success; -} - -bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self) { - return self->locked; -} - -void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self) { - self->locked = false; -} - -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; - } - while (spi_busy(HSPI)) {}; // Wait for SPI to finish the last byte. - return true; -} - -bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self, - uint8_t * data, size_t len, uint8_t write_value) { - // 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; - uint32_t long_write_value = ((uint32_t) write_value) << 24 | - write_value << 16 | - write_value << 8 | - write_value; - for (size_t j = 0; j < count; ++j) { - for (size_t k = 0; k < chunk_size; ++k) { - data[i] = spi_transaction(HSPI, 0, 0, 0, 0, 0, 0, 8, long_write_value); - ++i; - } - ets_loop_iter(); - } - while (i < len) { - data[i] = spi_transaction(HSPI, 0, 0, 0, 0, 0, 0, 8, long_write_value); - ++i; - } - return true; -} diff --git a/esp8266/common-hal/nativeio/TouchIn.c b/esp8266/common-hal/nativeio/TouchIn.c deleted file mode 100644 index 1501a7936..000000000 --- a/esp8266/common-hal/nativeio/TouchIn.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 - -#include "py/nlr.h" -#include "py/runtime.h" -#include "py/binary.h" -#include "py/mphal.h" -#include "shared-bindings/nativeio/TouchIn.h" - -void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self, - const mcu_pin_obj_t *pin) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, - "No hardware support for capacitive touch.")); -} - -void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) { -} - -bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) { - return false; -} diff --git a/esp8266/common-hal/nativeio/UART.c b/esp8266/common-hal/nativeio/UART.c deleted file mode 100644 index 8509dec64..000000000 --- a/esp8266/common-hal/nativeio/UART.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * 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 "common-hal/microcontroller/__init__.h" -#include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/nativeio/UART.h" - -#include "ets_sys.h" -#include "uart.h" - -#include "py/nlr.h" - -// UartDev is defined and initialized in rom code. -extern UartDevice UartDev; - -void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self, - const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate, - uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout, - uint8_t receiver_buffer_size) { - if (rx != NULL || tx != &pin_GPIO2) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "Only tx supported on UART1 (GPIO2).")); - } - // set baudrate - UartDev.baut_rate = baudrate; - - // set data bits - switch (bits) { - case 5: - UartDev.data_bits = UART_FIVE_BITS; - break; - case 6: - UartDev.data_bits = UART_SIX_BITS; - break; - case 7: - UartDev.data_bits = UART_SEVEN_BITS; - break; - case 8: - UartDev.data_bits = UART_EIGHT_BITS; - break; - default: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid data bits")); - break; - } - - if (parity == PARITY_NONE) { - UartDev.parity = UART_NONE_BITS; - UartDev.exist_parity = UART_STICK_PARITY_DIS; - } else { - UartDev.exist_parity = UART_STICK_PARITY_EN; - if (parity == PARITY_ODD) { - UartDev.parity = UART_ODD_BITS; - } else { - UartDev.parity = UART_EVEN_BITS; - } - } - - switch (stop) { - case 1: - UartDev.stop_bits = UART_ONE_STOP_BIT; - break; - case 2: - UartDev.stop_bits = UART_TWO_STOP_BIT; - break; - default: - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid stop bits")); - break; - } - - uart_setup(UART1); -} - -void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self) { - PIN_FUNC_SELECT(FUNC_U1TXD_BK, 0); -} - -size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { - return 0; -} - -// Write characters. -size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { - // write the data - for (size_t i = 0; i < len; ++i) { - uart_tx_one_char(UART1, *data++); - } - - // return number of bytes written - return len; -} - -uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self) { - return 0; -} - -bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self) { - return true; -} diff --git a/esp8266/common-hal/nativeio/__init__.c b/esp8266/common-hal/nativeio/__init__.c deleted file mode 100644 index e0ec8acfb..000000000 --- a/esp8266/common-hal/nativeio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No nativeio module functions. diff --git a/esp8266/common-hal/nativeio/types.h b/esp8266/common-hal/nativeio/types.h deleted file mode 100644 index 05b7d28c2..000000000 --- a/esp8266/common-hal/nativeio/types.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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" - -// Use the bitbang wrapper for I2C and OneWire -#include "shared-module/nativeio/I2C.h" -#include "shared-module/nativeio/OneWire.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; - bool locked; -} nativeio_spi_obj_t; - -typedef struct { - mp_obj_base_t base; -} nativeio_pulsein_obj_t; - -typedef struct { - mp_obj_base_t base; -} nativeio_pulseout_obj_t; - -typedef struct { - mp_obj_base_t base; - int channel; - const mcu_pin_obj_t* pin; -} nativeio_pwmout_obj_t; - -typedef struct { - mp_obj_base_t base; -} nativeio_touchin_obj_t; - -typedef struct { - mp_obj_base_t base; -} nativeio_uart_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 index daaeead43..84a743d11 100644 --- a/esp8266/common-hal/neopixel_write/__init__.c +++ b/esp8266/common-hal/neopixel_write/__init__.c @@ -28,6 +28,6 @@ #include "espneopixel.h" -void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) { +void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) { esp_neopixel_write(digitalinout->pin->gpio_number, pixels, numBytes); } diff --git a/esp8266/common-hal/pulseio/PWMOut.c b/esp8266/common-hal/pulseio/PWMOut.c new file mode 100644 index 000000000..40a46e392 --- /dev/null +++ b/esp8266/common-hal/pulseio/PWMOut.c @@ -0,0 +1,118 @@ +/* + * 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 +#include + +#include "esppwm.h" + +#include "py/runtime.h" +#include "shared-bindings/pulseio/PWMOut.h" + +#include "eagle_soc.h" +#include "c_types.h" +#include "gpio.h" + +#define PWM_FREQ_MAX 1000 + +// Shared with pybpwm +extern bool pwm_inited; +bool first_channel_variable; + +void pwmout_reset(void) { + first_channel_variable = false; +} + +void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, + bool variable_frequency) { + if (frequency > PWM_FREQ_MAX) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, + "Maximum PWM frequency is %dhz.", PWM_FREQ_MAX)); + } else if (frequency < 1) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, + "Minimum PWM frequency is 1hz.")); + } + + // start the PWM subsystem if it's not already running + if (!pwm_inited) { + pwm_init(); + pwm_inited = true; + pwm_set_freq(frequency, 0); + first_channel_variable = variable_frequency; + } else if (first_channel_variable || pwm_get_freq(0) != frequency) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, + "Multiple PWM frequencies not supported. PWM already set to %dhz.", pwm_get_freq(0))); + } + + self->channel = pwm_add(pin->gpio_number, + pin->peripheral, + pin->gpio_function); + self->pin = pin; + 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_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) { + pwm_delete(self->channel); + pwm_start(); + if (self->pin->gpio_number < 16) { + uint32_t pin_mask = 1 << self->pin->gpio_number; + gpio_output_set(0x0, 0x0, 0x0, pin_mask); + PIN_FUNC_SELECT(self->pin->peripheral, 0); + PIN_PULLUP_DIS(self->pin->peripheral); + } +} + +extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_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_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) { + return pwm_get_duty(self->channel) << 6; +} + +void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) { + if (frequency > PWM_FREQ_MAX) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, + "Maximum PWM frequency is %dhz.", PWM_FREQ_MAX)); + } else if (frequency < 1) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, + "Minimum PWM frequency is 1hz.")); + } + pwm_set_freq(frequency, 0); +} + +uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) { + return pwm_get_freq(0); +} + +bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) { + return first_channel_variable; +} diff --git a/esp8266/common-hal/pulseio/PWMOut.h b/esp8266/common-hal/pulseio/PWMOut.h new file mode 100644 index 000000000..7eb22e6e1 --- /dev/null +++ b/esp8266/common-hal/pulseio/PWMOut.h @@ -0,0 +1,40 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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_PULSEIO_PWMOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PWMOUT_H__ + +#include "common-hal/microcontroller/types.h" + +typedef struct { + mp_obj_base_t base; + int channel; + const mcu_pin_obj_t* pin; +} pulseio_pwmout_obj_t; + +void pwmout_reset(void); + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PWMOUT_H__ diff --git a/esp8266/common-hal/pulseio/PulseIn.c b/esp8266/common-hal/pulseio/PulseIn.c new file mode 100644 index 000000000..784116bef --- /dev/null +++ b/esp8266/common-hal/pulseio/PulseIn.c @@ -0,0 +1,69 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 + +#include "mpconfigport.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/pulseio/PulseIn.h" + +void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, + const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { + mp_raise_NotImplementedError(""); +} + +void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { + +} + +void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { +} + +void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, + uint16_t trigger_duration) { +} + +void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { +} + +uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { + return 0; +} + +uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) { + return 0; +} + +uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) { + return 0; +} + +uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, + int16_t index) { + return 0; +} diff --git a/esp8266/common-hal/pulseio/PulseIn.h b/esp8266/common-hal/pulseio/PulseIn.h new file mode 100644 index 000000000..c3a27d929 --- /dev/null +++ b/esp8266/common-hal/pulseio/PulseIn.h @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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_PULSEIO_PULSEIN_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEIN_H__ + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} pulseio_pulsein_obj_t; + +void pwmout_reset(void); + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEIN_H__ diff --git a/esp8266/common-hal/pulseio/PulseOut.c b/esp8266/common-hal/pulseio/PulseOut.c new file mode 100644 index 000000000..1301abbf3 --- /dev/null +++ b/esp8266/common-hal/pulseio/PulseOut.c @@ -0,0 +1,42 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 + +#include "py/runtime.h" +#include "shared-bindings/pulseio/PulseOut.h" + +void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, + const pulseio_pwmout_obj_t* carrier) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut.")); +} + +void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { +} + +void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { +} diff --git a/esp8266/common-hal/pulseio/PulseOut.h b/esp8266/common-hal/pulseio/PulseOut.h new file mode 100644 index 000000000..c4c6ef616 --- /dev/null +++ b/esp8266/common-hal/pulseio/PulseOut.h @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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_PULSEIO_PULSEOUT_H__ +#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H__ + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} pulseio_pulseout_obj_t; + +void pwmout_reset(void); + +#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H__ diff --git a/esp8266/common-hal/pulseio/__init__.c b/esp8266/common-hal/pulseio/__init__.c new file mode 100644 index 000000000..2bee925bc --- /dev/null +++ b/esp8266/common-hal/pulseio/__init__.c @@ -0,0 +1 @@ +// No pulseio module functions. diff --git a/esp8266/esp_mphal.c b/esp8266/esp_mphal.c index f5e284fde..5db4f1968 100644 --- a/esp8266/esp_mphal.c +++ b/esp8266/esp_mphal.c @@ -27,7 +27,7 @@ #include #include "ets_sys.h" #include "etshal.h" -#include "uart.h" +#include "espuart.h" #include "esp_mphal.h" #include "user_interface.h" #include "ets_alt_task.h" diff --git a/esp8266/espuart.c b/esp8266/espuart.c new file mode 100644 index 000000000..ee893f4ec --- /dev/null +++ b/esp8266/espuart.c @@ -0,0 +1,281 @@ +/****************************************************************************** + * Copyright 2013-2014 Espressif Systems (Wuxi) + * + * FileName: uart.c + * + * Description: Two UART mode configration and interrupt handler. + * Check your hardware connection while use this mode. + * + * Modification history: + * 2014/3/12, v1.0 create this file. +*******************************************************************************/ +#include "ets_sys.h" +#include "osapi.h" +#include "espuart.h" +#include "osapi.h" +#include "uart_register.h" +#include "etshal.h" +#include "c_types.h" +#include "user_interface.h" +#include "esp_mphal.h" + +// seems that this is missing in the Espressif SDK +#define FUNC_U0RXD 0 + +#define UART_REPL UART0 + +// UartDev is defined and initialized in rom code. +extern UartDevice UartDev; + +// the uart to which OS messages go; -1 to disable +static int uart_os = UART_OS; + +#if MICROPY_REPL_EVENT_DRIVEN +static os_event_t uart_evt_queue[16]; +#endif + +static void uart0_rx_intr_handler(void *para); + +void soft_reset(void); +void mp_keyboard_interrupt(void); + +/****************************************************************************** + * FunctionName : uart_config + * Description : Internal used function + * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled + * UART1 just used for debug output + * Parameters : uart_no, use UART0 or UART1 defined ahead + * Returns : NONE +*******************************************************************************/ +static void ICACHE_FLASH_ATTR uart_config(uint8 uart_no) { + if (uart_no == UART1) { + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); + } else { + ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, NULL); + PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); + PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD); + } + + uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate)); + + WRITE_PERI_REG(UART_CONF0(uart_no), UartDev.exist_parity + | UartDev.parity + | (UartDev.stop_bits << UART_STOP_BIT_NUM_S) + | (UartDev.data_bits << UART_BIT_NUM_S)); + + // clear rx and tx fifo,not ready + SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST); + CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST); + + if (uart_no == UART0) { + // set rx fifo trigger + WRITE_PERI_REG(UART_CONF1(uart_no), + ((0x10 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) | + ((0x10 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) | + UART_RX_FLOW_EN | + (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S | + UART_RX_TOUT_EN); + SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA | + UART_FRM_ERR_INT_ENA); + } else { + WRITE_PERI_REG(UART_CONF1(uart_no), + ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S)); + } + + // clear all interrupt + WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff); + // enable rx_interrupt + SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA); +} + +/****************************************************************************** + * FunctionName : uart1_tx_one_char + * Description : Internal used function + * Use uart1 interface to transfer one char + * Parameters : uint8 TxChar - character to tx + * Returns : OK +*******************************************************************************/ +void uart_tx_one_char(uint8 uart, uint8 TxChar) { + while (true) { + uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) { + break; + } + } + WRITE_PERI_REG(UART_FIFO(uart), TxChar); +} + +void uart_flush(uint8 uart) { + while (true) { + uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) == 0) { + break; + } + } +} + +/****************************************************************************** + * FunctionName : uart1_write_char + * Description : Internal used function + * Do some special deal while tx char is '\r' or '\n' + * Parameters : char c - character to tx + * Returns : NONE +*******************************************************************************/ +static void ICACHE_FLASH_ATTR +uart_os_write_char(char c) { + if (uart_os == -1) { + return; + } + if (c == '\n') { + uart_tx_one_char(uart_os, '\r'); + uart_tx_one_char(uart_os, '\n'); + } else if (c == '\r') { + } else { + uart_tx_one_char(uart_os, c); + } +} + +void ICACHE_FLASH_ATTR +uart_os_config(int uart) { + uart_os = uart; +} + +/****************************************************************************** + * FunctionName : uart0_rx_intr_handler + * Description : Internal used function + * UART0 interrupt handler, add self handle code inside + * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg + * Returns : NONE +*******************************************************************************/ + +static void uart0_rx_intr_handler(void *para) { + /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents + * uart1 and uart0 respectively + */ + + uint8 uart_no = UART_REPL; + + if (UART_FRM_ERR_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_FRM_ERR_INT_ST)) { + // frame error + WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_FRM_ERR_INT_CLR); + } + + if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST)) { + // fifo full + goto read_chars; + } else if (UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)) { + read_chars: + ETS_UART_INTR_DISABLE(); + + while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { + uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff; + if (RcvChar == mp_interrupt_char) { + mp_keyboard_interrupt(); + } else { + ringbuf_put(&input_buf, RcvChar); + } + } + + mp_hal_signal_input(); + + // Clear pending FIFO interrupts + WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST); + ETS_UART_INTR_ENABLE(); + } +} + +// Waits at most timeout microseconds for at least 1 char to become ready for reading. +// Returns true if something available, false if not. +bool uart_rx_wait(uint32_t timeout_us) { + uint32_t start = system_get_time(); + for (;;) { + if (input_buf.iget != input_buf.iput) { + return true; // have at least 1 char ready for reading + } + if (system_get_time() - start >= timeout_us) { + return false; // timeout + } + ets_event_poll(); + } +} + +// Returns char from the input buffer, else -1 if buffer is empty. +int uart_rx_char(void) { + return ringbuf_get(&input_buf); +} + +int uart_rx_one_char(uint8 uart_no) { + if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { + return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff; + } + return -1; +} + +/****************************************************************************** + * FunctionName : uart_init + * Description : user interface for init uart + * Parameters : UartBautRate uart0_br - uart0 bautrate + * UartBautRate uart1_br - uart1 bautrate + * Returns : NONE +*******************************************************************************/ +void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) { + // rom use 74880 baut_rate, here reinitialize + UartDev.baut_rate = uart0_br; + uart_config(UART0); + UartDev.baut_rate = uart1_br; + uart_config(UART1); + ETS_UART_INTR_ENABLE(); + + // install handler for "os" messages + os_install_putc1((void *)uart_os_write_char); +} + +void ICACHE_FLASH_ATTR uart_reattach() { + uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880); +} + +void ICACHE_FLASH_ATTR uart_setup(uint8 uart) { + ETS_UART_INTR_DISABLE(); + uart_config(uart); + ETS_UART_INTR_ENABLE(); +} + +// Task-based UART interface + +#include "py/obj.h" +#include "lib/utils/pyexec.h" + +#if MICROPY_REPL_EVENT_DRIVEN +void uart_task_handler(os_event_t *evt) { + if (pyexec_repl_active) { + // TODO: Just returning here isn't exactly right. + // What really should be done is something like + // enquing delayed event to itself, for another + // chance to feed data to REPL. Otherwise, there + // can be situation when buffer has bunch of data, + // and sits unprocessed, because we consumed all + // processing signals like this. + return; + } + + int c, ret = 0; + while ((c = ringbuf_get(&input_buf)) >= 0) { + if (c == interrupt_char) { + mp_keyboard_interrupt(); + } + ret = pyexec_event_repl_process_char(c); + if (ret & PYEXEC_FORCED_EXIT) { + break; + } + } + + if (ret & PYEXEC_FORCED_EXIT) { + soft_reset(); + } +} + +void uart_task_init() { + system_os_task(uart_task_handler, UART_TASK_ID, uart_evt_queue, sizeof(uart_evt_queue) / sizeof(*uart_evt_queue)); +} +#endif diff --git a/esp8266/espuart.h b/esp8266/espuart.h new file mode 100644 index 000000000..2b97683ff --- /dev/null +++ b/esp8266/espuart.h @@ -0,0 +1,103 @@ +#ifndef _INCLUDED_UART_H_ +#define _INCLUDED_UART_H_ + +#include + +#define UART0 (0) +#define UART1 (1) + +typedef enum { + UART_FIVE_BITS = 0x0, + UART_SIX_BITS = 0x1, + UART_SEVEN_BITS = 0x2, + UART_EIGHT_BITS = 0x3 +} UartBitsNum4Char; + +typedef enum { + UART_ONE_STOP_BIT = 0, + UART_ONE_HALF_STOP_BIT = BIT2, + UART_TWO_STOP_BIT = BIT2 +} UartStopBitsNum; + +typedef enum { + UART_NONE_BITS = 0, + UART_ODD_BITS = BIT0, + UART_EVEN_BITS = 0 +} UartParityMode; + +typedef enum { + UART_STICK_PARITY_DIS = 0, + UART_STICK_PARITY_EN = BIT1 +} UartExistParity; + +typedef enum { + UART_BIT_RATE_9600 = 9600, + UART_BIT_RATE_19200 = 19200, + UART_BIT_RATE_38400 = 38400, + UART_BIT_RATE_57600 = 57600, + UART_BIT_RATE_74880 = 74880, + UART_BIT_RATE_115200 = 115200, + UART_BIT_RATE_230400 = 230400, + UART_BIT_RATE_256000 = 256000, + UART_BIT_RATE_460800 = 460800, + UART_BIT_RATE_921600 = 921600 +} UartBautRate; + +typedef enum { + UART_NONE_CTRL, + UART_HARDWARE_CTRL, + UART_XON_XOFF_CTRL +} UartFlowCtrl; + +typedef enum { + UART_EMPTY, + UART_UNDER_WRITE, + UART_WRITE_OVER +} RcvMsgBuffState; + +typedef struct { + uint32 RcvBuffSize; + uint8 *pRcvMsgBuff; + uint8 *pWritePos; + uint8 *pReadPos; + uint8 TrigLvl; //JLU: may need to pad + RcvMsgBuffState BuffState; +} RcvMsgBuff; + +typedef struct { + uint32 TrxBuffSize; + uint8 *pTrxBuff; +} TrxMsgBuff; + +typedef enum { + UART_BAUD_RATE_DET, + UART_WAIT_SYNC_FRM, + UART_SRCH_MSG_HEAD, + UART_RCV_MSG_BODY, + UART_RCV_ESC_CHAR, +} RcvMsgState; + +typedef struct { + UartBautRate baut_rate; + UartBitsNum4Char data_bits; + UartExistParity exist_parity; + UartParityMode parity; // chip size in byte + UartStopBitsNum stop_bits; + UartFlowCtrl flow_ctrl; + RcvMsgBuff rcv_buff; + TrxMsgBuff trx_buff; + RcvMsgState rcv_state; + int received; + int buff_uart_no; //indicate which uart use tx/rx buffer +} UartDevice; + +void uart_init(UartBautRate uart0_br, UartBautRate uart1_br); +int uart0_rx(void); +bool uart_rx_wait(uint32_t timeout_us); +int uart_rx_char(void); +void uart_tx_one_char(uint8 uart, uint8 TxChar); +void uart_flush(uint8 uart); +void uart_os_config(int uart); +void uart_setup(uint8 uart); + +#endif // _INCLUDED_UART_H_ diff --git a/esp8266/machine_uart.c b/esp8266/machine_uart.c index 9bc6422cc..0ae9c6e6c 100644 --- a/esp8266/machine_uart.c +++ b/esp8266/machine_uart.c @@ -29,7 +29,7 @@ #include #include "ets_sys.h" -#include "uart.h" +#include "espuart.h" #include "py/runtime.h" #include "py/stream.h" diff --git a/esp8266/main.c b/esp8266/main.c index 0c1ad1bab..9ac728e38 100644 --- a/esp8266/main.c +++ b/esp8266/main.c @@ -40,7 +40,7 @@ #include "gccollect.h" #include "user_interface.h" #include "common-hal/microcontroller/Pin.h" -#include "common-hal/nativeio/PWMOut.h" +#include "common-hal/pulseio/PWMOut.h" STATIC char heap[36 * 1024]; diff --git a/esp8266/modesp.c b/esp8266/modesp.c index f13d94b32..77f8d1332 100644 --- a/esp8266/modesp.c +++ b/esp8266/modesp.c @@ -38,7 +38,7 @@ #include "netutils.h" #include "queue.h" #include "ets_sys.h" -#include "uart.h" +#include "espuart.h" #include "user_interface.h" #include "espconn.h" #include "spi_flash.h" diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index fa2e8bc1b..c4a375200 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -156,7 +156,10 @@ 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 analogio_module; +extern const struct _mp_obj_module_t digitalio_module; +extern const struct _mp_obj_module_t pulseio_module; +extern const struct _mp_obj_module_t busio_module; extern const struct _mp_obj_module_t bitbangio_module; extern const struct _mp_obj_module_t time_module; @@ -172,7 +175,10 @@ extern const struct _mp_obj_module_t time_module; { MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&onewire_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)µcontroller_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_analogio), (mp_obj_t)&analogio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_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 }, \ diff --git a/esp8266/uart.c b/esp8266/uart.c deleted file mode 100644 index 001a9c673..000000000 --- a/esp8266/uart.c +++ /dev/null @@ -1,281 +0,0 @@ -/****************************************************************************** - * Copyright 2013-2014 Espressif Systems (Wuxi) - * - * FileName: uart.c - * - * Description: Two UART mode configration and interrupt handler. - * Check your hardware connection while use this mode. - * - * Modification history: - * 2014/3/12, v1.0 create this file. -*******************************************************************************/ -#include "ets_sys.h" -#include "osapi.h" -#include "uart.h" -#include "osapi.h" -#include "uart_register.h" -#include "etshal.h" -#include "c_types.h" -#include "user_interface.h" -#include "esp_mphal.h" - -// seems that this is missing in the Espressif SDK -#define FUNC_U0RXD 0 - -#define UART_REPL UART0 - -// UartDev is defined and initialized in rom code. -extern UartDevice UartDev; - -// the uart to which OS messages go; -1 to disable -static int uart_os = UART_OS; - -#if MICROPY_REPL_EVENT_DRIVEN -static os_event_t uart_evt_queue[16]; -#endif - -static void uart0_rx_intr_handler(void *para); - -void soft_reset(void); -void mp_keyboard_interrupt(void); - -/****************************************************************************** - * FunctionName : uart_config - * Description : Internal used function - * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled - * UART1 just used for debug output - * Parameters : uart_no, use UART0 or UART1 defined ahead - * Returns : NONE -*******************************************************************************/ -static void ICACHE_FLASH_ATTR uart_config(uint8 uart_no) { - if (uart_no == UART1) { - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); - } else { - ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, NULL); - PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD); - } - - uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate)); - - WRITE_PERI_REG(UART_CONF0(uart_no), UartDev.exist_parity - | UartDev.parity - | (UartDev.stop_bits << UART_STOP_BIT_NUM_S) - | (UartDev.data_bits << UART_BIT_NUM_S)); - - // clear rx and tx fifo,not ready - SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST); - CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST); - - if (uart_no == UART0) { - // set rx fifo trigger - WRITE_PERI_REG(UART_CONF1(uart_no), - ((0x10 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) | - ((0x10 & UART_RX_FLOW_THRHD) << UART_RX_FLOW_THRHD_S) | - UART_RX_FLOW_EN | - (0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S | - UART_RX_TOUT_EN); - SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_TOUT_INT_ENA | - UART_FRM_ERR_INT_ENA); - } else { - WRITE_PERI_REG(UART_CONF1(uart_no), - ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S)); - } - - // clear all interrupt - WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff); - // enable rx_interrupt - SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA); -} - -/****************************************************************************** - * FunctionName : uart1_tx_one_char - * Description : Internal used function - * Use uart1 interface to transfer one char - * Parameters : uint8 TxChar - character to tx - * Returns : OK -*******************************************************************************/ -void uart_tx_one_char(uint8 uart, uint8 TxChar) { - while (true) { - uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) { - break; - } - } - WRITE_PERI_REG(UART_FIFO(uart), TxChar); -} - -void uart_flush(uint8 uart) { - while (true) { - uint32 fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT<> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) == 0) { - break; - } - } -} - -/****************************************************************************** - * FunctionName : uart1_write_char - * Description : Internal used function - * Do some special deal while tx char is '\r' or '\n' - * Parameters : char c - character to tx - * Returns : NONE -*******************************************************************************/ -static void ICACHE_FLASH_ATTR -uart_os_write_char(char c) { - if (uart_os == -1) { - return; - } - if (c == '\n') { - uart_tx_one_char(uart_os, '\r'); - uart_tx_one_char(uart_os, '\n'); - } else if (c == '\r') { - } else { - uart_tx_one_char(uart_os, c); - } -} - -void ICACHE_FLASH_ATTR -uart_os_config(int uart) { - uart_os = uart; -} - -/****************************************************************************** - * FunctionName : uart0_rx_intr_handler - * Description : Internal used function - * UART0 interrupt handler, add self handle code inside - * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg - * Returns : NONE -*******************************************************************************/ - -static void uart0_rx_intr_handler(void *para) { - /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents - * uart1 and uart0 respectively - */ - - uint8 uart_no = UART_REPL; - - if (UART_FRM_ERR_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_FRM_ERR_INT_ST)) { - // frame error - WRITE_PERI_REG(UART_INT_CLR(uart_no), UART_FRM_ERR_INT_CLR); - } - - if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_FULL_INT_ST)) { - // fifo full - goto read_chars; - } else if (UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)) { - read_chars: - ETS_UART_INTR_DISABLE(); - - while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { - uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff; - if (RcvChar == mp_interrupt_char) { - mp_keyboard_interrupt(); - } else { - ringbuf_put(&input_buf, RcvChar); - } - } - - mp_hal_signal_input(); - - // Clear pending FIFO interrupts - WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST); - ETS_UART_INTR_ENABLE(); - } -} - -// Waits at most timeout microseconds for at least 1 char to become ready for reading. -// Returns true if something available, false if not. -bool uart_rx_wait(uint32_t timeout_us) { - uint32_t start = system_get_time(); - for (;;) { - if (input_buf.iget != input_buf.iput) { - return true; // have at least 1 char ready for reading - } - if (system_get_time() - start >= timeout_us) { - return false; // timeout - } - ets_event_poll(); - } -} - -// Returns char from the input buffer, else -1 if buffer is empty. -int uart_rx_char(void) { - return ringbuf_get(&input_buf); -} - -int uart_rx_one_char(uint8 uart_no) { - if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { - return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff; - } - return -1; -} - -/****************************************************************************** - * FunctionName : uart_init - * Description : user interface for init uart - * Parameters : UartBautRate uart0_br - uart0 bautrate - * UartBautRate uart1_br - uart1 bautrate - * Returns : NONE -*******************************************************************************/ -void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br) { - // rom use 74880 baut_rate, here reinitialize - UartDev.baut_rate = uart0_br; - uart_config(UART0); - UartDev.baut_rate = uart1_br; - uart_config(UART1); - ETS_UART_INTR_ENABLE(); - - // install handler for "os" messages - os_install_putc1((void *)uart_os_write_char); -} - -void ICACHE_FLASH_ATTR uart_reattach() { - uart_init(UART_BIT_RATE_74880, UART_BIT_RATE_74880); -} - -void ICACHE_FLASH_ATTR uart_setup(uint8 uart) { - ETS_UART_INTR_DISABLE(); - uart_config(uart); - ETS_UART_INTR_ENABLE(); -} - -// Task-based UART interface - -#include "py/obj.h" -#include "lib/utils/pyexec.h" - -#if MICROPY_REPL_EVENT_DRIVEN -void uart_task_handler(os_event_t *evt) { - if (pyexec_repl_active) { - // TODO: Just returning here isn't exactly right. - // What really should be done is something like - // enquing delayed event to itself, for another - // chance to feed data to REPL. Otherwise, there - // can be situation when buffer has bunch of data, - // and sits unprocessed, because we consumed all - // processing signals like this. - return; - } - - int c, ret = 0; - while ((c = ringbuf_get(&input_buf)) >= 0) { - if (c == interrupt_char) { - mp_keyboard_interrupt(); - } - ret = pyexec_event_repl_process_char(c); - if (ret & PYEXEC_FORCED_EXIT) { - break; - } - } - - if (ret & PYEXEC_FORCED_EXIT) { - soft_reset(); - } -} - -void uart_task_init() { - system_os_task(uart_task_handler, UART_TASK_ID, uart_evt_queue, sizeof(uart_evt_queue) / sizeof(*uart_evt_queue)); -} -#endif diff --git a/esp8266/uart.h b/esp8266/uart.h deleted file mode 100644 index 2b97683ff..000000000 --- a/esp8266/uart.h +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef _INCLUDED_UART_H_ -#define _INCLUDED_UART_H_ - -#include - -#define UART0 (0) -#define UART1 (1) - -typedef enum { - UART_FIVE_BITS = 0x0, - UART_SIX_BITS = 0x1, - UART_SEVEN_BITS = 0x2, - UART_EIGHT_BITS = 0x3 -} UartBitsNum4Char; - -typedef enum { - UART_ONE_STOP_BIT = 0, - UART_ONE_HALF_STOP_BIT = BIT2, - UART_TWO_STOP_BIT = BIT2 -} UartStopBitsNum; - -typedef enum { - UART_NONE_BITS = 0, - UART_ODD_BITS = BIT0, - UART_EVEN_BITS = 0 -} UartParityMode; - -typedef enum { - UART_STICK_PARITY_DIS = 0, - UART_STICK_PARITY_EN = BIT1 -} UartExistParity; - -typedef enum { - UART_BIT_RATE_9600 = 9600, - UART_BIT_RATE_19200 = 19200, - UART_BIT_RATE_38400 = 38400, - UART_BIT_RATE_57600 = 57600, - UART_BIT_RATE_74880 = 74880, - UART_BIT_RATE_115200 = 115200, - UART_BIT_RATE_230400 = 230400, - UART_BIT_RATE_256000 = 256000, - UART_BIT_RATE_460800 = 460800, - UART_BIT_RATE_921600 = 921600 -} UartBautRate; - -typedef enum { - UART_NONE_CTRL, - UART_HARDWARE_CTRL, - UART_XON_XOFF_CTRL -} UartFlowCtrl; - -typedef enum { - UART_EMPTY, - UART_UNDER_WRITE, - UART_WRITE_OVER -} RcvMsgBuffState; - -typedef struct { - uint32 RcvBuffSize; - uint8 *pRcvMsgBuff; - uint8 *pWritePos; - uint8 *pReadPos; - uint8 TrigLvl; //JLU: may need to pad - RcvMsgBuffState BuffState; -} RcvMsgBuff; - -typedef struct { - uint32 TrxBuffSize; - uint8 *pTrxBuff; -} TrxMsgBuff; - -typedef enum { - UART_BAUD_RATE_DET, - UART_WAIT_SYNC_FRM, - UART_SRCH_MSG_HEAD, - UART_RCV_MSG_BODY, - UART_RCV_ESC_CHAR, -} RcvMsgState; - -typedef struct { - UartBautRate baut_rate; - UartBitsNum4Char data_bits; - UartExistParity exist_parity; - UartParityMode parity; // chip size in byte - UartStopBitsNum stop_bits; - UartFlowCtrl flow_ctrl; - RcvMsgBuff rcv_buff; - TrxMsgBuff trx_buff; - RcvMsgState rcv_state; - int received; - int buff_uart_no; //indicate which uart use tx/rx buffer -} UartDevice; - -void uart_init(UartBautRate uart0_br, UartBautRate uart1_br); -int uart0_rx(void); -bool uart_rx_wait(uint32_t timeout_us); -int uart_rx_char(void); -void uart_tx_one_char(uint8 uart, uint8 TxChar); -void uart_flush(uint8 uart); -void uart_os_config(int uart); -void uart_setup(uint8 uart); - -#endif // _INCLUDED_UART_H_ -- cgit v1.2.3