|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2023 n0xa, 2025 bootc |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "supervisor/board.h" |
| 8 | +#include "mpconfigboard.h" |
| 9 | +#include "shared-bindings/busio/SPI.h" |
| 10 | +#include "shared-bindings/busio/I2C.h" |
| 11 | +#include "shared-bindings/fourwire/FourWire.h" |
| 12 | +#include "shared-module/displayio/__init__.h" |
| 13 | +#include "shared-module/displayio/mipi_constants.h" |
| 14 | +#include "shared-bindings/board/__init__.h" |
| 15 | +#include "shared-bindings/microcontroller/Pin.h" |
| 16 | +#include "driver/gpio.h" |
| 17 | +#include "common-hal/microcontroller/Pin.h" |
| 18 | + |
| 19 | +// display init sequence according to adafruit_st7735r.py library |
| 20 | +uint8_t display_init_sequence[] = { |
| 21 | + 0x01, 0x80, 0x96, // SWRESET and Delay 150ms |
| 22 | + 0x11, 0x80, 0xff, // SLPOUT and Delay |
| 23 | + 0xb1, 0x03, 0x01, 0x2C, 0x2D, // _FRMCTR1 |
| 24 | + 0xb2, 0x03, 0x01, 0x2C, 0x2D, // _FRMCTR2 |
| 25 | + 0xb3, 0x06, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D, // _FRMCTR3 |
| 26 | + 0xb4, 0x01, 0x07, // _INVCTR line inversion |
| 27 | + 0xc0, 0x03, 0xa2, 0x02, 0x84, // _PWCTR1 GVDD = 4.7V, 1.0uA |
| 28 | + 0xc1, 0x01, 0xc5, // _PWCTR2 VGH=14.7V, VGL=-7.35V |
| 29 | + 0xc2, 0x02, 0x0a, 0x00, // _PWCTR3 Opamp current small, Boost frequency |
| 30 | + 0xc3, 0x02, 0x8a, 0x2a, |
| 31 | + 0xc4, 0x02, 0x8a, 0xee, |
| 32 | + 0xc5, 0x01, 0x0e, // _VMCTR1 VCOMH = 4V, VOML = -1.1V |
| 33 | + 0x36, 0x01, 0xc8, // MADCTL Rotate display |
| 34 | + 0x21, 0x00, // _INVON |
| 35 | + 0x3a, 0x01, 0x05, // COLMOD - 16bit color |
| 36 | + 0xe0, 0x10, 0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29, 0x25, 0x2B, 0x39, 0x00, 0x01, 0x03, 0x10, // _GMCTRP1 Gamma |
| 37 | + 0xe1, 0x10, 0x03, 0x1d, 0x07, 0x06, 0x2E, 0x2C, 0x29, 0x2D, 0x2E, 0x2E, 0x37, 0x3F, 0x00, 0x00, 0x02, 0x10, // _GMCTRN1 |
| 38 | + 0x13, 0x80, 0x0a, // _NORON |
| 39 | + 0x29, 0x80, 0x64 // _DISPON |
| 40 | +}; |
| 41 | + |
| 42 | +static void display_init(void) { |
| 43 | + fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; |
| 44 | + busio_spi_obj_t *spi = &bus->inline_bus; |
| 45 | + common_hal_busio_spi_construct(spi, &pin_GPIO13, &pin_GPIO15, NULL, false); |
| 46 | + common_hal_busio_spi_never_reset(spi); |
| 47 | + |
| 48 | + bus->base.type = &fourwire_fourwire_type; |
| 49 | + |
| 50 | + common_hal_fourwire_fourwire_construct( |
| 51 | + bus, |
| 52 | + spi, |
| 53 | + &pin_GPIO14, // DC |
| 54 | + &pin_GPIO5, // CS |
| 55 | + &pin_GPIO12, // RST |
| 56 | + 10000000, // baudrate |
| 57 | + 0, // polarity |
| 58 | + 0 // phase |
| 59 | + ); |
| 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 | + 135, // width (after rotation) |
| 68 | + 240, // height (after rotation) |
| 69 | + 40, // column start |
| 70 | + 52, // row start |
| 71 | + 1, // 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_GPIO27, // 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 | + 60, // native_frames_per_second |
| 90 | + true, // backlight_on_high |
| 91 | + false, // SH1107_addressing |
| 92 | + 50000 // backlight pwm frequency |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +void board_init(void) { |
| 97 | + display_init(); |
| 98 | +} |
| 99 | + |
| 100 | +bool board_requests_safe_mode(void) { |
| 101 | + // Enable HOLD early on |
| 102 | + config_pin_as_output_with_level(GPIO_NUM_4, true); |
| 103 | + |
| 104 | + // Change the buttons to inputs |
| 105 | + gpio_set_direction(GPIO_NUM_35, GPIO_MODE_INPUT); |
| 106 | + gpio_set_pull_mode(GPIO_NUM_35, GPIO_FLOATING); |
| 107 | + gpio_set_direction(GPIO_NUM_37, GPIO_MODE_INPUT); |
| 108 | + gpio_set_pull_mode(GPIO_NUM_37, GPIO_FLOATING); |
| 109 | + gpio_set_direction(GPIO_NUM_39, GPIO_MODE_INPUT); |
| 110 | + gpio_set_pull_mode(GPIO_NUM_39, GPIO_FLOATING); |
| 111 | + |
| 112 | + // Let the pins settle |
| 113 | + mp_hal_delay_ms(1); |
| 114 | + |
| 115 | + // Safe mode if BTN_A is held at boot (logic low) |
| 116 | + return gpio_get_level(GPIO_NUM_37) == 0; // BTN_A |
| 117 | +} |
| 118 | + |
| 119 | +bool espressif_board_reset_pin_number(gpio_num_t pin_number) { |
| 120 | + switch (pin_number) { |
| 121 | + case GPIO_NUM_4: // HOLD |
| 122 | + // HOLD(G4) pin must be set high to avoid a power off when battery powered |
| 123 | + config_pin_as_output_with_level(pin_number, true); |
| 124 | + return true; |
| 125 | + |
| 126 | + case GPIO_NUM_35: // BTN_C/PWR |
| 127 | + case GPIO_NUM_37: // BTN_A |
| 128 | + case GPIO_NUM_39: // BTN_B |
| 129 | + gpio_set_direction(pin_number, GPIO_MODE_INPUT); |
| 130 | + gpio_set_pull_mode(pin_number, GPIO_FLOATING); |
| 131 | + return true; |
| 132 | + |
| 133 | + default: |
| 134 | + return false; |
| 135 | + } |
| 136 | + return false; |
| 137 | +} |
0 commit comments