Skip to content

Commit 05d8885

Browse files
committed
Rework displays in prep for dynamic support and 8bit parallel.
1 parent 076ddfd commit 05d8885

28 files changed

Lines changed: 817 additions & 318 deletions

File tree

main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <stdint.h>
2828
#include <string.h>
2929

30+
#include "shared-module/displayio/__init__.h"
3031
#include "extmod/vfs.h"
3132
#include "extmod/vfs_fat.h"
3233

@@ -49,6 +50,7 @@
4950
#include "supervisor/port.h"
5051
#include "supervisor/filesystem.h"
5152
#include "supervisor/shared/autoreload.h"
53+
#include "supervisor/shared/board_busses.h"
5254
#include "supervisor/shared/translate.h"
5355
#include "supervisor/shared/rgb_led_status.h"
5456
#include "supervisor/shared/safe_mode.h"
@@ -198,10 +200,13 @@ bool run_code_py(safe_mode_t safe_mode) {
198200
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
199201
}
200202
}
203+
// Turn off the display before the heap disappears.
204+
reset_primary_display();
201205
stop_mp();
202206
free_memory(heap);
203207

204208
reset_port();
209+
reset_board_busses();
205210
reset_board();
206211
reset_status_led();
207212

ports/atmel-samd/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ SRC_ASF := $(addprefix asf4/$(CHIP_FAMILY)/, $(SRC_ASF))
238238

239239
SRC_C = \
240240
audio_dma.c \
241-
board_busses.c \
242241
background.c \
243242
fatfs_port.c \
244243
mphalport.c \
@@ -307,6 +306,7 @@ SRC_COMMON_HAL = \
307306
digitalio/__init__.c \
308307
digitalio/DigitalInOut.c \
309308
displayio/FourWire.c \
309+
displayio/ParallelBus.c \
310310
i2cslave/__init__.c \
311311
i2cslave/I2CSlave.c \
312312
microcontroller/__init__.c \
@@ -379,6 +379,7 @@ SRC_SHARED_MODULE = \
379379
displayio/__init__.c \
380380
displayio/Bitmap.c \
381381
displayio/ColorConverter.c \
382+
displayio/Display.c \
382383
displayio/Group.c \
383384
displayio/OnDiskBitmap.c \
384385
displayio/Palette.c \

ports/atmel-samd/boards/board.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@
3333

3434
#include "py/mpconfig.h"
3535

36-
#ifdef CIRCUITPY_DISPLAYIO
37-
#include "common-hal/displayio/FourWire.h"
38-
39-
extern displayio_fourwire_obj_t board_display_obj;
40-
#endif
41-
4236
// Initializes board related state once on start up.
4337
void board_init(void);
4438

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
77
*
88
* Permission is hereby granted, free of charge, to any person obtaining a copy
99
* of this software and associated documentation files (the "Software"), to deal
@@ -26,6 +26,13 @@
2626

2727
#include "boards/board.h"
2828
#include "mpconfigboard.h"
29+
#include "hal/include/hal_gpio.h"
30+
31+
#include "shared-bindings/displayio/Display.h"
32+
#include "shared-bindings/displayio/FourWire.h"
33+
#include "shared-module/displayio/mipi_constants.h"
34+
35+
#include "tick.h"
2936

3037
void board_init(void) {
3138
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@
4343
#define IGNORE_PIN_PA25 1
4444

4545
#define CIRCUITPY_I2CSLAVE
46+
#define CIRCUITPY_DISPLAYIO (1)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "shared-bindings/board/__init__.h"
22

3-
#include "board_busses.h"
3+
#include "boards/board.h"
4+
#include "supervisor/shared/board_busses.h"
45

56
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
67
{ MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) },

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

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -25,94 +25,21 @@
2525
*/
2626

2727
#include "boards/board.h"
28+
#include "board_busses.h"
2829
#include "mpconfigboard.h"
2930
#include "hal/include/hal_gpio.h"
3031

31-
#include "shared-bindings/displayio/FourWire.h"
32-
#include "shared-module/displayio/mipi_constants.h"
32+
#include "shared-module/displayio/__init__.h"
3333

3434
#include "tick.h"
3535

36-
displayio_fourwire_obj_t board_display_obj;
37-
38-
#define DELAY 0x80
39-
40-
uint8_t display_init_sequence[] = {
41-
0xEF, 3, 0x03, 0x80, 0x02,
42-
0xCF, 3, 0x00, 0xC1, 0x30,
43-
0xED, 4, 0x64, 0x03, 0x12, 0x81,
44-
0xE8, 3, 0x85, 0x00, 0x78,
45-
0xCB, 5, 0x39, 0x2C, 0x00, 0x34, 0x02,
46-
0xF7, 1, 0x20,
47-
0xEA, 2, 0x00, 0x00,
48-
0xc0, 1, 0x23, // Power control VRH[5:0]
49-
0xc1, 1, 0x10, // Power control SAP[2:0];BT[3:0]
50-
0xc5, 2, 0x3e, 0x28, // VCM control
51-
0xc7, 1, 0x86, // VCM control2
52-
0x36, 1, 0x38, // Memory Access Control
53-
0x37, 1, 0x00, // Vertical scroll zero
54-
0x3a, 1, 0x55, // COLMOD: Pixel Format Set
55-
0xb1, 2, 0x00, 0x18, // Frame Rate Control (In Normal Mode/Full Colors)
56-
0xb6, 3, 0x08, 0x82, 0x27, // Display Function Control
57-
0xF2, 1, 0x00, // 3Gamma Function Disable
58-
0x26, 1, 0x01, // Gamma curve selected
59-
0xe0, 15, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, // Set Gamma
60-
0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00,
61-
0xe1, 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, // Set Gamma
62-
0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F,
63-
0x11, DELAY, 120, // Exit Sleep
64-
0x29, DELAY, 120, // Display on
65-
};
66-
67-
6836
void board_init(void) {
69-
board_display_obj.base.type = &displayio_fourwire_type;
70-
common_hal_displayio_fourwire_construct(&board_display_obj,
71-
&pin_PA13, // Clock
72-
&pin_PA12, // Data
73-
&pin_PB09, // Command or data
74-
&pin_PB06, // Chip select
75-
&pin_PA00, // Reset
76-
320, // Width
77-
240, // Height
78-
0, // column start
79-
0, // row start
80-
16, // Color depth
81-
MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command
82-
MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command
83-
MIPI_COMMAND_WRITE_MEMORY_START); // Write memory command
84-
85-
uint32_t i = 0;
86-
common_hal_displayio_fourwire_begin_transaction(&board_display_obj);
87-
while (i < sizeof(display_init_sequence)) {
88-
uint8_t *cmd = display_init_sequence + i;
89-
uint8_t data_size = *(cmd + 1);
90-
bool delay = (data_size & DELAY) != 0;
91-
data_size &= ~DELAY;
92-
uint8_t *data = cmd + 2;
93-
common_hal_displayio_fourwire_send(&board_display_obj, true, cmd, 1);
94-
common_hal_displayio_fourwire_send(&board_display_obj, false, data, data_size);
95-
if (delay) {
96-
data_size++;
97-
uint16_t delay_length_ms = *(cmd + 1 + data_size);
98-
if (delay_length_ms == 255) {
99-
delay_length_ms = 500;
100-
}
101-
uint64_t start = ticks_ms;
102-
while (ticks_ms - start < delay_length_ms) {}
103-
} else {
104-
uint64_t start = ticks_ms;
105-
while (ticks_ms - start < 10) {}
106-
}
107-
i += 2 + data_size;
108-
}
109-
common_hal_displayio_fourwire_end_transaction(&board_display_obj);
11037
}
11138

11239
bool board_requests_safe_mode(void) {
11340
return false;
11441
}
11542

11643
void reset_board(void) {
117-
common_hal_displayio_fourwire_show(&board_display_obj, NULL);
44+
common_hal_displayio_display_show(&primary_display_obj, NULL);
11845
}

ports/atmel-samd/common-hal/busio/SPI.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ void reset_sercoms(void) {
6565
if (sercom_instances[i] == MICROPY_HW_APA102_SERCOM) {
6666
continue;
6767
}
68-
#endif
69-
#ifdef CIRCUITPY_DISPLAYIO
70-
// TODO(tannewt): Make this dynamic.
71-
if (sercom_instances[i] == board_display_obj.bus.spi_desc.dev.prvt) {
72-
continue;
73-
}
7468
#endif
7569
// SWRST is same for all modes of SERCOMs.
7670
sercom_instances[i]->SPI.CTRLA.bit.SWRST = 1;

ports/atmel-samd/common-hal/displayio/FourWire.c

Lines changed: 14 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@
3434
#include "tick.h"
3535

3636
void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
37-
const mcu_pin_obj_t* clock, const mcu_pin_obj_t* data, const mcu_pin_obj_t* command,
38-
const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset, uint16_t width,
39-
uint16_t height, int16_t colstart, int16_t rowstart, uint16_t color_depth,
40-
uint8_t set_column_command, uint8_t set_row_command, uint8_t write_ram_command) {
37+
busio_spi_obj_t* spi, const mcu_pin_obj_t* command,
38+
const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset) {
4139

42-
common_hal_busio_spi_construct(&self->bus, clock, data, mp_const_none);
43-
common_hal_busio_spi_never_reset(&self->bus);
40+
self->bus = spi;
41+
common_hal_busio_spi_never_reset(self->bus);
4442

4543
common_hal_digitalio_digitalinout_construct(&self->command, command);
4644
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
@@ -53,93 +51,27 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
5351
never_reset_pin_number(command->number);
5452
never_reset_pin_number(chip_select->number);
5553
never_reset_pin_number(reset->number);
56-
57-
self->width = width;
58-
self->height = height;
59-
self->color_depth = color_depth;
60-
self->set_column_command = set_column_command;
61-
self->set_row_command = set_row_command;
62-
self->write_ram_command = write_ram_command;
63-
self->current_group = NULL;
64-
self->colstart = colstart;
65-
self->rowstart = rowstart;
6654
}
6755

68-
bool common_hal_displayio_fourwire_begin_transaction(displayio_fourwire_obj_t* self) {
69-
if (!common_hal_busio_spi_try_lock(&self->bus)) {
56+
bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t obj) {
57+
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
58+
if (!common_hal_busio_spi_try_lock(self->bus)) {
7059
return false;
7160
}
7261
// TODO(tannewt): Stop hardcoding SPI frequency, polarity and phase.
73-
common_hal_busio_spi_configure(&self->bus, 48000000, 0, 0, 8);
62+
common_hal_busio_spi_configure(self->bus, 12000000, 0, 0, 8);
7463
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
7564
return true;
7665
}
7766

78-
void common_hal_displayio_fourwire_send(displayio_fourwire_obj_t* self, bool command, uint8_t *data, uint32_t data_length) {
67+
void common_hal_displayio_fourwire_send(mp_obj_t obj, bool command, uint8_t *data, uint32_t data_length) {
68+
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
7969
common_hal_digitalio_digitalinout_set_value(&self->command, !command);
80-
common_hal_busio_spi_write(&self->bus, data, data_length);
70+
common_hal_busio_spi_write(self->bus, data, data_length);
8171
}
8272

83-
void common_hal_displayio_fourwire_end_transaction(displayio_fourwire_obj_t* self) {
73+
void common_hal_displayio_fourwire_end_transaction(mp_obj_t obj) {
74+
displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj);
8475
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
85-
common_hal_busio_spi_unlock(&self->bus);
86-
}
87-
88-
void common_hal_displayio_fourwire_show(displayio_fourwire_obj_t* self, displayio_group_t* root_group) {
89-
self->current_group = root_group;
90-
common_hal_displayio_fourwire_refresh_soon(self);
91-
}
92-
93-
void common_hal_displayio_fourwire_refresh_soon(displayio_fourwire_obj_t* self) {
94-
self->refresh = true;
95-
}
96-
97-
int32_t common_hal_displayio_fourwire_wait_for_frame(displayio_fourwire_obj_t* self) {
98-
uint64_t last_refresh = self->last_refresh;
99-
while (last_refresh == self->last_refresh) {
100-
MICROPY_VM_HOOK_LOOP
101-
}
102-
return 0;
103-
}
104-
105-
void displayio_fourwire_start_region_update(displayio_fourwire_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
106-
// TODO(tannewt): Handle displays with single byte bounds.
107-
common_hal_displayio_fourwire_begin_transaction(self);
108-
uint16_t data[2];
109-
common_hal_displayio_fourwire_send(self, true, &self->set_column_command, 1);
110-
data[0] = __builtin_bswap16(x0 + self->colstart);
111-
data[1] = __builtin_bswap16(x1-1 + self->colstart);
112-
common_hal_displayio_fourwire_send(self, false, (uint8_t*) data, 4);
113-
common_hal_displayio_fourwire_send(self, true, &self->set_row_command, 1);
114-
data[0] = __builtin_bswap16(y0 + 1 + self->rowstart);
115-
data[1] = __builtin_bswap16(y1 + self->rowstart);
116-
common_hal_displayio_fourwire_send(self, false, (uint8_t*) data, 4);
117-
common_hal_displayio_fourwire_send(self, true, &self->write_ram_command, 1);
118-
}
119-
120-
bool displayio_fourwire_send_pixels(displayio_fourwire_obj_t* self, uint32_t* pixels, uint32_t length) {
121-
// TODO: Set this up so its async and 32 bit DMA transfers.
122-
common_hal_displayio_fourwire_send(self, false, (uint8_t*) pixels, length*4);
123-
return true;
124-
}
125-
126-
void displayio_fourwire_finish_region_update(displayio_fourwire_obj_t* self) {
127-
common_hal_displayio_fourwire_end_transaction(self);
128-
}
129-
130-
bool displayio_fourwire_frame_queued(displayio_fourwire_obj_t* self) {
131-
// Refresh at ~30 fps.
132-
return (ticks_ms - self->last_refresh) > 32;
133-
}
134-
135-
bool displayio_fourwire_refresh_queued(displayio_fourwire_obj_t* self) {
136-
return self->refresh || (self->current_group != NULL && displayio_group_needs_refresh(self->current_group));
137-
}
138-
139-
void displayio_fourwire_finish_refresh(displayio_fourwire_obj_t* self) {
140-
if (self->current_group != NULL) {
141-
displayio_group_finish_refresh(self->current_group);
142-
}
143-
self->refresh = false;
144-
self->last_refresh = ticks_ms;
76+
common_hal_busio_spi_unlock(self->bus);
14577
}

ports/atmel-samd/common-hal/displayio/FourWire.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,10 @@
3333

3434
typedef struct {
3535
mp_obj_base_t base;
36-
busio_spi_obj_t bus;
36+
busio_spi_obj_t* bus;
3737
digitalio_digitalinout_obj_t command;
3838
digitalio_digitalinout_obj_t chip_select;
3939
digitalio_digitalinout_obj_t reset;
40-
uint16_t width;
41-
uint16_t height;
42-
uint16_t color_depth;
43-
uint8_t set_column_command;
44-
uint8_t set_row_command;
45-
uint8_t write_ram_command;
46-
displayio_group_t *current_group;
47-
bool refresh;
48-
uint64_t last_refresh;
49-
int16_t colstart;
50-
int16_t rowstart;
5140
} displayio_fourwire_obj_t;
5241

5342
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H

0 commit comments

Comments
 (0)