|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2023 n0xa |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "supervisor/board.h" |
| 28 | +#include "mpconfigboard.h" |
| 29 | +#include "shared-bindings/busio/SPI.h" |
| 30 | +#include "shared-bindings/busio/I2C.h" |
| 31 | +#include "shared-bindings/displayio/FourWire.h" |
| 32 | +#include "shared-module/displayio/__init__.h" |
| 33 | +#include "shared-module/displayio/mipi_constants.h" |
| 34 | +#include "shared-bindings/board/__init__.h" |
| 35 | +#include "shared-bindings/microcontroller/Pin.h" |
| 36 | +#include "components/driver/include/driver/gpio.h" |
| 37 | +#include "components/hal/include/hal/gpio_hal.h" |
| 38 | +#include "common-hal/microcontroller/Pin.h" |
| 39 | + |
| 40 | +#include "../../pmic/axp192/axp192.h" |
| 41 | + |
| 42 | +// display init sequence according to adafruit_st7735r.py library |
| 43 | +uint8_t display_init_sequence[] = { |
| 44 | + 0x01,0x80,0x96, // SWRESET and Delay 150ms |
| 45 | + 0x11,0x80,0xff, // SLPOUT and Delay |
| 46 | + 0xb1,0x03,0x01,0x2C,0x2D, // _FRMCTR1 |
| 47 | + 0xb2,0x03,0x01,0x2C,0x2D, // _FRMCTR2 |
| 48 | + 0xb3,0x06,0x01,0x2C,0x2D,0x01,0x2C,0x2D, // _FRMCTR3 |
| 49 | + 0xb4,0x01,0x07, // _INVCTR line inversion |
| 50 | + 0xc0,0x03,0xa2,0x02,0x84, // _PWCTR1 GVDD = 4.7V, 1.0uA |
| 51 | + 0xc1,0x01,0xc5, // _PWCTR2 VGH=14.7V, VGL=-7.35V |
| 52 | + 0xc2,0x02,0x0a,0x00, // _PWCTR3 Opamp current small, Boost frequency |
| 53 | + 0xc3,0x02,0x8a,0x2a, |
| 54 | + 0xc4,0x02,0x8a,0xee, |
| 55 | + 0xc5,0x01,0x0e, // _VMCTR1 VCOMH = 4V, VOML = -1.1V |
| 56 | + 0x36,0x01,0xc8, // MADCTL Rotate display |
| 57 | + 0x21,0x00, // _INVON |
| 58 | + 0x3a,0x01,0x05, // COLMOD - 16bit color |
| 59 | + 0xe0,0x10,0x02,0x1c,0x07,0x12,0x37,0x32,0x29,0x2d,0x29,0x25,0x2B,0x39,0x00,0x01,0x03,0x10, // _GMCTRP1 Gamma |
| 60 | + 0xe1,0x10,0x03,0x1d,0x07,0x06,0x2E,0x2C,0x29,0x2D,0x2E,0x2E,0x37,0x3F,0x00,0x00,0x02,0x10, // _GMCTRN1 |
| 61 | + 0x13,0x80,0x0a, // _NORON |
| 62 | + 0x29,0x80,0x64 // _DISPON |
| 63 | +}; |
| 64 | + |
| 65 | +static bool pmic_init(busio_i2c_obj_t *i2c) { |
| 66 | + int rc; |
| 67 | + uint8_t write_buf[2]; |
| 68 | + |
| 69 | + if (!pmic_common_init(i2c)) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + // Reg: 30h |
| 74 | + // The VBUS-IPSOUT path can be selected to be opened regardless of the status of N_VBUSEN |
| 75 | + // VBUS VHOLD pressure limit control disabled |
| 76 | + // VBUS current limit control disabled |
| 77 | + write_buf[0] = AXP192_VBUS_IPSOUT; |
| 78 | + write_buf[1] = AXP192_VBUS_IPSOUT_IGNORE_VBUSEN; |
| 79 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 80 | + if (rc != 0) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + // Reg: 33h |
| 85 | + // Charge function enable control bit, including internal and external channels |
| 86 | + // Charging target voltage: 4.2V |
| 87 | + // Charging end current: End charging when charging current is less than 10% setting |
| 88 | + // Internal path charging current: 100mA |
| 89 | + write_buf[0] = AXP192_CHARGING_CTRL1; |
| 90 | + write_buf[1] = AXP192_CHARGING_CTRL1_ENABLE | |
| 91 | + AXP192_CHARGING_CTRL1_VOLTAGE_4_20V | |
| 92 | + AXP192_CHARGING_CTRL1_CURRENT_100mA; |
| 93 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 94 | + if (rc != 0) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + // Reg: 90h |
| 99 | + // GPIO0(LDOio0) floating |
| 100 | + write_buf[0] = AXP192_GPIO0_FUNCTION; |
| 101 | + write_buf[1] = AXP192_GPIO0_FUNCTION_FLOATING; |
| 102 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 103 | + if (rc != 0) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + // Reg: 91h |
| 108 | + // GPIO0(LDOio0) 2.8V |
| 109 | + write_buf[0] = AXP192_GPIO0_LDO_VOLTAGE; |
| 110 | + write_buf[1] = AXP192_GPIO0_LDO_VOLTAGE_2_8V; |
| 111 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 112 | + if (rc != 0) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + // Reg: 28h |
| 117 | + // LDO2 (TFT backlight): 2.8V |
| 118 | + // LDO3 (TFT logic): 3.0V |
| 119 | + write_buf[0] = AXP192_LDO23_OUT_VOLTAGE; |
| 120 | + write_buf[1] = AXP192_LDO23_OUT_VOLTAGE_LDO2_2_8V | |
| 121 | + AXP192_LDO23_OUT_VOLTAGE_LDO3_3_0V; |
| 122 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 123 | + if (rc != 0) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + // Reg: 12h |
| 128 | + // Enable CTRL_EXTEN, DCDC1, LDO2 and LDO3 |
| 129 | + write_buf[0] = AXP192_DCDC13_LDO23_CTRL; |
| 130 | + write_buf[1] = AXP192_DCDC13_LDO23_CTRL_EXTEN | |
| 131 | + AXP192_DCDC13_LDO23_CTRL_LDO3 | |
| 132 | + AXP192_DCDC13_LDO23_CTRL_LDO2 | |
| 133 | + AXP192_DCDC13_LDO23_CTRL_DCDC1; |
| 134 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 135 | + if (rc != 0) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + // Reg: 26h |
| 140 | + // DCDC1 (ESP32 VDD): 3.350V |
| 141 | + write_buf[0] = AXP192_DCDC1_OUT_VOLTAGE; |
| 142 | + write_buf[1] = AXP192_DCDC1_OUT_VOLTAGE_3_350V; |
| 143 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 144 | + if (rc != 0) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + if (!pmic_disable_all_irq(i2c)) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + if (!pmic_clear_all_irq(i2c)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + if (!pmic_enable_power_key_press_irq(i2c)) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + if (!pmic_enable_low_battery_irq(i2c)) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + return true; |
| 165 | +} |
| 166 | + |
| 167 | +static bool display_init(void) { |
| 168 | + displayio_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; |
| 169 | + busio_spi_obj_t *spi = &bus->inline_bus; |
| 170 | + common_hal_busio_spi_construct(spi, &pin_GPIO13, &pin_GPIO15, NULL, false); |
| 171 | + common_hal_busio_spi_never_reset(spi); |
| 172 | + |
| 173 | + bus->base.type = &displayio_fourwire_type; |
| 174 | + |
| 175 | + common_hal_displayio_fourwire_construct( |
| 176 | + bus, |
| 177 | + spi, |
| 178 | + &pin_GPIO23, // DC |
| 179 | + &pin_GPIO5, // CS |
| 180 | + &pin_GPIO18, // RST |
| 181 | + 10000000, // baudrate |
| 182 | + 0, // polarity |
| 183 | + 0 // phase |
| 184 | + ); |
| 185 | + |
| 186 | + displayio_display_obj_t *display = &allocate_display()->display; |
| 187 | + display->base.type = &displayio_display_type; |
| 188 | + |
| 189 | + common_hal_displayio_display_construct( |
| 190 | + display, |
| 191 | + bus, |
| 192 | + 135, // width (after rotation) |
| 193 | + 240, // height (after rotation) |
| 194 | + 40, // column start |
| 195 | + 52, // row start |
| 196 | + 1, // rotation |
| 197 | + 16, // color depth |
| 198 | + false, // grayscale |
| 199 | + false, // pixels in a byte share a row. Only valid for depths < 8 |
| 200 | + 1, // bytes per cell. Only valid for depths < 8 |
| 201 | + false, // reverse_pixels_in_byte. Only valid for depths < 8 |
| 202 | + true, // reverse_pixels_in_word |
| 203 | + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command |
| 204 | + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command |
| 205 | + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command |
| 206 | + display_init_sequence, |
| 207 | + sizeof(display_init_sequence), |
| 208 | + NULL, // backlight pin |
| 209 | + NO_BRIGHTNESS_COMMAND, |
| 210 | + 1.0f, // brightness |
| 211 | + false, // single_byte_bounds |
| 212 | + false, // data_as_commands |
| 213 | + true, // auto_refresh |
| 214 | + 80, // native_frames_per_second |
| 215 | + false, // backlight_on_high |
| 216 | + false, // SH1107_addressing |
| 217 | + 50000 // backlight pwm frequency |
| 218 | + ); |
| 219 | + |
| 220 | + return true; |
| 221 | +} |
| 222 | + |
| 223 | +void board_init(void) { |
| 224 | + busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0); |
| 225 | + |
| 226 | + if (!pmic_init(internal_i2c)) { |
| 227 | + mp_printf(&mp_plat_print, "could not initialize axp192 pmic\n"); |
| 228 | + return; |
| 229 | + } |
| 230 | + |
| 231 | + if (!display_init()) { |
| 232 | + mp_printf(&mp_plat_print, "could not initialize the display"); |
| 233 | + return; |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +bool espressif_board_reset_pin_number(gpio_num_t pin_number) { |
| 238 | + // Set IR led gpio high to prevent power drain from the led |
| 239 | + if (pin_number == 9) { |
| 240 | + gpio_set_direction(pin_number, GPIO_MODE_DEF_OUTPUT); |
| 241 | + gpio_set_level(pin_number, true); |
| 242 | + return true; |
| 243 | + } |
| 244 | + return false; |
| 245 | +} |
0 commit comments