|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2023 CDarius |
| 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 | +#include "supervisor/board.h" |
| 27 | +#include "mpconfigboard.h" |
| 28 | +#include "shared-bindings/microcontroller/Pin.h" |
| 29 | +#include "shared-module/displayio/__init__.h" |
| 30 | +#include "shared-module/displayio/mipi_constants.h" |
| 31 | + |
| 32 | +// display init sequence according to adafruit_st7735r.py library |
| 33 | +uint8_t display_init_sequence[] = { |
| 34 | + 0x01, 0x80, 0x96, // SWRESET and Delay 150ms |
| 35 | + 0x11, 0x80, 0xff, // SLPOUT and Delay |
| 36 | + 0xb1, 0x03, 0x01, 0x2C, 0x2D, // _FRMCTR1 |
| 37 | + 0xb2, 0x03, 0x01, 0x2C, 0x2D, // _FRMCTR2 |
| 38 | + 0xb3, 0x06, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D, // _FRMCTR3 |
| 39 | + 0xb4, 0x01, 0x07, // _INVCTR line inversion |
| 40 | + 0xc0, 0x03, 0xa2, 0x02, 0x84, // _PWCTR1 GVDD = 4.7V, 1.0uA |
| 41 | + 0xc1, 0x01, 0xc5, // _PWCTR2 VGH=14.7V, VGL=-7.35V |
| 42 | + 0xc2, 0x02, 0x0a, 0x00, // _PWCTR3 Opamp current small, Boost frequency |
| 43 | + 0xc3, 0x02, 0x8a, 0x2a, |
| 44 | + 0xc4, 0x02, 0x8a, 0xee, |
| 45 | + 0xc5, 0x01, 0x0e, // _VMCTR1 VCOMH = 4V, VOML = -1.1V |
| 46 | + 0x36, 0x01, 0xc8, // MADCTL Rotate display |
| 47 | + 0x3a, 0x01, 0x05, // COLMOD - 16bit color |
| 48 | + 0xe0, 0x10, 0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10, // _GMCTRP1 Gamma |
| 49 | + 0xe1, 0x10, 0x03, 0x1d, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D, 0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x10, // _GMCTRN1 |
| 50 | + 0x13, 0x80, 0x0a, // _NORON |
| 51 | + 0x29, 0x80, 0x64 // _DISPON |
| 52 | +}; |
| 53 | + |
| 54 | +static bool display_init(void) { |
| 55 | + fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; |
| 56 | + busio_spi_obj_t *spi = &bus->inline_bus; |
| 57 | + common_hal_busio_spi_construct(spi, &pin_GPIO3, &pin_GPIO4, NULL, false); |
| 58 | + common_hal_busio_spi_never_reset(spi); |
| 59 | + |
| 60 | + bus->base.type = &fourwire_fourwire_type; |
| 61 | + |
| 62 | + common_hal_fourwire_fourwire_construct( |
| 63 | + bus, |
| 64 | + spi, |
| 65 | + &pin_GPIO0, // DC |
| 66 | + &pin_GPIO2, // CS |
| 67 | + &pin_GPIO5, // RST |
| 68 | + 10000000, // baudrate |
| 69 | + 0, // polarity |
| 70 | + 0 // phase |
| 71 | + ); |
| 72 | + |
| 73 | + busdisplay_busdisplay_obj_t *display = &allocate_display()->display; |
| 74 | + display->base.type = &busdisplay_busdisplay_type; |
| 75 | + |
| 76 | + common_hal_busdisplay_busdisplay_construct( |
| 77 | + display, |
| 78 | + bus, |
| 79 | + 128, // width (after rotation) |
| 80 | + 128, // height (after rotation) |
| 81 | + 2, // column start |
| 82 | + 1, // row start |
| 83 | + 0, // rotation |
| 84 | + 16, // color depth |
| 85 | + false, // grayscale |
| 86 | + false, // pixels in a byte share a row. Only valid for depths < 8 |
| 87 | + 1, // bytes per cell. Only valid for depths < 8 |
| 88 | + false, // reverse_pixels_in_byte. Only valid for depths < 8 |
| 89 | + true, // reverse_pixels_in_word |
| 90 | + MIPI_COMMAND_SET_COLUMN_ADDRESS, // set column command |
| 91 | + MIPI_COMMAND_SET_PAGE_ADDRESS, // set row command |
| 92 | + MIPI_COMMAND_WRITE_MEMORY_START, // write memory command |
| 93 | + display_init_sequence, |
| 94 | + sizeof(display_init_sequence), |
| 95 | + NULL, // backlight pin |
| 96 | + NO_BRIGHTNESS_COMMAND, |
| 97 | + 1.0f, // brightness |
| 98 | + false, // single_byte_bounds |
| 99 | + false, // data_as_commands |
| 100 | + true, // auto_refresh |
| 101 | + 80, // native_frames_per_second |
| 102 | + false, // backlight_on_high |
| 103 | + false, // SH1107_addressing |
| 104 | + 50000 // backlight pwm frequency |
| 105 | + ); |
| 106 | + |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +void board_init(void) { |
| 111 | + display_init(); |
| 112 | +} |
0 commit comments