|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries |
| 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 | + |
| 27 | +#include "supervisor/board.h" |
| 28 | +#include "mpconfigboard.h" |
| 29 | +#include "shared-bindings/busio/SPI.h" |
| 30 | +#include "shared-bindings/fourwire/FourWire.h" |
| 31 | +#include "shared-bindings/microcontroller/Pin.h" |
| 32 | +#include "shared-module/displayio/__init__.h" |
| 33 | +#include "supervisor/shared/board.h" |
| 34 | +#include "components/driver/gpio/include/driver/gpio.h" |
| 35 | +#include "components/hal/include/hal/gpio_hal.h" |
| 36 | + |
| 37 | +#include "common-hal/microcontroller/Pin.h" |
| 38 | + |
| 39 | +#include "components/log/include/esp_log.h" |
| 40 | +#include "supervisor/filesystem.h" |
| 41 | + |
| 42 | +#define DELAY 0x80 |
| 43 | + |
| 44 | +#define EPD_WIDTH 200 |
| 45 | +#define EPD_HEIGHT 200 |
| 46 | +#define EPD_X_OFFSET 0 |
| 47 | +#define EPD_Y_OFFSET 0 |
| 48 | +#define EPD_FIRST_COLUMN EPD_X_OFFSET |
| 49 | +#define EPD_LAST_COLUMN (EPD_X_OFFSET + EPD_WIDTH - 1) |
| 50 | +#define EPD_FIRST_ROW EPD_Y_OFFSET |
| 51 | +#define EPD_LAST_ROW (EPD_Y_OFFSET + EPD_HEIGHT - 1) |
| 52 | +#define EPD_X_WINDOW_START (EPD_FIRST_COLUMN / 8) |
| 53 | +#define EPD_X_WINDOW_END (EPD_LAST_COLUMN / 8) |
| 54 | +#define EPD_Y_WINDOW_START EPD_FIRST_ROW |
| 55 | +#define EPD_Y_WINDOW_END EPD_LAST_ROW |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +#define EPD_EVEN_FIRST 0x0 |
| 60 | +#define EPD_ODD_FIRST 0x4 |
| 61 | +#define EPD_NO_INTERLACE 0x0 |
| 62 | +#define EPD_INTERLACE 0x2 |
| 63 | +#define EPD_SCAN_FORWARD 0x0 |
| 64 | +#define EPD_SCAN_BACKWARD 0x1 |
| 65 | + |
| 66 | +#define EPD_WHITE_BORDER 0x5 |
| 67 | +#define EPD_BLACK_BORDER 0x2 |
| 68 | + |
| 69 | +#define EPD_EXTERNAL_SENSOR 0x48 |
| 70 | +#define EPD_INTERNAL_SENSOR 0x80 |
| 71 | + |
| 72 | +#define EPD_RED_ENABLE 0x00 |
| 73 | +#define EPD_RED_DISABLE 0x40 |
| 74 | +#define EPD_RED_INVERT 0x80 |
| 75 | +#define EPD_BLACK_ENABLE 0x00 |
| 76 | +#define EPD_BLACK_DISABLE 0x04 |
| 77 | +#define EPD_BLACK_INVERT 0x08 |
| 78 | + |
| 79 | +#define EPD_Y_FORWARDS 0x02 |
| 80 | +#define EPD_Y_BACKWARDS 0x00 |
| 81 | +#define EPD_X_FORWARDS 0x01 |
| 82 | +#define EPD_X_BACKWARDS 0x00 |
| 83 | +#define EPD_RIGHT_THEN_DOWN 0x00 |
| 84 | +#define EPD_DOWN_THEN_RIGHT 0x04 |
| 85 | + |
| 86 | +#define EPD_MODE_NORMAL 0x00 |
| 87 | +#define EPD_MODE_SLEEP_1 0x01 |
| 88 | +#define EPD_MODE_SLEEP_2 0x03 |
| 89 | + |
| 90 | +// Combine the below commands to make an update sequence. |
| 91 | +// Note that commands like CLOCK_ON and CLOCK_OFF can be used together. |
| 92 | +// In this case, the signal will be enabled before the update and disabled after. |
| 93 | +#define EPDU_CLOCK_ON 0x80 |
| 94 | +#define EPDU_ANALOG_ON 0x40 |
| 95 | +#define EPDU_LOAD_TEMP 0x20 |
| 96 | +#define EPDU_LOAD_LUT 0x10 |
| 97 | +#define EPDU_DISP_MODE_1 0x00 |
| 98 | +#define EPDU_DISP_MODE_2 0x08 |
| 99 | +#define EPDU_OUTPUT 0x04 |
| 100 | +#define EPDU_ANALOG_OFF 0x02 |
| 101 | +#define EPDU_CLOCK_OFF 0x01 |
| 102 | +#define EPDU_DISPLAY (EPDU_OUTPUT | EPDU_LOAD_LUT) |
| 103 | + |
| 104 | +// Watchy EPD settings |
| 105 | +#define EPD_GATE_SETTINGS EPD_EVEN_FIRST | EPD_NO_INTERLACE | EPD_SCAN_FORWARD |
| 106 | +#define EPD_ENTRY_MODE EPD_X_FORWARDS | EPD_Y_FORWARDS | EPD_RIGHT_THEN_DOWN |
| 107 | +#define EPD_COLOUR_MODE EPD_RED_DISABLE | EPD_BLACK_INVERT |
| 108 | +#define EPD_UPDATE_SEQUENCE (EPDU_CLOCK_ON | EPDU_ANALOG_ON | EPDU_LOAD_TEMP | \ |
| 109 | + EPDU_DISPLAY | EPDU_DISP_MODE_1 | EPDU_CLOCK_OFF | \ |
| 110 | + EPDU_ANALOG_OFF) |
| 111 | +#define EPD_POST_UPDATE (EPDU_CLOCK_ON | EPDU_CLOCK_OFF | EPDU_ANALOG_OFF) |
| 112 | + |
| 113 | + |
| 114 | +#define EPDCMD_GATE_SETTING 0x01 |
| 115 | +#define EPDCMD_DEEP_SLEEP 0x10 |
| 116 | +#define EPDCMD_ENTRY_MODE 0x11 |
| 117 | +#define EPDCMD_SOFT_RESET 0x12 |
| 118 | +#define EPDCMD_TEMP_SENSOR 0x18 |
| 119 | +#define EPDCMD_PANEL_COLOURS 0x21 |
| 120 | +#define EPDCMD_MASTER_ACTIVATE 0x20 |
| 121 | +#define EPDCMD_UPDATE_SEQUENCE 0x22 |
| 122 | +#define EPDCMD_BORDER_WAVEFORM 0x3C |
| 123 | +#define EPDCMD_WRITE_RAM 0x24 |
| 124 | +#define EPDCMD_X_WINDOW 0x44 |
| 125 | +#define EPDCMD_Y_WINDOW 0x45 |
| 126 | +#define EPDCMD_X_OFFSET 0x4E |
| 127 | +#define EPDCMD_Y_OFFSET 0x4F |
| 128 | +#define EPDCMD_NOP 0x7F |
| 129 | + |
| 130 | + |
| 131 | +const uint8_t display_start_sequence[] = { |
| 132 | + EPDCMD_SOFT_RESET, DELAY, 20, |
| 133 | + EPDCMD_GATE_SETTING, 3, EPD_LAST_ROW % 256, EPD_LAST_ROW / 256, |
| 134 | + EPD_GATE_SETTINGS, |
| 135 | + EPDCMD_BORDER_WAVEFORM, 1, EPD_BLACK_BORDER, |
| 136 | + EPDCMD_TEMP_SENSOR, 1, EPD_INTERNAL_SENSOR, |
| 137 | + EPDCMD_PANEL_COLOURS, 1, EPD_COLOUR_MODE, |
| 138 | + EPDCMD_ENTRY_MODE, 1, EPD_ENTRY_MODE, |
| 139 | + EPDCMD_X_WINDOW, 2, EPD_X_WINDOW_START, EPD_X_WINDOW_END, |
| 140 | + EPDCMD_Y_WINDOW, 4, EPD_Y_WINDOW_START % 256, EPD_Y_WINDOW_START / 256, |
| 141 | + EPD_Y_WINDOW_END % 256, EPD_Y_WINDOW_END / 256, |
| 142 | + EPDCMD_X_OFFSET, 1, EPD_X_WINDOW_START, |
| 143 | + EPDCMD_Y_OFFSET, 2, EPD_Y_WINDOW_START % 256, EPD_Y_WINDOW_START / 256, |
| 144 | + |
| 145 | + EPDCMD_UPDATE_SEQUENCE, 1, EPD_UPDATE_SEQUENCE, |
| 146 | +}; |
| 147 | + |
| 148 | +const uint8_t display_stop_sequence[] = { |
| 149 | + EPDCMD_DEEP_SLEEP, 1, 0x01 |
| 150 | +}; |
| 151 | + |
| 152 | +const uint8_t refresh_sequence[] = { |
| 153 | + EPDCMD_MASTER_ACTIVATE, 0 |
| 154 | +}; |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +void board_init(void) { |
| 159 | + // Debug UART |
| 160 | + #ifdef DEBUG |
| 161 | + common_hal_never_reset_pin(&pin_GPIO43); |
| 162 | + common_hal_never_reset_pin(&pin_GPIO44); |
| 163 | + #endif /* DEBUG */ |
| 164 | + |
| 165 | + fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus; |
| 166 | + busio_spi_obj_t *spi = &bus->inline_bus; |
| 167 | + common_hal_busio_spi_construct(spi, &pin_GPIO18, &pin_GPIO23, NULL, false); |
| 168 | + common_hal_busio_spi_never_reset(spi); |
| 169 | + bus->base.type = &fourwire_fourwire_type; |
| 170 | + |
| 171 | + common_hal_fourwire_fourwire_construct( |
| 172 | + bus, |
| 173 | + spi, |
| 174 | + &pin_GPIO10, // EPD_DC Command or data |
| 175 | + &pin_GPIO5, // EPD_CS Chip select |
| 176 | + &pin_GPIO9, // EPD_RST Reset |
| 177 | + 2000000, // Baudrate |
| 178 | + 0, // Polarity |
| 179 | + 0 // Phase |
| 180 | + ); |
| 181 | + |
| 182 | + epaperdisplay_epaperdisplay_obj_t *display = &allocate_display()->epaper_display; |
| 183 | + display->base.type = &epaperdisplay_epaperdisplay_type; |
| 184 | + common_hal_epaperdisplay_epaperdisplay_construct( |
| 185 | + display, |
| 186 | + bus, |
| 187 | + display_start_sequence, sizeof(display_start_sequence), |
| 188 | + (double)0.01, // start up time |
| 189 | + display_stop_sequence, sizeof(display_stop_sequence), |
| 190 | + 200, // width |
| 191 | + 200, // height |
| 192 | + 200, // ram_width |
| 193 | + 296, // ram_height |
| 194 | + 0, // colstart |
| 195 | + 0, // rowstart |
| 196 | + 0, // rotation |
| 197 | + 0x44, // set_column_window_command |
| 198 | + 0x45, // set_row_window_command |
| 199 | + 0x4E, // set_current_column_command |
| 200 | + 0x4F, // set_current_row_command |
| 201 | + 0x24, // write_black_ram_command |
| 202 | + true, // black_bits_inverted |
| 203 | + 0x26, // write_color_ram_command |
| 204 | + false, // color_bits_inverted |
| 205 | + 0x000000, // highlight_color |
| 206 | + refresh_sequence, sizeof(refresh_sequence), |
| 207 | + (double)1.0, // refresh_time |
| 208 | + &pin_GPIO19, // busy_pin |
| 209 | + true, // busy_state |
| 210 | + (double)5.0, // seconds_per_frame |
| 211 | + false, // always_toggle_chip_select |
| 212 | + false, // grayscale |
| 213 | + false, // acep |
| 214 | + false, // two_byte_sequence_length |
| 215 | + true // address_little_endian |
| 216 | + ); |
| 217 | +} |
| 218 | + |
| 219 | +bool board_requests_safe_mode(void) { |
| 220 | + return false; |
| 221 | +} |
| 222 | + |
| 223 | +bool espressif_board_reset_pin_number(gpio_num_t pin_number) { |
| 224 | + if (pin_number == 13) { |
| 225 | + gpio_set_direction(pin_number, GPIO_MODE_DEF_OUTPUT); |
| 226 | + gpio_set_level(pin_number, false); |
| 227 | + return true; |
| 228 | + } |
| 229 | + return false; |
| 230 | +} |
| 231 | + |
| 232 | +void reset_board(void) { |
| 233 | + gpio_set_direction(13, GPIO_MODE_OUTPUT); |
| 234 | + gpio_set_level(13, false); |
| 235 | + |
| 236 | +} |
| 237 | + |
| 238 | +void board_deinit(void) { |
| 239 | + gpio_set_direction(13, GPIO_MODE_DEF_OUTPUT); |
| 240 | + gpio_set_level(13, false); |
| 241 | +} |
0 commit comments