Skip to content

Commit f477848

Browse files
committed
paralleldisplay: reset and read pins should be optional
The ``reset`` and ``read`` pins should be optional, but the espressif code had several places where it assumed they are not, and a bug that caused a crash on ``release_displays`` if they were made optional. The bug was caused by the fields for storing pin numbers being set to ``NO_PIN``, which has value of -1, while the fields have type ``uint8_t``. That set the actual value to 255, and a subsequent comparison to ``NO_PIN`` returned false.
1 parent 08b44ea commit f477848

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

ports/espressif/common-hal/paralleldisplay/ParallelBus.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ void common_hal_paralleldisplay_parallelbus_construct_nonsequential(paralleldisp
7272
.buffer_size = 512,
7373
};
7474

75-
if (reset != NULL) {
76-
common_hal_never_reset_pin(reset);
77-
self->reset_pin_number = reset->number;
78-
} else {
79-
self->reset_pin_number = NO_PIN;
80-
}
81-
8275
for (uint8_t i = 0; i < n_pins; i++) {
8376
common_hal_never_reset_pin(data_pins[i]);
8477
config.pin_data_num[i] = common_hal_mcu_pin_number(data_pins[i]);
@@ -98,10 +91,14 @@ void common_hal_paralleldisplay_parallelbus_construct_nonsequential(paralleldisp
9891
gpio_set_level(read->number, true);
9992
}
10093

94+
self->reset_pin_number = NO_PIN;
95+
if (reset != NULL) {
96+
common_hal_never_reset_pin(reset);
97+
self->reset_pin_number = reset->number;
98+
}
99+
101100
common_hal_never_reset_pin(chip_select);
102101
common_hal_never_reset_pin(command);
103-
common_hal_never_reset_pin(read);
104-
common_hal_never_reset_pin(reset);
105102
common_hal_never_reset_pin(write);
106103

107104
self->config = config;
@@ -140,8 +137,8 @@ void common_hal_paralleldisplay_parallelbus_deinit(paralleldisplay_parallelbus_o
140137

141138
reset_pin_number(self->config.pin_num_cs);
142139
reset_pin_number(self->config.pin_num_wr);
143-
reset_pin_number(self->read_pin_number);
144140
reset_pin_number(self->config.pin_num_rs);
141+
reset_pin_number(self->read_pin_number);
145142
reset_pin_number(self->reset_pin_number);
146143

147144
port_i2s_reset_instance(0);

ports/espressif/common-hal/paralleldisplay/ParallelBus.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
typedef struct {
3434
mp_obj_base_t base;
35-
uint8_t read_pin_number;
36-
uint8_t reset_pin_number;
35+
int8_t read_pin_number;
36+
int8_t reset_pin_number;
3737
i2s_lcd_config_t config;
3838
i2s_lcd_handle_t handle;
3939
} paralleldisplay_parallelbus_obj_t;

shared-bindings/paralleldisplay/ParallelBus.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//| protocol may be refered to as 8080-I Series Parallel Interface in datasheets. It doesn't handle
4343
//| display initialization."""
4444
//|
45-
//| def __init__(self, *, data0: microcontroller.Pin, command: microcontroller.Pin, chip_select: microcontroller.Pin, write: microcontroller.Pin, read: microcontroller.Pin, reset: microcontroller.Pin, frequency: int = 30_000_000) -> None:
45+
//| def __init__(self, *, data0: microcontroller.Pin, command: microcontroller.Pin, chip_select: microcontroller.Pin, write: microcontroller.Pin, read: Optional[microcontroller.Pin], reset: Optional[microcontroller.Pin] = None, frequency: int = 30_000_000) -> None:
4646
//| """Create a ParallelBus object associated with the given pins. The bus is inferred from data0
4747
//| by implying the next 7 additional pins on a given GPIO port.
4848
//|
@@ -56,8 +56,8 @@
5656
//| :param microcontroller.Pin command: Data or command pin
5757
//| :param microcontroller.Pin chip_select: Chip select pin
5858
//| :param microcontroller.Pin write: Write pin
59-
//| :param microcontroller.Pin read: Read pin
60-
//| :param microcontroller.Pin reset: Reset pin
59+
//| :param microcontroller.Pin read: Read pin, optional
60+
//| :param microcontroller.Pin reset: Reset pin, optional
6161
//| :param int frequency: The communication frequency in Hz for the display on the bus"""
6262
//| ...
6363
//|
@@ -69,8 +69,8 @@ STATIC mp_obj_t paralleldisplay_parallelbus_make_new(const mp_obj_type_t *type,
6969
{ MP_QSTR_command, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
7070
{ MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
7171
{ MP_QSTR_write, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
72-
{ MP_QSTR_read, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
73-
{ MP_QSTR_reset, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
72+
{ MP_QSTR_read, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
73+
{ MP_QSTR_reset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none } },
7474
{ MP_QSTR_frequency, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 30000000 } },
7575
};
7676
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -79,8 +79,8 @@ STATIC mp_obj_t paralleldisplay_parallelbus_make_new(const mp_obj_type_t *type,
7979
const mcu_pin_obj_t *command = validate_obj_is_free_pin(args[ARG_command].u_obj);
8080
const mcu_pin_obj_t *chip_select = validate_obj_is_free_pin(args[ARG_chip_select].u_obj);
8181
const mcu_pin_obj_t *write = validate_obj_is_free_pin(args[ARG_write].u_obj);
82-
const mcu_pin_obj_t *read = validate_obj_is_free_pin(args[ARG_read].u_obj);
83-
const mcu_pin_obj_t *reset = validate_obj_is_free_pin(args[ARG_reset].u_obj);
82+
const mcu_pin_obj_t *read = validate_obj_is_free_pin_or_none(args[ARG_read].u_obj);
83+
const mcu_pin_obj_t *reset = validate_obj_is_free_pin_or_none(args[ARG_reset].u_obj);
8484

8585
paralleldisplay_parallelbus_obj_t *self = &allocate_display_bus_or_raise()->parallel_bus;
8686
self->base.type = &paralleldisplay_parallelbus_type;

0 commit comments

Comments
 (0)