|
| 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 "mpconfigboard.h" |
| 8 | +#include "supervisor/board.h" |
| 9 | +#include "supervisor/shared/serial.h" |
| 10 | +#include "shared-bindings/busio/SPI.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 "py/runtime.h" |
| 17 | +#include "py/ringbuf.h" |
| 18 | +#include "shared/runtime/interrupt_char.h" |
| 19 | + |
| 20 | + |
| 21 | +uint8_t display_init_sequence[] = { |
| 22 | + 0x01, 0x80, 0x96, // SWRESET and Delay 150ms |
| 23 | + 0x11, 0x80, 0xff, // SLPOUT and Delay |
| 24 | + 0xb1, 0x03, 0x01, 0x2C, 0x2D, // _FRMCTR1 |
| 25 | + 0xb2, 0x03, 0x01, 0x2C, 0x2D, // _FRMCTR2 |
| 26 | + 0xb3, 0x06, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D, // _FRMCTR3 |
| 27 | + 0xb4, 0x01, 0x07, // _INVCTR line inversion |
| 28 | + 0xc0, 0x03, 0xa2, 0x02, 0x84, // _PWCTR1 GVDD = 4.7V, 1.0uA |
| 29 | + 0xc1, 0x01, 0xc5, // _PWCTR2 VGH=14.7V, VGL=-7.35V |
| 30 | + 0xc2, 0x02, 0x0a, 0x00, // _PWCTR3 Opamp current small, Boost frequency |
| 31 | + 0xc3, 0x02, 0x8a, 0x2a, |
| 32 | + 0xc4, 0x02, 0x8a, 0xee, |
| 33 | + 0xc5, 0x01, 0x0e, // _VMCTR1 VCOMH = 4V, VOML = -1.1V |
| 34 | + 0x36, 0x01, 0xc8, // MADCTL Rotate display |
| 35 | + 0x21, 0x00, // _INVON |
| 36 | + 0x3a, 0x01, 0x05, // COLMOD - 16bit color |
| 37 | + 0xe0, 0x10, 0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10, // _GMCTRP1 Gamma |
| 38 | + 0xe1, 0x10, 0x03, 0x1d, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D, 0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x10, // _GMCTRN1 |
| 39 | + 0x13, 0x80, 0x0a, // _NORON |
| 40 | + 0x29, 0x80, 0x64 // _DISPON |
| 41 | +}; |
| 42 | + |
| 43 | + |
| 44 | +// Overrides the weakly linked function from supervisor/shared/board.c |
| 45 | +void board_init(void) { |
| 46 | + busio_spi_obj_t *spi = common_hal_board_create_spi(0); |
| 47 | + fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; |
| 48 | + bus->base.type = &fourwire_fourwire_type; |
| 49 | + |
| 50 | + // see here for inspiration: https://github.com/m5stack/M5GFX/blob/33d7d3135e816a86a008fae8ab3757938cee95d2/src/M5GFX.cpp#L1350 |
| 51 | + common_hal_fourwire_fourwire_construct( |
| 52 | + bus, |
| 53 | + spi, |
| 54 | + &pin_GPIO4, // DC |
| 55 | + &pin_GPIO7, // CS |
| 56 | + &pin_GPIO8, // RST |
| 57 | + 40000000, // baudrate |
| 58 | + 0, // polarity |
| 59 | + 0 // phase |
| 60 | + ); |
| 61 | + busdisplay_busdisplay_obj_t *display = &allocate_display()->display; |
| 62 | + display->base.type = &busdisplay_busdisplay_type; |
| 63 | + |
| 64 | + common_hal_busdisplay_busdisplay_construct( |
| 65 | + display, |
| 66 | + bus, |
| 67 | + 240, // width (after rotation) |
| 68 | + 135, // height (after rotation) |
| 69 | + 40, // column start |
| 70 | + 52, // row start |
| 71 | + 0, // rotation |
| 72 | + 16, // color depth |
| 73 | + false, // grayscale |
| 74 | + false, // pixels in a byte share a row. Only valid for depths < 8 |
| 75 | + 1, // bytes per cell. Only valid for depths < 8 |
| 76 | + false, // reverse_pixels_in_byte. Only valid for depths < 8 |
| 77 | + true, // reverse_pixels_in_word |
| 78 | + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command |
| 79 | + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command |
| 80 | + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command |
| 81 | + display_init_sequence, |
| 82 | + sizeof(display_init_sequence), |
| 83 | + &pin_GPIO9, // backlight pin |
| 84 | + NO_BRIGHTNESS_COMMAND, |
| 85 | + 1.0f, // brightness |
| 86 | + false, // single_byte_bounds |
| 87 | + false, // data_as_commands |
| 88 | + true, // auto_refresh |
| 89 | + 80, // native_frames_per_second |
| 90 | + true, // backlight_on_high |
| 91 | + false, // SH1107_addressing |
| 92 | + 50000 // backlight pwm frequency |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +bool espressif_board_reset_pin_number(gpio_num_t pin_number) { |
| 98 | + // Hold pin must be set high to avoid a power off when battery powered |
| 99 | + if (pin_number == 46) { |
| 100 | + // Turn on hold output |
| 101 | + config_pin_as_output_with_level(pin_number, true); |
| 102 | + return true; |
| 103 | + } |
| 104 | + return false; |
| 105 | +} |
| 106 | + |
| 107 | +// TODO: Should we turn off the display when asleep, in board_deinit() ? |
0 commit comments