circuitpython/ports/espressif/boards/m5stack_stick_c/board.c

362 lines
13 KiB
C
Executable File

/*
* 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.
*/
#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 "axp192.h"
// display init sequence according to adafruit_st7735r.py library
uint8_t display_init_sequence[] = {
0x01,0x80,0x96, // SWRESET and Delay 150ms
0x11,0x80,0xff, // SLPOUT and Delay
0xb1,0x03,0x01,0x2C,0x2D, // _FRMCTR1
0xb2,0x03,0x01,0x2C,0x2D, // _FRMCTR2
0xb3,0x06,0x01,0x2C,0x2D,0x01,0x2C,0x2D, // _FRMCTR3
0xb4,0x01,0x07, // _INVCTR line inversion
0xc0,0x03,0xa2,0x02,0x84, // _PWCTR1 GVDD = 4.7V, 1.0uA
0xc1,0x01,0xc5, // _PWCTR2 VGH=14.7V, VGL=-7.35V
0xc2,0x02,0x0a,0x00, // _PWCTR3 Opamp current small, Boost frequency
0xc3,0x02,0x8a,0x2a,
0xc4,0x02,0x8a,0xee,
0xc5,0x01,0x0e, // _VMCTR1 VCOMH = 4V, VOML = -1.1V
0x36,0x01,0xc8, // MADCTL Rotate display
0x21,0x00, // _INVON
0x3a,0x01,0x05, // COLMOD - 16bit color
0xe0,0x10,0x02,0x1c,0x07,0x12,0x37,0x32,0x29,0x2d,0x29,0x25,0x2B,0x39,0x00,0x01,0x03,0x10, // _GMCTRP1 Gamma
0xe1,0x10,0x03,0x1d,0x07,0x06,0x2E,0x2C,0x29,0x2D,0x2E,0x2E,0x37,0x3F,0x00,0x00,0x02,0x10, // _GMCTRN1
0x13,0x80,0x0a, // _NORON
0x29,0x80,0x64 // _DISPON
};
static bool pmic_init(void) {
int rc;
// uint8_t read_buf[1];
uint8_t write_buf[2];
busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0);
// 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 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));
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_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));
if (rc != 0) {
return false;
}
// Reg: 90h
// 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));
if (rc != 0) {
return false;
}
// Reg: 91h
// 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));
if (rc != 0) {
return false;
}
// Reg: 28h
// LDO2 (TFT backlight): 2.8V
// LDO3 (TFT logic): 3.0V
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));
if (rc != 0) {
return false;
}
// Reg: 12h
// Enable CTRL_EXTEN, DCDC1, LDO2 and LDO3
write_buf[0] = AXP192_DCDC13_LDO23_CTRL;
write_buf[1] = AXP192_DCDC13_LDO23_CTRL_EXTEN |
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));
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(internal_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) {
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) {
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) {
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) {
return false;
}
return true;
}
static bool display_init(void) {
busio_spi_obj_t *spi = &displays[0].fourwire_bus.inline_bus;
common_hal_busio_spi_construct(spi, &pin_GPIO13, &pin_GPIO15, NULL, false);
common_hal_busio_spi_never_reset(spi);
displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus;
bus->base.type = &displayio_fourwire_type;
common_hal_displayio_fourwire_construct(
bus,
spi,
&pin_GPIO23, // DC
&pin_GPIO5, // CS
&pin_GPIO18, // RST
10000000, // 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,
80, // width (after rotation)
160, // height (after rotation)
26, // column start
1, // 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
80, // native_frames_per_second
false, // backlight_on_high
false, // SH1107_addressing
50000 // backlight pwm frequency
);
return true;
}
void board_init(void) {
if (!pmic_init()) {
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;
}
}
bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
// Set IR led gpio high to prevent power drain from the led
if (pin_number == 9) {
gpio_set_direction(pin_number, GPIO_MODE_DEF_OUTPUT);
gpio_set_level(pin_number, true);
return true;
}
return false;
}