|
| 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/microcontroller/Pin.h" |
| 30 | +#include "shared-module/displayio/__init__.h" |
| 31 | +#include "shared-module/displayio/mipi_constants.h" |
| 32 | + |
| 33 | +#define DELAY 0x80 |
| 34 | + |
| 35 | +/* All init code scraped from Adafruit_GC9A01A driver */ |
| 36 | +#define GC9A01A_SWRESET 0x01 ///< Software Reset (maybe, not documented) |
| 37 | +#define GC9A01A_INREGEN1 0xFE ///< Inter register enable 1 |
| 38 | +#define GC9A01A_INREGEN2 0xEF ///< Inter register enable 2 |
| 39 | +#define GC9A01A_MADCTL 0x36 ///< Memory Access Control |
| 40 | +#define GC9A01A_COLMOD 0x3A ///< Pixel Format Set |
| 41 | +#define GC9A01A1_POWER2 0xC3 ///< Power Control 2 |
| 42 | +#define GC9A01A1_POWER3 0xC4 ///< Power Control 3 |
| 43 | +#define GC9A01A1_POWER4 0xC9 ///< Power Control 4 |
| 44 | +#define GC9A01A_GAMMA1 0xF0 ///< Set gamma 1 |
| 45 | +#define GC9A01A_GAMMA2 0xF1 ///< Set gamma 2 |
| 46 | +#define GC9A01A_GAMMA3 0xF2 ///< Set gamma 3 |
| 47 | +#define GC9A01A_GAMMA4 0xF3 ///< Set gamma 4 |
| 48 | +#define GC9A01A_TEON 0x35 ///< Tearing Effect Line ON |
| 49 | +#define GC9A01A_INVON 0x21 ///< Display Inversion ON |
| 50 | +#define GC9A01A_SLPOUT 0x11 ///< Sleep Out |
| 51 | +#define GC9A01A_DISPON 0x29 ///< Display ON |
| 52 | +#define GC9A01A_FRAMERATE 0xE8 ///< Frame rate control |
| 53 | + |
| 54 | +#define MADCTL_MX 0x40 ///< Right to left |
| 55 | +#define MADCTL_BGR 0x08 ///< Blue-Green-Red pixel order |
| 56 | + |
| 57 | +uint8_t display_init_sequence[] = { |
| 58 | + GC9A01A_SWRESET, DELAY, 150, |
| 59 | + GC9A01A_INREGEN2, 0, |
| 60 | + 0xEB, 1, 0x14, |
| 61 | + GC9A01A_INREGEN1, 0, |
| 62 | + GC9A01A_INREGEN2, 0, |
| 63 | + 0xEB, 1, 0x14, |
| 64 | + 0x84, 1, 0x40, |
| 65 | + 0x85, 1, 0xFF, |
| 66 | + 0x86, 1, 0xFF, |
| 67 | + 0x87, 1, 0xFF, |
| 68 | + 0x88, 1, 0x0A, |
| 69 | + 0x89, 1, 0x21, |
| 70 | + 0x8A, 1, 0x00, |
| 71 | + 0x8B, 1, 0x80, |
| 72 | + 0x8C, 1, 0x01, |
| 73 | + 0x8D, 1, 0x01, |
| 74 | + 0x8E, 1, 0xFF, |
| 75 | + 0x8F, 1, 0xFF, |
| 76 | + 0xB6, 2, 0x00, 0x00, |
| 77 | + GC9A01A_MADCTL, 1, MADCTL_MX | MADCTL_BGR, |
| 78 | + GC9A01A_COLMOD, 1, 0x05, |
| 79 | + 0x90, 4, 0x08, 0x08, 0x08, 0x08, |
| 80 | + 0xBD, 1, 0x06, |
| 81 | + 0xBC, 1, 0x00, |
| 82 | + 0xFF, 3, 0x60, 0x01, 0x04, |
| 83 | + GC9A01A1_POWER2, 1, 0x13, |
| 84 | + GC9A01A1_POWER3, 1, 0x13, |
| 85 | + GC9A01A1_POWER4, 1, 0x22, |
| 86 | + 0xBE, 1, 0x11, |
| 87 | + 0xE1, 2, 0x10, 0x0E, |
| 88 | + 0xDF, 3, 0x21, 0x0c, 0x02, |
| 89 | + GC9A01A_GAMMA1, 6, 0x45, 0x09, 0x08, 0x08, 0x26, 0x2A, |
| 90 | + GC9A01A_GAMMA2, 6, 0x43, 0x70, 0x72, 0x36, 0x37, 0x6F, |
| 91 | + GC9A01A_GAMMA3, 6, 0x45, 0x09, 0x08, 0x08, 0x26, 0x2A, |
| 92 | + GC9A01A_GAMMA4, 6, 0x43, 0x70, 0x72, 0x36, 0x37, 0x6F, |
| 93 | + 0xED, 2, 0x1B, 0x0B, |
| 94 | + 0xAE, 1, 0x77, |
| 95 | + 0xCD, 1, 0x63, |
| 96 | + // Unsure what this line (from manufacturer's boilerplate code) is |
| 97 | + // meant to do, but users reported issues, seems to work OK without: |
| 98 | + // 0x70, 9, 0x07, 0x07, 0x04, 0x0E, 0x0F, 0x09, 0x07, 0x08, 0x03, // ? |
| 99 | + GC9A01A_FRAMERATE, 1, 0x34, |
| 100 | + 0x62, 12, 0x18, 0x0D, 0x71, 0xED, 0x70, 0x70, 0x18, 0x0F, 0x71, 0xEF, 0x70, 0x70, |
| 101 | + 0x63, 12, 0x18, 0x11, 0x71, 0xF1, 0x70, 0x70, 0x18, 0x13, 0x71, 0xF3, 0x70, 0x70, |
| 102 | + 0x64, 7, 0x28, 0x29, 0xF1, 0x01, 0xF1, 0x00, 0x07, |
| 103 | + 0x66, 10, 0x3C, 0x00, 0xCD, 0x67, 0x45, 0x45, 0x10, 0x00, 0x00, 0x00, |
| 104 | + 0x67, 10, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x01, 0x54, 0x10, 0x32, 0x98, |
| 105 | + 0x74, 7, 0x10, 0x85, 0x80, 0x00, 0x00, 0x4E, 0x00, |
| 106 | + 0x98, 2, 0x3e, 0x07, |
| 107 | + GC9A01A_TEON, 0, |
| 108 | + GC9A01A_INVON, 0, |
| 109 | + GC9A01A_SLPOUT, DELAY, 10, // Exit sleep |
| 110 | + GC9A01A_DISPON, DELAY, 150, // Display on |
| 111 | +}; |
| 112 | + |
| 113 | +static void display_init(void) { |
| 114 | + fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; |
| 115 | + busio_spi_obj_t *spi = &bus->inline_bus; |
| 116 | + common_hal_busio_spi_construct( |
| 117 | + spi, |
| 118 | + &pin_GPIO10, // CLK |
| 119 | + &pin_GPIO11, // MOSI |
| 120 | + NULL, // MISO not connected |
| 121 | + false // Not half-duplex |
| 122 | + ); |
| 123 | + common_hal_busio_spi_never_reset(spi); |
| 124 | + bus->base.type = &fourwire_fourwire_type; |
| 125 | + common_hal_fourwire_fourwire_construct( |
| 126 | + bus, |
| 127 | + spi, |
| 128 | + &pin_GPIO8, // DC |
| 129 | + &pin_GPIO9, // CS |
| 130 | + &pin_GPIO12, // RST |
| 131 | + 80000000, // baudrate |
| 132 | + 0, // polarity |
| 133 | + 0 // phase |
| 134 | + ); |
| 135 | + busdisplay_busdisplay_obj_t *display = &allocate_display()->display; |
| 136 | + display->base.type = &busdisplay_busdisplay_type; |
| 137 | + common_hal_busdisplay_busdisplay_construct( |
| 138 | + display, |
| 139 | + bus, |
| 140 | + 240, // width (after rotation) |
| 141 | + 240, // height (after rotation) |
| 142 | + 0, // column start |
| 143 | + 0, // row start |
| 144 | + 0, // rotation |
| 145 | + 16, // color depth |
| 146 | + false, // grayscale |
| 147 | + false, // pixels in a byte share a row. Only valid for depths < 8 |
| 148 | + 1, // bytes per cell. Only valid for depths < 8 |
| 149 | + false, // reverse_pixels_in_byte. Only valid for depths < 8 |
| 150 | + true, // reverse_pixels_in_word |
| 151 | + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command |
| 152 | + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command |
| 153 | + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command |
| 154 | + display_init_sequence, |
| 155 | + sizeof(display_init_sequence), |
| 156 | + &pin_GPIO40, // backlight pin |
| 157 | + NO_BRIGHTNESS_COMMAND, |
| 158 | + 1.0f, // brightness |
| 159 | + false, // single_byte_bounds |
| 160 | + false, // data_as_commands |
| 161 | + true, // auto_refresh |
| 162 | + 60, // native_frames_per_second |
| 163 | + true, // backlight_on_high |
| 164 | + false, // SH1107_addressing |
| 165 | + 5000 // backlight pwm frequency |
| 166 | + ); |
| 167 | +} |
| 168 | + |
| 169 | +void board_init(void) { |
| 170 | + // Display |
| 171 | + display_init(); |
| 172 | +} |
| 173 | +// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. |
0 commit comments