Skip to content

Commit 6b7acc6

Browse files
committed
Add polarity and phase to FourWire.
It was fixed as 0/0 even though it used to get it from the current SPI state. This makes it more explicit with kwargs. Thanks to magpie_lark and kmatocha on the Adafruit Support forum for finding the issue: https://forums.adafruit.com/viewtopic.php?f=60&t=162515
1 parent 59eb35d commit 6b7acc6

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

shared-bindings/displayio/FourWire.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
//| Manage updating a display over SPI four wire protocol in the background while Python code runs.
4747
//| It doesn't handle display initialization.
4848
//|
49-
//| .. class:: FourWire(spi_bus, *, command, chip_select, reset=None, baudrate=24000000)
49+
//| .. class:: FourWire(spi_bus, *, command, chip_select, reset=None, baudrate=24000000, polarity=0,
50+
//| phase=0)
5051
//|
5152
//| Create a FourWire object associated with the given pins.
5253
//|
@@ -60,15 +61,20 @@
6061
//| :param microcontroller.Pin chip_select: Chip select pin
6162
//| :param microcontroller.Pin reset: Reset pin. When None only software reset can be used
6263
//| :param int baudrate: Maximum baudrate in Hz for the display on the bus
64+
//| :param int polarity: the base state of the clock line (0 or 1)
65+
//| :param int phase: the edge of the clock that data is captured. First (0)
66+
//| or second (1). Rising or falling depends on clock polarity.
6367
//|
6468
STATIC mp_obj_t displayio_fourwire_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
65-
enum { ARG_spi_bus, ARG_command, ARG_chip_select, ARG_reset, ARG_baudrate };
69+
enum { ARG_spi_bus, ARG_command, ARG_chip_select, ARG_reset, ARG_baudrate, ARG_polarity, ARG_phase };
6670
static const mp_arg_t allowed_args[] = {
6771
{ MP_QSTR_spi_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
6872
{ MP_QSTR_command, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
6973
{ MP_QSTR_chip_select, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
7074
{ MP_QSTR_reset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} },
7175
{ MP_QSTR_baudrate, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 24000000} },
76+
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
77+
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
7278
};
7379
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
7480
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -91,8 +97,17 @@ STATIC mp_obj_t displayio_fourwire_make_new(const mp_obj_type_t *type, size_t n_
9197
mp_raise_RuntimeError(translate("Too many display busses"));
9298
}
9399

100+
uint8_t polarity = args[ARG_polarity].u_int;
101+
if (polarity != 0 && polarity != 1) {
102+
mp_raise_ValueError(translate("Invalid polarity"));
103+
}
104+
uint8_t phase = args[ARG_phase].u_int;
105+
if (phase != 0 && phase != 1) {
106+
mp_raise_ValueError(translate("Invalid phase"));
107+
}
108+
94109
common_hal_displayio_fourwire_construct(self,
95-
MP_OBJ_TO_PTR(spi), command, chip_select, reset, args[ARG_baudrate].u_int);
110+
MP_OBJ_TO_PTR(spi), command, chip_select, reset, args[ARG_baudrate].u_int, polarity, phase);
96111
return self;
97112
}
98113

shared-bindings/displayio/FourWire.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ extern const mp_obj_type_t displayio_fourwire_type;
3838

3939
void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
4040
busio_spi_obj_t* spi, const mcu_pin_obj_t* command,
41-
const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset, uint32_t baudrate);
41+
const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset, uint32_t baudrate,
42+
uint8_t polarity, uint8_t phase);
4243

4344
void common_hal_displayio_fourwire_deinit(displayio_fourwire_obj_t* self);
4445

shared-module/displayio/FourWire.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040

4141
void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
4242
busio_spi_obj_t* spi, const mcu_pin_obj_t* command,
43-
const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset, uint32_t baudrate) {
43+
const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset, uint32_t baudrate,
44+
uint8_t polarity, uint8_t phase) {
4445

4546
self->bus = spi;
4647
common_hal_busio_spi_never_reset(self->bus);
@@ -49,8 +50,8 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
4950
gc_never_free(self->bus);
5051

5152
self->frequency = baudrate;
52-
self->polarity = 0;
53-
self->phase = 0;
53+
self->polarity = polarity;
54+
self->phase = phase;
5455

5556
common_hal_digitalio_digitalinout_construct(&self->command, command);
5657
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);

0 commit comments

Comments
 (0)