|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "supervisor/board.h" |
| 8 | +#include "mpconfigboard.h" |
| 9 | +#include "shared-bindings/busio/SPI.h" |
| 10 | +#include "shared-bindings/busio/I2C.h" |
| 11 | +#include "shared-bindings/fourwire/FourWire.h" |
| 12 | +#include "shared-bindings/microcontroller/Pin.h" |
| 13 | +#include "shared-module/displayio/__init__.h" |
| 14 | +#include "shared-module/displayio/mipi_constants.h" |
| 15 | +#include "shared-bindings/board/__init__.h" |
| 16 | +#include "common-hal/microcontroller/Pin.h" |
| 17 | + |
| 18 | + |
| 19 | +#define DELAY 0x80 |
| 20 | +#define AXP2101_I2C_ADDRESS 0x34 |
| 21 | +#define AW9523B_I2C_ADDRESS 0x58 |
| 22 | + |
| 23 | +uint8_t display_init_sequence[] = { |
| 24 | + 0x01, DELAY, 0x80, // Software reset then delay 0x80 (128ms) |
| 25 | + 0xC8, 0x03, 0xFF, 0x93, 0x42, // Turn on the external command |
| 26 | + 0xC0, 0x02, 0x12, 0x12, // Power Control 1 |
| 27 | + 0xC1, 0x01, 0x03, // Power Control 2 |
| 28 | + 0xC5, 0x01, 0xF2, // VCOM Control 1 |
| 29 | + 0xB0, 0x01, 0xE0, // RGB Interface SYNC Mode |
| 30 | + 0xF6, 0x03, 0x01, 0x00, 0x00, // Interface control |
| 31 | + 0XE0, 0x0F, 0x00, 0x0C, 0x11, 0x04, 0x11, 0x08, 0x37, 0x89, 0x4C, 0x06, 0x0C, 0x0A, 0x2E, 0x34, 0x0F, // Positive Gamma Correction |
| 32 | + 0xE1, 0x0F, 0x00, 0x0B, 0x11, 0x05, 0x13, 0x09, 0x33, 0x67, 0x48, 0x07, 0x0E, 0x0B, 0x2E, 0x33, 0x0F, // Negative Gamma Correction |
| 33 | + 0xB6, 0x04, 0x08, 0x82, 0x1D, 0x04, // Display Function Control |
| 34 | + 0x3A, 0x01, 0x55, // COLMOD: Pixel Format Set 16 bit |
| 35 | + 0x21, 0x00, // Display inversion ON |
| 36 | + 0x36, 0x01, 0x08, // Memory Access Control: RGB order |
| 37 | + 0x11, DELAY, 0x78, // Exit Sleep then delay 0x78 (120ms) |
| 38 | + 0x29, DELAY, 0x78, // Display on then delay 0x78 (120ms) |
| 39 | +}; |
| 40 | + |
| 41 | +static bool display_init(void) { |
| 42 | + busio_spi_obj_t *spi = common_hal_board_create_spi(0); |
| 43 | + fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; |
| 44 | + bus->base.type = &fourwire_fourwire_type; |
| 45 | + |
| 46 | + common_hal_fourwire_fourwire_construct( |
| 47 | + bus, |
| 48 | + spi, |
| 49 | + &pin_GPIO35, // DC |
| 50 | + &pin_GPIO3, // CS |
| 51 | + NULL, // RST |
| 52 | + 40000000, // baudrate |
| 53 | + 0, // polarity |
| 54 | + 0 // phase |
| 55 | + ); |
| 56 | + busdisplay_busdisplay_obj_t *display = &allocate_display()->display; |
| 57 | + display->base.type = &busdisplay_busdisplay_type; |
| 58 | + |
| 59 | + common_hal_busdisplay_busdisplay_construct( |
| 60 | + display, |
| 61 | + bus, |
| 62 | + 320, // width (after rotation) |
| 63 | + 240, // height (after rotation) |
| 64 | + 0, // column start |
| 65 | + 0, // row start |
| 66 | + 0, // rotation |
| 67 | + 16, // color depth |
| 68 | + false, // grayscale |
| 69 | + false, // pixels in a byte share a row. Only valid for depths < 8 |
| 70 | + 1, // bytes per cell. Only valid for depths < 8 |
| 71 | + false, // reverse_pixels_in_byte. Only valid for depths < 8 |
| 72 | + true, // reverse_pixels_in_word |
| 73 | + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command |
| 74 | + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command |
| 75 | + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command |
| 76 | + display_init_sequence, |
| 77 | + sizeof(display_init_sequence), |
| 78 | + NULL, // backlight pin |
| 79 | + NO_BRIGHTNESS_COMMAND, |
| 80 | + 1.0f, // brightness |
| 81 | + false, // single_byte_bounds |
| 82 | + false, // data_as_commands |
| 83 | + true, // auto_refresh |
| 84 | + 61, // native_frames_per_second |
| 85 | + true, // backlight_on_high |
| 86 | + false, // SH1107_addressing |
| 87 | + 50000 // backlight pwm frequency |
| 88 | + ); |
| 89 | + |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +static bool axp2101_init(busio_i2c_obj_t *i2c) { |
| 94 | + int rc; |
| 95 | + uint8_t write_buf[2]; |
| 96 | + |
| 97 | + // 0x90 = 0b1011_0011 // LDOS ON/OFF control 0 |
| 98 | + // Compared to CoreS3: ALDO2 (bit 2) and ALDO3 (bit 3) disabled (no ES7210/camera) |
| 99 | + write_buf[0] = 0x90; |
| 100 | + write_buf[1] = 0b10110011; |
| 101 | + rc = common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 102 | + if (rc != 0) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + // 0x92, 0x0D // ALDO1 set to 1.8v for AW88298 |
| 107 | + write_buf[0] = 0x92; |
| 108 | + write_buf[1] = 0x0D; |
| 109 | + rc = common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 110 | + if (rc != 0) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + // 0x95, 0x1C // ALDO4 set to 3.3v for TF card slot |
| 115 | + write_buf[0] = 0x95; |
| 116 | + write_buf[1] = 0x1C; |
| 117 | + rc = common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 118 | + if (rc != 0) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + // 0x99, 0x18 // DLDO1 set to 2.9v for TFT backlight |
| 123 | + write_buf[0] = 0x99; |
| 124 | + write_buf[1] = 0x18; |
| 125 | + rc = common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 126 | + if (rc != 0) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + // 0x27, 0x00 // PowerKey Hold=1sec / PowerOff=4sec |
| 131 | + write_buf[0] = 0x27; |
| 132 | + write_buf[1] = 0x00; |
| 133 | + rc = common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 134 | + if (rc != 0) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + // 0x69, 0x11 // CHGLED setting |
| 139 | + write_buf[0] = 0x69; |
| 140 | + write_buf[1] = 0x11; |
| 141 | + rc = common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 142 | + if (rc != 0) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + // 0x10, 0x30 // PMU common config |
| 147 | + write_buf[0] = 0x10; |
| 148 | + write_buf[1] = 0x30; |
| 149 | + rc = common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 150 | + if (rc != 0) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + return true; |
| 155 | +} |
| 156 | + |
| 157 | +static bool aw9523b_init(busio_i2c_obj_t *i2c) { |
| 158 | + int rc; |
| 159 | + uint8_t write_buf[2]; |
| 160 | + |
| 161 | + // 0x02 = 0b0000_0111 // AW_RST, BUD_OUT_EN, TOUCH_RST |
| 162 | + write_buf[0] = 0x02; |
| 163 | + write_buf[1] = 0b00000111; |
| 164 | + rc = common_hal_busio_i2c_write(i2c, AW9523B_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 165 | + if (rc != 0) { |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + // 0x03 = 0b1000_0011 // BOOST_EN, CAM_RST, LCD_RST |
| 170 | + write_buf[0] = 0x03; |
| 171 | + write_buf[1] = 0b10000011; |
| 172 | + rc = common_hal_busio_i2c_write(i2c, AW9523B_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 173 | + if (rc != 0) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + // 0x04 = 0b0001_1000 // Set TF_SW, ES_INT as input |
| 178 | + write_buf[0] = 0x04; |
| 179 | + write_buf[1] = 0b00011000; |
| 180 | + rc = common_hal_busio_i2c_write(i2c, AW9523B_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 181 | + if (rc != 0) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + // 0x05 = 0b0000_1100 // Set AW_INT, TOUCH_INT as input |
| 186 | + write_buf[0] = 0x05; |
| 187 | + write_buf[1] = 0b00001100; |
| 188 | + rc = common_hal_busio_i2c_write(i2c, AW9523B_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 189 | + if (rc != 0) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + |
| 193 | + // 0x11 = 0b0001_0000 // Set P0 outputs in push pull mode |
| 194 | + write_buf[0] = 0x11; |
| 195 | + write_buf[1] = 0b00010000; |
| 196 | + rc = common_hal_busio_i2c_write(i2c, AW9523B_I2C_ADDRESS, write_buf, sizeof(write_buf)); |
| 197 | + if (rc != 0) { |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + return true; |
| 202 | +} |
| 203 | + |
| 204 | +void board_init(void) { |
| 205 | + // Deselect SD card on shared SPI bus before display init (see #10536) |
| 206 | + config_pin_as_output_with_level(GPIO_NUM_4, true); |
| 207 | + |
| 208 | + busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0); |
| 209 | + |
| 210 | + if (!axp2101_init(internal_i2c)) { |
| 211 | + mp_printf(&mp_plat_print, "could not initialize AXP2101"); |
| 212 | + return; |
| 213 | + } |
| 214 | + |
| 215 | + if (!aw9523b_init(internal_i2c)) { |
| 216 | + mp_printf(&mp_plat_print, "could not initialize AW9523B"); |
| 217 | + return; |
| 218 | + } |
| 219 | + |
| 220 | + if (!display_init()) { |
| 221 | + mp_printf(&mp_plat_print, "could not initialize the display"); |
| 222 | + return; |
| 223 | + } |
| 224 | +} |
0 commit comments