Skip to content

Commit bcc33fa

Browse files
committed
displayio: check whether we locked the bus or not
1 parent 77fcc6f commit bcc33fa

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

shared-module/busdisplay/BusDisplay.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "shared-bindings/busdisplay/BusDisplay.h"
88

9+
#include "py/mphal.h"
910
#include "py/runtime.h"
1011
#if CIRCUITPY_FOURWIRE
1112
#include "shared-bindings/fourwire/FourWire.h"
@@ -16,6 +17,7 @@
1617
#if CIRCUITPY_PARALLELDISPLAYBUS
1718
#include "shared-bindings/paralleldisplaybus/ParallelBus.h"
1819
#endif
20+
#include "shared/runtime/interrupt_char.h"
1921
#include "shared-bindings/microcontroller/Pin.h"
2022
#include "shared-bindings/time/__init__.h"
2123
#include "shared-module/displayio/__init__.h"
@@ -73,6 +75,9 @@ void common_hal_busdisplay_busdisplay_construct(busdisplay_busdisplay_obj_t *sel
7375
uint8_t *data = cmd + 2;
7476
while (!displayio_display_bus_begin_transaction(&self->bus)) {
7577
RUN_BACKGROUND_TASKS;
78+
if (mp_hal_is_interrupted()) {
79+
mp_raise_RuntimeError_varg(MP_ERROR_TEXT("%q init failed"), MP_QSTR_display);
80+
}
7681
}
7782
if (self->bus.data_as_commands) {
7883
uint8_t full_command[data_size + 1];
@@ -288,11 +293,9 @@ static bool _refresh_area(busdisplay_busdisplay_obj_t *self, const displayio_are
288293
displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer);
289294

290295
// Can't acquire display bus; skip the rest of the data.
291-
if (!displayio_display_bus_is_free(&self->bus)) {
296+
if (!displayio_display_bus_begin_transaction(&self->bus)) {
292297
return false;
293298
}
294-
295-
displayio_display_bus_begin_transaction(&self->bus);
296299
_send_pixels(self, (uint8_t *)buffer, subrectangle_size_bytes);
297300
displayio_display_bus_end_transaction(&self->bus);
298301

shared-module/displayio/bus_core.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,16 @@ void displayio_display_bus_construct(displayio_display_bus_t *self,
8585
self->bus = bus;
8686
}
8787

88+
// This is just a hint, and is not a reliable result, since the bus could be grabbed in between this called
89+
// and the attempt to use the bus. Use displayio_display_bus_begin_transaction(), which is atomic.
8890
bool displayio_display_bus_is_free(displayio_display_bus_t *self) {
8991
return !self->bus || self->bus_free(self->bus);
9092
}
9193

92-
bool displayio_display_bus_begin_transaction(displayio_display_bus_t *self) {
94+
MP_WARN_UNUSED_RESULT bool displayio_display_bus_begin_transaction(displayio_display_bus_t *self) {
95+
if (!self->bus) {
96+
return false;
97+
}
9398
mp_obj_base_t *bus_base = MP_OBJ_TO_PTR(self->bus);
9499
if (bus_base->type == &mp_type_NoneType) {
95100
return false;
@@ -128,7 +133,9 @@ void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, d
128133
}
129134

130135
// Set column.
131-
displayio_display_bus_begin_transaction(self);
136+
if (!displayio_display_bus_begin_transaction(self)) {
137+
return;
138+
}
132139
uint8_t data[5];
133140
data[0] = self->column_command;
134141
uint8_t data_length = 1;
@@ -167,7 +174,9 @@ void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, d
167174

168175
if (self->set_current_column_command != NO_COMMAND) {
169176
uint8_t command = self->set_current_column_command;
170-
displayio_display_bus_begin_transaction(self);
177+
if (!displayio_display_bus_begin_transaction(self)) {
178+
return;
179+
}
171180
self->send(self->bus, DISPLAY_COMMAND, chip_select, &command, 1);
172181
// Only send the first half of data because it is the first coordinate.
173182
self->send(self->bus, DISPLAY_DATA, chip_select, data, data_length / 2);
@@ -176,7 +185,9 @@ void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, d
176185

177186

178187
// Set row.
179-
displayio_display_bus_begin_transaction(self);
188+
if (!displayio_display_bus_begin_transaction(self)) {
189+
return;
190+
}
180191
data[0] = self->row_command;
181192
data_length = 1;
182193
if (!self->data_as_commands) {
@@ -211,7 +222,9 @@ void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, d
211222

212223
if (self->set_current_row_command != NO_COMMAND) {
213224
uint8_t command = self->set_current_row_command;
214-
displayio_display_bus_begin_transaction(self);
225+
if (!displayio_display_bus_begin_transaction(self)) {
226+
return;
227+
}
215228
self->send(self->bus, DISPLAY_COMMAND, chip_select, &command, 1);
216229
// Only send the first half of data because it is the first coordinate.
217230
self->send(self->bus, DISPLAY_DATA, chip_select, data, data_length / 2);

shared-module/epaperdisplay/EPaperDisplay.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ static void send_command_sequence(epaperdisplay_epaperdisplay_obj_t *self,
153153
data_size = ((data_size & ~DELAY) << 8) + *(cmd + 2);
154154
data = cmd + 3;
155155
}
156-
displayio_display_bus_begin_transaction(&self->bus);
156+
while (!displayio_display_bus_begin_transaction(&self->bus) &&
157+
!mp_hal_is_interrupted()) {
158+
RUN_BACKGROUND_TASKS;
159+
}
157160
self->bus.send(self->bus.bus, DISPLAY_COMMAND, self->chip_select, cmd, 1);
158161
self->bus.send(self->bus.bus, DISPLAY_DATA, self->chip_select, data, data_size);
159162
displayio_display_bus_end_transaction(&self->bus);
@@ -311,7 +314,10 @@ static bool epaperdisplay_epaperdisplay_refresh_area(epaperdisplay_epaperdisplay
311314
if (pass == 1) {
312315
write_command = self->write_color_ram_command;
313316
}
314-
displayio_display_bus_begin_transaction(&self->bus);
317+
if (!displayio_display_bus_begin_transaction(&self->bus)) {
318+
// Display bus not available now.
319+
return false;
320+
}
315321
self->bus.send(self->bus.bus, DISPLAY_COMMAND, self->chip_select, &write_command, 1);
316322
displayio_display_bus_end_transaction(&self->bus);
317323

@@ -388,7 +394,9 @@ static bool _clean_area(epaperdisplay_epaperdisplay_obj_t *self) {
388394
memset(buffer, 0x77, width / 2);
389395

390396
uint8_t write_command = self->write_black_ram_command;
391-
displayio_display_bus_begin_transaction(&self->bus);
397+
if (displayio_display_bus_begin_transaction(&self->bus)) {
398+
return false;
399+
}
392400
self->bus.send(self->bus.bus, DISPLAY_COMMAND, self->chip_select, &write_command, 1);
393401
displayio_display_bus_end_transaction(&self->bus);
394402

0 commit comments

Comments
 (0)