|
| 1 | +/* |
| 2 | + * |
| 3 | + * The MIT License (MIT) |
| 4 | + * |
| 5 | + * Copyright (c) 2023 CDarius |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include "supervisor/board.h" |
| 27 | +#include "mpconfigboard.h" |
| 28 | +#include "shared-bindings/busio/SPI.h" |
| 29 | +#include "shared-bindings/busio/I2C.h" |
| 30 | +#include "shared-bindings/displayio/FourWire.h" |
| 31 | +#include "shared-bindings/microcontroller/Pin.h" |
| 32 | +#include "shared-module/displayio/__init__.h" |
| 33 | +#include "shared-module/displayio/mipi_constants.h" |
| 34 | +#include "shared-bindings/board/__init__.h" |
| 35 | + |
| 36 | +#include "shared-bindings/microcontroller/Pin.h" |
| 37 | +#include "components/driver/include/driver/gpio.h" |
| 38 | +#include "components/hal/include/hal/gpio_hal.h" |
| 39 | +#include "common-hal/microcontroller/Pin.h" |
| 40 | + |
| 41 | +#include "../../pmic/axp192/axp192.h" |
| 42 | + |
| 43 | +displayio_fourwire_obj_t board_display_obj; |
| 44 | + |
| 45 | +#define DELAY 0x80 |
| 46 | +#define PMIC_POWER_SOURCE_USB 0 |
| 47 | +#define PMIC_POWER_SOURCE_M_BUS 1 |
| 48 | + |
| 49 | +// display init sequence according to M5Gfx |
| 50 | +uint8_t display_init_sequence[] = { |
| 51 | + 0x01,DELAY,0x80, // Software reset then delay 0x80 (128ms) |
| 52 | + 0xC8,0x03,0xFF,0x93,0x42, // Turn on the external command |
| 53 | + 0xC0,0x02,0x12, 0x12, // Power Control 1 |
| 54 | + 0xC1,0x01,0x03, // Power Control 2 |
| 55 | + 0xC5,0x01,0xF2, // VCOM Control 1 |
| 56 | + 0xB0,0x01,0xE0, // RGB Interface SYNC Mode |
| 57 | + 0xF6,0x03,0x01, 0x00, 0x00, // Interface control |
| 58 | + 0XE0,0x0F,0x00,0x0C,0x11,0x04,0x11,0x08,0x37,0x89,0x4C,0x06,0x0C,0x0A,0x2E,0x34,0x0F, // Positive Gamma Correction |
| 59 | + 0xE1,0x0F,0x00,0x0B,0x11,0x05,0x13,0x09,0x33,0x67,0x48,0x07,0x0E,0x0B,0x2E,0x33,0x0F, // Negative Gamma Correction |
| 60 | + 0xB6,0x04,0x08,0x82,0x1D,0x04, // Display Function Control |
| 61 | + 0x3A,0x01,0x55, // COLMOD: Pixel Format Set 16 bit |
| 62 | + 0x21,0x00, // Display inversion ON |
| 63 | + 0x36,0x01,0x08, // Memory Access Control: RGB order |
| 64 | + 0x11,DELAY,0x78, // Exit Sleep then delay 0x78 (120ms) |
| 65 | + 0x29,DELAY,0x78, // Display on then delay 0x78 (120ms) |
| 66 | +}; |
| 67 | + |
| 68 | +static bool pmic_set_power_source(uint8_t source, busio_i2c_obj_t *i2c) { |
| 69 | + int rc; |
| 70 | + uint8_t read_buf[1]; |
| 71 | + uint8_t write_buf[2]; |
| 72 | + |
| 73 | + switch (source) |
| 74 | + { |
| 75 | + case PMIC_POWER_SOURCE_USB: |
| 76 | + // Set GPIO to 3.3V (when LDO OUTPUT mode is active) |
| 77 | + write_buf[0] = AXP192_GPIO0_LDO_VOLTAGE; |
| 78 | + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); |
| 79 | + if (rc != 0) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + write_buf[1] = (read_buf[0] & ~AXP192_GPIO0_LDO_VOLTAGE_MASK) | AXP192_GPIO0_LDO_VOLTAGE_3_3V; |
| 83 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 84 | + if (rc != 0) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + // Set GPIO0 to LDO_OUTPUT to set N_VBUSEN high |
| 88 | + // When N_VBUSEN is high IPSOUT do not select VBUS as source (BUS_5V) |
| 89 | + write_buf[0] = AXP192_GPIO0_FUNCTION; |
| 90 | + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); |
| 91 | + if (rc != 0) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + write_buf[1] = (read_buf[0] & ~AXP192_GPIO0_FUNCTION_MASK) | AXP192_GPIO0_FUNCTION_LDO_OUTPUT; |
| 95 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 96 | + if (rc != 0) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + #if M5STACK_CORE2_5V_OUTPUT_ENABLE_DEFAULT |
| 101 | + // Set EXTENT output high to enable 5V power boost |
| 102 | + write_buf[0] = AXP192_DCDC13_LDO23_CTRL; |
| 103 | + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); |
| 104 | + if (rc != 0) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + write_buf[1] = read_buf[0] | AXP192_DCDC13_LDO23_CTRL_EXTEN; |
| 108 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 109 | + if (rc != 0) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + #endif |
| 113 | + |
| 114 | + // Enable VBUS-IPSOUT when N_VBUSEN is high |
| 115 | + write_buf[0] = AXP192_VBUS_IPSOUT; |
| 116 | + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); |
| 117 | + if (rc != 0) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + write_buf[1] = read_buf[0] & ~AXP192_VBUS_IPSOUT_IGNORE_VBUSEN; |
| 121 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 122 | + if (rc != 0) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + return true; |
| 127 | + |
| 128 | + case PMIC_POWER_SOURCE_M_BUS: |
| 129 | + // Enable VBUS-IPSOUT regardless of f N_VBUSEN |
| 130 | + write_buf[0] = AXP192_VBUS_IPSOUT; |
| 131 | + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); |
| 132 | + if (rc != 0) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + write_buf[1] = read_buf[0] | AXP192_VBUS_IPSOUT_IGNORE_VBUSEN; |
| 136 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 137 | + if (rc != 0) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + // Set EXTENT output low to disable 5V power boost |
| 142 | + write_buf[0] = AXP192_DCDC13_LDO23_CTRL; |
| 143 | + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); |
| 144 | + if (rc != 0) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + write_buf[1] = read_buf[0] & ~AXP192_DCDC13_LDO23_CTRL_EXTEN; |
| 148 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 149 | + if (rc != 0) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + // Set GPIO0 to float and the pull down resistor set N_VBUSEN low |
| 154 | + // When N_VBUSEN is low IPSOUT select VBUS as source (BUS_5V) |
| 155 | + write_buf[0] = AXP192_GPIO0_FUNCTION; |
| 156 | + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); |
| 157 | + if (rc != 0) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + write_buf[1] = (read_buf[0] & ~AXP192_GPIO0_FUNCTION_MASK) | AXP192_GPIO0_FUNCTION_FLOATING; |
| 161 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 162 | + if (rc != 0) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + return true; |
| 167 | + |
| 168 | + default: |
| 169 | + return false; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +static bool pmic_init(busio_i2c_obj_t *i2c) { |
| 174 | + int rc; |
| 175 | + uint8_t read_buf[1]; |
| 176 | + uint8_t write_buf[2]; |
| 177 | + |
| 178 | + if (!pmic_common_init(i2c)) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + // Reg: 30h |
| 183 | + // The VBUS-IPSOUT path can be selected to be opened regardless of the status of N_VBUSEN |
| 184 | + // VBUS VHOLD pressure limit control disabled |
| 185 | + // VBUS current limit control enabled |
| 186 | + write_buf[0] = AXP192_VBUS_IPSOUT; |
| 187 | + write_buf[1] = AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT | |
| 188 | + AXP192_VBUS_IPSOUT_VBUS_LIMIT_CURRENT_500mA; |
| 189 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 190 | + if (rc != 0) { |
| 191 | + return false; |
| 192 | + } |
| 193 | + |
| 194 | + // Reg: 33h |
| 195 | + // Charge function enable control bit, including internal and external channels |
| 196 | + // Charging target voltage: 4.2V |
| 197 | + // Charging end current: End charging when charging current is less than 10% setting |
| 198 | + // Internal path charging current: 100mA |
| 199 | + write_buf[0] = AXP192_CHARGING_CTRL1; |
| 200 | + write_buf[1] = AXP192_CHARGING_CTRL1_ENABLE | |
| 201 | + AXP192_CHARGING_CTRL1_VOLTAGE_4_20V | |
| 202 | + AXP192_CHARGING_CTRL1_CHARGING_THRESH_10PERC | |
| 203 | + AXP192_CHARGING_CTRL1_CURRENT_360mA; |
| 204 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 205 | + if (rc != 0) { |
| 206 | + return false; |
| 207 | + } |
| 208 | + |
| 209 | + // Reg: 98h |
| 210 | + // PWM1 frequency |
| 211 | + write_buf[0] = AXP192_PWM1_OUTPUT_FREQUECY; |
| 212 | + write_buf[1] = 0x00; |
| 213 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 214 | + if (rc != 0) { |
| 215 | + return false; |
| 216 | + } |
| 217 | + |
| 218 | + // Reg: 99h |
| 219 | + // PWM1 duty cycle Y1 |
| 220 | + write_buf[0] = AXP192_PWM1_DUTY_RATIO_Y1; |
| 221 | + write_buf[1] = 0xFF; |
| 222 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 223 | + if (rc != 0) { |
| 224 | + return false; |
| 225 | + } |
| 226 | + |
| 227 | + // Reg: 9Ah |
| 228 | + // PWM1 duty cycle Y2 |
| 229 | + write_buf[0] = AXP192_PWM1_DUTY_RATIO_Y2; |
| 230 | + write_buf[1] = 0xFF; |
| 231 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 232 | + if (rc != 0) { |
| 233 | + return false; |
| 234 | + } |
| 235 | + |
| 236 | + // Reg: 93h |
| 237 | + // Speaker off (GPIO2 output low) |
| 238 | + write_buf[0] = AXP192_GPIO2_FUNCTION; |
| 239 | + write_buf[1] = AXP192_GPIO2_FUNCTION_LOW_OUTPUT; |
| 240 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 241 | + if (rc != 0) { |
| 242 | + return false; |
| 243 | + } |
| 244 | + |
| 245 | + // Reg: 28h |
| 246 | + // LDO2 (SD + TFT Logic): 3.3V |
| 247 | + // LDO3 (Vibration motore): Set to minimum voltage 1.8V |
| 248 | + write_buf[0] = AXP192_LDO23_OUT_VOLTAGE; |
| 249 | + write_buf[1] = AXP192_LDO23_OUT_VOLTAGE_LDO2_3_3V | |
| 250 | + AXP192_LDO23_OUT_VOLTAGE_LDO3_1_8V; |
| 251 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 252 | + if (rc != 0) { |
| 253 | + return false; |
| 254 | + } |
| 255 | + |
| 256 | + // Reg: 26h |
| 257 | + // DCDC1 (ESP32 VDD): 3.350V |
| 258 | + write_buf[0] = AXP192_DCDC1_OUT_VOLTAGE; |
| 259 | + write_buf[1] = AXP192_DCDC1_OUT_VOLTAGE_3_350V; |
| 260 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 261 | + if (rc != 0) { |
| 262 | + return false; |
| 263 | + } |
| 264 | + |
| 265 | + // Reg: 27h |
| 266 | + // DCDC3 (TFT backlight): 3.0V |
| 267 | + write_buf[0] = AXP192_DCDC3_OUT_VOLTAGE; |
| 268 | + write_buf[1] = AXP192_DCDC3_OUT_VOLTAGE_3_0V; |
| 269 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 270 | + if (rc != 0) { |
| 271 | + return false; |
| 272 | + } |
| 273 | + |
| 274 | + // Reg: 12h |
| 275 | + // Enable: |
| 276 | + // DCDC1 ESP32 VDD, touch screen and 3.3V on M-Bus |
| 277 | + // DCDC3 LCD backlight |
| 278 | + // LDO2 LCD logic and SD card |
| 279 | + write_buf[0] = AXP192_DCDC13_LDO23_CTRL; |
| 280 | + write_buf[1] = AXP192_DCDC13_LDO23_CTRL_LDO2 | |
| 281 | + AXP192_DCDC13_LDO23_CTRL_DCDC3 | |
| 282 | + AXP192_DCDC13_LDO23_CTRL_DCDC1; |
| 283 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 284 | + if (rc != 0) { |
| 285 | + return false; |
| 286 | + } |
| 287 | + |
| 288 | + // Reg: 92h |
| 289 | + // Set GPIO1 Sys green LED output as PWM |
| 290 | + // PWM duty cycle is set to 100% therefore sys led is off |
| 291 | + write_buf[0] = AXP192_GPIO1_FUNCTION; |
| 292 | + write_buf[1] = AXP192_GPIO1_FUNCTION_PWM1_OUTPUT; |
| 293 | + rc = common_hal_busio_i2c_write(i2c, AXP192_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 294 | + if (rc != 0) { |
| 295 | + return false; |
| 296 | + } |
| 297 | + |
| 298 | + /* |
| 299 | + * Select power source: |
| 300 | + * If there is voltage on VBUS means that BUS_5V is powered from M-BUS |
| 301 | + * and it can use VBUS as power source. If no voltage is present on VBUS |
| 302 | + * disable VBUS and rely on ACIN (USB_5V) as power source. |
| 303 | + */ |
| 304 | + write_buf[0] = AXP192_INPUT_POWER_STATE; |
| 305 | + rc = common_hal_busio_i2c_write_read(i2c, AXP192_I2C_ADDRESS, write_buf, 1, read_buf, sizeof(read_buf)); |
| 306 | + if (rc != 0) { |
| 307 | + return false; |
| 308 | + } |
| 309 | + uint8_t powersource = (read_buf[0] & AXP192_INPUT_POWER_STATE_VBUS_AVAILABLE) ? |
| 310 | + PMIC_POWER_SOURCE_M_BUS : PMIC_POWER_SOURCE_USB; |
| 311 | + if (!pmic_set_power_source(powersource, i2c)) { |
| 312 | + return false; |
| 313 | + } |
| 314 | + |
| 315 | + if (!pmic_disable_all_irq(i2c)) { |
| 316 | + return false; |
| 317 | + } |
| 318 | + |
| 319 | + if (!pmic_clear_all_irq(i2c)) { |
| 320 | + return false; |
| 321 | + } |
| 322 | + |
| 323 | + if (!pmic_enable_power_key_press_irq(i2c)) { |
| 324 | + return false; |
| 325 | + } |
| 326 | + |
| 327 | + if (!pmic_enable_low_battery_irq(i2c)) { |
| 328 | + return false; |
| 329 | + } |
| 330 | + |
| 331 | + return true; |
| 332 | +} |
| 333 | + |
| 334 | +static bool display_init(void) { |
| 335 | + busio_spi_obj_t *spi = common_hal_board_create_spi(0); |
| 336 | + displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus; |
| 337 | + bus->base.type = &displayio_fourwire_type; |
| 338 | + |
| 339 | + common_hal_displayio_fourwire_construct( |
| 340 | + bus, |
| 341 | + spi, |
| 342 | + &pin_GPIO15, // DC |
| 343 | + &pin_GPIO5, // CS |
| 344 | + NULL, // RST |
| 345 | + 32000000, // baudrate |
| 346 | + 0, // polarity |
| 347 | + 0 // phase |
| 348 | + ); |
| 349 | + |
| 350 | + displayio_display_obj_t *display = &displays[0].display; |
| 351 | + display->base.type = &displayio_display_type; |
| 352 | + |
| 353 | + common_hal_displayio_display_construct( |
| 354 | + display, |
| 355 | + bus, |
| 356 | + 320, // width (after rotation) |
| 357 | + 240, // height (after rotation) |
| 358 | + 0, // column start |
| 359 | + 0, // row start |
| 360 | + 0, // rotation |
| 361 | + 16, // color depth |
| 362 | + false, // grayscale |
| 363 | + false, // pixels in a byte share a row. Only valid for depths < 8 |
| 364 | + 1, // bytes per cell. Only valid for depths < 8 |
| 365 | + false, // reverse_pixels_in_byte. Only valid for depths < 8 |
| 366 | + true, // reverse_pixels_in_word |
| 367 | + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command |
| 368 | + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command |
| 369 | + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command |
| 370 | + display_init_sequence, |
| 371 | + sizeof(display_init_sequence), |
| 372 | + NULL, // backlight pin |
| 373 | + NO_BRIGHTNESS_COMMAND, |
| 374 | + 1.0f, // brightness |
| 375 | + false, // single_byte_bounds |
| 376 | + false, // data_as_commands |
| 377 | + true, // auto_refresh |
| 378 | + 61, // native_frames_per_second |
| 379 | + true, // backlight_on_high |
| 380 | + false, // SH1107_addressing |
| 381 | + 50000 // backlight pwm frequency |
| 382 | + ); |
| 383 | + |
| 384 | + return true; |
| 385 | +} |
| 386 | + |
| 387 | +void board_init(void) { |
| 388 | + busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0); |
| 389 | + |
| 390 | + if (!pmic_init(internal_i2c)) { |
| 391 | + mp_printf(&mp_plat_print, "could not initialize axp192 pmic\n"); |
| 392 | + return; |
| 393 | + } |
| 394 | + |
| 395 | + if (!display_init()) { |
| 396 | + mp_printf(&mp_plat_print, "could not initialize the display"); |
| 397 | + return; |
| 398 | + } |
| 399 | +} |
0 commit comments