Skip to content

Commit c064dea

Browse files
authored
Merge pull request #1785 from pewpew-game/ugame-displayio
Enable displayio for the ugame10 board
2 parents 22ca985 + 7aab3e8 commit c064dea

File tree

8 files changed

+109
-32
lines changed

8 files changed

+109
-32
lines changed

ports/atmel-samd/boards/ugame10/board.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,80 @@
2626

2727
#include "boards/board.h"
2828

29+
#include "shared-bindings/board/__init__.h"
30+
#include "shared-bindings/displayio/FourWire.h"
31+
#include "shared-module/displayio/__init__.h"
32+
#include "shared-module/displayio/mipi_constants.h"
33+
#include "shared-bindings/busio/SPI.h"
34+
35+
#include "tick.h"
36+
37+
displayio_fourwire_obj_t board_display_obj;
38+
39+
#define DELAY 0x80
40+
41+
uint8_t display_init_sequence[] = {
42+
0x01, 0 | DELAY, 150, // SWRESET
43+
0x11, 0 | DELAY, 255, // SLPOUT
44+
0xb1, 3, 0x01, 0x2C, 0x2D, // _FRMCTR1
45+
0xb2, 3, 0x01, 0x2C, 0x2D, //
46+
0xb3, 6, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D,
47+
0xb4, 1, 0x07, // _INVCTR line inversion
48+
0xc0, 3, 0xa2, 0x02, 0x84, // _PWCTR1 GVDD = 4.7V, 1.0uA
49+
0xc1, 1, 0xc5, // _PWCTR2 VGH=14.7V, VGL=-7.35V
50+
0xc2, 2, 0x0a, 0x00, // _PWCTR3 Opamp current small, Boost frequency
51+
0xc3, 2, 0x8a, 0x2a,
52+
0xc4, 2, 0x8a, 0xee,
53+
0xc5, 1, 0x0e, // _VMCTR1 VCOMH = 4V, VOML = -1.1V
54+
0x2a, 0, // _INVOFF
55+
0x36, 1, 0xa0, // _MADCTL bottom to top refresh
56+
// 1 clk cycle nonoverlap, 2 cycle gate rise, 3 sycle osc equalie,
57+
// fix on VTL
58+
0x3a, 1, 0x05, // COLMOD - 16bit color
59+
0xe0, 16, 0x02, 0x1c, 0x07, 0x12, // _GMCTRP1 Gamma
60+
0x37, 0x32, 0x29, 0x2d,
61+
0x29, 0x25, 0x2B, 0x39,
62+
0x00, 0x01, 0x03, 0x10,
63+
0xe1, 16, 0x03, 0x1d, 0x07, 0x06, // _GMCTRN1
64+
0x2E, 0x2C, 0x29, 0x2D,
65+
0x2E, 0x2E, 0x37, 0x3F,
66+
0x00, 0x00, 0x02, 0x10,
67+
0x2a, 3, 0x02, 0x00, 0x81, // _CASET XSTART = 2, XEND = 129
68+
0x2b, 3, 0x02, 0x00, 0x81, // _RASET XSTART = 2, XEND = 129
69+
0x13, 0 | DELAY, 10, // _NORON
70+
0x29, 0 | DELAY, 100, // _DISPON
71+
};
72+
2973
void board_init(void) {
74+
displayio_fourwire_obj_t* bus = &displays[0].fourwire_bus;
75+
bus->base.type = &displayio_fourwire_type;
76+
busio_spi_obj_t *spi = common_hal_board_create_spi();
77+
common_hal_busio_spi_configure(spi, 24000000, 0, 0, 8);
78+
common_hal_displayio_fourwire_construct(bus,
79+
spi,
80+
&pin_PA09, // Command or data
81+
&pin_PA08, // Chip select
82+
NULL); // Reset
83+
84+
displayio_display_obj_t* display = &displays[0].display;
85+
display->base.type = &displayio_display_type;
86+
common_hal_displayio_display_construct(display,
87+
bus,
88+
128, // Width
89+
128, // Height
90+
3, // column start
91+
2, // row start
92+
0, // rotation
93+
16, // Color depth
94+
MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command
95+
MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command
96+
MIPI_COMMAND_WRITE_MEMORY_START, // Write memory command
97+
0x37, // set vertical scroll command
98+
display_init_sequence,
99+
sizeof(display_init_sequence),
100+
NULL,
101+
false, // single_byte_bounds
102+
false); // data as commands
30103
}
31104

32105
bool board_requests_safe_mode(void) {

ports/atmel-samd/boards/ugame10/mpconfigboard.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919

2020
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
2121

22-
#define EXTRA_BUILTIN_MODULES \
23-
{ MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, \
24-
{ MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module }, \
25-
{ MP_OBJ_NEW_QSTR(MP_QSTR__stage), (mp_obj_t)&stage_module }
22+
#define DEFAULT_SPI_BUS_SCK (&pin_PA07)
23+
#define DEFAULT_SPI_BUS_MISO (&pin_PA11)
24+
#define DEFAULT_SPI_BUS_MOSI (&pin_PA06)
2625

2726
#define IGNORE_PIN_PB00 1
2827
#define IGNORE_PIN_PB01 1

ports/atmel-samd/boards/ugame10/mpconfigboard.mk

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ CIRCUITPY_MATH = 1
1717
CIRCUITPY_AUDIOIO = 1
1818
CIRCUITPY_ANALOGIO = 1
1919
CIRCUITPY_GAMEPAD = 1
20+
CIRCUITPY_DISPLAYIO = 1
21+
2022
CIRCUITPY_TOUCHIO = 0
2123
CIRCUITPY_NEOPIXEL_WRITE = 0
2224
CIRCUITPY_RTC = 0
23-
CIRCUITPY_SAMD = 0
2425
CIRCUITPY_USB_MIDI = 0
2526
CIRCUITPY_USB_HID = 0
27+
CIRCUITPY_I2CSLAVE = 0
2628
CIRCUITPY_FREQUENCYIO = 0
27-
CIRCUITPY_SMALL_BUILD = 1
29+
CIRCUITPY_AUDIOBUSIO = 0
30+
CIRCUITPY_PIXELBUF = 0
31+
32+
FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/ugame10
2833

29-
FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage
34+
CIRCUITPY_DISPLAY_FONT = "../../tools/Tecate-bitmap-fonts/bitmap/phallus/lemon.bdf"

ports/atmel-samd/boards/ugame10/pins.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "shared-bindings/board/__init__.h"
2+
#include "shared-module/displayio/__init__.h"
23

34
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
45
{ MP_ROM_QSTR(MP_QSTR_X), MP_ROM_PTR(&pin_PA00) },
@@ -23,8 +24,8 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
2324
{ MP_ROM_QSTR(MP_QSTR_B), MP_ROM_PTR(&pin_PA14) },
2425
{ MP_ROM_QSTR(MP_QSTR_C), MP_ROM_PTR(&pin_PA15) },
2526
{ MP_ROM_QSTR(MP_QSTR_D), MP_ROM_PTR(&pin_PA28) },
26-
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
27+
2728
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) },
28-
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },
29+
{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)}
2930
};
3031
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

shared-bindings/_stage/__init__.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "py/mperrno.h"
2929
#include "py/runtime.h"
3030
#include "shared-bindings/busio/SPI.h"
31+
#include "shared-bindings/displayio/Display.h"
3132
#include "shared-module/_stage/__init__.h"
3233
#include "Layer.h"
3334
#include "Text.h"
@@ -49,7 +50,7 @@
4950
//| Layer
5051
//| Text
5152
//|
52-
//| .. function:: render(x0, y0, x1, y1, layers, buffer, spi)
53+
//| .. function:: render(x0, y0, x1, y1, layers, buffer, display)
5354
//|
5455
//| Render and send to the display a fragment of the screen.
5556
//|
@@ -59,11 +60,8 @@
5960
//| :param int y1: Bottom edge of the fragment.
6061
//| :param list layers: A list of the :py:class:`~_stage.Layer` objects.
6162
//| :param bytearray buffer: A buffer to use for rendering.
62-
//| :param ~busio.SPI spi: The SPI bus to use.
63+
//| :param ~displayio.Display display: The display to use.
6364
//|
64-
//| Note that this function only sends the raw pixel data. Setting up
65-
//| the display for receiving it and handling the chip-select and
66-
//| data-command pins has to be done outside of it.
6765
//| There are also no sanity checks, outside of the basic overflow
6866
//| checking. The caller is responsible for making the passed parameters
6967
//| valid.
@@ -85,12 +83,19 @@ STATIC mp_obj_t stage_render(size_t n_args, const mp_obj_t *args) {
8583
uint16_t *buffer = bufinfo.buf;
8684
size_t buffer_size = bufinfo.len / 2; // 16-bit indexing
8785

88-
busio_spi_obj_t *spi = MP_OBJ_TO_PTR(args[6]);
86+
if (!MP_OBJ_IS_TYPE(args[6], &displayio_display_type)) {
87+
mp_raise_TypeError(translate("argument num/types mismatch"));
88+
}
89+
displayio_display_obj_t *display = MP_OBJ_TO_PTR(args[6]);
8990

90-
if (!render_stage(x0, y0, x1, y1, layers, layers_size,
91-
buffer, buffer_size, spi)) {
92-
mp_raise_OSError(MP_EIO);
91+
while (!displayio_display_begin_transaction(display)) {
92+
#ifdef MICROPY_VM_HOOK_LOOP
93+
MICROPY_VM_HOOK_LOOP ;
94+
#endif
9395
}
96+
displayio_display_set_region_to_update(display, x0, y0, x1, y1);
97+
render_stage(x0, y0, x1, y1, layers, layers_size, buffer, buffer_size, display);
98+
displayio_display_end_transaction(display);
9499

95100
return mp_const_none;
96101
}

shared-module/_stage/__init__.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
#include "shared-bindings/_stage/Text.h"
3232

3333

34-
bool render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
34+
void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
3535
mp_obj_t *layers, size_t layers_size,
3636
uint16_t *buffer, size_t buffer_size,
37-
busio_spi_obj_t *spi) {
37+
displayio_display_obj_t *display) {
3838

3939
size_t index = 0;
4040
for (uint16_t y = y0; y < y1; ++y) {
@@ -55,19 +55,13 @@ bool render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
5555
index += 1;
5656
// The buffer is full, send it.
5757
if (index >= buffer_size) {
58-
if (!common_hal_busio_spi_write(spi,
59-
((uint8_t*)buffer), buffer_size * 2)) {
60-
return false;
61-
}
58+
display->send(display->bus, false, ((uint8_t*)buffer), buffer_size * 2);
6259
index = 0;
6360
}
6461
}
6562
}
6663
// Send the remaining data.
6764
if (index) {
68-
if (!common_hal_busio_spi_write(spi, ((uint8_t*)buffer), index * 2)) {
69-
return false;
70-
}
65+
display->send(display->bus, false, ((uint8_t*)buffer), index * 2);
7166
}
72-
return true;
7367
}

shared-module/_stage/__init__.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
#ifndef MICROPY_INCLUDED_SHARED_MODULE__STAGE_H
2828
#define MICROPY_INCLUDED_SHARED_MODULE__STAGE_H
2929

30-
#include "shared-bindings/busio/SPI.h"
30+
#include "shared-bindings/displayio/Display.h"
3131
#include <stdint.h>
3232
#include <stdbool.h>
3333
#include "py/obj.h"
3434

3535
#define TRANSPARENT (0x1ff8)
3636

37-
bool render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
37+
void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
3838
mp_obj_t *layers, size_t layers_size,
3939
uint16_t *buffer, size_t buffer_size,
40-
busio_spi_obj_t *spi);
40+
displayio_display_obj_t *display);
4141

4242
#endif // MICROPY_INCLUDED_SHARED_MODULE__STAGE

0 commit comments

Comments
 (0)