|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2021 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/microcontroller/Pin.h" |
| 10 | +#include "shared-bindings/paralleldisplaybus/ParallelBus.h" |
| 11 | +#include "shared-module/displayio/__init__.h" |
| 12 | +#include "shared-module/displayio/mipi_constants.h" |
| 13 | +#include "hardware/gpio.h" |
| 14 | + |
| 15 | +// Display pins from Pimoroni Explorer parallel bus: {cs=27, dc=28, wr=30, rd=31, d0=32, bl=26} |
| 16 | +#define LCD_BACKLIGHT_PIN 26 |
| 17 | +#define LCD_CS_PIN 27 |
| 18 | +#define LCD_DC_PIN 28 |
| 19 | +#define LCD_WR_PIN 30 |
| 20 | +#define LCD_RD_PIN 31 |
| 21 | +#define LCD_D0_PIN 32 // Data pins are GPIO32-39 (8 consecutive pins) |
| 22 | + |
| 23 | +#define DELAY 0x80 |
| 24 | + |
| 25 | +// ST7789V display init sequence for 320x240 parallel bus |
| 26 | +// Based on Pimoroni's pimoroni-pico ST7789 driver configuration |
| 27 | +uint8_t display_init_sequence[] = { |
| 28 | + // Software reset |
| 29 | + 0x01, 0 | DELAY, 150, |
| 30 | + // Sleep out |
| 31 | + 0x11, 0 | DELAY, 255, |
| 32 | + // Tearing effect line on (frame sync) |
| 33 | + 0x35, 1, 0x00, |
| 34 | + // COLMOD: 16-bit color (5-6-5 RGB) |
| 35 | + 0x3A, 1, 0x55, |
| 36 | + // Porch control (PORCTRL) |
| 37 | + 0xB2, 5, 0x0C, 0x0C, 0x00, 0x33, 0x33, |
| 38 | + // Gate control (GCTRL) - VGH=13.26V, VGL=-10.43V |
| 39 | + 0xB7, 1, 0x35, |
| 40 | + // VCOM setting (VCOMS) |
| 41 | + 0xBB, 1, 0x1F, |
| 42 | + // LCM control (LCMCTRL) |
| 43 | + 0xC0, 1, 0x2C, |
| 44 | + // VDV and VRH command enable (VDVVRHEN) |
| 45 | + 0xC2, 1, 0x01, |
| 46 | + // VRH set (VRHS) |
| 47 | + 0xC3, 1, 0x12, |
| 48 | + // VDV set (VDVS) |
| 49 | + 0xC4, 1, 0x20, |
| 50 | + // Frame rate control (FRCTRL2) |
| 51 | + 0xC6, 1, 0x0F, |
| 52 | + // Power control 1 (PWCTRL1) |
| 53 | + 0xD0, 2, 0xA4, 0xA1, |
| 54 | + // RAM control (RAMCTRL) - for proper endianness |
| 55 | + 0xB0, 2, 0x00, 0xC0, |
| 56 | + // Positive gamma correction |
| 57 | + 0xE0, 14, 0xD0, 0x08, 0x11, 0x08, 0x0C, 0x15, 0x39, 0x33, 0x50, 0x36, 0x13, 0x14, 0x29, 0x2D, |
| 58 | + // Negative gamma correction |
| 59 | + 0xE1, 14, 0xD0, 0x08, 0x10, 0x08, 0x06, 0x06, 0x39, 0x44, 0x51, 0x0B, 0x16, 0x14, 0x2F, 0x31, |
| 60 | + // Inversion on |
| 61 | + 0x21, 0, |
| 62 | + // Normal display mode on |
| 63 | + 0x13, 0 | DELAY, 10, |
| 64 | + // MADCTL: MX=0, MY=1, MV=1, ML=1 (COL_ORDER | SWAP_XY | SCAN_ORDER) = 0x70 |
| 65 | + // This configures the 320x240 display in landscape orientation |
| 66 | + 0x36, 1, 0x70, |
| 67 | + // Display on |
| 68 | + 0x29, 0 | DELAY, 100, |
| 69 | +}; |
| 70 | + |
| 71 | +static void display_init(void) { |
| 72 | + paralleldisplaybus_parallelbus_obj_t *bus = &allocate_display_bus()->parallel_bus; |
| 73 | + bus->base.type = ¶lleldisplaybus_parallelbus_type; |
| 74 | + |
| 75 | + common_hal_paralleldisplaybus_parallelbus_construct(bus, |
| 76 | + &pin_GPIO32, // Data0 (D0) - data pins are sequential GPIO32-39 |
| 77 | + &pin_GPIO28, // Command/Data (DC) |
| 78 | + &pin_GPIO27, // Chip select (CS) |
| 79 | + &pin_GPIO30, // Write (WR) |
| 80 | + &pin_GPIO31, // Read (RD) |
| 81 | + NULL, // Reset (directly connected to board reset) |
| 82 | + 15000000); // Frequency - ST7789 min clock cycle ~66ns = ~15MHz |
| 83 | + |
| 84 | + busdisplay_busdisplay_obj_t *display = &allocate_display()->display; |
| 85 | + display->base.type = &busdisplay_busdisplay_type; |
| 86 | + |
| 87 | + common_hal_busdisplay_busdisplay_construct(display, |
| 88 | + bus, |
| 89 | + 320, // Width |
| 90 | + 240, // Height |
| 91 | + 0, // column start |
| 92 | + 0, // row start |
| 93 | + 0, // rotation |
| 94 | + 16, // Color depth |
| 95 | + false, // grayscale |
| 96 | + false, // pixels_in_byte_share_row |
| 97 | + 1, // bytes per cell |
| 98 | + false, // reverse_pixels_in_byte |
| 99 | + true, // reverse_pixels_in_word |
| 100 | + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command |
| 101 | + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command |
| 102 | + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command |
| 103 | + display_init_sequence, |
| 104 | + sizeof(display_init_sequence), |
| 105 | + &pin_GPIO26, // Backlight pin (BL) |
| 106 | + NO_BRIGHTNESS_COMMAND, |
| 107 | + 1.0f, // brightness |
| 108 | + false, // single_byte_bounds |
| 109 | + false, // data_as_commands |
| 110 | + true, // auto_refresh |
| 111 | + 60, // native_frames_per_second |
| 112 | + true, // backlight_on_high |
| 113 | + false, // SH1107_addressing |
| 114 | + 50000 // backlight pwm frequency |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +void board_init(void) { |
| 119 | + // Ensure backlight is on before display init |
| 120 | + board_reset_pin_number(LCD_BACKLIGHT_PIN); |
| 121 | + display_init(); |
| 122 | +} |
| 123 | + |
| 124 | +// Prevent the backlight pin from being reset, keeping display visible across soft resets |
| 125 | +bool board_reset_pin_number(uint8_t pin_number) { |
| 126 | + if (pin_number == LCD_BACKLIGHT_PIN) { |
| 127 | + // Keep backlight on - set high output without glitching |
| 128 | + gpio_put(pin_number, 1); |
| 129 | + gpio_set_dir(pin_number, GPIO_OUT); |
| 130 | + gpio_set_function(pin_number, GPIO_FUNC_SIO); |
| 131 | + return true; |
| 132 | + } |
| 133 | + return false; |
| 134 | +} |
| 135 | + |
| 136 | +void reset_board(void) { |
| 137 | + // Keep backlight on during reset |
| 138 | + board_reset_pin_number(LCD_BACKLIGHT_PIN); |
| 139 | +} |
| 140 | + |
| 141 | +void board_deinit(void) { |
| 142 | + // Backlight will be handled by board_reset_pin_number |
| 143 | +} |
| 144 | + |
| 145 | +// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. |
0 commit comments