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