|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2025 Benjamin Shockley |
| 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-module/displayio/__init__.h" |
| 11 | +#include "shared-module/displayio/mipi_constants.h" |
| 12 | + |
| 13 | +#define DELAY 0x80 |
| 14 | + |
| 15 | +// Driver is ST7789V3 |
| 16 | +// Display Panel is LBS147TC-IF15 |
| 17 | +// 172 X 320 Pixels RGB 18-bit |
| 18 | + |
| 19 | +uint8_t display_init_sequence[] = { |
| 20 | + // (0x01) SW Reset |
| 21 | + // The display module performs a software reset, registers are written with their SW reset default values. No parameters. |
| 22 | + // ST7789V3 requirement is to wait 120msec before sending sleep out command. |
| 23 | + 0x01, 0 | DELAY, 120, |
| 24 | + // (0x11) Sleep Out |
| 25 | + // This command turns off sleep mode. |
| 26 | + // ST7789V3 requirement is to wait 120msec before sending sleep in command and wait 5msec before sending any new commands. No parameters. |
| 27 | + 0x11, 0 | DELAY, 120, |
| 28 | + // (0x13) Normal Display Mode On |
| 29 | + // This command turns the display to normal mode. No parameters. |
| 30 | + 0x13, 0, |
| 31 | + // Display and Color Format Settings |
| 32 | + // |
| 33 | + // (0x36) Memory Data Access Control |
| 34 | + // LBS147TC-IF15 as Mounted on the board is in Horizontal Mode |
| 35 | + // This command defines read/ write scanning direction of frame memory. 1 parameter. |
| 36 | + 0x36, 1, 0x00, |
| 37 | + // (0x3A) Interface Pixel Format |
| 38 | + // This command is used to define the format of RGB picture data. 1 parameter. Value of 0x05 is setting the display to 16-bit (5,6,5). Could do 18-bit |
| 39 | + // but may not really see the benefit and costs much more memory. |
| 40 | + 0x3A, 1 | DELAY, 0x05, 10, |
| 41 | + // (0xB2) Porch Setting |
| 42 | + // These 5 parameters front and backporch buffer sizes as well as enabling separate front and back control. Default value provided. |
| 43 | + 0xB2, 5, 0x0C, 0x0C, 0x00, 0x33, 0x33, |
| 44 | + // (0xB7) Gate Voltage Control |
| 45 | + // VGHS and VGLS settings. 1 paramter contains both. Per LBS147TC-IF15 spec; 12.2 < VGH < 14.97 (13.58) and12.5 < VGL <7.16 9.83 |
| 46 | + 0xB7, 1, 0x35, |
| 47 | + // (0xBB) VCOMS Setting |
| 48 | + // VCOMS is used for feed through voltage compensation. Default value provided. |
| 49 | + 0xBB, 1, 0x20, |
| 50 | + // (0xC0) LCM Control |
| 51 | + // Various bits to control modes. 1 paramter. Value taken from Waveshare's own Arduino example LCD init code. |
| 52 | + 0xC0, 1, 0x2C, |
| 53 | + // (0xC2) VDV and VRH Command Enable |
| 54 | + // VDV and VRH command write enable. 2 paramters. Default values provided. |
| 55 | + 0xC2, 2, 0x01, 0xFF, |
| 56 | + // (0xC3) VRH Set |
| 57 | + // 1 parameter. Value taken from Waveshare's own Arduino example LCD init code. |
| 58 | + 0xC3, 1, 0x13, |
| 59 | + // (0xC4) VDV Set |
| 60 | + // 1 parameter. Value taken from Waveshare's own Arduino example LCD init code. |
| 61 | + 0xC4, 1, 0x20, |
| 62 | + // (0xC6) Frame Rate Control in Normal Mode |
| 63 | + // Frame rate value betwee 119 and 39 Hz. Default value is 60 Hz - uses values from 0xB2 as well. See manual. Default value provided. |
| 64 | + 0xC6, 1, 0x0F, |
| 65 | + // (0xD0) Power Control 1 |
| 66 | + // Sets AVDD, AVCL, and VDS voltages. 2 parameters. Values taken from Waveshare's own Arduino example LCD init code. |
| 67 | + 0xD0, 2, 0xA4, 0xA1, |
| 68 | + // ST7789V gamma setting |
| 69 | + // (0xE0) Positive Voltage Gamma Control |
| 70 | + // 14 parameters. Values taken from Waveshare's own Arduino example LCD init code. |
| 71 | + 0xE0, 14, 0xF0, 0x00, 0x04, 0x04, 0x04, 0x05, 0x29, 0x33, 0x3E, 0x38, 0x12, 0x12, 0x28, 0x30, |
| 72 | + // (0xE1) Negative Voltage Gamma Control |
| 73 | + // 14 parameters. Values taken from Waveshare's own Arduino example LCD init code. |
| 74 | + 0xE1, 14, 0xF0, 0x07, 0x0A, 0x0D, 0x0B, 0x07, 0x28, 0x33, 0x3E, 0x36, 0x14, 0x14, 0x29, 0x32, |
| 75 | + // (0x21) Display Inversion On |
| 76 | + // This command is used to recover from display inversion mode. No parameters. |
| 77 | + 0x21, 0, |
| 78 | + // (0x29) Display On |
| 79 | + // This command is used to recover from DISPLAY OFF mode. No parameters. |
| 80 | + 0x29, 0 | DELAY, 255, |
| 81 | +}; |
| 82 | + |
| 83 | +static void display_init(void) { |
| 84 | + fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; |
| 85 | + busio_spi_obj_t *spi = &bus->inline_bus; |
| 86 | + |
| 87 | + common_hal_busio_spi_construct( |
| 88 | + spi, |
| 89 | + &pin_GPIO7, // CLK |
| 90 | + &pin_GPIO6, // MOSI |
| 91 | + NULL, // MISO not connected |
| 92 | + false); // Not half-duplex |
| 93 | + |
| 94 | + common_hal_busio_spi_never_reset(spi); |
| 95 | + |
| 96 | + bus->base.type = &fourwire_fourwire_type; |
| 97 | + |
| 98 | + common_hal_fourwire_fourwire_construct( |
| 99 | + bus, |
| 100 | + spi, |
| 101 | + &pin_GPIO15, // DC |
| 102 | + &pin_GPIO14, // CS |
| 103 | + &pin_GPIO21, // RST |
| 104 | + 80000000, // baudrate |
| 105 | + 0, // polarity |
| 106 | + 0 // phase |
| 107 | + ); |
| 108 | + |
| 109 | + busdisplay_busdisplay_obj_t *display = &allocate_display()->display; |
| 110 | + display->base.type = &busdisplay_busdisplay_type; |
| 111 | + |
| 112 | + common_hal_busdisplay_busdisplay_construct( |
| 113 | + display, |
| 114 | + bus, |
| 115 | + 172, // width (after rotation) |
| 116 | + 320, // height (after rotation) |
| 117 | + 34, // column start |
| 118 | + 0, // row start |
| 119 | + 0, // rotation |
| 120 | + 16, // color depth |
| 121 | + false, // grayscale |
| 122 | + false, // pixels in a byte share a row. Only valid for depths < 8 |
| 123 | + 1, // bytes per cell. Only valid for depths < 8 |
| 124 | + false, // reverse_pixels_in_byte. Only valid for depths < 8 |
| 125 | + true, // reverse_pixels_in_word |
| 126 | + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command |
| 127 | + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command |
| 128 | + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command |
| 129 | + display_init_sequence, |
| 130 | + sizeof(display_init_sequence), |
| 131 | + &pin_GPIO22, // backlight pin |
| 132 | + NO_BRIGHTNESS_COMMAND, |
| 133 | + 1.0f, // brightness |
| 134 | + false, // single_byte_bounds |
| 135 | + false, // data_as_commands |
| 136 | + true, // auto_refresh |
| 137 | + 60, // native_frames_per_second |
| 138 | + true, // backlight_on_high |
| 139 | + false, // SH1107_addressing |
| 140 | + 50000 // backlight pwm frequency |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +void board_init(void) { |
| 145 | + // Display |
| 146 | + display_init(); |
| 147 | +} |
| 148 | + |
| 149 | +// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. |
0 commit comments