diff --git a/ports/espressif/boards/m5stack_core2/board.c b/ports/espressif/boards/m5stack_core2/board.c new file mode 100644 index 0000000000..a2565229ce --- /dev/null +++ b/ports/espressif/boards/m5stack_core2/board.c @@ -0,0 +1,399 @@ +/* + * + * The MIT License (MIT) + * + * Copyright (c) 2023 CDarius + * + * 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 "supervisor/board.h" +#include "mpconfigboard.h" +#include "shared-bindings/busio/SPI.h" +#include "shared-bindings/busio/I2C.h" +#include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-module/displayio/__init__.h" +#include "shared-module/displayio/mipi_constants.h" +#include "shared-bindings/board/__init__.h" + +#include "shared-bindings/microcontroller/Pin.h" +#include "components/driver/include/driver/gpio.h" +#include "components/hal/include/hal/gpio_hal.h" +#include "common-hal/microcontroller/Pin.h" + +#include "../../pmic/axp192/axp192.h" + +displayio_fourwire_obj_t board_display_obj; + +#define DELAY 0x80 +#define PMIC_POWER_SOURCE_USB 0 +#define PMIC_POWER_SOURCE_M_BUS 1 + +// display init sequence according to M5Gfx +uint8_t display_init_sequence[] = { + 0x01,DELAY,0x80, // Software reset then delay 0x80 (128ms) + 0xC8,0x03,0xFF,0x93,0x42, // Turn on the external command + 0xC0,0x02,0x12, 0x12, // Power Control 1 + 0xC1,0x01,0x03, // Power Control 2 + 0xC5,0x01,0xF2, // VCOM Control 1 + 0xB0,0x01,0xE0, // RGB Interface SYNC Mode + 0xF6,0x03,0x01, 0x00, 0x00, // Interface control + 0XE0,0x0F,0x00,0x0C,0x11,0x04,0x11,0x08,0x37,0x89,0x4C,0x06,0x0C,0x0A,0x2E,0x34,0x0F, // Positive Gamma Correction + 0xE1,0x0F,0x00,0x0B,0x11,0x05,0x13,0x09,0x33,0x67,0x48,0x07,0x0E,0x0B,0x2E,0x33,0x0F, // Negative Gamma Correction + 0xB6,0x04,0x08,0x82,0x1D,0x04, // Display Function Control + 0x3A,0x01,0x55, // COLMOD: Pixel Format Set 16 bit + 0x21,0x00, // Display inversion ON + 0x36,0x01,0x08, // Memory Access Control: RGB order + 0x11,DELAY,0x78, // Exit Sleep then delay 0x78 (120ms) + 0x29,DELAY,0x78, // Display on then delay 0x78 (120ms) +}; + +static bool pmic_set_power_source(uint8_t source, busio_i2c_obj_t *i2c) { + int rc; + uint8_t read_buf[1]; + uint8_t write_buf[2]; + + switch (source) + { + case PMIC_POWER_SOURCE_USB: + // Set GPIO to 3.3V (when LDO OUTPUT mode is active) + write_buf[0] = AXP192_GPIO0_LDO_VOLTAGE; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + write_buf[1] = (read_buf[0] & ~AXP192_GPIO0_LDO_VOLTAGE_MASK) | AXP192_GPIO0_LDO_VOLTAGE_3_3V; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + // Set GPIO0 to LDO_OUTPUT to set N_VBUSEN high + // When N_VBUSEN is high IPSOUT do not select VBUS as source (BUS_5V) + write_buf[0] = AXP192_GPIO0_FUNCTION; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + write_buf[1] = (read_buf[0] & ~AXP192_GPIO0_FUNCTION_MASK) | AXP192_GPIO0_FUNCTION_LDO_OUTPUT; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + #if M5STACK_CORE2_5V_OUTPUT_ENABLE_DEFAULT + // Set EXTENT output high to enable 5V power boost + write_buf[0] = AXP192_DCDC13_LDO23_CTRL; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + write_buf[1] = read_buf[0] | AXP192_DCDC13_LDO23_CTRL_EXTEN; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + #endif + + // Enable VBUS-IPSOUT when N_VBUSEN is high + write_buf[0] = AXP192_VBUS_IPSOUT; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + write_buf[1] = read_buf[0] & ~AXP192_VBUS_IPSOUT_IGNORE_VBUSEN; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + return true; + + case PMIC_POWER_SOURCE_M_BUS: + // Enable VBUS-IPSOUT regardless of f N_VBUSEN + write_buf[0] = AXP192_VBUS_IPSOUT; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + write_buf[1] = read_buf[0] | AXP192_VBUS_IPSOUT_IGNORE_VBUSEN; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Set EXTENT output low to disable 5V power boost + write_buf[0] = AXP192_DCDC13_LDO23_CTRL; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + write_buf[1] = read_buf[0] & ~AXP192_DCDC13_LDO23_CTRL_EXTEN; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Set GPIO0 to float and the pull down resistor set N_VBUSEN low + // When N_VBUSEN is low IPSOUT select VBUS as source (BUS_5V) + write_buf[0] = AXP192_GPIO0_FUNCTION; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + write_buf[1] = (read_buf[0] & ~AXP192_GPIO0_FUNCTION_MASK) | AXP192_GPIO0_FUNCTION_FLOATING; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + return true; + + default: + return false; + } +} + +static bool pmic_init(busio_i2c_obj_t *i2c) { + int rc; + uint8_t read_buf[1]; + uint8_t write_buf[2]; + + if (!pmic_common_init(i2c)) { + return false; + } + + // Reg: 30h + // The VBUS-IPSOUT path can be selected to be opened regardless of the status of N_VBUSEN + // VBUS VHOLD pressure limit control disabled + // VBUS current limit control enabled + write_buf[0] = AXP192_VBUS_IPSOUT; + write_buf[1] = AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT | + AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT_500mA; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 33h + // Charge function enable control bit, including internal and external channels + // Charging target voltage: 4.2V + // Charging end current: End charging when charging current is less than 10% setting + // Internal path charging current: 100mA + write_buf[0] = AXP192_CHARGING_CTRL1; + write_buf[1] = AXP192_CHARGING_CTRL1_ENABLE | + AXP192_CHARGING_CTRL1_VOLTAGE_4_20V | + AXP192_CHARGING_CTRL1_CHARGING_THRESH_10PERC | + AXP192_CHARGING_CTRL1_CURRENT_360mA; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 98h + // PWM1 frequency + write_buf[0] = AXP192_PWM1_OUTPUT_FREQUECY; + write_buf[1] = 0x00; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 99h + // PWM1 duty cycle Y1 + write_buf[0] = AXP192_PWM1_DUTY_RATIO_Y1; + write_buf[1] = 0xFF; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 9Ah + // PWM1 duty cycle Y2 + write_buf[0] = AXP192_PWM1_DUTY_RATIO_Y2; + write_buf[1] = 0xFF; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 93h + // Speaker off (GPIO2 output low) + write_buf[0] = AXP192_GPIO2_FUNCTION; + write_buf[1] = AXP192_GPIO2_FUNCTION_LOW_OUTPUT; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 28h + // LDO2 (SD + TFT Logic): 3.3V + // LDO3 (Vibration motore): Set to minimum voltage 1.8V + write_buf[0] = AXP192_LDO23_OUT_VOLTAGE; + write_buf[1] = AXP192_LDO23_OUT_VOLTAGE_LDO2_3_3V | + AXP192_LDO23_OUT_VOLTAGE_LDO3_1_8V; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 26h + // DCDC1 (ESP32 VDD): 3.350V + write_buf[0] = AXP192_DCDC1_OUT_VOLTAGE; + write_buf[1] = AXP192_DCDC1_OUT_VOLTAGE_3_350V; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 27h + // DCDC3 (TFT backlight): 3.0V + write_buf[0] = AXP192_DCDC3_OUT_VOLTAGE; + write_buf[1] = AXP192_DCDC3_OUT_VOLTAGE_3_0V; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 12h + // Enable: + // DCDC1 ESP32 VDD, touch screen and 3.3V on M-Bus + // DCDC3 LCD backlight + // LDO2 LCD logic and SD card + write_buf[0] = AXP192_DCDC13_LDO23_CTRL; + write_buf[1] = AXP192_DCDC13_LDO23_CTRL_LDO2 | + AXP192_DCDC13_LDO23_CTRL_DCDC3 | + AXP192_DCDC13_LDO23_CTRL_DCDC1; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 92h + // Set GPIO1 Sys green LED output as PWM + // PWM duty cycle is set to 100% therefore sys led is off + write_buf[0] = AXP192_GPIO1_FUNCTION; + write_buf[1] = AXP192_GPIO1_FUNCTION_PWM1_OUTPUT; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + /* + * Select power source: + * If there is voltage on VBUS means that BUS_5V is powered from M-BUS + * and it can use VBUS as power source. If no voltage is present on VBUS + * disable VBUS and rely on ACIN (USB_5V) as power source. + */ + write_buf[0] = AXP192_INPUT_POWER_STATE; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + uint8_t powersource = (read_buf[0] & AXP192_INPUT_POWER_STATE_VBUS_AVAILABLE) ? + PMIC_POWER_SOURCE_M_BUS : PMIC_POWER_SOURCE_USB; + if (!pmic_set_power_source(powersource, i2c)) { + return false; + } + + if (!pmic_disable_all_irq(i2c)) { + return false; + } + + if (!pmic_clear_all_irq(i2c)) { + return false; + } + + if (!pmic_enable_power_key_press_irq(i2c)) { + return false; + } + + if (!pmic_enable_low_battery_irq(i2c)) { + return false; + } + + return true; +} + +static bool display_init(void) { + busio_spi_obj_t *spi = common_hal_board_create_spi(0); + displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus; + bus->base.type = &displayio_fourwire_type; + + common_hal_displayio_fourwire_construct( + bus, + spi, + &pin_GPIO15, // DC + &pin_GPIO5, // CS + NULL, // RST + 32000000, // baudrate + 0, // polarity + 0 // phase + ); + + displayio_display_obj_t *display = &displays[0].display; + display->base.type = &displayio_display_type; + + common_hal_displayio_display_construct( + display, + bus, + 320, // width (after rotation) + 240, // height (after rotation) + 0, // column start + 0, // row start + 0, // rotation + 16, // color depth + false, // grayscale + false, // pixels in a byte share a row. Only valid for depths < 8 + 1, // bytes per cell. Only valid for depths < 8 + false, // reverse_pixels_in_byte. Only valid for depths < 8 + true, // reverse_pixels_in_word + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command + display_init_sequence, + sizeof(display_init_sequence), + NULL, // backlight pin + NO_BRIGHTNESS_COMMAND, + 1.0f, // brightness + false, // single_byte_bounds + false, // data_as_commands + true, // auto_refresh + 61, // native_frames_per_second + true, // backlight_on_high + false, // SH1107_addressing + 50000 // backlight pwm frequency + ); + + return true; +} + +void board_init(void) { + busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0); + + if (!pmic_init(internal_i2c)) { + mp_printf(&mp_plat_print, "could not initialize axp192 pmic\n"); + return; + } + + if (!display_init()) { + mp_printf(&mp_plat_print, "could not initialize the display"); + return; + } +} diff --git a/ports/espressif/boards/m5stack_core2/mpconfigboard.h b/ports/espressif/boards/m5stack_core2/mpconfigboard.h new file mode 100755 index 0000000000..f55f754fa7 --- /dev/null +++ b/ports/espressif/boards/m5stack_core2/mpconfigboard.h @@ -0,0 +1,44 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2023 CDarius + * + * 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. + */ + +// Micropython setup + +#define MICROPY_HW_BOARD_NAME "M5Stack Core2" +#define MICROPY_HW_MCU_NAME "ESP32" + +#define CIRCUITPY_BOARD_I2C (2) +#define CIRCUITPY_BOARD_I2C_PIN {{.scl = &pin_GPIO22, .sda = &pin_GPIO21}, \ + {.scl = &pin_GPIO33, .sda = &pin_GPIO32}} + +#define CIRCUITPY_BOARD_SPI (1) +#define CIRCUITPY_BOARD_SPI_PIN {{.clock = &pin_GPIO18, .mosi = &pin_GPIO23, .miso = &pin_GPIO38}} + +#define CIRCUITPY_BOARD_UART (1) +#define CIRCUITPY_BOARD_UART_PIN {{.tx = &pin_GPIO14, .rx = &pin_GPIO13}} + +// UART pins attached to the USB-serial converter chip +#define CIRCUITPY_CONSOLE_UART_TX (&pin_GPIO1) +#define CIRCUITPY_CONSOLE_UART_RX (&pin_GPIO3) diff --git a/ports/espressif/boards/m5stack_core2/mpconfigboard.mk b/ports/espressif/boards/m5stack_core2/mpconfigboard.mk new file mode 100644 index 0000000000..74ed5bcb83 --- /dev/null +++ b/ports/espressif/boards/m5stack_core2/mpconfigboard.mk @@ -0,0 +1,20 @@ +CIRCUITPY_CREATOR_ID = 0x10151015 +CIRCUITPY_CREATION_ID = 0x00320008 + +IDF_TARGET = esp32 + +CIRCUITPY_ESP_FLASH_MODE = qio +CIRCUITPY_ESP_FLASH_FREQ = 80m +CIRCUITPY_ESP_FLASH_SIZE = 16MB + +M5STACK_CORE2_5V_OUTPUT_ENABLE_DEFAULT = 1 + +CFLAGS += -DM5STACK_CORE2_5V_OUTPUT_ENABLE_DEFAULT=$(M5STACK_CORE2_5V_OUTPUT_ENABLE_DEFAULT) + +SRC_C += pmic/axp192/axp192.c + +# Include these Python libraries in firmware. +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Display_Text +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_FakeRequests +FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Requests diff --git a/ports/espressif/boards/m5stack_core2/pins.c b/ports/espressif/boards/m5stack_core2/pins.c new file mode 100644 index 0000000000..ee8fe21ad9 --- /dev/null +++ b/ports/espressif/boards/m5stack_core2/pins.c @@ -0,0 +1,95 @@ +#include "shared-bindings/board/__init__.h" +#include "shared-module/displayio/__init__.h" + +CIRCUITPY_BOARD_BUS_SINGLETON(porta_i2c, i2c, 1) + +STATIC const mp_rom_map_elem_t board_module_globals_table[] = { + CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS + + // External pins are in silkscreen order, from top to bottom, left side, then right side + + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_GPIO23) }, + { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_GPIO23) }, + + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_GPIO38) }, + { MP_ROM_QSTR(MP_QSTR_D38), MP_ROM_PTR(&pin_GPIO38) }, + + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_GPIO18) }, + { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_GPIO18) }, + + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_GPIO3) }, + + { MP_ROM_QSTR(MP_QSTR_PORTC_RX), MP_ROM_PTR(&pin_GPIO13) }, + { MP_ROM_QSTR(MP_QSTR_RX2), MP_ROM_PTR(&pin_GPIO13) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_GPIO13) }, + + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_GPIO21) }, + { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_GPIO21) }, + + { MP_ROM_QSTR(MP_QSTR_PORTA_SDA), MP_ROM_PTR(&pin_GPIO32) }, + { MP_ROM_QSTR(MP_QSTR_D32), MP_ROM_PTR(&pin_GPIO32) }, + + { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_GPIO27) }, + { MP_ROM_QSTR(MP_QSTR_A27), MP_ROM_PTR(&pin_GPIO27) }, + + { MP_ROM_QSTR(MP_QSTR_I2S_SDO), MP_ROM_PTR(&pin_GPIO2) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_GPIO2) }, + + { MP_ROM_QSTR(MP_QSTR_A35), MP_ROM_PTR(&pin_GPIO35) }, + { MP_ROM_QSTR(MP_QSTR_D35), MP_ROM_PTR(&pin_GPIO35) }, + + { MP_ROM_QSTR(MP_QSTR_PORTB_IN), MP_ROM_PTR(&pin_GPIO36) }, + { MP_ROM_QSTR(MP_QSTR_A36), MP_ROM_PTR(&pin_GPIO36) }, + { MP_ROM_QSTR(MP_QSTR_D36), MP_ROM_PTR(&pin_GPIO36) }, + + { MP_ROM_QSTR(MP_QSTR_A25), MP_ROM_PTR(&pin_GPIO25) }, + { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_GPIO25) }, + + { MP_ROM_QSTR(MP_QSTR_PORTB_OUT), MP_ROM_PTR(&pin_GPIO26) }, + { MP_ROM_QSTR(MP_QSTR_A26), MP_ROM_PTR(&pin_GPIO26) }, + { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_GPIO26) }, + + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_GPIO1) }, + + { MP_ROM_QSTR(MP_QSTR_PORTC_TX), MP_ROM_PTR(&pin_GPIO14) }, + { MP_ROM_QSTR(MP_QSTR_TX2), MP_ROM_PTR(&pin_GPIO14) }, + { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_GPIO14) }, + + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_GPIO22) }, + { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_GPIO22) }, + + { MP_ROM_QSTR(MP_QSTR_PORTA_SCL), MP_ROM_PTR(&pin_GPIO33) }, + { MP_ROM_QSTR(MP_QSTR_D33), MP_ROM_PTR(&pin_GPIO33) }, + + { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_GPIO19) }, + + { MP_ROM_QSTR(MP_QSTR_I2S_LRC), MP_ROM_PTR(&pin_GPIO0) }, + { MP_ROM_QSTR(MP_QSTR_I2S_PDM_MIC_CLOCK), MP_ROM_PTR(&pin_GPIO0) }, + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_GPIO0) }, + + { MP_ROM_QSTR(MP_QSTR_I2S_PDM_MIC_DATA), MP_ROM_PTR(&pin_GPIO34) }, + { MP_ROM_QSTR(MP_QSTR_D34), MP_ROM_PTR(&pin_GPIO34) }, + { MP_ROM_QSTR(MP_QSTR_A34), MP_ROM_PTR(&pin_GPIO34) }, + + { MP_ROM_QSTR(MP_QSTR_I2S_SCK), MP_ROM_PTR(&pin_GPIO12) }, + + // tft + { MP_ROM_QSTR(MP_QSTR_TFT_CS), MP_ROM_PTR(&pin_GPIO5) }, + { MP_ROM_QSTR(MP_QSTR_TFT_DC), MP_ROM_PTR(&pin_GPIO15) }, + // { MP_ROM_QSTR(MP_QSTR_TFT_RESET), MP_ROM_PTR(&pin_GPIO33) }, AXP_IO4 + // { MP_ROM_QSTR(MP_QSTR_TFT_BACKLIGHT), MP_ROM_PTR(&pin_GPIO32) }, AXP_DC3 + + // sd card + { MP_ROM_QSTR(MP_QSTR_SD_CS),MP_ROM_PTR(&pin_GPIO4) }, + + // touch screen + { MP_ROM_QSTR(MP_QSTR_TOUCH_INT),MP_ROM_PTR(&pin_GPIO39) }, + + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_PORTA_I2C), MP_ROM_PTR(&board_porta_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, + + { MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)} +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/espressif/boards/m5stack_core2/sdkconfig b/ports/espressif/boards/m5stack_core2/sdkconfig new file mode 100644 index 0000000000..47a0a18a5e --- /dev/null +++ b/ports/espressif/boards/m5stack_core2/sdkconfig @@ -0,0 +1,34 @@ +CONFIG_ESP32_ECO3_CACHE_LOCK_FIX=y +CONFIG_ESP32_SPIRAM_SUPPORT=y +CONFIG_ESP32_REV_MIN_3=y + +# +# LWIP +# +CONFIG_LWIP_LOCAL_HOSTNAME="M5StaskCore2" +# end of LWIP + +# +# SPI RAM config +# +# CONFIG_SPIRAM_TYPE_AUTO=y +# CONFIG_SPIRAM_TYPE_ESPPSRAM16 is not set +# CONFIG_SPIRAM_TYPE_ESPPSRAM32 is not set +CONFIG_SPIRAM_TYPE_ESPPSRAM64=y +CONFIG_SPIRAM_SIZE=8388608 +# end of SPI RAM config + +# CONFIG_SPIRAM_FETCH_INSTRUCTIONS is not set +# CONFIG_SPIRAM_RODATA is not set +CONFIG_SPIRAM_SPEED_80M=y +# CONFIG_SPIRAM_SPEED_40M is not set +# CONFIG_SPIRAM_SPEED_26M is not set +# CONFIG_SPIRAM_SPEED_20M is not set +CONFIG_SPIRAM=y +CONFIG_SPIRAM_BOOT_INIT=y +# CONFIG_SPIRAM_IGNORE_NOTFOUND is not set +CONFIG_SPIRAM_USE_MEMMAP=y +# CONFIG_SPIRAM_USE_CAPS_ALLOC is not set +# CONFIG_SPIRAM_USE_MALLOC is not set +CONFIG_SPIRAM_MEMTEST=y +# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set diff --git a/ports/espressif/boards/m5stack_stick_c/board.c b/ports/espressif/boards/m5stack_stick_c/board.c index 2f993d4a05..d25663895c 100755 --- a/ports/espressif/boards/m5stack_stick_c/board.c +++ b/ports/espressif/boards/m5stack_stick_c/board.c @@ -29,12 +29,15 @@ #include "shared-bindings/busio/SPI.h" #include "shared-bindings/busio/I2C.h" #include "shared-bindings/displayio/FourWire.h" -#include "shared-bindings/microcontroller/Pin.h" #include "shared-module/displayio/__init__.h" #include "shared-module/displayio/mipi_constants.h" #include "shared-bindings/board/__init__.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "components/driver/include/driver/gpio.h" +#include "components/hal/include/hal/gpio_hal.h" +#include "common-hal/microcontroller/Pin.h" -#include "axp192.h" +#include "../../pmic/axp192/axp192.h" // display init sequence according to adafruit_st7735r.py library uint8_t display_init_sequence[] = { @@ -59,12 +62,13 @@ uint8_t display_init_sequence[] = { 0x29,0x80,0x64 // _DISPON }; -static bool pmic_init(void) { +static bool pmic_init(busio_i2c_obj_t *i2c) { int rc; - // uint8_t read_buf[1]; uint8_t write_buf[2]; - busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0); + if (!pmic_common_init(i2c)) { + return false; + } // Reg: 30h // The VBUS-IPSOUT path can be selected to be opened regardless of the status of N_VBUSEN @@ -72,16 +76,7 @@ static bool pmic_init(void) { // VBUS current limit control disabled write_buf[0] = AXP192_VBUS_IPSOUT; write_buf[1] = AXP192_VBUS_IPSOUT_IGNORE_VBUSEN; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 31h - // VOFF Shutdown voltage setting ( 3.0V ) - write_buf[0] = AXP192_POWER_OFF_VOLTAGE; - write_buf[1] = AXP192_POWER_OFF_VOLTAGE_3_0V; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); if (rc != 0) { return false; } @@ -95,88 +90,7 @@ static bool pmic_init(void) { write_buf[1] = AXP192_CHARGING_CTRL1_ENABLE | AXP192_CHARGING_CTRL1_VOLTAGE_4_20V | AXP192_CHARGING_CTRL1_CURRENT_100mA; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 35h - // Enable RTC battery charge: 3.0V, 200uA - write_buf[0] = AXP192_BACKUP_BATT; - write_buf[1] = AXP192_BACKUP_BATT_CHARGING_ENABLE | - AXP192_BACKUP_BATT_CHARGING_VOLTAGE_3_0V | - AXP192_BACKUP_BATT_CHARGING_CURRENT_200uA; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 36h - // Power on: Short press 128ms - // Power off: Long press 1s - // Power OK delay 64ms - // Power off delay 4s - write_buf[0] = AXP192_PEK; - write_buf[1] = AXP192_PEK_SHORT_PRESS_128mS | - AXP192_PEK_LONG_PRESS_1_0S | - AXP192_PEK_LONG_PRESS_POWER_OFF | - AXP192_PEK_PWROK_DELAY_64mS | - AXP192_PEK_POWER_OFF_TIME_4S; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 3Ah - // APS Low battery warning level 1: 3.695V - write_buf[0] = AXP192_APS_LOW_BATT_LEVEL_1; - write_buf[1] = AXP192_APS_LOW_BATT_VOLTAGE_3_695V; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 3Bh - // APS Low battery warning level 2: 3.600V - write_buf[0] = AXP192_APS_LOW_BATT_LEVEL_2; - write_buf[1] = AXP192_APS_LOW_BATT_VOLTAGE_3_600V; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 82h - // ADC all on - write_buf[0] = AXP192_ADC_ENABLE_1; - write_buf[1] = AXP192_ADC_ENABLE_1_BATT_VOL | - AXP192_ADC_ENABLE_1_BATT_CUR | - AXP192_ADC_ENABLE_1_ACIN_VOL | - AXP192_ADC_ENABLE_1_ACIN_CUR | - AXP192_ADC_ENABLE_1_VBUS_VOL | - AXP192_ADC_ENABLE_1_VBUS_CUR | - AXP192_ADC_ENABLE_1_APS_VOL | - AXP192_ADC_ENABLE_1_TS_PIN; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 83h - // ADC temperature on - write_buf[0] = AXP192_ADC_ENABLE_2; - write_buf[1] = AXP192_ADC_ENABLE_2_TEMP_MON; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 84h - // ADC 25Hz - write_buf[0] = AXP192_ADC_TS; - write_buf[1] = AXP192_ADC_TS_SAMPLE_25HZ | - AXP192_ADC_TS_OUT_CUR_80uA | - AXP192_ADC_TS_PIN_OUT_SAVE_ENG; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); if (rc != 0) { return false; } @@ -185,7 +99,7 @@ static bool pmic_init(void) { // GPIO0(LDOio0) floating write_buf[0] = AXP192_GPIO0_FUNCTION; write_buf[1] = AXP192_GPIO0_FUNCTION_FLOATING; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); if (rc != 0) { return false; } @@ -194,7 +108,7 @@ static bool pmic_init(void) { // GPIO0(LDOio0) 2.8V write_buf[0] = AXP192_GPIO0_LDO_VOLTAGE; write_buf[1] = AXP192_GPIO0_LDO_VOLTAGE_2_8V; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); if (rc != 0) { return false; } @@ -205,7 +119,7 @@ static bool pmic_init(void) { write_buf[0] = AXP192_LDO23_OUT_VOLTAGE; write_buf[1] = AXP192_LDO23_OUT_VOLTAGE_LDO2_2_8V | AXP192_LDO23_OUT_VOLTAGE_LDO3_3_0V; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); if (rc != 0) { return false; } @@ -217,7 +131,7 @@ static bool pmic_init(void) { AXP192_DCDC13_LDO23_CTRL_LDO3 | AXP192_DCDC13_LDO23_CTRL_LDO2 | AXP192_DCDC13_LDO23_CTRL_DCDC1; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); if (rc != 0) { return false; } @@ -226,56 +140,24 @@ static bool pmic_init(void) { // DCDC1 (ESP32 VDD): 3.350V write_buf[0] = AXP192_DCDC1_OUT_VOLTAGE; write_buf[1] = AXP192_DCDC1_OUT_VOLTAGE_3_350V; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); if (rc != 0) { return false; } - // Reg: 40h - // IRQ enable control register 1 - write_buf[0] = AXP192_IRQ_1_ENABLE; - write_buf[1] = AXP192_IRQ_X_DISABLE_ALL; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { + if (!pmic_disable_all_irq(i2c)) { return false; } - // Reg: 41h - // IRQ enable control register 2 - write_buf[0] = AXP192_IRQ_2_ENABLE; - write_buf[1] = AXP192_IRQ_X_DISABLE_ALL; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { + if (!pmic_clear_all_irq(i2c)) { return false; } - // Reg: 42h - // IRQ enable control register 3 - // Enable power on key short and long press interrupt - write_buf[0] = AXP192_IRQ_2_ENABLE; - write_buf[1] = AXP192_IRQ_3_PEK_SHORT_PRESS | - AXP192_IRQ_3_PEK_LONG_PRESS; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { + if (!pmic_enable_power_key_press_irq(i2c)) { return false; } - // Reg: 43h - // IRQ enable control register 4 - // Enable power on key short and long press interrupt - write_buf[0] = AXP192_IRQ_2_ENABLE; - write_buf[1] = AXP192_IRQ_4_LOW_VOLTAGE_WARNING; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { - return false; - } - - // Reg: 44h - // IRQ enable control register 5 - write_buf[0] = AXP192_IRQ_2_ENABLE; - write_buf[1] = AXP192_IRQ_X_DISABLE_ALL; - rc = common_hal_busio_i2c_write(internal_i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); - if (rc != 0) { + if (!pmic_enable_low_battery_irq(i2c)) { return false; } @@ -339,7 +221,9 @@ static bool display_init(void) { } void board_init(void) { - if (!pmic_init()) { + busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0); + + if (!pmic_init(internal_i2c)) { mp_printf(&mp_plat_print, "could not initialize axp192 pmic\n"); return; } diff --git a/ports/espressif/boards/m5stack_stick_c/mpconfigboard.mk b/ports/espressif/boards/m5stack_stick_c/mpconfigboard.mk index 5910605915..ce4d23a303 100644 --- a/ports/espressif/boards/m5stack_stick_c/mpconfigboard.mk +++ b/ports/espressif/boards/m5stack_stick_c/mpconfigboard.mk @@ -7,3 +7,5 @@ CIRCUITPY_ESP_FLASH_MODE = qio CIRCUITPY_ESP_FLASH_FREQ = 80m CIRCUITPY_ESP_FLASH_SIZE = 4MB CIRCUITPY_ESPCAMERA = 0 + +SRC_C += pmic/axp192/axp192.c diff --git a/ports/espressif/pmic/axp192/axp192.c b/ports/espressif/pmic/axp192/axp192.c new file mode 100644 index 0000000000..fe698a0416 --- /dev/null +++ b/ports/espressif/pmic/axp192/axp192.c @@ -0,0 +1,255 @@ +#include "axp192.h" + +bool pmic_common_init(busio_i2c_obj_t *i2c) { + int rc; + uint8_t write_buf[2]; + + // Reg: 31h + // VOFF Shutdown voltage setting ( 3.0V ) + write_buf[0] = AXP192_POWER_OFF_VOLTAGE; + write_buf[1] = AXP192_POWER_OFF_VOLTAGE_3_0V; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 32h + // Enable battery monitoring + // N_OE shout down delay 2 seconds + write_buf[0] = AXP192_POWER_OFF_BATT_CHGLED_CTRL; + write_buf[1] = AXP192_POWER_OFF_BATT_CHGLED_CTRL_BATT_MONITOR_ON | + AXP192_POWER_OFF_BATT_CHGLED_CTRL_N_OE_SHUTDOWN_DELAY_2_0S; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 35h + // Enable RTC battery charge: 3.0V, 200uA + write_buf[0] = AXP192_BACKUP_BATT; + write_buf[1] = AXP192_BACKUP_BATT_CHARGING_ENABLE | + AXP192_BACKUP_BATT_CHARGING_VOLTAGE_3_0V | + AXP192_BACKUP_BATT_CHARGING_CURRENT_200uA; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 36h + // Power on: Short press 128ms + // Power off: Long press 1s + // Power OK delay 64ms + // Power off delay 4s + write_buf[0] = AXP192_PEK; + write_buf[1] = AXP192_PEK_SHORT_PRESS_128mS | + AXP192_PEK_LONG_PRESS_1_0S | + AXP192_PEK_LONG_PRESS_POWER_OFF | + AXP192_PEK_PWROK_DELAY_64mS | + AXP192_PEK_POWER_OFF_TIME_4S; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 3Ah + // APS Low battery warning level 1: 3.695V + write_buf[0] = AXP192_APS_LOW_BATT_LEVEL_1; + write_buf[1] = AXP192_APS_LOW_BATT_VOLTAGE_3_695V; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 3Bh + // APS Low battery warning level 2: 3.600V + write_buf[0] = AXP192_APS_LOW_BATT_LEVEL_2; + write_buf[1] = AXP192_APS_LOW_BATT_VOLTAGE_3_600V; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 82h + // ADC all on + write_buf[0] = AXP192_ADC_ENABLE_1; + write_buf[1] = AXP192_ADC_ENABLE_1_BATT_VOL | + AXP192_ADC_ENABLE_1_BATT_CUR | + AXP192_ADC_ENABLE_1_ACIN_VOL | + AXP192_ADC_ENABLE_1_ACIN_CUR | + AXP192_ADC_ENABLE_1_VBUS_VOL | + AXP192_ADC_ENABLE_1_VBUS_CUR | + AXP192_ADC_ENABLE_1_APS_VOL | + AXP192_ADC_ENABLE_1_TS_PIN; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 83h + // ADC temperature on + write_buf[0] = AXP192_ADC_ENABLE_2; + write_buf[1] = AXP192_ADC_ENABLE_2_TEMP_MON; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 84h + // ADC 25Hz + write_buf[0] = AXP192_ADC_TS; + write_buf[1] = AXP192_ADC_TS_SAMPLE_25HZ | + AXP192_ADC_TS_OUT_CUR_80uA | + AXP192_ADC_TS_PIN_TEMP_MON | + AXP192_ADC_TS_PIN_OUT_SAVE_ENG; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + return true; +} + +bool pmic_disable_all_irq(busio_i2c_obj_t *i2c) { + int rc; + uint8_t write_buf[2]; + + // Reg: 40h + // IRQ enable control register 1 + write_buf[0] = AXP192_IRQ_1_ENABLE; + write_buf[1] = AXP192_IRQ_X_DISABLE_ALL; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 41h + // IRQ enable control register 2 + write_buf[0] = AXP192_IRQ_2_ENABLE; + write_buf[1] = AXP192_IRQ_X_DISABLE_ALL; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 42h + // IRQ enable control register 3 + // Enable power on key short and long press interrupt + write_buf[0] = AXP192_IRQ_3_ENABLE; + write_buf[1] = AXP192_IRQ_X_DISABLE_ALL; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 43h + // IRQ enable control register 4 + // Enable power on key short and long press interrupt + write_buf[0] = AXP192_IRQ_4_ENABLE; + write_buf[1] = AXP192_IRQ_X_DISABLE_ALL; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 4Ah + // IRQ enable control register 5 + write_buf[0] = AXP192_IRQ_5_ENABLE; + write_buf[1] = AXP192_IRQ_X_DISABLE_ALL; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + return true; +} + +bool pmic_clear_all_irq(busio_i2c_obj_t *i2c) { + int rc; + uint8_t write_buf[2]; + + // Reg: 44h + // IRQ enable control register 1 + write_buf[0] = AXP192_IRQ_1_STATUS; + write_buf[1] = AXP192_IRQ_X_CLEAR_STATUS; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 45h + // IRQ enable control register 2 + write_buf[0] = AXP192_IRQ_2_STATUS; + write_buf[1] = AXP192_IRQ_X_CLEAR_STATUS; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 46h + // IRQ enable control register 3 + write_buf[0] = AXP192_IRQ_3_STATUS; + write_buf[1] = AXP192_IRQ_X_CLEAR_STATUS; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 47h + // IRQ enable control register 4 + write_buf[0] = AXP192_IRQ_4_STATUS; + write_buf[1] = AXP192_IRQ_X_CLEAR_STATUS; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + // Reg: 4Ah + // IRQ enable control register 5 + write_buf[0] = AXP192_IRQ_5_STATUS; + write_buf[1] = AXP192_IRQ_X_CLEAR_STATUS; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + return true; +} + +bool pmic_enable_power_key_press_irq(busio_i2c_obj_t *i2c) { + int rc; + uint8_t read_buf[1]; + uint8_t write_buf[2]; + + // Reg: 42h + // IRQ enable control register 3 + // Enable power on key short and long press interrupt + write_buf[0] = AXP192_IRQ_2_ENABLE; + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); + if (rc != 0) { + return false; + } + write_buf[1] = read_buf[0] | AXP192_IRQ_3_PEK_SHORT_PRESS | AXP192_IRQ_3_PEK_LONG_PRESS; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + return true; +} + +bool pmic_enable_low_battery_irq(busio_i2c_obj_t *i2c) { + int rc; + uint8_t write_buf[2]; + + // Reg: 43h + // IRQ enable control register 4 + // Enable power on key short and long press interrupt + write_buf[0] = AXP192_IRQ_2_ENABLE; + write_buf[1] = AXP192_IRQ_4_LOW_VOLTAGE_WARNING; + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); + if (rc != 0) { + return false; + } + + return true; +} diff --git a/ports/espressif/boards/m5stack_stick_c/axp192.h b/ports/espressif/pmic/axp192/axp192.h similarity index 83% rename from ports/espressif/boards/m5stack_stick_c/axp192.h rename to ports/espressif/pmic/axp192/axp192.h index aa8f6367a7..587ce04b0c 100755 --- a/ports/espressif/boards/m5stack_stick_c/axp192.h +++ b/ports/espressif/pmic/axp192/axp192.h @@ -28,33 +28,44 @@ #ifndef MICROPY_AXP192_H #define MICROPY_AXP192_H +#include "shared-bindings/busio/I2C.h" + #define AXP192_I2C_ADDRESS 0x34 -#define AXP192_EXTEN_DCDC2_CTRL 0x10 -#define AXP192_EXTEN_DCDC2_CTRL_EXTEN 0b00000100 -#define AXP192_EXTEN_DCDC2_CTRL_DCDC2 0b00000001 +#define AXP192_INPUT_POWER_STATE 0x00 +#define AXP192_INPUT_POWER_STATE_ACIN_IS_PRESENT 0b10000000 +#define AXP192_INPUT_POWER_STATE_ACIN_AVAILABLE 0b01000000 +#define AXP192_INPUT_POWER_STATE_VBUS_IS_PRESENT 0b00100000 +#define AXP192_INPUT_POWER_STATE_VBUS_AVAILABLE 0b00010000 +#define AXP192_INPUT_POWER_STATE_VBUS_GREATER_VHOLD_BEFORE_USE 0b00001000 +#define AXP192_INPUT_POWER_STATE_BATT_CURRENT_DIR_IS_CHARGING 0b00000100 +#define AXP192_INPUT_POWER_STATE_ACIN_VBUS_PCB_SHORTED 0b00000010 +#define AXP192_INPUT_POWER_STATE_BOOT_SOURCE_ACIN_OR_VBUS 0b00000001 #define AXP192_DCDC13_LDO23_CTRL 0x12 #define AXP192_DCDC13_LDO23_CTRL_EXTEN 0b01000000 -#define AXP192_DCDC13_LDO23_CTRL_LDO3 0b00001000 -#define AXP192_DCDC13_LDO23_CTRL_LDO2 0b00000100 +#define AXP192_DCDC13_LDO23_CTRL_DCDC2 0b00010000 +#define AXP192_DCDC13_LDO23_CTRL_LDO3 0b00001000 +#define AXP192_DCDC13_LDO23_CTRL_LDO2 0b00000100 #define AXP192_DCDC13_LDO23_CTRL_DCDC3 0b00000010 #define AXP192_DCDC13_LDO23_CTRL_DCDC1 0b00000001 -#define AXP192_DCDC2_OUT_VOLTAGE 0x25 +#define AXP192_DCDC2_OUT_VOLTAGE 0x23 #define AXP192_DCDC1_OUT_VOLTAGE 0x26 #define AXP192_DCDC1_OUT_VOLTAGE_3_350V 0b01101010 #define AXP192_DCDC3_OUT_VOLTAGE 0x27 - +#define AXP192_DCDC3_OUT_VOLTAGE_3_0V 0b01011100 #define AXP192_LDO23_OUT_VOLTAGE 0x28 +#define AXP192_LDO23_OUT_VOLTAGE_LDO2_3_3V 0b11110000 #define AXP192_LDO23_OUT_VOLTAGE_LDO2_3_0V 0b11000000 #define AXP192_LDO23_OUT_VOLTAGE_LDO2_2_8V 0b10100000 #define AXP192_LDO23_OUT_VOLTAGE_LDO2_MASK 0b11110000 #define AXP192_LDO23_OUT_VOLTAGE_LDO3_3_0V 0b00001100 #define AXP192_LDO23_OUT_VOLTAGE_LDO3_2_8V 0b00001010 +#define AXP192_LDO23_OUT_VOLTAGE_LDO3_1_8V 0b00000000 #define AXP192_LDO23_OUT_VOLTAGE_LDO3_MASK 0b00001111 #define AXP192_VBUS_IPSOUT 0x30 @@ -63,8 +74,8 @@ #define AXP192_VBUS_IPSOUT_VHOLD_VOLTAGE_4_4V 0b00100000 #define AXP192_VBUS_IPSOUT_VHOLD_VOLTAGE_MASK 0b00111000 #define AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT 0b00000010 -#define AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT_500mA 0b00000001 -#define AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT_100mA 0b00000000 +#define AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT_100mA 0b00000001 +#define AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT_500mA 0b00000000 #define AXP192_POWER_OFF_VOLTAGE 0x31 #define AXP192_POWER_OFF_VOLTAGE_2_6V 0b0000 @@ -79,6 +90,11 @@ #define AXP192_POWER_OFF_BATT_CHGLED_CTRL 0x32 #define AXP192_POWER_OFF_BATT_CHGLED_CTRL_OFF 0b10000000 +#define AXP192_POWER_OFF_BATT_CHGLED_CTRL_BATT_MONITOR_ON 0b01000000 +#define AXP192_POWER_OFF_BATT_CHGLED_CTRL_N_OE_SHUTDOWN_DELAY_3_0S 0b00000011 +#define AXP192_POWER_OFF_BATT_CHGLED_CTRL_N_OE_SHUTDOWN_DELAY_2_0S 0b00000010 +#define AXP192_POWER_OFF_BATT_CHGLED_CTRL_N_OE_SHUTDOWN_DELAY_1_0S 0b00000001 +#define AXP192_POWER_OFF_BATT_CHGLED_CTRL_N_OE_SHUTDOWN_DELAY_0_5S 0b00000000 #define AXP192_CHARGING_CTRL1 0x33 #define AXP192_CHARGING_CTRL1_ENABLE 0b10000000 @@ -166,6 +182,8 @@ #define AXP192_IRQ_4_STATUS 0x47 #define AXP192_IRQ_5_STATUS 0x4d +#define AXP192_IRQ_X_CLEAR_STATUS 0b11111111 + #define AXP192_ADC_ACIN_VOLTAGE_H 0x56 #define AXP192_ADC_ACIN_VOLTAGE_L 0x57 #define AXP192_ADC_ACIN_CURRENT_H 0x58 @@ -228,6 +246,7 @@ #define AXP192_ADC_TS_PIN_OUT_MASK 0b00000011 #define AXP192_GPIO0_FUNCTION 0x90 +#define AXP192_GPIO0_FUNCTION_MASK 0b00000111 #define AXP192_GPIO0_FUNCTION_FLOATING 0b00000111 #define AXP192_GPIO0_FUNCTION_LOW_OUTPUT 0b00000101 #define AXP192_GPIO0_FUNCTION_ADC_INPUT 0b00000100 @@ -236,6 +255,7 @@ #define AXP192_GPIO0_FUNCTION_OPEN_DRAIN_OUTPUT 0b00000000 #define AXP192_GPIO0_LDO_VOLTAGE 0x91 +#define AXP192_GPIO0_LDO_VOLTAGE_MASK 0b11110000 #define AXP192_GPIO0_LDO_VOLTAGE_3_3V 0b11110000 #define AXP192_GPIO0_LDO_VOLTAGE_2_8V 0b10100000 #define AXP192_GPIO0_LDO_VOLTAGE_1_8V 0b00000000 @@ -254,10 +274,18 @@ #define AXP192_GPIO2_FUNCTION_FLOATING 0b00000111 #define AXP192_GPIO2_FUNCTION_LOW_OUTPUT 0b00000101 #define AXP192_GPIO2_FUNCTION_ADC_INPUT 0b00000100 -#define AXP192_GPIO1_FUNCTION_PWM2_OUTPUT 0b00000010 +#define AXP192_GPIO2_FUNCTION_PWM2_OUTPUT 0b00000010 #define AXP192_GPIO2_FUNCTION_GENERAL_INPUT 0b00000001 #define AXP192_GPIO2_FUNCTION_OPEN_DRAIN_OUTPUT 0b00000000 -#define AXP192_PWM1_DUTY_RATIO 0x9A +#define AXP192_PWM1_OUTPUT_FREQUECY 0x98 +#define AXP192_PWM1_DUTY_RATIO_Y1 0x99 +#define AXP192_PWM1_DUTY_RATIO_Y2 0x9A + +bool pmic_common_init(busio_i2c_obj_t *i2c); +bool pmic_disable_all_irq(busio_i2c_obj_t *i2c); +bool pmic_clear_all_irq(busio_i2c_obj_t *i2c); +bool pmic_enable_power_key_press_irq(busio_i2c_obj_t *i2c); +bool pmic_enable_low_battery_irq(busio_i2c_obj_t *i2c); #endif